Loading Images, help(SDL 2.0)

Im tring to load an image and it doesnt work, it just has a black screen which is supposed to be there but nothing goes to it no matter if i switch up the order of SDL_RenderClear() and SDL_RenderCopy(), but the thing is the code seems like its correctt i dont even know now, im new at SDL 2.0 as you already know, but. i dont know how to fix this. im using codeblocks on windows 7. heres my code:

#include <SDL.h> #include <SDL_image.h> #include

int main(int argc, char* args[]) { bool quit = false;

SDL_Init(SDL_INIT_VIDEO);

SDL_Window *window = NULL; SDL_Renderer *render = NULL; SDL_Event *evt = new SDL_Event(); text = IMG_LoadTexture(render, “player.png”); window = SDL_CreateWindow(“SDL_PROJ”, 100,100,800,600,SDL_WINDOW_SHOWN); render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_Rect txt_rect; txt_rect.x = 10; txt_rect.y = 10; txt_rect.w = 20; txt_rect.h = 10; while (!quit && evt->type != SDL_QUIT) {

SDL_PollEvent(evt); SDL_RenderClear(render); SDL_RenderCopy(render,text,NULL, &txt_rect); SDL_RenderPresent(render); } SDL_DestroyWindow(window); SDL_DestroyRenderer(render); delete evt; return 0; }}

Hi,

Im tring to load an image and it doesnt work, it just has a black
screen which is supposed to be there but nothing goes to it no matter
if i switch up the order of SDL_RenderClear() and SDL_RenderCopy(),
but the thing is the code seems like its correctt i dont even know
now, im new at SDL 2.0 as you already know, but. i dont know how to
fix this. im using codeblocks on windows 7. heres my code:

#include <SDL.h> #include <SDL_image.h> #include

int main(int argc, char* args[]) { bool quit = false;

SDL_Init(SDL_INIT_VIDEO);

SDL_Window *window = NULL; SDL_Renderer *render = NULL; SDL_Event
*evt = new SDL_Event(); text = IMG_LoadTexture(render, “player.png”);
window = SDL_CreateWindow(“SDL_PROJ”,
100,100,800,600,SDL_WINDOW_SHOWN); render =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

you have to first create the renderer before you create a texture for
this renderer. At the point of your call to IMG_LoadTexture, render is
NULL. Therefore, creating the texture will (probably) fail, and if it
does not, the created texture will bei incompatible with the renderer
you create with SDL_CreateRenderer.

If you fix the order of the commands in the above block, it should
work. That is, reorder them as follows:

window = SDL_CreateWindow(“SDL_PROJ”, 100,100,800,600,SDL_WINDOW_SHOWN);
render = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
text = IMG_LoadTexture(render, “player.png”);

The order of the commands in the main loop is ok:

SDL_PollEvent(evt); SDL_RenderClear(render);
SDL_RenderCopy(render,text,NULL, &txt_rect);
SDL_RenderPresent(render); }

But note that when destroying the objects, you got another problem:

SDL_DestroyWindow(window);
SDL_DestroyRenderer(render); delete evt; return 0; }}

You should first destroy the texture (which you don’t do at all at the
moment), then destroy the renderer, and finally destroy the window.

Best regards,
Felix–
Felix Fontein – @Felix_Fonteinhttps://felix.fontein.de/

As a rule of thumb, destroy objects in the reverse order you create the objects. It ensure that the objects that might be referred from another object get destroy later than ones refer to. Preventing objects with invalid state.

Thanks!

2014-02-07 22:39 GMT+01:00 theatron :

Im tring to load an image and it doesnt work, it just has a black screen
which is supposed to be there but nothing goes to it no matter if i switch
up the order of SDL_RenderClear() and SDL_RenderCopy(), but the thing is
the code seems like its correctt i dont even know now, im new at SDL 2.0 as
you already know, but. i dont know how to fix this. im using codeblocks on
windows 7. heres my code:

#include #include #include

int main(int argc, char* args[]) { bool quit = false;

SDL_Init(SDL_INIT_VIDEO);

SDL_Window *window = NULL; SDL_Renderer *render = NULL; SDL_Event *evt =
new SDL_Event(); text = IMG_LoadTexture(render, “player.png”); window =
SDL_CreateWindow(“SDL_PROJ”, 100,100,800,600,SDL_WINDOW_SHOWN); render =
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_Rect
txt_rect; txt_rect.x = 10; txt_rect.y = 10; txt_rect.w = 20; txt_rect.h =
10; while (!quit && evt->type != SDL_QUIT) {

SDL_PollEvent(evt); SDL_RenderClear(render);
SDL_RenderCopy(render,text,NULL, &txt_rect); SDL_RenderPresent(render); }
SDL_DestroyWindow(window); SDL_DestroyRenderer(render); delete evt; return
0; }}

You’re trying to create a texture on a NULL renderer pointer, without even
a window setup. That’s not going to work. Put the IMG_ call after the
creation of your renderer.