Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 284

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 "sprite.h"
21
#include "rectangle.h"
22 182 up20180642
#include "font.h"
23 192 up20180642
#include "ent.h"
24 168 up20180642
25 192 up20180642
#include "crosshair.h"
26
#include "shooter.h"
27
#include "pistol.h"
28
#include "nothing.h"
29 237 up20180642
#include "bullet.h"
30 216 up20180642
#include "map1.h"
31 183 up20180642
32 226 up20180642
#include "list.h"
33
34 144 up20180655
int main(int argc, char* argv[]) {
35
36
    lcf_set_language("EN-US");
37
38 235 up20180642
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
39 144 up20180655
40 189 up20180655
    lcf_log_output("/home/lcom/labs/proj/output.txt");
41 144 up20180655
42
    if (lcf_start(argc, argv)) return 1;
43
44
    lcf_cleanup();
45
46
    return 0;
47
}
48 147 up20180655
49 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
50 170 up20180642
51
    int r;
52
53 261 up20180642
    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
54
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
55 192 up20180642
56 261 up20180642
    /// subscribe interrupts
57
    if (subscribe_all()) { return 1; }
58 170 up20180642
59 269 up20180642
    #ifndef DIOGO
60
        /// initialize graphics
61
        if(graph_init(GRAPH_MODE)){
62
            printf("%s: failed to initalize graphics.\n", __func__);
63
            if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
64
            return 1;
65
        }
66
    #endif
67 152 up20180642
68 261 up20180642
    /// Load stuff
69
    basic_sprite_t       *bsp_crosshair = NULL;
70
    basic_sprite_t       *bsp_shooter   = NULL;
71
    basic_sprite_t       *bsp_pistol    = NULL;
72
    basic_sprite_t       *bsp_nothing   = NULL;
73
    basic_sprite_t       *bsp_bullet    = NULL;
74
    map_t                *map1          = NULL;
75
    sprite_t             *sp_crosshair  = NULL;
76
    {
77 269 up20180642
        #ifndef DIOGO
78
            graph_clear_screen();
79
            text_t *txt = text_ctor(consolas, "Loading...");
80
            text_draw(txt);
81
            text_dtor(txt);
82
            graph_draw();
83
        #endif
84 183 up20180642
85 261 up20180642
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
86
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
87
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
88
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
89
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
90
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
91 226 up20180642
92 261 up20180642
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
93
    }
94 226 up20180642
95 261 up20180642
    ent_set_scale(DEFAULT_SCALE);
96 183 up20180642
97 261 up20180642
    text_timer_t *in_game_timer = timer_ctor(consolas);
98 234 up20180655
99 262 up20180642
    #ifndef DIOGO
100
        menu_t *main_menu = menu_ctor(consolas);
101
    #endif
102 234 up20180655
103 261 up20180642
    list_t *shooter_list = list_ctor();
104 234 up20180655
105 261 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
106
    gunner_set_spawn(shooter1, 75, 75);
107
    gunner_set_pos(shooter1, 75, 75);
108 234 up20180655
109 261 up20180642
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
110
    gunner_set_spawn(shooter2, 975, 75);
111
    gunner_set_pos(shooter2, 775, 75);
112 152 up20180642
113 261 up20180642
    list_insert(shooter_list, list_end(shooter_list), shooter1);
114
    list_insert(shooter_list, list_end(shooter_list), shooter2);
115 250 up20180655
116 261 up20180642
    list_t *bullet_list  = list_ctor();
117 251 up20180655
118 261 up20180642
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
119
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
120 192 up20180642
121 262 up20180642
    #ifndef DIOGO
122
        //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
123
        double angle; // mouse angle
124
        int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y();
125
        uint8_t last_lb = 0;
126
        struct packet pp;
127
        keys_t *keys = get_key_presses();
128
    #endif
129 261 up20180642
    /// loop stuff
130
    int ipc_status;
131
    message msg;
132 262 up20180642
    int game_state = MENU;
133 267 up20180655
134 273 up20180642
    #ifdef DIOGO
135
        char buffer[1024]; // buffer
136
        int buffer_pos = 0;
137
    #endif
138 267 up20180655
139 262 up20180642
    #ifndef DIOGO
140
        int click = 0;
141
    #endif
142 269 up20180642
    #ifdef DIOGO
143
        char *s = NULL;
144
    #endif
145 236 up20180642
146 261 up20180642
    while (game_state != EXIT) {
147
        /* Get a request message. */
148
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
149
            printf("driver_receive failed with %d", r);
150
            continue;
151
        }
