Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 235

History | View | Annotate | Download (11.2 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
        uint8_t conf;
58
        if((r = util_sys_inb(0x3F8+3, &conf))) return 1; //printf("0x%02X\n", conf);
59
        conf = 0x19; //00011001
60
        //printf("0x%02X\n", conf);
61
        if((r = sys_outb(0x3F8+3, conf))) return 1;
62
        if((r = util_sys_inb(0x3F8+3, &conf))) return 1; //printf("0x%02X\n", conf);
63
    #endif
64
65 221 up20180642
    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
66 192 up20180642
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
67
68 170 up20180642
    /// subscribe interrupts
69
    if (subscribe_all()) { return 1; }
70
71
    /// initialize graphics
72 166 up20180642
    if(graph_init(GRAPH_MODE)){
73
        printf("%s: failed to initalize graphics.\n", __func__);
74
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
75 152 up20180642
        return 1;
76
    }
77
78 188 up20180642
    /// Load stuff
79 216 up20180642
    basic_sprite_t       *bsp_crosshair = NULL;
80
    basic_sprite_t       *bsp_shooter   = NULL;
81
    basic_sprite_t       *bsp_pistol    = NULL;
82
    basic_sprite_t       *bsp_nothing   = NULL;
83
    map_t                *map1      = NULL;
84
    sprite_t             *sp_crosshair  = NULL;
85 188 up20180642
    {
86
        graph_clear_screen();
87
        text_t *txt = text_ctor(consolas, "Loading...");
88
        text_draw(txt);
89
        text_dtor(txt);
90
        graph_draw();
91 192 up20180642
92
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
93
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
94
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
95
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
96 216 up20180642
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
97 192 up20180642
98
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
99 188 up20180642
    }
100 192 up20180642
101 159 up20180642
    #ifdef DIOGO
102 234 up20180655
        /*
103 188 up20180642
        graph_clear_screen();
104 183 up20180642

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

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

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

124
        graph_draw();
125 188 up20180642
        rectangle_dtor(rect);
126 183 up20180642

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

150

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

157
        if (rtc_read_time(time)) return 1;
158

159
        if (rtc_read_date(date)) return 1;
160

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

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