From: DeaDDooMER Date: Wed, 1 Apr 2020 17:37:55 +0000 (+0300) Subject: system: optimize videomode and fullscreen changing X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=adac70590f44505dbc9e99a05e8c5bfde8649dd6;p=flatwaifu.git system: optimize videomode and fullscreen changing --- diff --git a/src/gl/render.c b/src/gl/render.c index 426f43c..8b16371 100644 --- a/src/gl/render.c +++ b/src/gl/render.c @@ -1678,15 +1678,11 @@ 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; - } if (root != NULL) { R_cache_free(root, 0); root = NULL; } - int res = Y_set_videomode(w, h, flags); + int res = Y_set_videomode_opengl(w, h, fullscreen); if (res == 0) { if (was == 0) { ERR_failinit("Unable to set video mode\n"); diff --git a/src/sdl/main.c b/src/sdl/main.c index 0b1aada..2cc83f6 100644 --- a/src/sdl/main.c +++ b/src/sdl/main.c @@ -42,8 +42,13 @@ #include "music.h" // S_initmusic S_updatemusic S_donemusic #include "render.h" // R_init R_draw R_done +#define MODE_NONE 0 +#define MODE_OPENGL 1 +#define MODE_SOFTWARE 2 + static int quit = 0; static SDL_Surface *surf = NULL; +static int mode = MODE_NONE; /* --- error.h --- */ @@ -91,32 +96,56 @@ void ERR_quit (void) { /* --- system.h --- */ -int Y_set_videomode (int w, int h, int flags) { - SDL_Surface *s; - int colors; - Uint32 f; +int Y_set_videomode_opengl (int w, int h, int fullscreen) { assert(w > 0); assert(h > 0); - f = SDL_DOUBLEBUF; - if (flags & SYSTEM_USE_FULLSCREEN) { - f = flags | SDL_FULLSCREEN; - } - if (flags & SYSTEM_USE_OPENGL) { - f = flags | SDL_OPENGL; - colors = 0; + Uint32 flags; + SDL_Surface *s; + if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) { + s = surf; } else { - f = flags | SDL_SWSURFACE | SDL_HWPALETTE; - colors = 8; + flags = SDL_DOUBLEBUF | SDL_OPENGL; + if (fullscreen) { + flags = flags | SDL_FULLSCREEN; + } +# ifdef WIN32 + flags = flags | SDL_RESIZABLE; +# endif + s = SDL_SetVideoMode(w, h, 0, flags); + if (s != NULL) { + mode = MODE_OPENGL; + surf = s; + } } - s = SDL_SetVideoMode(w, h, colors, f); - if (s != NULL) { - surf = s; + return s != NULL; +} + +int Y_set_videomode_software (int w, int h, int fullscreen) { + assert(w > 0); + assert(h > 0); + Uint32 flags; + SDL_Surface *s; + if (mode == MODE_OPENGL && surf->w == w && surf->h == h && Y_get_fullscreen() == fullscreen) { + s = surf; + } else { + flags = SDL_DOUBLEBUF | SDL_SWSURFACE | SDL_HWPALETTE; + if (fullscreen) { + flags = flags | SDL_FULLSCREEN; + } +# ifdef WIN32 + flags = flags | SDL_RESIZABLE; +# endif + s = SDL_SetVideoMode(w, h, 8, flags); + if (s != NULL) { + mode = MODE_SOFTWARE; + surf = s; + } } return s != NULL; } void Y_get_videomode (int *w, int *h) { - if (surf != NULL) { + if (mode != MODE_NONE) { *w = surf->w; *h = surf->h; } else { @@ -126,40 +155,43 @@ void Y_get_videomode (int *w, int *h) { } int Y_videomode_setted (void) { - return surf != NULL; + return mode != MODE_NONE; } void Y_unset_videomode (void) { surf = NULL; + mode = MODE_NONE; SDL_QuitSubSystem(SDL_INIT_VIDEO); SDL_InitSubSystem(SDL_INIT_VIDEO); } -void Y_set_fullscreen (int yes) { - assert(surf != NULL); - int flags = 0; - if ((surf->flags & SDL_FULLSCREEN) == 0) { - flags |= SYSTEM_USE_FULLSCREEN; - } - if (surf->flags & SDL_OPENGL) { - flags |= SDL_OPENGL; +void Y_set_fullscreen (int fullscreen) { + int fs = Y_get_fullscreen(); + if (mode != MODE_NONE && fs != fullscreen) { + if (SDL_WM_ToggleFullScreen(surf) == 0) { + switch (mode) { + case MODE_OPENGL: + Y_set_videomode_opengl(surf->w, surf->h, fullscreen); + break; + case MODE_SOFTWARE: + Y_set_videomode_software(surf->w, surf->h, fullscreen); + break; + } + } } - Y_set_videomode(surf->w, surf->h, flags); } int Y_get_fullscreen (void) { - return (surf != NULL) && ((surf->flags & SDL_FULLSCREEN) != 0); + return (mode != MODE_NONE) && ((surf->flags & SDL_FULLSCREEN) != 0); } void Y_swap_buffers (void) { - assert(surf != NULL); - assert(surf->flags & SDL_OPENGL); + assert(mode == MODE_OPENGL); SDL_GL_SwapBuffers(); } void Y_get_buffer (byte **buf, int *w, int *h, int *pitch) { - assert(surf != NULL); - assert((surf->flags & SDL_OPENGL) == 0); + assert(mode == MODE_SOFTWARE); *buf = surf->pixels; *w = surf->w; *h = surf->h; @@ -170,8 +202,7 @@ void Y_set_vga_palette (byte *vgapal) { int i; byte *p = vgapal; assert(vgapal != NULL); - assert(surf != NULL); - assert((surf->flags & SDL_OPENGL) == 0); + assert(mode == MODE_SOFTWARE); SDL_Color colors[256]; for (i = 0; i < 256; i++) { colors[i] = (SDL_Color) { @@ -185,14 +216,12 @@ void Y_set_vga_palette (byte *vgapal) { } void Y_repaint_rect (int x, int y, int w, int h) { - assert(surf != NULL); - assert((surf->flags & SDL_OPENGL) == 0); + assert(mode == MODE_SOFTWARE); SDL_UpdateRect(surf, x, y, w, h); } void Y_repaint (void) { - assert(surf != NULL); - assert((surf->flags & SDL_OPENGL) == 0); + assert(mode == MODE_SOFTWARE); SDL_Flip(surf); } @@ -316,6 +345,9 @@ static void poll_events (void (*h)(int key, int down)) { case SDL_QUIT: ERR_quit(); break; + case SDL_VIDEORESIZE: + R_set_videomode(ev.resize.w, ev.resize.h, Y_get_fullscreen()); + break; case SDL_KEYDOWN: case SDL_KEYUP: key = sdl_to_key(ev.key.keysym.sym); diff --git a/src/sdl2/main.c b/src/sdl2/main.c index fc50b90..3f49369 100644 --- a/src/sdl2/main.c +++ b/src/sdl2/main.c @@ -73,55 +73,6 @@ void ERR_quit (void) { /* --- system.h --- */ -static int Y_set_videomode_opengl (int w, int h, int fullscreen) { - assert(w > 0); - assert(h > 0); - 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); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - int x = SDL_WINDOWPOS_CENTERED; - int y = SDL_WINDOWPOS_CENTERED; - SDL_Window *win = SDL_CreateWindow(TITLE_STR, x, y, w, h, flags); - if (win != NULL) { - SDL_GLContext ctx = SDL_GL_CreateContext(win); - if (ctx != NULL) { - Y_unset_videomode(); - window = win; - context = ctx; - SDL_GL_MakeCurrent(window, context); - } else { - SDL_DestroyWindow(win); - win = NULL; - } - } - return win != NULL; -} - -static int Y_set_videomode_software (int w, int h, int fullscreen) { - assert(w > 0); - assert(h > 0); - int x = SDL_WINDOWPOS_CENTERED; - int y = SDL_WINDOWPOS_CENTERED; - 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) { - SDL_Surface *s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0); - if (s != NULL) { - Y_unset_videomode(); - window = win; - surf = s; - } else { - SDL_DestroyWindow(win); - win = NULL; - } - } - return win != NULL; -} - static int Y_resize_window (int w, int h, int fullscreen) { assert(w > 0); assert(h > 0); @@ -141,23 +92,69 @@ static int Y_resize_window (int w, int h, int fullscreen) { return 1; } -int Y_set_videomode (int w, int h, int flags) { +int Y_set_videomode_opengl (int w, int h, int fullscreen) { assert(w > 0); assert(h > 0); - int fullscreen = (flags & SYSTEM_USE_FULLSCREEN) != 0; - if (flags & SYSTEM_USE_OPENGL) { - if (window != NULL && context != NULL) { - return Y_resize_window(w, h, fullscreen); - } else { - return Y_set_videomode_opengl(w, h, fullscreen); + Uint32 flags; + SDL_Window *win; + SDL_GLContext ctx; + if (window != NULL && context != NULL) { + Y_resize_window(w, h, fullscreen); + win = window; + } else { + flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL; + if (fullscreen) { + flags = flags | SDL_WINDOW_FULLSCREEN; } + // TODO set context version and type + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags); + if (win != NULL) { + ctx = SDL_GL_CreateContext(win); + if (ctx != NULL) { + Y_unset_videomode(); + window = win; + context = ctx; + SDL_GL_MakeCurrent(window, context); + } else { + SDL_DestroyWindow(win); + win = NULL; + } + } + } + return win != NULL; +} + +int Y_set_videomode_software (int w, int h, int fullscreen) { + assert(w > 0); + assert(h > 0); + Uint32 flags; + SDL_Surface *s; + SDL_Window *win; + if (window != NULL && surf != NULL) { + Y_resize_window(w, h, fullscreen); + win = window; } else { - if (window != NULL && surf != NULL) { - return Y_resize_window(w, h, fullscreen); - } else { - return Y_set_videomode_software(w, h, fullscreen); + flags = SDL_WINDOW_RESIZABLE; + if (fullscreen) { + flags = flags | SDL_WINDOW_FULLSCREEN; + } + win = SDL_CreateWindow(TITLE_STR, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags); + if (win != NULL) { + s = SDL_CreateRGBSurface(0, w, h, 8, 0, 0, 0, 0); + if (s != NULL) { + Y_unset_videomode(); + window = win; + surf = s; + } else { + SDL_DestroyWindow(win); + win = NULL; + } } } + return win != NULL; } void Y_get_videomode (int *w, int *h) { @@ -364,9 +361,7 @@ 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()); - } + R_set_videomode(ev->data1, ev->data2, Y_get_fullscreen()); break; case SDL_WINDOWEVENT_CLOSE: ERR_quit(); @@ -383,7 +378,9 @@ static void poll_events (void (*h)(int key, int down)) { ERR_quit(); break; case SDL_WINDOWEVENT: - window_event_handler(&ev.window); + if (ev.window.windowID == SDL_GetWindowID(window)) { + window_event_handler(&ev.window); + } break; case SDL_KEYDOWN: case SDL_KEYUP: diff --git a/src/soft/render.c b/src/soft/render.c index fafe379..74a3c0f 100644 --- a/src/soft/render.c +++ b/src/soft/render.c @@ -1354,7 +1354,7 @@ 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); + int res = Y_set_videomode_software(w, h, fullscreen); if (res == 0) { if (was == 0) { ERR_failinit("Unable to set video mode"); diff --git a/src/system.h b/src/system.h index 445de10..f9de3ce 100644 --- a/src/system.h +++ b/src/system.h @@ -7,7 +7,6 @@ #define SYSTEM_USE_FULLSCREEN (1 << 1) /* common video subsystem routines */ -int Y_set_videomode (int w, int h, int flags); void Y_get_videomode (int *w, int *h); int Y_videomode_setted (void); void Y_unset_videomode (void); @@ -15,12 +14,14 @@ void Y_set_fullscreen (int yes); int Y_get_fullscreen (void); /* hardware specific rendering */ +int Y_set_videomode_opengl (int w, int h, int fullscreen); void Y_swap_buffers (void); /* software specific rendering */ +int Y_set_videomode_software (int w, int h, int fullscreen); void Y_get_buffer (byte **buf, int *w, int *h, int *pitch); void Y_set_vga_palette (byte *vgapal); void Y_repaint_rect (int x, int y, int w, int h); void Y_repaint (void); -#endif /* SYSTEM_H_INCLUDED */ \ No newline at end of file +#endif /* SYSTEM_H_INCLUDED */