Jump to content
  • Advertisement
Sign in to follow this  
dolphins87

getting graphic to display using classes?

This topic is 3902 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to write a class to display the a background to the screen just using SDL and I have tried to write it into a class. I can set up the screen and everything but I cannot get the background to display on the screen and I have been through it but I cannot find out why. graphics.h
include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>

class c_Graphics
{
public:
	// constructor that set the parameters for the screen and the window caption
	// and initalizes SDL
	c_Graphics(int screenWidth, int screenHeight, int screenBpp, const char *windowCaption); 

	~c_Graphics(); // destructor which cleans up
	
	// loads the images into the surfaces
	SDL_Surface *load_image(std::string filename);

	// apply the selected surface at position X and Y to the destination surface
	void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip = NULL);

	// load the files into there surfaces
	void load_file(SDL_Surface *bitmap, std::string filename);

	// return the screen
	SDL_Surface * returnScreen();

	// update the screen
	void update();
private:
	SDL_Surface *screen;

};

graphics.cpp
#include "c_Graphics.h"

c_Graphics::c_Graphics(int screenWidth, int screenHeight, int screenBpp, const char* windowCaption)
{
	SDL_Init(SDL_INIT_EVERYTHING);

	screen = SDL_SetVideoMode(screenWidth,screenHeight,screenBpp, SDL_SWSURFACE);

	SDL_WM_SetCaption(windowCaption,NULL);
}

c_Graphics::~c_Graphics()
{}

SDL_Surface *c_Graphics::load_image(std::string filename)
{
	// two surfaces uses. One to load the image and another to optimizes the image
	SDL_Surface *loadedImage = NULL;
	SDL_Surface *optimizedImage = NULL;

	// load the image in the loadedImage surface
	loadedImage = IMG_Load(filename.c_str());

	if(loadedImage != NULL)
	{
		optimizedImage = SDL_DisplayFormat(loadedImage);

		SDL_FreeSurface(loadedImage);

		if(optimizedImage != NULL)
		{
			SDL_SetColorKey(optimizedImage,SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format,0xFF,0xFF,0xFF));
		}
	}

	// return the optimizedImage
	return optimizedImage;
}

void c_Graphics::apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination, SDL_Rect *clip)
{
	// the offsets to apply at
	SDL_Rect offsets;

	offsets.x = x;
	offsets.y = y;

	// blit the surface to the screen
	SDL_BlitSurface(source,clip,destination,&offsets);
}

void c_Graphics::load_file(SDL_Surface *bitmap,std::string filename)
{
	bitmap = load_image(filename);
}

SDL_Surface *c_Graphics::returnScreen()
{
	return screen;
}

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

main.cpp
#include "c_Graphics.h"

// constants for the screen
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

// the surface for the background
SDL_Surface *background = NULL;

int main(int argc, char *args[])
{
	c_Graphics render(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,"Testing Engine");

	render.load_file(background,"background.png");

	render.apply_surface(0,0,background,render.returnScreen());

	SDL_Flip(render.returnScreen());

        // used so I dont have to enter a loop just to see briefly enough to see image on screen
	SDL_Delay(4000);



	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
First things first:
"SDL_Flip(render.returnScreen())": you defined a function to do this, and should probably stick with using it ("void update()").

I don't quite see where the problem is either. I'd recommend either going through it with a debugger, or just outputting "std::cout << "finished initializing" << std::endl", which is the poorman's debugger. I'd guess that your problem is something silly, like the .png not loading or you messing up the order of the destination or source surfaces.

Experiment!

Share this post


Link to post
Share on other sites
I believe that you've got a pointer problem with load_file, in that background still points to NULL before and after the function.

The easiest solution: Have load_file return the SDL_Surface* instead of passing it in (otherwise you'll have to pass in an SDL_Surface** or SDL_Surface*& [ordering might be off there]).

Cheers,
--Brian

Share this post


Link to post
Share on other sites
yep that was it. Now that you say something background is local in the function call so after the function is over it resets background to NULL therefore have no image stored in the file. Thank you.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!