Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 237

History | View | Annotate | Download (11.8 KB)

1
#include <lcom/lcf.h>
2
#include <lcom/proj.h>
3
#include <lcom/liblm.h>
4
#include <math.h>
5

    
6
#include "proj_macros.h"
7
#include "proj_func.h"
8

    
9
#include "kbc.h"
10
#include "timer.h"
11
#include "keyboard.h"
12
#include "mouse.h"
13
#include "graph.h"
14
#include "rtc.h"
15
#include "interrupts_func.h"
16

    
17
#include "graph.h"
18
#include "sprite.h"
19
#include "rectangle.h"
20
#include "font.h"
21
#include "ent.h"
22

    
23
#include "crosshair.h"
24
#include "shooter.h"
25
#include "pistol.h"
26
#include "nothing.h"
27
#include "bullet.h"
28
#include "map1.h"
29

    
30
#include "list.h"
31

    
32
#ifdef DIOGO
33
    #include "uart_macros.h"
34
    #include "test7.h"
35
#endif
36

    
37
int main(int argc, char* argv[]) {
38

    
39
    lcf_set_language("EN-US");
40

    
41
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
42

    
43
    lcf_log_output("/home/lcom/labs/proj/output.txt");
44

    
45
    if (lcf_start(argc, argv)) return 1;
46

    
47
    lcf_cleanup();
48

    
49
    return 0;
50
}
51

    
52
int(proj_main_loop)(int argc, char *argv[]) {
53

    
54
    int r;
55

    
56
    #ifdef DIOGO
57
        /*
58
        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
        */
65
    #endif
66

    
67
    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
68
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
69

    
70
    /// subscribe interrupts
71
    if (subscribe_all()) { return 1; }
72

    
73
    /// initialize graphics
74
    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
        return 1;
78
    }
79

    
80
    /// Load stuff
81
    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
    basic_sprite_t       *bsp_bullet    = NULL;
86
    map_t                *map1          = NULL;
87
    sprite_t             *sp_crosshair  = NULL;
88
    {
89
        graph_clear_screen();
90
        text_t *txt = text_ctor(consolas, "Loading...");
91
        text_draw(txt);
92
        text_dtor(txt);
93
        graph_draw();
94

    
95
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
96
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
97
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
98
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
99
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
100
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
101

    
102
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
103
    }
104

    
105
    #ifdef DIOGO
106
        /*
107
        graph_clear_screen();
108

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

118
        text_t *txt  = text_ctor(consolas, "Hello world!");
119
        text_set_color(txt, 0x888888);
120

121
        text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
122
                          rectangle_get_y(rect)+rectangle_get_h(rect)/2);
123
        text_set_valign(txt, text_valign_center);
124
        text_set_halign(txt, text_halign_center);
125
        text_draw(txt);
126
        text_dtor(txt);
127

128
        graph_draw();
129
        rectangle_dtor(rect);
130

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

154

155
        tickdelay(micros_to_ticks(1000000));*/
156

    
157
        // RTC
158
        /*
159
        uint8_t date[4], time[3];
160

161
        if (rtc_read_time(time)) return 1;
162

163
        if (rtc_read_date(date)) return 1;
164

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

167
        printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
168
        */
169
        //UART
170
        /*
171
        printf("got %d\n", ser_test_conf(COM1_ADDR));
172
        printf("\n");
173
        printf("got %d\n", ser_test_set(COM1_ADDR, 6, 1, 0, 9800));
174
        tickdelay(micros_to_ticks(1000000));
175
        printf("got %d\n", ser_test_conf(COM1_ADDR));
176
        */
177

    
178

    
179
    #endif
180

    
181
    #ifdef TELMO
182
        ent_set_scale(DEFAULT_SCALE);
183

    
184
        list_t *shooter_list = list_ctor();
185

    
186
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
187
        gunner_set_spawn(shooter1, 75, 75);
188
        gunner_set_pos(shooter1, 75, 75);
189

    
190
        gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
191
        gunner_set_spawn(shooter2, 975, 75);
192
        gunner_set_pos(shooter2, 775, 75);
193

    
194
        list_insert(shooter_list, list_end(shooter_list), shooter1);
195
        list_insert(shooter_list, list_end(shooter_list), shooter2);
196

    
197
        //bullet_t *bullet = bullet_ctor(get_bullet(), 400.0, 400.0, 2.0, -1.0);
