Wednesday, July 15, 2015

rendering - SDL render to a texture


I am trying to create a button for my game there are lots of similar buttons so using an image for each one is a bit inefficient. So I want to load an image then copy some text over it and render that to the screen. But it just doesn't render anything, it just gives me a transparent surface.


SDL_Texture* button = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA888, SDL_TEXTUREACCES_TARGET, 200, 50);
SDL_SetRenderTarget(renderer, button);


SDL_Texture* border = IMG_LoadTexture(renderer, "border.png");
SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, TTF_RenderText_Blended(font, "new race", color));

SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, border, NULL, NULL);
SDL_RenderCopy(renderer, text, NULL, &text_centered);
SDL_SetRenderTarget(renderer, NULL);

Then I do some extra stuff and finally:


SDL_RenderClear(renderer);

SDL_RenderCopy(renderer, background, NULL, NULL);
SDL_RenderCopy(renderer, button, NULL, &position);
SDL_RenderPresent(renderer);

But it just rendered the background not the button, that indicates that the textures I tried to render to button where not rendered and it is still just a blank texture. I already set SDL_RENDERER_TARGETTEXTURE but it just doesn't work.


UPDATE:


I found out that the problem was that the format and the access where passed in the wrong way, I corrected it and now when I compile there is an access violation, this is because I pass SDL_TEXTUREACCES_TARGET as the access argument, if I pass SDL_TEXTUREACCES_STATIC there is no such violation, the problem is that I can't use SDL_SetRenderTarget if it is static, it needs to be target (which causes an access violation error).



Answer



I found out that the access violation was a bug in SDL that only happens in some drivers (intel graphics in my case) if I use SDL_RENDERER_SOFTWARE instead of SDL_RENDERER_ACCELERATED it works, so it is pretty much depending on your graphics card and its drivers.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...