Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 236

History | View | Annotate | Download (11.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 234 up20180655
#include "rtc.h"
15 153 up20180655
#include "interrupts_func.h"
16 149 up20180655
17 179 up20180642
#include "graph.h"
18
#include "sprite.h"
19
#include "rectangle.h"
20 182 up20180642
#include "font.h"
21 192 up20180642
#include "ent.h"
22 168 up20180642
23 192 up20180642
#include "crosshair.h"
24
#include "shooter.h"
25
#include "pistol.h"
26
#include "nothing.h"
27 231 up20180655
//#include "bullet.h"
28 216 up20180642
#include "map1.h"
29 183 up20180642
30 226 up20180642
#include "list.h"
31
32 235 up20180642
#ifdef DIOGO
33
    #include "uart_macros.h"
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 235 up20180642
    #ifdef DIOGO
57 236 up20180642
        /*
58 235 up20180642
        uint8_t conf;
59
        if((r = util_sys_inb(0x3F8+3, &conf))) return 1; //printf("0x%02X\n", conf);
60
        conf = 0x19; //00011001
61
        //printf("0x%02X\n", conf);
62
        if((r = sys_outb(0x3F8+3, conf))) return 1;
63
        if((r = util_sys_inb(0x3F8+3, &conf))) return 1; //printf("0x%02X\n", conf);
64 236 up20180642
        */
65 235 up20180642
    #endif
66
67 221 up20180642
    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
68 192 up20180642
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
69
70 170 up20180642
    /// subscribe interrupts
71
    if (subscribe_all()) { return 1; }
72
73
    /// initialize graphics
74 166 up20180642
    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 152 up20180642
        return 1;
78
    }
79
80 188 up20180642
    /// Load stuff
81 216 up20180642
    basic_sprite_t       *bsp_crosshair = NULL;
82
    basic_sprite_t       *bsp_shooter   = NULL;
83
    basic_sprite_t       *bsp_pistol    = NULL;
84
    basic_sprite_t       *bsp_nothing   = NULL;
85
    map_t                *map1      = NULL;
86
    sprite_t             *sp_crosshair  = NULL;
87 188 up20180642
    {
88
        graph_clear_screen();
89
        text_t *txt = text_ctor(consolas, "Loading...");
90
        text_draw(txt);
91
        text_dtor(txt);
92
        graph_draw();
93 192 up20180642
94
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
95
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
96
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
97
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
98 216 up20180642
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
99 192 up20180642
100
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
101 188 up20180642
    }
102 192 up20180642
103 159 up20180642
    #ifdef DIOGO
104 234 up20180655
        /*
105 188 up20180642
        graph_clear_screen();
106 183 up20180642

107 188 up20180642
        rectangle_t *rect = rectangle_ctor(0,0,400,100);
108
        rectangle_set_pos(rect,
109
                          graph_get_XRes()/2    - rectangle_get_w(rect)/2,
110
                          graph_get_YRes()*0.25 - rectangle_get_h(rect)/2);
111
        rectangle_set_fill_color(rect, BLACK);
112 179 up20180642
        rectangle_set_outline_width(rect, 2);
113 188 up20180642
        rectangle_set_outline_color(rect, WHITE);
114 179 up20180642
        rectangle_draw(rect);
115

116 188 up20180642
        text_t *txt  = text_ctor(consolas, "Hello world!");
117
        text_set_color(txt, 0x888888);
118 182 up20180642

119 188 up20180642
        text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
120
                          rectangle_get_y(rect)+rectangle_get_h(rect)/2);
121
        text_set_valign(txt, text_valign_center);
122
        text_set_halign(txt, text_halign_center);
123 183 up20180642
        text_draw(txt);
124
        text_dtor(txt);
125

126
        graph_draw();
127 188 up20180642
        rectangle_dtor(rect);
128 183 up20180642

129 226 up20180642
        list_t *l = list_ctor();
130
        int *p = NULL;
131
        for(int i = 10; i < 20; ++i){
132
            p = malloc(sizeof(int));
133
            *p = i;
134
            printf("INSERTING %d\n", i);
135
            list_insert(l, list_end(l), p);
136
            printf("INSERTED, SIZE=%d\n", list_size(l));
137
        }
138
        list_node_t *it = list_begin(l);
139
        while(it != list_end(l)){
140
            printf("%d\n", **(int**)list_node_val(it));
141
            it = list_node_next(it);
142
        }
143
        while(list_size(l) > 0){
144
            printf("ERASING\n");
145
            void *p = list_erase(l, list_begin(l));
146
            printf("ERASED %d, SIZE=%d\n", *(int*)p, list_size(l));
147
            free(p);
148
        }
149
        printf("DONE\n");
150
        if(list_dtor(l)) printf("COULD NOT DESTRUCT LIST\n");
151

152

153 234 up20180655
        tickdelay(micros_to_ticks(1000000));*/
