1#include<allegro5\allegro.h>
2#include<allegro5\allegro_native_dialog.h>
3#include <stdio.h>
4#include <vector>
5#include"Ship.h"
6
7
8
9enum KeyDirections
{ Up, Down, Left, Right
};
10int SCREEN_WIDTH
= 200;
11int SCREEN_HEGTH
= 200;
12int SHIP_WIDTH
= 10;
13int SHIP_HEIGTH
= 10;
14double FPS
= 1.
0 / 60.
0;
15float SHIPS_MOVEMENT
= 4.
0;
16int main
(int argc,
char **argv
)
17{
18
19
20 std::vector
<Ship> shipCollection
;
21 float ship_X_Position
= SCREEN_WIDTH
/ 2;
22 float ship_Y_Position
= SCREEN_HEGTH
/ 2;
23 ALLEGRO_DISPLAY* mainDisplay
= NULL
;
24 ALLEGRO_TIMER* screenRefreshTimer
= NULL
;
25 ALLEGRO_EVENT_QUEUE* eventQueue
= NULL
;
26 bool KeysPressed
[4] = { false,
false,
false,
false };
27 bool redraw
= true;
28 bool doExit
= false;
29
30 if (!al_init()) {
31 al_show_native_message_box(mainDisplay,
"Title",
"Heading",
"Text", NULL, ALLEGRO_MESSAGEBOX_ERROR
);
32 return 0;
33 }
34
35 Ship ship
(SHIP_WIDTH, SHIP_HEIGTH, ship_X_Position, ship_Y_Position
);
36 ship.setColor
(255,
0,
255);
37 shipCollection.push_back
(ship
);
38 std::cout
<< ship.getColor
().r
<< std::endl
;
39 std::cout
<< shipCollection
[0].getColor
().r
<< std::endl
;
40 mainDisplay
= al_create_display(SCREEN_WIDTH, SCREEN_HEGTH
);
41
42 if (mainDisplay
== NULL
) {
43 al_show_native_message_box(mainDisplay,
"Title",
"Heading",
"Text", NULL, ALLEGRO_MESSAGEBOX_ERROR
);
44 return 0;
45 }
46
47 if (!al_install_keyboard()) {
48 al_show_native_message_box(mainDisplay,
"Title",
"Heading",
"Text", NULL, ALLEGRO_MESSAGEBOX_ERROR
);
49 al_destroy_display(mainDisplay
);
50 }
51
52 screenRefreshTimer
= al_create_timer(FPS
);
53 if (screenRefreshTimer
== NULL
) {
54 al_show_native_message_box(mainDisplay,
"Title",
"Heading",
"Text", NULL, ALLEGRO_MESSAGEBOX_ERROR
);
55 al_destroy_display(mainDisplay
);
56 return 0;
57 }
58
59
60 eventQueue
= al_create_event_queue();
61 if (eventQueue
== NULL
) {
62 al_show_native_message_box(mainDisplay,
"Title",
"Heading",
"Text", NULL, ALLEGRO_MESSAGEBOX_ERROR
);
63 al_destroy_display(mainDisplay
);
64 al_destroy_timer(screenRefreshTimer
);
65 return 0;
66 }
67 al_set_target_bitmap(al_get_backbuffer(mainDisplay
));
68
69 al_register_event_source(eventQueue,
al_get_timer_event_source(screenRefreshTimer
));
70 al_register_event_source(eventQueue,
al_get_display_event_source(mainDisplay
));
71 al_register_event_source(eventQueue,
al_get_keyboard_event_source());
72 al_clear_to_color(al_map_rgb(0,
0,
0));
73 al_flip_display();
74 al_start_timer(screenRefreshTimer
);
75
76 while (!doExit
) {
77 ALLEGRO_EVENT thisEvent
;
78 al_wait_for_event(eventQueue,
&thisEvent
);
79 if (thisEvent.type
== ALLEGRO_EVENT_TIMER
) {
80 for(int index = 0; index < shipCollection.size
(); index++)
81 {
82 if (KeysPressed
[Up
] /*&& ship.upperLeft_Y_Position >= 4.0*/) {
83 shipCollection
[index].upperLeft_Y_Position
-= SHIPS_MOVEMENT
;
84 }
85
86 if (KeysPressed
[Down
] /*&& ship.upperLeft_Y_Position <= 200 - 10 - 4.0*/) {
87 shipCollection
[index].upperLeft_Y_Position
+= SHIPS_MOVEMENT
;
88 }
89
90 if (KeysPressed
[Left
] /*&& ship.upperLeft_X_Position >= 4.0*/) {
91 shipCollection
[index].upperLeft_X_Position
-= SHIPS_MOVEMENT
;
92 }
93
94 if (KeysPressed
[Right
] /*&& ship.upperLeft_X_Position <= 200 - 10 - 4.0*/) {
95 shipCollection
[index].upperLeft_X_Position
+= SHIPS_MOVEMENT
;
96 }
97 }
98
99 redraw
= true;
100 }
101 else if (thisEvent.type
== ALLEGRO_EVENT_DISPLAY_CLOSE
) {
102 break;
103 }
104 else if (thisEvent.type
== ALLEGRO_EVENT_KEY_DOWN
) {
105 switch (thisEvent.keyboard.keycode
) {
106 case ALLEGRO_KEY_UP:
107 KeysPressed
[Up
] = true;
108 break;
109 case ALLEGRO_KEY_DOWN:
110 KeysPressed
[Down
] = true;
111 break;
112 case ALLEGRO_KEY_LEFT:
113 KeysPressed
[Left
] = true;
114 break;
115 case ALLEGRO_KEY_RIGHT:
116 KeysPressed
[Right
] = true;
117 break;
118 }
119 }
120 else if (thisEvent.type
== ALLEGRO_EVENT_KEY_UP
) {
121 switch (thisEvent.keyboard.keycode
) {
122 case ALLEGRO_KEY_UP:
123 KeysPressed
[Up
] = false;
124 break;
125 case ALLEGRO_KEY_DOWN:
126 KeysPressed
[Down
] = false;
127 break;
128 case ALLEGRO_KEY_LEFT:
129 KeysPressed
[Left
] = false;
130 break;
131 case ALLEGRO_KEY_RIGHT:
132 KeysPressed
[Right
] = false;
133 break;
134 }
135 }
136
137 if (redraw
&& al_is_event_queue_empty(eventQueue
)) {
138 redraw
= false;
139 al_clear_to_color(al_map_rgb(0,
0,
0));
140 //for(int shipIndex = 0; shipIndex < ShipCollection.size(); shipIndex++)
141 //{
142 // al_draw_bitmap(ShipCollection[shipIndex].getImage(), ShipCollection[shipIndex].upperLeft_X_Position,
143 // ShipCollection[shipIndex].upperLeft_Y_Position, 0);
144 //}
145
146 al_draw_bitmap(shipCollection
[0].getImage
(), shipCollection
[0].upperLeft_X_Position,
147 shipCollection
[0].upperLeft_Y_Position,
0);
148 /*
149 foreach Ship in the collections
150 draw it.
151 */
152 al_flip_display();
153 }
154 }
155
156 al_destroy_timer(screenRefreshTimer
);
157 al_destroy_event_queue(eventQueue
);
158 al_destroy_display(mainDisplay
);
159}