Advertisement
Guest User

Untitled

a guest
Sep 26th, 2011
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. ============================= board.hpp =======================================
  2. extern SDL_Surface* screen;
  3. class board {
  4. private:
  5.     int brd[3][3]; /* brd contains the blocks. 0 is empty, 1s are Xs and 2s are Os */
  6.     int brdLocs[3][3][2];
  7.     /* brdLocs stores the positions where the marks ( X or O ) are drawn. */
  8.     SDL_Surface* brdImg;
  9.     SDL_Surface* XImg;
  10.     SDL_Surface* OImg;
  11.    
  12. public:
  13.     board(void);
  14.     void assignBlock(int x, int y, int type);
  15.     void drawBoard(void);
  16.     void drawBlocks(void);
  17. };
  18.  
  19. board::board(){
  20.     brdImg = SDL_DisplayFormat(IMG_Load("board.png"));
  21.     XImg = SDL_DisplayFormat(IMG_Load("x.png"));
  22.     OImg = SDL_DisplayFormat(IMG_Load("o.png"));
  23.     brdLocs =  { { {28,32}, {112,33}, {220,51} }, { {20,122}, {115,131}, {222,144 } }, { {13,210}, {111,221}, {227,238} } };
  24.     if( brdImg == NULL or XImg == NULL or OImg == NULL){
  25.         fprintf(stderr,"Failed to load one or more GFX files. Please make sure they are all in the same folder as this executable\n");
  26.     }
  27. }
  28.  
  29. void board::assignBlock(int x, int y, int type){
  30.     if ( brd[x-1][y-1] == 0){
  31.         brd[x-1][y-1] = type;
  32.     //  return true;
  33.     } //else {
  34.         //return false;
  35.     //}
  36. }
  37.  
  38. void board::drawBoard(void){
  39.    
  40. }
  41.  
  42. void board::drawBlocks(void){
  43.    
  44. }
  45. ============================ End board.hpp ==========================================
  46.  
  47.  
  48.  
  49. ============================ main.cpp ================================================
  50. #include <SDL/SDL.h>
  51. #include <SDL/SDL_image.h>
  52. #include "board.hpp"
  53.  
  54. SDL_Surface* screen;
  55.  
  56. SDL_Event event;
  57.  
  58. int player;
  59.  
  60. void player_change (){
  61.     if (player == 1) {
  62.         player = 2;
  63.     } else if (player == 2) {
  64.         player = 1;
  65.     } else {
  66.      player = 1; // This should never happen.. really!
  67.     }
  68. }
  69.  
  70. int main(void){
  71.     SDL_Init(SDL_INIT_EVERYTHING);
  72.     screen = SDL_SetVideoMode( 320, 320, 32, SDL_SWSURFACE );
  73.     board brd;
  74.     int mousex;
  75.     int mousey;
  76.     bool gameRunning = true;
  77.     while(gameRunning){
  78.         while(SDL_PollEvent(&event)){
  79.             if(event.type == SDL_MOUSEBUTTONDOWN){
  80.                 SDL_GetMouseState( &mousex, &mousey );
  81.                 if(mousex <= 106){
  82.                     if (mousey <= 106) {
  83.                         brd.assignBlock(1,1,player);
  84.                         player_change();
  85.                     } else if ( mousey > 106 && mousey <= 212 ){
  86.                         brd.assignBlock(1,2,player);
  87.                         player_change();
  88.                     } else if ( mousey > 106 && mousey <= 320 ){
  89.                         brd.assignBlock(1,3,player);
  90.                         player_change();
  91.                     }
  92.                 } else if ( mousex > 106 && mousex <= 212 ){
  93.                     if (mousey <= 106) {
  94.                         brd.assignBlock(2,1,player);
  95.                         player_change();
  96.                     } else if ( mousey > 106 && mousey <= 212 ){
  97.                         brd.assignBlock(2,2,player);
  98.                         player_change();
  99.                     } else if ( mousey > 106 && mousey <= 320 ){
  100.                         brd.assignBlock(2,3,player);
  101.                         player_change();
  102.                     }
  103.                 } else if ( mousex > 212 && mousex <= 320 ){
  104.                     if (mousey <= 106) {
  105.                         brd.assignBlock(3,1,player);
  106.                         player_change();
  107.                     } else if ( mousey > 106 && mousey <= 212 ){
  108.                         brd.assignBlock(3,2,player);
  109.                         player_change();
  110.                     } else if ( mousey > 106 && mousey <= 320 ){
  111.                         brd.assignBlock(3,3,player);
  112.                         player_change();
  113.                     }
  114.                 }
  115.             } else if (event.type == SDL_QUIT) {
  116.                 gameRunning = false;
  117.             }
  118.         }
  119.        
  120.         SDL_Flip( screen );
  121.     }
  122.     SDL_Quit();
  123.     return 0;
  124. }
  125. ==================================== end main.cpp =========================================
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement