Forráskód Böngészése

Update gridrotate.c

Rudy Boudewijn van Etten 4 éve
szülő
commit
a101844478
1 módosított fájl, 34 hozzáadás és 35 törlés
  1. 34 35
      SpriteEditor/gridrotate.c

+ 34 - 35
SpriteEditor/gridrotate.c

@@ -1,16 +1,11 @@
-//
-// Loses pixels in the progress. Trying to learn/do the reverse rotation.
-//
-
 
 #include "raylib.h"
-#include <stdlib.h>           // Required for: free()
+#include <math.h>
 
 //
-// How to draw into a image and then read the image data and use that. 
 //
-float angle=0;
-float reverseangle=0;
+float angle=0; // radians
+int debug=0;
 int main(void)
 {
     // Initialization
@@ -20,23 +15,15 @@ int main(void)
 
     InitWindow(screenWidth, screenHeight, "raylib example.");
 
-    // Create a Perlin Image
-    Image tempImage = GenImageColor(32,32,BLACK); 
-    ImageDrawRectangleV(&tempImage,(Vector2){16-5,16-5},(Vector2){10,10},RED);
-    Color *mapPixels = GetImageData(tempImage);
-
-    // We will put the image data from the rectangle in this map.
+    // Put some tiles in the map..
     int map[32][32] = {0};
     
-    for (int y = 0; y < tempImage.height; y++)
+    for (int y = 10; y < 32-10; y++)
     {
-        for (int x = 0; x < tempImage.width; x++)
+        for (int x = 10; x < 32-10; x++)
         {
-            if ((mapPixels[y*tempImage.width + x].r != 0)){       // Collision: if other than 0.
-            map[x][y] = 1;
-            }
-        }
-    }
+            map[x][y]=1;
+    }}
 
  
     SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
@@ -49,48 +36,60 @@ int main(void)
         //----------------------------------------------------------------------------------
         if(IsKeyDown(KEY_RIGHT)){
             angle+=0.1;
-            reverseangle=angle+3.142;
         }
         if(IsKeyDown(KEY_LEFT)){
             angle-=0.1;
-            reverseangle=angle+3.142;
         }
-
         //----------------------------------------------------------------------------------
         // Draw
         //----------------------------------------------------------------------------------
         BeginDrawing();
-            //angle+=.1;
             ClearBackground(RAYWHITE);
 
-            int temp[32][32]={0};    
+            int rotated[32][32]={0};    
             for(int y=0;y<32;y++){
                 for(int x=0;x<32;x++){
-                    float x2=(cos(angle)*(x-16) - sin(angle)*(y-16));
-                    float y2=(sin(angle)*(x-16) + cos(angle)*(y-16));
-                    //if(map[x+16][y+16]==1)DrawRectangle(x2*16+320,y2*8+240,16,8,RED);
-                    x2+=16;
-                    y2+=16;
+                    int x2=floor((cos(angle)*(x-16) - sin(angle)*(y-16)))+16;
+                    int y2=floor((sin(angle)*(x-16) + cos(angle)*(y-16)))+16;
                     if(x2>-1 && y2>-1 && x2<32 && y2<32){
-                    temp[(int)x2][(int)y2]=map[x][y];
+                        rotated[x][y]=map[x2][y2];
                     }
                 }
             }
+            // interpolate
+            for(int y=0;y<32;y++){
+                for(int x=0;x<32;x++){
+                    if(rotated[x][y]==0){
+                        int cnt=0;
+                        for(int y2=-1;y2<2;y2++){
+                        for(int x2=-1;x2<2;x2++){
+                            if(x2+x<0 || x2+x>31 || y2+y<0 || y2+y >31)continue;
+                                if(rotated[x2+x][y2+y]==1)cnt++;                                
+                        }}
+                        
+                        if(cnt>3){
+                //            rotated[x][y]=1;
+                //            debug = 99;
+                        }
+                    }
+                    
+            }}
+            
             for(int y=0;y<32;y++){
                 for(int x=0;x<32;x++){
-                    if(temp[x][y]!=0){
+                    if(rotated[x][y]!=0){
                         DrawRectangle(x*16,y*8,16,8,RED);
                     }
             }}            
 
+            DrawText(FormatText("%i",debug),0,0,30,BLACK);
+
         EndDrawing();
         //----------------------------------------------------------------------------------
     }
 
     // De-Initialization
     //--------------------------------------------------------------------------------------
-    free(mapPixels);            // Unload color array
-    UnloadImage(tempImage);             // Unload image from RAM
     CloseWindow();        // Close window and OpenGL context
     //--------------------------------------------------------------------------------------