154 183 up20180642
155 234 up20180655
        // RTC
156 235 up20180642
        /*
157 234 up20180655
        uint8_t date[4], time[3];
158

159
        if (rtc_read_time(time)) return 1;
160

161
        if (rtc_read_date(date)) return 1;
162

163
        printf("Hour: %02d:%02d:%02d\n", time[2], time[1], time[0]);
164

165
        printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
166 235 up20180642
        */
167
        //UART
168
        /*
169
        printf("got %d\n", ser_test_conf(COM1_ADDR));
170
        printf("\n");
171
        printf("got %d\n", ser_test_set(COM1_ADDR, 6, 1, 0, 9800));
172
        tickdelay(micros_to_ticks(1000000));
173
        printf("got %d\n", ser_test_conf(COM1_ADDR));
174
        */
175
176
177 159 up20180642
    #endif
178 152 up20180642
179 171 up20180655
    #ifdef TELMO
180 216 up20180642
        ent_set_scale(DEFAULT_SCALE);
181 192 up20180642
182 236 up20180642
        list_t *shooter_list = list_ctor();
183
184 201 up20180642
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
185 232 up20180655
        gunner_set_spawn(shooter1, 75, 75);
186 222 up20180655
        gunner_set_pos(shooter1, 75, 75);
187 192 up20180642
188 201 up20180642
        gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
189 232 up20180655
        gunner_set_spawn(shooter2, 975, 75);
190 231 up20180655
        gunner_set_pos(shooter2, 775, 75);
191 192 up20180642
192 236 up20180642
        list_insert(shooter_list, list_end(shooter_list), shooter1);
193
        list_insert(shooter_list, list_end(shooter_list), shooter2);
194
195 231 up20180655
        //bullet_t *bullet = bullet_ctor(get_bullet(), 400.0, 400.0, 2.0, -1.0);
196 202 up20180655
197 236 up20180642
        list_t *bullet_list  = list_ctor();
198 231 up20180655
199 216 up20180642
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
200
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
201 214 up20180642
202 200 up20180655
        uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
203 231 up20180655
204
        uint8_t last_lb = 0;
205 171 up20180655
    #endif
206
207 149 up20180655
    /// loop stuff
208 170 up20180642
    int ipc_status;
209 149 up20180655
    message msg;
210 147 up20180655
    int good = 1;
211 168 up20180642
212 183 up20180642
    #ifdef DIOGO
213
        good = 0;
214
    #endif
215
216 147 up20180655
    while (good) {
217
        /* Get a request message. */
218
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
219
            printf("driver_receive failed with %d", r);
220
            continue;
221
        }
