============================= board.hpp =======================================
extern SDL_Surface* screen;
class board {
private:
int brd[3][3]; /* brd contains the blocks. 0 is empty, 1s are Xs and 2s are Os */
int brdLocs[3][3][2];
/* brdLocs stores the positions where the marks ( X or O ) are drawn. */
SDL_Surface* brdImg;
SDL_Surface* XImg;
SDL_Surface* OImg;
public:
board(void);
void assignBlock(int x, int y, int type);
void drawBoard(void);
void drawBlocks(void);
};
board::board(){
brdImg = SDL_DisplayFormat(IMG_Load("board.png"));
XImg = SDL_DisplayFormat(IMG_Load("x.png"));
OImg = SDL_DisplayFormat(IMG_Load("o.png"));
brdLocs = { { {28,32}, {112,33}, {220,51} }, { {20,122}, {115,131}, {222,144 } }, { {13,210}, {111,221}, {227,238} } };
if( brdImg == NULL or XImg == NULL or OImg == NULL){
fprintf(stderr,"Failed to load one or more GFX files. Please make sure they are all in the same folder as this executable\n");
}
}
void board::assignBlock(int x, int y, int type){
if ( brd[x-1][y-1] == 0){
brd[x-1][y-1] = type;
// return true;
} //else {
//return false;
//}
}
void board::drawBoard(void){
}
void board::drawBlocks(void){
}
============================ End board.hpp ==========================================
============================ main.cpp ================================================
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "board.hpp"
SDL_Surface* screen;
SDL_Event event;
int player;
void player_change (){
if (player == 1) {
player = 2;
} else if (player == 2) {
player = 1;
} else {
player = 1; // This should never happen.. really!
}
}
int main(void){
SDL_Init(SDL_INIT_EVERYTHING);
screen = SDL_SetVideoMode( 320, 320, 32, SDL_SWSURFACE );
board brd;
int mousex;
int mousey;
bool gameRunning = true;
while(gameRunning){
while(SDL_PollEvent(&event)){
if(event.type == SDL_MOUSEBUTTONDOWN){
SDL_GetMouseState( &mousex, &mousey );
if(mousex <= 106){
if (mousey <= 106) {
brd.assignBlock(1,1,player);
player_change();
} else if ( mousey > 106 && mousey <= 212 ){
brd.assignBlock(1,2,player);
player_change();
} else if ( mousey > 106 && mousey <= 320 ){
brd.assignBlock(1,3,player);
player_change();
}
} else if ( mousex > 106 && mousex <= 212 ){
if (mousey <= 106) {
brd.assignBlock(2,1,player);
player_change();
} else if ( mousey > 106 && mousey <= 212 ){
brd.assignBlock(2,2,player);
player_change();
} else if ( mousey > 106 && mousey <= 320 ){
brd.assignBlock(2,3,player);
player_change();
}
} else if ( mousex > 212 && mousex <= 320 ){
if (mousey <= 106) {
brd.assignBlock(3,1,player);
player_change();
} else if ( mousey > 106 && mousey <= 212 ){
brd.assignBlock(3,2,player);
player_change();
} else if ( mousey > 106 && mousey <= 320 ){
brd.assignBlock(3,3,player);
player_change();
}
}
} else if (event.type == SDL_QUIT) {
gameRunning = false;
}
}
SDL_Flip( screen );
}
SDL_Quit();
return 0;
}
==================================== end main.cpp =========================================