Page 1 of 1
[SDL]beginners question
Posted: Mon Jun 15, 2009 9:16 am
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
Re: [SDL]beginners question
Posted: Mon Jun 15, 2009 10:16 am
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.
Re: [SDL]beginners question
Posted: Mon Jun 15, 2009 10:34 am
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.
Re: [SDL]beginners question
Posted: Mon Jun 15, 2009 2:12 pm
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.
Re: [SDL]beginners question
Posted: Mon Jun 15, 2009 6:48 pm
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.