Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 269

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