198

    
199
        list_t *bullet_list  = list_ctor();
200

    
201
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
202
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
203

    
204
        uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
205

    
206
        uint8_t last_lb = 0;
207
    #endif
208

    
209
    /// loop stuff
210
    int ipc_status;
211
    message msg;
212
    int good = 1;
213

    
214
    #ifdef DIOGO
215
        good = 0;
216
    #endif
217

    
218
    while (good) {
219
        /* Get a request message. */
220
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
221
            printf("driver_receive failed with %d", r);
222
            continue;
223
        }
224
        if (is_ipc_notify(ipc_status)) { /* received notification */
225
            switch (_ENDPOINT_P(msg.m_source)) {
226
                case HARDWARE: /* hardware interrupt notification */
227
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
228
                        if (msg.m_notify.interrupts & n) {
229
                            interrupt_handler(i);
230
                            if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
231
                            #ifdef TELMO
232
                            if (i == 0) {
233
                                if (no_interrupts % refresh_count_value == 0) {
234
                                    update_movement(map1, shooter1, shooter2);
235
                                    //bullet_update_movement(bullet);
236

    
237
                                    if (no_interrupts % 180 == 0) gunner_set_pos(shooter2, 775, 75);
238

    
239
                                    update_game_state(map1, shooter_list, bullet_list);
240

    
241
                                    if(map_collides_gunner(map1, shooter1)){
242
                                        printf("COLLIDING\n");
243
                                    }
244

    
245
                                    /*if (gunner_collides_bullet(shooter1, bullet)) {
246
                                        printf("Bullet Collide with Shooter\n");
247
                                        gunner_set_curr_health(shooter1, gunner_get_curr_health(shooter1) - bullet_get_damage(bullet));
248
                                    }*/
249
                                    update_scale();
250

    
251
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
252
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
253

    
254
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
255
                                    double angle = get_mouse_angle(shooter1);
256
                                    gunner_set_angle(shooter1, angle - M_PI_2);
257
                                    graph_clear_screen();
258

    
259
                                    clock_t t = clock();
260

    
261
                                    map_draw   (map1);
262
                                    gunner_draw(shooter2);
263
                                    gunner_draw(shooter1);
264
                                    bullet_draw_list(bullet_list);
265

    
266
                                    t = clock()-t; //printf("%d\n", (t*1000)/CLOCKS_PER_SEC);
267

    
268
                                    sprite_draw(sp_crosshair);
269
                                    graph_draw();
270
                                }
271
                            }
272
                            if (i == 12) {
273
                                if (counter_mouse_ih >= 3) {
274
                                    struct packet pp = mouse_parse_packet(packet_mouse_ih);
275
                                    update_mouse(&pp);
276
                                    //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
277
                                    counter_mouse_ih = 0;
278

    
279
                                    if (last_lb ^ get_key_presses()->lb_pressed && get_key_presses()->lb_pressed) {
280
                                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
281
                                    }
282

    
283
                                    last_lb = get_key_presses()->lb_pressed;
284
                                }
285
                            }
286
                            #endif
287
                        }
288
                    }
289

    
290
                    break;
291
                default:
292
                    break; /* no other notifications expected: do nothing */
293
            }
294
        } else { /* received standart message, not a notification */
295
            /* no standart message expected: do nothing */
296
        }
297
    }
298

    
299
    #ifdef TELMO
300
        //gunner_dtor(shooter1); shooter1 = NULL;
301
        //gunner_dtor(shooter2); shooter2 = NULL;
302

    
303
        while(list_size(shooter_list) > 0){
304
            gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
305
            gunner_dtor(p);
306
        }
307

    
308
        while(list_size(bullet_list) > 0){
309
            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
310
            bullet_dtor(p);
311
        }
312
        if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
313
    #endif
314

    
315
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
316
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
317
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
318
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
319
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
320
    map_dtor               (map1         ); map1          = NULL;
321
    font_dtor              (consolas     ); consolas      = NULL;
322

    
323
    // Unsubscribe interrupts
324
    if (unsubscribe_all()) {
325
        if (cleanup())
326
            printf("%s: failed to cleanup.\n", __func__);
327
        return 1;
328
    }
329

    
330

    
331
    if (cleanup()) {
332
        printf("%s: failed to cleanup.\n", __func__);
333
        return 1;
334
    }
335

    
336
    return 0;
337
}