[colobot] 03/377: Refactored window resize

Didier Raboud odyx at moszumanska.debian.org
Wed Mar 30 13:33:51 UTC 2016


This is an automated email from the git hooks/post-receive script.

odyx pushed a commit to branch debian/master
in repository colobot.

commit dfa06dafcf0ec98d99361fde9fa1de7cc8a80322
Author: krzys-h <krzys_h at interia.pl>
Date:   Fri Sep 25 11:11:35 2015 +0200

    Refactored window resize
---
 src/app/app.cpp                    | 67 +++-----------------------------------
 src/app/app.h                      |  2 --
 src/graphics/engine/engine.cpp     | 56 ++++++++-----------------------
 src/graphics/engine/engine.h       |  5 ++-
 src/graphics/engine/text.cpp       | 15 +++++----
 src/graphics/engine/text.h         |  5 +--
 src/graphics/opengl/gl21device.cpp | 15 +++++++--
 src/graphics/opengl/gl33device.cpp | 15 +++++++--
 src/graphics/opengl/gldevice.cpp   | 15 +++++++--
 src/level/robotmain.cpp            | 14 +++++---
 src/level/robotmain.h              |  3 +-
 11 files changed, 82 insertions(+), 130 deletions(-)

diff --git a/src/app/app.cpp b/src/app/app.cpp
index fb2e12a..ecdaa5e 100644
--- a/src/app/app.cpp
+++ b/src/app/app.cpp
@@ -655,25 +655,7 @@ bool CApplication::Create()
 
 bool CApplication::CreateVideoSurface()
 {
-    /*const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
-    if (videoInfo == nullptr)
-    {
-        m_errorMessage = std::string("SDL error while getting video info:\n ") +
-                         std::string(SDL_GetError());
-        GetLogger()->Error(m_errorMessage.c_str());
-        m_exitCode = 7;
-        return false;
-    }*/
-
-    Uint32 videoFlags = SDL_WINDOW_OPENGL; // | SDL_GL_DOUBLEBUFFER | SDL_HWPALETTE
-
-    // Use hardware surface if available
-    /*if (videoInfo->hw_available)
-        videoFlags |= SDL_HWSURFACE;*/
-
-    // Enable hardware blit if available
-    /*if (videoInfo->blit_hw)
-        videoFlags |= SDL_HWACCEL;*/
+    Uint32 videoFlags = SDL_WINDOW_OPENGL;
 
     if (m_deviceConfig.fullScreen)
         videoFlags |= SDL_WINDOW_FULLSCREEN;
@@ -710,54 +692,15 @@ bool CApplication::CreateVideoSurface()
 
 bool CApplication::ChangeVideoConfig(const Gfx::DeviceConfig &newConfig)
 {
-    static bool restore = false;
-
-    m_lastDeviceConfig = m_deviceConfig;
     m_deviceConfig = newConfig;
 
-    SDL_GL_DeleteContext(m_private->glcontext); //TODO: refactor this
-    SDL_DestroyWindow(m_private->window); // TODO: ?
-
-    if (! CreateVideoSurface())
-    {
-        // Fatal error, so post the quit event
-        m_eventQueue->AddEvent(Event(EVENT_SYS_QUIT));
-        return false;
-    }
-
-    if (m_private->window == nullptr)
-    {
-        if (! restore)
-        {
-            std::string error = std::string("SDL error while setting video mode:\n") +
-                          std::string(SDL_GetError()) + std::string("\n") +
-                          std::string("Previous mode will be restored");
-            GetLogger()->Error(error.c_str());
-            m_systemUtils->SystemDialog( SDT_ERROR, "COLOBOT - Error", error);
-
-            restore = true;
-            ChangeVideoConfig(m_lastDeviceConfig);
-            return false;
-        }
-        else
-        {
-            restore = false;
-
-            std::string error = std::string("SDL error while restoring previous video mode:\n") +
-                          std::string(SDL_GetError());
-            GetLogger()->Error(error.c_str());
-            m_systemUtils->SystemDialog( SDT_ERROR, "COLOBOT - Fatal Error", error);
-
-
-            // Fatal error, so post the quit event
-            m_eventQueue->AddEvent(Event(EVENT_SYS_QUIT));
-            return false;
-        }
-    }
+    // TODO: Somehow this doesn't work for maximized windows (at least on Ubuntu)
+    SDL_SetWindowSize(m_private->window, m_deviceConfig.size.x, m_deviceConfig.size.y);
+    SDL_SetWindowFullscreen(m_private->window, m_deviceConfig.fullScreen ? SDL_WINDOW_FULLSCREEN : 0);
 
     m_device->ConfigChanged(m_deviceConfig);
 
-    m_engine->ResetAfterDeviceChanged();
+    m_engine->ResetAfterVideoConfigChanged();
 
     return true;
 }
diff --git a/src/app/app.h b/src/app/app.h
index 6295adc..ef737b4 100644
--- a/src/app/app.h
+++ b/src/app/app.h
@@ -369,8 +369,6 @@ protected:
 
     //! Current configuration of OpenGL display device
     Gfx::DeviceConfig m_deviceConfig;
-    //! Previous configuration of OpenGL display device
-    Gfx::DeviceConfig m_lastDeviceConfig;
 
     //! Text set as window title
     std::string     m_windowTitle;
diff --git a/src/graphics/engine/engine.cpp b/src/graphics/engine/engine.cpp
index ae5aaf7..7cb4ed3 100644
--- a/src/graphics/engine/engine.cpp
+++ b/src/graphics/engine/engine.cpp
@@ -362,55 +362,27 @@ void CEngine::Destroy()
     m_planet.reset();
 }
 