222
        if (is_ipc_notify(ipc_status)) { /* received notification */
223
            switch (_ENDPOINT_P(msg.m_source)) {
224
                case HARDWARE: /* hardware interrupt notification */
225 153 up20180655
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
226
                        if (msg.m_notify.interrupts & n) {
227
                            interrupt_handler(i);
228 167 up20180655
                            if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
229 171 up20180655
                            #ifdef TELMO
230 187 up20180655
                            if (i == 0) {
231
                                if (no_interrupts % refresh_count_value == 0) {
232 233 up20180655
                                    update_movement(map1, shooter1, shooter2);
233 228 up20180655
                                    //bullet_update_movement(bullet);
234 232 up20180655
235
                                    if (no_interrupts % 180 == 0) gunner_set_pos(shooter2, 775, 75);
236
237 236 up20180642
                                    update_game_state(map1, shooter_list, bullet_list);
238 216 up20180642
239 220 up20180655
                                    if(map_collides_gunner(map1, shooter1)){
240 216 up20180642
                                        printf("COLLIDING\n");
241
                                    }
242
243 231 up20180655
                                    /*if (gunner_collides_bullet(shooter1, bullet)) {
244 228 up20180655
                                        printf("Bullet Collide with Shooter\n");
245 230 up20180655
                                        gunner_set_curr_health(shooter1, gunner_get_curr_health(shooter1) - bullet_get_damage(bullet));
246 231 up20180655
                                    }*/
247 200 up20180655
                                    update_scale();
248 192 up20180642
249 216 up20180642
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
250
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
251 214 up20180642
252 192 up20180642
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
253 187 up20180655
                                    double angle = get_mouse_angle(shooter1);
254 201 up20180642
                                    gunner_set_angle(shooter1, angle - M_PI_2);
255 187 up20180655
                                    graph_clear_screen();
256 207 up20180642
257
                                    clock_t t = clock();
258
259 216 up20180642
                                    map_draw   (map1);
260 201 up20180642
                                    gunner_draw(shooter2);
261
                                    gunner_draw(shooter1);
262 231 up20180655
                                    bullet_draw_list(bullet_list);
263 207 up20180642
264 222 up20180655
                                    t = clock()-t; //printf("%d\n", (t*1000)/CLOCKS_PER_SEC);
265 207 up20180642
266 192 up20180642
                                    sprite_draw(sp_crosshair);
267 187 up20180655
                                    graph_draw();
268
                                }
269 184 up20180655
                            }
270 220 up20180655
                            if (i == 12) {
271
                                if (counter_mouse_ih >= 3) {
272
                                    struct packet pp = mouse_parse_packet(packet_mouse_ih);
273 231 up20180655
                                    update_mouse(&pp);
274 220 up20180655
                                    //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
275
                                    counter_mouse_ih = 0;
276 231 up20180655
277
                                    if (last_lb ^ get_key_presses()->lb_pressed && get_key_presses()->lb_pressed) {
278
                                        shoot_bullet(shooter1, bullet_list);
279
                                    }
280
281
                                    last_lb = get_key_presses()->lb_pressed;
282 220 up20180655
                                }
283
                            }
284 171 up20180655
                            #endif
285 153 up20180655
                        }
286 147 up20180655
                    }
287 167 up20180655
288 147 up20180655
                    break;
289
                default:
290
                    break; /* no other notifications expected: do nothing */
291
            }
292
        } else { /* received standart message, not a notification */
293
            /* no standart message expected: do nothing */
294
        }
295
    }
296 149 up20180655
297 192 up20180642
    #ifdef TELMO
298 236 up20180642
        //gunner_dtor(shooter1); shooter1 = NULL;
299
        //gunner_dtor(shooter2); shooter2 = NULL;
300 231 up20180655
301 236 up20180642
        while(list_size(shooter_list) > 0){
302
            gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
303
            gunner_dtor(p);
304
        }
305
306 231 up20180655
        while(list_size(bullet_list) > 0){
307
            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
308 236 up20180642
            bullet_dtor(p);
309 231 up20180655
        }
310
        if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
311 192 up20180642
    #endif
312 188 up20180642
313 216 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
314
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
315
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
316
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
317
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
318
    map_dtor               (map1         ); map1          = NULL;
319
    font_dtor              (consolas     ); consolas      = NULL;
320 192 up20180642
321 153 up20180655
    // Unsubscribe interrupts
322 155 up20180655
    if (unsubscribe_all()) {
323
        if (cleanup())
324
            printf("%s: failed to cleanup.\n", __func__);
325 152 up20180642
        return 1;
326
    }
327
328 155 up20180655
329
    if (cleanup()) {
330
        printf("%s: failed to cleanup.\n", __func__);
331 152 up20180642
        return 1;
332
    }
333
334 149 up20180655
    return 0;
335 147 up20180655
}