Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 250

History | View | Annotate | Download (11.3 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 237 up20180642
#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 "test7.h"
34
#endif
35
36 144 up20180655
int main(int argc, char* argv[]) {
37
38
    lcf_set_language("EN-US");
39
40 235 up20180642
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
41 144 up20180655
42 189 up20180655
    lcf_log_output("/home/lcom/labs/proj/output.txt");
43 144 up20180655
44
    if (lcf_start(argc, argv)) return 1;
45
46
    lcf_cleanup();
47
48
    return 0;
49
}
50 147 up20180655
51 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
52 170 up20180642
53
    int r;
54 243 up20180642
    #ifndef DIOGO
55
        font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
56
        if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
57 170 up20180642
58 243 up20180642
        /// subscribe interrupts
59
        if (subscribe_all()) { return 1; }
60 192 up20180642
61 243 up20180642
        /// initialize graphics
62
        if(graph_init(GRAPH_MODE)){
63
            printf("%s: failed to initalize graphics.\n", __func__);
64
            if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
65
            return 1;
66
        }
67 170 up20180642
68 243 up20180642
        /// Load stuff
69
        basic_sprite_t       *bsp_crosshair = NULL;
70
        basic_sprite_t       *bsp_shooter   = NULL;
71
        basic_sprite_t       *bsp_pistol    = NULL;
72
        basic_sprite_t       *bsp_nothing   = NULL;
73
        basic_sprite_t       *bsp_bullet    = NULL;
74
        map_t                *map1          = NULL;
75
        sprite_t             *sp_crosshair  = NULL;
76
        {
77
            graph_clear_screen();
78
            text_t *txt = text_ctor(consolas, "Loading...");
79
            text_draw(txt);
80
            text_dtor(txt);
81
            graph_draw();
82 152 up20180642
83 243 up20180642
            bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
84
            bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
85
            bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
86
            bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
87
            bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
88
            map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
89 192 up20180642
90 243 up20180642
            sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
91
        }
92
    #endif
93 192 up20180642
94 159 up20180642
    #ifdef DIOGO
95 234 up20180655
        /*
96 188 up20180642
        graph_clear_screen();
97 183 up20180642

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

107 188 up20180642
        text_t *txt  = text_ctor(consolas, "Hello world!");
108
        text_set_color(txt, 0x888888);
109 182 up20180642

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

117
        graph_draw();
118 188 up20180642
        rectangle_dtor(rect);
119 183 up20180642

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

143

144 234 up20180655
        tickdelay(micros_to_ticks(1000000));*/
145 183 up20180642
146 234 up20180655
        // RTC
147 235 up20180642
        /*
148 234 up20180655
        uint8_t date[4], time[3];
149

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

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

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

156
        printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
157 235 up20180642
        */
158
        //UART
159 243 up20180642
        unsigned long bits = 8;
160
        unsigned long stop = 2;
161 249 up20180642
        long parity = 0;
162 243 up20180642
        unsigned long rate = 9600;
163 249 up20180642
        unsigned char tx = 1;
164 243 up20180642
        int stringc = 2;
165 249 up20180642
        char *strings[] = {"Hello", "world"}; (void)strings;
166
        //if((r = ser_test_conf(COM1_ADDR))) return r;
167
        //if((r = ser_test_set(COM1_ADDR, bits, stop, parity, rate))) return r;
168 243 up20180642
        if((r = ser_test_poll(COM1_ADDR, tx, bits, stop, parity, rate, stringc, strings))) return r;
169 249 up20180642
        printf("DONE\n");
170 159 up20180642
    #endif
171 243 up20180642
    #ifndef DIOGO
172 248 up20180655
        ent_set_scale(DEFAULT_SCALE);
173 152 up20180642
174 250 up20180655
        text_timer_t *in_game_timer = timer_ctor(consolas);
175
176 248 up20180655
        list_t *shooter_list = list_ctor();
177 192 up20180642
178 248 up20180655
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
179
        gunner_set_spawn(shooter1, 75, 75);
180
        gunner_set_pos(shooter1, 75, 75);
181 236 up20180642
182 248 up20180655
        gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
183
        gunner_set_spawn(shooter2, 975, 75);
184
        gunner_set_pos(shooter2, 775, 75);
185 192 up20180642
186 248 up20180655
        list_insert(shooter_list, list_end(shooter_list), shooter1);
187
        list_insert(shooter_list, list_end(shooter_list), shooter2);
188 192 up20180642
189 248 up20180655
        list_t *bullet_list  = list_ctor();
190 202 up20180655
191 248 up20180655
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
192
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
193 231 up20180655
194 250 up20180655
        //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
195
        double angle; // mouse angle
196
        int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y();
197 248 up20180655
        uint8_t last_lb = 0;
198 250 up20180655
        struct packet pp;
199
        keys_t *keys = get_key_presses();
200 231 up20180655
201 243 up20180642
        /// loop stuff
202
        int ipc_status;
203
        message msg;
204
        int good = 1;
205 171 up20180655
206 243 up20180642
        while (good) {
207
            /* Get a request message. */
208
            if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
209
                printf("driver_receive failed with %d", r);
210
                continue;
211
            }
212
            if (is_ipc_notify(ipc_status)) { /* received notification */
213
                switch (_ENDPOINT_P(msg.m_source)) {
214
                    case HARDWARE: /* hardware interrupt notification */
215
                        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
216
                            if (msg.m_notify.interrupts & n) {
217
                                interrupt_handler(i);
218
                                if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
219
                                if (i == 0) {
220 250 up20180655
                                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
221
                                    //if (no_interrupts % refresh_count_value == 0) {
222
                                            update_movement(map1, shooter1, keys, shooter_list);
223 168 up20180642
224 248 up20180655
                                            update_game_state(map1, shooter_list, bullet_list);
225 183 up20180642
226 250 up20180655
                                            //update_scale();
227
                                            sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
228
                                            angle = get_mouse_angle(shooter1);
229 248 up20180655
                                            gunner_set_angle(shooter1, angle - M_PI_2);
230
                                            graph_clear_screen();
231 232 up20180655
232 248 up20180655
                                            ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
233
                                                           gunner_get_y(shooter1)-ent_get_YLength()/2.0);
234 192 up20180642
235 248 up20180655
                                            map_draw   (map1);
236
                                            gunner_draw_list(shooter_list);
237
                                            bullet_draw_list(bullet_list);
238 214 up20180642
239 250 up20180655
                                            text_draw(in_game_timer->text);
240 248 up20180655
                                            sprite_draw(sp_crosshair);
241
                                            graph_draw();
242 250 up20180655
                                        //}
243 243 up20180642
                                    }
244 250 up20180655
                                    else if (i == 12) {
245 248 up20180655
                                        if (counter_mouse_ih >= 3) {
246 250 up20180655
                                            mouse_parse_packet(packet_mouse_ih, &pp);
247 248 up20180655
                                            update_mouse(&pp);
248 250 up20180655
249
                                            if (last_lb ^ keys->lb_pressed && keys->lb_pressed) {
250 248 up20180655
                                                shoot_bullet(shooter1, bullet_list, bsp_bullet);
251
                                            }
252 250 up20180655
                                            last_lb = keys->lb_pressed;
253
                                            counter_mouse_ih = 0;
254
255 248 up20180655
                                        }
256 231 up20180655
                                    }
257 220 up20180655
                                }
258
                            }
259 167 up20180655
260 248 up20180655
                            break;
261
                        default:
262
                            break; /* no other notifications expected: do nothing */
263
                    }
