Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 267

History | View | Annotate | Download (11.8 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
    #include "test7.h"
35
#endif
36
37 144 up20180655
int main(int argc, char* argv[]) {
38
39
    lcf_set_language("EN-US");
40
41 235 up20180642
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
42 144 up20180655
43 189 up20180655
    lcf_log_output("/home/lcom/labs/proj/output.txt");
44 144 up20180655
45
    if (lcf_start(argc, argv)) return 1;
46
47
    lcf_cleanup();
48
49
    return 0;
50
}
51 147 up20180655
52 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
53 170 up20180642
54
    int r;
55
56 261 up20180642
    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
57
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
58 192 up20180642
59 261 up20180642
    /// subscribe interrupts
60
    if (subscribe_all()) { return 1; }
61 170 up20180642
62 261 up20180642
    /// initialize graphics
63
    if(graph_init(GRAPH_MODE)){
64
        printf("%s: failed to initalize graphics.\n", __func__);
65
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
66
        return 1;
67
    }
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 188 up20180642
        graph_clear_screen();
79 261 up20180642
        text_t *txt = text_ctor(consolas, "Loading...");
80 183 up20180642
        text_draw(txt);
81
        text_dtor(txt);
82
        graph_draw();
83
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
    char buffer[1024]; // buffer
134
    int buffer_pos = 0;
135
136 262 up20180642
    #ifndef DIOGO
137
        int click = 0;
138
    #endif
139 236 up20180642
140 261 up20180642
    while (game_state != EXIT) {
141
        /* Get a request message. */
142
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
143
            printf("driver_receive failed with %d", r);
144
            continue;
145
        }
146
        if (is_ipc_notify(ipc_status)) { /* received notification */
147
            switch (_ENDPOINT_P(msg.m_source)) {
148
                case HARDWARE: /* hardware interrupt notification */
149
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
150
                        if (msg.m_notify.interrupts & n) {
151
                            interrupt_handler(i);
152
                            switch (i) {
153
                            #ifndef DIOGO
154
                            case TIMER0_IRQ:
155
                                switch (game_state) {
156
                                case MENU:
157
                                    if (no_interrupts % 2 == 0){
158
                                        graph_clear_screen();
159
                                        game_state = menu_update_state(main_menu, click);
160
                                        menu_draw(main_menu);
161 192 up20180642
162 261 up20180642
                                        click = 0;
163 192 up20180642
164 261 up20180642
                                        sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
165
                                        sprite_draw(sp_crosshair);
166
                                        graph_draw();
167
                                    }
168
                                    break;
169
                                case GAME:
170
                                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
171
                                    update_movement(map1, shooter1, keys, shooter_list);
172 202 up20180655
173 261 up20180642
                                    update_game_state(map1, shooter_list, bullet_list);
174 231 up20180655
175 261 up20180642
                                    //update_scale();
176
                                    angle = get_mouse_angle(shooter1);
177
                                    gunner_set_angle(shooter1, angle - M_PI_2);
178 231 up20180655
179 261 up20180642
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
180
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
181 171 up20180655
182 261 up20180642
                                    graph_clear_screen();
183
                                    map_draw   (map1);
184
                                    gunner_draw_list(shooter_list);
185
                                    bullet_draw_list(bullet_list);
186 168 up20180642
187 261 up20180642
                                    text_draw(in_game_timer->text);
188 183 up20180642
189 261 up20180642
                                    sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
190
                                    sprite_draw(sp_crosshair);
191
                                    graph_draw();
192
                                    break;
193
                                }
194
                                break;
195 263 up20180642
                            #endif
196 261 up20180642
                            case KBC_IRQ:
197
                                switch (game_state) {
198
                                case MENU:
199
                                    if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
200 267 up20180655
201
                                    else if ((scancode[0]) == A_MAKE_CODE) buffer[buffer_pos++] = 'A';
202
203
                                    else if ((scancode[0]) == ENTER_MAKE_CODE) {
204
                                        // func1
205
206
                                        memcpy(buffer, 0, buffer_pos+1);
207
                                        buffer_pos = 0;
208
                                    }
209
210 261 up20180642
                                    break;
211
                                case GAME:
212
                                    if ((scancode[0]) == ESC_BREAK_CODE) {
213
                                        game_state = MENU;
214
                                        // reset game
215
                                        while(list_size(bullet_list) > 0){
216
                                            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
217
                                            bullet_dtor(p);
218 251 up20180655
                                        }
219 261 up20180642
                                        list_node_t *it = list_begin(shooter_list);
220
                                        while (it != list_end(shooter_list)) {
221
                                            gunner_t *p = *(gunner_t**)list_node_val(it);
222
                                            get_random_spawn(map1, p);
223
                                            gunner_set_curr_health(p, gunner_get_health(p));
224
                                            it = list_node_next(it);
225
                                        }
226
                                        timer_reset(in_game_timer);
227 243 up20180642
                                    }
228 251 up20180655
                                    break;
229 261 up20180642
                                }
230
                                break;
231 263 up20180642
                            #ifndef DIOGO
232 261 up20180642
                            case MOUSE_IRQ:
233
                                if (counter_mouse_ih >= 3) {
234
                                    mouse_parse_packet(packet_mouse_ih, &pp);
235
                                    update_mouse(&pp);
236 251 up20180655
                                    switch (game_state) {
237
                                    case MENU:
238 261 up20180642
                                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
239 251 up20180655
                                        break;
240
                                    case GAME:
241 261 up20180642
                                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
242
                                            shoot_bullet(shooter1, bullet_list, bsp_bullet);
243 251 up20180655
                                        break;
244 231 up20180655
                                    }
245 261 up20180642
                                    last_lb = keys->lb_pressed;
246
                                    counter_mouse_ih = 0;
247 251 up20180655
248 220 up20180655
                                }
249 261 up20180642
                                break;
250
                            #endif
251
                            #ifdef DIOGO
252
                            case COM1_IRQ:
253 266 up20180642
                                char *s = NULL;
254
                                hltp_get_string(1, s);
255
                                printf("You've got mail: %s\n", s);
256 261 up20180642
                                break;
257
                            #endif
258 220 up20180655
                            }
259 251 up20180655
                        }
260 261 up20180642
                    }
261 167 up20180655
262 261 up20180642
                    break;
263
                default:
264
                    break; /* no other notifications expected: do nothing */
265 147 up20180655
            }
266 261 up20180642
        } else { /* received standart message, not a notification */
267
            /* no standart message expected: do nothing */
268 147 up20180655
        }
269 261 up20180642
    }
270 149 up20180655
271 261 up20180642
    while(list_size(shooter_list) > 0){
272
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
273
        gunner_dtor(p);
274
    }
275 236 up20180642
276 261 up20180642
    while(list_size(bullet_list) > 0){
277
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
278
        bullet_dtor(p);
279
    }
280
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
281
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
282 243 up20180642
283 261 up20180642
    timer_dtor(in_game_timer); in_game_timer = NULL;
284 250 up20180655
285 261 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
286
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
287
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
288
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
289
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
290
    map_dtor               (map1         ); map1          = NULL;
291
    font_dtor              (consolas     ); consolas      = NULL;
292 243 up20180642
293 261 up20180642
    // Unsubscribe interrupts
294
    if (unsubscribe_all()) {
295
        if (cleanup())
296
            printf("%s: failed to cleanup.\n", __func__);
297
        return 1;
298
    }
299 188 up20180642
300 192 up20180642
301 261 up20180642
    if (cleanup()) {
302
        printf("%s: failed to cleanup.\n", __func__);
303
        return 1;
304
    }
305 155 up20180655
306 149 up20180655
    return 0;
307 147 up20180655
}