00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef GLUTIO_MODULE_H
00029 #define GLUTIO_MODULE_H
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "FsfCell.h"
00044 #include "ImageModule.h"
00045
00046
00047
00048 #ifdef GLUTIO_CARBON
00049 #include <OpenGL/gl.h>
00050 #include <OpenGL/glu.h>
00051 #include <OpenGL/OpenGL.h>
00052 #include <GLUT/glut.h>
00053 #endif
00054
00055 #ifdef GLUTIO_X11
00056 #include <GL/gl.h>
00057 #include <GL/glu.h>
00058 #include <GL/glx.h>
00059 #include <GL/glut.h>
00060 #endif
00061
00062 #ifdef GLUTIO_WIN32
00063 #include <windows.h>
00064 #include <GL/gl.h>
00065 #include <GL/glu.h>
00066 #include <glut.h>
00067 #endif
00068
00069
00070 #include <iostream>
00071
00072 namespace glutio{
00073
00076
00081
00082
00083 using namespace std;
00084
00086 void registerFactories();
00087
00088
00089 class CListenKeyboard;
00090 class CListenMouse;
00091 class CListenJoystick;
00092
00093
00095
00097
00101 class CGlutIO{
00102 private:
00103 static CGlutIO *m_pSingle;
00104 CGlutIO() {}
00105
00106 static fsf::CMutex m_csContext;
00107 static int m_nWidth;
00108 static int m_nHeight;
00109
00110 #ifdef GLUTIO_CARBON
00111 static CGLContextObj m_context;
00112 #endif
00113
00114 #ifdef GLUTIO_X11
00115 static Display* m_display;
00116 static GLXDrawable m_drawable;
00117 static GLXContext m_context;
00118 #endif
00119
00120 #ifdef GLUTIO_WIN32
00121 static HDC m_hdc;
00122 static HGLRC m_context;
00123 #endif
00124
00126
00127 static CListenKeyboard *m_pKb;
00128 static CListenMouse *m_pMs;
00129 static CListenJoystick *m_pJs;
00130
00131
00132 public:
00134
00135 static CGlutIO* getInstance(){
00136 if(m_pSingle!=NULL)
00137 return m_pSingle;
00138 else
00139 return (m_pSingle=new CGlutIO);
00140 }
00141 static void deleteInstance(){
00142 if(m_pSingle!=NULL){
00143 delete m_pSingle;
00144 m_pSingle=NULL;
00145 }
00146 }
00148 ~CGlutIO() {}
00150
00152
00153
00154 static void init(bool bKeyboard=true, bool bMouse=false, bool bMotion=false, bool bPassiveMotion=false, bool bJoystick=false);
00156 static void mainLoop() { glutMainLoop(); }
00157
00159 static int getWidth() { return m_nWidth; }
00161 static int getHeight() { return m_nHeight; }
00162
00164 static void registerListenKeyboardCell(CListenKeyboard *pKb);
00166 static void registerListenMouseCell(CListenMouse *pMs, bool bClick=true, bool bMotion=false, bool bActiveMotion=false);
00168 static void registerListenJoystickCell(CListenJoystick *pJs, int pollInterval);
00170
00172
00173
00174 static void displayCb(void);
00176 static void idleCb();
00178 static void reshapeCb(int width, int height);
00180 static void keyCb(unsigned char key, int x, int y);
00182 static void specialKeyCb(int key, int x, int y);
00184 static void mouseCb(int button, int state, int x, int y);
00186 static void motionCb(int x, int y);
00188 static void passiveMotionCb(int x, int y);
00190 static void joystickCb(unsigned int buttonMask, int x, int y, int z);
00192
00194
00195 #ifdef GLUTIO_CARBON
00196 static void obtainContext() { m_csContext.lock(); CGLSetCurrentContext(m_context); }
00197 static void releaseContext() { CGLSetCurrentContext(NULL); m_csContext.unlock(); }
00198 #endif
00199
00200 #ifdef GLUTIO_X11
00201 static void ObtainContext() {
00202 m_csContext.lock();
00203 XFlush(m_display);
00204 glXMakeCurrent(m_display,m_drawable,m_context);
00205 glXWaitX();
00206 }
00207 static void ReleaseContext() {
00208 glXWaitGL();
00209 glXMakeCurrent(m_display,None,NULL);
00210 m_csContext.unlock();
00211 }
00212 #endif
00213
00214 #ifdef GLUTIO_WIN32
00215 static void ObtainContext() { m_csContext.lock(); wglMakeCurrent(m_hdc,m_context); }
00216 static void ReleaseContext() { wglMakeCurrent(NULL,NULL); m_csContext.unlock(); }
00217 #endif
00218
00220 };
00221
00222
00224
00226
00230 class CKeyboardEvent : public fsf::CNode {
00231 private:
00232 unsigned char m_key;
00233 int m_special;
00234 int m_modif;
00235 int m_x;
00236 int m_y;
00237 public:
00238
00240
00241
00242 CKeyboardEvent() : CNode(), m_key(0), m_special(-1), m_modif(0), m_x(-1), m_y(-1) {}
00244 CKeyboardEvent(fsf::CNode *pParent, fsf::Time tTime=0) : CNode(pParent,tTime), m_key(0), m_special(-1), m_modif(0), m_x(-1), m_y(-1) {}
00246 CKeyboardEvent(std::string &strName, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00247 : CNode(strName,pParent,tTime), m_key(0), m_special(-1), m_modif(0), m_x(-1), m_y(-1) {}
00249 CKeyboardEvent(unsigned char key, int modif, int x, int y, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00250 : CNode(pParent,tTime), m_key(key), m_special(-1), m_modif(modif), m_x(x), m_y(y) {}
00252 CKeyboardEvent(std::string &strName, unsigned char key, int modif, int x, int y, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00253 : CNode(strName,pParent,tTime), m_key(key), m_special(-1), m_modif(modif), m_x(x), m_y(y) {}
00255 CKeyboardEvent(int special, int modif, int x, int y, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00256 : CNode(pParent,tTime), m_key(0), m_special(special), m_modif(modif), m_x(x), m_y(y) {}
00258 CKeyboardEvent(std::string &strName, int special, int modif, int x, int y, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00259 : CNode(strName,pParent,tTime), m_key(0), m_special(special), m_modif(modif), m_x(x), m_y(y) {}
00261 CKeyboardEvent(const CKeyboardEvent &rhs) : CNode(rhs) {
00262 m_key=rhs.m_key; m_special=rhs.m_special; m_modif=rhs.m_modif; m_x=rhs.m_x; m_y=rhs.m_y;
00263 }
00264
00265 virtual fsf::CNode *clone() const { return new CKeyboardEvent(*this); }
00267
00269
00270
00271 CKeyboardEvent& operator=(const CKeyboardEvent&);
00272
00274 void setKey(unsigned char key) { m_key=key; }
00276 void setSpecial(int special) { m_special=special; }
00278 void setModifier(int modif) { m_modif=modif; }
00280 void setX(int x) { m_x=x; }
00282 void setY(int y) { m_y=y; }
00283
00285 bool isSpecial() { return (m_special>0); }
00287
00289
00290
00291 virtual void getTypeID(std::string &str) const { str.assign("GLUTIO_KEYBOARD_EVENT"); }
00292
00294 unsigned char getKey() const { return m_key; }
00296 int getSpecial() const { return m_special; }
00298 int getModifier() const { return m_modif; }
00300 int getX() const { return m_x; }
00302 int getY() const { return m_y; }
00304 };
00305
00309 class CListenKeyboard : public fsf::CCell {
00310 public:
00312 CListenKeyboard();
00314 virtual void getTypeID(std::string &str) const { str.assign("GLUTIO_LISTEN_KEYBOARD"); }
00316 void makePulse(CKeyboardEvent *pNode);
00317 };
00318
00319
00320
00322
00324
00327 class CMouseEvent : public fsf::CNode {
00328 private:
00329 int m_button;
00330 int m_state;
00331 int m_x;
00332 int m_y;
00333 public:
00335
00336
00337 CMouseEvent() : CNode(), m_button(-1), m_state(-1), m_x(-1), m_y(-1) {}
00339 CMouseEvent(fsf::CNode *pParent, fsf::Time tTime=0) : CNode(pParent,tTime), m_button(-1), m_state(-1), m_x(-1), m_y(-1) {}
00341 CMouseEvent(std::string &strName, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00342 : CNode(strName,pParent,tTime), m_button(-1), m_state(-1), m_x(-1), m_y(-1) {}
00344 CMouseEvent(int button, int state, int x, int y, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00345 : CNode(pParent,tTime), m_button(button), m_state(state), m_x(x), m_y(y) {}
00347 CMouseEvent(std::string &strName, int button, int state, int x, int y, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00348 : CNode(strName,pParent,tTime), m_button(button), m_state(state), m_x(x), m_y(y) {}
00350 CMouseEvent(const CMouseEvent &rhs) : CNode(rhs) {
00351 m_button=rhs.m_button; m_state=rhs.m_state; m_x=rhs.m_x; m_y=rhs.m_y;
00352 }
00354
00356
00357
00358 CMouseEvent& operator=(const CMouseEvent&);
00360 void setButton(int button) { m_button=button; }
00362 void setState(int state) { m_state=state; }
00364 void setX(int x) { m_x=x; }
00366 void setY(int y) { m_y=y; }
00368
00370
00371
00372 virtual fsf::CNode *clone() const { return new CMouseEvent(*this); }
00374 virtual void getTypeID(std::string &str) const { str.assign("GLUTIO_MOUSE_EVENT"); }
00376 int getButton() const { return m_button; }
00378 int getState() const { return m_state; }
00380 int getX() const { return m_x; }
00382 int getY() const { return m_y; }
00383 };
00384
00387 class CListenMouse : public fsf::CCell {
00388 public:
00390 CListenMouse();
00392 virtual void getTypeID(std::string &str) const { str.assign("GLUTIO_LISTEN_MOUSE"); }
00394 void makePulse(CMouseEvent *pNode);
00395 };
00396
00397
00399
00401
00404 class CJoystickState : public fsf::CNode {
00405 private:
00406 int m_buttonMask;
00407 int m_x;
00408 int m_y;
00409 int m_z;
00410 public:
00412
00413
00414 CJoystickState() : CNode(), m_buttonMask(0), m_x(0), m_y(0), m_z(0) {}
00416 CJoystickState(fsf::CNode *pParent, fsf::Time tTime=0) : CNode(pParent,tTime), m_buttonMask(0), m_x(0), m_y(0), m_z(0) {}
00418 CJoystickState(std::string &strName, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00419 : CNode(strName,pParent,tTime), m_buttonMask(0), m_x(0), m_y(0), m_z(0) {}
00421 CJoystickState(int buttonMask, int x, int y, int z, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00422 : CNode(pParent,tTime), m_buttonMask(buttonMask), m_x(x), m_y(y), m_z(z) {}
00424 CJoystickState(std::string &strName, int buttonMask, int x, int y, int z, fsf::CNode *pParent=NULL, fsf::Time tTime=0)
00425 : CNode(strName,pParent,tTime), m_buttonMask(buttonMask), m_x(x), m_y(y), m_z(z) {}
00427 CJoystickState(const CJoystickState &rhs) : CNode(rhs) {
00428 m_buttonMask=rhs.m_buttonMask; m_x=rhs.m_x; m_y=rhs.m_y; m_z=rhs.m_z;
00429 }
00431 virtual fsf::CNode *clone() const { return new CJoystickState(*this); }
00433
00435
00436
00437 CJoystickState& operator=(const CJoystickState&);
00438
00439 void setButtonMask(int buttonMask) { m_buttonMask=buttonMask; }
00440 void setX(int x) { m_x=x; }
00441 void setY(int y) { m_y=y; }
00442 void setZ(int z) { m_z=z; }
00444
00445
00447
00448
00449 virtual void getTypeID(std::string &str) const { str.assign("GLUTIO_JOYSTICK_STATE"); }
00450
00451
00452 int getButtonMask() const { return m_buttonMask; }
00453 int getX() const { return m_x; }
00454 int getY() const { return m_y; }
00455 int getZ() const { return m_z; }
00457
00458 };
00459
00462 class CListenJoystick : public fsf::CCell {
00463 public:
00465 CListenJoystick();
00467 virtual void getTypeID(std::string &str) const { str.assign("GLUTIO_LISTEN_JOYSTICK"); }
00469 void makePulse(CJoystickState *pNode);
00470 };
00471
00472
00473
00474
00475
00477
00479
00480
00483 class CDisplayRaster : public fsf::CCell {
00484 public:
00486 CDisplayRaster();
00487
00488 virtual void getTypeID(std::string &str) { str.assign("GLUTIO_DISPLAY_RASTER"); }
00489
00490 virtual void process(fsf::CPassiveHandle *pPassiveHandle, fsf::CActiveHandle *pActiveHandle, fsf::CActivePulse *pActivePulse);
00491 };
00492
00493
00495
00496 class CSurface : public fsf::CNode {
00497 private:
00498 fsf::CMutex m_csSurface;
00499
00500
00501 double m_x;
00502 double m_y;
00503 double m_w;
00504 double m_h;
00505
00506
00507
00508 double m_u1;
00509 double m_v1;
00510 double m_u2;
00511 double m_v2;
00512
00513 GLuint m_texture;
00514
00515 public:
00517
00518
00519 CSurface();
00521 CSurface(fsf::CNode *pParent, fsf::Time tTime=0);
00523 CSurface(const std::string &strName, fsf::CNode *pParent=NULL, fsf::Time tTime=0);
00525 CSurface(const CSurface&);
00527 virtual ~CSurface();
00529 virtual fsf::CNode *clone() const { return new CSurface(*this); }
00531
00533
00534
00535 CSurface& operator=(const CSurface&);
00537 void setCanvas(double x, double y, double w, double h){
00538 m_x=x;
00539 m_y=y;
00540 m_w=w;
00541 m_h=h;
00542 }
00544 void setTexture(double u1, double v1, double u2, double v2){
00545 m_u1=u1;
00546 m_v1=v1;
00547 m_u2=u2;
00548 m_v2=v2;
00549 }
00551 void refreshTexture(image::CImage *pImage);
00553 void draw();
00555
00557
00558
00559 virtual void getTypeID(string &str) const { str.assign("GLUTIO_SURFACE"); }
00561 double getX() const { return m_x; }
00563 double getY() const { return m_y; }
00565 double getW() const { return m_w; }
00567 double getH() const { return m_h; }
00569
00570 };
00571
00574 class CDisplaySurface : public fsf::CCell {
00575 public:
00577 CDisplaySurface();
00578
00579 virtual void getTypeID(std::string &str) const { str.assign("GLUTIO_DISPLAY_SURFACE"); }
00580
00581 virtual void process(fsf::CPassiveHandle *pPassiveHandle, fsf::CActiveHandle *pActiveHandle, fsf::CActivePulse *pActivePulse);
00582 };
00583
00584 }
00585
00586 #endif