GLFW & AppCore

This commit is contained in:
James Turk 2005-02-27 07:43:37 +00:00
parent 84143149dd
commit 7b74df2d18
6 changed files with 437 additions and 78 deletions

136
include/AppCore.hpp Normal file
View File

@ -0,0 +1,136 @@
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: AppCore.hpp,v 1.1 2005/02/27 07:43:37 cozman Exp $
#ifndef PHOTON_APPCORE_HPP
#define PHOTON_APPCORE_HPP
#include "types.hpp"
#include "glfw/types_glfw.hpp"
#include "util/VersionInfo.hpp"
#include "util/Singleton.hpp"
namespace photon
{
class AppCore : public util::Singleton<AppCore>
{
// Group: Video
public:
// Function: createDisplay
// This function attempts to create a display with the given parameters.
//
// Parameters:
// width - desired width of display
// height - desired height of display
// redBits - desired bits per pixel for red value
// greenBits - desired bits per pixel for green value
// blueBits - desired bits per pixel for blue value
// alphaBits - desired bits per pixel for alpha value
// depthBits - desired bitdepth of depth buffer
// stencilBits - desired bitdepth of stencil buffer
// fullscreen - true: fullscreen, false: windowed
// title - title of application
void createDisplay(uint width, uint height,
uint redBits, uint greenBits, uint blueBits,
uint alphaBits, uint depthBits, uint stencilBits,
bool fullscreen, const std::string &title);
// Function: createDisplay
// This function attempts to create a display with the given parameters.
//
// Parameters:
// width - desired width of display
// height - desired height of display
// bpp - desired bits per pixel (aka bitdepth) of display
// depthBits - desired bitdepth of depth buffer
// stencilBits - desired bitdepth of stencil buffer
// fullscreen - true: fullscreen, false: windowed
// title - title of application
void createDisplay(uint width, uint height, uint bpp,
uint depthBits, uint stencilBits, bool fullscreen,
const std::string &title);
// Function: updateDisplay
// Updates the display, usually involves flipping the front/back buffers.
void updateDisplay();
// Group: Input
public:
// Function: keyPressed
// Check if a given key is currently pressed.
//
// Parameters:
// key - <KeyCode> of key to determine status of.
//
// Returns:
// true: key is pressed, false: key isn't pressed
bool keyPressed(KeyCode key);
// Function: mouseButtonPressed
// Check if a given mouse button is currently pressed.
//
// Parameters:
// button - <MouseButton> to determine status of.
//
// Returns:
// true: button is pressed, false: button isn't pressed
bool mouseButtonPressed(MouseButton button);
// Function: getMouseX
// Gets current x location of mouse with respect to screen coordinates.
//
// Returns:
// Mouse x-coordinate, with respect to screen coordinates.
int getMouseX();
// Function: getMouseY
// Gets current y location of mouse with respect to screen coordinates.
//
// Returns:
// Mouse y-coordinate, with respect to screen coordinates.
int getMouseY();
// Function: getMouseWheelPos
// Gets current location of mouse wheel, treated as if wheel describes a
// third axis of movement for the mouse.
//
// Returns:
// Mouse wheel position, zero assumed to be starting position.
int getMouseWheelPos();
// Group: Timing
public:
// Function:
// Get time, in seconds, that application has been running.
//
// Returns:
// Time, represented as a floating-point number in seconds, application has
// been running.
scalar getTime();
private:
util::VersionInfo initGLFW();
// Singleton-required code
private:
AppCore();
~AppCore();
friend class util::Singleton<AppCore>;
friend class std::auto_ptr<AppCore>;
};
}
#endif //PHOTON_APPCORE_HPP

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Application.hpp,v 1.3 2005/02/16 06:58:05 cozman Exp $ // $Id: Application.hpp,v 1.4 2005/02/27 07:43:37 cozman Exp $
#ifndef PHOTON_APPLICATION_HPP #ifndef PHOTON_APPLICATION_HPP
#define PHOTON_APPLICATION_HPP #define PHOTON_APPLICATION_HPP
@ -20,7 +20,9 @@ namespace photon
{ {
// Class: Application // Class: Application
// Main class, from which all applications using Photon should be derived. // Abstract main class, all photon applications should derive from API-specific
// implementations of Application.
//
// Derived classes are made entrypoint via <ENTRYPOINT>. // Derived classes are made entrypoint via <ENTRYPOINT>.
class Application class Application
{ {
@ -52,29 +54,16 @@ public:
// Group: API Initialization // Group: API Initialization
private: private:
// Function: ensureVersion
// Checks a version of a library against the required version, throws
// an APIError if the version is not met.
//
// Parameters:
// library - Name of library being initialized.
// version - Version of library being used.
// required - Required version of library.
void ensureVersion(const std::string& library,
const util::VersionInfo& version,
const util::VersionInfo& required);
// Function: initPhysFS // Function: initPhysFS
// Initialize PhysFS for use. // Initialize PhysFS for use.
// //
// Parameters: // Parameters:
// arg0 - Path to application (argv[0]) // arg0 - Path to application (argv[0])
//
// Returns:
// <VersionInfo> with PhysFS version.
util::VersionInfo initPhysFS(const char* arg0); util::VersionInfo initPhysFS(const char* arg0);
// Function: initGLFW
// Initialize GLFW for use.
util::VersionInfo initGLFW();
// Behind the scenes // Behind the scenes
public: public:
// Function: setInitOptions(const char* arg0) // Function: setInitOptions(const char* arg0)
@ -85,6 +74,12 @@ public:
// Data Members // Data Members
private: private:
scalar secPerFrame_;
scalar lastUpdate_;
bool active_;
bool unpauseOnActive_;
bool quitRequested_;
// Variable: photonVer_ // Variable: photonVer_
// Contains version identifier for photon. // Contains version identifier for photon.
util::VersionInfo photonVer_; util::VersionInfo photonVer_;

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: types.hpp,v 1.2 2005/02/16 06:58:05 cozman Exp $ // $Id: types.hpp,v 1.3 2005/02/27 07:43:37 cozman Exp $
#ifndef PHOTON_TYPES_HPP #ifndef PHOTON_TYPES_HPP
#define PHOTON_TYPES_HPP #define PHOTON_TYPES_HPP
@ -21,25 +21,154 @@ namespace photon {
// Group: Types // Group: Types
// Type: ubyte // Type: ubyte
// Unsigned byte, alias for unsigned char. // Unsigned byte, alias for unsigned char.
typedef unsigned char ubyte; typedef unsigned char ubyte;
// Type: uint // Type: uint
// Alias for unsigned integer. // Alias for unsigned integer.
typedef unsigned int uint; typedef unsigned int uint;
// Type: scalar // Type: scalar
// Scalar value, used throughout photon. (double or float) // Scalar value, used throughout photon. (double or float)
typedef double scalar; typedef double scalar;
// Type: StrVec // Type: StrVec
// Typedef for vector of strings, which is used all throughout photon. // Typedef for vector of strings, which is used all throughout photon.
typedef std::vector<std::string> StrVec; typedef std::vector<std::string> StrVec;
// Type: shared_ptr // Type: shared_ptr
// Shared pointer type. (uses the boost implementation) // Shared pointer type. (uses the boost implementation)
using boost::shared_ptr; using boost::shared_ptr;
// Enum: KeyCode
// Enumeration defining keys, used in <AppCore::keyPressed>.
//
// Enums:
// KEY_ESC - Escape key
// KEY_F1 - F1 key
// KEY_F2 - F2 key
// KEY_F3 - F3 key
// KEY_F4 - F4 key
// KEY_F5 - F5 key
// KEY_F6 - F6 key
// KEY_F7 - F7 key
// KEY_F8 - F8 key
// KEY_F9 - F9 key
// KEY_F10 - F10 key
// KEY_F11 - F11 key
// KEY_F12 - F12 key
// KEY_F13 - F13 key
// KEY_F14 - F14 key
// KEY_F15 - F15 key
// KEY_F16 - F16 key
// KEY_F17 - F17 key
// KEY_F18 - F18 key
// KEY_F19 - F19 key
// KEY_F20 - F20 key
// KEY_F21 - F21 key
// KEY_F22 - F22 key
// KEY_F23 - F23 key
// KEY_F24 - F24 key
// KEY_F25 - F25 key
//
// KEY_INSERT - Insert key
// KEY_HOME - Home key
// KEY_PGUP - Page up key
// KEY_DELETE - Delete key
// KEY_END - End key
// KEY_PGDOWN - Page down key
// KEY_UP - Up arrow key
// KEY_LEFT - Left arrow key
// KEY_DOWN - Down arrow key
// KEY_RIGHT - Right arrow key
//
// KEY_TILDE - Tilde key
// KEY_1 - 1 key
// KEY_2 - 2 key
// KEY_3 - 3 key
// KEY_4 - 4 key
// KEY_5 - 5 key
// KEY_6 - 6 key
// KEY_7 - 7 key
// KEY_8 - 8 key
// KEY_9 - 9 key
// KEY_0 - 0 key
// KEY_MINUS - Minus key
// KEY_EQUAL - Equal key
// KEY_BACKSPACE- Backspace key
//
// KEY_TAB - Tab key
// KEY_Q - Q key
// KEY_W - W key
// KEY_E - E key
// KEY_R - R key
// KEY_T - T key
// KEY_Y - Y key
// KEY_U - U key
// KEY_I - I key
// KEY_O - O key
// KEY_P - P key
// KEY_LBRAC - Left bracket [ key
// KEY_RBRAC - Right bracket ] key
// KEY_BKSLASH - Backslash \ key
//
// KEY_A - A key
// KEY_S - S key
// KEY_D - D key
// KEY_F - F key
// KEY_G - G key
// KEY_H - H key
// KEY_J - J key
// KEY_K - K key
// KEY_L - L key
// KEY_COLON - Colon : key
// KEY_QUOTE - Quote " key
// KEY_RETURN - Enter/Return key
//
// KEY_LSHIFT - Left shift key
// KEY_Z - Z key
// KEY_X - X key
// KEY_C - C key
// KEY_V - V key
// KEY_B - B key
// KEY_N - N key
// KEY_M - M key
// KEY_COMMA - Comma , key
// KEY_PERIOD - Period . key
// KEY_SLASH - Slash / key
// KEY_RSHIFT - Right shift key
//
// KEY_LCTRL - Left control key
// KEY_LALT - Left alt key
// KEY_SPACE - Space bar key
// KEY_RALT - Right alt key
// KEY_RCTRL - Right control key
//
// KEY_NUM_SLASH - Numpad slash / key
// KEY_NUM_ASTERIX - Numpad asterix * key
// KEY_NUM_MINUS - Numpad minus - key
// KEY_NUM_PLUS - Numpad plus + key
// KEY_NUM_ENTER - Numpad enter key
// KEY_NUM_PERIOD - Numpad period . key
// KEY_NUM_0 - Numpad 0 key
// KEY_NUM_1 - Numpad 1 key
// KEY_NUM_2 - Numpad 2 key
// KEY_NUM_3 - Numpad 3 key
// KEY_NUM_4 - Numpad 4 key
// KEY_NUM_5 - Numpad 5 key
// KEY_NUM_6 - Numpad 6 key
// KEY_NUM_7 - Numpad 7 key
// KEY_NUM_8 - Numpad 8 key
// KEY_NUM_9 - Numpad 9 key
// Enum: KeyCode
// Enumeration defining keys, used in <AppCore::keyPressed>.
//
// MB_LEFT - Left mouse button.
// MB_MIDDLE - Middle mouse button.
// MB_RIGHT - Right mouse button.
} }

View File

@ -5,7 +5,7 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Singleton.hpp,v 1.2 2005/02/27 06:27:57 cozman Exp $ // $Id: Singleton.hpp,v 1.3 2005/02/27 07:43:37 cozman Exp $
#ifndef PHOTON_UTIL_SINGLETON_HPP #ifndef PHOTON_UTIL_SINGLETON_HPP
#define PHOTON_UTIL_SINGLETON_HPP #define PHOTON_UTIL_SINGLETON_HPP
@ -22,11 +22,11 @@ template<class T>
class Singleton : public boost::noncopyable class Singleton : public boost::noncopyable
{ {
public: public:
static void initSingleton(); static void initialize();
static void destroySingleton(); static void destroy();
static T& getSingleton(); static T& getInstance();
protected: protected:
virtual ~Singleton()=0; virtual ~Singleton()=0;
@ -44,7 +44,7 @@ Singleton<T>::~Singleton()
} }
template<class T> template<class T>
void Singleton<T>::initSingleton() void Singleton<T>::initialize()
{ {
assert(instance_.get() == 0); assert(instance_.get() == 0);
@ -52,7 +52,7 @@ void Singleton<T>::initSingleton()
} }
template<class T> template<class T>
void Singleton<T>::destroySingleton() void Singleton<T>::destroy()
{ {
assert(instance_.get() != 0); assert(instance_.get() != 0);
@ -60,7 +60,7 @@ void Singleton<T>::destroySingleton()
} }
template<class T> template<class T>
T& Singleton<T>::getSingleton() T& Singleton<T>::getInstance()
{ {
assert(instance_.get() != 0); assert(instance_.get() != 0);

125
src/AppCore.cpp Normal file
View File

@ -0,0 +1,125 @@
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
// $Id: AppCore.cpp,v 1.1 2005/02/27 07:43:37 cozman Exp $
#include "AppCore.hpp"
#include "glfw.h" //This file depends on glfw
#include "exceptions.hpp"
namespace photon
{
void AppCore::createDisplay(uint width, uint height,
uint redBits, uint greenBits, uint blueBits,
uint alphaBits, uint depthBits, uint stencilBits,
bool fullscreen, const std::string &title)
{
GLboolean status;
status = glfwOpenWindow(width, height, redBits, greenBits,
blueBits, alphaBits, depthBits, stencilBits,
fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW);
if(status == GL_FALSE)
{
throw APIError("Failed to create display.");
}
glfwSetWindowTitle(title.c_str()); // title is set separately
}
void AppCore::createDisplay(uint width, uint height, uint bpp,
uint depthBits, uint stencilBits, bool fullscreen,
const std::string &title)
{
// call main version of createDisplay with individual values for rgba bits
switch(depthBits)
{
case 8:
createDisplay(width, height, 3, 3, 2, 0, depthBits, stencilBits,
fullscreen, title);
break;
case 16:
createDisplay(width, height, 5, 6, 5, 0, depthBits, stencilBits,
fullscreen, title);
break;
case 24:
createDisplay(width, height, 8, 8, 8, 0, depthBits, stencilBits,
fullscreen, title);
break;
case 32:
createDisplay(width, height, 8, 8, 8, 8, depthBits, stencilBits,
fullscreen, title);
break;
}
}
void AppCore::updateDisplay()
{
glfwSwapBuffers();
}
bool AppCore::keyPressed(KeyCode key)
{
return glfwGetKey(key) == GLFW_PRESS;
}
bool AppCore::mouseButtonPressed(MouseButton button)
{
return glfwGetMouseButton(button) == GLFW_PRESS;
}
int AppCore::getMouseX()
{
int x;
glfwGetMousePos(&x,0); //only get x
return x;
}
int AppCore::getMouseY()
{
int y;
glfwGetMousePos(0,&y); //only get y
return y;
}
int AppCore::getMouseWheelPos()
{
return glfwGetMouseWheel();
}
scalar AppCore::getTime()
{
return glfwGetTime();
}
util::VersionInfo AppCore::initGLFW()
{
int maj,min,patch;
if(glfwInit() == GL_FALSE)
{
throw APIError("Initialization of GLFW failed!");
}
glfwGetVersion(&maj,&min,&patch);
return util::VersionInfo(maj,min,patch);
}
AppCore::AppCore()
{
util::VersionInfo glfwReq(2,4,2); // requires GLFW 2.4.2
util::ensureVersion("GLFW", initGLFW(), glfwReq);
}
AppCore::~AppCore()
{
glfwCloseWindow(); //close GLFW window
glfwTerminate(); //shutdown GLFW
}
}

View File

@ -5,14 +5,13 @@
// James Turk (jpt2433@rit.edu) // James Turk (jpt2433@rit.edu)
// //
// Version: // Version:
// $Id: Application.cpp,v 1.4 2005/02/16 06:58:26 cozman Exp $ // $Id: Application.cpp,v 1.5 2005/02/27 07:43:37 cozman Exp $
#include "Application.hpp" #include "Application.hpp"
#include "exceptions.hpp" #include "exceptions.hpp"
#include "physfs.h" #include "physfs.h"
#include "glfw.h"
#include <sstream> #include <sstream>
@ -23,30 +22,13 @@ Application::Application() :
photonVer_(0,0,1) // this is the current version photonVer_(0,0,1) // this is the current version
{ {
util::VersionInfo physfsReq(1,0,0); // requires PhysFS 1.0.0 util::VersionInfo physfsReq(1,0,0); // requires PhysFS 1.0.0
util::VersionInfo glfwReq(2,4,2); // requires GLFW 2.4.2
ensureVersion("PhysFS", initPhysFS(arg0_.c_str()), physfsReq); util::ensureVersion("PhysFS", initPhysFS(arg0_.c_str()), physfsReq);
ensureVersion("GLFW", initGLFW(), glfwReq);
} }
Application::~Application() Application::~Application()
{ {
PHYSFS_deinit(); //shutdown PhysFS PHYSFS_deinit(); //shutdown PhysFS
glfwTerminate(); //shutdown GLFW
}
void Application::ensureVersion(const std::string& library,
const util::VersionInfo& version,
const util::VersionInfo& required)
{
std::stringstream ss;
if(version < required)
{
ss << library << " version " << required << " required; " <<
version << "used, please update.";
throw APIError(ss.str());
}
} }
util::VersionInfo Application::initPhysFS(const char* arg0) util::VersionInfo Application::initPhysFS(const char* arg0)
@ -58,14 +40,6 @@ util::VersionInfo Application::initPhysFS(const char* arg0)
return util::VersionInfo(ver.major, ver.minor, ver.patch); return util::VersionInfo(ver.major, ver.minor, ver.patch);
} }
util::VersionInfo Application::initGLFW()
{
int maj,min,patch;
glfwInit();
glfwGetVersion(&maj,&min,&patch);
return util::VersionInfo(maj,min,patch);
}
void Application::setInitOptions(const char* appPath) void Application::setInitOptions(const char* appPath)
{ {
arg0_ = appPath; arg0_ = appPath;