1#include <allegro5/allegro.h>
2#include <allegro5/allegro_image.h>
3#include <allegro5/allegro_native_dialog.h>
4#include <allegro5/allegro_primitives.h>
5#include <stdio.h>
6#include <math.h>
7#include <iostream>
8#include "Ship.h"
9
10
11const int SCREEN_WIDTH
= 640;
12const int SCREEN_HEIGTH
= 640;
13
14const int SHIP_WIDTH
= 32;
15const int SHIP_HEIGTH
= 32;
16
17const double CHANGE_IN_ANGLE
= 0.
034906585;
18const double SPEED_OF_SHIP
= 4.
0;
19
20const double FRAMES_PER_SECOND
= 60.
0;
21
22const enum KEYS
{UP, DOWN, LEFT, RIGHT
};
23
24//I keep track of the center and not a second point since the second point is never used.
25//To get the secodn point the program uses First_X_Position + SHIP_WIDTH
26
27//The Center_X_Position and Center_Y_Position is used in the redraw step near the end of the program.
28//The Center points are to make the square move around the center of the square.
29//struct ShipStuff
30//{
31// float First_X_Position;
32// float First_Y_Position;
33// float Center_X_Position;
34// float Center_Y_Position;
35// float Angle;
36//};
37
38/* Function prototypes */
39static Ship MakeShip
(float Center_X_Position,
float Center_Y_Position,
float First_X_Position,
float First_Y_Position,
float Angle
);
40static ALLEGRO_DISPLAY* MakeDisplay
(const int ScreenWidth,
const int ScreenHeigth
);
41static ALLEGRO_TIMER* MakeTimer
(const int FramerPerSecond
);
42static ALLEGRO_EVENT_QUEUE* MakeEventQueue
();
43static bool MakeAllegro
();
44static bool InstallPrimitives
();
45static bool InstallKeyboard
();
46
47int main
(int argc,
char **argv
)
48{
49 MakeAllegro
();
50 InstallKeyboard
();
51 InstallPrimitives
();
52 ALLEGRO_DISPLAY *Display
= MakeDisplay
(SCREEN_WIDTH, SCREEN_HEIGTH
);
53 ALLEGRO_TIMER *Timer
= MakeTimer
(FRAMES_PER_SECOND
);
54 ALLEGRO_EVENT_QUEUE *EventQueue
= MakeEventQueue
();
55
56
57
58 //UP, DOWN, LEFT, RIGHT
59 bool KeyPresses
[] = {false,
false,
false,
false};
60 bool Redraw
= true;
61
62 al_register_event_source(EventQueue,
al_get_display_event_source(Display
));
63 al_register_event_source(EventQueue,
al_get_timer_event_source(Timer
));
64 al_register_event_source(EventQueue,
al_get_keyboard_event_source());
65
66
67 float First_X_Position
= SCREEN_WIDTH
/ 2.
0;
68 float First_Y_Position
= SCREEN_HEIGTH
/ 2.
0;
69 float Center_X_Position
= First_X_Position
+ (SHIP_WIDTH
/ 2.
0);
70 float Center_Y_Position
= First_Y_Position
+ (SHIP_HEIGTH
/ 2.
0);
71 float Angle
= -ALLEGRO_PI/2;
72
73 Ship ThisShip
(Center_X_Position, Center_Y_Position, First_X_Position, First_Y_Position, Angle
);
74 al_draw_filled_rectangle(ThisShip.GetFirst_X_Position
(), ThisShip.GetFirst_Y_Position
(), ThisShip.GetFirst_X_Position
() + SHIP_WIDTH,
75 ThisShip.GetFirst_Y_Position
() + SHIP_HEIGTH,
al_map_rgb(255,
124,
68));
76 al_flip_display();
77 al_start_timer(Timer
);
78 float AngleInRadians
= 0.
0f;
79 bool DoExit
= false;
80 while (!DoExit
)
81 {
82 ALLEGRO_EVENT Event
;
83 al_wait_for_event(EventQueue,
&Event
);
84 std::cout
<< "In while loop" << std::endl
;
85
86 if(Event.type
== ALLEGRO_EVENT_TIMER
)
87 {
88 std::cout
<< "In if event type = ALLEGRO_EVENT_TIMER" << std::endl
;
89 if(KeyPresses
[UP
])
90 {
91 ThisShip.SetFirst_X_Position
(ThisShip.GetFirst_X_Position
() + cos(ThisShip.GetAngle
() * SPEED_OF_SHIP
));
92 ThisShip.SetCenter_X_Position
(ThisShip.GetCenter_X_Position
() + cos(ThisShip.GetAngle
() * SPEED_OF_SHIP
));
93
94 ThisShip.SetFirst_Y_Position
(ThisShip.GetFirst_Y_Position
() + sin(ThisShip.GetAngle
() * SPEED_OF_SHIP
));
95 ThisShip.SetCenter_Y_Position
(ThisShip.GetCenter_Y_Position
() + sin(ThisShip.GetAngle
() * SPEED_OF_SHIP
));
96 }
97 if (KeyPresses
[DOWN
])
98 {
99 ThisShip.SetFirst_X_Position
(ThisShip.GetFirst_X_Position
() - cos(ThisShip.GetAngle
() * SPEED_OF_SHIP
));
100 ThisShip.SetCenter_X_Position
(ThisShip.GetCenter_X_Position
() - cos(ThisShip.GetAngle
() * SPEED_OF_SHIP
));
101
102 ThisShip.SetFirst_Y_Position
(ThisShip.GetFirst_Y_Position
() - sin(ThisShip.GetAngle
() * SPEED_OF_SHIP
));
103 ThisShip.SetCenter_Y_Position
(ThisShip.GetCenter_Y_Position
() - sin(ThisShip.GetAngle
() * SPEED_OF_SHIP
));
104
105 }
106 if(KeyPresses
[LEFT
])
107 {
108 ThisShip.SetAngle
(ThisShip.GetAngle
() - CHANGE_IN_ANGLE
);
109
110 }
111 if(KeyPresses
[RIGHT
])
112 {
113 ThisShip.SetAngle
(ThisShip.GetAngle
() + CHANGE_IN_ANGLE
);
114 }
115 Redraw
= true;
116 }
117 else if (Event.type
== ALLEGRO_EVENT_DISPLAY_CLOSE
)
118 {
119 break;
120 }
121 else if (Event.type
== ALLEGRO_EVENT_KEY_DOWN
)
122 {
123 std::cout
<< "In if ALLEGRO_EVENT_KEY_DOWN" << std::endl
;
124 switch (Event.keyboard.keycode
)
125 {
126 case ALLEGRO_KEY_UP:
127 KeyPresses
[UP
] = true;
128 break;
129 case ALLEGRO_KEY_DOWN:
130 KeyPresses
[DOWN
] = true;
131 break;
132 case ALLEGRO_KEY_LEFT:
133 KeyPresses
[LEFT
] = true;
134 break;
135 case ALLEGRO_KEY_RIGHT:
136 KeyPresses
[RIGHT
] = true;
137 break;
138 }
139 }
140 else if (Event.type
== ALLEGRO_EVENT_KEY_UP
)
141 {
142 std::cout
<< "In If ALLEGRO_EVENT_KEY_UP" << std::endl
;
143 switch(Event.keyboard.keycode
)
144 {
145 case ALLEGRO_KEY_UP:
146 KeyPresses
[UP
] = false;
147 break;
148 case ALLEGRO_KEY_DOWN:
149 KeyPresses
[DOWN
] = false;
150 break;
151 case ALLEGRO_KEY_LEFT:
152 KeyPresses
[LEFT
] = false;
153 break;
154 case ALLEGRO_KEY_RIGHT:
155 KeyPresses
[RIGHT
] = false;
156 break;
157 case ALLEGRO_KEY_ESCAPE:
158 DoExit
= true;
159 break;
160 }
161 }
162
163 if(Redraw
&& al_is_event_queue_empty(EventQueue
))
164 {
165 std::cout
<< "In If Redraw." << std::endl
;
166 Redraw
= false;
167 al_clear_to_color(al_map_rgb(0,
0,
0));
168 ALLEGRO_TRANSFORM ShipTransformation, Old
;
169 al_identity_transform(&ShipTransformation
);
170 //al_translate_transform(&ShipTransformation, -Ship.Center_X_Position, -Ship.Center_Y_Position);
171 al_translate_transform(&ShipTransformation,
-ThisShip.GetCenter_X_Position
(),
-ThisShip.GetCenter_Y_Position
());
172 //al_rotate_transform(&ShipTransformation, Ship.Angle);
173 al_rotate_transform(&ShipTransformation, ThisShip.GetAngle
());
174 //al_translate_transform(&ShipTransformation, (Ship.Center_X_Position), (Ship.Center_Y_Position));
175 al_translate_transform(&ShipTransformation, ThisShip.GetCenter_X_Position
(), ThisShip.GetCenter_Y_Position
());
176 al_use_transform(&ShipTransformation
);
177 //al_draw_filled_rectangle(Ship.First_X_Position, Ship.First_Y_Position, Ship.First_X_Position + SHIP_WIDTH, Ship.First_Y_Position + SHIP_HEIGTH, al_map_rgb(255,127,0));
178 al_draw_filled_rectangle(ThisShip.GetFirst_X_Position
(), ThisShip.GetFirst_Y_Position
(), ThisShip.GetFirst_X_Position
() - SHIP_WIDTH,
179 ThisShip.GetFirst_Y_Position
() + SHIP_HEIGTH,
al_map_rgb(255,
127,
0));
180 al_flip_display();
181 al_use_transform(&Old
);
182 }
183 }
184 al_destroy_timer(Timer
);
185 al_destroy_event_queue(EventQueue
);
186 al_destroy_display(Display
);
187
188 return 0;
189}
190
191static ALLEGRO_DISPLAY* MakeDisplay
(const int SCREEN_WIDTH,
const int SCREEN_HEIGTH
)
192{
193 ALLEGRO_DISPLAY *Display
= al_create_display(SCREEN_WIDTH, SCREEN_HEIGTH
);
194 if(!Display
)
195 {
196 std::cout
<< "Sorry, but allegro can't make the display." << std::endl
;
197 std::cout
<< "Press any key to exit...";
198 return NULL
;
199 }
200 return Display
;
201}
202
203static ALLEGRO_TIMER* MakeTimer
(const int FramerPerSecond
)
204{
205 ALLEGRO_TIMER* Timer
= al_create_timer(FramerPerSecond
);
206 if(!Timer
)
207 {
208 std::cout
<< "Sorry, but allegro can't make the timer." << std::endl
;
209 std::cout
<< "Press any key to exit...";
210 std::cin.get
();
211 return NULL
;
212 }
213 return Timer
;
214}
215
216static ALLEGRO_EVENT_QUEUE* MakeEventQueue
()
217{
218 ALLEGRO_EVENT_QUEUE* EventQueue
= al_create_event_queue();
219 if(!EventQueue
)
220 {
221 std::cout
<< "Sorry, but allegro can't make the event queue." << std::endl
;
222 std::cout
<< "Press any key to exit...";
223 std::cin.get
();
224 return NULL
;
225 }
226 return EventQueue
;
227}
228
229
230static bool MakeAllegro
()
231{
232 if(!al_init())
233 {
234 std::cout
<< "Sorry, but Allegro did not install start correctly." << std::endl
;
235 std::cout
<< "Press any key to exit...";
236 std::cin.get
();
237 return false;
238 }
239 return true;
240}
241
242static bool InstallPrimitives
()
243{
244 if(!al_init_primitives_addon())
245 {
246 std::cout
<< "Sorry but allegro could not install the primitives." << std::endl
;
247 std::cout
<< "Press any key to exit...";
248 std::cin.get
();
249 return false;
250 }
251 return true;
252}
253
254static bool InstallKeyboard
()
255{
256 if(!al_install_keyboard())
257 {
258 std::cout
<< "Sorry, allegro could not install the keyboard." << std::endl
;
259 std::cout
<< "Press any key to exit...";
260 std::cin.get
();
261 return false;
262 }
263 return true;
264}