Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 295

History | View | Annotate | Download (13.3 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 291 up20180642
#include "menu.h"
15 234 up20180655
#include "rtc.h"
16 287 up20180642
#include "hltp.h"
17 153 up20180655
#include "interrupts_func.h"
18 270 up20180655
#include "makecode_map.h"
19 149 up20180655
20 179 up20180642
#include "graph.h"
21
#include "rectangle.h"
22 182 up20180642
#include "font.h"
23 192 up20180642
#include "ent.h"
24 168 up20180642
25 192 up20180642
#include "crosshair.h"
26
#include "shooter.h"
27
#include "pistol.h"
28
#include "nothing.h"
29 237 up20180642
#include "bullet.h"
30 216 up20180642
#include "map1.h"
31 183 up20180642
32 294 up20180642
#include "errors.h"
33
34 226 up20180642
#include "list.h"
35
36 144 up20180655
int main(int argc, char* argv[]) {
37
38
    lcf_set_language("EN-US");
39
40 235 up20180642
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
41 144 up20180655
42 189 up20180655
    lcf_log_output("/home/lcom/labs/proj/output.txt");
43 144 up20180655
44
    if (lcf_start(argc, argv)) return 1;
45
46
    lcf_cleanup();
47
48
    return 0;
49
}
50 147 up20180655
51 294 up20180642
font_t               *consolas      = NULL;
52
basic_sprite_t       *bsp_crosshair = NULL;
53
basic_sprite_t       *bsp_shooter   = NULL;
54
basic_sprite_t       *bsp_pistol    = NULL;
55
basic_sprite_t       *bsp_nothing   = NULL;
56
basic_sprite_t       *bsp_bullet    = NULL;
57
map_t                *map1          = NULL;
58
sprite_t             *sp_crosshair  = NULL;
59
60
static int (game)(void);
61
62 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
63 170 up20180642
64
    int r;
65
66 294 up20180642
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
67 261 up20180642
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
68 192 up20180642
69 261 up20180642
    /// subscribe interrupts
70
    if (subscribe_all()) { return 1; }
71 170 up20180642
72 269 up20180642
    #ifndef DIOGO
73
        /// initialize graphics
74
        if(graph_init(GRAPH_MODE)){
75
            printf("%s: failed to initalize graphics.\n", __func__);
76
            if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
77
            return 1;
78
        }
79
    #endif
80 152 up20180642
81 261 up20180642
    /// Load stuff
82
    {
83 269 up20180642
        #ifndef DIOGO
84
            graph_clear_screen();
85
            text_t *txt = text_ctor(consolas, "Loading...");
86
            text_draw(txt);
87
            text_dtor(txt);
88
            graph_draw();
89
        #endif
90 183 up20180642
91 261 up20180642
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
92
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
93
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
94
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
95
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
96
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
97 226 up20180642
98 261 up20180642
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
99
    }
100 226 up20180642
101 262 up20180642
    #ifndef DIOGO
102
        menu_t *main_menu = menu_ctor(consolas);
103
    #endif
104 234 up20180655
105 262 up20180642
    #ifndef DIOGO
106
        //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
107
        uint8_t last_lb = 0;
108
        struct packet pp;
109
        keys_t *keys = get_key_presses();
110
    #endif
111 261 up20180642
    /// loop stuff
112
    int ipc_status;
113
    message msg;
114 267 up20180655
115 273 up20180642
    #ifdef DIOGO
116
        char buffer[1024]; // buffer
117
        int buffer_pos = 0;
118
    #endif
119 267 up20180655
120 262 up20180642
    #ifndef DIOGO
121
        int click = 0;
122
    #endif
123 269 up20180642
    #ifdef DIOGO
124
        char *s = NULL;
125
    #endif
126 236 up20180642
127 295 up20180642
    int good = true;
128
129
    while (good) {
130 261 up20180642
        /* Get a request message. */
131
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
132
            printf("driver_receive failed with %d", r);
133
            continue;
134
        }
135
        if (is_ipc_notify(ipc_status)) { /* received notification */
136
            switch (_ENDPOINT_P(msg.m_source)) {
137
                case HARDWARE: /* hardware interrupt notification */
138
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
139
                        if (msg.m_notify.interrupts & n) {
140
                            interrupt_handler(i);
141
                            switch (i) {
142
                            #ifndef DIOGO
143
                            case TIMER0_IRQ:
144 192 up20180642
145 295 up20180642
                                graph_clear_screen();
146
                                switch(menu_update_state(main_menu, click)){
147
                                    case -1: break;
148
                                    case  0: game(); break;
149
                                    case  1: good = false; break;
150
                                    case  2: good = false; break;
151
                                }
152
                                menu_draw(main_menu);
153 192 up20180642
154 295 up20180642
                                click = 0;
155 202 up20180655
156 295 up20180642
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
157
                                sprite_draw(sp_crosshair);
158
                                graph_draw();
159 231 up20180655
160 261 up20180642
                                break;
161 263 up20180642
                            #endif
162 261 up20180642
                            case KBC_IRQ:
163 295 up20180642
                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
164
                                #ifdef DIOGO
165
                                else if ((scancode[0]) == ENTER_MAKE_CODE) {
166
                                    buffer[buffer_pos] = '\0';
167
                                    printf("\nSending string -%s-", buffer);
168
                                    printf(" (output: %d)\n",
169
                                        hltp_send_string(buffer));
170
                                    buffer_pos = 0;
171 261 up20180642
                                }
172 295 up20180642
                                else {
173
                                    char c = map_makecode(scancode[0]);
174
                                    if (c == ERROR_CODE) break;
175
                                    buffer[buffer_pos++] = c;
176
                                    printf("%c", c);
177
                                }
178
                                #endif
179 263 up20180642
                            #ifndef DIOGO
180 261 up20180642
                            case MOUSE_IRQ:
181
                                if (counter_mouse_ih >= 3) {
182
                                    mouse_parse_packet(packet_mouse_ih, &pp);
183
                                    update_mouse(&pp);
184 295 up20180642
                                    if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
185 261 up20180642
                                    last_lb = keys->lb_pressed;
186
                                    counter_mouse_ih = 0;
187 220 up20180655
                                }
188 261 up20180642
                                break;
189
                            #endif
190
                            #ifdef DIOGO
191
                            case COM1_IRQ:
192 275 up20180642
                                nctp_ih();
193
                                break;
194 261 up20180642
                            #endif
195 220 up20180655
                            }
196 251 up20180655
                        }
197 261 up20180642
                    }
198 167 up20180655
199 261 up20180642
                    break;
200
                default:
201
                    break; /* no other notifications expected: do nothing */
202 147 up20180655
            }
203 261 up20180642
        } else { /* received standart message, not a notification */
204
            /* no standart message expected: do nothing */
205 147 up20180655
        }
