Hello All,
I am having trouble. When I use the mouse event to click on a graphic rect, the event does not respond. When the user clicks on the graphics ball, points should be added, and the graphic should change to ‘pop’ graphic. I’ll post my code to see if anyone know’s what is up.
Code:
//Main h
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <time.h>
#include <wchar.h>
#include
#include
#include “SDL.h”
#include “SDL_image.h”
#include “SDL_mixer.h”
#include “SDL_ttf.h”
const int MAX_WIDTH = 640;
const int MAX_HEIGHT = 480;
const int MAX_DEPTH = 32;
const char* szTitle = “Shoot Ball - DarkScar Games”;
const int MAX_FPS = 30;
SDL_Surface* gScreen = NULL;
SDL_Surface* gLogo = NULL;
SDL_Surface* gTitle = NULL;
SDL_Surface* gBackdrop = NULL;
SDL_Rect LogoRect;
SDL_Rect TitleRect;
SDL_Rect BackdropRect;
SDL_Surface* gBall[5];
SDL_Rect BallRect[5];
SDL_Event event;
TTF_Font* Font = NULL;
Uint8* keyState = SDL_GetKeyState(NULL);
SDL_Color textColor = {255,255,0};
Mix_Music* backMusic = NULL;
Mix_Chunk* hit = NULL;
const int Red_Ball = 500;
const int Blue_Ball = 400;
const int Purple_Ball = 300;
const int Black_Ball = 200;
const int Green_Ball = 100;
const int Red_Points = 10;
const int Blue_Points = 20;
const int Purple_Points = 30;
const int Black_Points = 40;
const int Green_Points = 50;
int ball_x[5];
int ball_y[5];
int ball_w[5];
int ball_h[5];
long unsigned int points = 0;
unsigned int point_counter = 0;
SDL_Surface* gCrosshair = NULL;
SDL_Rect CrosshairRect;
SDL_Surface* gPop = NULL;
SDL_Rect PopRect;
SDL_Surface* gPoints = NULL;
SDL_Rect PointRect;
SDL_Surface* gButton[2];
SDL_Rect ButtonRect[2];
int button_x[2];
int button_y[2];
int button_w[2];
int button_h[2];
int cross_x;
int cross_y;
int cross_w;
int cross_h;
int pop_x;
int pop_y;
int pop_w;
int pop_h;
int mx = 0;
int my = 0;
bool bLogo = true;
bool bTitle = false;
bool bGame = false;
bool GameRunning = true;
long lastick;
int fpstickcounter;
int fpscounter;
int currentfps;
Code:
//Main.cpp
#include “main.h”
int main(int argc, char* argv[])
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return 1;
}
gScreen = SDL_SetVideoMode(MAX_WIDTH,MAX_HEIGHT,MAX_DEPTH,SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption(szTitle, NULL);
gLogo = IMG_Load("Logo.png");
LogoRect.x = 0;
LogoRect.y = 0;
LogoRect.w = MAX_WIDTH;
LogoRect.h = MAX_HEIGHT;
gTitle = IMG_Load("Title.png");
TitleRect.x = 0;
TitleRect.y = 0;
TitleRect.w = MAX_WIDTH;
TitleRect.h = MAX_HEIGHT;
gBackdrop = IMG_Load("Backdrop.png");
BackdropRect.x = 0;
BackdropRect.y = 0;
BackdropRect.w = MAX_WIDTH;
BackdropRect.h = MAX_HEIGHT;
cross_x = 320;
cross_y = 180;
cross_w = 64;
cross_h = 64;
gCrosshair = IMG_Load("Crosshair.png");
CrosshairRect.x = cross_x;
CrosshairRect.y = cross_y;
CrosshairRect.w = cross_w;
CrosshairRect.h = cross_h;
srand((unsigned)time(0));
ball_x[0] = 10;
ball_y[0] = 10;
ball_x[1] = 580;
ball_y[1] = 10;
ball_x[2] = 10;
ball_y[2] = 430;
ball_x[3] = 580;
ball_y[3] = 430;
ball_x[4] = 220;
ball_y[4] = 100;
ball_w[0] = 64;
ball_h[0] = 64;
ball_w[1] = 64;
ball_h[1] = 64;
ball_w[2] = 64;
ball_h[2] = 64;
ball_w[3] = 64;
ball_h[3] = 64;
ball_w[4] = 64;
ball_h[4] = 64;
gBall[0] = IMG_Load("RedBall.png");
BallRect[0].x = ball_x[0];
BallRect[0].y = ball_y[0];
BallRect[0].w = ball_w[0];
BallRect[0].h = ball_h[0];
gBall[1] = IMG_Load("BlueBall.png");
BallRect[1].x = ball_x[1];
BallRect[1].y = ball_y[1];
BallRect[1].w = ball_w[1];
BallRect[1].h = ball_h[1];
gBall[2] = IMG_Load("PurpleBall.png");
BallRect[2].x = ball_x[2];
BallRect[2].y = ball_y[2];
BallRect[2].w = ball_w[2];
BallRect[2].h = ball_h[2];
gBall[3] = IMG_Load("BlackBall.png");
BallRect[3].x = ball_x[3];
BallRect[3].y = ball_y[3];
BallRect[3].w = ball_w[3];
BallRect[3].h = ball_h[3];
gBall[4] = IMG_Load("GreenBall.png");
BallRect[4].x = ball_x[4];
BallRect[4].y = ball_y[4];
BallRect[4].w = ball_w[4];
BallRect[4].h = ball_h[4];
SDL_SetColorKey(gBall[0],SDL_SRCCOLORKEY,SDL_MapRGB(gBall[0]->format,255,255,255));
SDL_SetColorKey(gBall[1],SDL_SRCCOLORKEY,SDL_MapRGB(gBall[1]->format,255,255,255));
SDL_SetColorKey(gBall[2],SDL_SRCCOLORKEY,SDL_MapRGB(gBall[2]->format,255,255,255));
SDL_SetColorKey(gBall[3],SDL_SRCCOLORKEY,SDL_MapRGB(gBall[3]->format,255,255,255));
SDL_SetColorKey(gBall[4],SDL_SRCCOLORKEY,SDL_MapRGB(gBall[4]->format,255,255,255));
pop_x = 0;
pop_y = 0;
pop_w = 64;
pop_h = 64;
gPop = IMG_Load("Pop.png");
PopRect.x = pop_x;
PopRect.y = pop_y;
PopRect.w = pop_w;
PopRect.h = pop_h;
SDL_SetColorKey(gPop,SDL_SRCCOLORKEY,SDL_MapRGB(gPop->format,255,255,255));
button_x[0] = 210;
button_x[1] = 210;
button_y[0] = 150;
button_y[1] = 200;
button_w[0] = 200;
button_h[0] = 30;
button_w[1] = 200;
button_h[1] = 30;
gButton[0] = IMG_Load("NewGameButton.png");
ButtonRect[0].x = button_x[0];
ButtonRect[0].y = button_y[0];
ButtonRect[0].w = button_w[0];
ButtonRect[0].h = button_h[0];
gButton[1] = IMG_Load("ExitGameButton.png");
ButtonRect[1].x = button_x[1];
ButtonRect[1].y = button_y[1];
ButtonRect[1].w = button_w[1];
ButtonRect[1].h = button_h[1];
gPoints = TTF_RenderText_Solid(Font,"Points:",textColor);
PointRect.x = 1;
PointRect.y = 1;
PointRect.w = 0;
PointRect.h = 0;
lastick = 0;
fpstickcounter = 0;
fpscounter = 0;
currentfps = 0;
if(TTF_Init() == -1)
{
return 1;
}
Font = TTF_OpenFont("Arial.ttf",20);
if(Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096) == -1)
{
return 1;
}
hit = Mix_LoadWAV("hit.mid");
while(GameRunning)
{
lastick = SDL_GetTicks();
long Elapsedticks = SDL_GetTicks() - lastick;
fpscounter += Elapsedticks;
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
GameRunning = false;
}
if(event.type == SDL_MOUSEMOTION)
{
mx = event.motion.x;
my = event.motion.y;
}
if(event.type == SDL_MOUSEBUTTONDOWN)
{
if(event.button.button == SDL_BUTTON_LEFT)
{
mx = event.button.x;
my = event.button.y;
if(mx > ButtonRect[0].x && mx < ButtonRect[0].x + ButtonRect[0].w && my > ButtonRect[0].y && my < ButtonRect[0].y + ButtonRect[0].h && bTitle == true)
{
bTitle = false;
bGame = true;
}
else if(mx > ButtonRect[1].x && mx < ButtonRect[1].x + ButtonRect[1].w && my > ButtonRect[1].y && my < ButtonRect[1].y + ButtonRect[1].h && bTitle == true)
{
GameRunning = false;
}
if(mx > BallRect[0].x && mx < BallRect[0].x + BallRect[0].w && my > BallRect[0].y && my < BallRect[0].y + BallRect[0].h && bGame == true)
{
points = Red_Points;
Mix_PlayChannel(-1,hit,-1);
}
else if(mx > BallRect[1].x && mx < BallRect[1].x + BallRect[1].w && my > BallRect[1].y && my < BallRect[1].y + BallRect[1].h && bGame == true)
{
points = Blue_Points;
Mix_PlayChannel(-1,hit,-1);
}
else if(mx > BallRect[2].x && mx < BallRect[2].x + BallRect[2].w && my > BallRect[2].y && my < BallRect[2].y + BallRect[2].h && bGame == true)
{
points = Purple_Points;
Mix_PlayChannel(-1,hit,-1);
}
else if(mx > BallRect[3].x && mx < BallRect[3].x + BallRect[3].w && my > BallRect[3].y && my < BallRect[3].y + BallRect[3].h && bGame == true)
{
points = Black_Points;
Mix_PlayChannel(-1,hit,-1);
}
else if(mx > BallRect[4].x && mx < BallRect[4].x + BallRect[4].w && my > BallRect[4].y && my < BallRect[4].y + BallRect[4].h && bGame == true)
{
points = Green_Points;
Mix_PlayChannel(-1,hit,-1);
}
}
}
if(event.type == SDL_KEYDOWN)
{
if(keyState[SDLK_ESCAPE])
{
GameRunning = false;
}
if(keyState[SDLK_RETURN] && bLogo == true)
{
bLogo = false;
bTitle = true;
}
}
if(bLogo == true)
{
SDL_BlitSurface(gLogo,NULL,gScreen,&LogoRect);
}
else if(bTitle == true)
{
SDL_BlitSurface(gTitle,NULL,gScreen,&TitleRect);
SDL_BlitSurface(gButton[0],NULL,gScreen,&ButtonRect[0]);
SDL_BlitSurface(gButton[1],NULL,gScreen,&ButtonRect[1]);
}
else if(bGame == true)
{
SDL_BlitSurface(gBackdrop,NULL,gScreen,&BackdropRect);
SDL_BlitSurface(gPoints,NULL,gScreen,&PointRect);
SDL_BlitSurface(gBall[0],NULL,gScreen,&BallRect[0]);
SDL_BlitSurface(gBall[1],NULL,gScreen,&BallRect[1]);
SDL_BlitSurface(gBall[2],NULL,gScreen,&BallRect[2]);
SDL_BlitSurface(gBall[3],NULL,gScreen,&BallRect[3]);
SDL_BlitSurface(gBall[4],NULL,gScreen,&BallRect[4]);
}
BallRect[0].x++;
BallRect[1].x++;
BallRect[2].x++;
BallRect[3].x++;
BallRect[4].x++;
if(BallRect[0].x < 0 || BallRect[0].x + BallRect[0].w > MAX_WIDTH)
{
BallRect[0].x--;
}
else if(BallRect[1].x < 0 || BallRect[1].x + BallRect[1].w > MAX_WIDTH)
{
BallRect[1].x--;
}
else if(BallRect[2].x < 0 || BallRect[2].x + BallRect[2].w > MAX_WIDTH)
{
BallRect[2].x--;
}
else if(BallRect[3].x < 0 || BallRect[3].x + BallRect[3].w > MAX_WIDTH)
{
BallRect[3].x--;
}
else if(BallRect[4].x < 0 || BallRect[4].x + BallRect[4].w > MAX_WIDTH)
{
BallRect[4].x--;
}
}
if(SDL_Flip(gScreen) == -1)
{
return 1;
}
++fpscounter;
if(fpstickcounter >= 1000)
{
currentfps = fpscounter;
fpscounter = 0;
fpstickcounter = 0;
}
}
SDL_FreeSurface(gLogo);
SDL_FreeSurface(gTitle);
SDL_FreeSurface(gBackdrop);
SDL_FreeSurface(gBall[0]);
SDL_FreeSurface(gBall[1]);
SDL_FreeSurface(gBall[2]);
SDL_FreeSurface(gBall[3]);
SDL_FreeSurface(gBall[4]);
SDL_FreeSurface(gPop);
Mix_FreeMusic(backMusic);
Mix_FreeChunk(hit);
TTF_CloseFont(Font);
TTF_Quit();
Mix_CloseAudio();
SDL_Quit();
return 0;
}
[/code]