2005-02-27 07:43:37 +00:00
|
|
|
//This file is part of Photon (http://photon.sourceforge.net)
|
|
|
|
//Copyright (C) 2004-2005 James Turk
|
|
|
|
//
|
|
|
|
// Author:
|
|
|
|
// James Turk (jpt2433@rit.edu)
|
|
|
|
//
|
|
|
|
// Version:
|
2005-06-11 05:28:41 +00:00
|
|
|
// $Id: AppCore.cpp,v 1.7 2005/06/11 05:28:41 cozman Exp $
|
2005-02-27 07:43:37 +00:00
|
|
|
|
|
|
|
#include "AppCore.hpp"
|
|
|
|
|
2005-03-04 13:06:49 +00:00
|
|
|
#include <boost/lexical_cast.hpp>
|
2005-04-21 19:30:19 +00:00
|
|
|
#include "GL/glfw.h" //This file depends on glfw
|
2005-02-27 07:43:37 +00:00
|
|
|
|
2005-03-15 19:21:51 +00:00
|
|
|
#include "Kernel.hpp"
|
2005-02-27 07:43:37 +00:00
|
|
|
#include "exceptions.hpp"
|
2005-06-11 05:28:41 +00:00
|
|
|
#include "video/VideoCore.hpp"
|
2005-02-27 07:43:37 +00:00
|
|
|
|
|
|
|
namespace photon
|
|
|
|
{
|
|
|
|
|
2005-03-15 19:21:51 +00:00
|
|
|
AppCore::AppCore() :
|
|
|
|
dispWidth_(0), dispHeight_(0),
|
|
|
|
task_(new UpdateTask())
|
|
|
|
{
|
|
|
|
util::VersionInfo glfwReq(2,4,2); // requires GLFW 2.4.2
|
|
|
|
util::ensureVersion("GLFW", initGLFW(), glfwReq);
|
|
|
|
|
|
|
|
Kernel::getInstance().addTask(task_);
|
|
|
|
}
|
|
|
|
|
|
|
|
AppCore::~AppCore()
|
|
|
|
{
|
|
|
|
glfwCloseWindow(); //close GLFW window
|
|
|
|
glfwTerminate(); //shutdown GLFW
|
|
|
|
}
|
|
|
|
|
|
|
|
void AppCore::createDisplay(uint width, uint height,
|
|
|
|
uint redBits, uint greenBits, uint blueBits,
|
2005-02-27 07:43:37 +00:00
|
|
|
uint alphaBits, uint depthBits, uint stencilBits,
|
|
|
|
bool fullscreen, const std::string &title)
|
|
|
|
{
|
|
|
|
GLboolean status;
|
2005-03-15 19:21:51 +00:00
|
|
|
status = glfwOpenWindow(width, height, redBits, greenBits,
|
|
|
|
blueBits, alphaBits, depthBits, stencilBits,
|
|
|
|
fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW);
|
|
|
|
if(status == GL_FALSE)
|
2005-02-27 07:43:37 +00:00
|
|
|
{
|
|
|
|
throw APIError("Failed to create display.");
|
|
|
|
}
|
2005-03-15 19:21:51 +00:00
|
|
|
|
2005-03-02 08:40:51 +00:00
|
|
|
dispWidth_ = width;
|
|
|
|
dispHeight_ = height;
|
2005-06-11 05:28:41 +00:00
|
|
|
video::VideoCore::getInstance().setDisplaySize(width,height);
|
2005-03-15 19:21:51 +00:00
|
|
|
|
2005-02-27 07:43:37 +00:00
|
|
|
glfwSetWindowTitle(title.c_str()); // title is set separately
|
|
|
|
}
|
|
|
|
|
2005-03-15 19:21:51 +00:00
|
|
|
void AppCore::createDisplay(uint width, uint height, uint bpp,
|
2005-02-27 07:43:37 +00:00
|
|
|
uint depthBits, uint stencilBits, bool fullscreen,
|
|
|
|
const std::string &title)
|
|
|
|
{
|
|
|
|
// call main version of createDisplay with individual values for rgba bits
|
2005-03-04 13:06:49 +00:00
|
|
|
switch(bpp)
|
2005-02-27 07:43:37 +00:00
|
|
|
{
|
|
|
|
case 8:
|
2005-03-15 19:21:51 +00:00
|
|
|
createDisplay(width, height, 3, 3, 2, 0, depthBits, stencilBits,
|
2005-02-27 07:43:37 +00:00
|
|
|
fullscreen, title);
|
|
|
|
break;
|
|
|
|
case 16:
|
2005-03-15 19:21:51 +00:00
|
|
|
createDisplay(width, height, 5, 6, 5, 0, depthBits, stencilBits,
|
2005-02-27 07:43:37 +00:00
|
|
|
fullscreen, title);
|
|
|
|
break;
|
|
|
|
case 24:
|
2005-03-15 19:21:51 +00:00
|
|
|
createDisplay(width, height, 8, 8, 8, 0, depthBits, stencilBits,
|
2005-02-27 07:43:37 +00:00
|
|
|
fullscreen, title);
|
|
|
|
break;
|
|
|
|
case 32:
|
2005-03-15 19:21:51 +00:00
|
|
|
createDisplay(width, height, 8, 8, 8, 8, depthBits, stencilBits,
|
2005-02-27 07:43:37 +00:00
|
|
|
fullscreen, title);
|
|
|
|
break;
|
2005-03-02 08:40:51 +00:00
|
|
|
default:
|
2005-03-15 19:21:51 +00:00
|
|
|
throw ArgumentException("bpp argument of createDisplay must be "
|
|
|
|
"8,16,24, or 32, passed " +
|
2005-03-04 13:06:49 +00:00
|
|
|
boost::lexical_cast<std::string>(bpp) );
|
2005-02-27 07:43:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AppCore::keyPressed(KeyCode key)
|
|
|
|
{
|
|
|
|
return glfwGetKey(key) == GLFW_PRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AppCore::mouseButtonPressed(MouseButton button)
|
|
|
|
{
|
|
|
|
return glfwGetMouseButton(button) == GLFW_PRESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int AppCore::getMouseX()
|
|
|
|
{
|
2005-03-15 19:21:51 +00:00
|
|
|
return task_->mouseX_;
|
2005-02-27 07:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AppCore::getMouseY()
|
|
|
|
{
|
2005-03-15 19:21:51 +00:00
|
|
|
return task_->mouseY_;
|
2005-02-27 07:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int AppCore::getMouseWheelPos()
|
|
|
|
{
|
|
|
|
return glfwGetMouseWheel();
|
|
|
|
}
|
|
|
|
|
|
|
|
scalar AppCore::getTime()
|
|
|
|
{
|
2005-03-15 19:21:51 +00:00
|
|
|
return glfwGetTime() - task_->pausedTime_;
|
2005-03-01 07:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AppCore::setTitle(const std::string& title)
|
|
|
|
{
|
|
|
|
glfwSetWindowTitle(title.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AppCore::isActive()
|
|
|
|
{
|
2005-03-15 19:21:51 +00:00
|
|
|
return task_->active_;
|
2005-03-01 07:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double AppCore::getElapsedTime()
|
|
|
|
{
|
2005-03-15 19:21:51 +00:00
|
|
|
return task_->secPerFrame_;
|
2005-03-01 07:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
double AppCore::getFramerate()
|
|
|
|
{
|
2005-03-15 19:21:51 +00:00
|
|
|
return 1/task_->secPerFrame_;
|
2005-02-27 07:43:37 +00:00
|
|
|
}
|
|
|
|
|
2005-03-02 08:40:51 +00:00
|
|
|
uint AppCore::getDisplayWidth()
|
|
|
|
{
|
|
|
|
return dispWidth_;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint AppCore::getDisplayHeight()
|
|
|
|
{
|
|
|
|
return dispHeight_;
|
|
|
|
}
|
|
|
|
|
2005-02-27 07:43:37 +00:00
|
|
|
util::VersionInfo AppCore::initGLFW()
|
|
|
|
{
|
|
|
|
int maj,min,patch;
|
2005-03-15 19:21:51 +00:00
|
|
|
if(glfwInit() == GL_FALSE)
|
2005-02-27 07:43:37 +00:00
|
|
|
{
|
|
|
|
throw APIError("Initialization of GLFW failed!");
|
|
|
|
}
|
|
|
|
glfwGetVersion(&maj,&min,&patch);
|
|
|
|
return util::VersionInfo(maj,min,patch);
|
|
|
|
}
|
|
|
|
|
2005-03-15 19:21:51 +00:00
|
|
|
AppCore::UpdateTask::UpdateTask() :
|
|
|
|
Task("AppCore::UpdateTask", PRI_CORE),
|
|
|
|
mouseX_(0), mouseY_(0),
|
|
|
|
active_(false), timerPaused_(false),
|
2005-03-01 07:51:23 +00:00
|
|
|
unpauseOnActive_(false), lastPause_(0), pausedTime_(0),
|
|
|
|
secPerFrame_(0), lastUpdate_(0)
|
2005-02-27 07:43:37 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-03-15 19:21:51 +00:00
|
|
|
void AppCore::UpdateTask::update()
|
2005-02-27 07:43:37 +00:00
|
|
|
{
|
2005-03-15 19:21:51 +00:00
|
|
|
scalar curTime = glfwGetTime() - pausedTime_;
|
|
|
|
|
|
|
|
// update the display here instead of VideoCore (since it belongs to glfw)
|
|
|
|
glfwSwapBuffers();
|
|
|
|
|
|
|
|
// keep track of time between frames
|
|
|
|
secPerFrame_ = curTime-lastUpdate_;
|
|
|
|
lastUpdate_ = curTime;
|
|
|
|
|
|
|
|
// quit on window closing or Alt-F4/Alt-X
|
|
|
|
if(!glfwGetWindowParam(GLFW_OPENED) ||
|
|
|
|
( (glfwGetKey(GLFW_KEY_LALT) || glfwGetKey(GLFW_KEY_RALT)) &&
|
|
|
|
(glfwGetKey(GLFW_KEY_F4) || glfwGetKey('X')) ) )
|
|
|
|
{
|
|
|
|
Kernel::getInstance().killAllTasks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// hold active-state
|
|
|
|
active_ = (glfwGetWindowParam(GLFW_ACTIVE) == GL_TRUE);
|
|
|
|
|
|
|
|
// automatically pause/unpause app timer on focus
|
|
|
|
if(!active_ && !timerPaused_)
|
|
|
|
{
|
|
|
|
timerPaused_ = true;
|
|
|
|
lastPause_ = curTime;
|
|
|
|
unpauseOnActive_ = true;
|
|
|
|
}
|
|
|
|
else if(active_ && unpauseOnActive_)
|
|
|
|
{
|
|
|
|
timerPaused_ = true;
|
|
|
|
pausedTime_ += curTime - lastPause_;
|
|
|
|
unpauseOnActive_ = false;
|
|
|
|
}
|
2005-02-27 07:43:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|