206 261 up20180642
    }
207 149 up20180655
208 261 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
209
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
210
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
211
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
212
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
213
    map_dtor               (map1         ); map1          = NULL;
214
    font_dtor              (consolas     ); consolas      = NULL;
215 243 up20180642
216 261 up20180642
    // Unsubscribe interrupts
217
    if (unsubscribe_all()) {
218
        if (cleanup())
219
            printf("%s: failed to cleanup.\n", __func__);
220
        return 1;
221
    }
222 188 up20180642
223 192 up20180642
224 261 up20180642
    if (cleanup()) {
225
        printf("%s: failed to cleanup.\n", __func__);
226
        return 1;
227
    }
228 155 up20180655
229 149 up20180655
    return 0;
230 147 up20180655
}
231 294 up20180642
232
static int (game)(void){
233 295 up20180642
234
    int r;
235
236
    ent_set_scale(DEFAULT_SCALE);
237
    text_timer_t *in_game_timer = timer_ctor(consolas);
238
239
    list_t *shooter_list = list_ctor();
240
241
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
242
    gunner_set_spawn(shooter1, 75, 75);
243
    gunner_set_pos(shooter1, 75, 75);
244
245
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
246
    gunner_set_spawn(shooter2, 975, 75);
247
    gunner_set_pos(shooter2, 775, 75);
248
249
    list_insert(shooter_list, list_end(shooter_list), shooter1);
250
    list_insert(shooter_list, list_end(shooter_list), shooter2);
251
252
    list_t *bullet_list  = list_ctor();
253
254
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
255
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
256
257
   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
258
   uint8_t last_lb = 0;
259
   struct packet pp;
260
   keys_t *keys = get_key_presses();
261
262
    /// loop stuff
263
    int ipc_status;
264
    message msg;
265
    int game_state = GAME;
266
267
    while (game_state == GAME) {
268
       /* Get a request message. */
269
       if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
270
           printf("driver_receive failed with %d", r);
271
           continue;
272
       }
273
       if (is_ipc_notify(ipc_status)) { /* received notification */
274
           switch (_ENDPOINT_P(msg.m_source)) {
275
               case HARDWARE: /* hardware interrupt notification */
276
                   for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
277
                       if (msg.m_notify.interrupts & n) {
278
                           interrupt_handler(i);
279
                           switch (i) {
280
                           case TIMER0_IRQ:
281
282
                               if (no_interrupts % 60 == 0) timer_update(in_game_timer);
283
                               update_movement(map1, shooter1, keys, shooter_list);
284
285
                               update_game_state(map1, shooter_list, bullet_list);
286
287
                               //update_scale();
288
                               double angle = get_mouse_angle(shooter1);
289
                               gunner_set_angle(shooter1, angle - M_PI_2);
290
291
                               ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
292
                                              gunner_get_y(shooter1)-ent_get_YLength()/2.0);
293
294
                               graph_clear_screen();
295
                               map_draw   (map1);
296
                               gunner_draw_list(shooter_list);
297
                               bullet_draw_list(bullet_list);
298
299
                               text_draw(in_game_timer->text);
300
301
                               sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
302
                               sprite_draw(sp_crosshair);
303
                               graph_draw();
304
305
                               break;
306
                           case KBC_IRQ:
307
                               if ((scancode[0]) == ESC_BREAK_CODE) {
308
                                   game_state = MENU;
309
                                   // reset game
310
                                   while(list_size(bullet_list) > 0){
311
                                       bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
312
                                       bullet_dtor(p);
313
                                   }
314
                                   list_node_t *it = list_begin(shooter_list);
315
                                   while (it != list_end(shooter_list)) {
316
                                       gunner_t *p = *(gunner_t**)list_node_val(it);
317
                                       get_random_spawn(map1, p);
318
                                       gunner_set_curr_health(p, gunner_get_health(p));
319
                                       it = list_node_next(it);
320
                                   }
321
                                   timer_reset(in_game_timer);
322
                               }
323
                               break;
324
                           case MOUSE_IRQ:
325
                               if (counter_mouse_ih >= 3) {
326
                                   mouse_parse_packet(packet_mouse_ih, &pp);
327
                                   update_mouse(&pp);
328
                                   if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
329
                                       shoot_bullet(shooter1, bullet_list, bsp_bullet);
330
                                   last_lb = keys->lb_pressed;
331
                                   counter_mouse_ih = 0;
332
333
                               }
334
                               break;
335
                           }
336
                       }
337
                   }
338
                   break;
339
               default:
340
                   break; /* no other notifications expected: do nothing */
341
           }
342
       } else { /* received standart message, not a notification */
343
           /* no standart message expected: do nothing */
344
       }
345
    }
346
347
    #ifdef DIOGO
348
        free(s);
349
    #endif
350
351
    while(list_size(shooter_list) > 0){
352
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
353
        gunner_dtor(p);
354
    }
355
356
    while(list_size(bullet_list) > 0){
357
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
358
        bullet_dtor(p);
359
    }
360
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
361
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
362
363
    timer_dtor(in_game_timer); in_game_timer = NULL;
364
365 294 up20180642
    return SUCCESS;
366
}