ObjGameKit  Check-in [c6c34e7b99]

Overview
Comment:Initial import.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | descendants | trunk
Files: files | file ages | folders
SHA3-256: c6c34e7b9977fd8fc5f999711560b6013415ccba19107e2fa89ac467a1d2d639
User & Date: js on 2012-08-19 21:53:47
Other Links: manifest | tags
Context
2012-08-19
22:49
Add OGKBitmap. check-in: 2a081e62c9 user: js tags: trunk
21:53
Initial import. check-in: c6c34e7b99 user: js tags: trunk
Changes

Added Makefile version [9b7eb118a8].

















>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
all:
	@cd src && ${MAKE} ${MFLAGS}
	@cd test && ${MAKE} ${MFLAGS}

clean:
	@cd src && ${MAKE} ${MFLAGS} clean
	@cd test && ${MAKE} ${MFLAGS} clean
	@rm -f *~

Added src/Makefile version [a545af16af].















>
>
>
>
>
>
>
1
2
3
4
5
6
7
all:
	@mkdir -p build
	@objfw-compile --lib 0.0 -o objgamekit --builddir build *.m \
		--arc `pkg-config --cflags --libs allegro-5.0 allegro_main-5.0`

clean:
	@rm -fr build libobjgamekit.* *~

Added src/OGKDisplay.h version [575fe769bc].











































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
 * Copyright (c) 2012 Jonathan Schleifer <js@webkeks.org>
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *   1.) The origin of this software must not be misrepresented; you must not
 *       claim that you wrote the original software. If you use this software
 *       in a product, an acknowledgment in the product documentation would be
 *       appreciated but is not required.
 *   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.
 */

#include <allegro5/allegro.h>

#import <ObjFW/ObjFW.h>

@interface OGKDisplay: OFObject
{
	ALLEGRO_DISPLAY *display;
}

+ displayWithSize: (of_dimension_t)size
       fullscreen: (BOOL)fullscreen
	resizable: (BOOL)resizable;
- initWithSize: (of_dimension_t)size
    fullscreen: (BOOL)fullscreen
     resizable: (BOOL)resizable;
- (ALLEGRO_DISPLAY*)OGK_allegroDisplay;
@end

Added src/OGKDisplay.m version [78f573a0bd].





































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
 * Copyright (c) 2012 Jonathan Schleifer <js@webkeks.org>
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *   1.) The origin of this software must not be misrepresented; you must not
 *       claim that you wrote the original software. If you use this software
 *       in a product, an acknowledgment in the product documentation would be
 *       appreciated but is not required.
 *   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"

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

	if (!al_install_system(ALLEGRO_VERSION_INT, NULL))
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
}

+ displayWithSize: (of_dimension_t)size
       fullscreen: (BOOL)fullscreen
	resizable: (BOOL)resizable
{
	return [[self alloc] initWithSize: size
			       fullscreen: fullscreen
				resizable: resizable];
}

- initWithSize: (of_dimension_t)size
    fullscreen: (BOOL)fullscreen
     resizable: (BOOL)resizable
{
	int flags = 0;

	self = [super init];

#if 0
	/* TODO: Find a nice way to set these when requested */
	flags |= ALLEGRO_OPENGL_3_0;
	flags |= ALLEGRO_OPENGL_FORWARD_COMPATIBLE;
#endif

	if (fullscreen)
		flags |= ALLEGRO_FULLSCREEN;
	else if (resizable)
		flags |= ALLEGRO_RESIZABLE;

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

	if (display == NULL)
		@throw [OFInitializationFailedException
		    exceptionWithClass: [self class]];

	return self;
}

- (void)dealloc
{
	if (display != NULL)
		al_destroy_display(display);
}

- (ALLEGRO_DISPLAY*)OGK_allegroDisplay
{
	return display;
}
@end

Added src/OGKEvent.h version [19b5dab082].







































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
65
66
67
/*
 * Copyright (c) 2012 Jonathan Schleifer <js@webkeks.org>
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *   1.) The origin of this software must not be misrepresented; you must not
 *       claim that you wrote the original software. If you use this software
 *       in a product, an acknowledgment in the product documentation would be
 *       appreciated but is not required.
 *   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.
 */

#include <allegro5/allegro.h>

#import <ObjFW/ObjFW.h>

typedef enum ogk_event_type_t {
	OGK_EVENT_TYPE_CLOSE = 1
} ogk_event_type_t;

@interface OGKEvent: OFObject
{
	ALLEGRO_EVENT event;
}

- (ALLEGRO_EVENT*)OGK_allegroEvent;
@end

@interface OGKCloseEvent: OGKEvent
@end

@interface OGKKeyboardEvent: OGKEvent
@property (readonly, assign) int keycode;
@end

@interface OGKKeyPressEvent: OGKKeyboardEvent
@end

@interface OGKKeyReleaseEvent: OGKKeyboardEvent
@end

@interface OGKMouseEvent: OGKEvent
@property (readonly, assign) of_point_t cursor;
@property (readonly, assign) of_point_t wheel;
@end

