// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// bool DirectSound8Audio::VInitialize() { if(Audio::IsInitialized()) { return (true); } Audio::SetInitialized(false); Release(m_pDS); HRESULT hr; // Create IDirectSound using the primary sound device if(FAILED(hr = DirectSoundCreate8(NULL, &m_pDS, NULL))) { GF_LOG_TRACE_ERR("DirectSound8Audio::VInitialize()", "Failed to initialized the DirectSound interface"); return (false); } if(FAILED(hr = m_pDS->SetCooperativeLevel(m_hWnd, DSSCL_PRIORITY))) { GF_LOG_TRACE_ERR("DirectSound8Audio::VInitialize()", "Failed to set the coop level of the DirectSound interface"); return (false); } if(FAILED(hr = SetPrimaryBufferFormat(8, 44100, 16))) { GF_LOG_TRACE_ERR("DirectSound8Audio::VInitialize()", "Failed to set the primary buffer format of the DirectSound interface"); return (false); } Audio::SetInitialized(true); m_AllSamples.clear(); return (true); }
// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// void DirectSound8AudioBuffer::VSetVolume(I32 volume) { if(!g_audioPtr || !g_audioPtr->VActive()) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VSetVolume()", "The sound system is not active"); return; } LPDIRECTSOUNDBUFFER pDSB = static_cast<LPDIRECTSOUNDBUFFER>(VGet()); if(!pDSB) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VSetVolume()", "Failed to cast the buffer to a DirectSound buffer"); return; } if(volume < 0) { GF_LOG_TRACE_INF("DirectSound8AudioBuffer::VSetVolume()", "Volume cannot be less than 0 so we are setting it to 0"); volume = 0; } else if(volume > 100) { GF_LOG_TRACE_INF("DirectSound8AudioBuffer::VSetVolume()", "The volume cannot be greater than 100 so we are setting it to 100"); volume = 100; } // convert volume from 0-100 into range for DirectX - don't forget to use a logarithmic scale! F32 coeff = (F32)volume / 100.0f; F32 logarithmicProportion = coeff > 0.1f ? 1 + log10(coeff) : 0; F32 range = (DSBVOLUME_MAX - GCC_DSBVOLUME_MIN); F32 fvolume = (range * logarithmicProportion) + GCC_DSBVOLUME_MIN; assert(fvolume >= GCC_DSBVOLUME_MIN && fvolume <= DSBVOLUME_MAX); HRESULT hr = pDSB->SetVolume(LONG(fvolume)); if(hr != S_OK) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VSetVolume()", "Failed to set the volum of the DirectSound Buffer"); } }
// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// boost::shared_ptr<IAudioBuffer> DirectSound8Audio::VInitAudioBuffer(shared_ptr<SoundResHandle> soundResource)//const { boost::shared_ptr<IAudioBuffer> null; const char *fileExtension = FindExtFromSoundType(soundResource->GetSoundType()); if(m_pDS == NULL) { return (null); } switch(soundResource->GetSoundType()) { case SOUND_TYPE_OGG: case SOUND_TYPE_WAVE: // We support WAVs and OGGs break; case SOUND_TYPE_MP3: case SOUND_TYPE_MIDI: //If it's a midi file, then do nothin at this time... maybe we will support this in the future GF_LOG_TRACE_ERR("DirectSound8Audio::VInitAudioBuffer()", "MP3s and MIDI are not supported"); return (null); break; default: GF_LOG_TRACE_ERR("DirectSound8Audio::VInitAudioBuffer()", "Unknown sound file"); return (null); break; }//end switch LPDIRECTSOUNDBUFFER sampleHandle; // Create the direct sound buffer, and only request the flags needed // since each requires some overhead and limits if the buffer can // be hardware accelerated DSBUFFERDESC dsbd; ZeroMemory(&dsbd, sizeof(DSBUFFERDESC)); dsbd.dwSize = sizeof(DSBUFFERDESC); dsbd.dwFlags = DSBCAPS_CTRLVOLUME; dsbd.dwBufferBytes = soundResource->GetPCMBufferSize(); dsbd.guid3DAlgorithm = GUID_NULL; dsbd.lpwfxFormat = const_cast<WAVEFORMATEX *>(soundResource->GetFormat()); HRESULT hr; if(FAILED(hr = m_pDS->CreateSoundBuffer(&dsbd, &sampleHandle, NULL))) { GF_LOG_TRACE_ERR("DirectSound8Audio::VInitAudioBuffer()", ""); return (null); } // Add handle to the list boost::shared_ptr<IAudioBuffer> audioBuffer(GCC_NEW DirectSound8AudioBuffer(sampleHandle, soundResource)); if(audioBuffer) { m_AllSamples.insert(m_AllSamples.begin(), audioBuffer); } return (audioBuffer); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool EnvironmentSceneNode::VRender() { bool result = SceneNode::VRender(); if(result) { Matrix4 mvp; m_sgmPtr->GetStackManager()->GetModelViewProjectionMatrix(mvp); m_mvpUniform->SetValue((GLfloat * const)mvp.GetComponentsConst(), 16); m_cmUniform->SetValue(0); m_shaderPtr->Activate(); m_cubeBatch.VDraw(); #if defined(DEBUG) if(g_appPtr->GetLoggerPtr() && g_appPtr->GetLoggerPtr()->GetLogLevel() >= GameLog::DEB) { GLenum error = glGetError(); if(error != GL_NO_ERROR) { GF_LOG_TRACE_ERR("EnvironmentSceneNode::VRender()", "The rendering of the cubemap failed"); result = false; } } #endif } return (result); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool EnvironmentSceneNode::VPreRender() { bool result = false; result = SceneNode::VPreRender(); if(result && !g_appPtr->GetTextureManagerPtr()->Bind(m_texHandle, GL_TEXTURE_CUBE_MAP, GL_TEXTURE0)) { GF_LOG_TRACE_ERR("EnvironmentSceneNode::VPreRender()", "Failed to activate the CubeMap texture"); result = false; } if(result) { glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); // Get the camera matrix and clear the cameras position (we want to be able to rotate the environment box but not move it!). Matrix4 camMatrix(m_sgmPtr->GetCamera()->VGet()->GetToWorld()); camMatrix[Matrix4::M30] = 0.0f; camMatrix[Matrix4::M31] = 0.0f; camMatrix[Matrix4::M32] = 0.0f; camMatrix[Matrix4::M33] = 1.0f; m_sgmPtr->GetStackManager()->GetModelViewMatrixStack()->LoadMatrix(camMatrix); } return (result); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool LuaStateManager::ExecuteResourceFile(LuaPlus::LuaStateOwner &luaState, const char * const scriptName, ResCache *rcManagerPtr) { if(!rcManagerPtr || !scriptName || strlen(scriptName) == 0) { return (false); } std::string rn(scriptName); TextResource tr(rn); boost::shared_ptr<TextResHandle> scriptHandle = boost::static_pointer_cast<TextResHandle>(rcManagerPtr->GetHandle(&tr)); if(!scriptHandle || !scriptHandle->VInitialize()) { GF_LOG_TRACE_ERR("LuaStateManager::ExecuteResourceFile()", std::string("Failed to initialize from cache: ") + tr.GetName()); return (false); } std::string scriptData(scriptHandle->GetTextBuffer()); // Split up the file by the newline character. #ifdef _WINDOWS std::string newLine("\n"); #else std::string newLine("\t\n"); #endif // Make a large vector to prevent repeating reallocations (500 - we dont know how long the file is, this is a guess to help reduce reallocations). std::vector<std::string> linesVec; linesVec.reserve(500); boost::algorithm::split(linesVec, scriptData, boost::algorithm::is_any_of(newLine)); // Trim excess capacity. std::vector<std::string>(linesVec.begin(), linesVec.end()).swap(linesVec); RemoveTrailingCr fo; std::for_each(linesVec.begin(), linesVec.end(), fo); scriptData.clear(); for(std::vector<std::string>::iterator i = linesVec.begin(), end = linesVec.end(); i != end; ++i) { scriptData += *i; } if(luaState->DoString(scriptData.c_str()) != 0) { GF_LOG_TRACE_ERR("LuaStateManager::ExecuteResourceFile()", std::string("Failed to execute ") + tr.GetName()); return (false); } return (true); }
// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// void *DirectSound8AudioBuffer::VGet() { if(!VOnRestore()) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VGet()", "Failed to restore the buffer"); return (NULL); } return (m_Sample); }
// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// bool DirectSound8AudioBuffer::VIsPlaying() { if(!g_audioPtr || !g_audioPtr->VActive()) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VIsPlaying()", "The sound system is not active"); return (false); } DWORD dwStatus = 0; LPDIRECTSOUNDBUFFER pDSB = static_cast<LPDIRECTSOUNDBUFFER>(VGet()); if(!pDSB) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VIsPlaying()", "Failed to cast the buffer to a DirectSound buffer"); return (false); } pDSB->GetStatus(&dwStatus); bool bIsPlaying = ((dwStatus & DSBSTATUS_PLAYING) != 0); return (bIsPlaying); }
// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// bool DirectSound8AudioBuffer::VStop() { LPDIRECTSOUNDBUFFER pDSB = static_cast<LPDIRECTSOUNDBUFFER>(VGet()); // If the global audio pointer is NULL OR the sound system is not active. if(!g_audioPtr || !g_audioPtr->VActive()) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VStop()", "The sound system is not active"); return (false); } if(pDSB == NULL) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VStop()", "Failed to cast the buffer to a DirectSound buffer"); return (false); } // Set the base class attribute and stop playback. AudioBuffer::VSetPaused(true); pDSB->Stop(); return (true); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// SliderControl::SliderControl(const LuaPlus::LuaObject &widgetScriptData, \ boost::shared_ptr<ModelViewProjStackManager> mvpStackManPtr, \ const boost::shared_ptr<GLSLShader> shaderFlatObj, \ const boost::shared_ptr<GLSLShader> shaderTexObj, \ boost::shared_ptr<FTFont> fontPtr, \ const ScreenElementId id) throw(GameException &)\ : ControlWidget(widgetScriptData, mvpStackManPtr, shaderFlatObj, shaderTexObj, fontPtr, id) , m_sliderPos(0.5f) , m_sliderButPtr() , m_sliderLineBatch() , m_sliding(false) , m_eventTypeId(0) , m_lineColor(0.0f, 0.0f, 0.0f, 1.0f) { SetLuaSliderPosition(widgetScriptData.GetByName("SliderPosition")); SetLuaEventId(widgetScriptData.GetByName("EventTypeId")); std::string buttonTableName; LuaPlus::LuaObject tableName = widgetScriptData.GetByName("ButtonTableId"); if(tableName.IsString()) { LuaPlus::LuaObject buttonData = widgetScriptData.GetByName(tableName.GetString()); if(buttonData.IsTable()) { m_sliderButPtr.reset(GCC_NEW ButtonControl(buttonData, mvpStackManPtr, shaderFlatObj, shaderTexObj, fontPtr, 0)); m_sliderButPtr->VSetPosition(CalculateButtonPositionFromSlider()); m_sliderButPtr->VSetText(""); m_sliderButPtr->VSetWidth(GetProjectedButtonWidth()); m_sliderButPtr->VSetHeight(GetProjectedButtonHeight()); m_sliderButPtr->VSetVisible(VIsVisible()); m_sliderButPtr->VSetEnabled(VIsEnabled()); m_sliderButPtr->SetSendEvent(false); } else { GF_LOG_TRACE_ERR("SliderControl::SliderControl()", "Creation of scripted slider button failed. Creating default button"); CreateDefaultButton(VGetColor(), mvpStackManPtr, fontPtr, shaderFlatObj, shaderTexObj, VIsVisible(), VIsEnabled()); } } else { GF_LOG_TRACE_ERR("SliderControl::SliderControl()", "Missing slider button information from script so creating default button"); CreateDefaultButton(VGetColor(), mvpStackManPtr, fontPtr, shaderFlatObj, shaderTexObj, VIsVisible(), VIsEnabled()); } RebuildSliderLine(); }
// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// bool DirectSound8AudioBuffer::VPause() { LPDIRECTSOUNDBUFFER pDSB = static_cast<LPDIRECTSOUNDBUFFER>(VGet()); // If the global audio pointer is NULL OR the sound system is not active. if(!g_audioPtr || !g_audioPtr->VActive()) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VPause()", "The sound system is not active"); return (false); } if(pDSB == NULL) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VPause()", "Failed to cast the buffer to a DirectSound buffe"); return (false); } // TODO: See if VPause and VStop are mixed up (SetCurrentPosition here and not in VStop???) AudioBuffer::VSetPaused(true); pDSB->Stop(); pDSB->SetCurrentPosition(0); // rewinds buffer to beginning. return (true); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool LuaStateManager::ExecuteString(char const * const pStringToExecute) { const I32 retVal = m_GlobalState->DoString(pStringToExecute); const bool bSucceeded = (0 == retVal); assert(bSucceeded && "Unable to execute Lua script buffer!"); #if DEBUG if(!bSucceeded) { GF_LOG_TRACE_ERR("LuaStateManager::ExecuteString()", "Failed to execute: " + std::string(pStringToExecute)); } #endif return (bSucceeded); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool LuaStateManager::ExecuteFile(LuaPlus::LuaStateOwner &luaState, char const * const pFileName) { const I32 retVal = luaState->DoFile(pFileName); const bool bSucceeded = (0 == retVal); assert(bSucceeded && "Unable to execute Lua script file!"); #if DEBUG if(!bSucceeded) { GF_LOG_TRACE_ERR("LuaStateManager::ExecuteFile()", "Failed to execute: " + std::string(pFileName)); } #endif return (bSucceeded); }
// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// F32 DirectSound8AudioBuffer::VGetProgress() { LPDIRECTSOUNDBUFFER pDSB = static_cast<LPDIRECTSOUNDBUFFER>(VGet()); if(!pDSB) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VGetProgress()", "Failed to cast the buffer to a DirectSound buffer"); return (false); } DWORD progress = 0; pDSB->GetCurrentPosition(&progress, NULL); F32 length = static_cast<F32>(m_Resource->GetPCMBufferSize()); return (static_cast<F32>(progress) / length); }
// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// bool DirectSound8AudioBuffer::VOnRestore() { HRESULT hr; BOOL bRestored; // Restore the buffer if it was lost if(FAILED(hr = RestoreBuffer(&bRestored))) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VOnRestore()", "Failed to restore the buffer"); return (false); } if(bRestored) { // The buffer was restored, so we need to fill it with new data if(FAILED(hr = FillBufferWithSound())) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VOnRestore()", "Failed to fill the buffer with sound"); return (false); } } else { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VOnRestore()", "Failed to restore the buffer so cannot fill it with sound"); } return (true); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool SliderControl::VOnAction() { m_sliderPos = CalculateSliderPositionFromButton(); IEventDataPtr sliderActionEvent(GCC_NEW EvtData_Slider_Action(AbstractWidget::VGetId(), m_eventTypeId, m_sliderPos)); if(!safeQueEvent(sliderActionEvent)) { std::string idStr; try { idStr = boost::lexical_cast<std::string, ScreenElementId>(AbstractWidget::VGetId()); } catch(...) {} GF_LOG_TRACE_ERR("SliderControl::VOnAction()", std::string("Failed to send the EvtData_Slider_Action event for the button ") + idStr); return (false); } return (true); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool TextureManager::LoadCommon2D(const GLenum target, const GLint level, const GLint internalFormat, const GLsizei width, const GLsizei height,\ const GLint border, const GLenum format, const GLenum type, void *data, const bool tightlyPack) { // Ensure the target is a 2D texture. if((target != GL_TEXTURE_2D)\ && (target != GL_TEXTURE_RECTANGLE)\ && (target != GL_TEXTURE_CUBE_MAP_POSITIVE_X)\ && (target != GL_TEXTURE_CUBE_MAP_NEGATIVE_X)\ && (target != GL_TEXTURE_CUBE_MAP_POSITIVE_Y)\ && (target != GL_TEXTURE_CUBE_MAP_NEGATIVE_Y)\ && (target != GL_TEXTURE_CUBE_MAP_POSITIVE_Z)\ && (target != GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)) { GF_LOG_TRACE_ERR("TextureManager::LoadCommon2D()", "Target is not a 2D, RECTANGLE or CubeMap texture"); return (false); } GF_CLEAR_GL_ERROR(); // Check if we should change the GL_UNPACK_ALIGNMENT value. GLint oldUnpackAlignment(0); if(tightlyPack) { glGetIntegerv(GL_UNPACK_ALIGNMENT, &oldUnpackAlignment); if(!GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCommon2D(): ")) return (false); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); if(!GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCommon2D(): ")) return (false); } // Send the texture data to the GPU. glTexImage2D(target, level, internalFormat, width, height, border, format, type, data); bool glError = !GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCommon2D(): "); // Reset the GL_UNPACK_ALIGNMENT value back to the default value after. if(tightlyPack) { glPixelStorei(GL_UNPACK_ALIGNMENT, oldUnpackAlignment); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCommon2D(): "); } // increment the current size of the textures stored in the GPU. if(!glError) m_currSize += width * height; return (!glError); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool ListButtonControl::VOnAction() { std::string text; if(!m_list.empty()) { text.assign(*m_curr); } IEventDataPtr buttonActionEvent(GCC_NEW EvtData_List_Button_Action(AbstractWidget::VGetId(), AbstractButtonControl::GetEventType(), text)); if(!safeQueEvent(buttonActionEvent)) { std::string idStr; try { idStr = boost::lexical_cast<std::string, ScreenElementId>(AbstractWidget::VGetId()); } catch(...) {} GF_LOG_TRACE_ERR("ListButtonControl::ListButtonControl()", std::string("Failed to send the EvtData_List_Button_Action event for the list button ") + idStr); return (false); } return (true); }
// //////////////////////////////////////////////////////////////////// // // //////////////////////////////////////////////////////////////////// bool DirectSound8AudioBuffer::VPlay(const I32 volume, const bool looping) { VStop(); // Set base class attributs. AudioBuffer::VSetVolume(volume); AudioBuffer::VSetLooping(looping); // Get DirectSound buffer. LPDIRECTSOUNDBUFFER pDSB = static_cast<LPDIRECTSOUNDBUFFER>(VGet()); if(!pDSB) { GF_LOG_TRACE_ERR("DirectSound8AudioBuffer::VPlay()", "Failed to cast the buffer to a DirectSound buffer"); return (false); } // Set the buffers volume and play it. pDSB->SetVolume(volume); DWORD dwFlags = looping ? DSBPLAY_LOOPING : 0L; return (S_OK == pDSB->Play(0, 0, dwFlags)); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// boost::optional<U32> TextureManager::UnloadTexture(ElementMap::iterator &texIter) { boost::optional<U32> bytesFreed; // Number of bytes freed. if(texIter != m_elementsMap.end()) { GF_CLEAR_GL_ERROR(); glDeleteTextures(1, &((*texIter).second).m_glTexId); if(!GF_CHECK_GL_ERROR_TRC("TextureManager::UnloadTexture(): ")) { GF_LOG_TRACE_ERR("TextureManager::UnloadTexture()", std::string("An OpenGL error occurred freeing the texture data for the image ") + ((*texIter).second).m_filename); return (bytesFreed); } bytesFreed = ((*texIter).second).m_width * ((*texIter).second).m_height; m_currSize -= *bytesFreed; m_elementsMap.erase(texIter); bool found(false); for(std::vector<GLuint>::iterator i = m_glIdVec.begin(); ((!found) && (i != m_glIdVec.end())); ) { if((*i) == ((*texIter).second).m_glTexId) { i = m_glIdVec.erase(i); found = true; } else { ++i; } } if(found) { --m_usedTextureCount; } } // Success! return (bytesFreed); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// void ListButtonControl::SetLuaTextList(const LuaPlus::LuaObject &table) { bool error = true; if(table.IsTable()) { I64 size = table.GetTableCount(); // LUA table indices start at 1!! for(I64 i = 1; i <= size; ++i) { LuaPlus::LuaObject currTable = table[i]; if(currTable.IsString()) { error = false; m_list.push_back(std::string(currTable.GetString())); } } } #if DEBUG if(error) { GF_LOG_TRACE_ERR("ListButtonControl::SetLuaTextList()", "No List of text strings got from lua data"); } #endif }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// bool TextureManager::Bind(const TexHandle textureHandle, const GLenum target, const GLint textureLayer) { GF_CLEAR_GL_ERROR(); // Change to a different texture layer if its different to the cached value! if(textureLayer != m_currTexLayer) { glActiveTexture(m_currTexLayer); if(!GF_CHECK_GL_ERROR_TRC("TextureManager::Bind(): ")) { m_currTexLayer = textureLayer; } } // Search for the texture with the handle supplied (also timestamp the tex element!). boost::optional<GLuint> textureId = Find(textureHandle, true); if(!textureId.is_initialized()) { GF_LOG_TRACE_ERR("TextureManager::Bind()", "The texture with the public ID has not been loaded into the TextureManager"); return (false); } // Optimization to stop trying to switch textures when the current GL texture is the one we want. if(m_curBindTex == *textureId) return (true); // Bind to the texture. glBindTexture(target, *textureId); if(!GF_CHECK_GL_ERROR_TRC("TextureManager::Bind(): ")) { return (false); } m_curBindTex = *textureId; return (true); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// boost::optional<TexHandle> TextureManager::LoadCubeMap(const std::vector<std::string> &cubeImgVec, const GLenum wrapMode) { boost::optional<TexHandle> tHandle; // The texture handle. const U32 CUBE_SIDES = 6; // Number of sides in a 3D cube. std::string concatStr; // string ID for string lookup map. // Check input parameters if(cubeImgVec.size() != CUBE_SIDES) { GF_LOG_TRACE_ERR("TextureManager::LoadCubeMap()", "Invalid parameters"); return (tHandle); } for(std::vector<std::string>::const_iterator i = cubeImgVec.begin(), end = cubeImgVec.end(); i != end; ++i) { if((*i).empty()) { GF_LOG_TRACE_ERR("TextureManager::LoadCubeMap()", "Invalid parameters"); return (tHandle); } else { // Create string ID for lookup map. concatStr += *i; } } // Check if we have already loaded a texture with this texture name. tHandle = Find(concatStr); if(tHandle.is_initialized()) { // We loaded this texture already! So we will simply return the textures outside/public ID. return (tHandle); } GLenum cubeEnum[CUBE_SIDES] = { GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z }; // Ensure we have generated enough texture objects. if(m_usedTextureCount >= m_glIdVec.size()) { ResizeTextureVector(); } GF_CLEAR_GL_ERROR(); // Bind to the next available texture object. glBindTexture(GL_TEXTURE_CUBE_MAP, m_glIdVec[m_usedTextureCount]); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCubeMap(): "); // Set the textures filtering and wrap mode. glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, m_currMinFilter); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCubeMap(): "); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, m_currMagFilter); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCubeMap(): "); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, wrapMode); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCubeMap(): "); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, wrapMode); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCubeMap(): "); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, wrapMode); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCubeMap(): "); U32 totalSize(0); boost::shared_ptr<ImageResHandle> imgResArr[6]; // Initialize cubemap images and calculate total size required. for(U32 i = 0; i < CUBE_SIDES; ++i) { // Load the texture from the resource cache and initialize its data. ImageResource imgRes(cubeImgVec[i]); imgResArr[i] = boost::static_pointer_cast<ImageResHandle>(g_appPtr->GetResourceCache()->GetHandle(&imgRes)); if(!imgResArr[i] || !imgResArr[i]->VInitialize()) { GF_LOG_TRACE_ERR("TextureManager::LoadCubeMap()", std::string("Failed to retrieve and/or initialize the resource ") + cubeImgVec[i]); return (tHandle); } totalSize += imgResArr[i]->GetImageWidth() * imgResArr[i]->GetImageHeight(); } // Check if we have to swap out textures. if(m_maxSize != 0) { if(totalSize > m_maxSize) { GF_LOG_TRACE_ERR("TextureManager::LoadCubeMap()", "Cannot load image! It is bigger than the entire size of the TextureManagers memory budget!"); return (tHandle); } while(totalSize + m_currSize > m_maxSize) { UnloadLRUTexture(); } } // Load in the cubemap textures. for(U32 i = 0; i < CUBE_SIDES; ++i) { bool pack = (FindImageTypeFromFile(cubeImgVec[i]) == IMAGE_TYPE_TGA); if(!LoadCommon2D(cubeEnum[i], 0, imgResArr[i]->GetImageComponents(), imgResArr[i]->GetImageWidth(), imgResArr[i]->GetImageHeight(),\ 0, imgResArr[i]->GetImageFormat(), GL_UNSIGNED_BYTE, const_cast<GLbyte *>(imgResArr[i]->GetImageBuffer()), pack)) { return (tHandle); } } // Should we ask OpenGL to generate mipmaps for us? if(m_currMinFilter >= eBasicMipMap) { glGenerateMipmap(GL_TEXTURE_CUBE_MAP); if(!GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCubeMap(): ")) return (tHandle); } // Set the public texture ID. tHandle = m_usedTextureCount; GLint unpackAlignment(1); if(FindImageTypeFromFile(cubeImgVec[0]) != IMAGE_TYPE_TGA) { glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpackAlignment); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadCubeMap(): "); } // Fill out the data struct for the texture we have loaded onto the GPU. TextureElement newTexElement; newTexElement.m_id = *tHandle; newTexElement.m_filename.assign(concatStr); newTexElement.m_timestamp = static_cast<F32>(g_appPtr->GetCurrTime()); newTexElement.m_glTexId = m_glIdVec[*tHandle]; newTexElement.m_minFilter = m_currMinFilter; newTexElement.m_magFilter = m_currMagFilter; newTexElement.m_wrapMode = wrapMode; newTexElement.m_glTarget = GL_TEXTURE_CUBE_MAP; newTexElement.m_width = imgResArr[0]->GetImageWidth(); newTexElement.m_height = imgResArr[0]->GetImageHeight(); newTexElement.m_imgFormat = imgResArr[0]->GetImageFormat(); newTexElement.m_imgType = GL_UNSIGNED_BYTE; newTexElement.m_unpackAlignment = unpackAlignment; // Append the struct to the map. m_elementsMap[*tHandle] = newTexElement; ++m_usedTextureCount; return (tHandle); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// void LuaStateManager::Shutdown() { // Write out the various player options so they are available to the lua UI setup scripts. LuaPlus::LuaObject luaPlayerOptions = m_GlobalState->GetGlobal("INIT_PLAYER_OPTIONS"); boost::shared_ptr<GameOptions> goPtr = g_appPtr->GetGameOptions(); if(!goPtr) { // We must have a valid options pointer to proceed! return; } // Sound options. std::string optionName("MasterVolume"); if(luaPlayerOptions[optionName.c_str()].IsNumber()) { if(!SetAndConvertOption<F32>(goPtr, optionName, GameOptions::PLAYER, luaPlayerOptions[optionName.c_str()].GetFloat())) { GF_LOG_TRACE_ERR("LuaStateManager::Shutdown()", std::string("Failed to save (to the GameOptions file) the lua option: ") + optionName); } } optionName.assign(std::string("Music")); if(luaPlayerOptions[optionName.c_str()].IsBoolean()) { if(!SetAndConvertOption<bool>(goPtr, optionName, GameOptions::PLAYER, luaPlayerOptions[optionName.c_str()].GetBoolean())) { GF_LOG_TRACE_ERR("LuaStateManager::Shutdown()", std::string("Failed to save (to the GameOptions file) the lua option: ") + optionName); } } optionName.assign(std::string("SoundFx")); if(luaPlayerOptions[optionName.c_str()].IsBoolean()) { if(!SetAndConvertOption<bool>(goPtr, optionName, GameOptions::PLAYER, luaPlayerOptions[optionName.c_str()].GetBoolean())) { GF_LOG_TRACE_ERR("LuaStateManager::Shutdown()", std::string("Failed to save (to the GameOptions file) the lua option: ") + optionName); } } // Graphics options. optionName.assign(std::string("RenderShadows")); if(luaPlayerOptions[optionName.c_str()].IsBoolean()) { if(!SetAndConvertOption<bool>(goPtr, optionName, GameOptions::PLAYER, luaPlayerOptions[optionName.c_str()].GetBoolean())) { GF_LOG_TRACE_ERR("LuaStateManager::Shutdown()", std::string("Failed to save (to the GameOptions file) the lua option: ") + optionName); } } optionName.assign(std::string("Multisampling")); if(luaPlayerOptions[optionName.c_str()].IsString()) { std::string value(luaPlayerOptions[optionName.c_str()].GetString()); if(boost::algorithm::istarts_with(value, std::string("x"))) { value.assign(value.substr(1)); } if(!SetAndConvertOption<std::string>(goPtr, optionName, GameOptions::PLAYER, value)) { GF_LOG_TRACE_ERR("LuaStateManager::Shutdown()", std::string("Failed to save (to the GameOptions file) the lua option: ") + optionName); } } optionName.assign(std::string("TextureFilteringType")); if(luaPlayerOptions[optionName.c_str()].IsString()) { if(!SetAndConvertOption<std::string>(goPtr, optionName, GameOptions::PLAYER, luaPlayerOptions[optionName.c_str()].GetString())) { GF_LOG_TRACE_ERR("LuaStateManager::Shutdown()", std::string("Failed to save (to the GameOptions file) the lua option: ") + optionName); } } if(luaPlayerOptions["ScreenResolution"].IsString()) { std::string resolutionStr(luaPlayerOptions["ScreenResolution"].GetString()); std::vector<std::string> tokens; boost::algorithm::split(tokens, resolutionStr, boost::algorithm::is_any_of(std::string("*"))); if(tokens.size() == 2 && boost::algorithm::all(tokens[0], boost::algorithm::is_digit()) && boost::algorithm::all(tokens[1], boost::algorithm::is_digit())) { optionName.assign(std::string("ScreenWidth")); if(!SetAndConvertOption<std::string>(goPtr, optionName, GameOptions::PLAYER, tokens[0])) { GF_LOG_TRACE_ERR("LuaStateManager::Shutdown()", std::string("Failed to save (to the GameOptions file) the lua option: ") + optionName); } else { optionName.assign(std::string("ScreenHeight")); if(!SetAndConvertOption<std::string>(goPtr, optionName, GameOptions::PLAYER, tokens[1])) { GF_LOG_TRACE_ERR("LuaStateManager::Shutdown()", std::string("Failed to save (to the GameOptions file) the lua option: ") + optionName); } } } } goPtr->Commit(); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// boost::optional<TexHandle> TextureManager::LoadRectangle(const std::string &imgnameRef, U32 &wRef, U32 &hRef, const GLenum wrapMode) { boost::optional<TexHandle> tHandle; // The texture handle. // Check input parameters if(imgnameRef.empty() || wrapMode == GL_REPEAT) { GF_LOG_TRACE_ERR("TextureManager::LoadRectangle()", "Invalid parameters"); return (tHandle); } // Check if we have already loaded a texture with this texture name. tHandle = Find(imgnameRef); if(tHandle.is_initialized()) { // We loaded this texture already! So we will simply return the textures outside/public ID. return (tHandle); } // Load the texture from the resource cache and initialize its data. ImageResource imgRes(imgnameRef); boost::shared_ptr<ImageResHandle> imgResHandle = boost::static_pointer_cast<ImageResHandle>(g_appPtr->GetResourceCache()->GetHandle(&imgRes)); if(!imgResHandle || !imgResHandle->VInitialize()) { GF_LOG_TRACE_ERR("TextureManager::LoadRectangle()", std::string("Failed to retrieve and/or initialize the resource ") + imgnameRef); return (tHandle); } wRef = imgResHandle->GetImageWidth(); hRef = imgResHandle->GetImageHeight(); // Check if we need to swap out old textures to make room. if(m_maxSize != 0) { U32 imgsize(wRef * hRef); if(imgsize > m_maxSize) { GF_LOG_TRACE_ERR("TextureManager::LoadRectangle()", "Cannot load image! It is bigger than the entire size of the TextureManagers memory budget!"); return (tHandle); } while(imgsize + m_currSize > m_maxSize) { UnloadLRUTexture(); } } // Ensure we have generated enough texture objects. if(m_usedTextureCount >= m_glIdVec.size()) { ResizeTextureVector(); } GF_CLEAR_GL_ERROR(); // Bind to the next available texture object. glBindTexture(GL_TEXTURE_RECTANGLE, m_glIdVec[m_usedTextureCount]); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadRectangle(): "); bool pack = (FindImageTypeFromFile(imgnameRef) == IMAGE_TYPE_TGA); // Set the textures filtering and wrap mode (basic filtering only for rectangle textures and no mipmaps). glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadRectangle(): "); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadRectangle(): "); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, wrapMode); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadRectangle(): "); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, wrapMode); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadRectangle(): "); if(!LoadCommon2D(GL_TEXTURE_RECTANGLE, 0, imgResHandle->GetImageComponents(), wRef, hRef,\ 0, imgResHandle->GetImageFormat(), GL_UNSIGNED_BYTE, const_cast<GLbyte *>(imgResHandle->GetImageBuffer()), pack)) { return (tHandle); } // Set the public texture ID. tHandle = m_usedTextureCount; GLint unpackAlignment(1); if(!pack) { glGetIntegerv(GL_UNPACK_ALIGNMENT, &unpackAlignment); GF_CHECK_GL_ERROR_TRC("TextureManager::LoadRectangle(): "); } // Fill out the data struct for the texture we have loaded onto the GPU. TextureElement newTexElement; newTexElement.m_id = *tHandle; newTexElement.m_filename.assign(imgnameRef); newTexElement.m_timestamp = static_cast<F32>(g_appPtr->GetCurrTime()); newTexElement.m_glTexId = m_glIdVec[*tHandle]; newTexElement.m_minFilter = GL_NEAREST; newTexElement.m_magFilter = GL_NEAREST; newTexElement.m_wrapMode = wrapMode; newTexElement.m_glTarget = GL_TEXTURE_RECTANGLE; newTexElement.m_width = wRef; newTexElement.m_height = hRef; newTexElement.m_imgFormat = imgResHandle->GetImageFormat(); newTexElement.m_imgType = GL_UNSIGNED_BYTE; newTexElement.m_unpackAlignment = unpackAlignment; // Append the struct to the map. m_elementsMap[*tHandle] = newTexElement; ++m_usedTextureCount; return (tHandle); }
// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// boost::optional<TexHandle> TextureManager::Load1D(const std::string &imgnameRef, GLubyte **textureData, const U32 w, const GLenum wrapMode) { boost::optional<TexHandle> tHandle; // The texture handle. // Check input parameters if((textureData == NULL) || (w == 0) || (imgnameRef.empty())) { GF_LOG_TRACE_ERR("TextureManager::Load1D()", "Invalid parameters"); return (tHandle); } // Check if we have already loaded a texture with this texture name. tHandle = Find(imgnameRef); if(tHandle.is_initialized()) { // We loaded this texture already! So we will simply return the textures outside/public ID. return (tHandle); } // Check if we need to swap out textures from memory. if(m_maxSize != 0) { if(w > m_maxSize) { GF_LOG_TRACE_ERR("TextureManager::Load1D()", "Cannot load image! It is bigger than the entire size of the TextureManagers memory budget!"); return (tHandle); } while(w + m_currSize > m_maxSize) { UnloadLRUTexture(); } } // Ensure we have generated enough texture objects. if(m_usedTextureCount >= m_glIdVec.size()) { ResizeTextureVector(); } GF_CLEAR_GL_ERROR(); // Bind to the next available texture object. glBindTexture(GL_TEXTURE_1D, m_glIdVec[m_usedTextureCount]); GF_CHECK_GL_ERROR_TRC("TextureManager::Load1D(): "); // Set the textures filtering and wrap mode. glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, m_currMinFilter); GF_CHECK_GL_ERROR_TRC("TextureManager::Load1D(): "); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, m_currMagFilter); GF_CHECK_GL_ERROR_TRC("TextureManager::Load1D(): "); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, wrapMode); GF_CHECK_GL_ERROR_TRC("TextureManager::Load1D(): "); // Send the texture data to the GPU (using set format values). glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, w, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData); if(!GF_CHECK_GL_ERROR_TRC("TextureManager::Load1D(): ")) { return (tHandle); } // Should we ask OpenGL to generate mipmaps for us? if(m_currTexFilterMode >= eBasicMipMap) { glGenerateMipmap(GL_TEXTURE_1D); if(!GF_CHECK_GL_ERROR_TRC("TextureManager::Load1D(): ")) { return (tHandle); } } // increment the current size of the textures stored in the GPU. m_currSize += w; // Set the public texture ID. tHandle = m_usedTextureCount; // Fill out the data struct for the texture we have loaded onto the GPU. TextureElement newTexElement; newTexElement.m_id = *tHandle; newTexElement.m_filename.assign(imgnameRef); newTexElement.m_timestamp = static_cast<F32>(g_appPtr->GetCurrTime()); newTexElement.m_glTexId = m_glIdVec[*tHandle]; newTexElement.m_minFilter = m_currMinFilter; newTexElement.m_magFilter = m_currMagFilter; newTexElement.m_wrapMode = wrapMode; newTexElement.m_glTarget = GL_TEXTURE_1D; newTexElement.m_width = w; newTexElement.m_height = 0; newTexElement.m_imgFormat = GL_RGB; newTexElement.m_imgType = GL_UNSIGNED_BYTE; newTexElement.m_unpackAlignment = -1; // Append the struct to the map. m_elementsMap[*tHandle] = newTexElement; ++m_usedTextureCount; return (tHandle); }