locked
Can someone help me split this up? RRS feed

  • Question

  • Im using SDL with this, but. I was up all night trying to split this up into .h and .cpp files (aside from main.cpp, which i basically want to just cant the main process). I had no luck and my friend wouldn't help me. So I can understand later on, how does one split this up to work? It works altogether but I couldn't get it to work in peices. My friend told me something about the .h tells what everything is and a .cpp is what they do, but that makes no sense, I need to see code. Thanks

        //The headers
        #include <string>
        #include <iostream>
         
        #include "SDL/SDL.h"
        #include "SDL/SDL_image.h"
         
        //Screen attributes
        const int SCREEN_WIDTH = 320;
        const int SCREEN_HEIGHT = 240;
        const int SCREEN_BPP = 32;
         
        //The frames per second
        const int FRAMES_PER_SECOND = 60;
         
        //The dimenstions of the player
        const int PLAYER_WIDTH = 32;
        const int PLAYER_HEIGHT = 32;
         
        //The constant speed of the player (when moving)
        const int PLAYER_SPEED = 3.14;
         
        //The direction status of the player
        const int PLAYER_RIGHT = 0;
        const int PLAYER_LEFT = 1;
         
        //The surfaces
        SDL_Surface *player = NULL;
        SDL_Surface *screen = NULL;
         
        //The event structure
        SDL_Event event;
         
        //The areas of the sprite sheet
        SDL_Rect clipsRight[ 5 ];
        SDL_Rect clipsLeft[ 5 ];
         
        //The player
        class Player
        {
            private:
            //The offset
            int offSet;
         
            //Its rate of movement
            int velocity;
         
            //Its current frame
            int frame;
         
            //Its animation status
            int status;
         
            public:
            //Initializes the variables
            Player();
         
            //Handles input
            void handle_events();
         
            //Moves the player
            void move();
         
            //Shows the player
            void show();
        };
         
        //The timer
        class Timer
        {
            private:
            //The clock time when the timer started
            int startTicks;
         
            //The ticks stored when the timer was paused
            int pausedTicks;
         
            //The timer status
            bool paused;
            bool started;
         
            public:
            //Initializes variables
            Timer();
         
            //The various clock actions
            void start();
            void stop();
            void pause();
            void unpause();
         
            //Gets the timer's time
            int get_ticks();
         
            //Checks the status of the timer
            bool is_started();
            bool is_paused();
        };
         
        void set_clips()
        {
            //Clip the sprites
            clipsRight[ 0 ].x = 0;
            clipsRight[ 0 ].y = 0;
            clipsRight[ 0 ].w = PLAYER_WIDTH;
            clipsRight[ 0 ].h = PLAYER_HEIGHT;
         
            clipsRight[ 1 ].x = PLAYER_WIDTH;
            clipsRight[ 1 ].y = 0;
            clipsRight[ 1 ].w = PLAYER_WIDTH;
            clipsRight[ 1 ].h = PLAYER_HEIGHT;
         
            clipsRight[ 2 ].x = PLAYER_WIDTH * 2;
            clipsRight[ 2 ].y = 0;
            clipsRight[ 2 ].w = PLAYER_WIDTH;
            clipsRight[ 2 ].h = PLAYER_HEIGHT;
         
            clipsRight[ 3 ].x = PLAYER_WIDTH * 3;
            clipsRight[ 3 ].y = 0;
            clipsRight[ 3 ].w = PLAYER_WIDTH;
            clipsRight[ 3 ].h = PLAYER_HEIGHT;
           
            clipsRight[ 4 ] = clipsRight[ 2 ];
         
            clipsLeft[ 0 ].x = 0;
            clipsLeft[ 0 ].y = PLAYER_HEIGHT;
            clipsLeft[ 0 ].w = PLAYER_WIDTH;
            clipsLeft[ 0 ].h = PLAYER_HEIGHT;
         
            clipsLeft[ 1 ].x = PLAYER_WIDTH;
            clipsLeft[ 1 ].y = PLAYER_HEIGHT;
            clipsLeft[ 1 ].w = PLAYER_WIDTH;
            clipsLeft[ 1 ].h = PLAYER_HEIGHT;
         
            clipsLeft[ 2 ].x = PLAYER_WIDTH * 2;
            clipsLeft[ 2 ].y = PLAYER_HEIGHT;
            clipsLeft[ 2 ].w = PLAYER_WIDTH;
            clipsLeft[ 2 ].h = PLAYER_HEIGHT;
         
            clipsLeft[ 3 ].x = PLAYER_WIDTH * 3;
            clipsLeft[ 3 ].y = PLAYER_HEIGHT;
            clipsLeft[ 3 ].w = PLAYER_WIDTH;
            clipsLeft[ 3 ].h = PLAYER_HEIGHT;
           
            clipsLeft[ 4 ] = clipsLeft[ 2 ];
        }
         
        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( "Platform Engine", NULL );
         
            //If everything initialized fine
            return true;
        }
         
        bool load_files()
        {
            //Load the sprite sheet
            player = load_image( "player.png" );
         
            //If there was a problem in loading the sprite
            if( player == NULL )
            {
                return false;
            }
         
            //If everything loaded fine
            return true;
        }
         
        void clean_up()
        {
            //Free the surface
            SDL_FreeSurface( player );
         
            //Quit SDL
            SDL_Quit();
        }
         
        Player::Player()
        {
            //Initialize movement variables
            offSet = 0;
            velocity = 0;
         
            //Initialize animation variables
            frame = 0;
            status = PLAYER_RIGHT;
        }
         
        void Player::handle_events()
        {
            //If a key was pressed
            if( event.type == SDL_KEYDOWN )
            {
                //Set the velocity
                switch( event.key.keysym.sym )
                {
                    case SDLK_RIGHT: velocity += PLAYER_SPEED; break;
                    case SDLK_LEFT: velocity -= PLAYER_SPEED; break;
                }
            }
            //If a key was released
            else if( event.type == SDL_KEYUP )
            {
                //Set the velocity
                switch( event.key.keysym.sym )
                {
                    case SDLK_RIGHT: velocity -= PLAYER_SPEED; break;
                    case SDLK_LEFT: velocity += PLAYER_SPEED; break;
                }
            }
        }
         
        void Player::move()
        {
            //Move
            offSet += velocity;
         
            //Keep the player in bounds
            if( ( offSet < 0 ) || ( offSet + PLAYER_WIDTH > SCREEN_WIDTH ) )
            {
                offSet -= velocity;
            }
        }
         
        void Player::show()
        {
            //If Player is moving left
            if( velocity < 0 )
            {
                //Set the animation to left
                status = PLAYER_LEFT;
         
                //Move to the next frame in the animation
                frame++;
            }
            //If Player is moving right
            else if( velocity > 0 )
            {
                //Set the animation to right
                status = PLAYER_RIGHT;
         
                //Move to the next frame in the animation
                frame++;
            }
            //If Player standing
            else
            {
                //Restart the animation
                frame = 0;
            }
         
            //Loop the animation
            if( frame >= 5 )
            {
                frame = 1;
            }
         
            //Show the player
            if( status == PLAYER_RIGHT )
            {
                apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsRight[ frame ] );
            }
            else if( status == PLAYER_LEFT )
            {
                apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsLeft[ frame ] );
            }
        }
         
        Timer::Timer()
        {
            //Initialize the variables
            startTicks = 0;
            pausedTicks = 0;
            paused = false;
            started = false;
        }
         
        void Timer::start()
        {
            //Start the timer
            started = true;
         
            //Unpause the timer
            paused = false;
         
            //Get the current clock time
            startTicks = SDL_GetTicks();
        }
         
        void Timer::stop()
        {
            //Stop the timer
            started = false;
         
            //Unpause the timer
            paused = false;
        }
         
        void Timer::pause()
        {
            //If the timer is running and isn't already paused
            if( ( started == true ) && ( paused == false ) )
            {
                //Pause the timer
                paused = true;
         
                //Calculate the paused ticks
                pausedTicks = SDL_GetTicks() - startTicks;
            }
        }
         
        void Timer::unpause()
        {
            //If the timer is paused
            if( paused == true )
            {
                //Unpause the timer
                paused = false;
         
                //Reset the starting ticks
                startTicks = SDL_GetTicks() - pausedTicks;
         
                //Reset the paused ticks
                pausedTicks = 0;
            }
        }
         
        int Timer::get_ticks()
        {
            //If the timer is running
            if( started == true )
            {
                //If the timer is paused
                if( paused == true )
                {
                    //Return the number of ticks when the timer was paused
                    return pausedTicks;
                }
                else
                {
                    //Return the current time minus the start time
                    return SDL_GetTicks() - startTicks;
                }
            }
         
            //If the timer isn't running
            return 0;
        }
         
        bool Timer::is_started()
        {
            return started;
        }
         
        bool Timer::is_paused()
        {
            return paused;
        }
         
        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();
         
            //The frame rate regulator
            Timer fps;
         
            //The player
            Player walk;
         
            //While the user hasn't quit
            while( quit == false )
            {
                //Start the frame timer
                fps.start();
         
                //While there's events to handle
                while( SDL_PollEvent( &event ) )
                {
                    //Handle events for the player
                    walk.handle_events();
         
                    //If the user has Xed out the window
                    if( event.type == SDL_QUIT )
                    {
                        //Quit the program
                        quit = true;
                    }
                }
         
                //Move the player
                walk.move();
         
                //Fill the screen white
                SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
         
                //Show the player on the screen
                walk.show();
         
                //Update the screen
                if( SDL_Flip( screen ) == -1 )
                {
                    return 1;
                }
         
                //Cap the frame rate
                if( fps.get_ticks() < 6000 / FRAMES_PER_SECOND )
                {
                    SDL_Delay( ( 6000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
                }
            }
         
            //Clean up
            clean_up();
         
            return 0;
        }
    

    Monday, July 9, 2012 4:38 PM

Answers

  • Sorry i missed it.Small changes in following files

    Globals.h

    #ifndef GLOBALS_H_INCLUDED__
    #define GLOBALS_H_INCLUDED__
    #include "SDL.h"
    #include "SDL_image.h"
    #include <string>
    
    extern SDL_Surface *player;
    extern SDL_Surface *screen ;
    
    //The event structure
    extern SDL_Event event;
    
    //The areas of the sprite sheet
    extern SDL_Rect clipsRight[ 5 ];
    extern SDL_Rect clipsLeft[ 5 ];
    
    void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip );
    void clean_up();
    bool init();
    bool load_files();
    SDL_Surface *load_image( std::string filename );
    void set_clips();
    #endif  // GLOBALS_H_INCLUDED__

    Globals.cpp

    #include "Globals.h"
    #include "Constants.h"
    
    //The surfaces
    SDL_Surface *player = NULL;
    SDL_Surface *screen = NULL;
    
    //The event structure
    SDL_Event event;
    
    //The areas of the sprite sheet
    SDL_Rect clipsRight[ 5 ];
    SDL_Rect clipsLeft[ 5 ];
    
    void set_clips()
    {
    	//Clip the sprites
    	clipsRight[ 0 ].x = 0;
    	clipsRight[ 0 ].y = 0;
    	clipsRight[ 0 ].w = PLAYER_WIDTH;
    	clipsRight[ 0 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 1 ].x = PLAYER_WIDTH;
    	clipsRight[ 1 ].y = 0;
    	clipsRight[ 1 ].w = PLAYER_WIDTH;
    	clipsRight[ 1 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 2 ].x = PLAYER_WIDTH * 2;
    	clipsRight[ 2 ].y = 0;
    	clipsRight[ 2 ].w = PLAYER_WIDTH;
    	clipsRight[ 2 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 3 ].x = PLAYER_WIDTH * 3;
    	clipsRight[ 3 ].y = 0;
    	clipsRight[ 3 ].w = PLAYER_WIDTH;
    	clipsRight[ 3 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 4 ] = clipsRight[ 2 ];
    
    	clipsLeft[ 0 ].x = 0;
    	clipsLeft[ 0 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 0 ].w = PLAYER_WIDTH;
    	clipsLeft[ 0 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 1 ].x = PLAYER_WIDTH;
    	clipsLeft[ 1 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 1 ].w = PLAYER_WIDTH;
    	clipsLeft[ 1 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 2 ].x = PLAYER_WIDTH * 2;
    	clipsLeft[ 2 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 2 ].w = PLAYER_WIDTH;
    	clipsLeft[ 2 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 3 ].x = PLAYER_WIDTH * 3;
    	clipsLeft[ 3 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 3 ].w = PLAYER_WIDTH;
    	clipsLeft[ 3 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 4 ] = clipsLeft[ 2 ];
    }
    
    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( "Platform Engine", NULL );
    
    	//If everything initialized fine
    	return true;
    }
    
    bool load_files()
    {
    	//Load the sprite sheet
    	player = load_image( "player.png" );
    
    	//If there was a problem in loading the sprite
    	if( player == NULL )
    	{
    		return false;
    	}
    
    	//If everything loaded fine
    	return true;
    }
    
    void clean_up()
    {
    	//Free the surface
    	SDL_FreeSurface( player );
    
    	//Quit SDL
    	SDL_Quit();
    }



    Thanks, Renjith V R

    Tuesday, July 17, 2012 5:01 PM

