koji se bave programiranjem igrica verovatno ce prepoznati ovaj code iz jednog poznatog
tutorijala za SDL.Neznam kako drugacije da postavim pitanje osim da postujem ceo kod.
Code:
/*This source code copyrighted by Lazy Foo' Productions (2004-2009) and may not
be redestributed without written permission.*/
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The button states in the sprite sheet
const int CLIP_MOUSEOVER = 0;
const int CLIP_MOUSEOUT = 1;
const int CLIP_MOUSEDOWN = 2;
const int CLIP_MOUSEUP = 3;
//The surfaces
SDL_Surface *buttonSheet = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//The clip regions of the sprite sheet
SDL_Rect clips[ 4 ];
//The button
class Button
{
private:
//The attributes of the button
SDL_Rect box;
//The part of the button 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();
//Shows the button on the screen
void show();
};
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
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 the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Button Test", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the button sprite sheet
buttonSheet = load_image( "button.png" );
//If there was a problem in loading the button sprite sheet
if( buttonSheet == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface( buttonSheet );
//Quit SDL
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;
//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 a mouse button was pressed
if( event.type == SDL_MOUSEBUTTONDOWN )
{
//If the left mouse button was pressed
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse offsets
x = event.button.x;
y = event.button.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_MOUSEDOWN ];
}
}
}
//If a mouse button was released
if( event.type == SDL_MOUSEBUTTONUP )
{
//If the left mouse button was released
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse offsets
x = event.button.x;
y = event.button.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_MOUSEUP ];
}
}
}
}
void Button::show()
{
//Show the button
apply_surface( box.x, box.y, buttonSheet, screen, clip );
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//Initialize
if( init() == false )
{
return 1;
}
//Load the 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
while( quit == false )
{
//If there's events to handle
if( SDL_PollEvent( &event ) )
{
//Handle button events
myButton.handle_events();
//If the user has Xed out 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
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
//Clean up
clean_up();
return 0;
}
/*This source code copyrighted by Lazy Foo' Productions (2004-2009) and may not
be redestributed without written permission.*/
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The button states in the sprite sheet
const int CLIP_MOUSEOVER = 0;
const int CLIP_MOUSEOUT = 1;
const int CLIP_MOUSEDOWN = 2;
const int CLIP_MOUSEUP = 3;
//The surfaces
SDL_Surface *buttonSheet = NULL;
SDL_Surface *screen = NULL;
//The event structure
SDL_Event event;
//The clip regions of the sprite sheet
SDL_Rect clips[ 4 ];
//The button
class Button
{
private:
//The attributes of the button
SDL_Rect box;
//The part of the button 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();
//Shows the button on the screen
void show();
};
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized surface that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized surface
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old surface
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 the optimized surface
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
//Holds offsets
SDL_Rect offset;
//Get offsets
offset.x = x;
offset.y = y;
//Blit
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Button Test", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the button sprite sheet
buttonSheet = load_image( "button.png" );
//If there was a problem in loading the button sprite sheet
if( buttonSheet == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface( buttonSheet );
//Quit SDL
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;
//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 a mouse button was pressed
if( event.type == SDL_MOUSEBUTTONDOWN )
{
//If the left mouse button was pressed
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse offsets
x = event.button.x;
y = event.button.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_MOUSEDOWN ];
}
}
}
//If a mouse button was released
if( event.type == SDL_MOUSEBUTTONUP )
{
//If the left mouse button was released
if( event.button.button == SDL_BUTTON_LEFT )
{
//Get the mouse offsets
x = event.button.x;
y = event.button.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_MOUSEUP ];
}
}
}
}
void Button::show()
{
//Show the button
apply_surface( box.x, box.y, buttonSheet, screen, clip );
}
int main( int argc, char* args[] )
{
//Quit flag
bool quit = false;
//Initialize
if( init() == false )
{
return 1;
}
//Load the 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
while( quit == false )
{
//If there's events to handle
if( SDL_PollEvent( &event ) )
{
//Handle button events
myButton.handle_events();
//If the user has Xed out 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
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
//Clean up
clean_up();
return 0;
}
E sada pitanje:
mozda neko moze da me usmeri u pravom pravcu kako da izdvojim Button klasu u Button.cpp i
Button.h? :( Kada izdvojim Button.cpp ostaje jedna f-ja koja je definisana u main.cpp
(apply_surface)- a to je bash f-ja koju koristi metoda Button klase koja prikazuje dugme (Button::show()).
Taj apply_surface naravno nije def. u Button.cpp pa dobijam gresku pri prevodjenju.
Kako da sad to napravim da Button.show() funkcionise kako treba?
Ag + Na -> Xe