first question and I really need help. So I was trying to learn
SDL2 api on C++ . I am using a raspberry pi 3 model B . I was first following a tutorial on Youtube (CodingMadeEasy to be more specific)
but now i am following lazyfoo's tutorials. I copied CME's code and tried displaying an image(.bmp) but the window showed nothing , also
SDL_GetError() didn't report anything wrong. I thought there must
be something wrong with the code I wrote or the image that I downloaded from the internet may not be .bmp so I went to lazyfoo's tutorials here http://lazyfoo.net/tutorials/SDL/02_getting_an_image_on_the_screen/index.php and downloaded the source code and .bmp file . But when I run it , I get the same output: a blank window and SDL_GetError() not reporting anything on the console.
Here's the code:
/*This source code copyrighted by Lazy Foo' Productions
(2004-2015)
and may not be redistributed without written permission.*/
//Using SDL and standard IO
#include <SDL2/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( "02_getting_an_image_on_the_screen/hello_world.bmp" );
if( gHelloWorld == NULL )
{
printf( "Unable to load image %s! SDL Error: %s\n", "02_getting_an_image_on_the_screen/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 );
//Wait two seconds
SDL_Delay( 2000 );
}
}
//Free resources and close SDL
close();
return 0;
}
If you want the .bmp file , please visit the link above and download the source code and files. Its not letting me upload it. Here's the output I get:Output . This transparant-ish , greyish window . I am using gnu c++ compiler and compiled it using
g++ 02_getting_an_image_on_the_screen.cpp -w -lSDL2 -o hl
and get that transparant-ish greyish window . I guess its transparant because my terminal is configured like that. To see what the problem was , i removed the .bmp to see if sdl reports an error and it did which meant it was able to read the .bmp when it was in the right location , just not able to display it. I searched google for hours found nothing . On stack overflow , the closest problem to mine was this SDL window does not show
I even tried polling events so as to give the system its chance to have it's event loop run ,as suggested there, but no matter how long the window stays open , the .bmp does not show up.
What's even more bizarre is that this isn't happening to me first time. I tried learning SDL2 an year ago but the same thing happened back then. I was using a cheap Windows 10 notebook with 2GBs of RAM and was using code::blocks (mingw compiler version) and the same thing happened except the window wasn't transparant , it was white , just as you get when you create a window with sdl
in Windows without displaying anything on it. For some reason , codeblocks didn't let me set the window to nullptr saying it didn't recognized what it was so i had to set it to NULL instead . Because of this, I thought the window not displaying .bmp was the compiler's fault too. So I downloaded Microsoft visual studio 2015
but the result was same : no image displayed .
At that point , I gave up , but now I came back because I have a new machine ( rpi model 3 b) and a new compiler (gnu c/c++) on it thinking SDL won't haunt me again but SDL doesn't seem to give up . I really need help . And yes, the .bmp is in right location , and i've included its right name in the code . Keep in mind that I've tried doing this on 2 machines , 3 compilers , 2 very different operating systems and 2 different cpu architectures( armv71 and x86) and at this point its getting frustrating . I've got some amazing ideas which can easily be implemented if SDL2 lets me . Sorry for the long post and any help is appreciated . Thank you.
-wcompiler switch. 2. your path says your image is in subdirectory, is it? 3. From your screenshot it doesn't seem like you're running your program from terminal, where do you expect to see error output then? 4. Judging from screemshot, your window don't get updated at all (all you see is "bachground" of file manager and desktop), bmp loading is unlikely to be the source of your problem; the fact that you don't redraw on specific events is. Make event loop and rendering loop. Drawing once and waiting is not a way to display. - keltarls -lain this directory, and full output of a program. Because the code in question technically isn't required to display anything. Also it might be a good time to start using debugger to inspect program flow and variables values. - keltar