-void CEngine::ResetAfterDeviceChanged()
+void CEngine::ResetAfterVideoConfigChanged()
 {
     m_size = m_app->GetVideoConfig().size;
     m_mouseSize = Math::Point(0.04f, 0.04f * (static_cast<float>(m_size.x) / static_cast<float>(m_size.y)));
 
-    if (m_shadowMap.id != 0)
-    {
-        if (m_offscreenShadowRendering)
-            m_device->DeleteFramebuffer("shadow");
-        else
-            m_device->DestroyTexture(m_shadowMap);
+    CRobotMain::GetInstancePointer()->ResetAfterVideoConfigChanged(); //TODO: Remove cross-reference to CRobotMain
 
-        m_shadowMap = Texture();
-    }
+    // Update the camera projection matrix for new aspect ratio
+    SetFocus(m_focus);
 
-    m_text->FlushCache();
+    // This needs to be recreated on resolution change
+    m_device->DeleteFramebuffer("multisample");
+}
 
+void CEngine::ReloadAllTextures()
+{
     FlushTextureCache();
+    m_text->FlushCache();
 
-    CRobotMain::GetInstancePointer()->ResetAfterDeviceChanged();
-
+    CRobotMain::GetInstancePointer()->ReloadAllTextures(); //TODO: Remove cross-reference to CRobotMain
     LoadAllTextures();
-
-    for (int baseObjRank = 0; baseObjRank < static_cast<int>( m_baseObjects.size() ); baseObjRank++)
-    {
-        EngineBaseObject& p1 = m_baseObjects[baseObjRank];
-        if (! p1.used)
-            continue;
-
-        for (int l2 = 0; l2 < static_cast<int>( p1.next.size() ); l2++)
-        {
-            EngineBaseObjTexTier& p2 = p1.next[l2];
-
-            for (int l3 = 0; l3 < static_cast<int>( p2.next.size() ); l3++)
-            {
-                EngineBaseObjDataTier& p3 = p2.next[l3];
-
-                m_device->DestroyStaticBuffer(p3.staticBufferId);
-                p3.staticBufferId = 0;
-                p3.updateStaticBuffer = true;
-            }
-        }
-    }
-
-    // Update the camera projection matrix for new aspect ratio
-    SetFocus(m_focus);
-
-    // Because reloading textures takes a long time
-    m_app->ResetTimeAfterLoading();
 }
 
 bool CEngine::ProcessEvent(const Event &event)
@@ -2875,7 +2847,7 @@ void CEngine::SetTextureFilterMode(TexFilter value)
 
     m_defaultTexParams.filter = m_terrainTexParams.filter = value;
     m_defaultTexParams.mipmap = m_terrainTexParams.mipmap = (value == TEX_FILTER_TRILINEAR);
-    ResetAfterDeviceChanged();
+    ReloadAllTextures();
 }
 
 TexFilter CEngine::GetTextureFilterMode()
@@ -2890,7 +2862,7 @@ void CEngine::SetTextureMipmapLevel(int value)
     if(m_textureMipmapLevel == value) return;
 
     m_textureMipmapLevel = value;
-    ResetAfterDeviceChanged();
+    ReloadAllTextures();
 }
 
 int CEngine::GetTextureMipmapLevel()
@@ -2906,7 +2878,7 @@ void CEngine::SetTextureAnisotropyLevel(int value)
     if(m_textureAnisotropy == value) return;
 
     m_textureAnisotropy = value;
-    ResetAfterDeviceChanged();
+    ReloadAllTextures();
 }
 
 int CEngine::GetTextureAnisotropyLevel()
