Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 275

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