2
0

grid_rotate_interpolate.c 3.4 KB

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