ObjGameKit  Check-in [f309ba2b33]

Overview
Comment:Pass the display on events.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f309ba2b336aff8fa1006a6bb4be85e1c7143141ee426f19c6aa6ba8a839e02b
User & Date: js on 2012-08-20 01:18:05
Other Links: manifest | tags
Context
2012-08-26
10:09
Use flags for -[OGKDisplay init...]. check-in: 40c2d53ff0 user: js tags: trunk
2012-08-20
01:18
Pass the display on events. check-in: f309ba2b33 user: js tags: trunk
2012-08-19
23:08
New OGKDisplay methods. check-in: e1d3ff7a4f user: js tags: trunk
Changes

Modified src/OGKDisplay.h from [25cd74c4c2] to [1753a97d4c].

26
27
28
29
30
31
32

33
34
35
36
37
38
39
40
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41







+








{
	ALLEGRO_DISPLAY *display;
}

@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
     resizable: (BOOL)resizable;
- (void)setWindowTitle: (OFString*)title;
- (void)update;
- (ALLEGRO_DISPLAY*)OGK_allegroDisplay;
@end

Modified src/OGKDisplay.m from [3e2683ad84] to [fbfc273e25].

16
17
18
19
20
21
22




23
24
25
26
27
28
29
30
31






















32
33
34
35
36
37
38
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64







+
+
+
+









+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







 *   2.) Altered source versions must be plainly marked as such, and must not
 *       be misrepresented as being the original software.
 *   3.) This notice may not be removed or altered from any source distribution.
 */

#import "OGKDisplay.h"

static OFMutex *mutex = nil;
static OFMutableArray *displays = nil;
static OFDataArray *allegroDisplays = nil;

@implementation OGKDisplay
+ (void)initialize
{
	if (self != [OGKDisplay class])
		return;

	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
      position: (of_point_t)position
    fullscreen: (BOOL)fullscreen
     resizable: (BOOL)resizable
{
55
56
57
58
59
60
61








62
63
64
65
66
67










68
69
70
71
72
73
74
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118







+
+
+
+
+
+
+
+






+
+
+
+
+
+
+
+
+
+








	al_set_new_display_flags(flags);
	display = al_create_display(size.width, size.height);

	if (display == NULL)
		@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);
}

- (void)setWindowTitle: (OFString*)title
{
	al_set_window_title(display,

Modified src/OGKEventQueue.h from [e90f6c2f9f] to [b38728a4f6].

23
24
25
26
27
28
29

30

31
32
33
34
35
36










37
38
39
40
41
42
43
23
24
25
26
27
28
29
30

31






32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48







+
-
+
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+







#import <ObjFW/ObjFW.h>

#import "OGKEvent.h"
#import "OGKDisplay.h"

@protocol OGKEventQueueDelegate <OFObject>
@optional
- (void)display: (OGKDisplay*)display
- (void)displayWasClosed: (OGKCloseEvent*)event;
      wasClosed: (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)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
{
	ALLEGRO_EVENT_QUEUE *eventQueue;
	/* FIXME: Make this weak once there is support in ObjFW for it */
	__unsafe_unretained id <OGKEventQueueDelegate> delegate;

Modified src/OGKEventQueue.m from [8d48ecf372] to [7219e86adc].

59
60
61
62
63
64
65
66
67
68










69
70
71
72
73
74
75
76
77










78
79
80
81
82
83
84
85
86










87
88
89
90
91
92
93
94
95










96
97
98
99
100
101
102
103
104
105










106
107
108
109
110
111
112
113
114
115










116
117
118
119
120
121
122
59
60
61
62
63
64
65



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164







-
-
-
+
+
+
+
+
+
+
+
+
+






-
-
-
+
+
+
+
+
+
+
+
+
+






-
-
-
+
+
+
+
+
+
+
+
+
+






-
-
-
+
+
+
+
+
+
+
+
+
+







-
-
-
+
+
+
+
+
+
+
+
+
+







-
-
-
+
+
+
+
+
+
+
+
+
+








	while (al_get_next_event(eventQueue, allegroEvent)) {
		switch (allegroEvent->type) {
		case ALLEGRO_EVENT_DISPLAY_CLOSE:
			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:
			object_setClass(event,
			    [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:
			object_setClass(event,
			    [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;
		}
	}
}

- (void)registerDisplay: (OGKDisplay*)display

Modified test/TestMain.h from [0399db4bce] to [150ee79de4].

25
26
27
28
29
30
31

32
33
34
25
26
27
28
29
30
31
32
33
34
35







+



#import "OGKBitmap.h"

@interface TestMain: OFObject <OFApplicationDelegate, OGKEventQueueDelegate>
{
	OGKDisplay *display;
	OGKEventQueue *eventQueue;
	OGKBitmap *bitmap;
	of_point_t position;
	BOOL running;
}
@end

Modified test/TestMain.m from [bc217b5c18] to [003d86a89e].

23
24
25
26
27
28
29

30

31
32
33
34
35

36
37
38
39
40

41
42
43
44
45

46
47
48
49
50
51


52
53
54

55
56
57
58
59
60
61

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

77
78
79
80
81
82
83
23
24
25
26
27
28
29
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

84
85
86
87
88
89
90
91







+
-
+





+





+





+






+
+



+







+














-
+







#import "OGKEventQueue.h"
#import "OGKBitmap.h"
#import "TestMain.h"

OF_APPLICATION_DELEGATE(TestMain)

@implementation TestMain
- (void)display: (OGKDisplay*)display
- (void)displayWasClosed: (OGKCloseEvent*)event
      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,
	    event.wheel.x, event.wheel.y);
}

- (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,
	    event.wheel.x, event.wheel.y);
}

- (void)handleEvents
{
	[eventQueue handleNextEvent];
}

- (void)draw
{
	[OGKBitmap clearToColor: OGK_COLOR_BLACK];
	[bitmap drawAtPosition: of_point(160, 120)];
	[bitmap drawAtPosition: position];
	[display update];
}

- (void)applicationDidFinishLaunching
{
	display = [[OGKDisplay alloc] initWithSize: of_dimension(640, 480)
					  position: of_point(200, 200)