@interface OGKMouseMovedEvent: OGKMouseEvent
@property (readonly, assign) of_point_t deltaCursor, deltaWheel;
@end

@interface OGKMouseButtonEvent: OGKMouseEvent
@property (readonly, assign) unsigned button;
@end

@interface OGKMouseButtonPressedEvent: OGKMouseButtonEvent
@end

@interface OGKMouseButtonReleasedEvent: OGKMouseButtonEvent
@end

Added src/OGKEvent.m version [5e13753beb].



































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (c) 2012 Jonathan Schleifer <js@webkeks.org>
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *   1.) The origin of this software must not be misrepresented; you must not
 *       claim that you wrote the original software. If you use this software
 *       in a product, an acknowledgment in the product documentation would be
 *       appreciated but is not required.
 *   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 "OGKEvent.h"

@implementation OGKEvent
- (ALLEGRO_EVENT*)OGK_allegroEvent
{
	return &event;
}
@end

@implementation OGKCloseEvent
@end

@implementation OGKKeyboardEvent
- (int)keycode
{
	return event.keyboard.keycode;
}
@end

@implementation OGKKeyPressEvent
@end

@implementation OGKKeyReleaseEvent
@end

@implementation OGKMouseEvent
- (of_point_t)cursor
{
	return of_point(event.mouse.x, event.mouse.y);
}

- (of_point_t)wheel
{
	return of_point(event.mouse.w, event.mouse.z);
}
@end

@implementation OGKMouseMovedEvent
- (of_point_t)deltaCursor
{
	return of_point(event.mouse.dx, event.mouse.dy);
}

- (of_point_t)deltaWheel
{
	return of_point(event.mouse.dw, event.mouse.dz);
}
@end

@implementation OGKMouseButtonEvent
- (unsigned)button
{
	return event.mouse.button;
}
@end

@implementation OGKMouseButtonPressedEvent
@end

@implementation OGKMouseButtonReleasedEvent
@end

Added src/OGKEventQueue.h version [e90f6c2f9f].















































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
/*
 * Copyright (c) 2012 Jonathan Schleifer <js@webkeks.org>
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *   1.) The origin of this software must not be misrepresented; you must not
 *       claim that you wrote the original software. If you use this software
 *       in a product, an acknowledgment in the product documentation would be
 *       appreciated but is not required.
 *   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.
 */

#include <allegro5/allegro.h>

#import <ObjFW/ObjFW.h>

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

@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;
@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;
}

@property (unsafe_unretained) id <OGKEventQueueDelegate> delegate;

- (void)handleNextEvent;
- (void)registerDisplay: (OGKDisplay*)display;
- (void)unregisterDisplay: (OGKDisplay*)display;
- (void)registerKeyboard;
- (void)unregisterKeyboard;
- (void)registerMouse;
- (void)unregisterMouse;
@end

Added src/OGKEventQueue.m version [8d48ecf372].



































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 * Copyright (c) 2012 Jonathan Schleifer <js@webkeks.org>
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *   1.) The origin of this software must not be misrepresented; you must not
 *       claim that you wrote the original software. If you use this software
 *       in a product, an acknowledgment in the product documentation would be
 *       appreciated but is not required.
 *   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 "OGKEventQueue.h"
#import "OGKEvent.h"
#import "OGKDisplay.h"

static int keyboard_retain_count = 0;
static int mouse_retain_count = 0;

@implementation OGKEventQueue
@synthesize delegate;

+ (void)initialize
{
	if (self != [OGKEventQueue class])
		return;

	if (!al_install_system(ALLEGRO_VERSION_INT, NULL))
		@throw [OFInitializationFailedException
		    exceptionWithClass: self];
}

- init
{
	self = [super init];

	eventQueue = al_create_event_queue();

	return self;
}

- (void)dealloc
{
	al_destroy_event_queue(eventQueue);
}

- (void)handleNextEvent
{
	OGKEvent *event = [[OGKEvent alloc] init];
	ALLEGRO_EVENT *allegroEvent = [event OGK_allegroEvent];

	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];

			break;
		case ALLEGRO_EVENT_KEY_DOWN:
			object_setClass(event, [OGKKeyPressEvent class]);

			if ([delegate respondsToSelector:
			    @selector(keyWasPressed:)])
				[delegate keyWasPressed:
				    (OGKKeyPressEvent*)event];

			break;
		case ALLEGRO_EVENT_KEY_UP:
			object_setClass(event, [OGKKeyReleaseEvent class]);

			if ([delegate respondsToSelector:
			    @selector(keyWasReleased:)])
				[delegate keyWasReleased:
				    (OGKKeyReleaseEvent*)event];

			break;
		case ALLEGRO_EVENT_MOUSE_AXES:
			object_setClass(event, [OGKMouseMovedEvent class]);

			if ([delegate respondsToSelector:
			    @selector(mouseWasMoved:)])
				[delegate mouseWasMoved:
				    (OGKMouseMovedEvent*)event];

			break;
		case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN:
			object_setClass(event,
			    [OGKMouseButtonPressedEvent class]);

			if ([delegate respondsToSelector:
			    @selector(mouseButtonWasPressed:)])
				[delegate mouseButtonWasPressed:
				    (OGKMouseButtonPressedEvent*)event];

			break;
		case ALLEGRO_EVENT_MOUSE_BUTTON_UP:
			object_setClass(event,
			    [OGKMouseButtonReleasedEvent class]);

			if ([delegate respondsToSelector:
			    @selector(mouseButtonWasReleased:)])
				[delegate mouseButtonWasReleased:
				    (OGKMouseButtonReleasedEvent*)event];

			break;
		}
	}
}

