I start out with this to make sure I can get a blank screen up. It works fine, all in one file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 400;
const int SCREEN_BPP = 32;
SDL_Surface *display = NULL;
int main(int argc, char* args[]) {
if( SDL_Init( SDL_INIT_EVERYTHING) == -1 ) return 1;
display = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if(!display) return 1;
SDL_Flip( display );
SDL_Delay( 2000 );
SDL_FreeSurface( display );
SDL_Quit();
return 0;
}
|
Then I put the constants into the constants.h and the SDL_Surface into the globals.h This works fine too,
the globals and constants are getting scope in main.cpp
1 2 3 4 5
|
// constants.h
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 400;
const int SCREEN_BPP = 32;
|
1 2
|
// globals.h
SDL_Surface *display = NULL;
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
//main.cpp
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "constants.h"
#include "globals.h"
int main(int argc, char* args[]) {
if( SDL_Init( SDL_INIT_EVERYTHING) == -1 ) return 1;
display = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if(!display) return 1;
SDL_Flip( display );
SDL_Delay( 2000 );
SDL_FreeSurface( display );
SDL_Quit();
return 0;
}
|
Now I am adding a class for the Hero of the game.. hero.h and hero.cpp, this is where the problems are
coming in play...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
//hero.h
class Hero {
private:
int x;
int y;
public:
Hero();
int GetX() { return x; }
int GetY() { return y; }
};
|
1 2 3 4 5 6 7 8 9
|
// hero.cpp
#include "hero.h"
Hero::Hero() {
x = SCREEN_WIDTH / 2;
y = SCREEN_HEIGHT / 2;
}
|
then I tried includeing into the top of main both ways:
1 2 3 4 5 6
|
// top of main.cpp
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "constants.h"
#include "globals.h"
#include "hero.h"
|
1 2 3 4 5 6
|
// top of main.cpp second attempt
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "constants.h"
#include "globals.h"
#include "hero.cpp"
|
When I run the code like this, I get this error:
error: 'SCREEN_WIDTH' was not declared in this scope
error: 'SCREEN_HEIGHT' was not declared in this scope
the errors light up the line in the Hero.h file where x = SCREEN_WIDTH / 2;
I just cant understand it, because I am looking at other examples with the code doing exactly this.. using
the constants from files include above it. How come mine is failing on me?