/** * ========================================================================= * File : SkyManager.cpp * Project : Pyrogenesis * Description : Sky settings, texture management and rendering. * * @author Matei Zaharia * ========================================================================= */ #include "precompiled.h" #include "lib/timer.h" #include "lib/res/file/vfs.h" #include "lib/res/graphics/tex.h" #include "lib/res/graphics/ogl_tex.h" #include "ps/CLogger.h" #include "ps/Loader.h" #include "renderer/SkyManager.h" #include "renderer/Renderer.h" #include "graphics/Camera.h" #define LOG_CATEGORY "graphics" /////////////////////////////////////////////////////////////////////////////////////////////// // SkyManager implementation /////////////////////////////////////////////////////////////////// // String names for each image, in order of the IMG_ constants const char* SkyManager::IMAGE_NAMES[5] = { "front", "back", "right", "left", "top" }; /////////////////////////////////////////////////////////////////// // Construction/Destruction SkyManager::SkyManager() { m_RenderSky = true; // TODO: add a way to set the initial skyset before progressive load m_SkySet = L"default"; for (uint i = 0; i < ARRAY_SIZE(m_SkyTexture); i++) m_SkyTexture[i] = 0; cur_loading_tex = 0; } SkyManager::~SkyManager() { // Cleanup if the caller messed up UnloadSkyTextures(); } /////////////////////////////////////////////////////////////////// // Progressive load of sky textures int SkyManager::LoadSkyTextures() { const uint num_textures = ARRAY_SIZE(m_SkyTexture); // yield after this time is reached. balances increased progress bar // smoothness vs. slowing down loading. const double end_time = get_time() + 100e-3; while (cur_loading_tex < num_textures) { char filename[PATH_MAX]; snprintf(filename, ARRAY_SIZE(filename), "art/textures/skies/%s/%s.dds", CStr8(m_SkySet).c_str(), IMAGE_NAMES[cur_loading_tex]); Handle ht = ogl_tex_load(filename); if (ht <= 0) { LOG(ERROR, LOG_CATEGORY, "SkyManager::LoadSkyTextures failed on \"%s\"", filename); return ht; } ogl_tex_set_wrap(ht, GL_CLAMP_TO_EDGE); m_SkyTexture[cur_loading_tex] = ht; RETURN_ERR(ogl_tex_upload(ht)); cur_loading_tex++; LDR_CHECK_TIMEOUT(cur_loading_tex, num_textures); } return 0; } /////////////////////////////////////////////////////////////////// // Unload sky textures void SkyManager::UnloadSkyTextures() { for(uint i = 0; i < ARRAY_SIZE(m_SkyTexture); i++) { ogl_tex_free(m_SkyTexture[i]); m_SkyTexture[i] = 0; } cur_loading_tex = 0; } /////////////////////////////////////////////////////////////////// // Switch to a different sky set (while the game is running) void SkyManager::SetSkySet( CStrW newSet ) { if( newSet != m_SkySet ) { m_SkySet = newSet; UnloadSkyTextures(); for( int i=0; i