repo: symmetry
action: blob
revision: 
path_from: game.c
revision_from: refs/heads/trunk:
path_to: 
revision_to: 
git.thebackupbox.net
symmetry
git clone git://git.thebackupbox.net/symmetry

blob of:

symmetry

/ game.c

blob_plain of this file

refs/heads/trunk:/game.c

 #include 
 #include 

 #if __EMSCRIPTEN__
 #include 
 #include 
 //#include 
 #else
 #include 
 #include 
 #endif

 #define SCREEN_WIDTH  256
 #define SCREEN_HEIGHT 256

 SDL_Window *window = NULL;
 SDL_Renderer *renderer = NULL;

 #define MAX(a,b) ((a) > (b) ? a : b)
 #define MIN(a,b) ((a) < (b) ? a : b)

 int posX=0;
 int posY=0;
 int sizeW=16;
 int sizeH=16;

 static int quit = 0;

 int mousex;
 int mousey;

 int drag_state=0;//0 for no drag, 1 for mouse-down, 2 for mouse-up and not dealt with yet.

 int start_cell=-1;

 char animating=0;

 #define BOARDW 16
 #define BOARDH 16

 unsigned char board[BOARDW * BOARDH];

 int screen_xy_to_cell(int x,int y) {
     return BOARDW * ((y - (y % sizeH)) / sizeH) + ((x - (x % sizeW)) / sizeW);
 }

 int cell_to_x(int c) {
     return c % BOARDW;
 }

 int cell_to_y(int c) {
     return c / BOARDH;
 }

 int xy_to_cell(int x,int y) {
     return y * BOARDW + x;
 }

 void render()
 {
     int min_x,min_y,max_x,max_y;
     int cell_x,cell_y;
     char anim;
     SDL_Rect cursor;

     SDL_Rect cell;

     int cur_cell=screen_xy_to_cell(mousex,mousey);

     SDL_SetRenderDrawColor( renderer, 0x00, 0x00, 0x00, 0xFF );//this is to black-out the screen.
     SDL_RenderClear(renderer);
     int i;
     for(i=0;i>3);
       cell.x=((i % BOARDW) * 16) + 1;
       cell.y=((i / BOARDH) * 16) + 1 - anim;
       cell.w=14;
       cell.h=14;
       // 3 bit color
       SDL_SetRenderDrawColor(renderer, (board[i] & 1) * 0xff, (board[i]>>1 & 1) * 0xff, (board[i]>>2 & 1) * 0xff, 0x00);
 //      SDL_RenderFillRect(renderer, &cell);
       SDL_RenderDrawRect(renderer, &cell);
 #define min(a,b) ((a>b)?b:a)
 #define max(a,b) ((a= min_x && cell_x <= max_x && cell_y >= min_y && cell_y <= max_y) {//inside of the rectangle...
           cell.x++;
           cell.y++;
           cell.w-=2;
           cell.h-=2;
           SDL_RenderDrawRect(renderer, &cell);
         }
       }
     }
     cursor.x = mousex - (mousex % sizeW);
     cursor.y = mousey - (mousey % sizeH);
     cursor.w = sizeW;
     cursor.h = sizeH;
     SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0x00, 0x00);//this is yellow
     SDL_RenderDrawRect(renderer, &cursor);

     //
     //cursor.x = mousex;
     //cursor.y = mousey;
     //cursor.w = sizeW;
     //cursor.h = sizeH;
     //draw the cursor here.
     //SDL_SetRenderDrawColor(renderer, 0xff, 0xff, 0x00, 0x00);//this is yellow
     //SDL_RenderDrawRect(renderer, &cursor);
     //SDL_RenderPresent(renderer);

 /* don't really need a software cursor atm.
     SDL_Vertex vertices[] = {
        {{mousex,mousey},{255,0,0,255},{1,1}},
        {{mousex+4,mousey+10},{255,0,0,255},{1,1}},
        {{mousex+10,mousey+4},{255,0,0,255},{1,1}}
     };
     SDL_RenderGeometry(renderer, 0, vertices, 3, 0, 0);
 */
     SDL_RenderPresent(renderer);
 }


 #if __EMSCRIPTEN__
 void main_tick() {
 #else
 int main_tick() {
 #endif
     char anim;
     int i,j;
     int min_x,min_y,max_x,max_y;
     int cell_x,cell_y;
     int cur_cell=-1;
     SDL_Event event;
     while (SDL_PollEvent(&event)) {
         switch (event.type) {
             case SDL_QUIT:
                 quit = 1;
                 break;
             case SDL_MOUSEMOTION:
 //                if(animating) break;
                 mousex=event.motion.x;
                 mousey=event.motion.y;
                 break;
             case SDL_MOUSEBUTTONDOWN:
                 if(animating) break;
                 drag_state=1;
                 start_cell=screen_xy_to_cell(event.button.x,event.button.y);
                 break;
             case SDL_MOUSEBUTTONUP:
                 if(animating) break;
                 if(start_cell == -1) break;
                 animating = 1;
                 // let's explode all of these cells.
                 board[start_cell]=0;//blow up just this cell for now...
                 //how to do an animation of the cells above falling down?
                 cur_cell=screen_xy_to_cell(event.button.x,event.button.y);
                 // we don't want to move all of them now, just set them to empty.
                 min_x=min(cell_to_x(start_cell),cell_to_x(cur_cell));
                 min_y=min(cell_to_y(start_cell),cell_to_y(cur_cell));
                 max_x=max(cell_to_x(start_cell),cell_to_x(cur_cell));
                 max_y=max(cell_to_y(start_cell),cell_to_y(cur_cell));
                 //for(i=xy_to_cell(min_x,min_y);i<=xy_to_cell(max_x,max_y);i++) { // let's compare for symmetry first?
                 //  ;
                 //}
                 for(i=xy_to_cell(min_x,min_y);i<=xy_to_cell(max_x,max_y);i++) {//this can be optimized to not try the cell outside of the rectangle
                   cell_x=cell_to_x(i);
                   cell_y=cell_to_y(i);
                   if(cell_x >= min_x && cell_x <= max_x && cell_y >= min_y && cell_y <= max_y) {//inside of the rectangle...
                      board[i]=0;
                   }
                 }
                 start_cell=-1;
                 break;
             default:
                 //nothing :D
                 break;
         }
     }
     if(animating) { // look for empty cells.
         animating=0;
         for(i=0;i<(BOARDW * BOARDH);i++) {
             if(board[i] & 0xf8) { // check if we are animating
                 animating=1;
                 board[i] = (((board[i] >> 3) - 1) << 3) | (board[i] & 0x7); // decrement just the animation by 1
                 continue; // if we animated, let's wait for the next time this loops.
             }
             if((board[i] & 0x7) == 0) { // only checking the color portion. black is empty.
                 animating=1; // we still have stuff to move around.
                 //anim = (board[i] >> 3);
                 if((i-BOARDW) < 0) {
                      while(!(board[i]=rand()%7)); // make it be an actual block :P
                 }
                 else if((board[i-BOARDW] >> 3) == 0) { // if there is a block above, that isn't animating, we set current cell to that and above cell to empty
                      board[i]=board[i-BOARDW];// should be getting set to the color above it...
                      board[i-BOARDW]=0;
                 }
                 if(board[i]) {
                     board[i] |= (16 << 3); // set animation to 16 and it'll get decremented each time we loop.
                 }
             }
         }
     }
     render();
     SDL_UpdateWindowSurface(window);
     SDL_Delay(15);

 #if !__EMSCRIPTEN__
     return 0;
 #endif
 }

 void main_loop()
 {
     int i=0;
     for(i=0;i