diff --git a/src/graphics/engine/engine.h b/src/graphics/engine/engine.h
index 29e0b56..3d5fbbe 100644
--- a/src/graphics/engine/engine.h
+++ b/src/graphics/engine/engine.h
@@ -659,7 +659,7 @@ public:
     void            Destroy();
 
     //! Resets some states and flushes textures after device was changed (e.g. resoulution changed)
-    void            ResetAfterDeviceChanged();
+    void            ResetAfterVideoConfigChanged();
 
 
     //! Called once per frame, the call is the entry point for rendering
@@ -1271,6 +1271,9 @@ protected:
     };
     static void WriteScreenShotThread(std::unique_ptr<WriteScreenShotData> data);
 
+    //! Reloads all textures
+    void ReloadAllTextures();
+
 protected:
     CApplication*     m_app;
     CSystemUtils*     m_systemUtils;
diff --git a/src/graphics/engine/text.cpp b/src/graphics/engine/text.cpp
index e8f3840..5cf724c 100644
--- a/src/graphics/engine/text.cpp
+++ b/src/graphics/engine/text.cpp
@@ -389,7 +389,7 @@ float CText::GetCharWidth(UTF8Char ch, FontType font, float size, float offset)
     auto it = cf->cache.find(ch);
     if (it != cf->cache.end())
     {
-        charSize = (*it).second.charSize;
+        charSize = m_engine->WindowToInterfaceSize((*it).second.charSize);
     }
     else
     {
@@ -926,8 +926,11 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::P
 
         CharTexture tex = GetCharTexture(ch, font, size);
 
-        Math::Point p1(pos.x, pos.y + tex.charSize.y - tex.texSize.y);
-        Math::Point p2(pos.x + tex.texSize.x, pos.y + tex.charSize.y);
+        Math::Point charSize = m_engine->WindowToInterfaceSize(tex.charSize);
+        Math::Point texSize  = m_engine->WindowToInterfaceSize(tex.texSize);
+
+        Math::Point p1(pos.x, pos.y + charSize.y - texSize.y);
+        Math::Point p2(pos.x + texSize.x, pos.y + charSize.y);
 
         Math::Vector n(0.0f, 0.0f, -1.0f);  // normal
 
@@ -943,7 +946,7 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::P
         m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, quad, 4, color);
         m_engine->AddStatisticTriangle(2);
 
-        pos.x += tex.charSize.x * width;
+        pos.x += charSize.x * width;
     }
 }
 
@@ -1038,8 +1041,8 @@ CharTexture CText::CreateCharTexture(UTF8Char ch, CachedFont* font)
     else
     {
         texture.id = tex.id;
-        texture.texSize =  m_engine->WindowToInterfaceSize(Math::IntPoint(textureSurface->w, textureSurface->h));
-        texture.charSize = m_engine->WindowToInterfaceSize(Math::IntPoint(textSurface->w, textSurface->h));
+        texture.texSize =  Math::IntPoint(textureSurface->w, textureSurface->h);
+        texture.charSize = Math::IntPoint(textSurface->w, textSurface->h);
     }
 
     SDL_FreeSurface(textSurface);
diff --git a/src/graphics/engine/text.h b/src/graphics/engine/text.h
index 1ab4821..6ce6be3 100644
--- a/src/graphics/engine/text.h
+++ b/src/graphics/engine/text.h
@@ -28,6 +28,7 @@
 #include "graphics/core/color.h"
 
 #include "math/point.h"
+#include "math/intpoint.h"
 
 #include <map>
 #include <memory>
@@ -190,8 +191,8 @@ struct UTF8Char
 struct CharTexture
 {
     unsigned int id = 0;
-    Math::Point texSize;
-    Math::Point charSize;
+    Math::IntPoint texSize;
+    Math::IntPoint charSize;
 };
 
 // Definition is private - in text.cpp
