From b72b6c8ac77c07e242d382c94687cf8d45542a7b Mon Sep 17 00:00:00 2001 From: Jonathan Schleifer Date: Mon, 20 Aug 2012 03:18:05 +0200 Subject: [PATCH] Pass the display on events. --- src/OGKDisplay.h | 1 + src/OGKDisplay.m | 44 +++++++++++++++++++++++++ src/OGKEventQueue.h | 19 +++++++---- src/OGKEventQueue.m | 78 ++++++++++++++++++++++++++++++++++----------- test/TestMain.h | 1 + test/TestMain.m | 12 +++++-- 6 files changed, 128 insertions(+), 27 deletions(-) diff --git a/src/OGKDisplay.h b/src/OGKDisplay.h index d43f744..f6d24c8 100644 --- a/src/OGKDisplay.h +++ b/src/OGKDisplay.h @@ -30,6 +30,7 @@ @property (assign) of_point_t windowPosition; @property (assign) of_dimension_t size; ++ OGK_displayForAllegroDisplay: (ALLEGRO_DISPLAY*)display; - initWithSize: (of_dimension_t)size position: (of_point_t)position fullscreen: (BOOL)fullscreen diff --git a/src/OGKDisplay.m b/src/OGKDisplay.m index 55676bf..b348e4b 100644 --- a/src/OGKDisplay.m +++ b/src/OGKDisplay.m @@ -20,6 +20,10 @@ #import "OGKDisplay.h" +static OFMutex *mutex = nil; +static OFMutableArray *displays = nil; +static OFDataArray *allegroDisplays = nil; + @implementation OGKDisplay + (void)initialize { @@ -29,6 +33,28 @@ if (!al_install_system(ALLEGRO_VERSION_INT, NULL)) @throw [OFInitializationFailedException exceptionWithClass: self]; + + mutex = [[OFMutex alloc] init]; + displays = [[OFMutableArray alloc] init]; + allegroDisplays = [[OFDataArray alloc] + initWithItemSize: sizeof(ALLEGRO_DISPLAY*)]; +} + ++ OGK_displayForAllegroDisplay: (ALLEGRO_DISPLAY*)display +{ + [mutex lock]; + @try { + ALLEGRO_DISPLAY **cArray = [allegroDisplays cArray]; + size_t i, count = [allegroDisplays count]; + + for (i = 0; i < count; i++) + if (cArray[i] == display) + return [displays objectAtIndex: i]; + } @finally { + [mutex unlock]; + } + + return nil; } - initWithSize: (of_dimension_t)size @@ -60,11 +86,29 @@ @throw [OFInitializationFailedException exceptionWithClass: [self class]]; + [mutex lock]; + @try { + [allegroDisplays addItem: &display]; + [displays addObject: self]; + } @finally { + [mutex unlock]; + } + return self; } - (void)dealloc { + [mutex lock]; + @try { + size_t index = [displays indexOfObject: self]; + + [allegroDisplays removeItemAtIndex: index]; + [displays removeObjectAtIndex: index]; + } @finally { + [mutex unlock]; + } + if (display != NULL) al_destroy_display(display); } diff --git a/src/OGKEventQueue.h b/src/OGKEventQueue.h index 3599852..e2dd320 100644 --- a/src/OGKEventQueue.h +++ b/src/OGKEventQueue.h @@ -27,13 +27,18 @@ @protocol OGKEventQueueDelegate @optional -- (void)displayWasClosed: (OGKCloseEvent*)event; -// FIXME: Those need to get the OGKDisplay passed! -- (void)keyWasPressed: (OGKKeyPressEvent*)event; -- (void)keyWasReleased: (OGKKeyReleaseEvent*)event; -- (void)mouseWasMoved: (OGKMouseMovedEvent*)event; -- (void)mouseButtonWasPressed: (OGKMouseButtonPressedEvent*)event; -- (void)mouseButtonWasReleased: (OGKMouseButtonReleasedEvent*)event; +- (void)display: (OGKDisplay*)display + wasClosed: (OGKCloseEvent*)event; +- (void)keyWasPressed: (OGKKeyPressEvent*)event + display: (OGKDisplay*)display; +- (void)keyWasReleased: (OGKKeyReleaseEvent*)event + display: (OGKDisplay*)display; +- (void)mouseWasMoved: (OGKMouseMovedEvent*)event + display: (OGKDisplay*)display; +- (void)mouseButtonWasPressed: (OGKMouseButtonPressedEvent*)event + display: (OGKDisplay*)display; +- (void)mouseButtonWasReleased: (OGKMouseButtonReleasedEvent*)event + display: (OGKDisplay*)display; @end @interface OGKEventQueue: OFObject diff --git a/src/OGKEventQueue.m b/src/OGKEventQueue.m index ba9e703..b28e498 100644 --- a/src/OGKEventQueue.m +++ b/src/OGKEventQueue.m @@ -63,36 +63,64 @@ static int mouse_retain_count = 0; object_setClass(event, [OGKCloseEvent class]); if ([delegate respondsToSelector: - @selector(displayWasClosed:)]) - [delegate displayWasClosed: - (OGKCloseEvent*)event]; + @selector(display:wasClosed:)]) { + OGKDisplay *display = [OGKDisplay + OGK_displayForAllegroDisplay: + allegroEvent->display.source]; + OGKCloseEvent *closeEvent = + (OGKCloseEvent*)event; + + [delegate display: display + wasClosed: closeEvent]; + } break; case ALLEGRO_EVENT_KEY_DOWN: object_setClass(event, [OGKKeyPressEvent class]); if ([delegate respondsToSelector: - @selector(keyWasPressed:)]) - [delegate keyWasPressed: - (OGKKeyPressEvent*)event]; + @selector(keyWasPressed:display:)]) { + OGKDisplay *display = [OGKDisplay + OGK_displayForAllegroDisplay: + allegroEvent->keyboard.display]; + OGKKeyPressEvent *keyPressEvent = + (OGKKeyPressEvent*)event; + + [delegate keyWasPressed: keyPressEvent + display: display]; + } break; case ALLEGRO_EVENT_KEY_UP: object_setClass(event, [OGKKeyReleaseEvent class]); if ([delegate respondsToSelector: - @selector(keyWasReleased:)]) - [delegate keyWasReleased: - (OGKKeyReleaseEvent*)event]; + @selector(keyWasReleased:display:)]) { + OGKDisplay *display = [OGKDisplay + OGK_displayForAllegroDisplay: + allegroEvent->keyboard.display]; + OGKKeyReleaseEvent *keyReleaseEvent = + (OGKKeyReleaseEvent*)event; + + [delegate keyWasReleased: keyReleaseEvent + display: display]; + } break; case ALLEGRO_EVENT_MOUSE_AXES: object_setClass(event, [OGKMouseMovedEvent class]); if ([delegate respondsToSelector: - @selector(mouseWasMoved:)]) - [delegate mouseWasMoved: - (OGKMouseMovedEvent*)event]; + @selector(mouseWasMoved:display:)]) { + OGKDisplay *display = [OGKDisplay + OGK_displayForAllegroDisplay: + allegroEvent->mouse.display]; + OGKMouseMovedEvent *mouseMovedEvent = + (OGKMouseMovedEvent*)event; + + [delegate mouseWasMoved: mouseMovedEvent + display: display]; + } break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: @@ -100,9 +128,16 @@ static int mouse_retain_count = 0; [OGKMouseButtonPressedEvent class]); if ([delegate respondsToSelector: - @selector(mouseButtonWasPressed:)]) - [delegate mouseButtonWasPressed: - (OGKMouseButtonPressedEvent*)event]; + @selector(mouseButtonWasPressed:display:)]) { + OGKDisplay *display = [OGKDisplay + OGK_displayForAllegroDisplay: + allegroEvent->mouse.display]; + OGKMouseButtonPressedEvent *pressedEvent = + (OGKMouseButtonPressedEvent*)event; + + [delegate mouseButtonWasPressed: pressedEvent + display: display]; + } break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: @@ -110,9 +145,16 @@ static int mouse_retain_count = 0; [OGKMouseButtonReleasedEvent class]); if ([delegate respondsToSelector: - @selector(mouseButtonWasReleased:)]) - [delegate mouseButtonWasReleased: - (OGKMouseButtonReleasedEvent*)event]; + @selector(mouseButtonWasReleased:display:)]) { + OGKDisplay *display = [OGKDisplay + OGK_displayForAllegroDisplay: + allegroEvent->mouse.display]; + OGKMouseButtonReleasedEvent *releasedEvent = + (OGKMouseButtonReleasedEvent*)event; + + [delegate mouseButtonWasReleased: releasedEvent + display: display]; + } break; } diff --git a/test/TestMain.h b/test/TestMain.h index bac382d..22eb057 100644 --- a/test/TestMain.h +++ b/test/TestMain.h @@ -29,6 +29,7 @@ OGKDisplay *display; OGKEventQueue *eventQueue; OGKBitmap *bitmap; + of_point_t position; BOOL running; } @end diff --git a/test/TestMain.m b/test/TestMain.m index d65767a..3aa5dbf 100644 --- a/test/TestMain.m +++ b/test/TestMain.m @@ -27,31 +27,38 @@ OF_APPLICATION_DELEGATE(TestMain) @implementation TestMain -- (void)displayWasClosed: (OGKCloseEvent*)event +- (void)display: (OGKDisplay*)display + wasClosed: (OGKCloseEvent*)event { running = NO; } - (void)keyWasPressed: (OGKKeyPressEvent*)event + display: (OGKDisplay*)display { of_log(@"Pressed: %d", event.keycode); } - (void)keyWasReleased: (OGKKeyReleaseEvent*)event + display: (OGKDisplay*)display { of_log(@"Released: %d", event.keycode); } - (void)mouseWasMoved: (OGKMouseMovedEvent*)event + display: (OGKDisplay*)display { of_log(@"Mouse moved: X=%.f(%.f) Y=%.f(%.f) WX=%.f(%.f) WY=%.f(%.f)", event.cursor.x, event.deltaCursor.x, event.cursor.y, event.deltaCursor.y, event.wheel.x, event.deltaWheel.x, event.wheel.y, event.deltaWheel.y); + + position = event.cursor; } - (void)mouseButtonWasPressed: (OGKMouseButtonPressedEvent*)event + display: (OGKDisplay*)display { of_log(@"Mouse button was pressed: %d (X=%.f Y=%.f WX=%.f WY=%.f)", event.button, event.cursor.x, event.cursor.y, @@ -59,6 +66,7 @@ OF_APPLICATION_DELEGATE(TestMain) } - (void)mouseButtonWasReleased: (OGKMouseButtonPressedEvent*)event + display: (OGKDisplay*)display { of_log(@"Mouse button was released: %d (X=%.f Y=%.f WX=%.f WY=%.f)", event.button, event.cursor.x, event.cursor.y, @@ -73,7 +81,7 @@ OF_APPLICATION_DELEGATE(TestMain) - (void)draw { [OGKBitmap clearToColor: OGK_COLOR_BLACK]; - [bitmap drawAtPosition: of_point(160, 120)]; + [bitmap drawAtPosition: position]; [display update]; }