Beginners_-_Sprite_RotationScaling.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. // Source rectangle (part of the texture to use for drawing)
  45. Rectangle sourceRec = { 0.0f, 0.0f, 32, 32 };
  46. // Origin of the texture (rotation/scale point), it's relative to destination rectangle size
  47. Vector2 origin = { 16, 16 };
  48. float rotation=0.0f;
  49. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  50. //--------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. rotation+=1;
  57. if(rotation>359)rotation=0;
  58. //----------------------------------------------------------------------------------
  59. // Draw
  60. //----------------------------------------------------------------------------------
  61. BeginDrawing();
  62. ClearBackground(RAYWHITE);
  63. DrawText("Sprite Creation and Rotation.", 100, 180, 40, LIGHTGRAY);
  64. DrawTexturePro(target.texture, sourceRec, (Rectangle){ GetMouseX(), GetMouseY(), 32, 32 }, origin, (float)rotation, WHITE);
  65. EndDrawing();
  66. //----------------------------------------------------------------------------------
  67. }
  68. // De-Initialization
  69. //--------------------------------------------------------------------------------------
  70. UnloadRenderTexture(target); // Unload render texture
  71. //--------------------------------------------------------------------------------------
  72. CloseWindow(); // Close window and OpenGL context
  73. //--------------------------------------------------------------------------------------
  74. return 0;
  75. }