Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Izdvajanje jedne klase iz main-a u dva fajla-.cpp i .h?

[es] :: C/C++ programiranje :: C/C++ za početnike :: Izdvajanje jedne klase iz main-a u dva fajla-.cpp i .h?

[ Pregleda: 1536 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

Mr. Rejn
Deki Karamatijević
Sremčica

Član broj: 2514
Poruke: 515
*.dynamic.sbb.rs.



+4 Profil

icon Izdvajanje jedne klase iz main-a u dva fajla-.cpp i .h?13.11.2009. u 00:12 - pre 175 meseci
Nikad nisam naisao na ovakav problem,pa se nadam da cu ga bar opisati kako treba.Oni
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;
}


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
 
Odgovor na temu

Mihajlo Cvetanović
Beograd

Moderator
Član broj: 37636
Poruke: 1249



+96 Profil

icon Re: Izdvajanje jedne klase iz main-a u dva fajla-.cpp i .h?13.11.2009. u 09:48 - pre 175 meseci
Stavi funkciju apply_surface u poseban .cpp, recimo Globals.cpp, napravi i Globals.h u kome je samo deklaracija funkcije, i onda ubaci #include "Globals.h" svuda gde ti je potrebna ta funkcija. Tu možeš da ubaciš i druge funkcije koje se tako koriste posvuda.
 
Odgovor na temu

Mr. Rejn
Deki Karamatijević
Sremčica

Član broj: 2514
Poruke: 515
*.dynamic.sbb.rs.



+4 Profil

icon Re: Izdvajanje jedne klase iz main-a u dva fajla-.cpp i .h?15.11.2009. u 10:54 - pre 175 meseci
Ne ide,sad zbog drugog.

Ove konstante:
Code:

 SDL_Rect clips[ 4 ];
 SDL_Event event;
 SDL_Surface *buttonSheet;
 SDL_Surface *screen;

moraju da postoje i u main.cpp i u Button.cpp.Kada ih izuzmem iz bilo kog
javlja se greska prilikom prevodjenja.Sad,kada ih dodam u oba,greska pri povezivanju.
Code:

obj/Release/main.o:(.bss+0x0): multiple definition of `clips'
obj/Release/Button.o:(.bss+0x0): first defined here
obj/Release/main.o:(.bss+0x20): multiple definition of `buttonSheet'
obj/Release/Button.o:(.bss+0x34): first defined here
obj/Release/main.o:(.bss+0x24): multiple definition of `screen'
obj/Release/Button.o:(.bss+0x38): first defined here
obj/Release/main.o:(.bss+0x28): multiple definition of `event'
obj/Release/Button.o:(.bss+0x20): first defined here
collect2: ld returned 1 exit status

Takodje greska u povezivanju kada ih dodam u Globals.h:
Code:

obj/Release/Globals.o:(.bss+0x0): multiple definition of `clips'
obj/Release/Button.o:(.bss+0x0): first defined here
obj/Release/Globals.o:(.bss+0x20): multiple definition of `event'
obj/Release/Button.o:(.bss+0x20): first defined here
obj/Release/Globals.o:(.bss+0x34): multiple definition of `buttonSheet'
obj/Release/Button.o:(.bss+0x34): first defined here
obj/Release/Globals.o:(.bss+0x38): multiple definition of `screen'
obj/Release/Button.o:(.bss+0x38): first defined here
obj/Release/main.o:(.bss+0x0): multiple definition of `clips'
obj/Release/Button.o:(.bss+0x0): first defined here
obj/Release/main.o:(.bss+0x34): multiple definition of `buttonSheet'
obj/Release/Button.o:(.bss+0x34): first defined here
obj/Release/main.o:(.bss+0x38): multiple definition of `screen'
obj/Release/Button.o:(.bss+0x38): first defined here
obj/Release/main.o:(.bss+0x20): multiple definition of `event'
obj/Release/Button.o:(.bss+0x20): first defined here
collect2: ld returned 1 exit status

Button.cpp ukljucuje Globals.h,a main.cpp ukljucuje Button.h i Globals.h-valjda sam to
ukljucivao kako treba.
Ag + Na -> Xe
 
Odgovor na temu

Mihajlo Cvetanović
Beograd

Moderator
Član broj: 37636
Poruke: 1249



+96 Profil

icon Re: Izdvajanje jedne klase iz main-a u dva fajla-.cpp i .h?15.11.2009. u 12:26 - pre 175 meseci
Stavi SDL_Rect clips[ 4 ]; u Globals.cpp, a extern SDL_Rect clips[ 4 ]; u Globals.h. Isto i za ostale globalne promenljive.
 
Odgovor na temu

Mr. Rejn
Deki Karamatijević
Sremčica

Član broj: 2514
Poruke: 515
*.dynamic.sbb.rs.



+4 Profil

icon Re: Izdvajanje jedne klase iz main-a u dva fajla-.cpp i .h?15.11.2009. u 14:46 - pre 175 meseci
^ Tako je.Sad radi.
Ag + Na -> Xe
 
Odgovor na temu

[es] :: C/C++ programiranje :: C/C++ za početnike :: Izdvajanje jedne klase iz main-a u dva fajla-.cpp i .h?

[ Pregleda: 1536 | Odgovora: 4 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.