Beginners_-_Create_Sprite_from_array.c 3.0 KB

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