Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 296

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