Changeset 39
- Timestamp:
- 08/03/2007 01:17:37 PM (5 years ago)
- Location:
- Bindings/glfw
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
Bindings/glfw/Binding.cpp
r1 r39 17 17 18 18 namespace nrBinding { 19 namespace glfw{ 20 21 using namespace nrEngine; 22 23 SharedPtr<Binding> Binding::sSingleton; 24 25 //------------------------------------------------------------------------ 26 ScriptFunctionDec(resizeWindow, Binding) 27 { 28 // check if the parameter count is valid 29 if (args.size() <= 2){ 30 return ScriptResult(std::string("resizeWindow(w,h) - wrong parameter count")); 31 } 32 33 // convert the given values 34 int32 width = 0; 35 int32 height = 0; 36 try{ 37 width = boost::lexical_cast<int32>(args[1]); 38 height = boost::lexical_cast<int32>(args[2]); 39 }catch(...){ 40 return ScriptResult(std::string("Given values are not valid!")); 41 } 42 43 // resize window 44 Binding::instance()->setWindow(Binding::instance()->getWindowTitle(), width, height); 45 return ScriptResult(); 46 } 47 48 //------------------------------------------------------------------------ 49 ScriptFunctionDec(setWindowTitle, Binding) 50 { 51 // check if the parameter count is valid 52 if (args.size() <= 1){ 53 return ScriptResult(std::string("setWindowTitle(title) - wrong parameter count")); 54 } 55 56 // combine the values 57 std::string title; 58 for (uint32 i=1; i < args.size(); i++) 59 title += (args[i] + " "); 60 61 // set title 62 Binding::instance()->setWindow(title); 63 return ScriptResult(); 64 } 65 66 //------------------------------------------------------------------------ 67 ScriptFunctionDec(openWindow, Binding) 68 { 69 70 // check if the parameter count is valid 71 if (args.size() <= 3){ 72 return ScriptResult(std::string("openWindow(w,h,full[,bpp,depth,stencil]) - wrong parameter count")); 73 } 74 75 // convert given values 76 int32 w,h; 77 int32 bpp = 32; 78 int32 depth = 16; 79 int32 stencil = 8; 80 bool full = false; 81 82 try{ 83 84 w = boost::lexical_cast<int32>(args[1]); 85 h = boost::lexical_cast<int32>(args[2]); 86 full = boost::lexical_cast<bool>(args[3]); 87 if (args.size() > 4) bpp = boost::lexical_cast<int32>(args[4]); 88 if (args.size() > 5) depth = boost::lexical_cast<int32>(args[5]); 89 if (args.size() > 6) stencil = boost::lexical_cast<int32>(args[6]); 90 91 }catch(...){ 92 return ScriptResult(std::string("Wrong parameters used!")); 93 } 94 95 Binding::instance()->createWindow(w,h,full,bpp,depth,stencil); 96 return ScriptResult(); 97 } 98 99 100 //------------------------------------------------------------------------ 101 Binding* Binding::instance() 102 { 103 if (!valid()){ 104 sSingleton.reset(new Binding()); 105 } 106 return sSingleton.get(); 107 } 108 109 //------------------------------------------------------------------------ 110 void Binding::release() 111 { 112 if (valid()) 113 { 114 sSingleton.reset(); 115 } 116 } 117 118 //-------------------------------------------------------------------------- 119 bool Binding::valid() 120 { 121 return sSingleton != NULL; 122 } 123 124 //---------------------------------------------------------------------- 125 Binding::Binding() 126 { 127 // initialize default variables 128 mName = "glfwBinding"; 129 mFullName = "OpenGL Framework Library Binding v0.1 for nrEngine"; 130 int32 major, minor, rev; 131 glfwGetVersion(&major, &minor, &rev); 132 NR_Log(Log::LOG_PLUGIN, "%s: %s (glfw v%d.%d.%d)",mName.c_str(), mFullName.c_str(), major, minor, rev); 133 bWindowCreated = false; 134 mWindowWidth = mWindowHeight = 0; 135 136 // check whenever engine is valid 137 if (!Engine::valid()) 138 { 139 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: Engine must be initialized before!", mName.c_str()); 140 NR_EXCEPT(ENGINE_ERROR, "Engine must be initialized before creating a binding", "glfw::Binding::Binding()"); 141 } 142 143 // initialize glfw task 144 NR_Log(Log::LOG_PLUGIN, "glfwBinding: Initialize the glfw subsystem (OpenGL Framework)"); 145 try{ 146 Engine* root = Engine::instance(); 147 mTask.reset(new Task(root)); 148 }catch(int i){ 149 if (i == GLFW_CANNOT_INITIALIZE){ 150 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: Initialization of glfw fails! glfwinit() returns GL_FALSE", mName.c_str()); 151 }else if (i == GLFW_TASK_ALREADY_RUNNING) { 152 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: You are trying to start the same plugin twice.", mName.c_str()); 153 } 154 } 155 156 // ok task is created now add it to the kernel 157 if (mTask) 158 { 159 if (Engine::sKernel()->AddTask(mTask, ORDER_LAST) == 0){ 160 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: glfwBinding_Task could not been added to the kernel", mName.c_str()); 161 } 162 } 163 164 // now register some new function by the scripting engine 165 Engine::sScriptEngine()->add("openWindow", openWindow); 166 Engine::sScriptEngine()->add("resizeWindow", resizeWindow); 167 Engine::sScriptEngine()->add("setWindowTitle", setWindowTitle); 168 169 } 170 171 //---------------------------------------------------------------------- 172 Binding::~Binding() 173 { 174 // remove the registered functions 175 Engine::sScriptEngine()->del("openWindow"); 176 Engine::sScriptEngine()->del("resizeWindow"); 177 Engine::sScriptEngine()->del("setWindowTitle"); 178 179 NR_Log(Log::LOG_PLUGIN, "glfwBinding: Terminate the glfw subsystem"); 180 181 // first close the rendering window 182 closeWindow(); 183 184 // remove the task from the system 185 if (Engine::valid()) 186 Engine::sKernel()->RemoveTask(mTask->getTaskID()); 187 188 // close the task 189 mTask.reset(); 190 } 191 192 //---------------------------------------------------------------------- 193 const std::string& Binding::getName() 194 { 195 return mName; 196 } 197 198 //---------------------------------------------------------------------- 199 const std::string& Binding::getFullName() 200 { 201 return mFullName; 202 } 203 204 //---------------------------------------------------------------------- 205 const std::string& Binding::getChannelName() 206 { 207 // cast the task to the glfwTask 208 return Task::getChannelName(); 209 } 210 211 //---------------------------------------------------------------------- 212 bool Binding::createWindow(int32 width, int32 height, bool fullscreen, int32 bpp, int32 depth, int32 stencil) 213 { 214 if (bWindowCreated) { 215 setWindow(mWindowTitle, width, height); 216 return true; 217 } 218 219 // convert given bpp to bits 220 int32 r,g,b,a; 221 if (bpp == 8){ 222 r = 3; g = 2; b = 3; a = 0; 223 }else if (bpp == 16){ 224 r = 5; g = 6; b = 5; a = 0; 225 }else if (bpp == 24){ 226 r = 8; g = 8; b = 8; a = 0; 227 }else if (bpp == 32){ 228 r = 8; g = 8; b = 8; a = 8; 229 }else if (bpp == 0){ 230 r = 0; g = 0; b = 0; a = 0; 231 }else{ 232 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: Could not match given colorbit depth", mName.c_str()); 233 return false; 234 } 235 236 // create a rendering window as requested 237 int32 res = glfwOpenWindow(width, height, r,g,b,a, depth, stencil, fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW); 238 if (res == GL_FALSE){ 239 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: Could not open a rendering window", mName.c_str()); 240 return false; 241 } 242 mWindowWidth = width; 243 mWindowHeight = height; 244 245 // initialize glew, so we get all extensions initialized 246 NR_Log(Log::LOG_PLUGIN, "glfwBinding: Initialize the glew subsystem (OpenGL extension wrapper)"); 247 GLenum err = glewInit(); 248 if (GLEW_OK != err){ 249 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: Initialization of glew failed! glewinit() returns error: %s", mName.c_str(), glewGetErrorString(err)); 250 return false; 251 }else{ 252 NR_Log(Log::LOG_PLUGIN, "glfwBinding: Using GLEW %s\n", glewGetString(GLEW_VERSION)); 253 } 254 255 // now create a callback function, so we get noticed if user close the window 256 glfwSetWindowCloseCallback(Binding::closeWindowCallback); 257 258 // now emit an event on the channel, that we got opened a window 259 SharedPtr<Event> msg(new OnCreateWindowEvent(width, height, fullscreen, bpp, depth, stencil)); 260 Engine::sEventManager()->emit(Binding::getChannelName(), msg); 261 262 // now notice the task, that we have created a window 263 boost::dynamic_pointer_cast<Task, ITask>(mTask)->noticeWindowCreated(); 264 265 // now get current OpenGL version 266 const char* renderer = (const char*)glGetString(GL_RENDERER); 267 const char* vendor = (const char*)glGetString(GL_VENDOR); 268 269 NR_Log(Log::LOG_PLUGIN, "%s: OpenGL-Driver %s %s", mName.c_str(), vendor, renderer); 270 bWindowCreated = true; 271 272 return true; 273 } 274 275 //---------------------------------------------------------------------- 276 void Binding::closeWindow() 277 { 278 // now emit an event on the channel, that we got to close the window 279 SharedPtr<Event> msg(new OnCloseWindowEvent()); 280 Engine::sEventManager()->emit(Binding::getChannelName(), msg); 281 282 glfwCloseWindow(); 283 } 284 285 //---------------------------------------------------------------------- 286 int32 Binding::closeWindowCallback() 287 { 288 // now emit an event on the channel, that we got to close the window 289 SharedPtr<Event> msg(new OnCloseWindowEvent()); 290 Engine::sEventManager()->emit(Binding::getChannelName(), msg); 291 292 return GL_TRUE; 293 } 294 295 //---------------------------------------------------------------------- 296 void Binding::setWindow(const std::string& title, nrEngine::int32 width, nrEngine::int32 height) 297 { 298 mWindowTitle = title; 299 if (width || height ) { 300 glfwSetWindowSize(width, height); 301 mWindowWidth = width; 302 mWindowHeight = height; 303 304 SharedPtr<Event> msg(new OnResizeWindowEvent(width, height)); 305 Engine::sEventManager()->emit(Binding::getChannelName(), msg); 306 } 307 308 glfwSetWindowTitle(title.c_str()); 309 } 310 311 //---------------------------------------------------------------------- 312 void Binding::getMousePos(int& x, int& y) 313 { 314 mTask->getMousePos(x,y); 315 } 316 317 //---------------------------------------------------------------------- 318 void Binding::setMousePos(int x, int y) 319 { 320 mTask->setMousePos(x,y); 321 } 322 323 //---------------------------------------------------------------------- 324 bool Binding::isKeyDown(nrEngine::keyIndex key) const 325 { 326 return mTask->isKeyDown(key); 327 } 328 329 }; 19 namespace glfw{ 20 21 using namespace nrEngine; 22 23 SharedPtr<Binding> Binding::sSingleton; 24 25 //------------------------------------------------------------------------ 26 ScriptFunctionDec(resizeWindow, Binding) 27 { 28 // check if the parameter count is valid 29 if (args.size() <= 2){ 30 return ScriptResult(std::string("resizeWindow(w,h) - wrong parameter count")); 31 } 32 33 // convert the given values 34 int32 width = 0; 35 int32 height = 0; 36 try{ 37 width = boost::lexical_cast<int32>(args[1]); 38 height = boost::lexical_cast<int32>(args[2]); 39 }catch(...){ 40 return ScriptResult(std::string("Given values are not valid!")); 41 } 42 43 // resize window 44 Binding::instance()->setWindow(Binding::instance()->getWindowTitle(), width, height); 45 return ScriptResult(); 46 } 47 48 //------------------------------------------------------------------------ 49 ScriptFunctionDec(setWindowTitle, Binding) 50 { 51 // check if the parameter count is valid 52 if (args.size() <= 1){ 53 return ScriptResult(std::string("setWindowTitle(title) - wrong parameter count")); 54 } 55 56 // combine the values 57 std::string title; 58 for (uint32 i=1; i < args.size(); i++) 59 title += (args[i] + " "); 60 61 // set title 62 Binding::instance()->setWindow(title); 63 return ScriptResult(); 64 } 65 66 //------------------------------------------------------------------------ 67 ScriptFunctionDec(openWindow, Binding) 68 { 69 70 // check if the parameter count is valid 71 if (args.size() <= 3){ 72 return ScriptResult(std::string("openWindow(w,h,full[,bpp,depth,stencil]) - wrong parameter count")); 73 } 74 75 // convert given values 76 int32 w,h; 77 int32 bpp = 32; 78 int32 depth = 16; 79 int32 stencil = 8; 80 bool full = false; 81 82 try{ 83 84 w = boost::lexical_cast<int32>(args[1]); 85 h = boost::lexical_cast<int32>(args[2]); 86 full = boost::lexical_cast<bool>(args[3]); 87 if (args.size() > 4) bpp = boost::lexical_cast<int32>(args[4]); 88 if (args.size() > 5) depth = boost::lexical_cast<int32>(args[5]); 89 if (args.size() > 6) stencil = boost::lexical_cast<int32>(args[6]); 90 91 }catch(...){ 92 return ScriptResult(std::string("Wrong parameters used!")); 93 } 94 95 Binding::instance()->createWindow(w,h,full,bpp,depth,stencil); 96 return ScriptResult(); 97 } 98 99 100 //------------------------------------------------------------------------ 101 Binding* Binding::instance() 102 { 103 if (!valid()){ 104 sSingleton.reset(new Binding()); 105 } 106 return sSingleton.get(); 107 } 108 109 //------------------------------------------------------------------------ 110 void Binding::release() 111 { 112 if (valid()) 113 { 114 sSingleton.reset(); 115 } 116 } 117 118 //-------------------------------------------------------------------------- 119 bool Binding::valid() 120 { 121 return sSingleton != NULL; 122 } 123 124 //---------------------------------------------------------------------- 125 Binding::Binding() 126 { 127 // initialize default variables 128 mName = "glfwBinding"; 129 mFullName = "OpenGL Framework Library Binding v0.1 for nrEngine"; 130 int32 major, minor, rev; 131 glfwGetVersion(&major, &minor, &rev); 132 NR_Log(Log::LOG_PLUGIN, "%s: %s (glfw v%d.%d.%d)",mName.c_str(), mFullName.c_str(), major, minor, rev); 133 bWindowCreated = false; 134 mWindowWidth = mWindowHeight = 0; 135 136 // check whenever engine is valid 137 if (!Engine::valid()) 138 { 139 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: Engine must be initialized before!", mName.c_str()); 140 NR_EXCEPT(ENGINE_ERROR, "Engine must be initialized before creating a binding", "glfw::Binding::Binding()"); 141 } 142 143 // initialize glfw task 144 NR_Log(Log::LOG_PLUGIN, "glfwBinding: Initialize the glfw subsystem (OpenGL Framework)"); 145 try{ 146 Engine* root = Engine::instance(); 147 mTask.reset(new Task(root)); 148 }catch(int i){ 149 if (i == GLFW_CANNOT_INITIALIZE){ 150 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: Initialization of glfw fails! glfwinit() returns GL_FALSE", mName.c_str()); 151 }else if (i == GLFW_TASK_ALREADY_RUNNING) { 152 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: You are trying to start the same plugin twice.", mName.c_str()); 153 } 154 } 155 156 // ok task is created now add it to the kernel 157 if (mTask) 158 { 159 if (Engine::sKernel()->AddTask(mTask, ORDER_LAST) == 0){ 160 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: glfwBinding_Task could not been added to the kernel", mName.c_str()); 161 } 162 } 163 164 // now register some new function by the scripting engine 165 Engine::sScriptEngine()->add("openWindow", openWindow); 166 Engine::sScriptEngine()->add("resizeWindow", resizeWindow); 167 Engine::sScriptEngine()->add("setWindowTitle", setWindowTitle); 168 169 } 170 171 //---------------------------------------------------------------------- 172 Binding::~Binding() 173 { 174 // remove the registered functions 175 Engine::sScriptEngine()->del("openWindow"); 176 Engine::sScriptEngine()->del("resizeWindow"); 177 Engine::sScriptEngine()->del("setWindowTitle"); 178 179 NR_Log(Log::LOG_PLUGIN, "glfwBinding: Terminate the glfw subsystem"); 180 181 // first close the rendering window 182 closeWindow(); 183 184 // remove the task from the system 185 if (Engine::valid()) 186 Engine::sKernel()->RemoveTask(mTask->getTaskID()); 187 188 // close the task 189 mTask.reset(); 190 } 191 192 //---------------------------------------------------------------------- 193 const std::string& Binding::getName() 194 { 195 return mName; 196 } 197 198 //---------------------------------------------------------------------- 199 const std::string& Binding::getFullName() 200 { 201 return mFullName; 202 } 203 204 //---------------------------------------------------------------------- 205 const std::string& Binding::getChannelName() 206 { 207 // cast the task to the glfwTask 208 return Task::getChannelName(); 209 } 210 211 //---------------------------------------------------------------------- 212 bool Binding::createWindow(int32 width, int32 height, bool fullscreen, int32 bpp, int32 depth, int32 stencil) 213 { 214 if (bWindowCreated) { 215 setWindow(mWindowTitle, width, height); 216 return true; 217 } 218 219 // convert given bpp to bits 220 int32 r,g,b,a; 221 if (bpp == 8){ 222 r = 3; g = 2; b = 3; a = 0; 223 }else if (bpp == 16){ 224 r = 5; g = 6; b = 5; a = 0; 225 }else if (bpp == 24){ 226 r = 8; g = 8; b = 8; a = 0; 227 }else if (bpp == 32){ 228 r = 8; g = 8; b = 8; a = 8; 229 }else if (bpp == 0){ 230 r = 0; g = 0; b = 0; a = 0; 231 }else{ 232 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: Could not match given colorbit depth", mName.c_str()); 233 return false; 234 } 235 236 // create a rendering window as requested 237 int32 res = glfwOpenWindow(width, height, r,g,b,a, depth, stencil, fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW); 238 if (res == GL_FALSE){ 239 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: Could not open a rendering window", mName.c_str()); 240 return false; 241 } 242 mWindowWidth = width; 243 mWindowHeight = height; 244 245 // initialize glew, so we get all extensions initialized 246 NR_Log(Log::LOG_PLUGIN, "glfwBinding: Initialize the glew subsystem (OpenGL extension wrapper)"); 247 GLenum err = glewInit(); 248 if (GLEW_OK != err){ 249 NR_Log(Log::LOG_PLUGIN, Log::LL_ERROR, "%s: Initialization of glew failed! glewinit() returns error: %s", mName.c_str(), glewGetErrorString(err)); 250 return false; 251 }else{ 252 NR_Log(Log::LOG_PLUGIN, "glfwBinding: Using GLEW %s\n", glewGetString(GLEW_VERSION)); 253 } 254 255 // now create a callback function, so we get noticed if user close the window 256 glfwSetWindowCloseCallback(Binding::closeWindowCallback); 257 258 // now emit an event on the channel, that we got opened a window 259 SharedPtr<Event> msg(new OnCreateWindowEvent(width, height, fullscreen, bpp, depth, stencil)); 260 Engine::sEventManager()->emit(Binding::getChannelName(), msg); 261 262 // now notice the task, that we have created a window 263 boost::dynamic_pointer_cast<Task, ITask>(mTask)->noticeWindowCreated(); 264 265 // now get current OpenGL version 266 const char* renderer = (const char*)glGetString(GL_RENDERER); 267 const char* vendor = (const char*)glGetString(GL_VENDOR); 268 269 NR_Log(Log::LOG_PLUGIN, "%s: OpenGL-Driver %s %s", mName.c_str(), vendor, renderer); 270 bWindowCreated = true; 271 272 return true; 273 } 274 275 //---------------------------------------------------------------------- 276 void Binding::closeWindow() 277 { 278 // now emit an event on the channel, that we got to close the window 279 SharedPtr<Event> msg(new OnCloseWindowEvent()); 280 Engine::sEventManager()->emit(Binding::getChannelName(), msg); 281 282 glfwCloseWindow(); 283 } 284 285 //---------------------------------------------------------------------- 286 int32 Binding::closeWindowCallback() 287 { 288 // now emit an event on the channel, that we got to close the window 289 SharedPtr<Event> msg(new OnCloseWindowEvent()); 290 Engine::sEventManager()->emit(Binding::getChannelName(), msg); 291 292 return GL_TRUE; 293 } 294 295 //---------------------------------------------------------------------- 296 void Binding::setWindow(const std::string& title, nrEngine::int32 width, nrEngine::int32 height) 297 { 298 mWindowTitle = title; 299 if (width || height ) { 300 glfwSetWindowSize(width, height); 301 mWindowWidth = width; 302 mWindowHeight = height; 303 304 SharedPtr<Event> msg(new OnResizeWindowEvent(width, height)); 305 Engine::sEventManager()->emit(Binding::getChannelName(), msg); 306 } 307 308 glfwSetWindowTitle(title.c_str()); 309 } 310 311 //---------------------------------------------------------------------- 312 void Binding::getMousePos(int& x, int& y) 313 { 314 mTask->getMousePos(x,y); 315 } 316 317 //---------------------------------------------------------------------- 318 void Binding::setMousePos(int x, int y) 319 { 320 mTask->setMousePos(x,y); 321 } 322 323 //---------------------------------------------------------------------- 324 bool Binding::isKeyDown(nrEngine::keyIndex key) const 325 { 326 return mTask->isKeyDown(key); 327 } 328 329 //---------------------------------------------------------------------- 330 void Binding::addKeyDownMonitor(nrEngine::keyIndex key) 331 { 332 mTask->addKeyDownMonitor(key); 333 } 334 335 //---------------------------------------------------------------------- 336 void Binding::delKeyDownMonitor(nrEngine::keyIndex key) 337 { 338 mTask->delKeyDownMonitor(key); 339 } 340 341 342 }; 330 343 }; -
Bindings/glfw/Binding.h
r1 r39 22 22 namespace nrBinding { 23 23 24 namespace glfw {24 namespace glfw { 25 25 26 using namespace nrEngine;27 28 //! Forward declaration29 class Task;26 using namespace nrEngine; 27 28 //! Forward declaration 29 class Task; 30 30 31 //! Main class of the glfw binding32 /**33 * Binding is a main class of the glfw loadtime plugin (or binding).34 * It is declared as a singleton, so it can be created only once.35 * This will initialize the subsysems like glfw framework and36 * event management. So you have to create a instance of this class37 * to provide the user the functionality of this binding.38 **/39 class _NRBindingExport Binding : public nrEngine::Binding{31 //! Main class of the glfw binding 32 /** 33 * Binding is a main class of the glfw loadtime plugin (or binding). 34 * It is declared as a singleton, so it can be created only once. 35 * This will initialize the subsysems like glfw framework and 36 * event management. So you have to create a instance of this class 37 * to provide the user the functionality of this binding. 38 **/ 39 class _NRBindingExport Binding : public nrEngine::Binding{ 40 40 41 public:41 public: 42 42 43 /**44 * Default constructor used, to create instance and45 * to initialize all underlying systems.46 *47 * NOTE: You have to instantiate a class after initialization48 *of the engine, otherwise exception will be thrown49 **/50 Binding();43 /** 44 * Default constructor used, to create instance and 45 * to initialize all underlying systems. 46 * 47 * NOTE: You have to instantiate a class after initialization 48 * of the engine, otherwise exception will be thrown 49 **/ 50 Binding(); 51 51 52 /**53 * Release used memory. Close glfw subsystem and close54 * the task running in the kernel55 **/56 ~Binding();52 /** 53 * Release used memory. Close glfw subsystem and close 54 * the task running in the kernel 55 **/ 56 ~Binding(); 57 57 58 /**59 * @see nrEngine::Binding::getName()60 **/61 const std::string& getName();58 /** 59 * @see nrEngine::Binding::getName() 60 **/ 61 const std::string& getName(); 62 62 63 /**64 * @see nrEngine::Binding::getFullName()65 **/66 const std::string& getFullName();63 /** 64 * @see nrEngine::Binding::getFullName() 65 **/ 66 const std::string& getFullName(); 67 67 68 /**69 * Open a window where all OpenGL operations access on.70 *71 * @param width,height Size of the window72 * @param fullscreen If true, create fullscreen73 * @param bpp Colorbuffer bit depth (default 32)74 * @param depth Depthbuffer bit depth (default 24)75 * @param stencil Stencilbuffer bit depth (default 8)76 * @return true if it succeeds, false otherwise. See log files77 **/78 bool createWindow(nrEngine::int32 width, nrEngine::int32 height, bool fullscreen, nrEngine::int32 bpp = 32, nrEngine::int32 depth = 24, nrEngine::int32 stencil = 8);68 /** 69 * Open a window where all OpenGL operations access on. 70 * 71 * @param width,height Size of the window 72 * @param fullscreen If true, create fullscreen 73 * @param bpp Colorbuffer bit depth (default 32) 74 * @param depth Depthbuffer bit depth (default 24) 75 * @param stencil Stencilbuffer bit depth (default 8) 76 * @return true if it succeeds, false otherwise. See log files 77 **/ 78 bool createWindow(nrEngine::int32 width, nrEngine::int32 height, bool fullscreen, nrEngine::int32 bpp = 32, nrEngine::int32 depth = 24, nrEngine::int32 stencil = 8); 79 79 80 /**81 * Close the window and kill all used OpenGL contexts82 **/83 void closeWindow();80 /** 81 * Close the window and kill all used OpenGL contexts 82 **/ 83 void closeWindow(); 84 84 85 /**86 * Set window data, as title, position and size87 **/88 void setWindow(const std::string& title, nrEngine::int32 width = 0, nrEngine::int32 height = 0);85 /** 86 * Set window data, as title, position and size 87 **/ 88 void setWindow(const std::string& title, nrEngine::int32 width = 0, nrEngine::int32 height = 0); 89 89 90 /**91 * Get window title92 **/93 const std::string& getWindowTitle() const { return mWindowTitle; }94 95 /**96 * Get the name of the message channel where events are97 * published98 **/99 static const std::string& getChannelName();90 /** 91 * Get window title 92 **/ 93 const std::string& getWindowTitle() const { return mWindowTitle; } 94 95 /** 96 * Get the name of the message channel where events are 97 * published 98 **/ 99 static const std::string& getChannelName(); 100 100 101 /**102 * Get current mouse position103 **/104 void getMousePos(nrEngine::int32& x, nrEngine::int32& y);101 /** 102 * Get current mouse position 103 **/ 104 void getMousePos(nrEngine::int32& x, nrEngine::int32& y); 105 105 106 /**107 * Set mouse to new position108 **/109 void setMousePos(nrEngine::int32 x, nrEngine::int32 y);106 /** 107 * Set mouse to new position 108 **/ 109 void setMousePos(nrEngine::int32 x, nrEngine::int32 y); 110 110 111 /**112 * Check if a certain key is down.113 **/114 bool isKeyDown(nrEngine::keyIndex) const ;115 116 /**117 * Get current window width118 **/119 nrEngine::int32 getWindowWidth() const { return mWindowWidth; }111 /** 112 * Check if a certain key is down. 113 **/ 114 bool isKeyDown(nrEngine::keyIndex) const ; 115 116 /** 117 * Get current window width 118 **/ 119 nrEngine::int32 getWindowWidth() const { return mWindowWidth; } 120 120 121 /**122 * Get current window height123 **/124 nrEngine::int32 getWindowHeight() const { return mWindowHeight; }121 /** 122 * Get current window height 123 **/ 124 nrEngine::int32 getWindowHeight() const { return mWindowHeight; } 125 125 126 /**127 * Access to the singleton object128 **/129 static Binding* instance();130 131 /**132 * Release the singleton object133 **/134 static void release();135 136 /**137 * Check whenever the engine'S singleton was created before138 */139 static bool valid();126 /** 127 * Access to the singleton object 128 **/ 129 static Binding* instance(); 130 131 /** 132 * Release the singleton object 133 **/ 134 static void release(); 135 136 /** 137 * Check whenever the engine'S singleton was created before 138 */ 139 static bool valid(); 140 140 141 private: 141 /** 142 * In normal keys the key event is only initiated if the state of the key is changed. 143 * This means, that you have always to poll for a key state if key is down (i.e. isKeyDown()). 144 * However you can specify keys to emit keypress events on every task update even if they are down on. 145 * 146 * @param keyIndex Index of a key to be monitored 147 **/ 148 void addKeyDownMonitor(nrEngine::keyIndex); 149 150 /** 151 * Remove a key from the key down monitor @see addKeyDownMonitor() 152 **/ 153 void delKeyDownMonitor(nrEngine::keyIndex); 154 155 private: 142 156 143 //! Singleton to hold the binding144 static SharedPtr<Binding> sSingleton;157 //! Singleton to hold the binding 158 static SharedPtr<Binding> sSingleton; 145 159 146 //! Task object of the bindings147 SharedPtr<Task>mTask;148 149 //! Name of the binding150 std::string mName;160 //! Task object of the bindings 161 SharedPtr<Task> mTask; 162 163 //! Name of the binding 164 std::string mName; 151 165 152 //! Full name153 std::string mFullName;166 //! Full name 167 std::string mFullName; 154 168 155 //! Is window already create156 std::string mWindowTitle;169 //! Is window already create 170 std::string mWindowTitle; 157 171 158 //! Current window size159 nrEngine::int32 mWindowWidth, mWindowHeight;172 //! Current window size 173 nrEngine::int32 mWindowWidth, mWindowHeight; 160 174 161 //! Is already created162 bool bWindowCreated;163 164 //! Callback function to close the window165 static nrEngine::int32 closeWindowCallback();175 //! Is already created 176 bool bWindowCreated; 177 178 //! Callback function to close the window 179 static nrEngine::int32 closeWindowCallback(); 166 180 167 ScriptFunctionDef(resizeWindow);168 ScriptFunctionDef(openWindow);169 ScriptFunctionDef(setWindowTitle);181 ScriptFunctionDef(resizeWindow); 182 ScriptFunctionDef(openWindow); 183 ScriptFunctionDef(setWindowTitle); 170 184 171 };185 }; 172 186 173 };187 }; 174 188 }; 175 189 -
Bindings/glfw/Event.h
r37 r39 20 20 namespace nrBinding{ 21 21 22 namespace glfw{ 23 24 /** 25 * OnCreateWindowEvent - this event is emmited as soon as 26 * a new window was created. 27 **/ 28 class _NRExport OnCreateWindowEvent : public nrEngine::Event{ 29 30 META_Event(OnCreateWindowEvent) 31 32 public: 33 //! Constructor 34 OnCreateWindowEvent(nrEngine::int32 width, nrEngine::int32 height, 35 bool full, nrEngine::int32 bpp, nrEngine::int32 depth, 36 nrEngine::int32 stencil): 37 nrEngine::Event(nrEngine::Priority::NORMAL) 38 { 39 this->width = width; 40 this->height = height; 41 this->bpp = bpp; 42 this->depth = depth; 43 this->stencil = stencil; 44 this->bFullscreen = full; 45 } 46 47 //! width stored in the event 48 nrEngine::int32 width; 49 50 //! height of the window 51 nrEngine::int32 height; 52 53 //! color buffer bpp 54 nrEngine::int32 bpp; 55 56 //! depth buffer bpp 57 nrEngine::int32 depth; 58 59 //! stencil buffer bpp 60 nrEngine::int32 stencil; 61 62 //! is fullscreen or not 63 bool bFullscreen; 64 }; 65 66 /** 67 * OnResizeWindowEvent - this event is emmited as soon as 68 * a teh size of the window is changed. 69 **/ 70 class _NRExport OnResizeWindowEvent : public nrEngine::Event{ 71 72 META_Event(OnResizeWindowEvent) 73 74 public: 75 //! Constructor 76 OnResizeWindowEvent(nrEngine::int32 width, nrEngine::int32 height): 77 nrEngine::Event(nrEngine::Priority::NORMAL) 78 { 79 this->width = width; 80 this->height = height; 81 } 82 83 //! width stored in the event 84 nrEngine::int32 width; 85 86 //! height of the window 87 nrEngine::int32 height; 88 }; 89 90 /** 91 * OnCloseWindowEvent - is emmitted if the rendering window 92 * is either closed by the user or by the application. In both cases 93 * if you recieve this message, you have to check, what happens, to 94 * handle appropriately 95 **/ 96 class _NRExport OnCloseWindowEvent : public nrEngine::Event{ 97 98 META_Event(OnCloseWindowEvent) 99 100 public: 101 OnCloseWindowEvent() : nrEngine::Event(nrEngine::Priority::IMMEDIATE) {} 102 }; 103 104 /** 105 * KeyboardEvent - is commited if there is any changes on the state of keyboard keys. 106 * 107 * For key mapping we use the mapping already provided with the engine. We do not 108 * want to write our own, because then the applications using other plugins for 109 * the input may get into troubles. 110 * 111 **/ 112 class _NRExport KeyboardEvent : public nrEngine::Event{ 113 114 META_Event(KeyboardEvent) 115 116 public: 117 118 //! Constructor 119 KeyboardEvent(nrEngine::keyIndex key) : nrEngine::Event(nrEngine::Priority::NORMAL) { mKey = key; } 120 121 //! Destructor non virtual 122 ~KeyboardEvent() {} 123 124 /** 125 * This returns a key index of a key whichs state changes. 126 * The message contain information only about one key, so 127 * you have to store the states somewhere in between, or use 128 * provided plugin methods, to get state information 129 * on the keys. 130 **/ 131 nrEngine::keyIndex getKey() { return mKey; } 132 133 private: 134 135 //! Key whichs state is changed 136 nrEngine::keyIndex mKey; 137 138 }; 139 140 /** 141 * OnKeyboardPressEvent is commited if a key was pressed 142 **/ 143 class _NRExport OnKeyboardPressEvent : public KeyboardEvent { 144 145 META_Event(OnKeyboardPressEvent) 146 147 public: 148 OnKeyboardPressEvent(nrEngine::keyIndex key = nrEngine::KEY_UNKNOWN) : KeyboardEvent(key) {} 149 }; 150 151 /** 152 * OnKeyboardReleaseEvent would be emited if a key was released 153 **/ 154 class _NRExport OnKeyboardReleaseEvent : public KeyboardEvent { 155 156 META_Event(OnKeyboardReleaseEvent) 157 158 public: 159 OnKeyboardReleaseEvent(nrEngine::keyIndex key = nrEngine::KEY_UNKNOWN) : KeyboardEvent(key) {} 160 }; 161 162 /** 163 * OnMouseMoveEvent is send whenever there was a mouse movement. 164 * The event will store the old and the new position of the movement in 165 * screen coordinates. 166 **/ 167 class _NRExport OnMouseMoveEvent : public nrEngine::Event { 168 169 META_Event(OnMouseMoveEvent) 170 171 public: 172 173 OnMouseMoveEvent(nrEngine::int32 newX, nrEngine::int32 newY, 174 nrEngine::int32 oldX, nrEngine::int32 oldY) : nrEngine::Event(nrEngine::Priority::NORMAL) 175 { 176 this->newX = newX; 177 this->newY = newY; 178 this->oldX = oldX; 179 this->oldY = oldY; 180 } 181 182 //! return mouse position 183 void getPosition(nrEngine::int32& x, nrEngine::int32& y){ 184 x = newX; 185 y = newY; 186 } 187 188 //! get old position 189 void getOldPosition(nrEngine::int32& x, nrEngine::int32& y){ 190 x = oldX; 191 y = oldY; 192 } 193 194 private: 195 nrEngine::int32 newX, newY; 196 nrEngine::int32 oldX, oldY; 197 }; 198 199 200 }; 22 namespace glfw{ 23 24 /** 25 * OnCreateWindowEvent - this event is emmited as soon as 26 * a new window was created. 27 **/ 28 class _NRExport OnCreateWindowEvent : public nrEngine::Event{ 29 30 META_Event(OnCreateWindowEvent) 31 32 public: 33 //! Constructor 34 OnCreateWindowEvent(nrEngine::int32 width, nrEngine::int32 height, 35 bool full, nrEngine::int32 bpp, nrEngine::int32 depth, 36 nrEngine::int32 stencil): 37 nrEngine::Event(nrEngine::Priority::NORMAL) 38 { 39 this->width = width; 40 this->height = height; 41 this->bpp = bpp; 42 this->depth = depth; 43 this->stencil = stencil; 44 this->bFullscreen = full; 45 } 46 47 //! width stored in the event 48 nrEngine::int32 width; 49 50 //! height of the window 51 nrEngine::int32 height; 52 53 //! color buffer bpp 54 nrEngine::int32 bpp; 55 56 //! depth buffer bpp 57 nrEngine::int32 depth; 58 59 //! stencil buffer bpp 60 nrEngine::int32 stencil; 61 62 //! is fullscreen or not 63 bool bFullscreen; 64 }; 65 66 /** 67 * OnResizeWindowEvent - this event is emmited as soon as 68 * a teh size of the window is changed. 69 **/ 70 class _NRExport OnResizeWindowEvent : public nrEngine::Event{ 71 72 META_Event(OnResizeWindowEvent) 73 74 public: 75 //! Constructor 76 OnResizeWindowEvent(nrEngine::int32 width, nrEngine::int32 height): 77 nrEngine::Event(nrEngine::Priority::NORMAL) 78 { 79 this->width = width; 80 this->height = height; 81 } 82 83 //! width stored in the event 84 nrEngine::int32 width; 85 86 //! height of the window 87 nrEngine::int32 height; 88 }; 89 90 /** 91 * OnCloseWindowEvent - is emmitted if the rendering window 92 * is either closed by the user or by the application. In both cases 93 * if you recieve this message, you have to check, what happens, to 94 * handle appropriately 95 **/ 96 class _NRExport OnCloseWindowEvent : public nrEngine::Event{ 97 98 META_Event(OnCloseWindowEvent) 99 100 public: 101 OnCloseWindowEvent() : nrEngine::Event(nrEngine::Priority::IMMEDIATE) {} 102 }; 103 104 /** 105 * KeyboardEvent - is commited if there is any changes on the state of keyboard keys. 106 * 107 * For key mapping we use the mapping already provided with the engine. We do not 108 * want to write our own, because then the applications using other plugins for 109 * the input may get into troubles. 110 * 111 **/ 112 class _NRExport KeyboardEvent : public nrEngine::Event{ 113 114 META_Event(KeyboardEvent) 115 116 public: 117 118 //! Constructor 119 KeyboardEvent(nrEngine::keyIndex key) : nrEngine::Event(nrEngine::Priority::NORMAL) { mKey = key; } 120 121 //! Destructor non virtual 122 ~KeyboardEvent() {} 123 124 /** 125 * This returns a key index of a key whichs state changes. 126 * The message contain information only about one key, so 127 * you have to store the states somewhere in between, or use 128 * provided plugin methods, to get state information 129 * on the keys. 130 **/ 131 nrEngine::keyIndex getKey() { return mKey; } 132 133 private: 134 135 //! Key whichs state is changed 136 nrEngine::keyIndex mKey; 137 138 }; 139 140 /** 141 * OnKeyboardDownEvent is commited if a key is down and monitored 142 **/ 143 class _NRExport OnKeyboardDownEvent : public KeyboardEvent { 144 145 META_Event(OnKeyboardDownEvent) 146 147 public: 148 OnKeyboardDownEvent(nrEngine::keyIndex key = nrEngine::KEY_UNKNOWN) : KeyboardEvent(key) {} 149 }; 150 151 /** 152 * OnKeyboardPressEvent is commited if a key was pressed 153 **/ 154 class _NRExport OnKeyboardPressEvent : public KeyboardEvent { 155 156 META_Event(OnKeyboardPressEvent) 157 158 public: 159 OnKeyboardPressEvent(nrEngine::keyIndex key = nrEngine::KEY_UNKNOWN) : KeyboardEvent(key) {} 160 }; 161 162 /** 163 * OnKeyboardReleaseEvent would be emited if a key was released 164 **/ 165 class _NRExport OnKeyboardReleaseEvent : public KeyboardEvent { 166 167 META_Event(OnKeyboardReleaseEvent) 168 169 public: 170 OnKeyboardReleaseEvent(nrEngine::keyIndex key = nrEngine::KEY_UNKNOWN) : KeyboardEvent(key) {} 171 }; 172 173 /** 174 * OnMouseMoveEvent is send whenever there was a mouse movement. 175 * The event will store the old and the new position of the movement in 176 * screen coordinates. 177 **/ 178 class _NRExport OnMouseMoveEvent : public nrEngine::Event { 179 180 META_Event(OnMouseMoveEvent) 181 182 public: 183 184 OnMouseMoveEvent(nrEngine::int32 newX, nrEngine::int32 newY, 185 nrEngine::int32 oldX, nrEngine::int32 oldY) : nrEngine::Event(nrEngine::Priority::NORMAL) 186 { 187 this->newX = newX; 188 this->newY = newY; 189 this->oldX = oldX; 190 this->oldY = oldY; 191 } 192 193 //! return mouse position 194 void getPosition(nrEngine::int32& x, nrEngine::int32& y){ 195 x = newX; 196 y = newY; 197 } 198 199 //! get old position 200 void getOldPosition(nrEngine::int32& x, nrEngine::int32& y){ 201 x = oldX; 202 y = oldY; 203 } 204 205 private: 206 nrEngine::int32 newX, newY; 207 nrEngine::int32 oldX, oldY; 208 }; 209 210 211 }; 201 212 }; 202 213 -
Bindings/glfw/Task.cpp
r1 r39 21 21 namespace nrBinding { 22 22 23 namespace glfw{ 24 25 std::string Task::ChannelName(TASK_NAME); 26 nrEngine::int32 Task::mMouseX = 0; 27 nrEngine::int32 Task::mMouseY = 0; 28 29 using namespace nrEngine; 30 31 //------------------------------------------------------------ 32 Task::Task(nrEngine::Engine* root) : mEngine(root) 33 { 34 setTaskName(TASK_NAME); 35 36 // check for engine 37 if (mEngine == NULL) throw(-1); 38 39 // check if such a task already running 40 if (mEngine->sKernel()->getTaskByName(TASK_NAME)) throw GLFW_TASK_ALREADY_RUNNING; 41 42 // ok let's initialize the glfw subsystem 43 if (glfwInit() == GL_FALSE) throw GLFW_CANNOT_INITIALIZE; 44 45 // create a communication channel 46 mEngine->sEventManager()->createChannel(Task::ChannelName); 47 } 48 49 //------------------------------------------------------------ 50 Task::~Task() 51 { 52 glfwTerminate(); 53 } 54 55 //------------------------------------------------------------ 56 void Task::noticeWindowCreated() 57 { 58 // propaget the key callback function 59 glfwSetKeyCallback(keyCallback); 60 glfwSetCharCallback(keyCharCallback); 61 glfwSetMousePosCallback(mousePosCallback); 62 } 63 64 //------------------------------------------------------------ 65 const std::string& Task::getChannelName(){ 66 return Task::ChannelName; 67 } 68 69 70 //------------------------------------------------------------ 71 Result Task::updateTask() 72 { 73 glfwSwapBuffers(); 74 return OK; 75 } 76 77 //------------------------------------------------------------ 78 void Task::keyCallback(int key, int action) 79 { 80 // first convert the key into suitable nrEngine's key index 81 keyIndex nrkey = convert(key); 82 SharedPtr<Event> msg; 83 84 // the key was pressed 85 if (action == GLFW_PRESS){ 86 87 // send a message that the key was pressed 88 msg.reset(new OnKeyboardPressEvent(nrkey)); 89 90 // key is released 91 }else if (action == GLFW_RELEASE){ 92 93 // send a message that key was released 94 msg.reset(new OnKeyboardReleaseEvent(nrkey)); 95 96 } 97 98 // send to the input chanel 99 Engine::sEventManager()->emit(Task::getChannelName(), msg); 100 } 101 102 //------------------------------------------------------------ 103 void Task::keyCharCallback(int character, int action) 104 { 105 106 } 107 108 //------------------------------------------------------------ 109 void Task::mousePosCallback (int x, int y) 110 { 111 SharedPtr<Event> msg (new OnMouseMoveEvent(x,y, mMouseX, mMouseY)); 112 Engine::sEventManager()->emit(Task::getChannelName(), msg); 113 mMouseX = x; 114 mMouseY = y; 115 } 116 117 //---------------------------------------------------------------------- 118 void Task::getMousePos(int& x, int& y) 119 { 120 x = mMouseX; 121 y = mMouseY; 122 } 123 124 //---------------------------------------------------------------------- 125 void Task::setMousePos(int x, int y) 126 { 127 mMouseX = x; 128 mMouseY = y; 129 glfwSetMousePos(x,y); 130 } 131 132 //------------------------------------------------------------ 133 bool Task::isKeyDown(nrEngine::keyIndex key) const 134 { 135 return glfwGetKey(inv_convert(key)) == GLFW_PRESS; 136 } 137 138 //------------------------------------------------------------ 139 keyIndex Task::convert(int key) 140 { 141 #define MAP(glfw, nrkey) if (key == glfw) return nrkey; 142 143 MAP(GLFW_KEY_SPACE, KEY_SPACE) 144 MAP(GLFW_KEY_ESC, KEY_ESCAPE) 145 146 MAP(GLFW_KEY_F1, KEY_F1) 147 MAP(GLFW_KEY_F2, KEY_F2) 148 MAP(GLFW_KEY_F3, KEY_F3) 149 MAP(GLFW_KEY_F4, KEY_F4) 150 MAP(GLFW_KEY_F5, KEY_F5) 151 MAP(GLFW_KEY_F6, KEY_F6) 152 MAP(GLFW_KEY_F7, KEY_F7) 153 MAP(GLFW_KEY_F8, KEY_F8) 154 MAP(GLFW_KEY_F9, KEY_F9) 155 MAP(GLFW_KEY_F10, KEY_F10) 156 MAP(GLFW_KEY_F11, KEY_F11) 157 MAP(GLFW_KEY_F12, KEY_F12) 158 MAP(GLFW_KEY_F13, KEY_F13) 159 MAP(GLFW_KEY_F14, KEY_F14) 160 MAP(GLFW_KEY_F15, KEY_F15) 161 162 MAP(GLFW_KEY_UP, KEY_UP) 163 MAP(GLFW_KEY_DOWN, KEY_DOWN) 164 MAP(GLFW_KEY_LEFT, KEY_LEFT) 165 MAP(GLFW_KEY_RIGHT, KEY_RIGHT) 166 167 MAP(GLFW_KEY_LSHIFT, KEY_LSHIFT) 168 MAP(GLFW_KEY_RSHIFT, KEY_RSHIFT) 169 MAP(GLFW_KEY_LALT, KEY_LALT) 170 MAP(GLFW_KEY_RALT, KEY_RALT) 171 MAP(GLFW_KEY_LCTRL, KEY_LCTRL) 172 MAP(GLFW_KEY_LCTRL, KEY_LCTRL) 173 174 MAP(GLFW_KEY_TAB, KEY_TAB) 175 MAP(GLFW_KEY_ENTER, KEY_RETURN) 176 MAP(GLFW_KEY_BACKSPACE, KEY_BACKSPACE) 177 MAP(GLFW_KEY_INSERT, KEY_INSERT) 178 MAP(GLFW_KEY_DEL, KEY_DELETE) 179 MAP(GLFW_KEY_PAGEUP, KEY_PAGEUP) 180 MAP(GLFW_KEY_PAGEDOWN, KEY_PAGEDOWN) 181 MAP(GLFW_KEY_HOME, KEY_HOME) 182 MAP(GLFW_KEY_END, KEY_END) 183 184 MAP(GLFW_KEY_KP_0, KEY_KP0) 185 MAP(GLFW_KEY_KP_1, KEY_KP1) 186 MAP(GLFW_KEY_KP_2, KEY_KP2) 187 MAP(GLFW_KEY_KP_3, KEY_KP3) 188 MAP(GLFW_KEY_KP_4, KEY_KP4) 189 MAP(GLFW_KEY_KP_5, KEY_KP5) 190 MAP(GLFW_KEY_KP_6, KEY_KP6) 191 MAP(GLFW_KEY_KP_7, KEY_KP7) 192 MAP(GLFW_KEY_KP_8, KEY_KP8) 193 MAP(GLFW_KEY_KP_9, KEY_KP9) 194 195 MAP(GLFW_KEY_KP_DIVIDE, KEY_KP_DIVIDE) 196 MAP(GLFW_KEY_KP_MULTIPLY, KEY_KP_MULTIPLY) 197 MAP(GLFW_KEY_KP_SUBTRACT, KEY_KP_MINUS) 198 MAP(GLFW_KEY_KP_ADD, KEY_KP_PLUS) 199 MAP(GLFW_KEY_KP_DECIMAL, KEY_KP_PERIOD) 200 MAP(GLFW_KEY_KP_EQUAL, KEY_KP_EQUALS) 201 MAP(GLFW_KEY_KP_ENTER, KEY_KP_ENTER) 202 203 #undef MAP 204 return KEY_UNKNOWN; 205 } 206 207 //------------------------------------------------------------ 208 int Task::inv_convert(nrEngine::keyIndex key) 209 { 210 if (key >= KEY_a && key <= KEY_z) { 211 return int('A') + int(key) - int(KEY_a); 212 } 213 214 #define MAP(glfw, nrkey) if (key == nrkey) return glfw; 215 216 MAP(GLFW_KEY_SPACE, KEY_SPACE) 217 MAP(GLFW_KEY_ESC, KEY_ESCAPE) 218 219 MAP(GLFW_KEY_F1, KEY_F1) 220 MAP(GLFW_KEY_F2, KEY_F2) 221 MAP(GLFW_KEY_F3, KEY_F3) 222 MAP(GLFW_KEY_F4, KEY_F4) 223 MAP(GLFW_KEY_F5, KEY_F5) 224 MAP(GLFW_KEY_F6, KEY_F6) 225 MAP(GLFW_KEY_F7, KEY_F7) 226 MAP(GLFW_KEY_F8, KEY_F8) 227 MAP(GLFW_KEY_F9, KEY_F9) 228 MAP(GLFW_KEY_F10, KEY_F10) 229 MAP(GLFW_KEY_F11, KEY_F11) 230 MAP(GLFW_KEY_F12, KEY_F12) 231 MAP(GLFW_KEY_F13, KEY_F13) 232 MAP(GLFW_KEY_F14, KEY_F14) 233 MAP(GLFW_KEY_F15, KEY_F15) 234 235 MAP(GLFW_KEY_UP, KEY_UP) 236 MAP(GLFW_KEY_DOWN, KEY_DOWN) 237 MAP(GLFW_KEY_LEFT, KEY_LEFT) 238 MAP(GLFW_KEY_RIGHT, KEY_RIGHT) 239 240 MAP(GLFW_KEY_LSHIFT, KEY_LSHIFT) 241 MAP(GLFW_KEY_RSHIFT, KEY_RSHIFT) 242 MAP(GLFW_KEY_LALT, KEY_LALT) 243 MAP(GLFW_KEY_RALT, KEY_RALT) 244 MAP(GLFW_KEY_LCTRL, KEY_LCTRL) 245 MAP(GLFW_KEY_LCTRL, KEY_LCTRL) 246 247 MAP(GLFW_KEY_TAB, KEY_TAB) 248 MAP(GLFW_KEY_ENTER, KEY_RETURN) 249 MAP(GLFW_KEY_BACKSPACE, KEY_BACKSPACE) 250 MAP(GLFW_KEY_INSERT, KEY_INSERT) 251 MAP(GLFW_KEY_DEL, KEY_DELETE) 252 MAP(GLFW_KEY_PAGEUP, KEY_PAGEUP) 253 MAP(GLFW_KEY_PAGEDOWN, KEY_PAGEDOWN) 254 MAP(GLFW_KEY_HOME, KEY_HOME) 255 MAP(GLFW_KEY_END, KEY_END) 256 257 MAP(GLFW_KEY_KP_0, KEY_KP0) 258 MAP(GLFW_KEY_KP_1, KEY_KP1) 259 MAP(GLFW_KEY_KP_2, KEY_KP2) 260 MAP(GLFW_KEY_KP_3, KEY_KP3) 261 MAP(GLFW_KEY_KP_4, KEY_KP4) 262 MAP(GLFW_KEY_KP_5, KEY_KP5) 263 MAP(GLFW_KEY_KP_6, KEY_KP6) 264 MAP(GLFW_KEY_KP_7, KEY_KP7) 265 MAP(GLFW_KEY_KP_8, KEY_KP8) 266 MAP(GLFW_KEY_KP_9, KEY_KP9) 267 268 MAP(GLFW_KEY_KP_DIVIDE, KEY_KP_DIVIDE) 269 MAP(GLFW_KEY_KP_MULTIPLY, KEY_KP_MULTIPLY) 270 MAP(GLFW_KEY_KP_SUBTRACT, KEY_KP_MINUS) 271 MAP(GLFW_KEY_KP_ADD, KEY_KP_PLUS) 272 MAP(GLFW_KEY_KP_DECIMAL, KEY_KP_PERIOD) 273 MAP(GLFW_KEY_KP_EQUAL, KEY_KP_EQUALS) 274 MAP(GLFW_KEY_KP_ENTER, KEY_KP_ENTER) 275 276 #undef MAP 277 return -1; 278 } 279 280 }; 23 namespace glfw{ 24 25 std::string Task::ChannelName(TASK_NAME); 26 nrEngine::int32 Task::mMouseX = 0; 27 nrEngine::int32 Task::mMouseY = 0; 28 29 using namespace nrEngine; 30 31 //------------------------------------------------------------ 32 Task::Task(nrEngine::Engine* root) : mEngine(root) 33 { 34 setTaskName(TASK_NAME); 35 36 // check for engine 37 if (mEngine == NULL) throw(-1); 38 39 // check if such a task already running 40 if (mEngine->sKernel()->getTaskByName(TASK_NAME)) throw GLFW_TASK_ALREADY_RUNNING; 41 42 // ok let's initialize the glfw subsystem 43 if (glfwInit() == GL_FALSE) throw GLFW_CANNOT_INITIALIZE; 44 45 // create a communication channel 46 mEngine->sEventManager()->createChannel(Task::ChannelName); 47 } 48 49 //------------------------------------------------------------ 50 Task::~Task() 51 { 52 glfwTerminate(); 53 } 54 55 //------------------------------------------------------------ 56 void Task::noticeWindowCreated() 57 { 58 // propaget the key callback function 59 glfwSetKeyCallback(keyCallback); 60 glfwSetCharCallback(keyCharCallback); 61 glfwSetMousePosCallback(mousePosCallback); 62 } 63 64 //------------------------------------------------------------ 65 const std::string& Task::getChannelName(){ 66 return Task::ChannelName; 67 } 68 69 70 //------------------------------------------------------------ 71 Result Task::updateTask() 72 { 73 // swap buffers and thus poll for new events 74 glfwSwapBuffers(); 75 76 // now check for monitored keys 77 std::vector<nrEngine::keyIndex>::iterator it = mMonitoredKeys.begin(); 78 for (; it != mMonitoredKeys.end(); it++) 79 { 80 // if key is down, then send event 81 if (isKeyDown(*it)) 82 { 83 SharedPtr<Event> msg(new OnKeyboardDownEvent(*it)); 84 Engine::sEventManager()->emit(Task::getChannelName(), msg); 85 } 86 } 87 88 89 // all things all right 90 return OK; 91 } 92 93 //------------------------------------------------------------ 94 void Task::keyCallback(int key, int action) 95 { 96 keyIndex nrkey = convert(key); 97 SharedPtr<Event> msg; 98 99 // the key was pressed 100 if (action == GLFW_PRESS){ 101 102 // send a message that the key was pressed 103 msg.reset(new OnKeyboardPressEvent(nrkey)); 104 105 // key is released 106 }else if (action == GLFW_RELEASE){ 107 108 // send a message that key was released 109 msg.reset(new OnKeyboardReleaseEvent(nrkey)); 110 111 } 112 113 // send to the input chanel 114 Engine::sEventManager()->emit(Task::getChannelName(), msg); 115 } 116 117 118 //------------------------------------------------------------ 119 void Task::keyCharCallback(int character, int action) 120 { 121 122 } 123 124 //------------------------------------------------------------ 125 void Task::mousePosCallback (int x, int y) 126 { 127 SharedPtr<Event> msg (new OnMouseMoveEvent(x,y, mMouseX, mMouseY)); 128 Engine::sEventManager()->emit(Task::getChannelName(), msg); 129 mMouseX = x; 130 mMouseY = y; 131 } 132 133 //---------------------------------------------------------------------- 134 void Task::getMousePos(int& x, int& y) 135 { 136 x = mMouseX; 137 y = mMouseY; 138 } 139 140 //---------------------------------------------------------------------- 141 void Task::setMousePos(int x, int y) 142 { 143 mMouseX = x; 144 mMouseY = y; 145 glfwSetMousePos(x,y); 146 } 147 148 //------------------------------------------------------------ 149 bool Task::isKeyDown(nrEngine::keyIndex key) const 150 { 151 return glfwGetKey(inv_convert(key)) == GLFW_PRESS; 152 } 153 154 //---------------------------------------------------------------------- 155 void Task::addKeyDownMonitor(nrEngine::keyIndex key) 156 { 157 // check if such a key index already exists in the database 158 for (unsigned int i = 0; i < mMonitoredKeys.size(); i++) 159 if (mMonitoredKeys[i] == key) return; 160 161 // add the key to the list 162 mMonitoredKeys.push_back(key); 163 } 164 165 //---------------------------------------------------------------------- 166 void Task::delKeyDownMonitor(nrEngine::keyIndex key) 167 { 168 // go through the database and check for the key 169 std::vector<nrEngine::keyIndex>::iterator it = mMonitoredKeys.begin(); 170 for (; it != mMonitoredKeys.end(); it++) 171 { 172 if (*it == key) 173 { 174 it = mMonitoredKeys.erase(it); 175 return; 176 } 177 } 178 } 179 180 //------------------------------------------------------------ 181 keyIndex Task::convert(int key) 182 { 183 #define MAP(glfw, nrkey) if (key == glfw) return nrkey; 184 185 MAP(GLFW_KEY_SPACE, KEY_SPACE) 186 MAP(GLFW_KEY_ESC, KEY_ESCAPE) 187 188 MAP(GLFW_KEY_F1, KEY_F1) 189 MAP(GLFW_KEY_F2, KEY_F2) 190 MAP(GLFW_KEY_F3, KEY_F3) 191 MAP(GLFW_KEY_F4, KEY_F4) 192 MAP(GLFW_KEY_F5, KEY_F5) 193 MAP(GLFW_KEY_F6, KEY_F6) 194 MAP(GLFW_KEY_F7, KEY_F7) 195 MAP(GLFW_KEY_F8, KEY_F8) 196 MAP(GLFW_KEY_F9, KEY_F9) 197 MAP(GLFW_KEY_F10, KEY_F10) 198 MAP(GLFW_KEY_F11, KEY_F11) 199 MAP(GLFW_KEY_F12, KEY_F12) 200 MAP(GLFW_KEY_F13, KEY_F13) 201 MAP(GLFW_KEY_F14, KEY_F14) 202 MAP(GLFW_KEY_F15, KEY_F15) 203 204 MAP(GLFW_KEY_UP, KEY_UP) 205 MAP(GLFW_KEY_DOWN, KEY_DOWN) 206 MAP(GLFW_KEY_LEFT, KEY_LEFT) 207 MAP(GLFW_KEY_RIGHT, KEY_RIGHT) 208 209 MAP(GLFW_KEY_LSHIFT, KEY_LSHIFT) 210 MAP(GLFW_KEY_RSHIFT, KEY_RSHIFT) 211 MAP(GLFW_KEY_LALT, KEY_LALT) 212 MAP(GLFW_KEY_RALT, KEY_RALT) 213 MAP(GLFW_KEY_LCTRL, KEY_LCTRL) 214 MAP(GLFW_KEY_LCTRL, KEY_LCTRL) 215 216 MAP(GLFW_KEY_TAB, KEY_TAB) 217 MAP(GLFW_KEY_ENTER, KEY_RETURN) 218 MAP(GLFW_KEY_BACKSPACE, KEY_BACKSPACE) 219 MAP(GLFW_KEY_INSERT, KEY_INSERT) 220 MAP(GLFW_KEY_DEL, KEY_DELETE) 221 MAP(GLFW_KEY_PAGEUP, KEY_PAGEUP) 222 MAP(GLFW_KEY_PAGEDOWN, KEY_PAGEDOWN) 223 MAP(GLFW_KEY_HOME, KEY_HOME) 224 MAP(GLFW_KEY_END, KEY_END) 225 226 MAP(GLFW_KEY_KP_0, KEY_KP0) 227 MAP(GLFW_KEY_KP_1, KEY_KP1) 228 MAP(GLFW_KEY_KP_2, KEY_KP2) 229 MAP(GLFW_KEY_KP_3, KEY_KP3) 230 MAP(GLFW_KEY_KP_4, KEY_KP4) 231 MAP(GLFW_KEY_KP_5, KEY_KP5) 232 MAP(GLFW_KEY_KP_6, KEY_KP6) 233 MAP(GLFW_KEY_KP_7, KEY_KP7) 234 MAP(GLFW_KEY_KP_8, KEY_KP8) 235 MAP(GLFW_KEY_KP_9, KEY_KP9) 236 237 MAP(GLFW_KEY_KP_DIVIDE, KEY_KP_DIVIDE) 238 MAP(GLFW_KEY_KP_MULTIPLY, KEY_KP_MULTIPLY) 239 MAP(GLFW_KEY_KP_SUBTRACT, KEY_KP_MINUS) 240 MAP(GLFW_KEY_KP_ADD, KEY_KP_PLUS) 241 MAP(GLFW_KEY_KP_DECIMAL, KEY_KP_PERIOD) 242 MAP(GLFW_KEY_KP_EQUAL, KEY_KP_EQUALS) 243 MAP(GLFW_KEY_KP_ENTER, KEY_KP_ENTER) 244 245 #undef MAP 246 return KEY_UNKNOWN; 247 } 248 249 //------------------------------------------------------------ 250 int Task::inv_convert(nrEngine::keyIndex key) 251 { 252 if (key >= KEY_a && key <= KEY_z) { 253 return int('A') + int(key) - int(KEY_a); 254 } 255 256 #define MAP(glfw, nrkey) if (key == nrkey) return glfw; 257 258 MAP(GLFW_KEY_SPACE, KEY_SPACE) 259 MAP(GLFW_KEY_ESC, KEY_ESCAPE) 260 261 MAP(GLFW_KEY_F1, KEY_F1) 262 MAP(GLFW_KEY_F2, KEY_F2) 263 MAP(GLFW_KEY_F3, KEY_F3) 264 MAP(GLFW_KEY_F4, KEY_F4) 265 MAP(GLFW_KEY_F5, KEY_F5) 266 MAP(GLFW_KEY_F6, KEY_F6) 267 MAP(GLFW_KEY_F7, KEY_F7) 268 MAP(GLFW_KEY_F8, KEY_F8) 269 MAP(GLFW_KEY_F9, KEY_F9) 270 MAP(GLFW_KEY_F10, KEY_F10) 271 MAP(GLFW_KEY_F11, KEY_F11) 272 MAP(GLFW_KEY_F12, KEY_F12) 273 MAP(GLFW_KEY_F13, KEY_F13) 274 MAP(GLFW_KEY_F14, KEY_F14) 275 MAP(GLFW_KEY_F15, KEY_F15) 276 277 MAP(GLFW_KEY_UP, KEY_UP) 278 MAP(GLFW_KEY_DOWN, KEY_DOWN) 279 MAP(GLFW_KEY_LEFT, KEY_LEFT) 280 MAP(GLFW_KEY_RIGHT, KEY_RIGHT) 281 282 MAP(GLFW_KEY_LSHIFT, KEY_LSHIFT) 283 MAP(GLFW_KEY_RSHIFT, KEY_RSHIFT) 284 MAP(GLFW_KEY_LALT, KEY_LALT) 285 MAP(GLFW_KEY_RALT, KEY_RALT) 286 MAP(GLFW_KEY_LCTRL, KEY_LCTRL) 287 MAP(GLFW_KEY_LCTRL, KEY_LCTRL) 288 289 MAP(GLFW_KEY_TAB, KEY_TAB) 290 MAP(GLFW_KEY_ENTER, KEY_RETURN) 291 MAP(GLFW_KEY_BACKSPACE, KEY_BACKSPACE) 292 MAP(GLFW_KEY_INSERT, KEY_INSERT) 293 MAP(GLFW_KEY_DEL, KEY_DELETE) 294 MAP(GLFW_KEY_PAGEUP, KEY_PAGEUP) 295 MAP(GLFW_KEY_PAGEDOWN, KEY_PAGEDOWN) 296 MAP(GLFW_KEY_HOME, KEY_HOME) 297 MAP(GLFW_KEY_END, KEY_END) 298 299 MAP(GLFW_KEY_KP_0, KEY_KP0) 300 MAP(GLFW_KEY_KP_1, KEY_KP1) 301 MAP(GLFW_KEY_KP_2, KEY_KP2) 302 MAP(GLFW_KEY_KP_3, KEY_KP3) 303 MAP(GLFW_KEY_KP_4, KEY_KP4) 304 MAP(GLFW_KEY_KP_5, KEY_KP5) 305 MAP(GLFW_KEY_KP_6, KEY_KP6) 306 MAP(GLFW_KEY_KP_7, KEY_KP7) 307 MAP(GLFW_KEY_KP_8, KEY_KP8) 308 MAP(GLFW_KEY_KP_9, KEY_KP9) 309 310 MAP(GLFW_KEY_KP_DIVIDE, KEY_KP_DIVIDE) 311 MAP(GLFW_KEY_KP_MULTIPLY, KEY_KP_MULTIPLY) 312 MAP(GLFW_KEY_KP_SUBTRACT, KEY_KP_MINUS) 313 MAP(GLFW_KEY_KP_ADD, KEY_KP_PLUS) 314 MAP(GLFW_KEY_KP_DECIMAL, KEY_KP_PERIOD) 315 MAP(GLFW_KEY_KP_EQUAL, KEY_KP_EQUALS) 316 MAP(GLFW_KEY_KP_ENTER, KEY_KP_ENTER) 317 318 #undef MAP 319 return -1; 320 } 321 322 }; 281 323 }; 282 324 -
Bindings/glfw/Task.h
r1 r39 25 25 namespace nrBinding { 26 26 27 namespace glfw {27 namespace glfw { 28 28 29 /**30 * glfwTask is a task provided by this plugin running in the engine's kernel.31 * The work of a task will be to poll the events from the glfw subsystem32 * and to send events through the engine's communication channel33 * to message listeners.34 *35 * The class also defines some callback functions which will be called36 * from the glfw subsystem and emit some events to nrEngine's message37 * listeners.38 **/39 class Task : public nrEngine::ITask{40 public:29 /** 30 * glfwTask is a task provided by this plugin running in the engine's kernel. 31 * The work of a task will be to poll the events from the glfw subsystem 32 * and to send events through the engine's communication channel 33 * to message listeners. 34 * 35 * The class also defines some callback functions which will be called 36 * from the glfw subsystem and emit some events to nrEngine's message 37 * listeners. 38 **/ 39 class Task : public nrEngine::ITask{ 40 public: 41 41 42 /**43 * Initialize glfw subsystem and check if such task is44 * already in the kernel. If it is a case, so throw an error.45 * The error must be catched by the calling functions, so46 * we can tell the application which loads the plugin, that47 * there is already a plugin running such a task.48 **/49 Task(nrEngine::Engine* root);42 /** 43 * Initialize glfw subsystem and check if such task is 44 * already in the kernel. If it is a case, so throw an error. 45 * The error must be catched by the calling functions, so 46 * we can tell the application which loads the plugin, that 47 * there is already a plugin running such a task. 48 **/ 49 Task(nrEngine::Engine* root); 50 50 51 /**52 * Terminate glfw subsystem and release used memory53 **/54 ~Task();51 /** 52 * Terminate glfw subsystem and release used memory 53 **/ 54 ~Task(); 55 55 56 /**57 * update the task58 **/59 nrEngine::Result updateTask();56 /** 57 * update the task 58 **/ 59 nrEngine::Result updateTask(); 60 60 61 /**62 * Window was opened, so do the stuff needed for this63 **/64 void noticeWindowCreated();61 /** 62 * Window was opened, so do the stuff needed for this 63 **/ 64 void noticeWindowCreated(); 65 65 66 /**67 * Get current mouse position68 **/69 void getMousePos(int& x, int& y);66 /** 67 * Get current mouse position 68 **/ 69 void getMousePos(int& x, int& y); 70 70 71 /**72 * Set mouse to new position73 **/74 void setMousePos(int x, int y);71 /** 72 * Set mouse to new position 73 **/ 74 void setMousePos(int x, int y); 75 75 76 /**77 * Check if a certain key is down78 **/79 bool isKeyDown(nrEngine::keyIndex) const;76 /** 77 * Check if a certain key is down 78 **/ 79 bool isKeyDown(nrEngine::keyIndex) const; 80 80 81 /**82 * Get the channel name where events are commited83 **/84 static const std::string& getChannelName() ;81 /** 82 * Get the channel name where events are commited 83 **/ 84 static const std::string& getChannelName() ; 85 85 86 //! Channel name87 static std::string ChannelName;86 //! Channel name 87 static std::string ChannelName; 88 88 89 private: 89 void addKeyDownMonitor(nrEngine::keyIndex); 90 void delKeyDownMonitor(nrEngine::keyIndex); 90 91 91 //! Engine's root pointer 92 nrEngine::Engine* mEngine; 92 private: 93 93 94 //! Call this callback if a key press event was emited from the glfw 95 static void keyCallback(int key, int action); 94 //! Here we store all the keys which sould be monitored for key down 95 std::vector<nrEngine::keyIndex> mMonitoredKeys; 96 96 97 //! Call this callback if a character key press event was emited from the glfw 98 static void keyCharCallback(int character, int action);97 //! Engine's root pointer 98 nrEngine::Engine* mEngine; 99 99 100 //! Convert the glfw key index into engine's one 101 static nrEngine::keyIndex convert(int key);100 //! Call this callback if a key press event was emited from the glfw 101 static void keyCallback(int key, int action); 102 102 103 //! Convert the glfw key index into engine's one 104 static int inv_convert(nrEngine::keyIndex);103 //! Call this callback if a character key press event was emited from the glfw 104 static void keyCharCallback(int character, int action); 105 105 106 //! Callback for any mouse movements 107 static void mousePosCallback (int x, inty);106 //! Convert the glfw key index into engine's one 107 static nrEngine::keyIndex convert(int key); 108 108 109 //! Store current mouse position 110 static nrEngine::int32 mMouseX, mMouseY;109 //! Convert the glfw key index into engine's one 110 static int inv_convert(nrEngine::keyIndex); 111 111 112 }; 113 }; 112 //! Callback for any mouse movements 113 static void mousePosCallback (int x, int y); 114 115 //! Store current mouse position 116 static nrEngine::int32 mMouseX, mMouseY; 117 118 }; 119 }; 114 120 }; 115 121 116 #endif //_NR...122 #endif //_NR...
Note: See TracChangeset
for help on using the changeset viewer.
