Posted 01 November 2012 - 04:51 PM
check out my code so far
main.h:
[source lang="cpp"]//The headers#include "SDL.h"#include "SDL_image.h"#include "SDL_ttf.h"#include <string>//global variables from main are inserted hereextern int const CLIP_MOUSEOVER;extern int const CLIP_MOUSEOUT; extern int const CLIP_MOUSEDOWN; extern int const CLIP_MOUSEUP; extern SDL_Event event;extern SDL_Rect clips[4];extern SDL_Surface* buttonSheet;extern SDL_Surface* screen;//global functions are inserted hereextern SDL_Surface *load_image(std::string filename);extern void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL);extern void set_clips();[/source]
main.cpp:
[source lang="cpp"]//The headers#include "SDL.h"#include "SDL_image.h"#include "SDL_ttf.h"#include <string>#include "Button.h"#include "main.h"//the screen attributesconst int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;//the button states in the sprite sheetint const CLIP_MOUSEOVER = 0;int const CLIP_MOUSEOUT = 1;int const CLIP_MOUSEDOWN = 2;int const CLIP_MOUSEUP = 3;//the surfacesSDL_Surface* buttonSheet = NULL;SDL_Surface* screen = NULL;//the sdl event structureSDL_Event event;//the clip regions of the spriteSDL_Rect clips[4];//the load image functionSDL_Surface *load_image(std::string filename){ SDL_Surface *loadedImage = NULL; SDL_Surface *optimizedImage = NULL; //loading the image loadedImage = IMG_Load(filename.c_str()); if(loadedImage != NULL) { optimizedImage = SDL_DisplayFormat(loadedImage); SDL_FreeSurface(loadedImage); //if the surface was optimized if(optimizedImage != NULL) { //color key surface SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY,SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF ) ); } } return optimizedImage;}//the apply surface functionvoid apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL){ SDL_Rect offset; offset.x = x; offset.y = y; SDL_BlitSurface(source,clip,destination, &offset);}//the function the sets the clips of the source surfacevoid set_clips(){ 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;}int main(int argc, char* args[] ){ //set the quit flag bool quit = false; //initialize everything SDL_Init(SDL_INIT_EVERYTHING); screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE); SDL_WM_SetCaption("Button Test", NULL); //loading the stuff buttonSheet = load_image("button.png"); //clip the sprite sheet set_clips(); //make a button object Button myButton(170,120,320,240); //while the user has not quit out yet while(quit == false) { //check to see if there are any event polls to handle if(SDL_PollEvent(&event)) { //handle button events myButton.handle_events(); //if the user has quit out of the window if(event.type == SDL_QUIT) { //quit the program quit = true; } } //Fill the screen white SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) ); //show the button myButton.show(); //update the screen SDL_Flip(screen); } //start cleaning up; SDL_FreeSurface(buttonSheet); SDL_Quit(); return 0;}[/source]
button.h
[source lang="cpp"]#pragma once#include "SDL.h"#include "SDL_image.h"#include "SDL_ttf.h"#include "main.h"class Button{public: //the constructor that initializes the variables Button(int x, int y, int w, int h); //handles events and sets the buttons sprite region void handle_events(); //shows the button on the screen void show();private: //the attributes of the button SDL_Rect box; // the part of the button sprite sheet that will be shown SDL_Rect *clip;};[/source]
button.cpp
[source lang="cpp"]#include "SDL.h"#include "SDL_image.h"#include "SDL_ttf.h"#include <string>#include "Button.h"Button::Button(int x, int y, int w, int h){ //set the buttons attributes box.x = x; box.y = y; box.w = w; box.h = h; //set the default sprite clip = &clips[CLIP_MOUSEOUT];}void Button::handle_events(){ //The mouse offsets int x = 0, y = 0; //If the mouse moved if( event.type == SDL_MOUSEMOTION ) { //Get the mouse offsets x = event.motion.x; y = event.motion.y; //If the mouse is over the button if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) ) { //Set the button sprite clip = &clips[CLIP_MOUSEOVER]; } //If not else { //Set the button sprite clip = &clips[CLIP_MOUSEOUT]; } } //if the mouse button is pressed if(event.type == SDL_MOUSEBUTTONDOWN) { //if the left button was pressed if(event.button.button == SDL_BUTTON_LEFT) { //get the mouse offsets x = event.motion.x; y = event.motion.y; //if the mouse is over the button if((x > box.x)&&(x < box.x+box.w)&&(y > box.y)&&(y < box.y+box.h)) { //set the button sprite clip = &clip[CLIP_MOUSEDOWN]; } } } //if a mouse button was released if(event.type == SDL_MOUSEBUTTONUP) { //if the left button was released if(event.button.button == SDL_BUTTON_LEFT) { //get the mouse offsets x = event.motion.x; y = event.motion.y; //if the mouse is over the button if((x > box.x)&&(x < box.x + box.w)&&(y > box.y)&&(y < box.y+box.h)) { //set the button sprite clip = &clip[CLIP_MOUSEUP]; } } }}void Button::show(){ //show the button apply_surface(box.x,box.y,buttonSheet,screen, clip);} [/source]
my problem is that im getting this eror now:
1>------ Build started: Project: sdl_test, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\aasim\desktop\sdl_test\sdl_test\main.h(20): error C2572: 'apply_surface' : redefinition of default parameter : parameter 5
1> c:\users\aasim\desktop\sdl_test\sdl_test\main.h(20) : see declaration of 'apply_surface'
1>c:\users\aasim\desktop\sdl_test\sdl_test\main.cpp(56): error C2572: 'apply_surface' : redefinition of default parameter : parameter 5
1> c:\users\aasim\desktop\sdl_test\sdl_test\main.h(20) : see declaration of 'apply_surface'
1> Button.cpp
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========