Page 1 of 1

C++ SDL ERROR NEED HELP!!!

Posted: Tue Apr 26, 2011 2:24 pm
by VoidElite
I'm getting 'screen' has not been declared in this scope but 'screen' is declared in 'Content.h' which is being included into 'Sprite.h'.

Here's 'Sprite.h':

Code: Select all

#include "Content.h"

using namespace std;

#ifndef SPRITE
#define SPRITE
class Sprite{
	public:
		Sprite();
        Sprite(string file2,int x2,int y2,bool visible2);
		void show();
		void hide();
		string file;
		int x;
		int y;
		bool visible;
	private:
		SDL_Surface *bmp;
		SDL_Rect offset;
};
Sprite::Sprite(){
	file="null.bmp";
	bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
	x=0;
	y=0;
	offset.x=x;
	offset.y=y;
	visible=false;
}
Sprite::Sprite(string file2,int x2,int y2,bool visible2){
	file=file2;
	bmp=SDL_DisplayFormat(SDL_LoadBMP(file.c_str()));
	x=x2;
	y=y2;
	offset.x=x;
	offset.y=y;
	visible=visible2;
}
void Sprite::show(){
	SDL_BlitSurface(bmp,NULL,screen,&offset);
}
void Sprite::hide(){
	SDL_FreeSurface(bmp);
	delete this; 
}
#endif
Anywhoo here's 'Content.h':

Code: Select all

#ifndef CONTENT
#define CONTENT
#include <iostream>
#include "SDL\SDL.h"
#include "windows.h"
#include "Environment.h"
#include "Sprite.h"
#include "Tile.h"
#include "Map.h"
#include "World.h"
#include "Object.h"
#include "Creature.h"
#include "Npc.h"
#include "Player.h"

using namespace std;

int mapIndex=0;

SDL_Surface *screen=NULL; //SDL's screen
Sprite *sprite=new Sprite("dude.bmp",10,10,true); //test sprite
Player *player=new Player(); //Main Player
World *world=new World(); //the world
#endif
Help once again would be greatly appreciated. :)

Re: C++ SDL ERROR NEED HELP!!!

Posted: Tue Apr 26, 2011 3:42 pm
by BlobOfFailure
Well, the error is caused because the screen is defined in a different file than the sprite class. To solve the problem you could make the surface a global by adding extern keyword infront of it.

Code: Select all

extern SDL_SURFACE* screen; // The screen
You then will have to declear it once more in a source file, and then every time you include the header containing the defined global you can access it. However most people don't use globals because they are messy and hard to keep track of. A better approach is to write a base class containing a static instance of the screen variable, and then have the sprite class inherit from the base class to use the variable.

Code: Select all

//Content.h
#ifndef CONTENT
#define CONTENT
//...

class B // For lack of a better name
{
    protected:
    static SDL_Surface *screen;

};

#endif
Then to access the screen,

Code: Select all

//Sprite.h
#ifndef SPRITE
#define SPRITE
#include "content.h"

class Sprite: public B
{
	public:
	//... All your functions and stuff
	
	private:
	SDL_Surface* screen;
	//...
};

#endif
Now, you can simpily use the screen, and only have access to it when you need it. However in order to use it now that it's in a class you may have to write a screen manager class which also inherits from the base class.

Code: Select all

//ScreenManager.h
#ifndef SM
#define SM
#include "content.h"

class ScreenManager: public B
{
	public:
	ScreenManager(); // Constructor to set up the screen.
	void update(); // Call during the run loop to update the screen
	
	private:
	SDL_Surface *screen;
	
};

#endif 

//ScreenManager.cpp
#include "ScreenManager.h"

ScreenManager::ScreenManager()
{
	SDL_Init(SDL_INIT_EVERYTHING); // If you plan on initilizing sdl in the Screen Manager
	
	screen = SDL_SetVideoMode(640, 640, 32, SDL_SWSURFACE);
	
	if(screen==NULL) // In-case of an error
	{
		SDL_Quit();
		exit(1);
	}
}

void ScreenManager::update()
{
	SDL_Flip(screen);
}


Then your main loop might look something like this.

Code: Select all


void Game::run()
{

//...
while(InputManager->getInput()==false)
{
	Graphics->draw();
	Screen->update();
	fpsLimit(); 
}
//...

}
Thats about it. Hope it helps, I had that same problem then I finally found this way out, and althought there are several other ways to go about the buffer issue but this is the best in my opinion.