Created
December 11, 2022 20:36
-
-
Save dreamlayers/a45c91fe2ace7d71147d732d58227042 to your computer and use it in GitHub Desktop.
Emscripten SDL 2 full screen test.
This file contains hidden or 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 <emscripten/emscripten.h> | |
#include "SDL.h" | |
SDL_Window *window = NULL; | |
SDL_Renderer *renderer = NULL; | |
SDL_Surface *surface; | |
//#define USE_SURFACE | |
/* If window lacks SDL_WINDOW_RESIZABLE, then after exiting from full screen | |
* canvas becomes 0x0 in size, and invisible. With that flag and USE_SURFACE, | |
* things usually work, but occasionally after switching to full screen | |
* nothing is displayed. Re-creating the window fixes both issues. | |
* Using only SDL_GetWindowSurface() and SDL_CreateSoftwareRenderer() is | |
* insufficient. Re-creating the window also fixes the problem when not | |
* using a surface. | |
*/ | |
void create_window() { | |
if (renderer != NULL) { | |
SDL_DestroyRenderer(renderer); | |
renderer = NULL; | |
} | |
if (window != NULL) SDL_DestroyWindow(window); | |
window = SDL_CreateWindow("Full screen test", | |
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, | |
640, 480, 0); | |
} | |
void create_renderer() { | |
#ifdef USE_SURFACE | |
surface = SDL_GetWindowSurface(window); | |
renderer = SDL_CreateSoftwareRenderer(surface); | |
#else | |
renderer = SDL_CreateRenderer(window, -1, 0); | |
#endif | |
} | |
void draw() { | |
static Uint8 red = 0; | |
red++; | |
SDL_SetRenderDrawColor(renderer, red, 0xFF, 0xFF, 0xFF); | |
SDL_RenderClear(renderer); | |
} | |
void update() { | |
#ifdef USE_SURFACE | |
SDL_UpdateWindowSurface(window); | |
#else | |
SDL_RenderPresent(renderer); | |
#endif | |
} | |
void loop() { | |
SDL_Event e; | |
while (SDL_PollEvent(&e)) { | |
printf("Event: %i\n", e.type); | |
if (e.type == SDL_KEYDOWN) { | |
create_window(); | |
create_renderer(); | |
printf("Recreated window and renderer: %i\n", (int)renderer); | |
} | |
} | |
draw(); | |
update(); | |
} | |
int main(int argc, char *argv[]) { | |
SDL_Init(SDL_INIT_VIDEO); | |
create_window(); | |
create_renderer(); | |
emscripten_set_main_loop(loop, 0, 1); | |
SDL_Quit(); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment