kick
/Users/morten/Programmering/cpp/kick/src/kick/core/engine.cpp
00001 //
00002 //  Engine.cpp
00003 //  KickCPP
00004 //
00005 //  Created by Morten Nobel-Jørgensen on 5/21/13.
00006 //  Copyright (c) 2013 Morten Nobel-Joergensen. All rights reserved.
00007 //
00008 
00009 #include "kick/core/engine.h"
00010 #include "kick/core/time.h"
00011 #include "kick/core/debug.h"
00012 #include "kick/context/sdl2_context.h"
00013 #include <iostream>
00014 
00015 using namespace std;
00016 
00017 namespace kick {
00018 
00019     Engine* Engine::instance = nullptr;
00020 
00021     Engine::Engine(int &argc, char **argv,const WindowConfig& config)
00022     : mContext(new SDL2Context()), tickStartTime{Time::total()} {
00023         instance = this;
00024         mContext->init(argc, argv);
00025         mContext->showWindow(config);
00026         printf("%s (%s)\n",
00027                 glGetString(GL_RENDERER),  // e.g. Intel HD Graphics 3000 OpenGL Engine
00028                 glGetString(GL_VERSION)    // e.g. 3.2  INTEL-8.0.61
00029         );
00030         createScene("defaultScene");
00031         mContext->contextSurfaceSize.registerSyncValue(engineUniforms.viewportDimension);
00032         engineUniforms.viewportDimension.setValue(mContext->getContextSurfaceDim());
00033 #ifdef DEBUG
00034                 printf("Working dir %s\n", Debug::workingDir().c_str());
00035 #endif
00036     }
00037 
00038     void Engine::startMainLoop(){
00039         instance->mContext->mainLoop();
00040     }
00041 
00042     void Engine::update(){
00043         float now = Time::total();
00044         Time::deltaTime = now - instance->tickStartTime;
00045         instance->tickStartTime = now;
00046         Time::frame++;
00047 
00048         instance->eventQueue.run();
00049 
00050         instance->mDefaultKeyHandler.handleKeyPress(instance);
00051         instance->mActiveScene->update();
00052     }
00053     
00054     void Engine::render(){
00055         instance->mActiveScene->render(&instance->engineUniforms);
00056         instance->mContext->swapBuffer();
00057 #ifdef DEBUG
00058         printOpenGLError();
00059 #endif
00060     }
00061     
00062     Scene * Engine::createScene(const std::string & name){
00063         instance->scenes.push_back(Scene{name});
00064         Scene * scene = &(instance->scenes.back());;
00065         if (!instance->mActiveScene){
00066             instance->mActiveScene = scene;
00067         }
00068         return scene;
00069     }
00070     
00071     std::vector<Scene>::const_iterator Engine::begin()  {
00072         return instance->scenes.begin();
00073     }
00074     std::vector<Scene>::const_iterator Engine::end() {
00075         return instance->scenes.end();
00076     }
00077     
00078     Context* Engine::context(){
00079         return instance->mContext;
00080     }
00081 
00082     void Engine::startFrame() {
00083         KeyInput::reset();
00084         MouseInput::reset();
00085         TouchInput::reset();
00086     }
00087 
00088     DefaultKeyHandler &Engine::defaultKeyHandler() {
00089         return instance->mDefaultKeyHandler;
00090     }
00091 
00092     void Engine::init(int &argc, char **argv, WindowConfig const &config) {
00093         cout << "kick "<< headerVersion()<<" (lib "<< libVersion()<<")"<<endl;
00094 
00095         assert(instance == nullptr);
00096         new Engine(argc, argv, config);
00097     }
00098 
00099     EventQueue &Engine::getEventQueue() {
00100         return instance->eventQueue;
00101     }
00102 
00103     std::string Engine::libVersion() {
00104         return headerVersion();
00105     }
00106 }
 All Classes Functions Variables