Last active
May 2, 2017 19:59
-
-
Save zhelezkov/d568161b65b971dd35c8b87751ca339a to your computer and use it in GitHub Desktop.
SDL2Bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SDL2/SDL.h> | |
int main(int argc, const char * argv[]) { | |
SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengl"); | |
SDL_Init(SDL_INIT_EVERYTHING); | |
SDL_Window* window1 = SDL_CreateWindow("Test Window 1", 0, 0, 32 * 5, 32 * 5, SDL_WINDOW_OPENGL); | |
SDL_Renderer* renderer1 = SDL_CreateRenderer(window1, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC); | |
SDL_Texture* texture1 = SDL_CreateTexture(renderer1, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, 32, 32); | |
SDL_Window* window2 = SDL_CreateWindow("Test Window 2", 32 * 5, 0, 32 * 5, 32 * 5, SDL_WINDOW_OPENGL); | |
SDL_Renderer* renderer2 = SDL_CreateRenderer(window2, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_TARGETTEXTURE | SDL_RENDERER_PRESENTVSYNC); | |
bool running = true; | |
SDL_Event ev; | |
while (running) { | |
while (SDL_PollEvent(&ev)) { | |
if (ev.type == SDL_QUIT) { | |
running = false; | |
} | |
if (ev.type == SDL_KEYUP) { | |
SDL_Keycode key = ev.key.keysym.sym; | |
//press pace to destroy renderer2 | |
if (key == SDLK_SPACE) { | |
SDL_DestroyRenderer(renderer2); | |
} | |
} | |
} | |
//draw to texture | |
SDL_SetRenderTarget(renderer1, texture1); | |
SDL_SetRenderDrawColor(renderer1, 255, 0, 0, 255); // red color | |
SDL_RenderClear(renderer1); | |
SDL_SetRenderDrawColor(renderer1, 0, 255, 0, 255); // green color | |
SDL_Rect rect = {0, 0, 32, 32}; | |
SDL_RenderFillRect(renderer1, &rect); | |
//draw texture to ours screen | |
SDL_SetRenderTarget(renderer1, NULL); | |
SDL_SetRenderDrawColor(renderer1, 0, 0, 255, 255); // blue color | |
SDL_RenderClear(renderer1); | |
SDL_RenderCopy(renderer1, texture1, NULL, NULL); | |
SDL_RenderPresent(renderer1); | |
//theoretically we should see green color, and we see it, but.. | |
//but untill we destroy renderer2 | |
//and interesting fact, if we do anything to renderer2, like clear it or present, then renderer1 will works as expected | |
} | |
SDL_DestroyTexture(texture1); | |
SDL_DestroyRenderer(renderer1); | |
SDL_DestroyWindow(window1); | |
SDL_DestroyRenderer(renderer2); | |
SDL_DestroyWindow(window2); | |
SDL_Quit(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment