[SDL]beginners question

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
slayeriq
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Mon Jun 15, 2009 9:01 am

[SDL]beginners question

Post by slayeriq »

Hello guys,

Yesterday i started following the tutorials on lazyfoo. I am tyring to enhance my c++ skills and learn sdl at the same time everything goes great. Only i have 1 question when i compile my program it works in vc++ 2008, but when i go to the release folder inside my project folder and open the executable then i flashes and the program ends.

Am i missing something or am i doing something wrong.

Thanks in advance,

EDIT: http://pastebin.com/m1d41d9f5 that the code.

Slayeriq
User avatar
dani93
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Mon Apr 13, 2009 9:38 am
Location: Austria

Re: [SDL]beginners question

Post by dani93 »

Make cout's before the return's, then you see where the code stops.

Code: Select all

#include "SDL.h"
#include "SDL_image.h"
#include <string>

#include <iostream>
using namespace std;

//screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

//The surfaces that will be used 
SDL_Surface *image = NULL; 
SDL_Surface *screen = NULL;
SDL_Event event;

SDL_Surface *load_image(std::string filename)
{
	SDL_Surface *loadedImage = NULL;
	SDL_Surface *optimizedImage = NULL;

	loadedImage = IMG_Load(filename.c_str());

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

	return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface *source, SDL_Surface *destination)
{
	SDL_Rect offset;

	offset.x = x;
	offset.y = y;

	SDL_BlitSurface(source, NULL, destination, &offset);
}

bool init()
{
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
	  cout << "init" << endl;
		return false;
	}

	screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE | SDL_DOUBLEBUF);
	
	if(screen == NULL)
	{
	  cout << "videomode" << endl;
		return false;
	}

	SDL_WM_SetCaption("Hello world", NULL);
	return true;
}

bool load_files()
{
	image = load_image("x.png");

	if(image == NULL)
	{
	  cout << "image" << endl;
		return false;
	}

	return true;
}

void clean_up()
{
	SDL_FreeSurface(image);
	SDL_Quit();
}

int main( int argc, char* args[] ) 
{
	bool quit = false;

	if(init() == false)
	{
		return 1;
	}

	if(load_files() == false)
	{
		return 1;
	}

	apply_surface(0, 0, image, screen);

	if( SDL_Flip( screen ) == -1 ) 
	{ 
		return 1; 
	}

	while(quit == false)
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
			{
				quit = true;
			}
		}
	}

	clean_up();

	return 0;
}
My output is "image" (I don't have the image...). So the program couldn't load the image. Is "x.png" in the same directory as the executable? Or maybe it also works if it is in the project folder.


EDIT: It works if I put an image called "x.png" in the same directory.
slayeriq
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 2
Joined: Mon Jun 15, 2009 9:01 am

Re: [SDL]beginners question

Post by slayeriq »

Thanks for the reply, yes it was the image but its weird that when i compile in the IDE it links to another image.
herby490
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Thu Feb 12, 2009 5:59 pm

Re: [SDL]beginners question

Post by herby490 »

slayeriq wrote:Thanks for the reply, yes it was the image but its weird that when i compile in the IDE it links to another image.
I'm pretty sure that when you run the program from the release folder all the files(images, etc) need to be in the same folder as the exe but when you run it from the IDE the files need to be in the folder with the source files.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: [SDL]beginners question

Post by MarauderIIC »

herby490 wrote:I'm pretty sure that when you run the program from the release folder all the files(images, etc) need to be in the same folder as the exe but when you run it from the IDE the files need to be in the folder with the source files.
This is correct.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Post Reply