Returning 3 for no reason
when this program compiles it return 3
here is my code
Code:
#include <SDL/SDL.h>
#include <SDL/SDL_Image.h>
#include <string>
//screen atributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//the button states in the sprite sheet
int CLIP_MOUSEOVER = 0;
int CLIP_MOUSEOUT = 1;
int CLIP_MOUSEDOWN = 2;
int CLIP_MOUSEUP = 3;
//surfaces
SDL_Surface *buttonSheet = NULL;
SDL_Surface *screen = NULL;
//the event
SDL_Event event;
//the clip region of the sprite sheet
SDL_Rect clips[4];
//the button
class Button
{
private:
//the atributes of the button
SDL_Rect *box;
//the part of the button's sprite sheet that will be shown
SDL_Rect *clip;
public:
//initialize the variables
Button(int x, int y, int w, int h);
//handles events and set the button's sprite region
void handle_events();
//show button on screen
void show();
};
SDL_Surface *load_image(std::string filename){
//the image that's loaded
SDL_Surface *loaded_image = NULL;
//The optimized image that will be used
SDL_Surface *optimizedImage = NULL;
//load the image
loaded_image = IMG_Load(filename.c_str());
//if image is loaded
if(loaded_image != NULL){
//create optimized image
optimizedImage = SDL_DisplayFormat(loaded_image);
//free old surface
SDL_FreeSurface(loaded_image);
}
if(optimizedImage != NULL){
//map the color key
Uint32 colorkey = SDL_MapRGB(optimizedImage->format, 0xFF, 0xFF, 0);
//set color key of color R 0xFF, G 0xFF, and B 0 to be transparent
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colorkey);
}
return optimizedImage;
}
void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface(source, NULL, destination, &offset);
}
bool init(){
if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
return false;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if(screen == NULL){
return false;
}
//set window caption
SDL_WM_SetCaption("Mouse Events", NULL);
return true;
}
bool load_files(){
//load image
buttonSheet = load_image("button.png");
if(buttonSheet == NULL){
return false;
}
return true;
}
void clean_up(){
SDL_FreeSurface(buttonSheet);
SDL_Quit();
}
void set_clips()
{
//clip the sprite sheet
clips[CLIP_MOUSEOVER].x = 0;
clips[CLIP_MOUSEOVER].y = 0;
clips[CLIP_MOUSEOVER].w = 320;
clips[CLIP_MOUSEOVER].h = 240;
clips[CLIP_MOUSEOUT].x = 320;
clips[CLIP_MOUSEOUT].y = 0;
clips[CLIP_MOUSEOUT].w = 320;
clips[CLIP_MOUSEOUT].h = 240;
clips[CLIP_MOUSEDOWN].x = 0;
clips[CLIP_MOUSEDOWN].y = 240;
clips[CLIP_MOUSEDOWN].w = 320;
clips[CLIP_MOUSEDOWN].h = 240;
clips[CLIP_MOUSEUP].x = 320;
clips[CLIP_MOUSEUP].y = 240;
clips[CLIP_MOUSEUP].w = 320;
clips[CLIP_MOUSEUP].h = 240;
}
Button::Button(int x, int y, int w, int h){
//set the button's attributes
box->x = x;
box->y = y;
box->w = w;
box->h = h;
clip = &clips[CLIP_MOUSEOUT];
}
void Button::handle_events()
{
//the mouse offsets
int x = 0, y = 0;
//if mouse is moved
if(event.type = SDL_MOUSEMOTION){
x = event.motion.x;
y = event.motion.y;
if( (x > box->x) && (x < box->x + box->w) && (y > box->y) && (y < box->y + box->h)){
clip = &clips[CLIP_MOUSEOVER];
}
else{
clip = &clips[CLIP_MOUSEOUT];
}
//if mouse button was pressed
if(event.type == SDL_MOUSEBUTTONDOWN){
//if the left button was pressed
if(event.button.button == SDL_BUTTON_LEFT){
//get mouse offsets
x = event.button.x;
y = event.button.y;
//if the mouse is over button
if( (x > box->x) && (x < box->x + box->w) && (y > box->y) && (y < box->y + box->h)){
//set button sprite
clip = &clips[CLIP_MOUSEDOWN];
}
}
}
//if mouse was released
if(event.type == SDL_MOUSEBUTTONUP){
//if left mouse button was released
if(event.button.button == SDL_BUTTON_LEFT){
//get mouse offsets
x = event.button.x;
y = event.button.y;
//if the mouse was over the button
if( (x > box->x) && (x < box->x + box->y) && (y > box->y) && (y < box->y + box->h)){
//set button to sprite
clip = &clips[CLIP_MOUSEUP];
}
}
}
}
}
void Button::show(){
//show sprite
apply_surface(box->x, box->y, buttonSheet, screen, clip);
}
int main(int argc, char *args[]){
//makes sure program waits for a quit
bool quit = false;
//initialize
if(init() == false){
return 1;
}
//load files
if(load_files() == false){
return 1;
}
// clip the sprite sheet
set_clips();
//make the button
Button myButton(170, 120, 320, 240);
//while the user hasn't quit yet
while(quit == false){
//while there is an event to handle
while(SDL_PollEvent(&event)){
//handle button events
myButton.handle_events();
//if a key was pressed
}
//if the users Xs out of the window
if(event.type == SDL_QUIT){
quit = true;
}
//fill the screen white
SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
//update screen
if(SDL_Flip(screen) == -1){
return 1;
}
myButton.show();
}
clean_up();
return 0;
}
I cannot figure out why it's doing that, please help
p.s
I'm pretty sure I posted a thread like this but i can't seem to find it, if you could give me a link to that it would be nice