diff --git a/include/AppCore.hpp b/include/AppCore.hpp new file mode 100644 index 0000000..ba037df --- /dev/null +++ b/include/AppCore.hpp @@ -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 +{ + +// 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 - 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 - 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; + friend class std::auto_ptr; +}; + +} + +#endif //PHOTON_APPCORE_HPP diff --git a/include/Application.hpp b/include/Application.hpp index 50362a9..72353c1 100644 --- a/include/Application.hpp +++ b/include/Application.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // 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 #define PHOTON_APPLICATION_HPP @@ -20,7 +20,9 @@ namespace photon { // 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 . class Application { @@ -49,32 +51,19 @@ public: // See Also: // virtual int main(StrVec args)=0; - + // Group: API Initialization 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 // Initialize PhysFS for use. // // Parameters: // arg0 - Path to application (argv[0]) + // + // Returns: + // with PhysFS version. util::VersionInfo initPhysFS(const char* arg0); - - // Function: initGLFW - // Initialize GLFW for use. - util::VersionInfo initGLFW(); - + // Behind the scenes public: // Function: setInitOptions(const char* arg0) @@ -85,10 +74,16 @@ public: // Data Members private: + scalar secPerFrame_; + scalar lastUpdate_; + bool active_; + bool unpauseOnActive_; + bool quitRequested_; + // Variable: photonVer_ // Contains version identifier for photon. util::VersionInfo photonVer_; - + // Variable: arg0_ // Contains 0th argument from command line, obtained via // and used by PhysFS initialization. diff --git a/include/types.hpp b/include/types.hpp index c97bd0c..4cc9bbc 100644 --- a/include/types.hpp +++ b/include/types.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // 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 #define PHOTON_TYPES_HPP @@ -21,25 +21,154 @@ namespace photon { // Group: Types - // Type: ubyte - // Unsigned byte, alias for unsigned char. - typedef unsigned char ubyte; +// Type: ubyte +// Unsigned byte, alias for unsigned char. +typedef unsigned char ubyte; - // Type: uint - // Alias for unsigned integer. - typedef unsigned int uint; +// Type: uint +// Alias for unsigned integer. +typedef unsigned int uint; - // Type: scalar - // Scalar value, used throughout photon. (double or float) - typedef double scalar; - - // Type: StrVec - // Typedef for vector of strings, which is used all throughout photon. - typedef std::vector StrVec; - - // Type: shared_ptr - // Shared pointer type. (uses the boost implementation) - using boost::shared_ptr; +// Type: scalar +// Scalar value, used throughout photon. (double or float) +typedef double scalar; + +// Type: StrVec +// Typedef for vector of strings, which is used all throughout photon. +typedef std::vector StrVec; + +// Type: shared_ptr +// Shared pointer type. (uses the boost implementation) +using boost::shared_ptr; + + +// Enum: KeyCode +// Enumeration defining keys, used in . +// +// 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 . +// +// MB_LEFT - Left mouse button. +// MB_MIDDLE - Middle mouse button. +// MB_RIGHT - Right mouse button. } diff --git a/include/util/Singleton.hpp b/include/util/Singleton.hpp index 1384d35..c03ee97 100644 --- a/include/util/Singleton.hpp +++ b/include/util/Singleton.hpp @@ -5,7 +5,7 @@ // James Turk (jpt2433@rit.edu) // // 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 #define PHOTON_UTIL_SINGLETON_HPP @@ -22,11 +22,11 @@ template class Singleton : public boost::noncopyable { public: - static void initSingleton(); + static void initialize(); - static void destroySingleton(); + static void destroy(); - static T& getSingleton(); + static T& getInstance(); protected: virtual ~Singleton()=0; @@ -44,26 +44,26 @@ Singleton::~Singleton() } template -void Singleton::initSingleton() +void Singleton::initialize() { assert(instance_.get() == 0); - + instance_ = std::auto_ptr(new T); } template -void Singleton::destroySingleton() +void Singleton::destroy() { assert(instance_.get() != 0); - + instance_.reset(); } template -T& Singleton::getSingleton() +T& Singleton::getInstance() { assert(instance_.get() != 0); - + return *instance_; } diff --git a/src/AppCore.cpp b/src/AppCore.cpp new file mode 100644 index 0000000..2013bf4 --- /dev/null +++ b/src/AppCore.cpp @@ -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 +} + +} diff --git a/src/Application.cpp b/src/Application.cpp index d27af1b..97d1167 100644 --- a/src/Application.cpp +++ b/src/Application.cpp @@ -5,14 +5,13 @@ // James Turk (jpt2433@rit.edu) // // 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 "exceptions.hpp" #include "physfs.h" -#include "glfw.h" #include @@ -23,30 +22,13 @@ Application::Application() : photonVer_(0,0,1) // this is the current version { 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); - ensureVersion("GLFW", initGLFW(), glfwReq); + + util::ensureVersion("PhysFS", initPhysFS(arg0_.c_str()), physfsReq); } Application::~Application() { 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) @@ -58,14 +40,6 @@ util::VersionInfo Application::initPhysFS(const char* arg0) 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) { arg0_ = appPath;