root / proj / src / proj.c @ 226
History | View | Annotate | Download (8.9 KB)
1 | 144 | up20180655 | #include <lcom/lcf.h> |
---|---|---|---|
2 | #include <lcom/proj.h> |
||
3 | #include <lcom/liblm.h> |
||
4 | 185 | up20180655 | #include <math.h> |
5 | 144 | up20180655 | |
6 | 149 | up20180655 | #include "proj_macros.h" |
7 | 179 | up20180642 | #include "proj_func.h" |
8 | 147 | up20180655 | |
9 | 149 | up20180655 | #include "kbc.h" |
10 | #include "timer.h" |
||
11 | #include "keyboard.h" |
||
12 | 150 | up20180655 | #include "mouse.h" |
13 | 189 | up20180655 | #include "graph.h" |
14 | 153 | up20180655 | #include "interrupts_func.h" |
15 | 149 | up20180655 | |
16 | 179 | up20180642 | #include "graph.h" |
17 | #include "sprite.h" |
||
18 | #include "rectangle.h" |
||
19 | 182 | up20180642 | #include "font.h" |
20 | 192 | up20180642 | #include "ent.h" |
21 | 168 | up20180642 | |
22 | 192 | up20180642 | #include "crosshair.h" |
23 | #include "shooter.h" |
||
24 | #include "pistol.h" |
||
25 | #include "nothing.h" |
||
26 | 202 | up20180655 | #include "bullet.h" |
27 | 216 | up20180642 | #include "map1.h" |
28 | 183 | up20180642 | |
29 | 226 | up20180642 | #include "list.h" |
30 | |||
31 | 144 | up20180655 | int main(int argc, char* argv[]) { |
32 | |||
33 | lcf_set_language("EN-US");
|
||
34 | |||
35 | //lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
|
||
36 | |||
37 | 189 | up20180655 | lcf_log_output("/home/lcom/labs/proj/output.txt");
|
38 | 144 | up20180655 | |
39 | if (lcf_start(argc, argv)) return 1; |
||
40 | |||
41 | lcf_cleanup(); |
||
42 | |||
43 | return 0; |
||
44 | } |
||
45 | 147 | up20180655 | |
46 | 149 | up20180655 | int(proj_main_loop)(int argc, char *argv[]) { |
47 | 170 | up20180642 | |
48 | int r;
|
||
49 | |||
50 | 221 | up20180642 | font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
|
51 | 192 | up20180642 | if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; } |
52 | |||
53 | 170 | up20180642 | /// subscribe interrupts
|
54 | if (subscribe_all()) { return 1; } |
||
55 | |||
56 | /// initialize graphics
|
||
57 | 166 | up20180642 | if(graph_init(GRAPH_MODE)){
|
58 | printf("%s: failed to initalize graphics.\n", __func__);
|
||
59 | if (cleanup()) printf("%s: failed to cleanup.\n", __func__); |
||
60 | 152 | up20180642 | return 1; |
61 | } |
||
62 | |||
63 | 188 | up20180642 | /// Load stuff
|
64 | 216 | up20180642 | basic_sprite_t *bsp_crosshair = NULL;
|
65 | basic_sprite_t *bsp_shooter = NULL;
|
||
66 | basic_sprite_t *bsp_pistol = NULL;
|
||
67 | basic_sprite_t *bsp_nothing = NULL;
|
||
68 | map_t *map1 = NULL;
|
||
69 | sprite_t *sp_crosshair = NULL;
|
||
70 | 188 | up20180642 | { |
71 | graph_clear_screen(); |
||
72 | text_t *txt = text_ctor(consolas, "Loading...");
|
||
73 | text_draw(txt); |
||
74 | text_dtor(txt); |
||
75 | graph_draw(); |
||
76 | 192 | up20180642 | |
77 | bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n"); |
||
78 | bsp_shooter = get_shooter (); if(bsp_shooter == NULL) printf("Failed to get shooter\n"); |
||
79 | bsp_pistol = get_pistol (); if(bsp_pistol == NULL) printf("Failed to get pistol\n"); |
||
80 | bsp_nothing = get_nothing (); if(bsp_nothing == NULL) printf("Failed to get nothing\n"); |
||
81 | 216 | up20180642 | map1 = get_map1 (); if(map1 == NULL) printf("Failed to get map1\n"); |
82 | 192 | up20180642 | |
83 | sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n"); |
||
84 | 188 | up20180642 | } |
85 | 192 | up20180642 | |
86 | 159 | up20180642 | #ifdef DIOGO
|
87 | 188 | up20180642 | graph_clear_screen(); |
88 | 183 | up20180642 | |
89 | 188 | up20180642 | rectangle_t *rect = rectangle_ctor(0,0,400,100); |
90 | rectangle_set_pos(rect, |
||
91 | graph_get_XRes()/2 - rectangle_get_w(rect)/2, |
||
92 | graph_get_YRes()*0.25 - rectangle_get_h(rect)/2); |
||
93 | rectangle_set_fill_color(rect, BLACK); |
||
94 | 179 | up20180642 | rectangle_set_outline_width(rect, 2);
|
95 | 188 | up20180642 | rectangle_set_outline_color(rect, WHITE); |
96 | 179 | up20180642 | rectangle_draw(rect); |
97 | |||
98 | 188 | up20180642 | text_t *txt = text_ctor(consolas, "Hello world!");
|
99 | text_set_color(txt, 0x888888);
|
||
100 | 182 | up20180642 | |
101 | 188 | up20180642 | text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
|
102 | rectangle_get_y(rect)+rectangle_get_h(rect)/2);
|
||
103 | text_set_valign(txt, text_valign_center); |
||
104 | text_set_halign(txt, text_halign_center); |
||
105 | 183 | up20180642 | text_draw(txt); |
106 | text_dtor(txt); |
||
107 | |||
108 | graph_draw(); |
||
109 | 188 | up20180642 | rectangle_dtor(rect); |
110 | 183 | up20180642 | |
111 | 226 | up20180642 | list_t *l = list_ctor(); |
112 | int *p = NULL; |
||
113 | for(int i = 10; i < 20; ++i){ |
||
114 | p = malloc(sizeof(int)); |
||
115 | *p = i; |
||
116 | printf("INSERTING %d\n", i);
|
||
117 | list_insert(l, list_end(l), p); |
||
118 | printf("INSERTED, SIZE=%d\n", list_size(l));
|
||
119 | } |
||
120 | list_node_t *it = list_begin(l); |
||
121 | while(it != list_end(l)){
|
||
122 | printf("%d\n", **(int**)list_node_val(it)); |
||
123 | it = list_node_next(it); |
||
124 | } |
||
125 | while(list_size(l) > 0){ |
||
126 | printf("ERASING\n");
|
||
127 | void *p = list_erase(l, list_begin(l));
|
||
128 | printf("ERASED %d, SIZE=%d\n", *(int*)p, list_size(l)); |
||
129 | free(p); |
||
130 | } |
||
131 | printf("DONE\n");
|
||
132 | if(list_dtor(l)) printf("COULD NOT DESTRUCT LIST\n"); |
||
133 | |||
134 | |||
135 | 183 | up20180642 | tickdelay(micros_to_ticks(1000000));
|
136 | |||
137 | 159 | up20180642 | #endif
|
138 | 152 | up20180642 | |
139 | 171 | up20180655 | #ifdef TELMO
|
140 | 216 | up20180642 | ent_set_scale(DEFAULT_SCALE); |
141 | 192 | up20180642 | |
142 | 201 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
143 | 222 | up20180655 | gunner_set_pos(shooter1, 75, 75); |
144 | 192 | up20180642 | |
145 | 201 | up20180642 | gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing); |
146 | gunner_set_pos(shooter2, -50, -50); |
||
147 | 192 | up20180642 | |
148 | 204 | up20180655 | bullet_t *bullet = bullet_ctor(get_bullet()); |
149 | bullet_set_pos(bullet, 400, 400); |
||
150 | 202 | up20180655 | |
151 | 216 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
152 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
153 | 214 | up20180642 | |
154 | 200 | up20180655 | uint32_t refresh_count_value = sys_hz() / REFRESH_RATE; |
155 | 171 | up20180655 | #endif
|
156 | |||
157 | 149 | up20180655 | /// loop stuff
|
158 | 170 | up20180642 | int ipc_status;
|
159 | 149 | up20180655 | message msg; |
160 | 147 | up20180655 | int good = 1; |
161 | 168 | up20180642 | |
162 | 183 | up20180642 | #ifdef DIOGO
|
163 | good = 0;
|
||
164 | #endif
|
||
165 | |||
166 | 147 | up20180655 | while (good) {
|
167 | /* Get a request message. */
|
||
168 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
169 | printf("driver_receive failed with %d", r);
|
||
170 | continue;
|
||
171 | } |
||
172 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
173 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
174 | case HARDWARE: /* hardware interrupt notification */ |
||
175 | 153 | up20180655 | for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
176 | if (msg.m_notify.interrupts & n) {
|
||
177 | interrupt_handler(i); |
||
178 | 167 | up20180655 | if ((scancode[0]) == ESC_BREAK_CODE) good = 0; |
179 | 171 | up20180655 | #ifdef TELMO
|
180 | 187 | up20180655 | if (i == 0) { |
181 | if (no_interrupts % refresh_count_value == 0) { |
||
182 | 222 | up20180655 | update_movement(map1, shooter1); |
183 | 216 | up20180642 | |
184 | 220 | up20180655 | if(map_collides_gunner(map1, shooter1)){
|
185 | 216 | up20180642 | printf("COLLIDING\n");
|
186 | } |
||
187 | |||
188 | 200 | up20180655 | update_scale(); |
189 | 192 | up20180642 | |
190 | 216 | up20180642 | ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0, |
191 | gunner_get_y(shooter1)-ent_get_YLength()/2.0); |
||
192 | 214 | up20180642 | |
193 | 192 | up20180642 | sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y()); |
194 | 187 | up20180655 | double angle = get_mouse_angle(shooter1);
|
195 | 201 | up20180642 | gunner_set_angle(shooter1, angle - M_PI_2); |
196 | 187 | up20180655 | graph_clear_screen(); |
197 | 207 | up20180642 | |
198 | clock_t t = clock(); |
||
199 | |||
200 | 216 | up20180642 | map_draw (map1); |
201 | 201 | up20180642 | gunner_draw(shooter2); |
202 | gunner_draw(shooter1); |
||
203 | 204 | up20180655 | bullet_draw(bullet); |
204 | 207 | up20180642 | |
205 | 222 | up20180655 | t = clock()-t; //printf("%d\n", (t*1000)/CLOCKS_PER_SEC);
|
206 | 207 | up20180642 | |
207 | 192 | up20180642 | sprite_draw(sp_crosshair); |
208 | 187 | up20180655 | graph_draw(); |
209 | } |
||
210 | 184 | up20180655 | } |
211 | 220 | up20180655 | if (i == 12) { |
212 | if (counter_mouse_ih >= 3) { |
||
213 | struct packet pp = mouse_parse_packet(packet_mouse_ih);
|
||
214 | update_mouse_position(&pp); |
||
215 | //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
|
||
216 | counter_mouse_ih = 0;
|
||
217 | } |
||
218 | } |
||
219 | 171 | up20180655 | #endif
|
220 | 153 | up20180655 | } |
221 | 147 | up20180655 | } |
222 | 167 | up20180655 | |
223 | 147 | up20180655 | break;
|
224 | default:
|
||
225 | break; /* no other notifications expected: do nothing */ |
||
226 | } |
||
227 | } else { /* received standart message, not a notification */ |
||
228 | /* no standart message expected: do nothing */
|
||
229 | } |
||
230 | } |
||
231 | 149 | up20180655 | |
232 | 192 | up20180642 | #ifdef TELMO
|
233 | 201 | up20180642 | gunner_dtor(shooter1); shooter1 = NULL;
|
234 | 216 | up20180642 | gunner_dtor(shooter2); shooter2 = NULL;
|
235 | bullet_dtor(bullet); bullet = NULL;
|
||
236 | 192 | up20180642 | #endif
|
237 | 188 | up20180642 | |
238 | 216 | up20180642 | basic_sprite_dtor (bsp_crosshair); bsp_crosshair = NULL;
|
239 | basic_sprite_dtor (bsp_shooter ); bsp_shooter = NULL;
|
||
240 | sprite_dtor (sp_crosshair ); sp_crosshair = NULL;
|
||
241 | basic_sprite_dtor (bsp_pistol ); bsp_pistol = NULL;
|
||
242 | basic_sprite_dtor (bsp_nothing ); bsp_nothing = NULL;
|
||
243 | map_dtor (map1 ); map1 = NULL;
|
||
244 | font_dtor (consolas ); consolas = NULL;
|
||
245 | 192 | up20180642 | |
246 | 153 | up20180655 | // Unsubscribe interrupts
|
247 | 155 | up20180655 | if (unsubscribe_all()) {
|
248 | if (cleanup())
|
||
249 | printf("%s: failed to cleanup.\n", __func__);
|
||
250 | 152 | up20180642 | return 1; |
251 | } |
||
252 | |||
253 | 155 | up20180655 | |
254 | if (cleanup()) {
|
||
255 | printf("%s: failed to cleanup.\n", __func__);
|
||
256 | 152 | up20180642 | return 1; |
257 | } |
||
258 | |||
259 | 149 | up20180655 | return 0; |
260 | 147 | up20180655 | } |