1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
/*Tic tac toe
*************
Summer 2011
/************************************************************************
********************************Headers**********************************
************************************************************************/
#include "SDL.h"
#include "SDL_image.h"
#include "Board.h"
#include <string>
//prototypes
void ApplySurface(int x, int y, SDL_Surface *source, SDL_Surface *destination );
/************************************************************************
********************************Globals**********************************
************************************************************************/
// Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
// Surfaces that will be used
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *X = NULL;
//The event structure that will be used
SDL_Event event;
/************************************************************************
********************************Classes**********************************
************************************************************************/
class Board
{
public:
SDL_Rect box[ 3 ][ 3 ];
Board();
void HandleEvents();
private:
//bool IsFree(SDL_Rect);
void SetClips();
};
void Board::SetClips()
{
//Clip the board into 9 squares
box[ 0 ][ 0 ].x = 0;
box[ 0 ][ 0 ].y = 0;
box[ 0 ][ 0 ].w = 213;
box[ 0 ][ 0 ].h = 160;
box[ 0 ][ 1 ].x = 213;
box[ 0 ][ 1 ].y = 0;
box[ 0 ][ 1 ].w = 213;
box[ 0 ][ 1 ].h = 160;
box[ 0 ][ 2 ].x = 426;
box[ 0 ][ 2 ].y = 0;
box[ 0 ][ 2 ].w = 213;
box[ 0 ][ 2 ].h = 160;
box[ 1 ][ 0 ].x = 0;
box[ 1 ][ 0 ].y = 160;
box[ 1 ][ 0 ].w = 213;
box[ 1 ][ 0 ].h = 160;
box[ 1 ][ 1 ].x = 213;
box[ 1 ][ 1 ].y = 160;
box[ 1 ][ 1 ].w = 213;
box[ 1 ][ 1 ].h = 160;
box[ 1 ][ 2 ].x = 426;
box[ 1 ][ 2 ].y = 160;
box[ 1 ][ 2 ].w = 213;
box[ 1 ][ 2 ].h = 160;
box[ 2 ][ 0 ].x = 0;
box[ 2 ][ 0 ].y = 320;
box[ 2 ][ 0 ].w = 213;
box[ 2 ][ 0 ].h = 160;
box[ 2 ][ 1 ].x = 213;
box[ 2 ][ 1 ].y = 320;
box[ 2 ][ 1 ].w = 213;
box[ 2 ][ 1 ].h = 160;
box[ 2 ][ 2 ].x = 426;
box[ 2 ][ 2 ].y = 320;
box[ 2 ][ 2 ].w = 213;
box[ 2 ][ 2 ].h = 160;
}
void Board::HandleEvents()
{
//The mouse coords
int x = 0;
int y = 0;
//If the user clicks a place on the board
if( event.type == SDL_MOUSEBUTTONDOWN )
{
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse coords
x = event.button.x;
y = event.button.y;
//find the box the mouse was clicked in
// mouse was clicked in right column
if( x > 426 )
{
// Mouse was click in top right
if( y > 0 && y < 160)
{
ApplySurface(box[ 0 ][ 0 ].x, box[ 0 ][ 0 ].y, X, screen );
}
// Mouse was click in center right
else if( y > 160 && y < 320)
{
}
// Mouse was click in bottom right
else if( y > 320)
{
}
}
// Mouse click in center column
else if( x > 213 && x < 426 )
{
// Mouse was click in top center
if( y > 0 && y < 160)
{
}
// Mouse was click in center center
else if( y > 160 && y < 320)
{
}
// Mouse was click in center bottom
else if( y > 320)
{
}
}
// Mouse clicked in left column
else if( x > 0 && x < 213 )
{
// Mouse was click in top left
if( y > 0 && y < 160)
{
}
// Mouse was click in center left
else if( y > 160 && y < 320)
{
}
// Mouse was click in center left
else if( y > 320)
{
}
}
}
}
}
/************************************************************************
********************************Functions********************************
************************************************************************/
//Switch the format of the image to the same format as the screen
SDL_Surface *LoadImage( std::string filename )
{
SDL_Surface* loadedImage = NULL; // Temporary storage for the image that's loaded
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP( filename.c_str() ); // Load the image in
//If nothing went wrong in loading the image
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
}
return optimizedImage;
}
//Applys the surface to the screen
void ApplySurface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Make a temporary rectangle to hold the coordinates
SDL_Rect coord;
//Give the coordinates to the rectangle
coord.x = x;
coord.y = y;
//Draw the image
SDL_BlitSurface( source, NULL, destination, &coord );
}
bool Init()
{
if( SDL_Init( SDL_INIT_VIDEO ) == -1 )
return 1;
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
SDL_WM_SetCaption( "Tic-Tac-Toe", NULL );
//If everything initialized fine
return 0;
}
void CleanUp()
{
//Free the surfaces
SDL_FreeSurface( background );
SDL_FreeSurface( X );
SDL_Quit();
}
/************************************************************************
********************************Main************************************
************************************************************************/
int main( int argc, char* args[] )
{
//quit flag
bool gameDone = false;
if( Init() == 1 )
return 1;
//Load the images
background = LoadImage( "background.bmp" );
X = LoadImage( "x.bmp" );
// Draw the board
ApplySurface( 0, 0, background, screen );
//Update the screen
if( SDL_Flip( screen ) == -1 )
return 1;
Board gameBoard;
while( !gameDone )
{
while ( SDL_PollEvent( &event ) )
{
if( event.type == SDL_MOUSEBUTTONDOWN )
{
//Handle user events
gameBoard.HandleEvents();
//Handle the comps evets
}
if( event.type == SDL_QUIT)
{
gameDone = true;
}
}
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
return 1;
CleanUp();
return 0;
}
|