152
        if (is_ipc_notify(ipc_status)) { /* received notification */
153
            switch (_ENDPOINT_P(msg.m_source)) {
154
                case HARDWARE: /* hardware interrupt notification */
155
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
156
                        if (msg.m_notify.interrupts & n) {
157
                            interrupt_handler(i);
158
                            switch (i) {
159
                            #ifndef DIOGO
160
                            case TIMER0_IRQ:
161
                                switch (game_state) {
162
                                case MENU:
163
                                    if (no_interrupts % 2 == 0){
164
                                        graph_clear_screen();
165
                                        game_state = menu_update_state(main_menu, click);
166
                                        menu_draw(main_menu);
167 192 up20180642
168 261 up20180642
                                        click = 0;
169 192 up20180642
170 261 up20180642
                                        sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
171
                                        sprite_draw(sp_crosshair);
172
                                        graph_draw();
173
                                    }
174
                                    break;
175
                                case GAME:
176
                                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
177
                                    update_movement(map1, shooter1, keys, shooter_list);
178 202 up20180655
179 261 up20180642
                                    update_game_state(map1, shooter_list, bullet_list);
180 231 up20180655
181 261 up20180642
                                    //update_scale();
182
                                    angle = get_mouse_angle(shooter1);
183
                                    gunner_set_angle(shooter1, angle - M_PI_2);
184 231 up20180655
185 261 up20180642
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
186
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
187 171 up20180655
188 261 up20180642
                                    graph_clear_screen();
189
                                    map_draw   (map1);
190
                                    gunner_draw_list(shooter_list);
191
                                    bullet_draw_list(bullet_list);
192 168 up20180642
193 261 up20180642
                                    text_draw(in_game_timer->text);
194 183 up20180642
195 261 up20180642
                                    sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
196
                                    sprite_draw(sp_crosshair);
197
                                    graph_draw();
198
                                    break;
199
                                }
200
                                break;
201 263 up20180642
                            #endif
202 261 up20180642
                            case KBC_IRQ:
203
                                switch (game_state) {
204
                                case MENU:
205
                                    if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
206 273 up20180642
                                    #ifdef DIOGO
207 267 up20180655
                                    else if ((scancode[0]) == ENTER_MAKE_CODE) {
208 269 up20180642
                                        buffer[buffer_pos] = '\0';
209 274 up20180642
                                        printf("\nSending string -%s-", buffer);
210
                                        printf(" (output: %d)\n",
211 275 up20180642
                                            hltp_send_string(buffer));
212 267 up20180655
                                        buffer_pos = 0;
213
                                    }
214 270 up20180655
                                    else {
215
                                        char c = map_makecode(scancode[0]);
216
                                        if (c == ERROR_CODE) break;
217
                                        buffer[buffer_pos++] = c;
218
                                        printf("%c", c);
219
                                    }
220 273 up20180642
                                    #endif
221 261 up20180642
                                    break;
222
                                case GAME:
223
                                    if ((scancode[0]) == ESC_BREAK_CODE) {
224
                                        game_state = MENU;
225
                                        // reset game
226
                                        while(list_size(bullet_list) > 0){
227
                                            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
228
                                            bullet_dtor(p);
229 251 up20180655
                                        }
230 261 up20180642
                                        list_node_t *it = list_begin(shooter_list);
231
                                        while (it != list_end(shooter_list)) {
232
                                            gunner_t *p = *(gunner_t**)list_node_val(it);
233
                                            get_random_spawn(map1, p);
234
                                            gunner_set_curr_health(p, gunner_get_health(p));
235
                                            it = list_node_next(it);
236
                                        }
237
                                        timer_reset(in_game_timer);
238 243 up20180642
                                    }
239 251 up20180655
                                    break;
240 261 up20180642
                                }
241
                                break;
242 263 up20180642
                            #ifndef DIOGO
243 261 up20180642
                            case MOUSE_IRQ:
244
                                if (counter_mouse_ih >= 3) {
245
                                    mouse_parse_packet(packet_mouse_ih, &pp);
246
                                    update_mouse(&pp);
247 251 up20180655
                                    switch (game_state) {
248
                                    case MENU:
249 261 up20180642
                                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
250 251 up20180655
                                        break;
251
                                    case GAME:
252 261 up20180642
                                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
253
                                            shoot_bullet(shooter1, bullet_list, bsp_bullet);
254 251 up20180655
                                        break;
255 231 up20180655
                                    }
256 261 up20180642
                                    last_lb = keys->lb_pressed;
257
                                    counter_mouse_ih = 0;
258 251 up20180655
259 220 up20180655
                                }
260 261 up20180642
                                break;
261
                            #endif
262
                            #ifdef DIOGO
263
                            case COM1_IRQ:
264 275 up20180642
                                nctp_ih();
265
                                break;
266 261 up20180642
                            #endif
267 220 up20180655
                            }
268 251 up20180655
                        }
269 261 up20180642
                    }
270 167 up20180655
271 261 up20180642
                    break;
272
                default:
273
                    break; /* no other notifications expected: do nothing */
274 147 up20180655
            }
275 261 up20180642
        } else { /* received standart message, not a notification */
276
            /* no standart message expected: do nothing */
277 147 up20180655
        }
278 261 up20180642
    }
279 149 up20180655
280 269 up20180642
    #ifdef DIOGO
281
        free(s);
282
    #endif
283
284 261 up20180642
    while(list_size(shooter_list) > 0){
285
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
286
        gunner_dtor(p);
287
    }
288 236 up20180642
289 261 up20180642
    while(list_size(bullet_list) > 0){
290
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
291
        bullet_dtor(p);
292
    }
293
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
294
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
295 243 up20180642
296 261 up20180642
    timer_dtor(in_game_timer); in_game_timer = NULL;
297 250 up20180655
298 261 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
299
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
300
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
301
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
302
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
303
    map_dtor               (map1         ); map1          = NULL;
304
    font_dtor              (consolas     ); consolas      = NULL;
305 243 up20180642
306 261 up20180642
    // Unsubscribe interrupts
307
    if (unsubscribe_all()) {
308
        if (cleanup())
309
            printf("%s: failed to cleanup.\n", __func__);
310
        return 1;
311
    }
312 188 up20180642
313 192 up20180642
314 261 up20180642
    if (cleanup()) {
315
        printf("%s: failed to cleanup.\n", __func__);
316
        return 1;
317
    }
318 155 up20180655
319 149 up20180655
    return 0;
320 147 up20180655
}