- (void)registerDisplay: (OGKDisplay*)display
{
	ALLEGRO_EVENT_SOURCE *eventSource;

	eventSource = al_get_display_event_source([display OGK_allegroDisplay]);
	al_register_event_source(eventQueue, eventSource);
}

- (void)unregisterDisplay: (OGKDisplay*)display
{
	ALLEGRO_EVENT_SOURCE *eventSource;

	eventSource = al_get_display_event_source([display OGK_allegroDisplay]);
	al_unregister_event_source(eventQueue, eventSource);
}

- (void)registerKeyboard
{
	of_atomic_inc_int(&keyboard_retain_count);

	if (!al_is_keyboard_installed())
		if (!al_install_keyboard())
			@throw [OFInitializationFailedException
			    exceptionWithClass: [self class]];

	al_register_event_source(eventQueue, al_get_keyboard_event_source());
}

- (void)unregisterKeyboard
{
	al_unregister_event_source(eventQueue, al_get_keyboard_event_source());

	if (of_atomic_dec_int(&keyboard_retain_count) == 0)
		al_uninstall_keyboard();
}

- (void)registerMouse
{
	of_atomic_inc_int(&mouse_retain_count);

	if (!al_is_mouse_installed())
		if (!al_install_mouse())
			@throw [OFInitializationFailedException
			    exceptionWithClass: [self class]];

	al_register_event_source(eventQueue, al_get_mouse_event_source());
}

- (void)unregisterMouse
{
	al_unregister_event_source(eventQueue, al_get_mouse_event_source());

	if (of_atomic_dec_int(&mouse_retain_count))
		al_uninstall_mouse();
}
@end

Added test/Makefile version [ce0acc1603].















>
>
>
>
>
>
>
1
2
3
4
5
6
7
all:
	@objfw-compile -o test --arc *.m \
		-I../src -L../src -lobjgamekit \
		`pkg-config --cflags --libs allegro-5.0 allegro_main-5.0`

clean:
	@rm -f test *.o *~

Added test/TestMain.h version [f8c1ad0d84].

































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * Copyright (c) 2012 Jonathan Schleifer <js@webkeks.org>
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *   1.) The origin of this software must not be misrepresented; you must not
 *       claim that you wrote the original software. If you use this software
 *       in a product, an acknowledgment in the product documentation would be
 *       appreciated but is not required.
 *   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 <ObjFW/ObjFW.h>

#import "OGKDisplay.h"
#import "OGKEventQueue.h"

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

Added test/TestMain.m version [e02b7cbb4f].





























































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
/*
 * Copyright (c) 2012 Jonathan Schleifer <js@webkeks.org>
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from the
 * use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 *   1.) The origin of this software must not be misrepresented; you must not
 *       claim that you wrote the original software. If you use this software
 *       in a product, an acknowledgment in the product documentation would be
 *       appreciated but is not required.
 *   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"
#import "OGKEvent.h"
#import "OGKEventQueue.h"
#import "TestMain.h"

OF_APPLICATION_DELEGATE(TestMain)

@implementation TestMain
- (void)displayWasClosed: (OGKCloseEvent*)event
{
	running = NO;
}

- (void)keyWasPressed: (OGKKeyPressEvent*)event
{
	of_log(@"Pressed: %d", event.keycode);
}

- (void)keyWasReleased: (OGKKeyReleaseEvent*)event
{
	of_log(@"Released: %d", event.keycode);
}

- (void)mouseWasMoved: (OGKMouseMovedEvent*)event
{
	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);
}

- (void)mouseButtonWasPressed: (OGKMouseButtonPressedEvent*)event
{
	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
{
	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)applicationDidFinishLaunching
{
	display = [[OGKDisplay alloc] initWithSize: of_dimension(640, 480)
					fullscreen: NO
					 resizable: NO];
	eventQueue = [[OGKEventQueue alloc] init];
	eventQueue.delegate = self;

	[eventQueue registerDisplay: display];
	[eventQueue registerKeyboard];
	[eventQueue registerMouse];

	for (running = YES; running;) {
		@autoreleasepool {
			[eventQueue handleNextEvent];
		}
	}
}

- (void)applicationWillTerminate
{
	/* Make sure they don't get deallocated after al_uninstall_system() */
	display = nil;
	eventQueue = nil;

	al_uninstall_system();
}
@end