Pass the display on events.
This commit is contained in:
parent
7e755a78c9
commit
b72b6c8ac7
6 changed files with 128 additions and 27 deletions
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -27,13 +27,18 @@
|
|||
|
||||
@protocol OGKEventQueueDelegate <OFObject>
|
||||
@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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,7 @@
|
|||
OGKDisplay *display;
|
||||
OGKEventQueue *eventQueue;
|
||||
OGKBitmap *bitmap;
|
||||
of_point_t position;
|
||||
BOOL running;
|
||||
}
|
||||
@end
|
||||
|
|
|
@ -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];
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue