From a5050597190a33c8947d94fcb183899f9ff824b6 Mon Sep 17 00:00:00 2001 From: Misa Date: Mon, 15 Feb 2021 16:18:45 -0800 Subject: [PATCH] Move Graphics buffer creation to new func create_buffers() It doesn't feel quite write leaving all the buffer creation code in main(), even though it's perfectly okay to do so and it doesn't result in any memory mismanagement that Valgrind can report; so I'm factoring all of it out to a separate function, Graphics::create_buffers(). As a bonus, we no longer have to keep qualifying with `graphics.` in the buffer creation code, which is nice. --- desktop_version/src/Graphics.cpp | 51 ++++++++++++++++++++++++++++++++ desktop_version/src/Graphics.h | 1 + desktop_version/src/main.cpp | 49 +----------------------------- 3 files changed, 53 insertions(+), 48 deletions(-) diff --git a/desktop_version/src/Graphics.cpp b/desktop_version/src/Graphics.cpp index a41e5421..dc86a664 100644 --- a/desktop_version/src/Graphics.cpp +++ b/desktop_version/src/Graphics.cpp @@ -170,6 +170,57 @@ void Graphics::destroy() #undef CLEAR_ARRAY } +void Graphics::create_buffers(const SDL_PixelFormat* fmt) +{ + #define CREATE_SURFACE(w, h) \ + SDL_CreateRGBSurface( \ + SDL_SWSURFACE, \ + w, h, \ + fmt->BitsPerPixel, \ + fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask \ + ) + backBuffer = CREATE_SURFACE(320, 240); + SDL_SetSurfaceBlendMode(backBuffer, SDL_BLENDMODE_NONE); + + footerbuffer = CREATE_SURFACE(320, 10); + SDL_SetSurfaceBlendMode(footerbuffer, SDL_BLENDMODE_BLEND); + SDL_SetSurfaceAlphaMod(footerbuffer, 127); + FillRect(footerbuffer, SDL_MapRGB(fmt, 0, 0, 0)); + + ghostbuffer = CREATE_SURFACE(320, 240); + SDL_SetSurfaceBlendMode(ghostbuffer, SDL_BLENDMODE_BLEND); + SDL_SetSurfaceAlphaMod(ghostbuffer, 127); + + foregroundBuffer = CREATE_SURFACE(320, 240); + SDL_SetSurfaceBlendMode(foregroundBuffer, SDL_BLENDMODE_BLEND); + + menubuffer = CREATE_SURFACE(320, 240); + SDL_SetSurfaceBlendMode(menubuffer, SDL_BLENDMODE_NONE); + + warpbuffer = CREATE_SURFACE(320 + 16, 240 + 16); + SDL_SetSurfaceBlendMode(warpbuffer, SDL_BLENDMODE_NONE); + + warpbuffer_lerp = CREATE_SURFACE(320 + 16, 240 + 16); + SDL_SetSurfaceBlendMode(warpbuffer_lerp, SDL_BLENDMODE_NONE); + + towerbg.buffer = CREATE_SURFACE(320 + 16, 240 + 16); + SDL_SetSurfaceBlendMode(towerbg.buffer, SDL_BLENDMODE_NONE); + + towerbg.buffer_lerp = CREATE_SURFACE(320 + 16, 240 + 16); + SDL_SetSurfaceBlendMode(towerbg.buffer_lerp, SDL_BLENDMODE_NONE); + + titlebg.buffer = CREATE_SURFACE(320 + 16, 240 + 16); + SDL_SetSurfaceBlendMode(titlebg.buffer, SDL_BLENDMODE_NONE); + + titlebg.buffer_lerp = CREATE_SURFACE(320 + 16, 240 + 16); + SDL_SetSurfaceBlendMode(titlebg.buffer_lerp, SDL_BLENDMODE_NONE); + + tempBuffer = CREATE_SURFACE(320, 240); + SDL_SetSurfaceBlendMode(tempBuffer, SDL_BLENDMODE_NONE); + + #undef CREATE_SURFACE +} + void Graphics::destroy_buffers() { #define FREE_SURFACE(SURFACE) \ diff --git a/desktop_version/src/Graphics.h b/desktop_version/src/Graphics.h index 1d574a69..002c3eb2 100644 --- a/desktop_version/src/Graphics.h +++ b/desktop_version/src/Graphics.h @@ -19,6 +19,7 @@ public: void init(); void destroy(); + void create_buffers(const SDL_PixelFormat* fmt); void destroy_buffers(); GraphicsResources grphx; diff --git a/desktop_version/src/main.cpp b/desktop_version/src/main.cpp index 2695b627..a2e046be 100644 --- a/desktop_version/src/main.cpp +++ b/desktop_version/src/main.cpp @@ -245,54 +245,7 @@ int main(int argc, char *argv[]) } graphics.screenbuffer = &gameScreen; - const SDL_PixelFormat* fmt = gameScreen.GetFormat(); - #define CREATE_SURFACE(w, h) \ - SDL_CreateRGBSurface( \ - SDL_SWSURFACE, \ - w, h, \ - fmt->BitsPerPixel, \ - fmt->Rmask, fmt->Gmask, fmt->Bmask, fmt->Amask \ - ) - graphics.backBuffer = CREATE_SURFACE(320, 240); - SDL_SetSurfaceBlendMode(graphics.backBuffer, SDL_BLENDMODE_NONE); - - graphics.footerbuffer = CREATE_SURFACE(320, 10); - SDL_SetSurfaceBlendMode(graphics.footerbuffer, SDL_BLENDMODE_BLEND); - SDL_SetSurfaceAlphaMod(graphics.footerbuffer, 127); - FillRect(graphics.footerbuffer, SDL_MapRGB(fmt, 0, 0, 0)); - - graphics.ghostbuffer = CREATE_SURFACE(320, 240); - SDL_SetSurfaceBlendMode(graphics.ghostbuffer, SDL_BLENDMODE_BLEND); - SDL_SetSurfaceAlphaMod(graphics.ghostbuffer, 127); - - graphics.foregroundBuffer = CREATE_SURFACE(320, 240); - SDL_SetSurfaceBlendMode(graphics.foregroundBuffer, SDL_BLENDMODE_BLEND); - - graphics.menubuffer = CREATE_SURFACE(320, 240); - SDL_SetSurfaceBlendMode(graphics.menubuffer, SDL_BLENDMODE_NONE); - - graphics.warpbuffer = CREATE_SURFACE(320 + 16, 240 + 16); - SDL_SetSurfaceBlendMode(graphics.warpbuffer, SDL_BLENDMODE_NONE); - - graphics.warpbuffer_lerp = CREATE_SURFACE(320 + 16, 240 + 16); - SDL_SetSurfaceBlendMode(graphics.warpbuffer_lerp, SDL_BLENDMODE_NONE); - - graphics.towerbg.buffer = CREATE_SURFACE(320 + 16, 240 + 16); - SDL_SetSurfaceBlendMode(graphics.towerbg.buffer, SDL_BLENDMODE_NONE); - - graphics.towerbg.buffer_lerp = CREATE_SURFACE(320 + 16, 240 + 16); - SDL_SetSurfaceBlendMode(graphics.towerbg.buffer_lerp, SDL_BLENDMODE_NONE); - - graphics.titlebg.buffer = CREATE_SURFACE(320 + 16, 240 + 16); - SDL_SetSurfaceBlendMode(graphics.titlebg.buffer, SDL_BLENDMODE_NONE); - - graphics.titlebg.buffer_lerp = CREATE_SURFACE(320 + 16, 240 + 16); - SDL_SetSurfaceBlendMode(graphics.titlebg.buffer_lerp, SDL_BLENDMODE_NONE); - - graphics.tempBuffer = CREATE_SURFACE(320, 240); - SDL_SetSurfaceBlendMode(graphics.tempBuffer, SDL_BLENDMODE_NONE); - - #undef CREATE_SURFACE + graphics.create_buffers(gameScreen.GetFormat()); if (game.skipfakeload) game.gamestate = TITLEMODE;