Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 278

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 "i8254.h"
11
#include "timer.h"
12
#include "keyboard.h"
13
#include "mouse.h"
14
#include "graph.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 "sprite.h"
22
#include "rectangle.h"
23
#include "font.h"
24
#include "ent.h"
25

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

    
33
#include "list.h"
34

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

    
37
    lcf_set_language("EN-US");
38

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

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

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

    
45
    lcf_cleanup();
46

    
47
    return 0;
48
}
49

    
50
int(proj_main_loop)(int argc, char *argv[]) {
51

    
52
    int r;
53

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

    
57
    /// subscribe interrupts
58
    if (subscribe_all()) { return 1; }
59

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

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

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

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

    
96
    ent_set_scale(DEFAULT_SCALE);
97

    
98
    text_timer_t *in_game_timer = timer_ctor(consolas);
99

    
100
    #ifndef DIOGO
101
        menu_t *main_menu = menu_ctor(consolas);
102
    #endif
103

    
104
    list_t *shooter_list = list_ctor();
105

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

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

    
114
    list_insert(shooter_list, list_end(shooter_list), shooter1);
115
    list_insert(shooter_list, list_end(shooter_list), shooter2);
116

    
117
    list_t *bullet_list  = list_ctor();
118

    
119
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
120
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
121

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

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

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

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

    
169
                                        click = 0;
170

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

    
180
                                    update_game_state(map1, shooter_list, bullet_list);
181

    
182
                                    //update_scale();
183
                                    angle = get_mouse_angle(shooter1);
184
                                    gunner_set_angle(shooter1, angle - M_PI_2);
185

    
186
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
187
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
188

    
189
                                    graph_clear_screen();
190
                                    map_draw   (map1);
191
                                    gunner_draw_list(shooter_list);
192
                                    bullet_draw_list(bullet_list);
193

    
194
                                    text_draw(in_game_timer->text);
195

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

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

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

    
281
    #ifdef DIOGO
282
        free(s);
283
    #endif
284

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

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

    
297
    timer_dtor(in_game_timer); in_game_timer = NULL;
298

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

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

    
314

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

    
320
    return 0;
321
}