2012-04-12 19:43:08 +00:00
|
|
|
//
|
|
|
|
|
// KRContext.cpp
|
|
|
|
|
// KREngine
|
|
|
|
|
//
|
|
|
|
|
// Created by Kearwood Gilbert on 12-04-12.
|
|
|
|
|
// Copyright (c) 2012 Kearwood Software. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
|
|
#include "KRContext.h"
|
2012-08-17 01:04:49 +00:00
|
|
|
#include "KRCamera.h"
|
2012-04-12 19:43:08 +00:00
|
|
|
|
2012-10-19 23:17:43 +00:00
|
|
|
int KRContext::KRENGINE_MAX_VBO_HANDLES;
|
|
|
|
|
int KRContext::KRENGINE_MAX_VBO_MEM;
|
|
|
|
|
int KRContext::KRENGINE_MAX_SHADER_HANDLES;
|
|
|
|
|
int KRContext::KRENGINE_MAX_TEXTURE_HANDLES;
|
|
|
|
|
int KRContext::KRENGINE_MAX_TEXTURE_MEM;
|
|
|
|
|
int KRContext::KRENGINE_TARGET_TEXTURE_MEM_MAX;
|
|
|
|
|
int KRContext::KRENGINE_TARGET_TEXTURE_MEM_MIN;
|
|
|
|
|
int KRContext::KRENGINE_MAX_TEXTURE_DIM;
|
|
|
|
|
int KRContext::KRENGINE_MIN_TEXTURE_DIM;
|
2012-11-17 00:15:52 +00:00
|
|
|
int KRContext::KRENGINE_MAX_TEXTURE_THROUGHPUT;
|
2012-10-19 23:17:43 +00:00
|
|
|
|
2012-11-01 22:16:59 +00:00
|
|
|
const char *KRContext::extension_names[KRENGINE_NUM_EXTENSIONS] = {
|
|
|
|
|
"GL_EXT_texture_storage"
|
|
|
|
|
};
|
|
|
|
|
|
2012-04-13 23:24:07 +00:00
|
|
|
KRContext::KRContext() {
|
2012-09-11 03:06:35 +00:00
|
|
|
m_pBundleManager = new KRBundleManager(*this);
|
|
|
|
|
m_pShaderManager = new KRShaderManager(*this);
|
|
|
|
|
m_pTextureManager = new KRTextureManager(*this);
|
|
|
|
|
m_pMaterialManager = new KRMaterialManager(*this, m_pTextureManager, m_pShaderManager);
|
2013-01-09 22:37:23 +00:00
|
|
|
m_pModelManager = new KRMeshManager(*this);
|
2012-09-11 03:06:35 +00:00
|
|
|
m_pSceneManager = new KRSceneManager(*this);
|
2012-12-01 02:03:18 +00:00
|
|
|
m_pAnimationManager = new KRAnimationManager(*this);
|
2012-12-07 00:20:06 +00:00
|
|
|
m_pAnimationCurveManager = new KRAnimationCurveManager(*this);
|
2013-01-05 04:04:58 +00:00
|
|
|
m_pSoundManager = new KRAudioManager(*this);
|
2013-01-02 22:00:29 +00:00
|
|
|
m_pUnknownManager = new KRUnknownManager(*this);
|
2012-11-01 22:16:59 +00:00
|
|
|
m_bDetectedExtensions = false;
|
2012-11-17 00:15:52 +00:00
|
|
|
m_current_frame = 0;
|
2012-11-23 01:02:22 +00:00
|
|
|
m_absolute_time = 0.0f;
|
2012-04-12 19:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
KRContext::~KRContext() {
|
2012-09-11 03:06:35 +00:00
|
|
|
|
2012-04-12 19:43:08 +00:00
|
|
|
if(m_pSceneManager) {
|
|
|
|
|
delete m_pSceneManager;
|
|
|
|
|
m_pSceneManager = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(m_pModelManager) {
|
|
|
|
|
delete m_pModelManager;
|
|
|
|
|
m_pModelManager = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(m_pTextureManager) {
|
|
|
|
|
delete m_pTextureManager;
|
|
|
|
|
m_pTextureManager = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(m_pMaterialManager) {
|
|
|
|
|
delete m_pMaterialManager;
|
|
|
|
|
m_pMaterialManager = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(m_pShaderManager) {
|
|
|
|
|
delete m_pShaderManager;
|
|
|
|
|
m_pShaderManager = NULL;
|
|
|
|
|
}
|
2012-09-11 03:06:35 +00:00
|
|
|
|
2012-12-01 02:03:18 +00:00
|
|
|
if(m_pAnimationManager) {
|
|
|
|
|
delete m_pAnimationManager;
|
|
|
|
|
m_pAnimationManager = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-07 00:20:06 +00:00
|
|
|
if(m_pAnimationCurveManager) {
|
|
|
|
|
delete m_pAnimationCurveManager;
|
|
|
|
|
m_pAnimationCurveManager = NULL;
|
|
|
|
|
}
|
2013-01-05 04:04:58 +00:00
|
|
|
|
|
|
|
|
if(m_pSoundManager) {
|
|
|
|
|
delete m_pSoundManager;
|
|
|
|
|
m_pSoundManager = NULL;
|
|
|
|
|
}
|
2013-01-02 22:00:29 +00:00
|
|
|
|
|
|
|
|
if(m_pUnknownManager) {
|
|
|
|
|
delete m_pUnknownManager;
|
|
|
|
|
m_pUnknownManager = NULL;
|
|
|
|
|
}
|
2012-12-07 00:20:06 +00:00
|
|
|
|
2012-09-11 03:06:35 +00:00
|
|
|
// The bundles must be destroyed last, as the other objects may be using mmap'ed data from bundles
|
|
|
|
|
if(m_pBundleManager) {
|
|
|
|
|
delete m_pBundleManager;
|
|
|
|
|
m_pBundleManager = NULL;
|
|
|
|
|
}
|
2012-04-12 19:43:08 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-11 03:06:35 +00:00
|
|
|
KRBundleManager *KRContext::getBundleManager() {
|
|
|
|
|
return m_pBundleManager;
|
|
|
|
|
}
|
2012-04-12 19:43:08 +00:00
|
|
|
KRSceneManager *KRContext::getSceneManager() {
|
|
|
|
|
return m_pSceneManager;
|
|
|
|
|
}
|
|
|
|
|
KRTextureManager *KRContext::getTextureManager() {
|
|
|
|
|
return m_pTextureManager;
|
|
|
|
|
}
|
|
|
|
|
KRMaterialManager *KRContext::getMaterialManager() {
|
|
|
|
|
return m_pMaterialManager;
|
|
|
|
|
}
|
|
|
|
|
KRShaderManager *KRContext::getShaderManager() {
|
|
|
|
|
return m_pShaderManager;
|
|
|
|
|
}
|
2013-01-09 22:37:23 +00:00
|
|
|
KRMeshManager *KRContext::getModelManager() {
|
2012-04-12 19:43:08 +00:00
|
|
|
return m_pModelManager;
|
|
|
|
|
}
|
2012-12-01 02:03:18 +00:00
|
|
|
KRAnimationManager *KRContext::getAnimationManager() {
|
|
|
|
|
return m_pAnimationManager;
|
|
|
|
|
}
|
2012-12-07 00:20:06 +00:00
|
|
|
KRAnimationCurveManager *KRContext::getAnimationCurveManager() {
|
|
|
|
|
return m_pAnimationCurveManager;
|
|
|
|
|
}
|
2013-01-05 04:04:58 +00:00
|
|
|
KRAudioManager *KRContext::getAudioManager() {
|
|
|
|
|
return m_pSoundManager;
|
|
|
|
|
}
|
2013-01-02 22:00:29 +00:00
|
|
|
KRUnknownManager *KRContext::getUnknownManager() {
|
|
|
|
|
return m_pUnknownManager;
|
|
|
|
|
}
|
2012-04-12 19:43:08 +00:00
|
|
|
|
2012-09-11 03:06:35 +00:00
|
|
|
void KRContext::loadResource(const std::string &file_name, KRDataBlock *data) {
|
|
|
|
|
std::string name = KRResource::GetFileBase(file_name);
|
|
|
|
|
std::string extension = KRResource::GetFileExtension(file_name);
|
|
|
|
|
|
2012-10-06 01:35:41 +00:00
|
|
|
// fprintf(stderr, "KRContext::loadResource - Loading: %s\n", file_name.c_str());
|
2012-09-11 03:06:35 +00:00
|
|
|
|
|
|
|
|
if(extension.compare("krbundle") == 0) {
|
|
|
|
|
m_pBundleManager->loadBundle(name.c_str(), data);
|
2013-01-09 22:37:23 +00:00
|
|
|
} else if(extension.compare("krmesh") == 0) {
|
2012-09-11 03:06:35 +00:00
|
|
|
m_pModelManager->loadModel(name.c_str(), data);
|
2012-04-12 19:43:08 +00:00
|
|
|
} else if(extension.compare("krscene") == 0) {
|
2012-09-11 03:06:35 +00:00
|
|
|
m_pSceneManager->loadScene(name.c_str(), data);
|
2012-12-07 01:49:17 +00:00
|
|
|
} else if(extension.compare("kranimation") == 0) {
|
|
|
|
|
m_pAnimationManager->loadAnimation(name.c_str(), data);
|
2012-12-07 03:13:10 +00:00
|
|
|
} else if(extension.compare("kranimationcurve") == 0) {
|
2012-12-07 01:49:17 +00:00
|
|
|
m_pAnimationCurveManager->loadAnimationCurve(name.c_str(), data);
|
2012-04-12 19:43:08 +00:00
|
|
|
} else if(extension.compare("pvr") == 0) {
|
2012-10-25 03:15:28 +00:00
|
|
|
m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data);
|
|
|
|
|
} else if(extension.compare("tga") == 0) {
|
|
|
|
|
m_pTextureManager->loadTexture(name.c_str(), extension.c_str(), data);
|
2012-04-13 23:24:07 +00:00
|
|
|
} else if(extension.compare("vsh") == 0) {
|
2012-09-11 03:06:35 +00:00
|
|
|
m_pShaderManager->loadVertexShader(name.c_str(), data);
|
2012-04-13 23:24:07 +00:00
|
|
|
} else if(extension.compare("fsh") == 0) {
|
2012-09-11 03:06:35 +00:00
|
|
|
m_pShaderManager->loadFragmentShader(name.c_str(), data);
|
2012-04-12 19:43:08 +00:00
|
|
|
} else if(extension.compare("mtl") == 0) {
|
2012-09-11 03:06:35 +00:00
|
|
|
m_pMaterialManager->load(name.c_str(), data);
|
2013-01-05 04:04:58 +00:00
|
|
|
} else if(extension.compare("mp3") == 0) {
|
|
|
|
|
m_pSoundManager->load(name.c_str(), extension, data);
|
|
|
|
|
} else if(extension.compare("wav") == 0) {
|
|
|
|
|
m_pSoundManager->load(name.c_str(), extension, data);
|
|
|
|
|
} else if(extension.compare("aac") == 0) {
|
|
|
|
|
m_pSoundManager->load(name.c_str(), extension, data);
|
2012-09-11 03:06:35 +00:00
|
|
|
} else {
|
2013-01-02 22:00:29 +00:00
|
|
|
m_pUnknownManager->load(name, extension, data);
|
2012-09-11 03:06:35 +00:00
|
|
|
delete data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KRContext::loadResource(std::string path) {
|
|
|
|
|
KRDataBlock *data = new KRDataBlock();
|
|
|
|
|
if(data->load(path)) {
|
|
|
|
|
loadResource(path, data);
|
2012-04-12 19:43:08 +00:00
|
|
|
} else {
|
2012-10-06 01:35:41 +00:00
|
|
|
fprintf(stderr, "KRContext::loadResource - Failed to open file: %s\n", path.c_str());
|
2012-09-11 03:06:35 +00:00
|
|
|
delete data;
|
2012-04-12 19:43:08 +00:00
|
|
|
}
|
2012-09-13 20:09:19 +00:00
|
|
|
}
|
|
|
|
|
|
2012-09-26 20:07:48 +00:00
|
|
|
void KRContext::rotateBuffers(bool new_frame) {
|
2012-09-13 20:09:19 +00:00
|
|
|
//fprintf(stderr, "Rotating Buffers...\n");
|
2012-11-17 00:15:52 +00:00
|
|
|
if(!new_frame) GLDEBUG(glFinish());
|
2012-09-13 20:09:19 +00:00
|
|
|
|
2012-09-26 20:07:48 +00:00
|
|
|
m_pModelManager->rotateBuffers(new_frame);
|
2012-11-01 22:16:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void KRContext::detectExtensions() {
|
|
|
|
|
m_bDetectedExtensions = true;
|
|
|
|
|
|
2012-11-17 00:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
2012-12-07 01:49:17 +00:00
|
|
|
void KRContext::startFrame(float deltaTime)
|
2012-11-17 00:15:52 +00:00
|
|
|
{
|
2012-12-07 01:49:17 +00:00
|
|
|
m_pTextureManager->startFrame(deltaTime);
|
|
|
|
|
m_pAnimationManager->startFrame(deltaTime);
|
2012-11-17 00:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-23 01:02:22 +00:00
|
|
|
void KRContext::endFrame(float deltaTime)
|
2012-11-17 00:15:52 +00:00
|
|
|
{
|
2012-12-07 01:49:17 +00:00
|
|
|
m_pTextureManager->endFrame(deltaTime);
|
|
|
|
|
m_pAnimationManager->endFrame(deltaTime);
|
2012-11-17 00:15:52 +00:00
|
|
|
rotateBuffers(true);
|
|
|
|
|
m_current_frame++;
|
2012-11-23 01:02:22 +00:00
|
|
|
m_absolute_time += deltaTime;
|
2012-11-17 00:15:52 +00:00
|
|
|
}
|
|
|
|
|
|
2012-11-23 01:02:22 +00:00
|
|
|
long KRContext::getCurrentFrame() const
|
2012-11-17 00:15:52 +00:00
|
|
|
{
|
|
|
|
|
return m_current_frame;
|
|
|
|
|
}
|
2012-11-23 01:02:22 +00:00
|
|
|
|
|
|
|
|
float KRContext::getAbsoluteTime() const
|
|
|
|
|
{
|
|
|
|
|
return m_absolute_time;
|
|
|
|
|
}
|