Hello. As the title says, I’m trying to render to a target texture, and then render that texture to the window. Everything works fine, except if I resize the window.
The second after I resize the window, all of the textures disappear (including the renderer?), and the screen is just black.
Additionally, SDL sets this error message: CreateTexture(D3DPOOL_DEFAULT): INVALIDCALL
The weird thing is, it works perfectly if the SDL renderer is initiated with SDL_RENDERER_SOFTWARE.
I don’t know much about openGL, so I’m guessing it’s my fault and I’m doing something wrong. Any help or explanations would be appreciated.
I’ve tested this on 2 computers, both Windows 10.
Here’s a video of it working as expected (when set to SDL_RENDERER_SOFTWARE): https://giant.gfycat.com/NeglectedIckyGharial.webm
And here’s a video of it breaking (when set to SDL_RENDERER_ACCELERATED): https://giant.gfycat.com/PoliteUnfinishedIndianringneckparakeet.webm
Here’s the image I’m using:
And here’s the source code:
Source Code
// Mostly from https://gist.github.com/Twinklebear/8265888
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <SDL.h>
#define WIN_WIDTH 640
#define WIN_HEIGHT 480
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* win = SDL_CreateWindow("Rendering to a texture!", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIN_WIDTH, WIN_HEIGHT, SDL_WINDOW_RESIZABLE);
// Error only happens when flag is SDL_RENDERER_ACCELERATED, works fine with SDL_RENDERER_SOFTWARE
SDL_Renderer* renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
//Put your own bmp image here
SDL_Surface* bmpSurf = SDL_LoadBMP("hamster.bmp");
SDL_Texture* bmpTex = SDL_CreateTextureFromSurface(renderer, bmpSurf);
SDL_FreeSurface(bmpSurf);
//Make a target texture to render too
SDL_Texture* texTarget = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, WIN_WIDTH, WIN_HEIGHT);
//Now render to the texture
SDL_SetRenderTarget(renderer, texTarget);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bmpTex, NULL, NULL);
//Detach the texture
SDL_SetRenderTarget(renderer, NULL);
SDL_DestroyTexture(bmpTex);
if (SDL_RenderTargetSupported(renderer) == SDL_FALSE) {
return EXIT_FAILURE;
}
bool quit = false;
SDL_Event e;
while (!quit) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
quit = true;
}
}
SDL_RenderClear(renderer);
//Now render the texture target to our screen, but upside down
SDL_RenderCopyEx(renderer, texTarget, NULL, NULL, 0, NULL, SDL_FLIP_VERTICAL);
/*
*
*
* THIS IS WHERE SOMETHING GOES WRONG
*
*
*/
SDL_RenderPresent(renderer);
if (strlen(SDL_GetError()) != 0) {
printf("%s\n", SDL_GetError());
SDL_SetError('\0');
}
/*
*
*
*
*
*/
SDL_Delay(20);
}
SDL_DestroyTexture(texTarget);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
SDL_Quit();
return EXIT_SUCCESS;
}
Thanks!