Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 296

History | View | Annotate | Download (13.3 KB)

1
#include <lcom/lcf.h>
2
#include <lcom/proj.h>
3
#include <lcom/liblm.h>
4
#include <math.h>
5

    
6
#include "proj_macros.h"
7
#include "proj_func.h"
8

    
9
#include "kbc.h"
10
#include "timer.h"
11
#include "keyboard.h"
12
#include "mouse.h"
13
#include "graph.h"
14
#include "menu.h"
15
#include "rtc.h"
16
#include "hltp.h"
17
#include "interrupts_func.h"
18
#include "makecode_map.h"
19

    
20
#include "graph.h"
21
#include "rectangle.h"
22
#include "font.h"
23
#include "ent.h"
24

    
25
#include "crosshair.h"
26
#include "shooter.h"
27
#include "pistol.h"
28
#include "nothing.h"
29
#include "bullet.h"
30
#include "map1.h"
31

    
32
#include "errors.h"
33

    
34
#include "list.h"
35

    
36
int main(int argc, char* argv[]) {
37

    
38
    lcf_set_language("EN-US");
39

    
40
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
41

    
42
    lcf_log_output("/home/lcom/labs/proj/output.txt");
43

    
44
    if (lcf_start(argc, argv)) return 1;
45

    
46
    lcf_cleanup();
47

    
48
    return 0;
49
}
50

    
51
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
int (game)(void);
61
int (chat)(void);
62

    
63
int(proj_main_loop)(int argc, char *argv[]) {
64

    
65
    int r;
66

    
67
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
68
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
69

    
70
    /// subscribe interrupts
71
    if (subscribe_all()) { return 1; }
72

    
73
    #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

    
82
    /// Load stuff
83
    {
84
        #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

    
92
        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

    
99
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
100
    }
101

    
102
    #ifndef DIOGO
103
        menu_t *main_menu = menu_ctor(consolas);
104
        menu_add_item(main_menu, "Play");
105
        menu_add_item(main_menu, "Chat");
106
        menu_add_item(main_menu, "Exit");
107
    #endif
108

    
109
    #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
    /// loop stuff
116
    int ipc_status;
117
    message msg;
118

    
119
    #ifdef DIOGO
120
        char buffer[1024]; // buffer
121
        int buffer_pos = 0;
122
    #endif
123

    
124
    #ifndef DIOGO
125
        int click = 0;
126
    #endif
127

    
128
    int good = true;
129

    
130
    while (good) {
131
        /* 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

    
146
                                graph_clear_screen();
147
                                switch(menu_update_state(main_menu, click)){
148
                                    case -1: break;
149
                                    case  0: game(); break;
150
                                    case  1: chat(); break;
151
                                    case  2: good = false; break;
152
                                }
153
                                menu_draw(main_menu);
154

    
155
                                click = 0;
156

    
157
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
158
                                sprite_draw(sp_crosshair);
159
                                graph_draw();
160

    
161
                                break;
162
                            #endif
163
                            case KBC_IRQ:
164
                                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
                                }
173
                                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
                            #ifndef DIOGO
181
                            case MOUSE_IRQ:
182
                                if (counter_mouse_ih >= 3) {
183
                                    mouse_parse_packet(packet_mouse_ih, &pp);
184
                                    update_mouse(&pp);
185
                                    if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
186
                                    last_lb = keys->lb_pressed;
187
                                    counter_mouse_ih = 0;
188
                                }
189
                                break;
190
                            #endif
191
                            #ifdef DIOGO
192
                            case COM1_IRQ:
193
                                nctp_ih();
194
                                break;
195
                            #endif
196
                            }
197
                        }
198
                    }
199

    
200
                    break;
201
                default:
202
                    break; /* no other notifications expected: do nothing */
203
            }
204
        } else { /* received standart message, not a notification */
205
            /* no standart message expected: do nothing */
206
        }
207
    }
208

    
209
    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

    
217
    // Unsubscribe interrupts
218
    if (unsubscribe_all()) {
219
        if (cleanup())
220
            printf("%s: failed to cleanup.\n", __func__);
221
        return 1;
222
    }
223

    
224

    
225
    if (cleanup()) {
226
        printf("%s: failed to cleanup.\n", __func__);
227
        return 1;
228
    }
229

    
230
    return 0;
231
}
232

    
233
int (game)(void){
234

    
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
    int good = true;
267

    
268
    while (good) {
269
       /* 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
                                   good = false;
310
                                   // 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
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
353

    
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
    return SUCCESS;
363
}
364

    
365
int (chat)(void){
366
    return SUCCESS;
367
}