Hello, I'm learning SDL and am trying to make Tic Tac Toe. I know how to check if the Mouse button goes down, but how can I check if its one of the 9 hot spots? Heres my code so far.
#include <iostream>
#include <SDL/SDL.h>
SDL_Surface *back;
SDL_Surface *x;
SDL_Surface *o;
SDL_Surface *screen;
int cell[8];
int InitImages() {
back = SDL_LoadBMP("Back.bmp");
x = SDL_LoadBMP("x.bmp");
o = SDL_LoadBMP("o.bmp");
return 0;
}
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect, SDL_Surface *dst, SDL_Rect *dstrect);
void DrawIMG(SDL_Surface *img, int x, int y) {
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}
void drawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2) {
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_Rect dest2;
dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}
void checkcell(mousex, mousey) {
if(mousex < 317 && mousey < 297)
void drawbg() {
DrawIMG(back, 0, 0);
}
void drawx(int v, int z, int y, int x2, int y2) {
if(cell[v] == 0) {
cell[v] = 1;
drawIMG(x, z, y, 155, 147, x2, y2);
}
else
std::cout<<"Cannot put x there."<<std::endl;
}
void drawo(int v, int x, int y, int x2, int y2) {
if(cell[v] == 0) {
drawIMG(o, x, y, 155, 147, x2, y2);
cell[v] = 1;
}
else
std::cout<<"Cannot put o there."<<std::endl;
}
int main(int argc, char* argv[]) {
if(SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO)<0) {
std::cout<<"Unable to init SDL."<<std::endl;
SDL_GetError();
}
InitImages();
drawbg();
int done=0;
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
}
return 0;
}
could it be something like:
if(SDL_MOUSEBUTTONDOWN == 1) {
if(mousex < 317 && mousey < 297) {
if(turn == 'x') {
drawIMG(x, 320, 150, 155, 147, 317, 297);
cell[0] = 1;
}
if(turn == 'y') {
drawIMG(y, 320, 150, 155, 147, 317, 297);
cell[0] = 1;
}
}
}
except i would need a way to find mouse position and set it as mouse x, and mouse y.
I dunno though.
Is there a way to do it so I dont have to hard code for all 9 cells?
edit: Yes I know I have really bad code design, but I will fix that later.
[Edited by - Meta Adam on January 13, 2005 8:45:01 PM]