New to SDL, I’m following this tutorial which has the end result of displaying a bmp image within an SDL window. Simple stuff. However, when my program is run the contents of the window are transparent.
My program is a modified version of the source within the tutorial and is written in C ( as opposed to
C++, which really isn’t much of a difference in this case ).
The following is my source:
#include <SDL2/SDL.h>
#include <stdio.h>
typedef enum { false, true } bool;
// Starts up SDL and creates window
bool init( SDL_Window*, SDL_Surface*, int, int );
// Loads media
bool loadMedia( SDL_Surface*, char* );
// Frees media and shuts down SDL
void end( SDL_Surface*, SDL_Window* );
const int SCREEN_WIDTH= 640;
const int SCREEN_HEIGHT= 480;
/*
*
*/
bool init( SDL_Window* sdlWindow, SDL_Surface* sdlScreenSurface,
int height, int width )
{
// 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
sdlWindow= SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN );
if( sdlWindow == NULL ) {
printf( "Window could not be created! SDL_Error: %s\n",
SDL_GetError() );
success= false;
} else {
// Get Window surface
sdlScreenSurface= SDL_GetWindowSurface( sdlWindow );
}
}
return success;
}
/**
*
*/
bool loadMedia( SDL_Surface* sdlImage, char* imagePath )
{
// Loading success flag
bool success= true;
// Load splash image
sdlImage= SDL_LoadBMP(imagePath);
if( sdlImage == NULL ) {
printf( "Unable to load image %s! SDL Error: %s\n",
"hello_sdl.bmp", SDL_GetError() );
success= false;
}
return success;
}
/**
*
*/
void end( SDL_Surface* sdlImage, SDL_Window* sdlWindow )
{
// Dallocate surface
SDL_FreeSurface( sdlImage );
sdlImage= NULL;
// Destroy window
SDL_DestroyWindow( sdlWindow );
// Quit SDL subsystems
SDL_Quit();
}
/**
*
*/
int main( int argc, char* args[] )
{
// The window we'll be rendering to
SDL_Window* sdlWindow= NULL;
// The surface contained by the window
SDL_Surface* sdlScreenSurface= NULL;
// The image we will load and show on the screen
SDL_Surface* sdlImage= NULL;
char* imagePath= "assets/helloSDL.bmp";
// Start up SDL and create window
if( !init( sdlWindow, sdlScreenSurface, SCREEN_HEIGHT, SCREEN_WIDTH ) ) {
printf( "Failed to initialize!\n" );
} else {
// Load media
if( !loadMedia( sdlImage, imagePath ) ) {
printf( "Failed to load media!\n" );
} else {
// Apply the image
SDL_BlitSurface( sdlImage, NULL, sdlScreenSurface, NULL );
// Update the surface
SDL_UpdateWindowSurface( sdlWindow );
// Wait two seconds
SDL_Delay( 2000 );
}
}
// Free resources and close SDL
end( sdlImage, sdlWindow );
return 0;
}