From: DeaDDooMER Date: Wed, 1 Apr 2020 05:55:23 +0000 (+0400) Subject: sdl2: fix fullscreen X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=8191e06a991c4733abb2166b14934be4e3ea8ed7;p=flatwaifu.git sdl2: fix fullscreen --- diff --git a/src/gl/render.c b/src/gl/render.c index 4167001..73ce92a 100644 --- a/src/gl/render.c +++ b/src/gl/render.c @@ -1670,24 +1670,34 @@ void R_alloc (void) { } } -void R_init (void) { - int res = 0; +void R_set_videomode (int w, int h, int fullscreen) { + assert(w > 0); + assert(h > 0); + int was = Y_videomode_setted(); int flags = SYSTEM_USE_OPENGL; if (fullscreen) { flags |= SYSTEM_USE_FULLSCREEN; } - logo("R_init: intialize opengl render\n"); - int was = Y_videomode_setted(); - if (SCRW <= 0 || SCRH <= 0) { - ERR_failinit("Invalid screen size %ix%i\n", SCRW, SCRH); - } - if (was == 0) { - R_init_playpal(); // only onece - } - res = Y_set_videomode(SCRW, SCRH, flags); + int res = Y_set_videomode(w, h, flags); if (res == 0) { - ERR_failinit("Unable to set video mode\n"); + if (was == 0) { + ERR_failinit("Unable to set video mode\n"); + } + } else { + SCRW = w; + SCRH = h; } +} + +void R_toggle_fullscreen (void) { + Y_set_fullscreen(!Y_get_fullscreen()); + fullscreen = Y_get_fullscreen(); +} + +void R_init (void) { + logo("R_init: intialize opengl render\n"); + R_init_playpal(); + R_set_videomode(SCRW, SCRH, fullscreen); root = R_cache_new(); assert(root); R_alloc(); @@ -1696,6 +1706,7 @@ void R_init (void) { void R_done (void) { R_cache_free(root, 1); Y_unset_videomode(); + root = NULL; } void R_setgamma (int g) { @@ -1706,12 +1717,6 @@ int R_getgamma (void) { return gamma; } -void R_toggle_fullscreen (void) { - Y_set_fullscreen(!Y_get_fullscreen()); - fullscreen = Y_get_fullscreen(); - R_alloc(); -} - void R_get_name (int n, char s[8]) { assert(n >= 0 && n < 256); if (walp[n].res == -1) { diff --git a/src/render.h b/src/render.h index 71d4365..4904017 100644 --- a/src/render.h +++ b/src/render.h @@ -11,6 +11,7 @@ void R_done (void); void R_setgamma (int g); int R_getgamma (void); void R_toggle_fullscreen (void); +void R_set_videomode (int w, int h, int fullscreen); void R_get_name (int n, char s[8]); int R_get_special_id (int n); diff --git a/src/sdl2/main.c b/src/sdl2/main.c index 42fda3e..dd5b8d4 100644 --- a/src/sdl2/main.c +++ b/src/sdl2/main.c @@ -76,7 +76,7 @@ void ERR_quit (void) { static int Y_set_videomode_opengl (int w, int h, int fullscreen) { assert(w > 0); assert(h > 0); - Uint32 flags = SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL; + Uint32 flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL; flags |= fullscreen ? SDL_WINDOW_FULLSCREEN : 0; // TODO set context version and type SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); @@ -92,7 +92,6 @@ static int Y_set_videomode_opengl (int w, int h, int fullscreen) { window = win; context = ctx; SDL_GL_MakeCurrent(window, context); - SDL_ShowWindow(window); } else { SDL_DestroyWindow(win); win = NULL; @@ -106,7 +105,7 @@ static int Y_set_videomode_software (int w, int h, int fullscreen) { assert(h > 0); int x = SDL_WINDOWPOS_CENTERED; int y = SDL_WINDOWPOS_CENTERED; - Uint32 flags = SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL; + Uint32 flags = SDL_WINDOW_RESIZABLE; flags |= fullscreen ? SDL_WINDOW_FULLSCREEN : 0; SDL_Window *win = SDL_CreateWindow(TITLE_STR, x, y, w, h, flags); if (win != NULL) { @@ -115,7 +114,6 @@ static int Y_set_videomode_software (int w, int h, int fullscreen) { Y_unset_videomode(); window = win; surf = s; - SDL_ShowWindow(window); } else { SDL_DestroyWindow(win); win = NULL; @@ -124,14 +122,39 @@ static int Y_set_videomode_software (int w, int h, int fullscreen) { return win != NULL; } +static int Y_resize_window (int w, int h, int fullscreen) { + assert(w > 0); + assert(h > 0); + assert(window != NULL); + if (surf != NULL) { + SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0); + if (s != NULL) { + SDL_SetPaletteColors(s->format->palette, surf->format->palette->colors, 0, surf->format->palette->ncolors); + SDL_FreeSurface(surf); + surf = s; + } + } + SDL_SetWindowSize(window, w, h); + Y_set_fullscreen(fullscreen); + return 1; +} + int Y_set_videomode (int w, int h, int flags) { assert(w > 0); assert(h > 0); int fullscreen = (flags & SYSTEM_USE_FULLSCREEN) != 0; if (flags & SYSTEM_USE_OPENGL) { - return Y_set_videomode_opengl(w, h, fullscreen); + if (window != NULL && context != NULL) { + return Y_resize_window(w, h, fullscreen); + } else { + return Y_set_videomode_opengl(w, h, fullscreen); + } } else { - return Y_set_videomode_software(w, h, fullscreen); + if (window != NULL && surf != NULL) { + return Y_resize_window(w, h, fullscreen); + } else { + return Y_set_videomode_software(w, h, fullscreen); + } } } @@ -328,6 +351,19 @@ static int sdl_to_key (int code) { } } +static void window_event_handler (SDL_WindowEvent *ev) { + switch (ev->event) { + case SDL_WINDOWEVENT_RESIZED: + if (window != NULL && ev->windowID == SDL_GetWindowID(window)) { + R_set_videomode(ev->data1, ev->data2, Y_get_fullscreen()); + } + break; + case SDL_WINDOWEVENT_CLOSE: + ERR_quit(); + break; + } +} + static void poll_events (void (*h)(int key, int down)) { int key; SDL_Event ev; @@ -336,6 +372,9 @@ static void poll_events (void (*h)(int key, int down)) { case SDL_QUIT: ERR_quit(); break; + case SDL_WINDOWEVENT: + window_event_handler(&ev.window); + break; case SDL_KEYDOWN: case SDL_KEYUP: key = sdl_to_key(ev.key.keysym.scancode); diff --git a/src/soft/render.c b/src/soft/render.c index 224d926..aaaea63 100644 --- a/src/soft/render.c +++ b/src/soft/render.c @@ -22,6 +22,7 @@ #include "game.h" #include "sound.h" #include "music.h" +#include "system.h" // game static vgaimg *scrnh[3]; // TITLEPIC INTERPIC ENDPIC @@ -1349,30 +1350,47 @@ int R_getgamma (void) { return gammaa; } +void R_set_videomode (int w, int h, int fullscreen) { + assert(w > 0); + assert(h > 0); + int was = Y_videomode_setted(); + int res = Y_set_videomode(w, h, fullscreen ? SYSTEM_USE_FULLSCREEN : 0); + if (res == 0) { + if (was == 0) { + ERR_failinit("Unable to set video mode"); + } + } else { + SCRW = w; + SCRH = h; + Y_get_buffer(&buffer, &buf_w, &buf_h, &pitch); + } +} + void R_toggle_fullscreen (void) { - fullscreen = !fullscreen; - V_toggle(); + Y_set_fullscreen(!Y_get_fullscreen()); + fullscreen = Y_get_fullscreen(); } void R_init () { int i; + logo("R_init: initialize software render\n"); F_loadres(F_getresid("PLAYPAL"), main_pal, 0, 768); for (i = 0; i < 256; ++i) { bright[i] = ((int) main_pal[i][0] + main_pal[i][1] + main_pal[i][2]) * 8 / (63 * 3); } F_loadres(F_getresid("MIXMAP"), mixmap, 0, 0x10000); F_loadres(F_getresid("COLORMAP"), clrmap, 0, 256*12); - logo("V_init: настройка видео\n"); - if (V_init() != 0) { - ERR_failinit("Не могу установить видеорежим VGA"); - } + R_set_videomode(SCRW, SCRH, fullscreen); R_setgamma(gammaa); V_setrect(0, SCRW, 0, SCRH); -// V_setscr(scrbuf); V_clr(0, SCRW, 0, SCRH, 0); R_alloc(); } void R_done (void) { - V_done(); + buffer = NULL; + buf_w = 0; + buf_h = 0; + pitch = 0; + Y_unset_videomode(); } diff --git a/src/soft/vga.c b/src/soft/vga.c index ef97ca7..d9c77e1 100644 --- a/src/soft/vga.c +++ b/src/soft/vga.c @@ -40,8 +40,8 @@ byte bright[256]; byte mixmap[256][256]; byte clrmap[256*12]; -static byte *buffer; -static int buf_w, buf_h, pitch; +byte *buffer; +int buf_w, buf_h, pitch; static int offx, offy; static int cx1, cx2, cy1, cy2; static byte flametab[16] = { @@ -64,24 +64,6 @@ vgaimg *V_loadvgaimg (char *name) { return V_getvgaimg(F_getresid(name)); } -short V_init (void) { - int flags = fullscreen ? SYSTEM_USE_FULLSCREEN : 0; - int res = Y_set_videomode(SCRW, SCRH, flags); - if (res == 0) { - ERR_failinit("Unable to set video mode"); - } - Y_get_buffer(&buffer, &buf_w, &buf_h, &pitch); - return 0; -} - -void V_done (void) { - buffer = NULL; - buf_w = 0; - buf_h = 0; - pitch = 0; - Y_unset_videomode(); -} - static void draw_rect (int x, int y, int w, int h, int c) { int i; int x0 = max(x, cx1); @@ -241,7 +223,3 @@ void V_remap_rect (int x, int y, int w, int h, byte *cmap) { for (cy=y; cy