Whats wrong

So i’ve been using lazy foo’ tutorials for SDL2 but im stuck in lesson 2, when i run the program the window only opens like less than a second and then it closes, theres not even an image, i have tried all, i even ran lazy foo’s program and its the same, is it my computer?

by lesson 2, do you mean

did you include this line

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

which is near the bottom of the tutorial ?

Please show the code you currently have.

It’s probably like jamesL has written, that you don’t have a mainloop created/defined.

yes i included it, when i put it out of the mainloop the “hack” works but the program is all black

#include <SDL.h>
#include <stdio.h>

const int ANCHO_PANTALLA = 640;
const int LARGO_PANTALLA = 480;
bool init();

bool loadMedia();

void close();

SDL_Window* gWindow = NULL;
		
SDL_Surface* gScreenSurface = NULL;

SDL_Surface* gHelloWorld = NULL;


int main(int argc, char* args[] ){
	if(!init())
	{
		printf("No se pudo iniciar");
	}
	else
	{
		if(!loadMedia())
		{
			printf("Error en el Loadmedia");
		}
		else
		{
			SDL_BlitSurface(gHelloWorld,NULL,gScreenSurface,NULL);

			SDL_UpdateWindowSurface(gWindow);
			
			SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
		}
	}  
	
	close();
	return 0;
}

bool init(){
	bool success = true;

	if( SDL_Init(SDL_INIT_VIDEO) < 0){
		printf("SDL no se pudo iniciar! SDL_Error: %s\n",SDL_GetError());
		success = false;
	}
	else{
		gWindow = SDL_CreateWindow("SDL tutorial", SDL_WINDOWPOS_UNDEFINED,SDL_WINDOWPOS_UNDEFINED,ANCHO_PANTALLA,LARGO_PANTALLA,SDL_WINDOW_SHOWN);
		if(gWindow == NULL){
			printf("Error en la window, SDL_Error: %s\n",SDL_GetError());
		}
		else{
			gScreenSurface = SDL_GetWindowSurface(gWindow);
		}
	}
	return success;
}

bool loadMedia(){
	bool success = true;

	gHelloWorld = SDL_LoadBMP("descarga.bmp");
	if(gHelloWorld == NULL)
	{
	printf("Error en el loadMedia SDL_Error: %s\n",SDL_GetError());
		success = false;
	}
	return success;
}

void close(){
	SDL_FreeSurface(gHelloWorld);
	gHelloWorld = NULL;

	SDL_DestroyWindow(gWindow);
	gWindow = NULL;

	SDL_Quit();
}

so your main looks like this ?


int main( int argc, char* args[] )
{
	//Start up SDL and create window
	if( !init() )
	{
		printf( "Failed to initialize!\n" );
	}
	else
	{
		//Load media
		if( !loadMedia() )
		{
			printf( "Failed to load media!\n" );
		}
		else
		{
			//Apply the image
			SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
			
			//Update the surface
			SDL_UpdateWindowSurface( gWindow );

			//Hack to get window to stay up
			SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
		}
	}

	//Free resources and close SDL
	close();

	return 0;
}

indeed, ive tried everything, but its the same result, i dont know if its the image, the load, if it doesnt hack, im going crazy trying to find a mistake

I think your image exists in one folder, and the program is running in a different folder.
You need to either establish an absolute path to the image, or a relative path from the working directory. The working directory is where the program thinks it is running from.

Edit: The rest of this post is incorrect, please skip to the next post.
Here’s some lines of code that will report where the working directory is, but if you are using an IDE then you first need to find the console to read it from:

// This does not work for the intended purpose, as corrected by Peter87 below. 
// please ignore this post.
#include <SDL.h>
int main()
{
	SDL_Init(SDL_INIT_VIDEO);
	SDL_Log("You Are Here: %s", SDL_GetBasePath());
	SDL_Delay(9000);
	SDL_Quit();
}

I hope this helps you figure out a good place for the image file.

SDL_GetBasePath() basically gives you the path to the directory where the exe file is located. This is not necessarily the same as the process’s working directory (which is what’s used for relative paths).

1 Like

the image is in the same directory as the .exe file and the .cpp file

In that case you probably want to prepend SDL_GetBasePath() to the file path before passing it to SDL_LoadBMP.

i added the SDL_GetBasePath()it shows nothing, i even put a printf on the thing and it shows nothing i added this code =

 char* base_path = SDL_GetBasePath();

    if(base_path)

    {

        printf("LA APP SE HACE EN %s\n", base_path);

        SDL_free(base_path);

    }

    else

    {

        fprintf(stderr, "Nose pudo %s\n",SDL_GetError());

    }

before the SDL_LoadBMPand it shows nothing, is this how sisyphus felt? is this what awaits for me as a beginner programmer?

Are you looking for the printf output in the terminal/console window (or wherever your IDE shows these messages, if you use one)?

You might want to try SDL_Log and see if that works better.

SDL_Log("LA APP SE HACE EN %s\n", base_path);

Make sure the code is actually executed.

Otherwise, someone with more experience of using SDL on Windows than me might be able to tell you what is going on. I know that SDL 1.2 used to redirect output to files named stdout.txt and stderr.txt but I don’t think that is the case with SDL 2.


The program that you posted earlier should work and display the image if you start it by double clicking on the exe file (assuming the file name is correct and located where you say it is). It should also work (no matter how or where you start the program) if you explicitly specify the full image path in the code. You might want to try this to rule out other issues.

On Windows systems older than 11, this may not be true. At least since XP, Windows has had this strange quirk whereby applications launched by double-clicking from a custom address bar on the taskbar would have incorrect executable paths in argv[0], which could lead to non-existent locations if the program used relative paths, resulting in read errors.

Windows 11 no longer has this feature—you can’t add a custom address bar to the taskbar, so that’s no problem. However, it’s worth keeping in mind that the user may have an older Windows system (Windows 10 is still popular), and you need to ensure it works on them as well.

Therefore, on Windows systems, applications are installed. This means the user tells the installer where to extract the program files and adds the path to this directory to special registry keys. However, when the application starts, it reads this path from the registry and uses it—either setting it as the current directory (so that relative file paths can be used) or appending file names to it (creating absolute file paths).

1 Like

Good to know. I wasn’t aware. When I said that double clicking should work with relative paths I was thinking about using the file explorer.

Would SDL_GetBasePath() return the wrong path in this case?

i have windows 11, i did what peter87 suggested with the SDL_Log()it keeps showing nothing, i feel like god is no more with me, why? am i not one of his children? why gods of computation have you decided to curse me with this shackle, what purpose does my failure have?

Have you tried printing something right before and after SDL_Init?

Are you able to see the output of other non-SDL programs that you have written in C++?

yes im able to see output from other programs, even in the same folder, even other SDL programs without image of course only a white window

Are you saying that you’re able to see printf output from other SDL programs?

If you change the image file path so that it is incorrect, then the window will close immediately, right?

If you restore the file path and change the while loop in main to …

SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } SDL_Surface* ws = SDL_GetWindowSurface(gWindow); SDL_FillRect(ws, NULL, SDL_MapRGB(ws->format, 0xFF, 0x00, 0x00)); SDL_UpdateWindowSurface(gWindow); }

… do you see a red window?

i cant see a printf out of any SDL program, maybe SDL is messing with the output or the output is showing somewhere else.