Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 261

History | View | Annotate | Download (11 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 261 up20180642
    menu_t *main_menu = menu_ctor(consolas);
99 234 up20180655
100 261 up20180642
    list_t *shooter_list = list_ctor();
101 234 up20180655
102 261 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
103
    gunner_set_spawn(shooter1, 75, 75);
104
    gunner_set_pos(shooter1, 75, 75);
105 234 up20180655
106 261 up20180642
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
107
    gunner_set_spawn(shooter2, 975, 75);
108
    gunner_set_pos(shooter2, 775, 75);
109 152 up20180642
110 261 up20180642
    list_insert(shooter_list, list_end(shooter_list), shooter1);
111
    list_insert(shooter_list, list_end(shooter_list), shooter2);
112 250 up20180655
113 261 up20180642
    list_t *bullet_list  = list_ctor();
114 251 up20180655
115 261 up20180642
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
116
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
117 192 up20180642
118 261 up20180642
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
119
    double angle; // mouse angle
120
    int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y();
121
    uint8_t last_lb = 0;
122
    struct packet pp;
123
    keys_t *keys = get_key_presses();
124
    /// loop stuff
125
    int ipc_status;
126
    message msg;
127
    int game_state = MENU, click = 0; (void)click;
128 236 up20180642
129 261 up20180642
    while (game_state != EXIT) {
130
        /* Get a request message. */
131
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
132
            printf("driver_receive failed with %d", r);
133
            continue;
134
        }
135
        if (is_ipc_notify(ipc_status)) { /* received notification */
136
            switch (_ENDPOINT_P(msg.m_source)) {
137
                case HARDWARE: /* hardware interrupt notification */
138
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
139
                        if (msg.m_notify.interrupts & n) {
140
                            interrupt_handler(i);
141
                            switch (i) {
142
                            #ifndef DIOGO
143
                            case TIMER0_IRQ:
144
                                switch (game_state) {
145
                                case MENU:
146
                                    if (no_interrupts % 2 == 0){
147
                                        graph_clear_screen();
148
                                        game_state = menu_update_state(main_menu, click);
149
                                        menu_draw(main_menu);
150 192 up20180642
151 261 up20180642
                                        click = 0;
152 192 up20180642
153 261 up20180642
                                        sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
154
                                        sprite_draw(sp_crosshair);
155
                                        graph_draw();
156
                                    }
157
                                    break;
158
                                case GAME:
159
                                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
160
                                    update_movement(map1, shooter1, keys, shooter_list);
161 202 up20180655
162 261 up20180642
                                    update_game_state(map1, shooter_list, bullet_list);
163 231 up20180655
164 261 up20180642
                                    //update_scale();
165
                                    angle = get_mouse_angle(shooter1);
166
                                    gunner_set_angle(shooter1, angle - M_PI_2);
167 231 up20180655
168 261 up20180642
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
169
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
170 171 up20180655
171 261 up20180642
                                    graph_clear_screen();
172
                                    map_draw   (map1);
173
                                    gunner_draw_list(shooter_list);
174
                                    bullet_draw_list(bullet_list);
175 168 up20180642
176 261 up20180642
                                    text_draw(in_game_timer->text);
177 183 up20180642
178 261 up20180642
                                    sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
179
                                    sprite_draw(sp_crosshair);
180
                                    graph_draw();
181
                                    break;
182
                                }
183
                                break;
184
                            case KBC_IRQ:
185
                                switch (game_state) {
186
                                case MENU:
187
                                    if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
188
                                    break;
189
                                case GAME:
190
                                    if ((scancode[0]) == ESC_BREAK_CODE) {
191
                                        game_state = MENU;
192
                                        // reset game
193
                                        while(list_size(bullet_list) > 0){
194
                                            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
195
                                            bullet_dtor(p);
196 251 up20180655
                                        }
197 261 up20180642
                                        list_node_t *it = list_begin(shooter_list);
198
                                        while (it != list_end(shooter_list)) {
199
                                            gunner_t *p = *(gunner_t**)list_node_val(it);
200
                                            get_random_spawn(map1, p);
201
                                            gunner_set_curr_health(p, gunner_get_health(p));
202
                                            it = list_node_next(it);
203
                                        }
204
                                        timer_reset(in_game_timer);
205 243 up20180642
                                    }
206 251 up20180655
                                    break;
207 261 up20180642
                                }
208
                                break;
209
                            case MOUSE_IRQ:
210
                                if (counter_mouse_ih >= 3) {
211
                                    mouse_parse_packet(packet_mouse_ih, &pp);
212
                                    update_mouse(&pp);
213 251 up20180655
                                    switch (game_state) {
214
                                    case MENU:
215 261 up20180642
                                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
216 251 up20180655
                                        break;
217
                                    case GAME:
218 261 up20180642
                                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
219
                                            shoot_bullet(shooter1, bullet_list, bsp_bullet);
220 251 up20180655
                                        break;
221 231 up20180655
                                    }
222 261 up20180642
                                    last_lb = keys->lb_pressed;
223
                                    counter_mouse_ih = 0;
224 251 up20180655
225 220 up20180655
                                }
226 261 up20180642
                                break;
227
                            #endif
228
                            #ifdef DIOGO
229
                            case COM1_IRQ:
230
                                printf("You've got mail.\n");
231
                                break;
232
                            #endif
233 220 up20180655
                            }
234 251 up20180655
                        }
235 261 up20180642
                    }
236 167 up20180655
237 261 up20180642
                    break;
238
                default:
239
                    break; /* no other notifications expected: do nothing */
240 147 up20180655
            }
241 261 up20180642
        } else { /* received standart message, not a notification */
242
            /* no standart message expected: do nothing */
243 147 up20180655
        }
244 261 up20180642
    }
245 149 up20180655
246 261 up20180642
    while(list_size(shooter_list) > 0){
247
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
248
        gunner_dtor(p);
249
    }
250 236 up20180642
251 261 up20180642
    while(list_size(bullet_list) > 0){
252
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
253
        bullet_dtor(p);
254
    }
255
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
256
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
257 243 up20180642
258 261 up20180642
    timer_dtor(in_game_timer); in_game_timer = NULL;
259 250 up20180655
260 261 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
261
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
262
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
263
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
264
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
265
    map_dtor               (map1         ); map1          = NULL;
266
    font_dtor              (consolas     ); consolas      = NULL;
267 243 up20180642
268 261 up20180642
    // Unsubscribe interrupts
269
    if (unsubscribe_all()) {
270
        if (cleanup())
271
            printf("%s: failed to cleanup.\n", __func__);
272
        return 1;
273
    }
274 188 up20180642
275 192 up20180642
276 261 up20180642
    if (cleanup()) {
277
        printf("%s: failed to cleanup.\n", __func__);
278
        return 1;
279
    }
280 155 up20180655
281 149 up20180655
    return 0;
282 147 up20180655
}