root / proj / src / proj.c @ 214
History | View | Annotate | Download (7.6 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 | 207 | up20180642 | #include "map.h" |
28 | 183 | up20180642 | |
29 | 144 | up20180655 | int main(int argc, char* argv[]) { |
30 | |||
31 | lcf_set_language("EN-US");
|
||
32 | |||
33 | //lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
|
||
34 | |||
35 | 189 | up20180655 | lcf_log_output("/home/lcom/labs/proj/output.txt");
|
36 | 144 | up20180655 | |
37 | if (lcf_start(argc, argv)) return 1; |
||
38 | |||
39 | lcf_cleanup(); |
||
40 | |||
41 | return 0; |
||
42 | } |
||
43 | 147 | up20180655 | |
44 | 149 | up20180655 | int(proj_main_loop)(int argc, char *argv[]) { |
45 | 170 | up20180642 | |
46 | int r;
|
||
47 | |||
48 | 192 | up20180642 | font_t *consolas = font_ctor("/home/lcom/labs/proj/font/Consolas/xpm2");
|
49 | if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; } |
||
50 | |||
51 | 170 | up20180642 | /// subscribe interrupts
|
52 | if (subscribe_all()) { return 1; } |
||
53 | |||
54 | /// initialize graphics
|
||
55 | 166 | up20180642 | if(graph_init(GRAPH_MODE)){
|
56 | printf("%s: failed to initalize graphics.\n", __func__);
|
||
57 | if (cleanup()) printf("%s: failed to cleanup.\n", __func__); |
||
58 | 152 | up20180642 | return 1; |
59 | } |
||
60 | |||
61 | 188 | up20180642 | /// Load stuff
|
62 | 192 | up20180642 | basic_sprite_t *bsp_crosshair = NULL;
|
63 | basic_sprite_t *bsp_shooter = NULL;
|
||
64 | basic_sprite_t *bsp_pistol = NULL;
|
||
65 | basic_sprite_t *bsp_nothing = NULL;
|
||
66 | sprite_t *sp_crosshair = NULL;
|
||
67 | 188 | up20180642 | { |
68 | graph_clear_screen(); |
||
69 | text_t *txt = text_ctor(consolas, "Loading...");
|
||
70 | text_draw(txt); |
||
71 | text_dtor(txt); |
||
72 | graph_draw(); |
||
73 | 192 | up20180642 | |
74 | bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n"); |
||
75 | bsp_shooter = get_shooter (); if(bsp_shooter == NULL) printf("Failed to get shooter\n"); |
||
76 | bsp_pistol = get_pistol (); if(bsp_pistol == NULL) printf("Failed to get pistol\n"); |
||
77 | bsp_nothing = get_nothing (); if(bsp_nothing == NULL) printf("Failed to get nothing\n"); |
||
78 | |||
79 | sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n"); |
||
80 | 188 | up20180642 | } |
81 | 192 | up20180642 | |
82 | 159 | up20180642 | #ifdef DIOGO
|
83 | 188 | up20180642 | graph_clear_screen(); |
84 | 183 | up20180642 | |
85 | 188 | up20180642 | rectangle_t *rect = rectangle_ctor(0,0,400,100); |
86 | rectangle_set_pos(rect, |
||
87 | graph_get_XRes()/2 - rectangle_get_w(rect)/2, |
||
88 | graph_get_YRes()*0.25 - rectangle_get_h(rect)/2); |
||
89 | rectangle_set_fill_color(rect, BLACK); |
||
90 | 179 | up20180642 | rectangle_set_outline_width(rect, 2);
|
91 | 188 | up20180642 | rectangle_set_outline_color(rect, WHITE); |
92 | 179 | up20180642 | rectangle_draw(rect); |
93 | |||
94 | 188 | up20180642 | text_t *txt = text_ctor(consolas, "Hello world!");
|
95 | text_set_color(txt, 0x888888);
|
||
96 | 182 | up20180642 | |
97 | 188 | up20180642 | text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
|
98 | rectangle_get_y(rect)+rectangle_get_h(rect)/2);
|
||
99 | text_set_valign(txt, text_valign_center); |
||
100 | text_set_halign(txt, text_halign_center); |
||
101 | 183 | up20180642 | text_draw(txt); |
102 | text_dtor(txt); |
||
103 | |||
104 | graph_draw(); |
||
105 | 188 | up20180642 | rectangle_dtor(rect); |
106 | 183 | up20180642 | |
107 | tickdelay(micros_to_ticks(1000000));
|
||
108 | |||
109 | 159 | up20180642 | #endif
|
110 | 152 | up20180642 | |
111 | 171 | up20180655 | #ifdef TELMO
|
112 | 214 | up20180642 | ent_set_scale(10.0); |
113 | 192 | up20180642 | |
114 | 201 | up20180642 | gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n"); |
115 | gunner_set_pos(shooter1, 0, 0); |
||
116 | 192 | up20180642 | |
117 | 201 | up20180642 | gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing); |
118 | gunner_set_pos(shooter2, -50, -50); |
||
119 | 192 | up20180642 | |
120 | 204 | up20180655 | bullet_t *bullet = bullet_ctor(get_bullet()); |
121 | bullet_set_pos(bullet, 400, 400); |
||
122 | 202 | up20180655 | |
123 | 214 | up20180642 | ent_set_origin(bullet_get_x(bullet)-ent_get_XLength()/2.0, |
124 | bullet_get_y(bullet)-ent_get_YLength()/2.0); |
||
125 | |||
126 | 174 | up20180642 | graph_clear_screen(); |
127 | 201 | up20180642 | gunner_draw(shooter1); |
128 | 192 | up20180642 | sprite_draw(sp_crosshair); |
129 | 174 | up20180642 | graph_draw(); |
130 | 200 | up20180655 | |
131 | uint32_t refresh_count_value = sys_hz() / REFRESH_RATE; |
||
132 | 207 | up20180642 | |
133 | basic_sprite_t *bsp_pink = get_map(); |
||
134 | sprite_t *pink = sprite_ctor(bsp_pink); |
||
135 | basic_sprite_dtor(bsp_pink); |
||
136 | 171 | up20180655 | #endif
|
137 | |||
138 | 149 | up20180655 | /// loop stuff
|
139 | 170 | up20180642 | int ipc_status;
|
140 | 149 | up20180655 | message msg; |
141 | 147 | up20180655 | int good = 1; |
142 | 168 | up20180642 | |
143 | 183 | up20180642 | #ifdef DIOGO
|
144 | good = 0;
|
||
145 | #endif
|
||
146 | |||
147 | 147 | up20180655 | while (good) {
|
148 | /* Get a request message. */
|
||
149 | if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) { |
||
150 | printf("driver_receive failed with %d", r);
|
||
151 | continue;
|
||
152 | } |
||
153 | if (is_ipc_notify(ipc_status)) { /* received notification */ |
||
154 | switch (_ENDPOINT_P(msg.m_source)) {
|
||
155 | case HARDWARE: /* hardware interrupt notification */ |
||
156 | 153 | up20180655 | for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) { |
157 | if (msg.m_notify.interrupts & n) {
|
||
158 | interrupt_handler(i); |
||
159 | 167 | up20180655 | if ((scancode[0]) == ESC_BREAK_CODE) good = 0; |
160 | 171 | up20180655 | #ifdef TELMO
|
161 | 187 | up20180655 | if (i == 0) { |
162 | if (no_interrupts % refresh_count_value == 0) { |
||
163 | update_movement(shooter1); |
||
164 | 200 | up20180655 | update_scale(); |
165 | 192 | up20180642 | |
166 | 214 | up20180642 | ent_set_origin(bullet_get_x(bullet)-ent_get_XLength()/2.0, |
167 | bullet_get_y(bullet)-ent_get_YLength()/2.0); |
||
168 | |||
169 | 192 | up20180642 | sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y()); |
170 | 187 | up20180655 | double angle = get_mouse_angle(shooter1);
|
171 | 201 | up20180642 | gunner_set_angle(shooter1, angle - M_PI_2); |
172 | 187 | up20180655 | graph_clear_screen(); |
173 | 207 | up20180642 | |
174 | clock_t t = clock(); |
||
175 | |||
176 | sprite_draw(pink); |
||
177 | 201 | up20180642 | gunner_draw(shooter2); |
178 | gunner_draw(shooter1); |
||
179 | 204 | up20180655 | bullet_draw(bullet); |
180 | 207 | up20180642 | |
181 | t = clock()-t; printf("%d\n", (t*1000)/CLOCKS_PER_SEC); |
||
182 | |||
183 | 192 | up20180642 | sprite_draw(sp_crosshair); |
184 | 187 | up20180655 | graph_draw(); |
185 | } |
||
186 | 184 | up20180655 | } |
187 | 171 | up20180655 | #endif
|
188 | 153 | up20180655 | } |
189 | 147 | up20180655 | } |
190 | 187 | up20180655 | #ifdef TELMO
|
191 | if (counter_mouse_ih >= 3) { |
||
192 | struct packet pp = mouse_parse_packet(packet_mouse_ih);
|
||
193 | update_mouse_position(&pp); |
||
194 | 189 | up20180655 | //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
|
195 | 187 | up20180655 | counter_mouse_ih = 0;
|
196 | } |
||
197 | #endif
|
||
198 | 167 | up20180655 | |
199 | 147 | up20180655 | break;
|
200 | default:
|
||
201 | break; /* no other notifications expected: do nothing */ |
||
202 | } |
||
203 | } else { /* received standart message, not a notification */ |
||
204 | /* no standart message expected: do nothing */
|
||
205 | } |
||
206 | } |
||
207 | 149 | up20180655 | |
208 | 192 | up20180642 | #ifdef TELMO
|
209 | 201 | up20180642 | gunner_dtor(shooter1); shooter1 = NULL;
|
210 | 207 | up20180642 | sprite_dtor(pink); pink = NULL;
|
211 | 192 | up20180642 | #endif
|
212 | 188 | up20180642 | |
213 | 192 | up20180642 | basic_sprite_dtor(bsp_crosshair); bsp_crosshair = NULL;
|
214 | basic_sprite_dtor(bsp_shooter ); bsp_shooter = NULL;
|
||
215 | sprite_dtor (sp_crosshair ); sp_crosshair = NULL;
|
||
216 | font_dtor (consolas ); consolas = NULL;
|
||
217 | |||
218 | 153 | up20180655 | // Unsubscribe interrupts
|
219 | 155 | up20180655 | if (unsubscribe_all()) {
|
220 | if (cleanup())
|
||
221 | printf("%s: failed to cleanup.\n", __func__);
|
||
222 | 152 | up20180642 | return 1; |
223 | } |
||
224 | |||
225 | 155 | up20180655 | |
226 | if (cleanup()) {
|
||
227 | printf("%s: failed to cleanup.\n", __func__);
|
||
228 | 152 | up20180642 | return 1; |
229 | } |
||
230 | |||
231 | 149 | up20180655 | return 0; |
232 | 147 | up20180655 | } |