From 39332ce97fdba254df1eb78f7be4cbdb1ad8cc84 Mon Sep 17 00:00:00 2001 From: Ingo Ruhnke Date: Tue, 13 Apr 2004 13:02:06 +0000 Subject: [PATCH] - implemeted video-mode switching (this currently breaks shadow fonts, but shouldn't be difficult to fix) SVN-Revision: 517 --- src/setup.cpp | 4 +- src/texture.cpp | 130 ++++++++++++++++++++++++++++++++++++++++++++++---------- src/texture.h | 35 +++++++++++++++ 3 files changed, 144 insertions(+), 25 deletions(-) diff --git a/src/setup.cpp b/src/setup.cpp index bdfb2a4c3..3cf9980f1 100644 --- a/src/setup.cpp +++ b/src/setup.cpp @@ -628,7 +628,6 @@ void st_general_free(void) void st_video_setup(void) { - if(screen != NULL) SDL_FreeSurface(screen); @@ -649,9 +648,10 @@ void st_video_setup(void) else st_video_setup_sdl(); + Surface::reload_all(); + /* Set window manager stuff: */ SDL_WM_SetCaption("SuperTux " VERSION, "SuperTux"); - } void st_video_setup_sdl(void) diff --git a/src/texture.cpp b/src/texture.cpp index ae7cec4b6..b81d8eadd 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -10,12 +10,71 @@ // // +#include #include "SDL.h" #include "SDL_image.h" #include "texture.h" #include "globals.h" #include "setup.h" +Surface::Surfaces Surface::surfaces; + +SurfaceData::SurfaceData(SDL_Surface* surf, int use_alpha_) + : type(SURFACE), surface(surf), use_alpha(use_alpha_) +{ +} + +SurfaceData::SurfaceData(const std::string& file_, int use_alpha_) + : type(LOAD), file(file_), use_alpha(use_alpha_) +{ +} + +SurfaceData::SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_) + : type(LOAD_PART), file(file_), use_alpha(use_alpha_), + x(x_), y(y_), w(w_), h(h_) +{ +} + +SurfaceImpl* +SurfaceData::create() +{ + if (use_gl) + return create_SurfaceOpenGL(); + else + return create_SurfaceSDL(); +} + +SurfaceSDL* +SurfaceData::create_SurfaceSDL() +{ + switch(type) + { + case LOAD: + return new SurfaceSDL(file, use_alpha); + case LOAD_PART: + return new SurfaceSDL(file, x, y, w, h, use_alpha); + case SURFACE: + return 0; //new SurfaceSDL(surface, use_alpha); + } + assert(0); +} + +SurfaceOpenGL* +SurfaceData::create_SurfaceOpenGL() +{ + switch(type) + { + case LOAD: + return new SurfaceOpenGL(file, use_alpha); + case LOAD_PART: + return new SurfaceOpenGL(file, x, y, w, h, use_alpha); + case SURFACE: + return 0; //new SurfaceOpenGL(surface, use_alpha); + } + assert(0); +} + + /* Quick utility function for texture creation */ static int power_of_two(int input) { @@ -28,44 +87,69 @@ static int power_of_two(int input) } Surface::Surface(SDL_Surface* surf, int use_alpha) + : data(surf, use_alpha), w(0), h(0) { - if (use_gl) - impl = new SurfaceOpenGL(surf, use_alpha); - else - impl = new SurfaceSDL(surf, use_alpha); - - w = impl->w; - h = impl->h; + impl = data.create(); + if (impl) + { + w = impl->w; + h = impl->h; + } + surfaces.push_back(this); } Surface::Surface(const std::string& file, int use_alpha) + : data(file, use_alpha), w(0), h(0) { - if (use_gl) - impl = new SurfaceOpenGL(file, use_alpha); - else - impl = new SurfaceSDL(file, use_alpha); - - w = impl->w; - h = impl->h; + impl = data.create(); + if (impl) + { + w = impl->w; + h = impl->h; + } + surfaces.push_back(this); } Surface::Surface(const std::string& file, int x, int y, int w, int h, int use_alpha) + : data(file, x, y, w, h, use_alpha), w(0), h(0) { - if (use_gl) - impl = new SurfaceOpenGL(file, x, y, w, h, use_alpha); - else - impl = new SurfaceSDL(file, x, y, w, h, use_alpha); + impl = data.create(); + if (impl) + { + w = impl->w; + h = impl->h; + } + surfaces.push_back(this); +} - w = impl->w; - h = impl->h; +void +Surface::reload() +{ + delete impl; + impl = data.create(); + if (impl) + { + w = impl->w; + h = impl->h; + } } Surface::~Surface() { + surfaces.remove(this); delete impl; } void +Surface::reload_all() +{ + for(Surfaces::iterator i = surfaces.begin(); i != surfaces.end(); ++i) + { + (*i)->reload(); + } +} + +void Surface::draw(float x, float y, Uint8 alpha, bool update) { if (impl) impl->draw(x, y, alpha, update); @@ -413,7 +497,7 @@ SurfaceSDL::draw(float x, float y, Uint8 alpha, bool update) dest.h = h; if(alpha != 255) /* SDL isn't capable of this kind of alpha :( therefore we'll leave now. */ - return; + return; SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha); SDL_BlitSurface(sdl_surface, NULL, screen, &dest); @@ -433,7 +517,7 @@ SurfaceSDL::draw_bg(Uint8 alpha, bool update) dest.h = screen->h; if(alpha != 255) - SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha); + SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha); SDL_SoftStretch(sdl_surface, NULL, screen, &dest); if (update == UPDATE) @@ -456,7 +540,7 @@ SurfaceSDL::draw_part(float sx, float sy, float x, float y, float w, float h, Ui dest.h = (int)h; if(alpha != 255) - SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha); + SDL_SetAlpha(sdl_surface ,SDL_SRCALPHA,alpha); SDL_BlitSurface(sdl_surface, &src, screen, &dest); diff --git a/src/texture.h b/src/texture.h index cfac52e2e..3b5335701 100644 --- a/src/texture.h +++ b/src/texture.h @@ -19,23 +19,58 @@ #include #endif +#include #include "screen.h" class SurfaceImpl; +class SurfaceSDL; +class SurfaceOpenGL; + +/** This class holds all the data necessary to construct a surface */ +class SurfaceData +{ +public: + enum ConstructorType { LOAD, LOAD_PART, SURFACE }; + ConstructorType type; + SDL_Surface* surface; + std::string file; + int use_alpha; + int x; + int y; + int w; + int h; + + SurfaceData(SDL_Surface* surf, int use_alpha_); + SurfaceData(const std::string& file_, int use_alpha_); + SurfaceData(const std::string& file_, int x_, int y_, int w_, int h_, int use_alpha_); + + SurfaceSDL* create_SurfaceSDL(); + SurfaceOpenGL* create_SurfaceOpenGL(); + SurfaceImpl* create(); +}; /** Container class that holds a surface, necessary so that we can switch Surface implementations (OpenGL, SDL) on the fly */ class Surface { public: + SurfaceData data; SurfaceImpl* impl; int w; int h; + + typedef std::list Surfaces; + static Surfaces surfaces; public: + static void reload_all(); + Surface(SDL_Surface* surf, int use_alpha); Surface(const std::string& file, int use_alpha); Surface(const std::string& file, int x, int y, int w, int h, int use_alpha); ~Surface(); + + /** Reload the surface, which is necesarry in case of a mode swich */ + void reload(); void draw(float x, float y, Uint8 alpha = 255, bool update = false); void draw_bg(Uint8 alpha = 255, bool update = false); -- 2.11.0