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
[SDL]beginners question
Moderator: Coders of Rage
Re: [SDL]beginners question
Make cout's before the return's, then you see where the code stops.
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.
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;
}
EDIT: It works if I put an image called "x.png" in the same directory.
Re: [SDL]beginners question
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
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.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.
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: [SDL]beginners question
This is correct.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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.