Advertisement

SDL2 image

Started by March 14, 2024 02:37 AM
18 comments, last by NubDevice 9 months ago

I am trying to draw an bmp image using vs2019 and SDL2. I am getting the following error message.

Here is my code.

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//Starts up SDL and creates window
bool init();

//Loads media
bool loadMedia();

//Frees media and shuts down SDL
void close();

//The window we'll be rendering to
SDL_Window* gWindow = NULL;
	
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;

//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;

bool init()
{
	//Initialization flag
	bool success = true;

	//Initialize SDL
	if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
	{
		printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
		success = false;
	}
	else
	{
		//Create window
		gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
		if( gWindow == NULL )
		{
			printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
			success = false;
		}
		else
		{
			//Get window surface
			gScreenSurface = SDL_GetWindowSurface( gWindow );
		}
	}

	return success;
}

bool loadMedia()
{
	//Loading success flag
	bool success = true;

	//Load splash image
	gHelloWorld = SDL_LoadBMP( "hello_world.bmp" );
	if( gHelloWorld == NULL )
	{
		printf( "Unable to load image %s! SDL Error: %s\n", "hello_world.bmp", SDL_GetError() );
		success = false;
	}

	return success;
}

void close()
{
	//Deallocate surface
	SDL_FreeSurface( gHelloWorld );
	gHelloWorld = NULL;

	//Destroy window
	SDL_DestroyWindow( gWindow );
	gWindow = NULL;

	//Quit SDL subsystems
	SDL_Quit();
}

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;
}

Ask a question, Phil.

-- Tom Sloper -- sloperama.com

Advertisement

pbivens67 said:
gHelloWorld = SDL_LoadBMP("hello_world.bmp");

Maybe your file is not in the directory which the application is looking it for.

Try to add the full path, to see if it would fix it, e.g. “C:\\dev\\game\\hello_world.bmp”.

Hard coding paths is bad ofc., since you do not know where people install and run your game.
There are related project settings in VS to setup where the exe is generated, and where it is executed. The image should be in the latter path i guess, and then ideally it should not matter if you run debug or release builds, although they use different directories for the exe.

Congrats on getting rid of GLUT! \:D/

can you help me with the project settings in VS

Menu: Project > Properties:

The Output dir is where the exe goes. Personally i use the same dir for both debug and release builds.

Not sure if this might cause some problems. Sometimes VS refuses to update the exe even after changing code and compiling successfully, which is super annoying. By default VS uses different dirs for debug and release.

However, personally i do store files in this directory, which my app then loads without giving a path. So your image files should be in the directory given from those settings, than your code should work as is. Eventually put the image in both the debug and release directories.

(I did not find any other path settings for a directory where the exe is executed, as said before - maybe i remember this wrong. I'm also still a total amateur with those things in general. ; )

In fact using SDL should not change raleted behavior. What you did before with GLUT should still work.
Maybe it does find the file, but fails to load it due to some unknown image format.
Sadly the error message does not tell us much.

Actually your bmp should be in this directory:

Then it should load it without changing code or project settings.

Advertisement

I tried this code but I still get a error message which is:

"incorrectly formed universal character name"

I think you get this error if you write something like:

load (“C:\dir\file.bmp”);

instead

load (“C:\\dir\\file.bmp”);

Can this be?

well I finally solved my problem./ Here is the code.

bool loadMedia()
{
	//Loading success flag
	bool success = true;

	//Load splash image
	gHelloWorld = SDL_LoadBMP( "C:\\Users\\Owner\\source\\repos\\Project94\\Debug\\paddle.bmp" );
	if( gHelloWorld == NULL )
	{
		printf( "Unable to load image %s! SDL Error: %s\n", "C:\\Users\\Owner\source\\repos\\Project94\\Debug\\paddle.bmp", SDL_GetError() );
		success = false;
	}

	return success;
}

I am making a simple pong game to get good at sdl2. I am able to draw a paddle in the upper left corner of the screen but I want to draw it at the middle and bottom of the screen. I am just starting to learn sdl2. In my code how do I draw a paddle in the right place?

This topic is closed to new replies.

Advertisement