Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 286

History | View | Annotate | Download (12.4 KB)

1 144 up20180655
#include <lcom/lcf.h>
2
#include <lcom/proj.h>
3
#include <lcom/liblm.h>
4 185 up20180655
#include <math.h>
5 144 up20180655
6 149 up20180655
#include "proj_macros.h"
7 179 up20180642
#include "proj_func.h"
8 147 up20180655
9 149 up20180655
#include "kbc.h"
10
#include "timer.h"
11
#include "keyboard.h"
12 150 up20180655
#include "mouse.h"
13 189 up20180655
#include "graph.h"
14 234 up20180655
#include "rtc.h"
15 282 up20180642
#include "uart.h"
16 153 up20180655
#include "interrupts_func.h"
17 270 up20180655
#include "makecode_map.h"
18 149 up20180655
19 179 up20180642
#include "graph.h"
20
#include "rectangle.h"
21 182 up20180642
#include "font.h"
22 192 up20180642
#include "ent.h"
23 168 up20180642
24 192 up20180642
#include "crosshair.h"
25
#include "shooter.h"
26
#include "pistol.h"
27
#include "nothing.h"
28 237 up20180642
#include "bullet.h"
29 216 up20180642
#include "map1.h"
30 183 up20180642
31 226 up20180642
#include "list.h"
32
33 144 up20180655
int main(int argc, char* argv[]) {
34
35
    lcf_set_language("EN-US");
36
37 235 up20180642
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
38 144 up20180655
39 189 up20180655
    lcf_log_output("/home/lcom/labs/proj/output.txt");
40 144 up20180655
41
    if (lcf_start(argc, argv)) return 1;
42
43
    lcf_cleanup();
44
45
    return 0;
46
}
47 147 up20180655
48 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
49 170 up20180642
50
    int r;
51
52 261 up20180642
    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 192 up20180642
55 261 up20180642
    /// subscribe interrupts
56
    if (subscribe_all()) { return 1; }
57 170 up20180642
58 269 up20180642
    #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 152 up20180642
67 261 up20180642
    /// 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 269 up20180642
        #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 183 up20180642
84 261 up20180642
        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 226 up20180642
91 261 up20180642
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
92
    }
93 226 up20180642
94 261 up20180642
    ent_set_scale(DEFAULT_SCALE);
95 183 up20180642
96 261 up20180642
    text_timer_t *in_game_timer = timer_ctor(consolas);
97 234 up20180655
98 262 up20180642
    #ifndef DIOGO
99
        menu_t *main_menu = menu_ctor(consolas);
100
    #endif
101 234 up20180655
102 261 up20180642
    list_t *shooter_list = list_ctor();
103 234 up20180655
104 261 up20180642
    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 234 up20180655
108 261 up20180642
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
109
    gunner_set_spawn(shooter2, 975, 75);
110
    gunner_set_pos(shooter2, 775, 75);
111 152 up20180642
112 261 up20180642
    list_insert(shooter_list, list_end(shooter_list), shooter1);
113
    list_insert(shooter_list, list_end(shooter_list), shooter2);
114 250 up20180655
115 261 up20180642
    list_t *bullet_list  = list_ctor();
116 251 up20180655
117 261 up20180642
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
118
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
119 192 up20180642
120 262 up20180642
    #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 261 up20180642
    /// loop stuff
129
    int ipc_status;
130
    message msg;
131 262 up20180642
    int game_state = MENU;
132 267 up20180655
133 273 up20180642
    #ifdef DIOGO
134
        char buffer[1024]; // buffer
135
        int buffer_pos = 0;
136
    #endif
137 267 up20180655
138 262 up20180642
    #ifndef DIOGO
139
        int click = 0;
140
    #endif
141 269 up20180642
    #ifdef DIOGO
142
        char *s = NULL;
143
    #endif
144 236 up20180642
145 261 up20180642
    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 192 up20180642
167 261 up20180642
                                        click = 0;
168 192 up20180642
169 261 up20180642
                                        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 202 up20180655
178 261 up20180642
                                    update_game_state(map1, shooter_list, bullet_list);
179 231 up20180655
180 261 up20180642
                                    //update_scale();
