Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 294

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