diff --git a/src/graphics/opengl/gl21device.cpp b/src/graphics/opengl/gl21device.cpp
index bf3756e..d07c6be 100644
--- a/src/graphics/opengl/gl21device.cpp
+++ b/src/graphics/opengl/gl21device.cpp
@@ -164,7 +164,7 @@ bool CGL21Device::Create()
 {
     GetLogger()->Info("Creating CDevice - OpenGL 2.1\n");
 
-    /*static*/ bool glewInited = false;
+    static bool glewInited = false;
 
     if (!glewInited)
     {
@@ -396,8 +396,17 @@ void CGL21Device::ConfigChanged(const DeviceConfig& newConfig)
 
     // Reset state
     m_lighting = false;
-    Destroy();
-    Create();
+
+    glViewport(0, 0, m_config.size.x, m_config.size.y);
+
+    // create default framebuffer object
+    FramebufferParams framebufferParams;
+
+    framebufferParams.width = m_config.size.x;
+    framebufferParams.height = m_config.size.y;
+    framebufferParams.depth = m_config.depthSize;
+
+    m_framebuffers["default"] = MakeUnique<CDefaultFramebuffer>(framebufferParams);
 }
 
 void CGL21Device::BeginScene()
diff --git a/src/graphics/opengl/gl33device.cpp b/src/graphics/opengl/gl33device.cpp
index 251c1ec..65a8584 100644
--- a/src/graphics/opengl/gl33device.cpp
+++ b/src/graphics/opengl/gl33device.cpp
@@ -164,7 +164,7 @@ bool CGL33Device::Create()
 {
     GetLogger()->Info("Creating CDevice - OpenGL 3.3\n");
 
-    /*static*/ bool glewInited = false;
+    static bool glewInited = false;
 
     if (!glewInited)
     {
@@ -421,8 +421,17 @@ void CGL33Device::ConfigChanged(const DeviceConfig& newConfig)
 
     // Reset state
     m_lighting = false;
-    Destroy();
-    Create();
+
+    glViewport(0, 0, m_config.size.x, m_config.size.y);
+
+    // create default framebuffer object
+    FramebufferParams framebufferParams;
+
+    framebufferParams.width = m_config.size.x;
+    framebufferParams.height = m_config.size.y;
+    framebufferParams.depth = m_config.depthSize;
+
+    m_framebuffers["default"] = MakeUnique<CDefaultFramebuffer>(framebufferParams);
 }
 
 void CGL33Device::BeginScene()
diff --git a/src/graphics/opengl/gldevice.cpp b/src/graphics/opengl/gldevice.cpp
index 2e3c931..102fa9b 100644
--- a/src/graphics/opengl/gldevice.cpp
+++ b/src/graphics/opengl/gldevice.cpp
@@ -160,7 +160,7 @@ bool CGLDevice::Create()
 {
     GetLogger()->Info("Creating CDevice - OpenGL 1.4\n");
 
-    /*static*/ bool glewInited = false;
+    static bool glewInited = false;
 
     if (!glewInited)
     {
@@ -337,8 +337,17 @@ void CGLDevice::ConfigChanged(const DeviceConfig& newConfig)
 
     // Reset state
     m_lighting = false;
-    Destroy();
-    Create();
+
+    glViewport(0, 0, m_config.size.x, m_config.size.y);
+
+    // create default framebuffer object
+    FramebufferParams framebufferParams;
+
+    framebufferParams.width = m_config.size.x;
+    framebufferParams.height = m_config.size.y;
+    framebufferParams.depth = m_config.depthSize;
+
+    m_framebuffers["default"] = MakeUnique<CDefaultFramebuffer>(framebufferParams);
 }
 
 void CGLDevice::BeginScene()
diff --git a/src/level/robotmain.cpp b/src/level/robotmain.cpp
index 7d76bd7..674fca4 100644
--- a/src/level/robotmain.cpp
+++ b/src/level/robotmain.cpp
@@ -293,7 +293,15 @@ Ui::CDisplayText* CRobotMain::GetDisplayText()
     return m_displayText.get();
 }
 
-void CRobotMain::ResetAfterDeviceChanged()
+void CRobotMain::ResetAfterVideoConfigChanged()
+{
+    // Recreate the interface (needed if the aspect ratio changes)
+    // TODO: This can sometimes cause unwanted side effects, like hidden windows reappearing. To be fixed during CEGUI refactoring.
+    m_eventQueue->AddEvent(Event(EVENT_UPDINTERFACE));
+    CreateShortcuts();
+}
+
+void CRobotMain::ReloadAllTextures()
 {
     if (m_phase == PHASE_SETUPds ||
        m_phase == PHASE_SETUPgs ||
@@ -307,10 +315,6 @@ void CRobotMain::ResetAfterDeviceChanged()
         ChangeColor();
         UpdateMap();
     }
-
-    // Recreate the interface (needed if the aspect ratio changes)
-    m_eventQueue->AddEvent(Event(EVENT_UPDINTERFACE));
-    CreateShortcuts();
 }
 
 
diff --git a/src/level/robotmain.h b/src/level/robotmain.h
index 604e5e5..dbe319b 100644
--- a/src/level/robotmain.h
+++ b/src/level/robotmain.h
@@ -162,7 +162,8 @@ public:
     void        CreateConfigFile();
     void        LoadConfigFile();
 
-    void        ResetAfterDeviceChanged();
+    void        ResetAfterVideoConfigChanged();
+    void        ReloadAllTextures();
 
     void        ChangePhase(Phase phase);
     bool        ProcessEvent(Event &event);

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git



More information about the Pkg-games-commits mailing list