summaryrefslogtreecommitdiff
path: root/src/rb.c
blob: d0630ab2e6ff9a040098f759b2bef1e4cb2e360b (plain)
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
#include <allegro5/allegro5.h>
#include <allegro5/allegro_font.h>
#include <allegro5/allegro_primitives.h>
#include <stdlib.h>
#include <stdio.h>
#include <rb.h>

ALLEGRO_DISPLAY *
init_allegro_display(struct rb *rb)
{
	if (!rb)
		return NULL;

	al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST);
	al_set_new_display_option(ALLEGRO_SAMPLES, 8, ALLEGRO_SUGGEST);
	al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR);

	return al_create_display(rb->width, rb->height);
}

/*
 * Allegro lib functions return 0 in case of error (or NULL), to make
 * it compliant to Unix, return 0 if we succeed, non-zero otherwise
 */
int
initialize_allegro(struct rb **p)
{
	struct rb *rb;

	if (*p != NULL) {
		printf("Allegro already initialized\n");
		return 1;
	}

	rb = malloc(sizeof(struct rb));

	if (!rb)
		return 1;

	rb->width	= RB_WIDTH;
	rb->height	= RB_HEIGHT;
	rb->fps		= RB_FPS;

	if (!al_init())
		goto err_alloc;

	if (!al_install_keyboard())
		goto err_alloc;

	rb->timer = al_create_timer(rb->fps);
	if (!rb->timer)
		goto err_alloc;

	rb->event_queue = al_create_event_queue();
	if (!rb->event_queue)
		goto err_timer;

	rb->font = al_create_builtin_font();
	if (!rb->font)
		goto err_event_queue;

	rb->display = init_allegro_display(rb);
	if (!rb->display)
		goto err_font;

	if (!al_init_primitives_addon())
		goto err_display;

	*p = rb;
	return 0;

err_display:
	al_destroy_display(rb->display);
err_font:
	al_destroy_font(rb->font);
err_event_queue:
	al_destroy_event_queue(rb->event_queue);
err_timer:
	al_destroy_timer(rb->timer);
err_alloc:
	printf("Couldn't initialize Allegro\n");
	free(rb);
	return 1;
}

void
shutdown_allegro(struct rb *rb)
{
	al_destroy_display(rb->display);
	al_destroy_font(rb->font);
	al_destroy_event_queue(rb->event_queue);
	al_destroy_timer(rb->timer);
	free(rb);
}

void
register_events(struct rb *rb)
{
	al_register_event_source(rb->event_queue,
				 al_get_keyboard_event_source());
	al_register_event_source(rb->event_queue,
				 al_get_display_event_source(rb->display));
	al_register_event_source(rb->event_queue,
				 al_get_timer_event_source(rb->timer));
}

void
start_game(struct rb *rb)
{
	bool done	= false;
	bool redraw	= true;
	float x, y;
	ALLEGRO_EVENT	event;

	x = 100;
	y = 100;
	al_start_timer(rb->timer);

	while (1) {
		al_wait_for_event(rb->event_queue, &event);

		switch(event.type) {
		case ALLEGRO_EVENT_TIMER:
			redraw = true;
			break;

		case ALLEGRO_EVENT_KEY_DOWN:
			/* fallthrough */
		case ALLEGRO_EVENT_DISPLAY_CLOSE:
			done = true;
			break;
		}

		if (done)
			break;

		if (redraw && al_is_event_queue_empty(rb->event_queue)) {
			al_clear_to_color(al_map_rgb(0, 0, 0));
			al_draw_textf(rb->font, al_map_rgb(255, 255, 255),
				      0, 0, 0,
				      "X: %.1f Y: %.1f", x, y);
			al_draw_filled_rectangle(x, y, x+10, y+10,
						 al_map_rgb(255, 0, 0));
			al_flip_display();

			redraw = false;
		}
	}
}

int
main(void)
{
	int error;
	struct rb *rb = NULL;

	error = initialize_allegro(&rb);
	if (error)
		exit(EXIT_FAILURE);

	register_events(rb);
	start_game(rb);

	shutdown_allegro(rb);

	return 0;
}