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-11-12 23:28:15 +00:00
|
|
|
#include "views/ViewController.h"
|
2014-01-25 23:34:29 +00:00
|
|
|
#include "components/HelpComponent.h"
|
2014-03-04 22:48:33 +00:00
|
|
|
#include "components/ImageComponent.h"
|
2013-04-08 14:41:25 +00:00
|
|
|
|
2013-08-06 13:15:20 +00:00
|
|
|
Window::Window() : mNormalizeNextUpdate(false), mFrameTimeElapsed(0), mFrameCountElapsed(0), mAverageDeltaTime(10),
|
2013-12-12 19:48:29 +00:00
|
|
|
mAllowSleep(true)
|
2013-04-08 14:41:25 +00:00
|
|
|
{
|
|
|
|
mInputManager = new InputManager(this);
|
2013-11-12 23:28:15 +00:00
|
|
|
mViewController = new ViewController(this);
|
2014-01-25 23:34:29 +00:00
|
|
|
mHelp = new HelpComponent(this);
|
2014-03-04 22:48:33 +00:00
|
|
|
|
|
|
|
mBackgroundOverlay = new ImageComponent(this);
|
|
|
|
mBackgroundOverlay->setImage(":/scroll_gradient.png");
|
|
|
|
|
2013-11-12 23:28:15 +00:00
|
|
|
pushGui(mViewController);
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Window::~Window()
|
|
|
|
{
|
2013-12-12 19:48:29 +00:00
|
|
|
delete mViewController; // this would get deleted down below, but just to be safe, delete it here
|
2014-03-04 22:48:33 +00:00
|
|
|
delete mBackgroundOverlay;
|
2013-12-12 19:48:29 +00:00
|
|
|
|
2013-07-09 10:37:37 +00:00
|
|
|
//delete all our GUIs
|
|
|
|
while(peekGui())
|
|
|
|
delete peekGui();
|
|
|
|
|
2014-01-25 23:34:29 +00:00
|
|
|
delete mHelp;
|
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);
|
2014-01-25 23:34:29 +00:00
|
|
|
setHelpPrompts(gui->getHelpPrompts());
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2014-01-25 23:34:29 +00:00
|
|
|
i = mGuiStack.erase(i);
|
|
|
|
|
|
|
|
if(i == mGuiStack.end() && mGuiStack.size()) // we just popped the stack and the stack is not empty
|
|
|
|
setHelpPrompts(mGuiStack.back()->getHelpPrompts());
|
|
|
|
|
2013-06-02 15:08:32 +00:00
|
|
|
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;
|
|
|
|
|
2014-01-25 23:34:29 +00:00
|
|
|
return mGuiStack.back();
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
|
|
|
|
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-10-04 23:09:54 +00:00
|
|
|
ResourceManager::getInstance()->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-10-04 23:09:54 +00:00
|
|
|
mDefaultFonts.push_back(Font::get(FONT_SIZE_SMALL));
|
|
|
|
mDefaultFonts.push_back(Font::get(FONT_SIZE_MEDIUM));
|
|
|
|
mDefaultFonts.push_back(Font::get(FONT_SIZE_LARGE));
|
2013-04-08 17:40:15 +00:00
|
|
|
}
|
2013-07-09 10:37:37 +00:00
|
|
|
|
2014-03-04 22:48:33 +00:00
|
|
|
mBackgroundOverlay->setResize((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
|
|
|
|
|
2014-01-25 23:34:29 +00:00
|
|
|
// update our help because font sizes probably changed
|
|
|
|
if(peekGui())
|
|
|
|
setHelpPrompts(peekGui()->getHelpPrompts());
|
|
|
|
|
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-10-04 23:09:54 +00:00
|
|
|
ResourceManager::getInstance()->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)
|
|
|
|
{
|
2014-03-25 22:46:58 +00:00
|
|
|
if(config->getDeviceId() == DEVICE_KEYBOARD && input.value && input.id == SDLK_g && SDL_GetModState() & KMOD_LCTRL && Settings::getInstance()->getBool("Debug"))
|
|
|
|
{
|
|
|
|
// toggle debug grid
|
|
|
|
Settings::getInstance()->setBool("DebugGrid", !Settings::getInstance()->getBool("DebugGrid"));
|
|
|
|
}else if(config->isMappedTo("mastervolup", input))
|
2013-05-23 09:43:50 +00:00
|
|
|
{
|
|
|
|
VolumeControl::getInstance()->setVolume(VolumeControl::getInstance()->getVolume() + 5);
|
|
|
|
}
|
|
|
|
else if(config->isMappedTo("mastervoldown", input))
|
|
|
|
{
|
|
|
|
VolumeControl::getInstance()->setVolume(VolumeControl::getInstance()->getVolume() - 5);
|
|
|
|
}
|
2013-11-12 23:28:15 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if(peekGui())
|
|
|
|
this->peekGui()->input(config, input);
|
|
|
|
}
|
2013-04-08 14:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2014-03-08 19:00:18 +00:00
|
|
|
if(Settings::getInstance()->getBool("DrawFramerate"))
|
2013-07-17 06:47:02 +00:00
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2014-03-27 21:47:25 +00:00
|
|
|
|
|
|
|
// fps
|
2013-07-17 06:47:02 +00:00
|
|
|
ss << std::fixed << std::setprecision(1) << (1000.0f * (float)mFrameCountElapsed / (float)mFrameTimeElapsed) << "fps, ";
|
|
|
|
ss << std::fixed << std::setprecision(2) << ((float)mFrameTimeElapsed / (float)mFrameCountElapsed) << "ms";
|
2014-03-27 21:47:25 +00:00
|
|
|
|
|
|
|
// vram
|
|
|
|
float vramUsageMb = (TextureResource::getTotalMemUsage() + Font::getTotalMemUsage()) / 1000.0f / 1000.0f;
|
|
|
|
ss << "\nVRAM: " << vramUsageMb << "mb";
|
|
|
|
|
2013-07-17 06:47:02 +00:00
|
|
|
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()
|
|
|
|
{
|
2013-12-12 19:48:29 +00:00
|
|
|
Eigen::Affine3f transform = Eigen::Affine3f::Identity();
|
2014-03-04 22:48:33 +00:00
|
|
|
|
2014-03-12 23:24:34 +00:00
|
|
|
// draw only bottom and top of GuiStack (if they are different)
|
|
|
|
if(mGuiStack.size())
|
|
|
|
{
|
|
|
|
auto& bottom = mGuiStack.front();
|
|
|
|
auto& top = mGuiStack.back();
|
|
|
|
|
|
|
|
bottom->render(transform);
|
|
|
|
if(bottom != top)
|
|
|
|
{
|
|
|
|
mBackgroundOverlay->render(transform);
|
|
|
|
top->render(transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw everything
|
|
|
|
/*const unsigned int drawBGAfter = mGuiStack.size() > 1 ? mGuiStack.size() - 2 : mGuiStack.size();
|
2013-07-17 06:47:02 +00:00
|
|
|
for(unsigned int i = 0; i < mGuiStack.size(); i++)
|
|
|
|
{
|
2013-12-12 19:48:29 +00:00
|
|
|
mGuiStack.at(i)->render(transform);
|
2014-03-12 23:24:34 +00:00
|
|
|
|
2014-03-04 22:48:33 +00:00
|
|
|
if(i == drawBGAfter)
|
|
|
|
mBackgroundOverlay->render(transform);
|
2014-03-12 23:24:34 +00:00
|
|
|
}*/
|
2013-07-17 06:47:02 +00:00
|
|
|
|
2014-01-25 23:34:29 +00:00
|
|
|
mHelp->render(transform);
|
|
|
|
|
2014-03-08 19:00:18 +00:00
|
|
|
if(Settings::getInstance()->getBool("DrawFramerate"))
|
2013-07-17 06:47:02 +00:00
|
|
|
{
|
|
|
|
Renderer::setMatrix(Eigen::Affine3f::Identity());
|
|
|
|
mDefaultFonts.at(1)->drawText(mFrameDataString, Eigen::Vector2f(50, 50), 0xFF00FFFF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::normalizeNextUpdate()
|
|
|
|
{
|
|
|
|
mNormalizeNextUpdate = true;
|
|
|
|
}
|
|
|
|
|
2013-10-13 21:40:36 +00:00
|
|
|
bool Window::getAllowSleep()
|
|
|
|
{
|
|
|
|
return mAllowSleep;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Window::setAllowSleep(bool sleep)
|
|
|
|
{
|
|
|
|
mAllowSleep = sleep;
|
|
|
|
}
|
2013-12-12 19:48:29 +00:00
|
|
|
|
|
|
|
void Window::renderLoadingScreen()
|
|
|
|
{
|
|
|
|
Renderer::setMatrix(Eigen::Affine3f::Identity());
|
|
|
|
Renderer::drawRect(0, 0, Renderer::getScreenWidth(), Renderer::getScreenHeight(), 0x000000FF);
|
|
|
|
mDefaultFonts.at(2)->drawCenteredText("LOADING", 0, (Renderer::getScreenHeight() - mDefaultFonts.at(2)->getHeight()) / 2.0f , 0xFFFFFFFF);
|
|
|
|
Renderer::swapBuffers();
|
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
|
|
|
|
void Window::setHelpPrompts(const std::vector<HelpPrompt>& prompts)
|
|
|
|
{
|
|
|
|
mHelp->clearPrompts();
|
|
|
|
|
2014-03-24 01:33:27 +00:00
|
|
|
std::vector<HelpPrompt> addPrompts;
|
|
|
|
|
2014-01-25 23:34:29 +00:00
|
|
|
std::map<std::string, bool> seenMap;
|
|
|
|
for(auto it = prompts.begin(); it != prompts.end(); it++)
|
|
|
|
{
|
|
|
|
// only add it if the same icon hasn't already been added
|
|
|
|
if(seenMap.insert(std::make_pair<std::string, bool>(it->first, true)).second)
|
2014-03-24 01:33:27 +00:00
|
|
|
addPrompts.push_back(*it);
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|
2014-03-24 01:33:27 +00:00
|
|
|
|
|
|
|
mHelp->setPrompts(addPrompts);
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|