[HELP] Black Screen SDL

Hello guys,
I’m learning SDL2 with VisualStudio, i’ve been able to set up SDL without mistake I think (It compiles ! \o/) but i’ve an issue with a basic BMP display.
I followed this tutorial http://twinklebeardev.blogspot.fr/2012/07/lesson-1-hello-world.html, all seems to be okay but instead of displays the BMP it shows a black screen. I’ve try with different type of BMP but nothing works.

Here’s the code :

Code:

#include
#include <windows.h>
#include “SDL.h”

int main(int argc, char** argv){

if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
	std::cout << SDL_GetError() << std::endl;}


SDL_Window *win = NULL;
win = SDL_CreateWindow("Hello World!",0, 0, 1280, 960, SDL_WINDOW_SHOWN);
if (win == NULL){
	std::cout << SDL_GetError() << std::endl;
	return 1;
}

SDL_Renderer *ren = NULL;
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE	);
if (ren == NULL){
	std::cout << SDL_GetError() << std::endl;
	return 1;
}

SDL_Surface *bmp = NULL;
bmp = SDL_LoadBMP("../res/hello2.bmp");
if (bmp == NULL){
	std::cout << SDL_GetError() << std::endl;
	return 2;
}

SDL_Texture *tex = NULL;
tex = SDL_CreateTextureFromSurface(ren,bmp);
if (tex == NULL){
	std::cout << SDL_GetError() << std::endl;
	return 1;
}

SDL_FreeSurface(bmp);


    SDL_RenderClear(ren);

SDL_RenderCopy(ren, tex, NULL, NULL);

SDL_RenderPresent(ren);

SDL_Delay(5000);


SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;

}

YacineD wrote:

Hello guys,
I’m learning SDL2 with VisualStudio, i’ve been able to set up SDL without mistake I think (It compiles ! \o/) but i’ve an issue with a basic BMP display.
I followed this tutorial http://twinklebeardev.blogspot.fr/2012/07/lesson-1-hello-world.html, all seems to be okay but instead of displays the BMP it shows a black screen. I’ve try with different type of BMP but nothing works.

Here’s the code :

Code:

#include
#include <windows.h>
#include “SDL.h”

int main(int argc, char** argv){

if (SDL_Init(SDL_INIT_EVERYTHING) == -1){
std::cout << SDL_GetError() << std::endl;}

SDL_Window *win = NULL;
win = SDL_CreateWindow(“Hello World!”,0, 0, 1280, 960, SDL_WINDOW_SHOWN);
if (win == NULL){
std::cout << SDL_GetError() << std::endl;
return 1;
}

SDL_Renderer *ren = NULL;
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE );
if (ren == NULL){
std::cout << SDL_GetError() << std::endl;
return 1;
}

SDL_Surface *bmp = NULL;
bmp = SDL_LoadBMP("…/res/hello2.bmp");
if (bmp == NULL){
std::cout << SDL_GetError() << std::endl;
return 2;
}

SDL_Texture *tex = NULL;
tex = SDL_CreateTextureFromSurface(ren,bmp);
if (tex == NULL){
std::cout << SDL_GetError() << std::endl;
return 1;
}

SDL_FreeSurface(bmp);

    SDL_RenderClear(ren);

SDL_RenderCopy(ren, tex, NULL, NULL);

SDL_RenderPresent(ren);

SDL_Delay(5000);

SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}

Well, there’s nothing wrong with your code (runs fine on my Ubuntu system, with a little tweaking of the includes for the different OS).

A couple of ideas to narrow down the problem:

  1. Remove the “| SDL_RENDERER_TARGETTEXTURE” from the renderer creation - you aren’t using this capability, so remove it to eliminate it as a problem. I’m wondering if you’ve found a bug where you need to explicitly call “SDL_SetRenderTarget(ren, NULL)” to get it pointed at the screen…
  2. Add an SDL_SetRenderDrawColor() before the SDL_RenderClear(), using a color other than black. This will tell you if the problem is just with bmp/texture, or with SDL in general.

2014/1/7 YacineD

Hello guys,
I’m learning SDL2 with VisualStudio, i’ve been able to set up SDL without
mistake I think (It compiles ! \o/) but i’ve an issue with a basic BMP
display.
I followed this tutorial
http://twinklebeardev.blogspot.fr/2012/07/lesson-1-hello-world.html, all
seems to be okay but instead of displays the BMP it shows a black screen.
I’ve try with different type of BMP but nothing works.

Here’s the code : […]

Some folks have reported having to render & swap buffers twice before
anything
was visible on their screen, likely due to double buffering (?). Try the
clear, copy,
present cycle more than once. Ideally, you should redraw your window
constantly
in a loop and quit on user input (eg ESC).

You and Lloyd are saying exactly what I was thinking pretty much. I have a
loader that I built kind off to connect with the server and check files and
load things and stuff before the game starts. Anyway if I set everything up
and tell it to just render it won’t do it . I need to let it iterate
through the loop before it will start working and it makes my screen flash
twice before it starts rendering. This is with Windows and Visual Studio .
All of the examples I could find on the internet did the same thing but its
not a big deal and the flashes happen fast . Anyway I just changed it so it
would go into the loop and cycle for half of a second before I start doing
my file stuff and whatnot. If / when you make it work perfect please post
how you did it . What version of the library are you running? Mine is the
old one SDL2On Jan 7, 2014 9:06 AM, “Jonas Kulla” wrote:

2014/1/7 YacineD

Hello guys,
I’m learning SDL2 with VisualStudio, i’ve been able to set up SDL without
mistake I think (It compiles ! \o/) but i’ve an issue with a basic BMP
display.
I followed this tutorial
http://twinklebeardev.blogspot.fr/2012/07/lesson-1-hello-world.html, all
seems to be okay but instead of displays the BMP it shows a black screen.
I’ve try with different type of BMP but nothing works.

Here’s the code : […]

Some folks have reported having to render & swap buffers twice before
anything
was visible on their screen, likely due to double buffering (?). Try the
clear, copy,
present cycle more than once. Ideally, you should redraw your window
constantly
in a loop and quit on user input (eg ESC).


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org