2013-04-08 14:41:25 +00:00
|
|
|
#include "Window.h"
|
|
|
|
#include <iostream>
|
2013-04-10 17:29:07 +00:00
|
|
|
#include "Renderer.h"
|
|
|
|
#include "AudioManager.h"
|
2013-05-23 09:43:50 +00:00
|
|
|
#include "VolumeControl.h"
|
2013-07-09 10:37:37 +00:00
|
|
|
#include "Log.h"
|
|
|
|
#include "Settings.h"
|
2013-07-17 06:47:02 +00:00
|
|
|
#include <iomanip>
|
2013-04-08 14:41:25 +00:00
|
|
|
|
2013-07-17 06:47:02 +00:00
|
|
|
Window::Window() : mNormalizeNextUpdate(false), mFrameTimeElapsed(0), mFrameCountElapsed(0), mAverageDeltaTime(10)
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
|
|
|
mInputManager = new InputManager(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window()
|
|
|
|
{
|
2013-07-09 10:37:37 +00:00
|
|
|
//delete all our GUIs
|
|
|
|
while(peekGui())
|
|
|
|
delete peekGui();
|
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
delete mInputManager;
|
|
|
|
}
|
|
|
|
|
2013-06-02 15:08:32 +00:00
|
|
|
void Window::pushGui(GuiComponent* gui)
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
|
|
|
mGuiStack.push_back(gui);
|
|
|
|
}
|
|
|
|
|
2013-06-02 15:08:32 +00:00
|
|
|
void Window::removeGui(GuiComponent* gui)
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
2013-06-02 15:08:32 +00:00
|
|
|
for(auto i = mGuiStack.begin(); i != mGuiStack.end(); i++)
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
2013-06-02 15:08:32 +00:00
|
|
|
if(*i == gui)
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
2013-06-02 15:08:32 +00:00
|
|
|
mGuiStack.erase(i);
|
|
|
|
return;
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-02 15:08:32 +00:00
|
|
|
GuiComponent* Window::peekGui()
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
|
|
|
if(mGuiStack.size() == 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return mGuiStack.at(mGuiStack.size() - 1);
|
|
|
|
}
|
|
|
|
|
2013-07-09 10:37:37 +00:00
|
|
|
bool Window::init(unsigned int width, unsigned int height)
|
2013-04-08 17:40:15 +00:00
|
|
|
{
|
2013-07-09 10:37:37 +00:00
|
|
|
if(!Renderer::init(width, height))
|
|
|
|
{
|
|
|
|
LOG(LogError) << "Renderer failed to initialize!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
mInputManager->init();
|
|
|
|
|
2013-07-09 05:44:24 +00:00
|
|
|
mResourceManager.reloadAll();
|
2013-04-10 17:29:07 +00:00
|
|
|
|
2013-07-09 10:37:37 +00:00
|
|
|
//keep a reference to the default fonts, so they don't keep getting destroyed/recreated
|
|
|
|
if(mDefaultFonts.empty())
|
2013-04-08 17:40:15 +00:00
|
|
|
{
|
2013-07-09 10:37:37 +00:00
|
|
|
mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_SMALL));
|
|
|
|
mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_MEDIUM));
|
|
|
|
mDefaultFonts.push_back(Font::get(mResourceManager, Font::getDefaultPath(), FONT_SIZE_LARGE));
|
2013-04-08 17:40:15 +00:00
|
|
|
}
|
2013-07-09 10:37:37 +00:00
|
|
|
|
|
|
|
return true;
|
2013-04-08 17:40:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Window::deinit()
|
|
|
|
{
|
2013-04-10 17:29:07 +00:00
|
|
|
mInputManager->deinit();
|
2013-07-09 05:44:24 +00:00
|
|
|
mResourceManager.unloadAll();
|
2013-04-10 17:29:07 +00:00
|
|
|
Renderer::deinit();
|
2013-04-08 17:40:15 +00:00
|
|
|
}
|
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
void Window::input(InputConfig* config, Input input)
|
|
|
|
{
|
2013-05-23 09:43:50 +00:00
|
|
|
if(config->isMappedTo("mastervolup", input))
|
|
|
|
{
|
|
|
|
VolumeControl::getInstance()->setVolume(VolumeControl::getInstance()->getVolume() + 5);
|
|
|
|
}
|
|
|
|
else if(config->isMappedTo("mastervoldown", input))
|
|
|
|
{
|
|
|
|
VolumeControl::getInstance()->setVolume(VolumeControl::getInstance()->getVolume() - 5);
|
|
|
|
}
|
|
|
|
else if(peekGui())
|
2013-04-08 14:41:25 +00:00
|
|
|
this->peekGui()->input(config, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::update(int deltaTime)
|
|
|
|
{
|
2013-07-17 06:47:02 +00:00
|
|
|
if(mNormalizeNextUpdate)
|
|
|
|
{
|
|
|
|
mNormalizeNextUpdate = false;
|
|
|
|
if(deltaTime > mAverageDeltaTime)
|
|
|
|
deltaTime = mAverageDeltaTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
mFrameTimeElapsed += deltaTime;
|
|
|
|
mFrameCountElapsed++;
|
|
|
|
if(mFrameTimeElapsed > 500)
|
|
|
|
{
|
|
|
|
mAverageDeltaTime = mFrameTimeElapsed / mFrameCountElapsed;
|
|
|
|
|
|
|
|
if(Settings::getInstance()->getBool("DRAWFRAMERATE"))
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << std::fixed << std::setprecision(1) << (1000.0f * (float)mFrameCountElapsed / (float)mFrameTimeElapsed) << "fps, ";
|
|
|
|
ss << std::fixed << std::setprecision(2) << ((float)mFrameTimeElapsed / (float)mFrameCountElapsed) << "ms";
|
|
|
|
mFrameDataString = ss.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
mFrameTimeElapsed = 0;
|
|
|
|
mFrameCountElapsed = 0;
|
|
|
|
}
|
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
if(peekGui())
|
|
|
|
peekGui()->update(deltaTime);
|
|
|
|
}
|
|
|
|
|
2013-07-17 06:47:02 +00:00
|
|
|
void Window::render()
|
|
|
|
{
|
|
|
|
//there's nothing to render, which should pretty much never happen
|
|
|
|
if(mGuiStack.size() == 0)
|
|
|
|
std::cout << "guistack empty\n";
|
|
|
|
|
|
|
|
Eigen::Affine3f trans(Eigen::Affine3f::Identity());
|
|
|
|
|
|
|
|
for(unsigned int i = 0; i < mGuiStack.size(); i++)
|
|
|
|
{
|
|
|
|
mGuiStack.at(i)->render(trans);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(Settings::getInstance()->getBool("DRAWFRAMERATE"))
|
|
|
|
{
|
|
|
|
Renderer::setMatrix(Eigen::Affine3f::Identity());
|
|
|
|
mDefaultFonts.at(1)->drawText(mFrameDataString, Eigen::Vector2f(50, 50), 0xFF00FFFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::normalizeNextUpdate()
|
|
|
|
{
|
|
|
|
mNormalizeNextUpdate = true;
|
|
|
|
}
|
|
|
|
|
2013-04-08 14:41:25 +00:00
|
|
|
InputManager* Window::getInputManager()
|
|
|
|
{
|
|
|
|
return mInputManager;
|
|
|
|
}
|
2013-06-21 16:49:29 +00:00
|
|
|
|
|
|
|
ResourceManager* Window::getResourceManager()
|
|
|
|
{
|
|
|
|
return &mResourceManager;
|
|
|
|
}
|