#include #include #include #include "src/blockmatrix.h" const int SCREEN_WIDTH = BM_BLOCK_PIX_W*BM_WIDTH; const int SCREEN_HEIGHT = BM_BLOCK_PIX_H*BM_HEIGHT; int main(int argc, char **argv) { int i = 0, j = 0, counter = 0, score = 0; bool pause = true; Uint32 start = 0; SDL_Surface *screen, //This pointer will reference the backbuffer *temp; SDL_Event keyevent; BlockMatrix game_matrix; int running = 1, charxvel = 0, charyvel = 0; //We must first initialize the SDL video component, and check for success if (SDL_Init(SDL_INIT_VIDEO) != 0) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } game_matrix.gameboard = SDL_SetVideoMode(BM_BLOCK_PIX_W*BM_WIDTH, BM_BLOCK_PIX_H*BM_HEIGHT, SCREEN_BPP, SDL_SWSURFACE); if (game_matrix.gameboard == NULL) { printf("Unable to set video mode: %s\n", SDL_GetError()); exit(EXIT_FAILURE); } //When this program exits, SDL_Quit must be called atexit(SDL_Quit); //Set the video mode to fullscreen 640x480 with 16bit colour and double-buffering screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_DOUBLEBUF | SDL_ANYFORMAT); if (screen == NULL) { printf("Unable to set video mode: %s\n", SDL_GetError()); return 1; } //Construct the destination rectangle for our blit temp = SDL_LoadBMP("block_red.bmp"); game_matrix.red_block = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); temp = SDL_LoadBMP("block_blk.bmp"); game_matrix.blk_block = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); temp = SDL_LoadBMP("block_grn.bmp"); game_matrix.grn_block = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); temp = SDL_LoadBMP("block_ylw.bmp"); game_matrix.ylw_block = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); temp = SDL_LoadBMP("block_blu.bmp"); game_matrix.blu_block = SDL_DisplayFormat(temp); SDL_FreeSurface(temp); game_matrix.AddNewBlocks(); SDL_BlitSurface(game_matrix.gameboard, NULL, screen, NULL); SDL_Flip(screen); start = SDL_GetTicks(); pause = false; //Keep looping until the user closes the SDL window while(running) { //Get the next event from the stack while(SDL_PollEvent(&keyevent)) { switch(keyevent.type){ case SDL_KEYDOWN: switch(keyevent.key.keysym.sym){ case SDLK_LEFT: if(!pause && game_matrix.MoveBlocks(BM_DIRECTION_LEFT)) { ;} break; case SDLK_RIGHT: if(!pause && game_matrix.MoveBlocks(BM_DIRECTION_RIGHT)) { ; } break; case SDLK_UP: // if(!pause && game_matrix.MoveBlocks(BM_DIRECTION_UP)) { ; } break; case SDLK_DOWN: if(!pause && game_matrix.MoveBlocks(BM_DIRECTION_DOWN)) { ; } break; case SDLK_SPACE: if(!pause && game_matrix.RotateBlocks()) { ; } break; case SDLK_p: if(pause == false) pause = true; else if(pause == true) pause = false; start = SDL_GetTicks()-start; break; default: break; } break; case SDL_KEYUP: break; case SDL_QUIT: //The user has closed the SDL window running = 0; break; } } if(!pause && SDL_GetTicks()-start > 500) { if(game_matrix.MoveBlocks(BM_DIRECTION_DOWN)) { for(i = BM_HEIGHT-1; i >= 0; i--) if(game_matrix.RowIsFull(i)) { game_matrix.DeleteFullRow(i); score++; } SDL_Delay(100); if(game_matrix.AddNewBlocks()) { fprintf(stderr, "YOUR SCORE: %i\n", score); pause = true; } } start = SDL_GetTicks(); } if(!pause) { SDL_BlitSurface(game_matrix.GetGameBoard(), NULL, screen, NULL); SDL_Flip(screen); } } //Return success! return 0; }