2
0

Beginners_-_Create_Sprite_from_array.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "raylib.h"
  2. int main(void)
  3. {
  4. // Initialization
  5. //--------------------------------------------------------------------------------------
  6. const int screenWidth = 800;
  7. const int screenHeight = 450;
  8. InitWindow(screenWidth, screenHeight, "raylib example.");
  9. // Create a Image in memory
  10. RenderTexture2D target = LoadRenderTexture(32, 32);
  11. int sprite[8][8] = {{1,1,1,1,0,0,0,0},
  12. {0,1,1,1,0,0,0,0},
  13. {0,0,1,1,1,2,2,2},
  14. {0,0,0,1,1,2,2,2},
  15. {0,0,0,1,1,2,2,2},
  16. {1,1,1,1,1,2,2,2},
  17. {0,0,1,1,1,2,2,2},
  18. {0,0,0,0,0,2,2,2}};
  19. // Clear our texture(image) before entering the game loop
  20. BeginTextureMode(target);
  21. ClearBackground(BLANK); // Make the entire Sprite Transparent.
  22. EndTextureMode();
  23. // Draw something on it.
  24. for (int y=0;y<8;y++)
  25. {
  26. for (int x=0;x<8; x++)
  27. {
  28. // Our sprite color 1
  29. if (sprite[y][x]==1)
  30. {
  31. BeginTextureMode(target);
  32. DrawRectangle(x*4,y*4,4,4,RED);
  33. EndTextureMode();
  34. }
  35. // Our sprite color 2
  36. if (sprite[y][x]==2)
  37. {
  38. BeginTextureMode(target);
  39. DrawRectangle(x*4,y*4,4,8,BLACK);
  40. EndTextureMode();
  41. }
  42. }
  43. }
  44. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  45. //--------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. Vector2 mousePos = GetMousePosition();
  52. //----------------------------------------------------------------------------------
  53. // Draw
  54. //----------------------------------------------------------------------------------
  55. BeginDrawing();
  56. ClearBackground(RAYWHITE);
  57. DrawText("Sprite Creation from data.", 100, 180, 40, LIGHTGRAY);
  58. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  59. DrawTexture(target.texture,mousePos.x, mousePos.y, WHITE);
  60. EndDrawing();
  61. //----------------------------------------------------------------------------------
  62. }
  63. // De-Initialization
  64. //--------------------------------------------------------------------------------------
  65. UnloadRenderTexture(target); // Unload render texture
  66. //--------------------------------------------------------------------------------------
  67. CloseWindow(); // Close window and OpenGL context
  68. //--------------------------------------------------------------------------------------
  69. return 0;
  70. }