All replies

  • On 7/9/2012 12:38 PM, DoctorNicholas wrote:

    I was up all night trying to split this up into .h and .cpp files

         //The headers
         #include <string>

    These are fine in an .h file.

         const int SCREEN_WIDTH = 320;

    Definitions of constants could also go into .h file.

         SDL_Surface *player = NULL;

    Variable definitions need to be split up. Otherwise, every .cpp file including this .h file will get its own definition of the variable, and then you would get linker errors complaining about redefinitions. This is what you do:

    // In .h file - a declaration
    extern SDL_Surface *player;

    // In one .cpp file - a definition
    SDL_Surface *player = NULL;

         //The player
         class Player
         {...};

    Class definition goes into .h file

         void set_clips()
         {...}

    Function definition needs to be split up, similar to the treatment for global variables:

    // in .h file - a declaration
    void set_clips();  // no body (the part in braces).

    // in one .cpp file - a definition
    void set_clips() {...}  // complete with function body, the actual implementation

         Player::Player()
         {...}

    This is a function definition, and goes into a .cpp file (the declaration was made as part of the definition of class Player, in the header).

         int main( int argc, char* args[] )
         {...}

    This goes into a .cpp file. Since you never explicitly call main(), you don't need a declaration in the header for it.


    Igor Tandetnik

    Monday, July 9, 2012 4:48 PM
  • The declarations go in the .h and the definitions go in the .cpp.

    Constants:

    Put declarations of the form in a header file.

    static const int MY_SETTING = 5;

    Global variables

    Get rid of your global variables, or declare them like this in the header file.

    extern SDL_Surface *player;

    And put their actual declaration in the .cpp file:

    SDL_Surface *player = NULL;

    Classes

    Then put this into Player.h 

        //The player
        class Player
        {
            private:
            //The offset
            int offSet;
         
            //Its rate of movement
            int velocity;
         
            //Its current frame
            int frame;
         
            //Its animation status
            int status;
         
            public:
            //Initializes the variables
            Player();
         
            //Handles input
            void handle_events();
         
            //Moves the player
            void move();
         
            //Shows the player
            void show();
        };

    And Player.cpp will consist of #include "player.h"  and the definition of any Player::xxx member function.

    Similarly for Timer.  Break it up with the class declaration in the header, the definitions of the member functions in the .cpp file.

    File Scope (global) functions.

    The global file-scope functions should be in classes, but you can declare their prototypes in a header file like this:

    bool load_files();

    and then define them in .cpp files like this:

        bool load_files()
        {
            //Load the sprite sheet
            player = load_image( "player.png" );
         
            //If there was a problem in loading the sprite
            if( player == NULL )
            {
                return false;
            }
         
            //If everything loaded fine
            return true;
        }

    Or consider making them either static functions, to limit their visibility to just the .cpp file they are compiled in, or put them in an anonymous namespace (same deal.)

    Your .cpp files will need to include the header file containing the contants, and the header file containing the class declarations.


    Monday, July 9, 2012 4:58 PM
  • Define following things to .h file

    1. Constants

    2. You can initialize SDL inside class or global depends on your usage.

    3. Class declarations.For example

    Class A { private: // Your member variables int m_member; public: // function prototypes void SetMember( int member );

    A( int member); };

    Following things should write on cpp file

    1. Any static member initializations if present.

    2. If you are only using the constants inside class then you can define the constants in cpp file.

    3. Member  function definitions. For example

    void A::SetMember( int member )
    {
     m_member = member;
    }

    The above is just a sample. You can do these kind of simple assignment in constructor as follows

    A::A( int member):m_member( member)
    {
    }


    Thanks, Renjith V R

    Monday, July 9, 2012 5:24 PM
  • //The headers
    #include <string>
    #include <iostream>
     
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
     
    #include "video.h"
    #include "player.h"
    #include "timer.h"
     
     
    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( "Platform Engine", NULL );
     
    //If everything initialized fine
    return true;
    }
     
    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();
     
    //The frame rate regulator
    Timer fps;
     
    //The player
    Player walk;
     
    //While the user hasn't quit
    while( quit == false )
    {
    //Start the frame timer
    fps.start();
     
    //While there's events to handle
    while( SDL_PollEvent( &event ) )
    {
    //Handle events for the player
    walk.handle_events();
     
    //If the user has Xed out the window
    if( event.type == SDL_QUIT )
    {
    //Quit the program
    quit = true;
    }
    }
     
    //Move the player
    walk.move();
     
    //Fill the screen white
    SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
     
    //Show the player on the screen
    walk.show();
     
    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
    return 1;
    }
     
    //Cap the frame rate
    if( fps.get_ticks() < 6000 / FRAMES_PER_SECOND )
    {
    SDL_Delay( ( 6000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
    }
    }
     
    //Clean up
    clean_up();
     
    return 0;
    }

    main.cpp

    //The headers
    #include <string>
    #include <iostream>
     
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    
    #include "video.h"
    #include "player.h"
    
    //The player surface
    SDL_Surface *player = NULL;
    
    void set_clips()
    {
    //Clip the sprites
    clipsRight[ 0 ].x = 0;
    clipsRight[ 0 ].y = 0;
    clipsRight[ 0 ].w = PLAYER_WIDTH;
    clipsRight[ 0 ].h = PLAYER_HEIGHT;
     
    clipsRight[ 1 ].x = PLAYER_WIDTH;
    clipsRight[ 1 ].y = 0;
    clipsRight[ 1 ].w = PLAYER_WIDTH;
    clipsRight[ 1 ].h = PLAYER_HEIGHT;
     
    clipsRight[ 2 ].x = PLAYER_WIDTH * 2;
    clipsRight[ 2 ].y = 0;
    clipsRight[ 2 ].w = PLAYER_WIDTH;
    clipsRight[ 2 ].h = PLAYER_HEIGHT;
     
    clipsRight[ 3 ].x = PLAYER_WIDTH * 3;
    clipsRight[ 3 ].y = 0;
    clipsRight[ 3 ].w = PLAYER_WIDTH;
    clipsRight[ 3 ].h = PLAYER_HEIGHT;
       
    clipsRight[ 4 ] = clipsRight[ 2 ];
     
    clipsLeft[ 0 ].x = 0;
    clipsLeft[ 0 ].y = PLAYER_HEIGHT;
    clipsLeft[ 0 ].w = PLAYER_WIDTH;
    clipsLeft[ 0 ].h = PLAYER_HEIGHT;
     
    clipsLeft[ 1 ].x = PLAYER_WIDTH;
    clipsLeft[ 1 ].y = PLAYER_HEIGHT;
    clipsLeft[ 1 ].w = PLAYER_WIDTH;
    clipsLeft[ 1 ].h = PLAYER_HEIGHT;
     
    clipsLeft[ 2 ].x = PLAYER_WIDTH * 2;
    clipsLeft[ 2 ].y = PLAYER_HEIGHT;
    clipsLeft[ 2 ].w = PLAYER_WIDTH;
    clipsLeft[ 2 ].h = PLAYER_HEIGHT;
     
    clipsLeft[ 3 ].x = PLAYER_WIDTH * 3;
    clipsLeft[ 3 ].y = PLAYER_HEIGHT;
    clipsLeft[ 3 ].w = PLAYER_WIDTH;
    clipsLeft[ 3 ].h = PLAYER_HEIGHT;
       
    clipsLeft[ 4 ] = clipsLeft[ 2 ];
    }
    
    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;
    }
    
    bool load_files()
    {
    //Load the sprite sheet
    player = load_image( "player.png" );
     
    //If there was a problem in loading the sprite
    if( player == NULL )
    {
    return false;
    }
     
    //If everything loaded fine
    return true;
    }
    
    void clean_up()
    {
    //Free the surface
    SDL_FreeSurface( player );
     
    //Quit SDL
    SDL_Quit();
    }
    
    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 );
    }
    
    Player::Player()
    {
    //Initialize movement variables
    offSet = 0;
    velocity = 0;
     
    //Initialize animation variables
    frame = 0;
    status = PLAYER_RIGHT;
    }
     
    void Player::handle_events()
    {
    //If a key was pressed
    if( event.type == SDL_KEYDOWN )
    {
    //Set the velocity
    switch( event.key.keysym.sym )
    {
    case SDLK_RIGHT: velocity += PLAYER_SPEED; break;
    case SDLK_LEFT: velocity -= PLAYER_SPEED; break;
    }
    }
    //If a key was released
    else if( event.type == SDL_KEYUP )
    {
    //Set the velocity
    switch( event.key.keysym.sym )
    {
    case SDLK_RIGHT: velocity -= PLAYER_SPEED; break;
    case SDLK_LEFT: velocity += PLAYER_SPEED; break;
    }
    }
    }
     
    void Player::move()
    {
    //Move
    offSet += velocity;
     
    //Keep the player in bounds
    if( ( offSet < 0 ) || ( offSet + PLAYER_WIDTH > SCREEN_WIDTH ) )
    {
    offSet -= velocity;
    }
    }
     
    void Player::show()
    {
    //If Player is moving left
    if( velocity < 0 )
    {
    //Set the animation to left
    status = PLAYER_LEFT;
     
    //Move to the next frame in the animation
    frame++;
    }
    //If Player is moving right
    else if( velocity > 0 )
    {
    //Set the animation to right
    status = PLAYER_RIGHT;
     
    //Move to the next frame in the animation
    frame++;
    }
    //If Player standing
    else
    {
    //Restart the animation
    frame = 0;
    }
     
    //Loop the animation
    if( frame >= 5 )
    {
    frame = 1;
    }
     
    //Show the player
    if( status == PLAYER_RIGHT )
    {
    apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsRight[ frame ] );
    }
    else if( status == PLAYER_LEFT )
    {
    apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsLeft[ frame ] );
    }
    }

    player.cpp

    //The headers
    #include <string>
    #include <iostream>
     
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    
    #include "timer.h"
    
    Timer::Timer()
    {
    //Initialize the variables
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
    }
     
    void Timer::start()
    {
    //Start the timer
    started = true;
     
    //Unpause the timer
    paused = false;
     
    //Get the current clock time
    startTicks = SDL_GetTicks();
    }
     
    void Timer::stop()
    {
    //Stop the timer
    started = false;
     
    //Unpause the timer
    paused = false;
    }
     
    void Timer::pause()
    {
    //If the timer is running and isn't already paused
    if( ( started == true ) && ( paused == false ) )
    {
    //Pause the timer
    paused = true;
     
    //Calculate the paused ticks
    pausedTicks = SDL_GetTicks() - startTicks;
    }
    }
     
    void Timer::unpause()
    {
    //If the timer is paused
    if( paused == true )
    {
    //Unpause the timer
    paused = false;
     
    //Reset the starting ticks
    startTicks = SDL_GetTicks() - pausedTicks;
     
    //Reset the paused ticks
    pausedTicks = 0;
    }
    }
     
    int Timer::get_ticks()
    {
    //If the timer is running
    if( started == true )
    {
    //If the timer is paused
    if( paused == true )
    {
    //Return the number of ticks when the timer was paused
    return pausedTicks;
    }
    else
    {
    //Return the current time minus the start time
    return SDL_GetTicks() - startTicks;
    }
    }
     
    //If the timer isn't running
    return 0;
    }
     
    bool Timer::is_started()
    {
    return started;
    }
     
    bool Timer::is_paused()
    {
    return paused;
    }

    Timer.cpp

    //Screen attributes
    const int SCREEN_WIDTH = 320;
    const int SCREEN_HEIGHT = 240;
    const int SCREEN_BPP = 32;
     
    //The frames per second
    const int FRAMES_PER_SECOND = 60;
    
    //The window surface
    SDL_Surface *screen = NULL;

    video.h

    //The dimenstions of the player
    const int PLAYER_WIDTH = 32;
    const int PLAYER_HEIGHT = 32;
     
    //The constant speed of the player (when moving)
    const int PLAYER_SPEED = 3.14;
     
    //The direction status of the player
    const int PLAYER_RIGHT = 0;
    const int PLAYER_LEFT = 1;
     
    //The player surface
    extern SDL_Surface *player;
    
    //The event structure
    SDL_Event event;
    
    //The areas of the sprite sheet
    SDL_Rect clipsRight[ 5 ];
    SDL_Rect clipsLeft[ 5 ];
    
    //The player
    class Player
    {
    private:
    //The offset
    int offSet;
    //Its rate of movement
    int velocity;
    //Its current frame
    int frame;
    //Its animation status
    int status;
    public:
    //Initializes the variables
    Player();
    //Handles input
    void handle_events();
    //Moves the player
    void move();
    //Shows the player
    void show();
    };
    
    void set_clips();
    
    bool load_files();
    
    void clean_up();
    
    void apply_surface();

    player.h

    //The timer
    class Timer
    {
    private:
    //The clock time when the timer started
    int startTicks;
     
    //The ticks stored when the timer was paused
    int pausedTicks;
     
    //The timer status
    bool paused;
    bool started;
     
    public:
    //Initializes variables
    Timer();
     
    //The various clock actions
    void start();
    void stop();
    void pause();
    void unpause();
     
    //Gets the timer's time
    int get_ticks();
     
    //Checks the status of the timer
    bool is_started();
    bool is_paused();
    };

    timer.h

    The errors that I am getting are mutliple definitions of: screen, event, clipsRight, and clipsLeft

    I am lost, where do I fix this? Thanks

    Monday, July 9, 2012 5:50 PM
  • When you declare a global variable in a header file, use the extern keyword.  This is just a heads-up that such a variable exists, but doesn't actually define the storage for it.

    Then you must define storage for the variable in exactly one of the .cpp files.  Give the variable a home to live in by assigning it some storage in a .cpp file related to the .h file.  In your example, that would be video.cpp.

    In the video.h header file:

    //The event structure
    extern SDL_Event event;
    
    //The areas of the sprite sheet
    extern SDL_Rect clipsRight[ 5 ];
    extern SDL_Rect clipsLeft[ 5 ];
    

    In the video.cpp file:

    //The event structure
    SDL_Event event;
    
    //The areas of the sprite sheet
    SDL_Rect clipsRight[ 5 ];
    SDL_Rect clipsLeft[ 5 ];

    You defined storage for screen in Timer.cpp.  Maybe you wanted it in video.cpp?  I think so.  Then also include the extern declaration in video.h.

    Monday, July 9, 2012 7:30 PM
  • im so lost, can you correct for me and show me?
    Monday, July 9, 2012 10:50 PM
  • im so lost, can you correct for me and show me?

    *sigh*

    Main.cpp

    #include "Globals.h"
    #include "Timer.h"
    #include "Player.h"
    #include "Metrics.h"
    
    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();
     
        //The frame rate regulator
        Timer fps;
     
        //The player
        Player walk;
     
        //While the user hasn't quit
        while( quit == false )
        {
            //Start the frame timer
            fps.start();
     
            //While there's events to handle
            while( SDL_PollEvent( &event ) )
            {
                //Handle events for the player
                walk.handle_events();
     
                //If the user has Xed out the window
                if( event.type == SDL_QUIT )
                {
                    //Quit the program
                    quit = true;
                }
            }
     
            //Move the player
            walk.move();
     
            //Fill the screen white
            SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
     
            //Show the player on the screen
            walk.show();
     
            //Update the screen
            if( SDL_Flip( screen ) == -1 )
            {
                return 1;
            }
     
            //Cap the frame rate
            if( fps.get_ticks() < 6000 / FRAMES_PER_SECOND )
            {
                SDL_Delay( ( 6000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
            }
        }
     
        //Clean up
        clean_up();
     
        return 0;
    }

    Metrics.h

    #ifndef METRICS_H_INCLUDED__
    #define METRICS_H_INCLUDED__
    
    //Screen attributes
    static const int SCREEN_WIDTH = 320;
    static const int SCREEN_HEIGHT = 240;
    static const int SCREEN_BPP = 32;
     
    //The frames per second
    static const int FRAMES_PER_SECOND = 60;
     
    //The dimenstions of the player
    static const int PLAYER_WIDTH = 32;
    static const int PLAYER_HEIGHT = 32;
     
    //The constant speed of the player (when moving)
    static const double PLAYER_SPEED = 3.14;
     
    //The direction status of the player
    static const int PLAYER_RIGHT = 0;
    static const int PLAYER_LEFT = 1;
     
    #endif  // METRICS_H_INCLUDED__

    Globals.h

    #ifndef GLOBALS_H_INCLUDED__
    #define GLOBALS_H_INCLUDED__
    
    #include "SDL/SDL.h"
    #include "SDL/SDL_image.h"
    
    //The surfaces
    extern SDL_Surface *player;
    extern SDL_Surface *screen;
     
    //The event structure
    extern SDL_Event event;
     
    //The areas of the sprite sheet
    extern SDL_Rect clipsRight[ 5 ];
    extern SDL_Rect clipsLeft[ 5 ];
     
    bool init();
    bool load_files();
    void set_clips();
    void clean_up();
    void apply_surface( double x, double y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL );
    
    
    #endif  // GLOBALS_H_INCLUDED__

    Globals.cpp

    #include "Globals.h"
    
    #include <string>
    
    #include "Metrics.h"
    
    void set_clips()
    {
        //Clip the sprites
        clipsRight[ 0 ].x = 0;
        clipsRight[ 0 ].y = 0;
        clipsRight[ 0 ].w = PLAYER_WIDTH;
        clipsRight[ 0 ].h = PLAYER_HEIGHT;
     
        clipsRight[ 1 ].x = PLAYER_WIDTH;
        clipsRight[ 1 ].y = 0;
        clipsRight[ 1 ].w = PLAYER_WIDTH;
        clipsRight[ 1 ].h = PLAYER_HEIGHT;
     
        clipsRight[ 2 ].x = PLAYER_WIDTH * 2;
        clipsRight[ 2 ].y = 0;
        clipsRight[ 2 ].w = PLAYER_WIDTH;
        clipsRight[ 2 ].h = PLAYER_HEIGHT;
     
        clipsRight[ 3 ].x = PLAYER_WIDTH * 3;
        clipsRight[ 3 ].y = 0;
        clipsRight[ 3 ].w = PLAYER_WIDTH;
        clipsRight[ 3 ].h = PLAYER_HEIGHT;
       
        clipsRight[ 4 ] = clipsRight[ 2 ];
     
        clipsLeft[ 0 ].x = 0;
        clipsLeft[ 0 ].y = PLAYER_HEIGHT;
        clipsLeft[ 0 ].w = PLAYER_WIDTH;
        clipsLeft[ 0 ].h = PLAYER_HEIGHT;
     
        clipsLeft[ 1 ].x = PLAYER_WIDTH;
        clipsLeft[ 1 ].y = PLAYER_HEIGHT;
        clipsLeft[ 1 ].w = PLAYER_WIDTH;
        clipsLeft[ 1 ].h = PLAYER_HEIGHT;
     
        clipsLeft[ 2 ].x = PLAYER_WIDTH * 2;
        clipsLeft[ 2 ].y = PLAYER_HEIGHT;
        clipsLeft[ 2 ].w = PLAYER_WIDTH;
        clipsLeft[ 2 ].h = PLAYER_HEIGHT;
     
        clipsLeft[ 3 ].x = PLAYER_WIDTH * 3;
        clipsLeft[ 3 ].y = PLAYER_HEIGHT;
        clipsLeft[ 3 ].w = PLAYER_WIDTH;
        clipsLeft[ 3 ].h = PLAYER_HEIGHT;
       
        clipsLeft[ 4 ] = clipsLeft[ 2 ];
    }
     
    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( double x, double y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip )
    {
        //Holds offsets
        SDL_Rect offset;
     
        //Get offsets
        offset.x = static_cast<Sint16>( x );
        offset.y = static_cast<Sint16>( 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( "Platform Engine", NULL );
     
        //If everything initialized fine
        return true;
    }
     
    bool load_files()
    {
        //Load the sprite sheet
        player = load_image( "player.png" );
     
        //If there was a problem in loading the sprite
        if( player == NULL )
        {
            return false;
        }
     
        //If everything loaded fine
        return true;
    }
     
    void clean_up()
    {
        //Free the surface
        SDL_FreeSurface( player );
     
        //Quit SDL
        SDL_Quit();
    }
    

    Player.h

    #ifndef PLAYER_H_INCLUDED__
    #define PLAYER_H_INCLUDED__
    
    //The player
    class Player
    {
        private:
        //The offset
        double offSet;
     
        //Its rate of movement
        double velocity;
     
        //Its current frame
        int frame;
     
        //Its animation status
        int status;
     
        public:
        //Initializes the variables
        Player();
     
        //Handles input
        void handle_events();
     
        //Moves the player
        void move();
     
        //Shows the player
        void show();
    };
     
    #endif  // PLAYER_H_INCLUDED__

    Player.cpp

    #include "Player.h"
    
    #include "Metrics.h"
    #include "Globals.h"
    
    Player::Player()
    {
        //Initialize movement variables
        offSet = 0;
        velocity = 0;
     
        //Initialize animation variables
        frame = 0;
        status = PLAYER_RIGHT;
    }
     
    void Player::handle_events()
    {
        //If a key was pressed
        if( event.type == SDL_KEYDOWN )
        {
            //Set the velocity
            switch( event.key.keysym.sym )
            {
                case SDLK_RIGHT: velocity += PLAYER_SPEED; break;
                case SDLK_LEFT: velocity -= PLAYER_SPEED; break;
            }
        }
        //If a key was released
        else if( event.type == SDL_KEYUP )
        {
            //Set the velocity
            switch( event.key.keysym.sym )
            {
                case SDLK_RIGHT: velocity -= PLAYER_SPEED; break;
                case SDLK_LEFT: velocity += PLAYER_SPEED; break;
            }
        }
    }
     
    void Player::move()
    {
        //Move
        offSet += velocity;
     
        //Keep the player in bounds
        if( ( offSet < 0 ) || ( offSet + PLAYER_WIDTH > SCREEN_WIDTH ) )
        {
            offSet -= velocity;
        }
    }
     
    void Player::show()
    {
        //If Player is moving left
        if( velocity < 0 )
        {
            //Set the animation to left
            status = PLAYER_LEFT;
     
            //Move to the next frame in the animation
            frame++;
        }
        //If Player is moving right
        else if( velocity > 0 )
        {
            //Set the animation to right
            status = PLAYER_RIGHT;
     
            //Move to the next frame in the animation
            frame++;
        }
        //If Player standing
        else
        {
            //Restart the animation
            frame = 0;
        }
     
        //Loop the animation
        if( frame >= 5 )
        {
            frame = 1;
        }
     
        //Show the player
        if( status == PLAYER_RIGHT )
        {
            apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsRight[ frame ] );
        }
        else if( status == PLAYER_LEFT )
        {
            apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsLeft[ frame ] );
        }
    }
    

    Timer.h

    #ifndef TIMER_H_INCLUDED__
    #define TIMER_H_INCLUDED__
    
    //The timer
    class Timer
    {
        private:
        //The clock time when the timer started
        int startTicks;
     
        //The ticks stored when the timer was paused
        int pausedTicks;
     
        //The timer status
        bool paused;
        bool started;
     
        public:
        //Initializes variables
        Timer();
     
        //The various clock actions
        void start();
        void stop();
        void pause();
        void unpause();
     
        //Gets the timer's time
        int get_ticks();
     
        //Checks the status of the timer
        bool is_started();
        bool is_paused();
    };
     
    #endif  // TIMER_H_INCLUDED__

    Timer.cpp

    #include "Timer.h"
    
    #include "SDL\SDL.h"
    
    Timer::Timer()
    {
        //Initialize the variables
        startTicks = 0;
        pausedTicks = 0;
        paused = false;
        started = false;
    }
     
    void Timer::start()
    {
        //Start the timer
        started = true;
     
        //Unpause the timer
        paused = false;
     
        //Get the current clock time
        startTicks = SDL_GetTicks();
    }
     
    void Timer::stop()
    {
        //Stop the timer
        started = false;
     
        //Unpause the timer
        paused = false;
    }
     
    void Timer::pause()
    {
        //If the timer is running and isn't already paused
        if( ( started == true ) && ( paused == false ) )
        {
            //Pause the timer
            paused = true;
     
            //Calculate the paused ticks
            pausedTicks = SDL_GetTicks() - startTicks;
        }
    }
     
    void Timer::unpause()
    {
        //If the timer is paused
        if( paused == true )
        {
            //Unpause the timer
            paused = false;
     
            //Reset the starting ticks
            startTicks = SDL_GetTicks() - pausedTicks;
     
            //Reset the paused ticks
            pausedTicks = 0;
        }
    }
     
    int Timer::get_ticks()
    {
        //If the timer is running
        if( started == true )
        {
            //If the timer is paused
            if( paused == true )
            {
                //Return the number of ticks when the timer was paused
                return pausedTicks;
            }
            else
            {
                //Return the current time minus the start time
                return SDL_GetTicks() - startTicks;
            }
        }
     
        //If the timer isn't running
        return 0;
    }
     
    bool Timer::is_started()
    {
        return started;
    }
     
    bool Timer::is_paused()
    {
        return paused;
    }
     
    

    Tuesday, July 10, 2012 1:35 AM
  • I copied exactly as there was but its not working. Im getting a whole bunch of linker errors in main.cpp


    Tuesday, July 10, 2012 4:08 AM
  • I copied exactly as there was but its not working. Im getting a whole bunch of linker errors in main.cpp


    can you post the error information? 

    Thanks and Regards Selvam http://www15.brinkster.com/selvamselvam/

    Tuesday, July 10, 2012 6:12 AM
  • Im not at home right now (which has visual c++ 2008) so all i have is devc++ (although im sure the errors are the same). on both IDE's I got linker errors everywhere in main.cpp.
    Tuesday, July 10, 2012 6:22 AM
  • Do your project settings show ALL the source files?
    Tuesday, July 10, 2012 4:12 PM
  • what does that mean?
    Tuesday, July 10, 2012 4:34 PM
  • what does that mean?

    It sounds like you're having lots of trouble with this task.

    Your linker will give you very precise information about what the problems are.  Most likely something like LNK2019, which will say, quite specifically, which symbol it cannot resolve.  The linker will need a reference to the SDL libraries (additional library / addition dependency), and all the source files will need to be added to the project.

    Cut and paste the build error output and we'll help you as much as we can, but please make a reasonable effort to provide us with information with which we can help you.

    Post the build output log.

    Post your project settings (the contents of your .vcproj file) if you are really lost.

    Tuesday, July 10, 2012 4:52 PM
  • Im not at home at the moment (my home pc has visual c++ 2008) and im on a pc that has devc++, so I copied and pasted what was on this thread real quick and ill post the build log from that, and when I get home later ill be more than glad to post the visual c++ build log (if theyre different that is).

    C:\Users\The User\programming\@projects\platform_engine/main.cpp:45: undefined reference to `event'
    C:\Users\The User\programming\@projects\platform_engine/main.cpp:39: undefined reference to `event'
    C:\Users\The User\programming\@projects\platform_engine/main.cpp:56: undefined reference to `screen'
    C:\Users\The User\programming\@projects\platform_engine/main.cpp:56: undefined reference to `screen'
    C:\Users\The User\programming\@projects\platform_engine/main.cpp:56: undefined reference to `screen'
    C:\Users\The User\programming\@projects\platform_engine/main.cpp:62: undefined reference to `screen'
    player.o: In function `ZN6Player13handle_eventsEv':
    C:\Users\The User\programming\@projects\platform_engine/player.cpp:20: undefined reference to `event'
    C:\Users\The User\programming\@projects\platform_engine/player.cpp:23: undefined reference to `event'
    C:\Users\The User\programming\@projects\platform_engine/player.cpp:30: undefined reference to `event'
    C:\Users\The User\programming\@projects\platform_engine/player.cpp:33: undefined reference to `event'
    player.o: In function `ZN6Player4showEv':
    C:\Users\The User\programming\@projects\platform_engine/player.cpp:89: undefined reference to `clipsRight'
    C:\Users\The User\programming\@projects\platform_engine/player.cpp:89: undefined reference to `screen'
    C:\Users\The User\programming\@projects\platform_engine/player.cpp:89: undefined reference to `player'
    C:\Users\The User\programming\@projects\platform_engine/player.cpp:93: undefined reference to `clipsLeft'
    C:\Users\The User\programming\@projects\platform_engine/player.cpp:93: undefined reference to `screen'
    C:\Users\The User\programming\@projects\platform_engine/player.cpp:93: undefined reference to `player'
    globals.o: In function `Z9set_clipsv':
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:10: undefined reference to `clipsRight'
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:11: undefined reference to `clipsRight'
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:12: undefined reference to `clipsRight'
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:13: undefined reference to `clipsRight'
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:15: undefined reference to `clipsRight'
    globals.o:C:\Users\The User\programming\@projects\platform_engine/globals.cpp:16: more undefined references to `clipsRight' follow
    globals.o: In function `Z9set_clipsv':
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:32: undefined reference to `clipsLeft'
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:33: undefined reference to `clipsLeft'
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:34: undefined reference to `clipsLeft'
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:35: undefined reference to `clipsLeft'
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:37: undefined reference to `clipsLeft'
    globals.o:C:\Users\The User\programming\@projects\platform_engine/globals.cpp:38: more undefined references to `clipsLeft' follow
    globals.o: In function `Z4initv':
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:110: undefined reference to `screen'
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:113: undefined reference to `screen'
    globals.o: In function `Z10load_filesv':
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:128: undefined reference to `player'
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:131: undefined reference to `player'
    globals.o: In function `Z8clean_upv':
    C:\Users\The User\programming\@projects\platform_engine/globals.cpp:143: undefined reference to `player'
    collect2: ld returned 1 exit status
    
    mingw32-make.exe: *** [platform_engine.exe] Error 1
    
    Execution terminated

    I could have sworn visual c++ 2008 gave me the same error (although i believe it doesnt use mingw32, but do to the simplicity of the code at the moment, i dont think there would be any differences, since i set these both up using lazyfoos tutorial for setting the ide's up with windows)

    Tuesday, July 10, 2012 6:10 PM
  • On line 39 of main.cpp, you call the function SDL_PollEvent.  The argument of this funciton call is &event.  The compiler has no problem with this because in globals.h, you DECLARE event to be some kind of structure with
         extern SDL_Event event;

    It is important to realize that while this declaration tells the compiler everything it needs to know about the object named event, it does not DEFINE that object.  It simply tells the compiler that the object will be provided when the program is linked.  It also tells the compiler that the object will be defined with external linkage so that the linker can identify it and provide its address to any functions that need it.  The compiler then generates the appropriate code to use the address provided by the linker in order ot access the object.

    You need to define the object so that the linker can indeed find it and also so that the compiler can reserve space for it in one of the object modules.  This must be done EXACTLY once in one of the cpp files.  In order for the object to be defined with external linkage, if must be defined at file scope.  That is, the definition must appear ouside any function definition.  (These are often called global variables.)  The usual practice is to define these in main.cpp after any pre-processor directives and prior to the definition of main.  In this case, defining the variable in globals.cpp is also a viable option and preserves the parallelism between globals.h and globals.cpp.

    The situation is the same with the other undefined identifiers.  Note that the definition must match the declaration exactly.

    Tuesday, July 10, 2012 9:26 PM
  • im confused can you show me?
    Tuesday, July 10, 2012 11:00 PM
  • Exactly what are you confused about?

    Can you find the line that declares event in globals.h?  Can you copy that line into main.cpp (or globals.cpp)?  Can you place it between the last pre-processor directive and the first function definition?  Can you then edit the line to change it from a declaration to a definition by removing the "extern"?

    Can you repeat this process for all the other declaration which have no definition as listed in your linker error messages?

    What are you confused about?

    Wednesday, July 11, 2012 1:22 AM
  • my mind is drawing blanks the second that i read what youre talking about. Its alot easier for me to learn by studying different bits of code that im looking for, and put together in the right place. Right now im stuck on seperating file into different bits, but once someone shows me how it works fully functional, then i can sit down and mess around with everything, and then ill understand whats going on. Until then, I havent the slightest. The c++ book I bought, only small bits and peices make sense. It would be nice if I had it all ready for me to study :). thanks
    Wednesday, July 11, 2012 5:06 AM
  • What C++ book are you using?  Do you have any other programming experience?

    Which of the step by step instructions in my post do you not understand?  Start with the first one.  Can you find the line in globals.h which declares event?

    If not, then you need to put this project aside and start building you knowledge from the ground up.  You will not develop any fundamental understanding of programming,  the C++ language, or the Microsoft development environment by simply copying someone else's effort. 

    Wednesday, July 11, 2012 6:55 PM

  • C:\Users\The User\programming\@projects\platform_engine/main.cpp:45: undefined reference to `event'
    I find it interesting that event is not referenced on line 45 of either of the main.cpp files that you gave us, nor the main.cpp that I provided for you.  Perhaps (almost certainly) you're compiling different code that what you posted in this thread?
    Wednesday, July 11, 2012 7:31 PM
  • By my count, line 45 contains
           if( event.type == SDL_QUIT )
    in the main.cpp you posted on 10 July at 1:35AM.  Given the OP's reluctance to type anything himself, I'm fairly sure he is using that code.
    Wednesday, July 11, 2012 10:16 PM
  • The c++ book I have as I recall is "C++ How to Program (8th Edition) - Deitel".I copied and pasted the code that you modified from mine and it did not work. Until I know how its going to apply to my program, I have no idea what's going on lol. If you need the image thats loaded, here:

    Thursday, July 12, 2012 3:37 AM
  • A very useful skill in this forum is how to ask a question. If you say "I copied and pasted the code that you modified from mine and it did not work", you aren't going to get very useful answers here. The best answer I can think of given the dearth of information is: "You have a bug. Use your debugger to find and fix it." The answer is as useful to you as your posted question is to us.

    Suggestions for asking a question on help forums

    Thursday, July 12, 2012 4:12 AM
  • Oh, well im sorry about that. But I cant debug it cause it wont let me. Im really stumped.
    Thursday, July 12, 2012 4:32 AM
  • Until you make the recommended changes, your code will not compile.  You don't start debugging code until you get a successful compilation.
    Thursday, July 12, 2012 3:57 PM
  • im confused by that statement because i did do all your corrections and it still wont compile. how hard is it for someone trying to learn programming for some quick and easy help? i learn by studying code that correlates to 100% of what im looking for and i can build on it.
    Thursday, July 12, 2012 7:52 PM
  • There is no such thing as quick and easy help.  You get help when you give us enough information to actually determine what help you need.

    Your previous message said you didn't understand how to make the corrections.

    Show one of your .cpp modules that produces errors when it compiles.  Include your .h files that this module includes and the error messages from the compiler.  Cut and paste all of the above; do not retype or paraphrase.


    Thursday, July 12, 2012 10:14 PM
  • Those files I have are word for word as they are when Wyck posted them.
    Friday, July 13, 2012 6:01 AM
  • You need to make up your mind.  In a messages about 12 hours go, you claimed you made the changes I recommended two days ago.  Now you claim you are using the code from Wyck's post of three days ago.  Both statements cannot be true.

    You need to define objects in a .cpp file that the linker can use to resolve references to the external objects that are declared in your .h files.  Until you do so, your code is incomplete and will always generate linker errors.

    Friday, July 13, 2012 8:16 AM
  • I'd happily help you break up a program, but it's difficult because your code depends on a third party SDL libraries, so maybe if you upload a .zip of your original unrefactored and correctly compling project to skydrive, I'll just fix it for you.  How about that?

    Friday, July 13, 2012 3:21 PM
  • sure, I also have the code uploaded to pastebin. Ill zip it up in a bit :)
    Friday, July 13, 2012 4:16 PM
  • sure, I also have the code uploaded to pastebin. Ill zip it up in a bit :)
    Except that your project settings are also relevant, and so are the SDL libraries.  So you'll need to describe how they're set up on your computer or provide them in the zip.  I need something that compiles.
    Friday, July 13, 2012 9:27 PM
  • Show the compiler errors output when using the Visual C++ compiler.  This is a Visual C++ forum, after all.
    Saturday, July 14, 2012 12:54 PM
  • is it that hard for you to set up sdl on visual c++? ill do it later

    EDIT: Heres the entire project

    http://www.mediafire.com/?gv6wd6bcrcmgzvl


    Saturday, July 14, 2012 8:00 PM
  • First you add the following project settings

    Go->Project properties->Linker->Input and add SDL.lib SDLmain.lib SDL_image.lib

    Go->Project properties->Linker->General->Additional library directories  and add <Path>\SDL-devel-1.2.15-VC\SDL-1.2.15\lib\x86.

    Note: I have copied include and lib directories of SDL_image-devel-1.2.12-VC to SDL-1.2.15 to specify a single path

    Go->Project properties->C++->General->additional include directories and add <Path>\SDL-devel-1.2.15-VC\SDL-1.2.15\include.

    Constants.h

    #ifndef CONSTANTS_H_INCLUDED__
    #define CONSTANTS_H_INCLUDED__
    //Screen attributes
    const int SCREEN_WIDTH = 320;
    const int SCREEN_HEIGHT = 240;
    const int SCREEN_BPP = 32;
    
    //The frames per second
    const int FRAMES_PER_SECOND = 60;
    
    //The dimenstions of the player
    const int PLAYER_WIDTH = 32;
    const int PLAYER_HEIGHT = 32;
    
    //The constant speed of the player (when moving)
    const int PLAYER_SPEED = 3.14;
    
    //The direction status of the player
    const int PLAYER_RIGHT = 0;
    const int PLAYER_LEFT = 1;
    #endif  // CONSTANTS_H_INCLUDED__

    Globals.h

    #ifndef GLOBALS_H_INCLUDED__
    #define GLOBALS_H_INCLUDED__
    #include "SDL.h"
    #include "SDL_image.h"
    #include <string>
    
    //The surfaces
    static SDL_Surface *player = NULL;
    static SDL_Surface *screen = NULL;
    
    //The event structure
    static SDL_Event event;
    
    //The areas of the sprite sheet
    static SDL_Rect clipsRight[ 5 ];
    static SDL_Rect clipsLeft[ 5 ];
    void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip );
    void clean_up();
    bool init();
    bool load_files();
    SDL_Surface *load_image( std::string filename );
    void set_clips();
    #endif  // GLOBALS_H_INCLUDED__

    Globals.cpp

    #include "Globals.h"
    #include "Constants.h"
    extern SDL_Surface *player;
    extern SDL_Surface *screen ;
    
    //The event structure
    extern SDL_Event event;
    
    //The areas of the sprite sheet
    extern SDL_Rect clipsRight[ 5 ];
    extern SDL_Rect clipsLeft[ 5 ];
    
    void set_clips()
    {
    	//Clip the sprites
    	clipsRight[ 0 ].x = 0;
    	clipsRight[ 0 ].y = 0;
    	clipsRight[ 0 ].w = PLAYER_WIDTH;
    	clipsRight[ 0 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 1 ].x = PLAYER_WIDTH;
    	clipsRight[ 1 ].y = 0;
    	clipsRight[ 1 ].w = PLAYER_WIDTH;
    	clipsRight[ 1 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 2 ].x = PLAYER_WIDTH * 2;
    	clipsRight[ 2 ].y = 0;
    	clipsRight[ 2 ].w = PLAYER_WIDTH;
    	clipsRight[ 2 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 3 ].x = PLAYER_WIDTH * 3;
    	clipsRight[ 3 ].y = 0;
    	clipsRight[ 3 ].w = PLAYER_WIDTH;
    	clipsRight[ 3 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 4 ] = clipsRight[ 2 ];
    
    	clipsLeft[ 0 ].x = 0;
    	clipsLeft[ 0 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 0 ].w = PLAYER_WIDTH;
    	clipsLeft[ 0 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 1 ].x = PLAYER_WIDTH;
    	clipsLeft[ 1 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 1 ].w = PLAYER_WIDTH;
    	clipsLeft[ 1 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 2 ].x = PLAYER_WIDTH * 2;
    	clipsLeft[ 2 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 2 ].w = PLAYER_WIDTH;
    	clipsLeft[ 2 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 3 ].x = PLAYER_WIDTH * 3;
    	clipsLeft[ 3 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 3 ].w = PLAYER_WIDTH;
    	clipsLeft[ 3 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 4 ] = clipsLeft[ 2 ];
    }
    
    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( "Platform Engine", NULL );
    
    	//If everything initialized fine
    	return true;
    }
    
    bool load_files()
    {
    	//Load the sprite sheet
    	player = load_image( "player.png" );
    
    	//If there was a problem in loading the sprite
    	if( player == NULL )
    	{
    		return false;
    	}
    
    	//If everything loaded fine
    	return true;
    }
    
    void clean_up()
    {
    	//Free the surface
    	SDL_FreeSurface( player );
    
    	//Quit SDL
    	SDL_Quit();
    }


    Player.h

    #ifndef PLAYER_H_INCLUDED__
    #define PLAYER_H_INCLUDED__
    
    //The player
    class Player
    {
    private:
    	//The offset
    	int offSet;
    
    	//Its rate of movement
    	int velocity;
    
    	//Its current frame
    	int frame;
    
    	//Its animation status
    	int status;
    
    public:
    	//Initializes the variables
    	Player();
    
    	//Handles input
    	void handle_events();
    
    	//Moves the player
    	void move();
    
    	//Shows the player
    	void show();
    };
    #endif  // PLAYER_H_INCLUDED__

    Player.cpp

    #include "Player.h"
    #include "Constants.h"
    #include "Globals.h"
    Player::Player()
    {
    	//Initialize movement variables
    	offSet = 0;
    	velocity = 0;
    
    	//Initialize animation variables
    	frame = 0;
    	status = PLAYER_RIGHT;
    }
    
    void Player::handle_events()
    {
    	//If a key was pressed
    	if( event.type == SDL_KEYDOWN )
    	{
    		//Set the velocity
    		switch( event.key.keysym.sym )
    		{
    		case SDLK_RIGHT: velocity += PLAYER_SPEED; break;
    		case SDLK_LEFT: velocity -= PLAYER_SPEED; break;
    		}
    	}
    	//If a key was released
    	else if( event.type == SDL_KEYUP )
    	{
    		//Set the velocity
    		switch( event.key.keysym.sym )
    		{
    		case SDLK_RIGHT: velocity -= PLAYER_SPEED; break;
    		case SDLK_LEFT: velocity += PLAYER_SPEED; break;
    		}
    	}
    }
    
    void Player::move()
    {
    	//Move
    	offSet += velocity;
    
    	//Keep the player in bounds
    	if( ( offSet < 0 ) || ( offSet + PLAYER_WIDTH > SCREEN_WIDTH ) )
    	{
    		offSet -= velocity;
    	}
    }
    
    void Player::show()
    {
    	//If Player is moving left
    	if( velocity < 0 )
    	{
    		//Set the animation to left
    		status = PLAYER_LEFT;
    
    		//Move to the next frame in the animation
    		frame++;
    	}
    	//If Player is moving right
    	else if( velocity > 0 )
    	{
    		//Set the animation to right
    		status = PLAYER_RIGHT;
    
    		//Move to the next frame in the animation
    		frame++;
    	}
    	//If Player standing
    	else
    	{
    		//Restart the animation
    		frame = 0;
    	}
    
    	//Loop the animation
    	if( frame >= 5 )
    	{
    		frame = 1;
    	}
    
    	//Show the player
    	if( status == PLAYER_RIGHT )
    	{
    		apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsRight[ frame ] );
    	}
    	else if( status == PLAYER_LEFT )
    	{
    		apply_surface( offSet, SCREEN_HEIGHT - PLAYER_HEIGHT, player, screen, &clipsLeft[ frame ] );
    	}
    }


    Timer.h

    #ifndef TIMER_H_INCLUDED__
    #define TIMER_H_INCLUDED__
    //The timer
    class Timer
    {
    private:
    	//The clock time when the timer started
    	int startTicks;
    
    	//The ticks stored when the timer was paused
    	int pausedTicks;
    
    	//The timer status
    	bool paused;
    	bool started;
    
    public:
    	//Initializes variables
    	Timer();
    
    	//The various clock actions
    	void start();
    	void stop();
    	void pause();
    	void unpause();
    
    	//Gets the timer's time
    	int get_ticks();
    
    	//Checks the status of the timer
    	bool is_started();
    	bool is_paused();
    };
    #endif  // TIMER_H_INCLUDED__

    Timer.cpp

    #include "Timer.h"
    #include "SDL.h"
    Timer::Timer()
    {
    	//Initialize the variables
    	startTicks = 0;
    	pausedTicks = 0;
    	paused = false;
    	started = false;
    }
    
    void Timer::start()
    {
    	//Start the timer
    	started = true;
    
    	//Unpause the timer
    	paused = false;
    
    	//Get the current clock time
    	startTicks = SDL_GetTicks();
    }
    
    void Timer::stop()
    {
    	//Stop the timer
    	started = false;
    
    	//Unpause the timer
    	paused = false;
    }
    
    void Timer::pause()
    {
    	//If the timer is running and isn't already paused
    	if( ( started == true ) && ( paused == false ) )
    	{
    		//Pause the timer
    		paused = true;
    
    		//Calculate the paused ticks
    		pausedTicks = SDL_GetTicks() - startTicks;
    	}
    }
    
    void Timer::unpause()
    {
    	//If the timer is paused
    	if( paused == true )
    	{
    		//Unpause the timer
    		paused = false;
    
    		//Reset the starting ticks
    		startTicks = SDL_GetTicks() - pausedTicks;
    
    		//Reset the paused ticks
    		pausedTicks = 0;
    	}
    }
    
    int Timer::get_ticks()
    {
    	//If the timer is running
    	if( started == true )
    	{
    		//If the timer is paused
    		if( paused == true )
    		{
    			//Return the number of ticks when the timer was paused
    			return pausedTicks;
    		}
    		else
    		{
    			//Return the current time minus the start time
    			return SDL_GetTicks() - startTicks;
    		}
    	}
    
    	//If the timer isn't running
    	return 0;
    }
    
    bool Timer::is_started()
    {
    	return started;
    }
    
    bool Timer::is_paused()
    {
    	return paused;
    }

    Main.cpp

        //The headers
        #include <string>
        #include <iostream>
    	#include "Constants.h"
        #include "Globals.h"
    #include "Player.h"
    #include "Timer.h"
    
        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();
         
            //The frame rate regulator
            Timer fps;
         
            //The player
            Player walk;
         
            //While the user hasn't quit
            while( quit == false )
            {
                //Start the frame timer
                fps.start();
         
                //While there's events to handle
                while( SDL_PollEvent( &event ) )
                {
                    //Handle events for the player
                    walk.handle_events();
         
                    //If the user has Xed out the window
                    if( event.type == SDL_QUIT )
                    {
                        //Quit the program
                        quit = true;
                    }
                }
         
                //Move the player
                walk.move();
         
                //Fill the screen white
                SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
         
                //Show the player on the screen
                walk.show();
         
                //Update the screen
                if( SDL_Flip( screen ) == -1 )
                {
                    return 1;
                }
         
                //Cap the frame rate
                if( fps.get_ticks() < 6000 / FRAMES_PER_SECOND )
                {
                    SDL_Delay( ( 6000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
                }
            }
         
            //Clean up
            clean_up();
         
            return 0;
        }


    Thanks, Renjith V R

    Monday, July 16, 2012 6:46 PM
  • If the "event" in player.cpp is supposed to refer to the same object as the "event" in main.cpp, then you need to replace the static qualifer from event in globals.h with the extern qualifier.  In globals.cpp you need to delete the extern qualifier. 

    As it stands now, each cpp that includes global.h ends up with its own internal copy of an event.  Further more, the compilation of globals.cpp should fail since event is specified with both internal and external linkage.

    Monday, July 16, 2012 7:36 PM
  • yeah what I was shown I did the following

    http://www.mediafire.com/?8iur555s25dw7ch

    and the game blew up

    Monday, July 16, 2012 11:01 PM
  • Sorry i missed it.Small changes in following files

    Globals.h

    #ifndef GLOBALS_H_INCLUDED__
    #define GLOBALS_H_INCLUDED__
    #include "SDL.h"
    #include "SDL_image.h"
    #include <string>
    
    extern SDL_Surface *player;
    extern SDL_Surface *screen ;
    
    //The event structure
    extern SDL_Event event;
    
    //The areas of the sprite sheet
    extern SDL_Rect clipsRight[ 5 ];
    extern SDL_Rect clipsLeft[ 5 ];
    
    void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip );
    void clean_up();
    bool init();
    bool load_files();
    SDL_Surface *load_image( std::string filename );
    void set_clips();
    #endif  // GLOBALS_H_INCLUDED__

    Globals.cpp

    #include "Globals.h"
    #include "Constants.h"
    
    //The surfaces
    SDL_Surface *player = NULL;
    SDL_Surface *screen = NULL;
    
    //The event structure
    SDL_Event event;
    
    //The areas of the sprite sheet
    SDL_Rect clipsRight[ 5 ];
    SDL_Rect clipsLeft[ 5 ];
    
    void set_clips()
    {
    	//Clip the sprites
    	clipsRight[ 0 ].x = 0;
    	clipsRight[ 0 ].y = 0;
    	clipsRight[ 0 ].w = PLAYER_WIDTH;
    	clipsRight[ 0 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 1 ].x = PLAYER_WIDTH;
    	clipsRight[ 1 ].y = 0;
    	clipsRight[ 1 ].w = PLAYER_WIDTH;
    	clipsRight[ 1 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 2 ].x = PLAYER_WIDTH * 2;
    	clipsRight[ 2 ].y = 0;
    	clipsRight[ 2 ].w = PLAYER_WIDTH;
    	clipsRight[ 2 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 3 ].x = PLAYER_WIDTH * 3;
    	clipsRight[ 3 ].y = 0;
    	clipsRight[ 3 ].w = PLAYER_WIDTH;
    	clipsRight[ 3 ].h = PLAYER_HEIGHT;
    
    	clipsRight[ 4 ] = clipsRight[ 2 ];
    
    	clipsLeft[ 0 ].x = 0;
    	clipsLeft[ 0 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 0 ].w = PLAYER_WIDTH;
    	clipsLeft[ 0 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 1 ].x = PLAYER_WIDTH;
    	clipsLeft[ 1 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 1 ].w = PLAYER_WIDTH;
    	clipsLeft[ 1 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 2 ].x = PLAYER_WIDTH * 2;
    	clipsLeft[ 2 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 2 ].w = PLAYER_WIDTH;
    	clipsLeft[ 2 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 3 ].x = PLAYER_WIDTH * 3;
    	clipsLeft[ 3 ].y = PLAYER_HEIGHT;
    	clipsLeft[ 3 ].w = PLAYER_WIDTH;
    	clipsLeft[ 3 ].h = PLAYER_HEIGHT;
    
    	clipsLeft[ 4 ] = clipsLeft[ 2 ];
    }
    
    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( "Platform Engine", NULL );
    
    	//If everything initialized fine
    	return true;
    }
    
    bool load_files()
    {
    	//Load the sprite sheet
    	player = load_image( "player.png" );
    
    	//If there was a problem in loading the sprite
    	if( player == NULL )
    	{
    		return false;
    	}
    
    	//If everything loaded fine
    	return true;
    }
    
    void clean_up()
    {
    	//Free the surface
    	SDL_FreeSurface( player );
    
    	//Quit SDL
    	SDL_Quit();
    }



    Thanks, Renjith V R

    Tuesday, July 17, 2012 5:01 PM
  • It works! thank you so much! Now I will study this for when I add onto it :).
    Wednesday, July 18, 2012 1:24 AM
  • Please close your question by marking as answer which helped you.

    Thanks, Renjith V R


    Wednesday, July 18, 2012 5:39 AM