1#include<allegro5\allegro5.h>
2#include<allegro5\allegro_native_dialog.h>
3#include<allegro5\allegro_image.h>
4#include<allegro5\allegro_font.h>
5#include<allegro5\allegro_ttf.h>
6#include<allegro5\allegro_primitives.h>
7
8#include<iostream>
9using namespace std
;
10
11int main
(){
12 int ScreenWidth
= 800, ScreenHeight
= 600;
13 if (!al_init())
14 {
15 al_show_native_message_box(NULL,
"ERROR", NULL,
"Could not initialize Allegro 5", NULL, NULL
);
16 return -1;
17 }
18
19 al_set_new_display_flags(ALLEGRO_WINDOWED
);
20 ALLEGRO_DISPLAY *display
= al_create_display(ScreenWidth, ScreenHeight
);//creating window with dimensions: 800:600 px
21 al_set_window_position(display,
200,
50);//place from left top positioning the frame of display
22 al_set_window_title(display,
"Marcin Piwko RLZ");
23
24 if (!display
)//show this message if sth wrong with display
25 {
26 al_show_native_message_box(display,
"Sample Title",
"Display Settings",
"Display window was not created succesfully", NULL, ALLEGRO_MESSAGEBOX_ERROR
);
27 return -1;
28 }
29
30 //Fonts init
31 al_init_font_addon();//initialization font addon
32 al_init_ttf_addon();//initialization ttf(true type font) addon
33
34 //INTRO Fonts
35 ALLEGRO_FONT *fontOrbionBlack36
= al_load_font("Orbitron Black.ttf",
36, NULL
);
36 ALLEGRO_FONT *fontOrbionBlack18
= al_load_font("Orbitron Black.ttf",
18, NULL
);
37
38 const float FPS
= 60;
39 const float frameFPS
= 15;//1)
40
41 enum Direction
{ DOWN, LEFT, RIGHT, UP
};
42
43 bool done
= false, draw
= true, active
= false;
44 float x
= 10, y
= 10;
45 float moveSpeed
= 5;
46 int dir
= DOWN,
47 sourceX
= 32, sourceY
= 0;
48
49 al_install_keyboard();
50 al_init_image_addon();
51 al_init_primitives_addon();
52
53 ALLEGRO_COLOR electricBlue
= al_map_rgb(44,
117,
255);
54
55 ALLEGRO_BITMAP *player
= al_load_bitmap("PlayerImage1.png");
56
57 ALLEGRO_KEYBOARD_STATE keyState
;
58
59 ALLEGRO_TIMER *timer
= al_create_timer(3.
0 / FPS
);//velocity of player movement
60 ALLEGRO_TIMER *frameTimer
= al_create_timer(0.
5 / frameFPS
);//2)
61 ALLEGRO_TIMER *introTimer
= al_create_timer(4.
0 / FPS
);
62
63
64 ALLEGRO_EVENT_QUEUE *event_queue
= al_create_event_queue();
65 al_register_event_source(event_queue,
al_get_timer_event_source(timer
));
66 al_register_event_source(event_queue,
al_get_timer_event_source(frameTimer
));
67 al_register_event_source(event_queue,
al_get_timer_event_source(introTimer
));
68
69
70 al_register_event_source(event_queue,
al_get_display_event_source(display
));
71 al_register_event_source(event_queue,
al_get_keyboard_event_source());
72
73
74 int menuXStartPosition
= ScreenWidth
/ 2 - 120;
75 int menuYStartPosition
= ScreenHeight
/ 5 + 36;
76 int menuX
= menuXStartPosition, menuY
= menuYStartPosition
;
77 int moveMenuSpeed
= 36;
78 bool activeMenu
= false;
79
80 enum loopStates
{ showFirstMessage, showMenu, showStartGame, showBestScores, showExit, NUM_LOOPSTATES
};
81 bool keysLoopStates
[] = { false,
false,
false };
82 enum menuChoosePosiotion
{ startGameCh, showBestScoresCh, showExitCh, NUM_MENUCHOOSEPOSITION
};
83 int menuPosition
= 0;
84 bool drawMenuState
= false;
85
86 int loopTransition
= 0;
87
88 al_start_timer(timer
);
89 al_start_timer(frameTimer
);
90 al_start_timer(introTimer
);
91
92
93 while (!done
)
94 {
95 ALLEGRO_EVENT events
;
96 al_wait_for_event(event_queue,
&events
);
97 al_get_keyboard_state(&keyState
);
98 if (events.type
== ALLEGRO_EVENT_DISPLAY_CLOSE
)
99 {
100 done
= true;
101 }
102
103 else if (loopTransition
== showFirstMessage
)
104 {
105 al_clear_to_color(al_map_rgb(0,
0,
0));
106 al_draw_text(fontOrbionBlack36,
al_map_rgb(44,
117,
255), ScreenWidth
/ 2, ScreenHeight
/ 2, ALLEGRO_ALIGN_CENTRE,
"Try to catch me!");
107 al_draw_text(fontOrbionBlack18,
al_map_rgb(44,
117,
255), ScreenWidth
/ 2, ScreenHeight
/ 2 + 36 + 10, ALLEGRO_ALIGN_CENTRE,
"press Enter to go..");
108 al_draw_text(fontOrbionBlack18,
al_map_rgb(44,
117,
255),
0, ScreenHeight
- 18, ALLEGRO_ALIGN_LEFT,
"made by Marcin Piwko");
109 al_flip_display();
110 loopTransition
= 1;
111 al_wait_for_event(event_queue,
&events
);
112 }
113 if (events.type
== ALLEGRO_EVENT_TIMER
)
114 {
115
116 if (loopTransition
== showMenu
)
117 {
118 if (events.timer.source
== introTimer
)
119 {
120
121 if (al_key_down(&keyState, ALLEGRO_KEY_ENTER
))
122 {
123 activeMenu
= true;
124
125 }
126 if (activeMenu
== true)
127 {
128
129 drawMenuState
= true;
130 if (al_key_down(&keyState, ALLEGRO_KEY_DOWN
))
131 {
132 menuY
+= moveMenuSpeed
;
133 menuPosition
++;
134 cout
<< menuPosition
<< endl
;
135
136 }
137 else if (al_key_down(&keyState, ALLEGRO_KEY_UP
))
138 {
139 menuY
-= moveMenuSpeed
;
140 menuPosition--
;
141 cout
<< menuPosition
<< endl
;
142 }
143 else if (al_key_down(&keyState, ALLEGRO_KEY_ENTER
))
144 {
145 if (menuPosition
== startGameCh
)
146 {
147 loopTransition
= showStartGame
;
148 }
149 if (menuPosition
== showExitCh
)
150 {
151 done
= true;
152 }
153 }
154 //**********************************************
155 else
156 drawMenuState
= false;
157 }
158
159
160
161 if (drawMenuState
){
162 al_draw_text(fontOrbionBlack36,
al_map_rgb(44,
117,
255), ScreenWidth
/ 2, ScreenHeight
/ 6, ALLEGRO_ALIGN_CENTRE,
"Main Menu");
163 al_draw_text(fontOrbionBlack36,
al_map_rgb(44,
117,
255), ScreenWidth
/ 2, ScreenHeight
/ 5 + 36, ALLEGRO_ALIGN_CENTER,
"Star game");
164 al_draw_text(fontOrbionBlack36,
al_map_rgb(44,
117,
255), ScreenWidth
/ 2, ScreenHeight
/ 5 + 36 * 2 + 4, ALLEGRO_ALIGN_CENTER,
"Best Scores");
165 al_draw_text(fontOrbionBlack36,
al_map_rgb(44,
117,
255), ScreenWidth
/ 2, ScreenHeight
/ 5 + 36 * 3 + 4, ALLEGRO_ALIGN_CENTER,
"Exit");
166
167 al_draw_rectangle(menuX, menuY, menuX
+ 250, menuY
+ 36, electricBlue,
1.
0);
168
169 al_flip_display();
170 al_clear_to_color(al_map_rgb(0,
0,
0));
171 drawMenuState
= true;
172 }
173 }
174
175 }
176
177
178
179 if (loopTransition
== showStartGame
)
180 {
181 if (events.timer.source
== timer
)
182 {
183 active
= true;
184 if (al_key_down(&keyState, ALLEGRO_KEY_DOWN
))
185 {
186 y
+= moveSpeed
;
187 dir
= DOWN
;
188 }
189 else if (al_key_down(&keyState, ALLEGRO_KEY_UP
))
190 {
191 y
-= moveSpeed
;
192 dir
= UP
;
193 }
194 else if (al_key_down(&keyState, ALLEGRO_KEY_RIGHT
))
195 {
196 x
+= moveSpeed
;
197 dir
= RIGHT
;
198 }
199 else if (al_key_down(&keyState, ALLEGRO_KEY_LEFT
))
200 {
201 x
-= moveSpeed
;
202 dir
= LEFT
;
203 }
204 //**********************************************
205 else
206 active
= false;
207 }
208
209 else if (events.timer.source
= frameTimer
)//6)
210 {
211 if (active
== true)
212 sourceX
+= al_get_bitmap_width(player
) / 3;
213 else
214 sourceX
= 32;
215
216 if (sourceX
>= al_get_bitmap_width(player
))
217 sourceX
= 0;
218
219 sourceY
= dir
;
220 }
221 draw
= true;
222
223
224 if (draw
){
225 al_draw_bitmap_region(player, sourceX, sourceY
* al_get_bitmap_height(player
) / 4,
32,
32,
226 x, y, NULL
);
227 al_flip_display();
228 al_clear_to_color(al_map_rgb(0,
0,
0));
229 }
230 }
231 }
232 }
233 al_destroy_display(display
);
234 al_destroy_timer(timer
);
235 al_destroy_bitmap(player
);
236 al_destroy_event_queue(event_queue
);
237 return 0;
238}