summaryrefslogtreecommitdiff
path: root/lib/engine.cc
diff options
context:
space:
mode:
Diffstat (limited to 'lib/engine.cc')
-rw-r--r--lib/engine.cc90
1 files changed, 83 insertions, 7 deletions
diff --git a/lib/engine.cc b/lib/engine.cc
index 6e899e2..3a2aaa0 100644
--- a/lib/engine.cc
+++ b/lib/engine.cc
@@ -20,9 +20,27 @@ mogltk::glbase * mogltk::engine::glbase_o = 0;
mogltk::base * mogltk::engine::base_o = 0;
mogltk::engine::keyevent * mogltk::engine::keyevent_h = 0;
+mogltk::engine::mouseevent * mogltk::engine::mouseevent_h = 0;
#define UPDATERATE 1000
+mogltk::engine::keyevent::keyevent() {
+ new_handler = 0;
+ if ((old_handler = getkeyevent()))
+ old_handler->new_handler = this;
+ setkeyevent(this);
+}
+
+mogltk::engine::keyevent::~keyevent() {
+ if (new_handler)
+ new_handler->old_handler = old_handler;
+ if (old_handler)
+ old_handler->new_handler = new_handler;
+
+ if (getkeyevent() == this)
+ setkeyevent(old_handler);
+}
+
void mogltk::engine::keyevent::up(SDL_keysym) {
printm(M_INFO, "Generic keyevent::up called\n");
}
@@ -31,6 +49,31 @@ void mogltk::engine::keyevent::down(SDL_keysym) {
printm(M_INFO, "Generic keyevent::down called\n");
}
+mogltk::engine::mouseevent::mouseevent() {
+ new_handler = 0;
+ old_handler = getmouseevent();
+ old_handler->new_handler = this;
+ setmouseevent(this);
+}
+
+mogltk::engine::mouseevent::~mouseevent() {
+ if (new_handler)
+ new_handler->old_handler = old_handler;
+ if (old_handler)
+ old_handler->new_handler = new_handler;
+
+ if (getmouseevent() == this)
+ setmouseevent(old_handler);
+}
+
+void mogltk::engine::mouseevent::move(SDL_MouseMotionEvent) {
+ printm(M_INFO, "Generic mouseevent::move called\n");
+}
+
+void mogltk::engine::mouseevent::action(SDL_MouseButtonEvent) {
+ printm(M_INFO, "Generic mouseevent::action called\n");
+}
+
int mogltk::engine::setup() throw(GeneralException) {
if (inited) {
printm(M_WARNING, FUNCNAME + _(" called twice, ignoring second call.\n"));
@@ -154,7 +197,7 @@ void mogltk::engine::pollevents() throw (GeneralException) {
}
while(true) {
- if (hastoreturn)
+ if (hastoreturn || quitrequest)
if (!SDL_PollEvent(NULL)) {
updatemouse();
return;
@@ -186,8 +229,6 @@ void mogltk::engine::pollevents() throw (GeneralException) {
case SDL_KEYUP:
printm(M_INFO, String("Key ") + event.key.keysym.scancode + " on keyboard " + event.key.which + (event.key.state == SDL_PRESSED ? " pressed" : " released") + "\n");
printm(M_INFO, "SDL keysym: %i - Unicode: %04x = `%c`- modifiers: %04x\n", event.key.keysym.sym, event.key.keysym.unicode, event.key.keysym.unicode, event.key.keysym.mod);
- if (event.key.keysym.sym == 27)
- hastoreturn = quitrequest = true;
if (keyevent_h) {
if (event.key.state == SDL_PRESSED) {
keyevent_h->down(event.key.keysym);
@@ -205,18 +246,24 @@ void mogltk::engine::pollevents() throw (GeneralException) {
}
if (cursorvisible)
hastoreturn = true;
+ if (mouseevent_h) {
+ mouseevent_h->move(event.motion);
+ }
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
printm(M_INFO, String().set("Mouse button %02x ", event.button.button) + String((event.button.state == SDL_PRESSED ? "pressed" : "released")) + " at (" + event.button.x + ", " + event.button.y + ")\n");
- if (event.button.state == SDL_PRESSED) {
- if (event.button.button == 4) {
+ if (event.button.state == SDL_PRESSED) {
+ if (event.button.button == SDL_BUTTON_WHEELUP) {
mz--;
}
- if (event.button.button == 5) {
+ if (event.button.button == SDL_BUTTON_WHEELDOWN) {
mz++;
}
}
+ if (mouseevent_h) {
+ mouseevent_h->action(event.button);
+ }
break;
case SDL_QUIT:
printm(M_INFO, "Quit requested\n");
@@ -249,6 +296,10 @@ bool mogltk::engine::getcursorvisible() {
return cursorvisible;
}
+void mogltk::engine::quit() {
+ quitrequest = true;
+}
+
bool mogltk::engine::quitrequested() {
return quitrequest;
}
@@ -269,6 +320,20 @@ int mogltk::engine::mouseZ() {
return mz;
}
+void mogltk::engine::setmouseX(int _mx) {
+ mx = _mx;
+ SDL_WarpMouse(mx, my);
+}
+
+void mogltk::engine::setmouseY(int _my) {
+ my = _my;
+ SDL_WarpMouse(mx, my);
+}
+
+void mogltk::engine::setmouseZ(int _mz) {
+ mz = _mz;
+}
+
int mogltk::engine::mousebuttons() {
return mbuttons;
}
@@ -286,6 +351,17 @@ void mogltk::engine::unlockmouse() {
}
void mogltk::engine::setkeyevent(keyevent * k) {
- printm(M_INFO, "Setting up a new keyevent\n");
keyevent_h = k;
}
+
+void mogltk::engine::setmouseevent(mouseevent * m) {
+ mouseevent_h = m;
+}
+
+mogltk::engine::keyevent * mogltk::engine::getkeyevent() {
+ return keyevent_h;
+}
+
+mogltk::engine::mouseevent * mogltk::engine::getmouseevent() {
+ return mouseevent_h;
+}