Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 290

History | View | Annotate | Download (12.2 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
                                    graph_clear_screen();
163
                                    game_state = menu_update_state(main_menu, click);
164
                                    menu_draw(main_menu);
165

    
166
                                    click = 0;
167

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

    
176
                                    update_game_state(map1, shooter_list, bullet_list);
177

    
178
                                    //update_scale();
179
                                    angle = get_mouse_angle(shooter1);
180
                                    gunner_set_angle(shooter1, angle - M_PI_2);
181

    
182
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
183
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
184

    
185
                                    graph_clear_screen();
186
                                    map_draw   (map1);
187
                                    gunner_draw_list(shooter_list);
188
                                    bullet_draw_list(bullet_list);
189

    
190
                                    text_draw(in_game_timer->text);
191

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

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

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

    
277
    #ifdef DIOGO
278
        free(s);
279
    #endif
280

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

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

    
293
    timer_dtor(in_game_timer); in_game_timer = NULL;
294

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

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

    
310

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

    
316
    return 0;
317
}