264 243 up20180642
            } else { /* received standart message, not a notification */
265
                /* no standart message expected: do nothing */
266 147 up20180655
            }
267
        }
268 149 up20180655
269 248 up20180655
        while(list_size(shooter_list) > 0){
270
            gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
271
            gunner_dtor(p);
272
        }
273 236 up20180642
274 248 up20180655
        while(list_size(bullet_list) > 0){
275
            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
276
            bullet_dtor(p);
277
        }
278
        if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
279
        if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
280 243 up20180642
281 250 up20180655
        timer_dtor(in_game_timer); in_game_timer = NULL;
282
283 243 up20180642
        basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
284
        basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
285
        sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
286
        basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
287
        basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
288
        map_dtor               (map1         ); map1          = NULL;
289
        font_dtor              (consolas     ); consolas      = NULL;
290
291
        // Unsubscribe interrupts
292
        if (unsubscribe_all()) {
293
            if (cleanup())
294
                printf("%s: failed to cleanup.\n", __func__);
295
            return 1;
296 231 up20180655
        }
297 188 up20180642
298 192 up20180642
299 243 up20180642
        if (cleanup()) {
300 155 up20180655
            printf("%s: failed to cleanup.\n", __func__);
301 243 up20180642
            return 1;
302
        }
303
    #endif
304 155 up20180655
305 248 up20180655
306 149 up20180655
    return 0;
307 147 up20180655
}