Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 246

History | View | Annotate | Download (11.9 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
    #ifndef DIOGO
56
        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

    
59
        /// subscribe interrupts
60
        if (subscribe_all()) { return 1; }
61

    
62
        /// 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

    
69
        /// 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
            graph_clear_screen();
79
            text_t *txt = text_ctor(consolas, "Loading...");
80
            text_draw(txt);
81
            text_dtor(txt);
82
            graph_draw();
83

    
84
            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

    
91
            sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
92
        }
93
    #endif
94

    
95
    #ifdef DIOGO
96
        /*
97
        graph_clear_screen();
98

99
        rectangle_t *rect = rectangle_ctor(0,0,400,100);
100
        rectangle_set_pos(rect,
101
                          graph_get_XRes()/2    - rectangle_get_w(rect)/2,
102
                          graph_get_YRes()*0.25 - rectangle_get_h(rect)/2);
103
        rectangle_set_fill_color(rect, BLACK);
104
        rectangle_set_outline_width(rect, 2);
105
        rectangle_set_outline_color(rect, WHITE);
106
        rectangle_draw(rect);
107

108
        text_t *txt  = text_ctor(consolas, "Hello world!");
109
        text_set_color(txt, 0x888888);
110

111
        text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
112
                          rectangle_get_y(rect)+rectangle_get_h(rect)/2);
113
        text_set_valign(txt, text_valign_center);
114
        text_set_halign(txt, text_halign_center);
115
        text_draw(txt);
116
        text_dtor(txt);
117

118
        graph_draw();
119
        rectangle_dtor(rect);
120

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

144

145
        tickdelay(micros_to_ticks(1000000));*/
146

    
147
        // RTC
148
        /*
149
        uint8_t date[4], time[3];
150

151
        if (rtc_read_time(time)) return 1;
152

153
        if (rtc_read_date(date)) return 1;
154

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

157
        printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
158
        */
159
        //UART
160
        unsigned long bits = 8;
161
        unsigned long stop = 2;
162
        long parity = -1;
163
        unsigned long rate = 9600;
164
        unsigned char tx = 0;
165
        int stringc = 2;
166
        char *strings[] = {"Hello", "world"};
167
        if((r = ser_test_conf(COM1_ADDR))) return r;
168
        if((r = ser_test_set(COM1_ADDR, bits, stop, parity, rate))) return r;
169
        if((r = ser_test_poll(COM1_ADDR, tx, bits, stop, parity, rate, stringc, strings))) return r;
170
    #endif
171
    #ifndef DIOGO
172
        #ifdef TELMO
173
            ent_set_scale(DEFAULT_SCALE);
174

    
175
            list_t *shooter_list = list_ctor();
176

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

    
181
            gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
182
            gunner_set_spawn(shooter2, 975, 75);
183
            gunner_set_pos(shooter2, 775, 75);
184

    
185
            list_insert(shooter_list, list_end(shooter_list), shooter1);
186
            list_insert(shooter_list, list_end(shooter_list), shooter2);
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
        while (good) {
206
            /* Get a request message. */
207
            if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
208
                printf("driver_receive failed with %d", r);
209
                continue;
210
            }
211
            if (is_ipc_notify(ipc_status)) { /* received notification */
212
                switch (_ENDPOINT_P(msg.m_source)) {
213
                    case HARDWARE: /* hardware interrupt notification */
214
                        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
215
                            if (msg.m_notify.interrupts & n) {
216
                                interrupt_handler(i);
217
                                if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
218
                                #ifdef TELMO
219
                                if (i == 0) {
220
                                    if (no_interrupts % refresh_count_value == 0) {
221
                                        update_movement(map1, shooter1, shooter2);
222
                                        //bullet_update_movement(bullet);
223

    
224
                                        if (no_interrupts % 180 == 0) gunner_set_pos(shooter2, 775, 75);
225

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

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

    
251
                                        clock_t t = clock();
252

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

    
258
                                        sprite_draw(sp_crosshair);
259
                                        graph_draw();
260
                                    }
261
                                }
262
                                if (i == 12) {
263
                                    if (counter_mouse_ih >= 3) {
264
                                        struct packet pp = mouse_parse_packet(packet_mouse_ih);
265
                                        update_mouse(&pp);
266
                                        //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
267
                                        counter_mouse_ih = 0;
268
                                        last_lb = get_key_presses()->lb_pressed;
269
                                    }
270
                                }
271
                                #endif
272
                            }
273
                        }
274

    
275
                        break;
276
                    default:
277
                        break; /* no other notifications expected: do nothing */
278
                }
279
            } else { /* received standart message, not a notification */
280
                /* no standart message expected: do nothing */
281
            }
282
        }
283

    
284
        #ifdef TELMO
285
            while(list_size(shooter_list) > 0){
286
                gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
287
                gunner_dtor(p);
288
            }
289

    
290
            while(list_size(bullet_list) > 0){
291
                bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
292
                bullet_dtor(p);
293
            }
294
            if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
295
        #endif
296

    
297
        basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
298
        basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
299
        sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
300
        basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
301
        basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
302
        map_dtor               (map1         ); map1          = NULL;
303
        font_dtor              (consolas     ); consolas      = NULL;
304

    
305
        // Unsubscribe interrupts
306
        if (unsubscribe_all()) {
307
            if (cleanup())
308
                printf("%s: failed to cleanup.\n", __func__);
309
            return 1;
310
        }
311

    
312

    
313
        if (cleanup()) {
314
            printf("%s: failed to cleanup.\n", __func__);
315
            return 1;
316
        }
317

    
318
    #endif
319

    
320
    return 0;
321
}