1#include <stdio.h>
2#include <stddef.h>
3#include <stdbool.h>
4#include <allegro5/allegro.h>
5#include <allegro5/allegro_image.h>
6#include <allegro5/allegro_audio.h>
7#include <allegro5/allegro_acodec.h>
8#include <allegro5/allegro_font.h>
9#include <allegro5/allegro_primitives.h>
10
11#define SCREEN_WIDTH 1020
12#define SCREEN_HEIGHT 750
13#define SQUARE 30
14#define FPS 0.0166666666666667
15#define ALPHA 0.5
16#define PI 3.141592653589793
17
18void drawline
(uintptr_t state,
int x,
int y
);
19
20int main
(void) {
21
22 al_init();
23 al_install_keyboard();
24 al_install_audio();
25 al_init_acodec_addon();
26 al_init_image_addon();
27 al_init_primitives_addon();
28
29 ALLEGRO_EVENT_QUEUE *queue
;
30 ALLEGRO_VERTEX v
[2];
31 ALLEGRO_EVENT event
;
32 ALLEGRO_DISPLAY *display
;
33 ALLEGRO_TIMER *timer
;
34 ALLEGRO_TIMER *new_timer
;
35 ALLEGRO_FONT *font;
36
37 int quit
= false;
38 int redraw
= false;
39 int start
= false;
40 int fps
= 0, frameCounter
= 0;
41 int x
= 30;
42 int y
= 30;
43 char *message_start
= "Press SPACE to start";
44 char *message_end
= "Press ESCAPE to exit";
45
46 /*Initializing vertex*/
47 v
[1].color
= al_map_rgba_f(0,
0,
0,
0);
48 v
[1].u
= 0;
49 v
[1].v
= 0;
50 v
[1].x
= 500;
51 v
[1].y
= 300;
52 v
[1].z
= 0;
53 v
[0].color
= al_map_rgba_f(0,
0,
0,
0);
54 v
[0].u
= 0;
55 v
[0].v
= 0;
56 v
[0].x
= 500;
57 v
[0].y
= 400;
58 v
[0].z
= 0;
59
60 queue
= al_create_event_queue();
61 display
= al_create_display(SCREEN_WIDTH, SCREEN_HEIGHT
);
62 timer
= al_create_timer(FPS
);
63 new_timer
= al_create_timer(1);
64 font = al_create_builtin_font
();
65
66 al_register_event_source(queue,
al_get_display_event_source(display
));
67 al_register_event_source(queue,
al_get_timer_event_source(timer
));
68 al_register_event_source(queue,
al_get_timer_event_source(new_timer
));
69 al_register_event_source(queue,
al_get_keyboard_event_source());
70
71 /*creating the mighty pink square*/
72
73 /*initiate display*/
74 al_set_target_bitmap(al_get_backbuffer(display
));
75 al_clear_to_color(al_map_rgb(0,
0,
0));
76 al_draw_text(font,
al_map_rgb(255,
0,
255), SCREEN_WIDTH
/ 2,
77 SCREEN_HEIGHT
/ 2 - 5, ALLEGRO_ALIGN_CENTER, message_start
);
78 al_draw_text(font,
al_map_rgb(255,
0,
255), SCREEN_WIDTH
/ 2,
79 SCREEN_HEIGHT
/ 2 + 5, ALLEGRO_ALIGN_CENTER, message_end
);
80 al_flip_display();
81
82 al_start_timer(timer
);
83 al_start_timer(new_timer
);
84
85 while (!quit
) {
86 if (redraw
&& start
) {
87 al_set_target_bitmap(al_get_backbuffer(display
));
88 al_clear_to_color(al_map_rgb(127,
127,
127));
89 /*PRINT THING HERE*/
90 al_draw_soft_line(&v
[1],
&v
[0],
(uintptr_t
) NULL, NULL, NULL, drawline
);
91 /******************/
92 al_draw_textf(font,
al_map_rgb(0,
0,
255), SCREEN_WIDTH
- 10,
10, ALLEGRO_ALIGN_RIGHT,
"%d", fps
);
93 al_flip_display();
94 redraw
= false;
95 frameCounter
++;
96 }
97
98 do {
99 al_wait_for_event(queue,
&event
);
100 /*timer*/
101 if (event.type
== ALLEGRO_EVENT_TIMER
) {
102 if (start
) {
103 if (event.timer.source
== timer
) {
104 redraw
= true;
105 }
106 if (event.timer.source
== new_timer
) {
107 fps
= frameCounter
;
108 frameCounter
= 0;
109 }
110 }
111
112 }
113 /*keyboard*/
114 if (event.type
== ALLEGRO_EVENT_KEY_DOWN
) {
115 if (start
) {
116 if (event.keyboard.keycode
== ALLEGRO_KEY_DOWN
) {
117 if (y
!= SCREEN_HEIGHT
- SQUARE
) {
118 y
= y
+ 30;
119 }
120
121 }
122 if (event.keyboard.keycode
== ALLEGRO_KEY_UP
) {
123 if (y
!= 0) {
124 y
= y
- 30;
125 }
126 }
127 if (event.keyboard.keycode
== ALLEGRO_KEY_RIGHT
) {
128 if (x
!= SCREEN_WIDTH
- SQUARE
) {
129 x
= x
+ 30;
130 }
131 }
132 if (event.keyboard.keycode
== ALLEGRO_KEY_LEFT
) {
133 if (x
!= 0) {
134 x
= x
- 30;
135 }
136
137 }
138 }
139 if (event.keyboard.keycode
== ALLEGRO_KEY_ESCAPE
) {
140 quit
= true;
141 }
142 if (event.keyboard.keycode
== ALLEGRO_KEY_SPACE
) {
143 start
= true;
144 }
145 }
146 } while (!al_is_event_queue_empty(queue
));
147
148 }
149
150 al_destroy_event_queue(queue
);
151 al_destroy_display(display
);
152 al_destroy_timer(timer
);
153 al_destroy_timer(new_timer
);
154 al_destroy_font(font);
155
156 return EXIT_SUCCESS
;
157}
158
159void drawline
(uintptr_t state,
int x,
int y
) {
160
161 al_draw_line(x
+ 0.
5, y
+ 0.
5, x
+ 10.
5, y
+ 0.
5,
al_map_rgb(255,
0,
255),
1);
162}