implement singleton pattern

This commit is contained in:
Anakin 2016-09-07 12:46:34 +02:00
parent e7d7bec608
commit f824f4eb4c
3 changed files with 29 additions and 26 deletions

View File

@ -8,10 +8,13 @@
class OpenGLController class OpenGLController
{ {
public: public:
OpenGLController(); static OpenGLController& getInstance(int oglMajor = 4, int oglMinor = 5);
OpenGLController(int oglMajor, int oglMinor);
~OpenGLController(); ~OpenGLController();
private:
OpenGLController() {};
OpenGLController(int oglMajor, int oglMinor);
private: private:
int iOglMajorVersion; int iOglMajorVersion;
int iOglMinorVersion; int iOglMinorVersion;
@ -30,7 +33,7 @@ private:
glm::mat4 m4x4Model; glm::mat4 m4x4Model;
glm::mat4 m4x4MVP; glm::mat4 m4x4MVP;
Camera camera; Camera* camera;
Object* object; Object* object;
struct { struct {

View File

@ -8,24 +8,10 @@
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// public constructor/destructor // public constructor/destructor
OpenGLController::OpenGLController() : OpenGLController& OpenGLController::getInstance(int oglMajor, int oglMinor)
iWidth(640),
iHeight(480),
camera(iWidth, iHeight)
{ {
initDefault(); static OpenGLController instace(oglMajor, oglMinor);
processInit(); return instace;
}
OpenGLController::OpenGLController(int oglMajor, int oglMinor) :
iWidth(640),
iHeight(480),
camera(iWidth, iHeight)
{
initDefault();
iOglMajorVersion = oglMajor;
iOglMinorVersion = oglMinor;
processInit();
} }
OpenGLController::~OpenGLController() OpenGLController::~OpenGLController()
@ -35,6 +21,20 @@ OpenGLController::~OpenGLController()
} }
/////////////////////////////////////////////////////////////////////////
// private constructor
OpenGLController::OpenGLController(int oglMajor, int oglMinor) :
iWidth(640),
iHeight(480)
{
camera = new Camera(iWidth, iHeight);
initDefault();
iOglMajorVersion = oglMajor;
iOglMinorVersion = oglMinor;
processInit();
}
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
// private functions // private functions
@ -141,7 +141,7 @@ void OpenGLController::setCallbackFunctions()
glm::mat4 OpenGLController::getMVPMatrix() glm::mat4 OpenGLController::getMVPMatrix()
{ {
return camera.getMatrix() * object->getMatrix(); return camera->getMatrix() * object->getMatrix();
} }
GLFWwindow * OpenGLController::getWindow() const GLFWwindow * OpenGLController::getWindow() const
@ -155,7 +155,7 @@ GLFWwindow * OpenGLController::getWindow() const
void OpenGLController::resize(int width, int height) void OpenGLController::resize(int width, int height)
{ {
camera.setSize(width, height); camera->setSize(width, height);
} }
void OpenGLController::addRotX(float value) void OpenGLController::addRotX(float value)
@ -170,17 +170,17 @@ void OpenGLController::addRotY(float value)
void OpenGLController::addTransX(double value) void OpenGLController::addTransX(double value)
{ {
camera.add2x(value); camera->add2x(value);
} }
void OpenGLController::addTransY(double value) void OpenGLController::addTransY(double value)
{ {
camera.add2y(value); camera->add2y(value);
} }
void OpenGLController::addTransZ(double value) void OpenGLController::addTransZ(double value)
{ {
camera.add2z(value); camera->add2z(value);
} }
void OpenGLController::updateScene() void OpenGLController::updateScene()

View File

@ -6,7 +6,7 @@
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
OpenGLController scene; OpenGLController scene = OpenGLController::getInstance();
do { do {