181
                                    angle = get_mouse_angle(shooter1);
182
                                    gunner_set_angle(shooter1, angle - M_PI_2);
183 231 up20180655
184 261 up20180642
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
185
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
186 171 up20180655
187 261 up20180642
                                    graph_clear_screen();
188
                                    map_draw   (map1);
189
                                    gunner_draw_list(shooter_list);
190
                                    bullet_draw_list(bullet_list);
191 168 up20180642
192 261 up20180642
                                    text_draw(in_game_timer->text);
193 183 up20180642
194 261 up20180642
                                    sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
195
                                    sprite_draw(sp_crosshair);
196
                                    graph_draw();
197
                                    break;
198
                                }
199
                                break;
200 263 up20180642
                            #endif
201 261 up20180642
                            case KBC_IRQ:
202
                                switch (game_state) {
203
                                case MENU:
204
                                    if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
205 273 up20180642
                                    #ifdef DIOGO
206 267 up20180655
                                    else if ((scancode[0]) == ENTER_MAKE_CODE) {
207 269 up20180642
                                        buffer[buffer_pos] = '\0';
208 274 up20180642
                                        printf("\nSending string -%s-", buffer);
209
                                        printf(" (output: %d)\n",
210 275 up20180642
                                            hltp_send_string(buffer));
211 267 up20180655
                                        buffer_pos = 0;
212
                                    }
213 270 up20180655
                                    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 273 up20180642
                                    #endif
220 261 up20180642
                                    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 251 up20180655
                                        }
229 261 up20180642
                                        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 243 up20180642
                                    }
238 251 up20180655
                                    break;
239 261 up20180642
                                }
240
                                break;
241 263 up20180642
                            #ifndef DIOGO
242 261 up20180642
                            case MOUSE_IRQ:
243
                                if (counter_mouse_ih >= 3) {
244
                                    mouse_parse_packet(packet_mouse_ih, &pp);
245
                                    update_mouse(&pp);
246 251 up20180655
                                    switch (game_state) {
247
                                    case MENU:
248 261 up20180642
                                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
249 251 up20180655
                                        break;
250
                                    case GAME:
251 261 up20180642
                                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
252
                                            shoot_bullet(shooter1, bullet_list, bsp_bullet);
253 251 up20180655
                                        break;
254 231 up20180655
                                    }
255 261 up20180642
                                    last_lb = keys->lb_pressed;
256
                                    counter_mouse_ih = 0;
257 251 up20180655
258 220 up20180655
                                }
259 261 up20180642
                                break;
260
                            #endif
261
                            #ifdef DIOGO
262
                            case COM1_IRQ:
263 275 up20180642
                                nctp_ih();
264
                                break;
265 261 up20180642
                            #endif
266 220 up20180655
                            }
267 251 up20180655
                        }
268 261 up20180642
                    }
269 167 up20180655
270 261 up20180642
                    break;
271
                default:
272
                    break; /* no other notifications expected: do nothing */
273 147 up20180655
            }
274 261 up20180642
        } else { /* received standart message, not a notification */
275
            /* no standart message expected: do nothing */
276 147 up20180655
        }
277 261 up20180642
    }
278 149 up20180655
279 269 up20180642
    #ifdef DIOGO
280
        free(s);
281
    #endif
282
283 261 up20180642
    while(list_size(shooter_list) > 0){
284
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
285
        gunner_dtor(p);
286
    }
287 236 up20180642
288 261 up20180642
    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 243 up20180642
295 261 up20180642
    timer_dtor(in_game_timer); in_game_timer = NULL;
296 250 up20180655
297 261 up20180642
    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 243 up20180642
305 261 up20180642
    // Unsubscribe interrupts
306
    if (unsubscribe_all()) {
307
        if (cleanup())
308
            printf("%s: failed to cleanup.\n", __func__);
309
        return 1;
310
    }
311 188 up20180642
312 192 up20180642
313 261 up20180642
    if (cleanup()) {
314
        printf("%s: failed to cleanup.\n", __func__);
315
        return 1;
316
    }
317 155 up20180655
318 149 up20180655
    return 0;
319 147 up20180655
}