Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 289

History | View | Annotate | Download (12.4 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 "rtc.h"
15
#include "hltp.h"
16
#include "interrupts_func.h"
17
#include "makecode_map.h"
18

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

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

    
31
#include "list.h"
32

    
33
int main(int argc, char* argv[]) {
34

    
35
    lcf_set_language("EN-US");
36

    
37
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
38

    
39
    lcf_log_output("/home/lcom/labs/proj/output.txt");
40

    
41
    if (lcf_start(argc, argv)) return 1;
42

    
43
    lcf_cleanup();
44

    
45
    return 0;
46
}
47

    
48
int(proj_main_loop)(int argc, char *argv[]) {
49

    
50
    int r;
51

    
52
    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
53
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
54

    
55
    /// subscribe interrupts
56
    if (subscribe_all()) { return 1; }
57

    
58
    #ifndef DIOGO
59
        /// initialize graphics
60
        if(graph_init(GRAPH_MODE)){
61
            printf("%s: failed to initalize graphics.\n", __func__);
62
            if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
63
            return 1;
64
        }
65
    #endif
66

    
67
    /// Load stuff
68
    basic_sprite_t       *bsp_crosshair = NULL;
69
    basic_sprite_t       *bsp_shooter   = NULL;
70
    basic_sprite_t       *bsp_pistol    = NULL;
71
    basic_sprite_t       *bsp_nothing   = NULL;
72
    basic_sprite_t       *bsp_bullet    = NULL;
73
    map_t                *map1          = NULL;
74
    sprite_t             *sp_crosshair  = NULL;
75
    {
76
        #ifndef DIOGO
77
            graph_clear_screen();
78
            text_t *txt = text_ctor(consolas, "Loading...");
79
            text_draw(txt);
80
            text_dtor(txt);
81
            graph_draw();
82
        #endif
83

    
84
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
85
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
86
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
87
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
88
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
89
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
90

    
91
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
92
    }
93

    
94
    ent_set_scale(DEFAULT_SCALE);
95

    
96
    text_timer_t *in_game_timer = timer_ctor(consolas);
97

    
98
    #ifndef DIOGO
99
        menu_t *main_menu = menu_ctor(consolas);
100
    #endif
101

    
102
    list_t *shooter_list = list_ctor();
103

    
104
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
105
    gunner_set_spawn(shooter1, 75, 75);
106
    gunner_set_pos(shooter1, 75, 75);
107

    
108
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
109
    gunner_set_spawn(shooter2, 975, 75);
110
    gunner_set_pos(shooter2, 775, 75);
111

    
112
    list_insert(shooter_list, list_end(shooter_list), shooter1);
113
    list_insert(shooter_list, list_end(shooter_list), shooter2);
114

    
115
    list_t *bullet_list  = list_ctor();
116

    
117
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
118
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
119

    
120
    #ifndef DIOGO
121
        //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
122
        double angle; // mouse angle
123
        int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y();
124
        uint8_t last_lb = 0;
125
        struct packet pp;
126
        keys_t *keys = get_key_presses();
127
    #endif
128
    /// loop stuff
129
    int ipc_status;
130
    message msg;
131
    int game_state = MENU;
132

    
133
    #ifdef DIOGO
134
        char buffer[1024]; // buffer
135
        int buffer_pos = 0;
136
    #endif
137

    
138
    #ifndef DIOGO
139
        int click = 0;
140
    #endif
141
    #ifdef DIOGO
142
        char *s = NULL;
143
    #endif
144

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

    
167
                                        click = 0;
168

    
169
                                        sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
170
                                        sprite_draw(sp_crosshair);
171
                                        graph_draw();
172
                                    }
173
                                    break;
174
                                case GAME:
175
                                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
176
                                    update_movement(map1, shooter1, keys, shooter_list);
177

    
178
                                    update_game_state(map1, shooter_list, bullet_list);
179

    
180
                                    //update_scale();
181
                                    angle = get_mouse_angle(shooter1);
182
                                    gunner_set_angle(shooter1, angle - M_PI_2);
183

    
184
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
185
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
186

    
187
                                    graph_clear_screen();
188
                                    map_draw   (map1);
189
                                    gunner_draw_list(shooter_list);
190
                                    bullet_draw_list(bullet_list);
191

    
192
                                    text_draw(in_game_timer->text);
193

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

    
258
                                }
259
                                break;
260
                            #endif
261
                            #ifdef DIOGO
262
                            case COM1_IRQ:
263
                                nctp_ih();
264
                                break;
265
                            #endif
266
                            }
267
                        }
268
                    }
269

    
270
                    break;
271
                default:
272
                    break; /* no other notifications expected: do nothing */
273
            }
274
        } else { /* received standart message, not a notification */
275
            /* no standart message expected: do nothing */
276
        }
277
    }
278

    
279
    #ifdef DIOGO
280
        free(s);
281
    #endif
282

    
283
    while(list_size(shooter_list) > 0){
284
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
285
        gunner_dtor(p);
286
    }
287

    
288
    while(list_size(bullet_list) > 0){
289
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
290
        bullet_dtor(p);
291
    }
292
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
293
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
294

    
295
    timer_dtor(in_game_timer); in_game_timer = NULL;
296

    
297
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
298
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
299
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
300
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
301
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
302
    map_dtor               (map1         ); map1          = NULL;
303
    font_dtor              (consolas     ); consolas      = NULL;
304

    
305
    // Unsubscribe interrupts
306
    if (unsubscribe_all()) {
307
        if (cleanup())
308
            printf("%s: failed to cleanup.\n", __func__);
309
        return 1;
310
    }
311

    
312

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

    
318
    return 0;
319
}