From: Ingo Ruhnke Date: Sun, 17 Aug 2014 04:35:40 +0000 (+0200) Subject: Cleaned up VideoSystem initalisation X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=c946dc68fba6690788b4190e95690e5c576d3a5a;p=supertux.git Cleaned up VideoSystem initalisation This will make SuperTux crash at exit at the moment, as deinit gets out of order in Main::run() --- diff --git a/src/gui/menu.cpp b/src/gui/menu.cpp index f2347cb3f..be3b20820 100644 --- a/src/gui/menu.cpp +++ b/src/gui/menu.cpp @@ -31,6 +31,7 @@ #include "video/drawing_context.hpp" #include "video/font.hpp" #include "video/renderer.hpp" +#include "video/video_system.hpp" static const float MENU_REPEAT_INITIAL = 0.4f; static const float MENU_REPEAT_RATE = 0.1f; @@ -663,7 +664,7 @@ Menu::event(const SDL_Event& ev) case SDL_MOUSEBUTTONDOWN: if(ev.button.button == SDL_BUTTON_LEFT) { - Vector mouse_pos = Renderer::instance()->to_logical(ev.motion.x, ev.motion.y); + Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(ev.motion.x, ev.motion.y); int x = int(mouse_pos.x); int y = int(mouse_pos.y); @@ -679,7 +680,7 @@ Menu::event(const SDL_Event& ev) case SDL_MOUSEMOTION: { - Vector mouse_pos = Renderer::instance()->to_logical(ev.motion.x, ev.motion.y); + Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(ev.motion.x, ev.motion.y); float x = mouse_pos.x; float y = mouse_pos.y; diff --git a/src/gui/mousecursor.cpp b/src/gui/mousecursor.cpp index ae3f5a5ec..f56e7ef59 100644 --- a/src/gui/mousecursor.cpp +++ b/src/gui/mousecursor.cpp @@ -21,7 +21,7 @@ #include "supertux/globals.hpp" #include "video/drawing_context.hpp" #include "video/renderer.hpp" -#include "video/sdl/sdl_renderer.hpp" +#include "video/video_system.hpp" MouseCursor* MouseCursor::current_ = 0; @@ -61,7 +61,7 @@ void MouseCursor::draw(DrawingContext& context) int y; Uint8 ispressed = SDL_GetMouseState(&x, &y); - Vector mouse_pos = Renderer::instance()->to_logical(x, y); + Vector mouse_pos = VideoSystem::current()->get_renderer().to_logical(x, y); x = int(mouse_pos.x); y = int(mouse_pos.y); diff --git a/src/object/infoblock.cpp b/src/object/infoblock.cpp index 3996070cc..c8ef30660 100644 --- a/src/object/infoblock.cpp +++ b/src/object/infoblock.cpp @@ -98,7 +98,7 @@ HitResponse InfoBlock::collision(GameObject& other, const CollisionHit& hit_) { Player* player = dynamic_cast (&other); - if (player) + if (player) { if (player->does_buttjump) InfoBlock::hit(*player); diff --git a/src/scripting/functions.cpp b/src/scripting/functions.cpp index 6baa7e4a2..af907e37d 100644 --- a/src/scripting/functions.cpp +++ b/src/scripting/functions.cpp @@ -33,6 +33,7 @@ #include "supertux/world.hpp" #include "util/gettext.hpp" #include "video/renderer.hpp" +#include "video/video_system.hpp" #include "worldmap/tux.hpp" #include "worldmap/worldmap.hpp" @@ -276,7 +277,7 @@ void camera() void set_gamma(float gamma) { - Renderer::instance()->set_gamma(gamma); + VideoSystem::current()->get_renderer().set_gamma(gamma); } void quit() diff --git a/src/supertux/command_line_arguments.hpp b/src/supertux/command_line_arguments.hpp index 9fadc6301..d1c254b7b 100644 --- a/src/supertux/command_line_arguments.hpp +++ b/src/supertux/command_line_arguments.hpp @@ -21,7 +21,7 @@ #include "math/size.hpp" #include "util/log.hpp" -#include "video/video_systems.hpp" +#include "video/video_system.hpp" class Config; diff --git a/src/supertux/gameconfig.hpp b/src/supertux/gameconfig.hpp index 8558d6902..c26e5a5c6 100644 --- a/src/supertux/gameconfig.hpp +++ b/src/supertux/gameconfig.hpp @@ -17,7 +17,7 @@ #ifndef HEADER_SUPERTUX_SUPERTUX_GAMECONFIG_HPP #define HEADER_SUPERTUX_SUPERTUX_GAMECONFIG_HPP -#include "video/video_systems.hpp" +#include "video/video_system.hpp" #include "math/size.hpp" class Config diff --git a/src/supertux/main.cpp b/src/supertux/main.cpp index ab41bea88..376e5ee6e 100644 --- a/src/supertux/main.cpp +++ b/src/supertux/main.cpp @@ -212,7 +212,7 @@ Main::init_rand() void Main::init_video() { - SDL_SetWindowTitle(Renderer::instance()->get_window(), PACKAGE_NAME " " PACKAGE_VERSION); + SDL_SetWindowTitle(VideoSystem::current()->get_renderer().get_window(), PACKAGE_NAME " " PACKAGE_VERSION); const char* icon_fname = "images/engine/icons/supertux-256x256.png"; SDL_Surface* icon = IMG_Load_RW(get_physfs_SDLRWops(icon_fname), true); @@ -222,7 +222,7 @@ Main::init_video() } else { - SDL_SetWindowIcon(Renderer::instance()->get_window(), icon); + SDL_SetWindowIcon(VideoSystem::current()->get_renderer().get_window(), icon); SDL_FreeSurface(icon); } SDL_ShowCursor(0); @@ -336,9 +336,9 @@ Main::run(int argc, char** argv) timelog("commandline"); timelog("video"); - std::unique_ptr renderer(VideoSystem::new_renderer()); - std::unique_ptr lightmap(VideoSystem::new_lightmap()); - DrawingContext context(*renderer, *lightmap); + std::unique_ptr video_system = VideoSystem::create(g_config->video); + DrawingContext context(video_system->get_renderer(), + video_system->get_lightmap()); context_pointer = &context; init_video(); diff --git a/src/supertux/menu/options_menu.cpp b/src/supertux/menu/options_menu.cpp index 7877fa8a0..dfa6f61c9 100644 --- a/src/supertux/menu/options_menu.cpp +++ b/src/supertux/menu/options_menu.cpp @@ -217,13 +217,13 @@ OptionsMenu::menu_action(MenuItem* item) if (item->list[item->selected] == _("auto")) { g_config->aspect_size = Size(0, 0); // Magic values - Renderer::instance()->apply_config(); + VideoSystem::current()->get_renderer().apply_config(); MenuManager::instance().on_window_resize(); } else if (sscanf(item->list[item->selected].c_str(), "%d:%d", &g_config->aspect_size.width, &g_config->aspect_size.height) == 2) { - Renderer::instance()->apply_config(); + VideoSystem::current()->get_renderer().apply_config(); MenuManager::instance().on_window_resize(); } else @@ -242,7 +242,7 @@ OptionsMenu::menu_action(MenuItem* item) { g_config->magnification /= 100.0f; } - Renderer::instance()->apply_config(); + VideoSystem::current()->get_renderer().apply_config(); MenuManager::instance().on_window_resize(); break; @@ -271,7 +271,7 @@ OptionsMenu::menu_action(MenuItem* item) case MNID_FULLSCREEN: if(g_config->use_fullscreen != is_toggled(MNID_FULLSCREEN)) { g_config->use_fullscreen = !g_config->use_fullscreen; - Renderer::instance()->apply_config(); + VideoSystem::current()->get_renderer().apply_config(); MenuManager::instance().on_window_resize(); g_config->save(); } diff --git a/src/supertux/screen_manager.cpp b/src/supertux/screen_manager.cpp index 9e11eed7d..ea716ce9d 100644 --- a/src/supertux/screen_manager.cpp +++ b/src/supertux/screen_manager.cpp @@ -208,8 +208,8 @@ ScreenManager::process_events() switch(event.window.event) { case SDL_WINDOWEVENT_RESIZED: - Renderer::instance()->resize(event.window.data1, - event.window.data2); + VideoSystem::current()->get_renderer().resize(event.window.data1, + event.window.data2); m_menu_manager->on_window_resize(); break; } @@ -223,7 +223,7 @@ ScreenManager::process_events() else if (event.key.keysym.sym == SDLK_F11) { g_config->use_fullscreen = !g_config->use_fullscreen; - Renderer::instance()->apply_config(); + VideoSystem::current()->get_renderer().apply_config(); m_menu_manager->on_window_resize(); } else if (event.key.keysym.sym == SDLK_PRINTSCREEN || diff --git a/src/video/drawing_context.cpp b/src/video/drawing_context.cpp index 235126199..e093e5bf1 100644 --- a/src/video/drawing_context.cpp +++ b/src/video/drawing_context.cpp @@ -29,7 +29,7 @@ #include "video/surface.hpp" #include "video/texture.hpp" #include "video/texture_manager.hpp" -#include "video/video_systems.hpp" +#include "video/video_system.hpp" DrawingContext::DrawingContext(Renderer& renderer_, Lightmap& lightmap_) : renderer(renderer_), diff --git a/src/video/gl/gl_renderer.cpp b/src/video/gl/gl_renderer.cpp index 2c813f3ce..734895ff9 100644 --- a/src/video/gl/gl_renderer.cpp +++ b/src/video/gl/gl_renderer.cpp @@ -43,8 +43,6 @@ GLRenderer::GLRenderer() : m_desktop_size(0, 0), m_fullscreen_active(false) { - Renderer::instance_ = this; - SDL_DisplayMode mode; SDL_GetCurrentDisplayMode(0, &mode); m_desktop_size = Size(mode.w, mode.h); diff --git a/src/video/gl/gl_texture.cpp b/src/video/gl/gl_texture.cpp index fe3864c0a..b5ad44e6a 100644 --- a/src/video/gl/gl_texture.cpp +++ b/src/video/gl/gl_texture.cpp @@ -56,8 +56,8 @@ GLTexture::GLTexture(unsigned int width, unsigned int height) : try { glBindTexture(GL_TEXTURE_2D, m_handle); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, - m_texture_width, m_texture_height, + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, + m_texture_width, m_texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); set_texture_params(); @@ -139,7 +139,7 @@ GLTexture::GLTexture(SDL_Surface* image) : SDL_LockSurface(convert); } - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, m_texture_width, m_texture_height, 0, sdl_format, GL_UNSIGNED_BYTE, convert->pixels); diff --git a/src/video/gl/gl_video_system.cpp b/src/video/gl/gl_video_system.cpp new file mode 100644 index 000000000..935be6511 --- /dev/null +++ b/src/video/gl/gl_video_system.cpp @@ -0,0 +1,60 @@ +// SuperTux +// Copyright (C) 2014 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "video/gl/gl_video_system.hpp" + +#include "video/gl/gl_lightmap.hpp" +#include "video/gl/gl_renderer.hpp" +#include "video/gl/gl_surface_data.hpp" +#include "video/gl/gl_texture.hpp" + +GLVideoSystem::GLVideoSystem() : + m_renderer(new GLRenderer), + m_lightmap(new GLLightmap) +{ +} + +Renderer& +GLVideoSystem::get_renderer() +{ + return *m_renderer; +} + +Lightmap& +GLVideoSystem::get_lightmap() +{ + return *m_lightmap; +} + +TexturePtr +GLVideoSystem::new_texture(SDL_Surface* image) +{ + return TexturePtr(new GLTexture(image)); +} + +SurfaceData* +GLVideoSystem::new_surface_data(const Surface& surface) +{ + return new GLSurfaceData(surface); +} + +void +GLVideoSystem::free_surface_data(SurfaceData* surface_data) +{ + delete surface_data; +} + +/* EOF */ diff --git a/src/video/gl/gl_video_system.hpp b/src/video/gl/gl_video_system.hpp new file mode 100644 index 000000000..eab8a0895 --- /dev/null +++ b/src/video/gl/gl_video_system.hpp @@ -0,0 +1,47 @@ +// SuperTux +// Copyright (C) 2014 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef HEADER_SUPERTUX_VIDEO_GL_GL_VIDEO_SYSTEM_HPP +#define HEADER_SUPERTUX_VIDEO_GL_GL_VIDEO_SYSTEM_HPP + +#include +#include + +#include "video/video_system.hpp" + +class GLVideoSystem : public VideoSystem +{ +private: + std::unique_ptr m_renderer; + std::unique_ptr m_lightmap; + +public: + GLVideoSystem(); + + Renderer& get_renderer() override; + Lightmap& get_lightmap() override; + TexturePtr new_texture(SDL_Surface* image) override; + SurfaceData* new_surface_data(const Surface& surface) override; + void free_surface_data(SurfaceData* surface_data) override; + +private: + GLVideoSystem(const GLVideoSystem&) = delete; + GLVideoSystem& operator=(const GLVideoSystem&) = delete; +}; + +#endif + +/* EOF */ diff --git a/src/video/renderer.cpp b/src/video/renderer.cpp index db9e32dc3..f595db4a7 100644 --- a/src/video/renderer.cpp +++ b/src/video/renderer.cpp @@ -16,8 +16,6 @@ #include "video/renderer.hpp" -Renderer* Renderer::instance_ = 0; - Renderer::Renderer() { } diff --git a/src/video/renderer.hpp b/src/video/renderer.hpp index 41e24708c..b9d4ec370 100644 --- a/src/video/renderer.hpp +++ b/src/video/renderer.hpp @@ -57,11 +57,6 @@ public: virtual Vector to_logical(int physical_x, int physical_y) = 0; virtual void set_gamma(float gamma) = 0; virtual SDL_Window* get_window() const = 0; - - static Renderer* instance() { assert(instance_); return instance_; } - -protected: - static Renderer* instance_; }; #endif diff --git a/src/video/sdl/sdl_lightmap.cpp b/src/video/sdl/sdl_lightmap.cpp index 19ca449e3..68dc08529 100644 --- a/src/video/sdl/sdl_lightmap.cpp +++ b/src/video/sdl/sdl_lightmap.cpp @@ -23,7 +23,7 @@ #include "video/sdl/sdl_painter.hpp" SDLLightmap::SDLLightmap() : - m_renderer(static_cast(Renderer::instance())->get_sdl_renderer()), + m_renderer(static_cast(VideoSystem::current()->get_renderer()).get_sdl_renderer()), m_texture(), m_width(), m_height(), diff --git a/src/video/sdl/sdl_renderer.cpp b/src/video/sdl/sdl_renderer.cpp index 72ef39d01..c95ee4644 100644 --- a/src/video/sdl/sdl_renderer.cpp +++ b/src/video/sdl/sdl_renderer.cpp @@ -39,8 +39,6 @@ SDLRenderer::SDLRenderer() : m_desktop_size(0, 0), m_scale(1.0f, 1.0f) { - Renderer::instance_ = this; - SDL_DisplayMode mode; if (SDL_GetDesktopDisplayMode(0, &mode) != 0) { diff --git a/src/video/sdl/sdl_texture.cpp b/src/video/sdl/sdl_texture.cpp index fcb8d73c8..8019de604 100644 --- a/src/video/sdl/sdl_texture.cpp +++ b/src/video/sdl/sdl_texture.cpp @@ -33,7 +33,7 @@ SDLTexture::SDLTexture(SDL_Surface* image) : m_width(), m_height() { - m_texture = SDL_CreateTextureFromSurface(static_cast(Renderer::instance())->get_sdl_renderer(), + m_texture = SDL_CreateTextureFromSurface(static_cast(VideoSystem::current()->get_renderer()).get_sdl_renderer(), image); if (!m_texture) { diff --git a/src/video/sdl/sdl_video_system.cpp b/src/video/sdl/sdl_video_system.cpp new file mode 100644 index 000000000..f0aae38b8 --- /dev/null +++ b/src/video/sdl/sdl_video_system.cpp @@ -0,0 +1,65 @@ +// SuperTux +// Copyright (C) 2014 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "video/sdl/sdl_video_system.hpp" + +#include "supertux/gameconfig.hpp" +#include "video/lightmap.hpp" +#include "video/renderer.hpp" +#include "video/sdl/sdl_lightmap.hpp" +#include "video/sdl/sdl_renderer.hpp" +#include "video/sdl/sdl_surface_data.hpp" +#include "video/sdl/sdl_texture.hpp" +#include "video/texture.hpp" +#include "video/video_system.hpp" + +SDLVideoSystem::SDLVideoSystem() : + m_renderer(new SDLRenderer), + m_lightmap(new SDLLightmap) +{ +} + +Renderer& +SDLVideoSystem::get_renderer() +{ + return *m_renderer; +} + +Lightmap& +SDLVideoSystem::get_lightmap() +{ + return *m_lightmap; +} + +TexturePtr +SDLVideoSystem::new_texture(SDL_Surface* image) +{ + return TexturePtr(new SDLTexture(image)); +} + +SurfaceData* +SDLVideoSystem::new_surface_data(const Surface& surface) +{ + return new SDLSurfaceData(surface); +} + +void +SDLVideoSystem::free_surface_data(SurfaceData* surface_data) +{ + delete surface_data; +} + +/* EOF */ diff --git a/src/video/sdl/sdl_video_system.hpp b/src/video/sdl/sdl_video_system.hpp new file mode 100644 index 000000000..2445f4510 --- /dev/null +++ b/src/video/sdl/sdl_video_system.hpp @@ -0,0 +1,47 @@ +// SuperTux +// Copyright (C) 2014 Ingo Ruhnke +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef HEADER_SUPERTUX_VIDEO_SDL_SDL_VIDEO_SYSTEM_HPP +#define HEADER_SUPERTUX_VIDEO_SDL_SDL_VIDEO_SYSTEM_HPP + +#include +#include + +#include "video/video_system.hpp" + +class SDLVideoSystem : public VideoSystem +{ +private: + std::unique_ptr m_renderer; + std::unique_ptr m_lightmap; + +public: + SDLVideoSystem(); + + Renderer& get_renderer(); + Lightmap& get_lightmap(); + TexturePtr new_texture(SDL_Surface *image) override; + SurfaceData* new_surface_data(const Surface& surface) override; + void free_surface_data(SurfaceData* surface_data) override; + +private: + SDLVideoSystem(const SDLVideoSystem&) = delete; + SDLVideoSystem& operator=(const SDLVideoSystem&) = delete; +}; + +#endif + +/* EOF */ diff --git a/src/video/surface.cpp b/src/video/surface.cpp index 790aa3ffd..2236f62d6 100644 --- a/src/video/surface.cpp +++ b/src/video/surface.cpp @@ -21,7 +21,7 @@ #include #include "video/texture.hpp" -#include "video/video_systems.hpp" +#include "video/video_system.hpp" SurfacePtr Surface::create(const std::string& file) @@ -43,7 +43,7 @@ Surface::Surface(const std::string& file) : texture->get_image_height())), flipx(false) { - surface_data = VideoSystem::new_surface_data(*this); + surface_data = VideoSystem::current()->new_surface_data(*this); } Surface::Surface(const std::string& file, const Rect& rect_) : @@ -52,7 +52,7 @@ Surface::Surface(const std::string& file, const Rect& rect_) : rect(0, 0, Size(rect_.get_width(), rect_.get_height())), flipx(false) { - surface_data = VideoSystem::new_surface_data(*this); + surface_data = VideoSystem::current()->new_surface_data(*this); } Surface::Surface(const Surface& rhs) : @@ -61,12 +61,12 @@ Surface::Surface(const Surface& rhs) : rect(rhs.rect), flipx(false) { - surface_data = VideoSystem::new_surface_data(*this); + surface_data = VideoSystem::current()->new_surface_data(*this); } Surface::~Surface() { - VideoSystem::free_surface_data(surface_data); + VideoSystem::current()->free_surface_data(surface_data); } SurfacePtr diff --git a/src/video/texture_manager.cpp b/src/video/texture_manager.cpp index 7d7830efa..27b3f97b5 100644 --- a/src/video/texture_manager.cpp +++ b/src/video/texture_manager.cpp @@ -28,7 +28,7 @@ #include "util/log.hpp" #include "video/sdl_surface_ptr.hpp" #include "video/texture.hpp" -#include "video/video_systems.hpp" +#include "video/video_system.hpp" #ifdef HAVE_OPENGL #include "video/gl/gl_texture.hpp" @@ -171,7 +171,7 @@ TextureManager::create_image_texture_raw(const std::string& filename, const Rect } #endif - return VideoSystem::new_texture(subimage.get()); + return VideoSystem::current()->new_texture(subimage.get()); } TexturePtr @@ -200,7 +200,7 @@ TextureManager::create_image_texture_raw(const std::string& filename) } else { - TexturePtr texture = VideoSystem::new_texture(image.get()); + TexturePtr texture = VideoSystem::current()->new_texture(image.get()); image.reset(NULL); return texture; } @@ -229,7 +229,7 @@ TextureManager::create_dummy_texture() else { log_warning << "Couldn't load texture '" << dummy_texture_fname << "' (now using empty one): " << err.what() << std::endl; - TexturePtr texture = VideoSystem::new_texture(image.get()); + TexturePtr texture = VideoSystem::current()->new_texture(image.get()); image.reset(NULL); return texture; } diff --git a/src/video/video_system.cpp b/src/video/video_system.cpp new file mode 100644 index 000000000..f515f566c --- /dev/null +++ b/src/video/video_system.cpp @@ -0,0 +1,107 @@ +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#include "video/video_system.hpp" + +#include + +#include "util/log.hpp" +#include "video/sdl/sdl_video_system.hpp" + +#ifdef HAVE_OPENGL +#include "video/gl/gl_video_system.hpp" +#endif + +std::unique_ptr +VideoSystem::create(VideoSystem::Enum video_system) +{ + switch(video_system) + { + case AUTO_VIDEO: +#ifdef HAVE_OPENGL + try + { + return std::unique_ptr(new GLVideoSystem); + } + catch(std::exception& err) + { + log_warning << "Error creating GLVideoSystem, using SDL fallback: " << err.what() << std::endl; + return std::unique_ptr(new SDLVideoSystem); + } +#else + log_info << "new SDL renderer\n"; + return std::unique_ptr(new SDLVideoSystem); +#endif + + case OPENGL: +#ifdef HAVE_OPENGL + return std::unique_ptr(new GLVideoSystem); +#else + log_warning << "OpenGL requested, but missing using SDL fallback" << std::endl; + return std::unique_ptr(new SDLVideoSystem); +#endif + + case PURE_SDL: + log_info << "new SDL renderer\n"; + return std::unique_ptr(new SDLVideoSystem); + + default: + assert(!"invalid video system in config"); + break; + } +} + +VideoSystem::Enum +VideoSystem::get_video_system(const std::string &video) +{ + if(video == "auto") + { + return AUTO_VIDEO; + } +#ifdef HAVE_OPENGL + else if(video == "opengl") + { + return OPENGL; + } +#endif + else if(video == "sdl") + { + return PURE_SDL; + } + else + { + return AUTO_VIDEO; + } +} + +std::string +VideoSystem::get_video_string(VideoSystem::Enum video) +{ + switch(video) + { + case AUTO_VIDEO: + return "auto"; + case OPENGL: + return "opengl"; + case PURE_SDL: + return "sdl"; + default: + assert(!"invalid video system in config"); + return "auto"; + } +} + +/* EOF */ diff --git a/src/video/video_system.hpp b/src/video/video_system.hpp new file mode 100644 index 000000000..3ad7559e6 --- /dev/null +++ b/src/video/video_system.hpp @@ -0,0 +1,63 @@ +// SuperTux +// Copyright (C) 2006 Matthias Braun +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#ifndef HEADER_SUPERTUX_VIDEO_VIDEO_SYSTEM_HPP +#define HEADER_SUPERTUX_VIDEO_VIDEO_SYSTEM_HPP + +#include +#include + +#include "util/currenton.hpp" +#include "video/texture_ptr.hpp" + +class Renderer; +class Lightmap; +class Surface; +class SurfaceData; + +class VideoSystem : public Currenton +{ +public: + enum Enum { + AUTO_VIDEO, + OPENGL, + PURE_SDL, + NUM_SYSTEMS + }; + + static std::unique_ptr create(VideoSystem::Enum video_system); + + static Enum get_video_system(const std::string &video); + static std::string get_video_string(Enum video); + +public: + VideoSystem() {} + virtual ~VideoSystem() {} + + virtual Renderer& get_renderer() = 0; + virtual Lightmap& get_lightmap() = 0; + virtual TexturePtr new_texture(SDL_Surface *image) = 0; + virtual SurfaceData* new_surface_data(const Surface &surface) = 0; + virtual void free_surface_data(SurfaceData* surface_data) = 0; + +private: + VideoSystem(const VideoSystem&) = delete; + VideoSystem& operator=(const VideoSystem&) = delete; +}; + +#endif + +/* EOF */ diff --git a/src/video/video_systems.cpp b/src/video/video_systems.cpp deleted file mode 100644 index 1e37dd23c..000000000 --- a/src/video/video_systems.cpp +++ /dev/null @@ -1,202 +0,0 @@ -// SuperTux -// Copyright (C) 2006 Matthias Braun -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -#include - -#include "supertux/gameconfig.hpp" -#include "video/lightmap.hpp" -#include "video/renderer.hpp" -#include "video/sdl/sdl_lightmap.hpp" -#include "video/sdl/sdl_renderer.hpp" -#include "video/sdl/sdl_surface_data.hpp" -#include "video/sdl/sdl_texture.hpp" -#include "video/texture.hpp" -#include "video/video_systems.hpp" - -#ifdef HAVE_OPENGL -#include "video/gl/gl_lightmap.hpp" -#include "video/gl/gl_renderer.hpp" -#include "video/gl/gl_surface_data.hpp" -#include "video/gl/gl_texture.hpp" -#endif - -std::unique_ptr -VideoSystem::new_renderer() -{ - switch(g_config->video) - { - case AUTO_VIDEO: -#ifdef HAVE_OPENGL - try { - log_info << "new GL renderer\n"; - return std::unique_ptr(new GLRenderer()); - } catch(std::runtime_error& e) { - log_warning << "Error creating GL renderer: " << e.what() << std::endl; -#endif - log_warning << "new SDL renderer\n"; - return std::unique_ptr(new SDLRenderer()); -#ifdef HAVE_OPENGL - } - case OPENGL: - log_info << "new GL renderer\n"; - return std::unique_ptr(new GLRenderer()); -#endif - case PURE_SDL: - log_info << "new SDL renderer\n"; - return std::unique_ptr(new SDLRenderer()); - default: - assert(0 && "invalid video system in config"); -#ifdef HAVE_OPENGL - log_info << "new GL renderer\n"; - return std::unique_ptr(new GLRenderer()); -#else - log_warning << "new SDL renderer\n"; - return std::unique_ptr(new SDLRenderer()); -#endif - } -} - -std::unique_ptr -VideoSystem::new_lightmap() -{ - switch(g_config->video) - { - case AUTO_VIDEO: -#ifdef HAVE_OPENGL - return std::unique_ptr(new GLLightmap()); -#else - return std::unique_ptr(new SDLLightmap()); -#endif -#ifdef HAVE_OPENGL - case OPENGL: - return std::unique_ptr(new GLLightmap()); -#endif - case PURE_SDL: - return std::unique_ptr(new SDLLightmap()); - default: - assert(0 && "invalid video system in config"); -#ifdef HAVE_OPENGL - return std::unique_ptr(new GLLightmap()); -#else - return std::unique_ptr(new SDLLightmap()); -#endif - } -} - -TexturePtr -VideoSystem::new_texture(SDL_Surface *image) -{ - switch(g_config->video) - { - case AUTO_VIDEO: -#ifdef HAVE_OPENGL - return TexturePtr(new GLTexture(image)); -#else - return TexturePtr(new SDLTexture(image)); -#endif -#ifdef HAVE_OPENGL - case OPENGL: - return TexturePtr(new GLTexture(image)); -#endif - case PURE_SDL: - return TexturePtr(new SDLTexture(image)); - default: - assert(0 && "invalid video system in config"); -#ifdef HAVE_OPENGL - return TexturePtr(new GLTexture(image)); -#else - return TexturePtr(new SDLTexture(image)); -#endif - } -} - -SurfaceData* -VideoSystem::new_surface_data(const Surface &surface) -{ - switch(g_config->video) - { - case AUTO_VIDEO: -#ifdef HAVE_OPENGL - return new GLSurfaceData(surface); -#else - return new SDLSurfaceData(surface); -#endif -#ifdef HAVE_OPENGL - case OPENGL: - return new GLSurfaceData(surface); -#endif - case PURE_SDL: - return new SDLSurfaceData(surface); - default: - assert(0 && "invalid video system in config"); -#ifdef HAVE_OPENGL - return new GLSurfaceData(surface); -#else - return new SDLSurfaceData(surface); -#endif - } -} - -void -VideoSystem::free_surface_data(SurfaceData* surface_data) -{ - if(surface_data == NULL) - return; - - delete surface_data; -} - -VideoSystem::Enum -VideoSystem::get_video_system(const std::string &video) -{ - if(video == "auto") - { - return AUTO_VIDEO; - } -#ifdef HAVE_OPENGL - else if(video == "opengl") - { - return OPENGL; - } -#endif - else if(video == "sdl") - { - return PURE_SDL; - } - else - { - return AUTO_VIDEO; - } -} - -std::string -VideoSystem::get_video_string(VideoSystem::Enum video) -{ - switch(video) - { - case AUTO_VIDEO: - return "auto"; - case OPENGL: - return "opengl"; - case PURE_SDL: - return "sdl"; - default: - assert(0 && "invalid video system in config"); - return "auto"; - } -} - -/* EOF */ diff --git a/src/video/video_systems.hpp b/src/video/video_systems.hpp deleted file mode 100644 index f65d7bfd6..000000000 --- a/src/video/video_systems.hpp +++ /dev/null @@ -1,60 +0,0 @@ -// SuperTux -// Copyright (C) 2006 Matthias Braun -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -#ifndef HEADER_SUPERTUX_VIDEO_VIDEO_SYSTEMS_HPP -#define HEADER_SUPERTUX_VIDEO_VIDEO_SYSTEMS_HPP - -#include - -#include -#include - -#include "video/texture_ptr.hpp" - -class Renderer; -class Lightmap; -class Surface; -class SurfaceData; - -class VideoSystem -{ -public: - enum Enum { - AUTO_VIDEO, - OPENGL, - PURE_SDL, - NUM_SYSTEMS - }; - -public: - static std::unique_ptr new_renderer(); - static std::unique_ptr new_lightmap(); - static TexturePtr new_texture(SDL_Surface *image); - static SurfaceData* new_surface_data(const Surface &surface); - static void free_surface_data(SurfaceData* surface_data); - - static Enum get_video_system(const std::string &video); - static std::string get_video_string(Enum video); - -private: - VideoSystem(); - VideoSystem(const VideoSystem&); - VideoSystem& operator=(const VideoSystem&); -}; - -#endif - -/* EOF */