gridrotate.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "raylib.h"
  2. #include <math.h>
  3. //
  4. //
  5. float angle=0; // radians
  6. int debug=0;
  7. int main(void)
  8. {
  9. // Initialization
  10. //--------------------------------------------------------------------------------------
  11. const int screenWidth = 800;
  12. const int screenHeight = 450;
  13. InitWindow(screenWidth, screenHeight, "raylib example.");
  14. // Put some tiles in the map..
  15. int map[32][32] = {0};
  16. for (int y = 10; y < 32-10; y++)
  17. {
  18. for (int x = 10; x < 32-10; x++)
  19. {
  20. map[x][y]=1;
  21. }}
  22. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  23. //--------------------------------------------------------------------------------------
  24. // Main game loop
  25. while (!WindowShouldClose()) // Detect window close button or ESC key
  26. {
  27. // Update
  28. //----------------------------------------------------------------------------------
  29. if(IsKeyDown(KEY_RIGHT)){
  30. angle+=0.1;
  31. }
  32. if(IsKeyDown(KEY_LEFT)){
  33. angle-=0.1;
  34. }
  35. //----------------------------------------------------------------------------------
  36. // Draw
  37. //----------------------------------------------------------------------------------
  38. BeginDrawing();
  39. ClearBackground(RAYWHITE);
  40. int rotated[32][32]={0};
  41. for(int y=0;y<32;y++){
  42. for(int x=0;x<32;x++){
  43. int x2=floor((cos(angle)*(x-16) - sin(angle)*(y-16)))+16;
  44. int y2=floor((sin(angle)*(x-16) + cos(angle)*(y-16)))+16;
  45. if(x2>-1 && y2>-1 && x2<32 && y2<32){
  46. rotated[x][y]=map[x2][y2];
  47. }
  48. }
  49. }
  50. // interpolate
  51. for(int y=0;y<32;y++){
  52. for(int x=0;x<32;x++){
  53. if(rotated[x][y]==0){
  54. int cnt=0;
  55. for(int y2=-1;y2<2;y2++){
  56. for(int x2=-1;x2<2;x2++){
  57. if(x2+x<0 || x2+x>31 || y2+y<0 || y2+y >31)continue;
  58. if(rotated[x2+x][y2+y]==1)cnt++;
  59. }}
  60. if(cnt>3){
  61. // rotated[x][y]=1;
  62. // debug = 99;
  63. }
  64. }
  65. }}
  66. for(int y=0;y<32;y++){
  67. for(int x=0;x<32;x++){
  68. if(rotated[x][y]!=0){
  69. DrawRectangle(x*16,y*8,16,8,RED);
  70. }
  71. }}
  72. DrawText(FormatText("%i",debug),0,0,30,BLACK);
  73. EndDrawing();
  74. //----------------------------------------------------------------------------------
  75. }
  76. // De-Initialization
  77. //--------------------------------------------------------------------------------------
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }