Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 294

History | View | Annotate | Download (12.6 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
static int (game)(void);
61

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

    
64
    int r;
65

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

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

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

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

    
91
        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

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

    
101
    ent_set_scale(DEFAULT_SCALE);
102

    
103
    text_timer_t *in_game_timer = timer_ctor(consolas);
104

    
105
    #ifndef DIOGO
106
        menu_t *main_menu = menu_ctor(consolas);
107
    #endif
108

    
109
    list_t *shooter_list = list_ctor();
110

    
111
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
112
    gunner_set_spawn(shooter1, 75, 75);
113
    gunner_set_pos(shooter1, 75, 75);
114

    
115
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
116
    gunner_set_spawn(shooter2, 975, 75);
117
    gunner_set_pos(shooter2, 775, 75);
118

    
119
    list_insert(shooter_list, list_end(shooter_list), shooter1);
120
    list_insert(shooter_list, list_end(shooter_list), shooter2);
121

    
122
    list_t *bullet_list  = list_ctor();
123

    
124
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
125
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
126

    
127
    #ifndef DIOGO
128
        //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
129
        uint8_t last_lb = 0;
130
        struct packet pp;
131
        keys_t *keys = get_key_presses();
132
    #endif
133
    /// loop stuff
134
    int ipc_status;
135
    message msg;
136
    int game_state = MENU;
137

    
138
    #ifdef DIOGO
139
        char buffer[1024]; // buffer
140
        int buffer_pos = 0;
141
    #endif
142

    
143
    #ifndef DIOGO
144
        int click = 0;
145
    #endif
146
    #ifdef DIOGO
147
        char *s = NULL;
148
    #endif
149

    
150
    while (game_state != EXIT) {
151
        /* Get a request message. */
152
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
153
            printf("driver_receive failed with %d", r);
154
            continue;
155
        }
156
        if (is_ipc_notify(ipc_status)) { /* received notification */
157
            switch (_ENDPOINT_P(msg.m_source)) {
158
                case HARDWARE: /* hardware interrupt notification */
159
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
160
                        if (msg.m_notify.interrupts & n) {
161
                            interrupt_handler(i);
162
                            switch (i) {
163
                            #ifndef DIOGO
164
                            case TIMER0_IRQ:
165
                                switch (game_state) {
166
                                case MENU:
167
                                    graph_clear_screen();
168
                                    switch(menu_update_state(main_menu, click)){
169
                                        case -1: game_state = MENU; break;
170
                                        case  0: game_state = GAME; game(); break;
171
                                        case  1: game_state = TEST; break;
172
                                        case  2: game_state = EXIT; break;
173
                                    }
174
                                    menu_draw(main_menu);
175

    
176
                                    click = 0;
177

    
178
                                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
179
                                    sprite_draw(sp_crosshair);
180
                                    graph_draw();
181
                                    break;
182
                                case GAME:
183
                                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
184
                                    update_movement(map1, shooter1, keys, shooter_list);
185

    
186
                                    update_game_state(map1, shooter_list, bullet_list);
187

    
188
                                    //update_scale();
189
                                    double angle = get_mouse_angle(shooter1);
190
                                    gunner_set_angle(shooter1, angle - M_PI_2);
191

    
192
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
193
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
194

    
195
                                    graph_clear_screen();
196
                                    map_draw   (map1);
197
                                    gunner_draw_list(shooter_list);
198
                                    bullet_draw_list(bullet_list);
199

    
200
                                    text_draw(in_game_timer->text);
201

    
202
                                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
203
                                    sprite_draw(sp_crosshair);
204
                                    graph_draw();
205
                                    break;
206
                                }
207
                                break;
208
                            #endif
209
                            case KBC_IRQ:
210
                                switch (game_state) {
211
                                case MENU:
212
                                    if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
213
                                    #ifdef DIOGO
214
                                    else if ((scancode[0]) == ENTER_MAKE_CODE) {
215
                                        buffer[buffer_pos] = '\0';
216
                                        printf("\nSending string -%s-", buffer);
217
                                        printf(" (output: %d)\n",
218
                                            hltp_send_string(buffer));
219
                                        buffer_pos = 0;
220
                                    }
221
                                    else {
222
                                        char c = map_makecode(scancode[0]);
223
                                        if (c == ERROR_CODE) break;
224
                                        buffer[buffer_pos++] = c;
225
                                        printf("%c", c);
226
                                    }
227
                                    #endif
228
                                    break;
229
                                case GAME:
230
                                    if ((scancode[0]) == ESC_BREAK_CODE) {
231
                                        game_state = MENU;
232
                                        // reset game
233
                                        while(list_size(bullet_list) > 0){
234
                                            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
235
                                            bullet_dtor(p);
236
                                        }
237
                                        list_node_t *it = list_begin(shooter_list);
238
                                        while (it != list_end(shooter_list)) {
239
                                            gunner_t *p = *(gunner_t**)list_node_val(it);
240
                                            get_random_spawn(map1, p);
241
                                            gunner_set_curr_health(p, gunner_get_health(p));
242
                                            it = list_node_next(it);
243
                                        }
244
                                        timer_reset(in_game_timer);
245
                                    }
246
                                    break;
247
                                }
248
                                break;
249
                            #ifndef DIOGO
250
                            case MOUSE_IRQ:
251
                                if (counter_mouse_ih >= 3) {
252
                                    mouse_parse_packet(packet_mouse_ih, &pp);
253
                                    update_mouse(&pp);
254
                                    switch (game_state) {
255
                                    case MENU:
256
                                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
257
                                        break;
258
                                    case GAME:
259
                                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
260
                                            shoot_bullet(shooter1, bullet_list, bsp_bullet);
261
                                        break;
262
                                    }
263
                                    last_lb = keys->lb_pressed;
264
                                    counter_mouse_ih = 0;
265

    
266
                                }
267
                                break;
268
                            #endif
269
                            #ifdef DIOGO
270
                            case COM1_IRQ:
271
                                nctp_ih();
272
                                break;
273
                            #endif
274
                            }
275
                        }
276
                    }
277

    
278
                    break;
279
                default:
280
                    break; /* no other notifications expected: do nothing */
281
            }
282
        } else { /* received standart message, not a notification */
283
            /* no standart message expected: do nothing */
284
        }
285
    }
286

    
287
    #ifdef DIOGO
288
        free(s);
289
    #endif
290

    
291
    while(list_size(shooter_list) > 0){
292
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
293
        gunner_dtor(p);
294
    }
295

    
296
    while(list_size(bullet_list) > 0){
297
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
298
        bullet_dtor(p);
299
    }
300
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
301
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
302

    
303
    timer_dtor(in_game_timer); in_game_timer = NULL;
304

    
305
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
306
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
307
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
308
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
309
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
310
    map_dtor               (map1         ); map1          = NULL;
311
    font_dtor              (consolas     ); consolas      = NULL;
312

    
313
    // Unsubscribe interrupts
314
    if (unsubscribe_all()) {
315
        if (cleanup())
316
            printf("%s: failed to cleanup.\n", __func__);
317
        return 1;
318
    }
319

    
320

    
321
    if (cleanup()) {
322
        printf("%s: failed to cleanup.\n", __func__);
323
        return 1;
324
    }
325

    
326
    return 0;
327
}
328

    
329
static int (game)(void){
330
    return SUCCESS;
331
}