Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 235

History | View | Annotate | Download (11.2 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
        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
    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
66
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
67

    
68
    /// subscribe interrupts
69
    if (subscribe_all()) { return 1; }
70

    
71
    /// initialize graphics
72
    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
        return 1;
76
    }
77

    
78
    /// Load stuff
79
    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
    {
86
        graph_clear_screen();
87
        text_t *txt = text_ctor(consolas, "Loading...");
88
        text_draw(txt);
89
        text_dtor(txt);
90
        graph_draw();
91

    
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
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
97

    
98
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
99
    }
100

    
101
    #ifdef DIOGO
102
        /*
103
        graph_clear_screen();
104

105
        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
        rectangle_set_outline_width(rect, 2);
111
        rectangle_set_outline_color(rect, WHITE);
112
        rectangle_draw(rect);
113

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

117
        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
        text_draw(txt);
122
        text_dtor(txt);
123

124
        graph_draw();
125
        rectangle_dtor(rect);
126

127
        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
        tickdelay(micros_to_ticks(1000000));*/
152

    
153
        // RTC
154
        /*
155
        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
        */
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
    #endif
176

    
177
    #ifdef TELMO
178
        ent_set_scale(DEFAULT_SCALE);
179

    
180
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
181
        gunner_set_spawn(shooter1, 75, 75);
182
        gunner_set_pos(shooter1, 75, 75);
183

    
184
        gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
185
        gunner_set_spawn(shooter2, 975, 75);
186
        gunner_set_pos(shooter2, 775, 75);
187

    
188
        //bullet_t *bullet = bullet_ctor(get_bullet(), 400.0, 400.0, 2.0, -1.0);
189

    
190
        list_t *bullet_list = list_ctor();
191

    
192
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
193
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
194

    
195
        uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
196

    
197
        uint8_t last_lb = 0;
198
    #endif
199

    
200
    /// loop stuff
201
    int ipc_status;
202
    message msg;
203
    int good = 1;
204

    
205
    #ifdef DIOGO
206
        good = 0;
207
    #endif
208

    
209
    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
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
219
                        if (msg.m_notify.interrupts & n) {
220
                            interrupt_handler(i);
221
                            if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
222
                            #ifdef TELMO
223
                            if (i == 0) {
224
                                if (no_interrupts % refresh_count_value == 0) {
225
                                    update_movement(map1, shooter1, shooter2);
226
                                    //bullet_update_movement(bullet);
227

    
228
                                    if (no_interrupts % 180 == 0) gunner_set_pos(shooter2, 775, 75);
229

    
230
                                    update_game_state(map1, shooter2, bullet_list);
231

    
232
                                    if(map_collides_gunner(map1, shooter1)){
233
                                        printf("COLLIDING\n");
234
                                    }
235

    
236
                                    /*if (gunner_collides_bullet(shooter1, bullet)) {
237
                                        printf("Bullet Collide with Shooter\n");
238
                                        gunner_set_curr_health(shooter1, gunner_get_curr_health(shooter1) - bullet_get_damage(bullet));
239
                                    }*/
240
                                    update_scale();
241

    
242
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
243
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
244

    
245
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
246
                                    double angle = get_mouse_angle(shooter1);
247
                                    gunner_set_angle(shooter1, angle - M_PI_2);
248
                                    graph_clear_screen();
249

    
250
                                    clock_t t = clock();
251

    
252
                                    map_draw   (map1);
253
                                    gunner_draw(shooter2);
254
                                    gunner_draw(shooter1);
255
                                    bullet_draw_list(bullet_list);
256

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

    
259
                                    sprite_draw(sp_crosshair);
260
                                    graph_draw();
261
                                }
262
                            }
263
                            if (i == 12) {
264
                                if (counter_mouse_ih >= 3) {
265
                                    struct packet pp = mouse_parse_packet(packet_mouse_ih);
266
                                    update_mouse(&pp);
267
                                    //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
268
                                    counter_mouse_ih = 0;
269

    
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
                                }
276
                            }
277
                            #endif
278
                        }
279
                    }
280

    
281
                    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

    
290
    #ifdef TELMO
291
        gunner_dtor(shooter1); shooter1 = NULL;
292
        gunner_dtor(shooter2); shooter2 = NULL;
293

    
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
    #endif
300

    
301
    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

    
309
    // Unsubscribe interrupts
310
    if (unsubscribe_all()) {
311
        if (cleanup())
312
            printf("%s: failed to cleanup.\n", __func__);
313
        return 1;
314
    }
315

    
316

    
317
    if (cleanup()) {
318
        printf("%s: failed to cleanup.\n", __func__);
319
        return 1;
320
    }
321

    
322
    return 0;
323
}