Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 234

History | View | Annotate | Download (10.5 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
int main(int argc, char* argv[]) {
33

    
34
    lcf_set_language("EN-US");
35

    
36
    //lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
37

    
38
    lcf_log_output("/home/lcom/labs/proj/output.txt");
39

    
40
    if (lcf_start(argc, argv)) return 1;
41

    
42
    lcf_cleanup();
43

    
44
    return 0;
45
}
46

    
47
int(proj_main_loop)(int argc, char *argv[]) {
48

    
49
    int r;
50

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

    
54
    /// subscribe interrupts
55
    if (subscribe_all()) { return 1; }
56

    
57
    /// initialize graphics
58
    if(graph_init(GRAPH_MODE)){
59
        printf("%s: failed to initalize graphics.\n", __func__);
60
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
61
        return 1;
62
    }
63

    
64
    /// Load stuff
65
    basic_sprite_t       *bsp_crosshair = NULL;
66
    basic_sprite_t       *bsp_shooter   = NULL;
67
    basic_sprite_t       *bsp_pistol    = NULL;
68
    basic_sprite_t       *bsp_nothing   = NULL;
69
    map_t                *map1      = NULL;
70
    sprite_t             *sp_crosshair  = NULL;
71
    {
72
        graph_clear_screen();
73
        text_t *txt = text_ctor(consolas, "Loading...");
74
        text_draw(txt);
75
        text_dtor(txt);
76
        graph_draw();
77

    
78
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
79
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
80
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
81
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
82
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
83

    
84
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
85
    }
86

    
87
    #ifdef DIOGO
88
        /*
89
        graph_clear_screen();
90

91
        rectangle_t *rect = rectangle_ctor(0,0,400,100);
92
        rectangle_set_pos(rect,
93
                          graph_get_XRes()/2    - rectangle_get_w(rect)/2,
94
                          graph_get_YRes()*0.25 - rectangle_get_h(rect)/2);
95
        rectangle_set_fill_color(rect, BLACK);
96
        rectangle_set_outline_width(rect, 2);
97
        rectangle_set_outline_color(rect, WHITE);
98
        rectangle_draw(rect);
99

100
        text_t *txt  = text_ctor(consolas, "Hello world!");
101
        text_set_color(txt, 0x888888);
102

103
        text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
104
                          rectangle_get_y(rect)+rectangle_get_h(rect)/2);
105
        text_set_valign(txt, text_valign_center);
106
        text_set_halign(txt, text_halign_center);
107
        text_draw(txt);
108
        text_dtor(txt);
109

110
        graph_draw();
111
        rectangle_dtor(rect);
112

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

136

137
        tickdelay(micros_to_ticks(1000000));*/
138

    
139
        // RTC
140
        uint8_t date[4], time[3];
141

    
142
        if (rtc_read_time(time)) return 1;
143

    
144
        if (rtc_read_date(date)) return 1;
145

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

    
148
        printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
149
    #endif
150

    
151
    #ifdef TELMO
152
        ent_set_scale(DEFAULT_SCALE);
153

    
154
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
155
        gunner_set_spawn(shooter1, 75, 75);
156
        gunner_set_pos(shooter1, 75, 75);
157

    
158
        gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
159
        gunner_set_spawn(shooter2, 975, 75);
160
        gunner_set_pos(shooter2, 775, 75);
161

    
162
        //bullet_t *bullet = bullet_ctor(get_bullet(), 400.0, 400.0, 2.0, -1.0);
163

    
164
        list_t *bullet_list = list_ctor();
165

    
166
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
167
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
168

    
169
        uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
170

    
171
        uint8_t last_lb = 0;
172
    #endif
173

    
174
    /// loop stuff
175
    int ipc_status;
176
    message msg;
177
    int good = 1;
178

    
179
    #ifdef DIOGO
180
        good = 0;
181
    #endif
182

    
183
    while (good) {
184
        /* Get a request message. */
185
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
186
            printf("driver_receive failed with %d", r);
187
            continue;
188
        }
189
        if (is_ipc_notify(ipc_status)) { /* received notification */
190
            switch (_ENDPOINT_P(msg.m_source)) {
191
                case HARDWARE: /* hardware interrupt notification */
192
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
193
                        if (msg.m_notify.interrupts & n) {
194
                            interrupt_handler(i);
195
                            if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
196
                            #ifdef TELMO
197
                            if (i == 0) {
198
                                if (no_interrupts % refresh_count_value == 0) {
199
                                    update_movement(map1, shooter1, shooter2);
200
                                    //bullet_update_movement(bullet);
201

    
202
                                    if (no_interrupts % 180 == 0) gunner_set_pos(shooter2, 775, 75);
203

    
204
                                    update_game_state(map1, shooter2, bullet_list);
205

    
206
                                    if(map_collides_gunner(map1, shooter1)){
207
                                        printf("COLLIDING\n");
208
                                    }
209

    
210
                                    /*if (gunner_collides_bullet(shooter1, bullet)) {
211
                                        printf("Bullet Collide with Shooter\n");
212
                                        gunner_set_curr_health(shooter1, gunner_get_curr_health(shooter1) - bullet_get_damage(bullet));
213
                                    }*/
214
                                    update_scale();
215

    
216
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
217
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
218

    
219
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
220
                                    double angle = get_mouse_angle(shooter1);
221
                                    gunner_set_angle(shooter1, angle - M_PI_2);
222
                                    graph_clear_screen();
223

    
224
                                    clock_t t = clock();
225

    
226
                                    map_draw   (map1);
227
                                    gunner_draw(shooter2);
228
                                    gunner_draw(shooter1);
229
                                    bullet_draw_list(bullet_list);
230

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

    
233
                                    sprite_draw(sp_crosshair);
234
                                    graph_draw();
235
                                }
236
                            }
237
                            if (i == 12) {
238
                                if (counter_mouse_ih >= 3) {
239
                                    struct packet pp = mouse_parse_packet(packet_mouse_ih);
240
                                    update_mouse(&pp);
241
                                    //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
242
                                    counter_mouse_ih = 0;
243

    
244
                                    if (last_lb ^ get_key_presses()->lb_pressed && get_key_presses()->lb_pressed) {
245
                                        shoot_bullet(shooter1, bullet_list);
246
                                    }
247

    
248
                                    last_lb = get_key_presses()->lb_pressed;
249
                                }
250
                            }
251
                            #endif
252
                        }
253
                    }
254

    
255
                    break;
256
                default:
257
                    break; /* no other notifications expected: do nothing */
258
            }
259
        } else { /* received standart message, not a notification */
260
            /* no standart message expected: do nothing */
261
        }
262
    }
263

    
264
    #ifdef TELMO
265
        gunner_dtor(shooter1); shooter1 = NULL;
266
        gunner_dtor(shooter2); shooter2 = NULL;
267

    
268
        while(list_size(bullet_list) > 0){
269
            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
270
            free(p);
271
        }
272
        if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
273
    #endif
274

    
275
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
276
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
277
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
278
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
279
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
280
    map_dtor               (map1         ); map1          = NULL;
281
    font_dtor              (consolas     ); consolas      = NULL;
282

    
283
    // Unsubscribe interrupts
284
    if (unsubscribe_all()) {
285
        if (cleanup())
286
            printf("%s: failed to cleanup.\n", __func__);
287
        return 1;
288
    }
289

    
290

    
291
    if (cleanup()) {
292
        printf("%s: failed to cleanup.\n", __func__);
293
        return 1;
294
    }
295

    
296
    return 0;
297
}