Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 231

History | View | Annotate | Download (9.96 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 "interrupts_func.h"
15

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

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

    
29
#include "list.h"
30

    
31
int main(int argc, char* argv[]) {
32

    
33
    lcf_set_language("EN-US");
34

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

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

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

    
41
    lcf_cleanup();
42

    
43
    return 0;
44
}
45

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

    
48
    int r;
49

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

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

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

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

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

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

    
86
    #ifdef DIOGO
87
        graph_clear_screen();
88

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

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

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

    
108
        graph_draw();
109
        rectangle_dtor(rect);
110

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

    
134

    
135
        tickdelay(micros_to_ticks(1000000));
136

    
137
    #endif
138

    
139
    #ifdef TELMO
140
        ent_set_scale(DEFAULT_SCALE);
141

    
142
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
143
        gunner_set_pos(shooter1, 75, 75);
144

    
145
        gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
146
        gunner_set_pos(shooter2, 775, 75);
147

    
148
        //bullet_t *bullet = bullet_ctor(get_bullet(), 400.0, 400.0, 2.0, -1.0);
149

    
150
        list_t *bullet_list = list_ctor();
151

    
152
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
153
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
154

    
155
        uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
156

    
157
        uint8_t last_lb = 0;
158
    #endif
159

    
160
    /// loop stuff
161
    int ipc_status;
162
    message msg;
163
    int good = 1;
164

    
165
    #ifdef DIOGO
166
        good = 0;
167
    #endif
168

    
169
    while (good) {
170
        /* Get a request message. */
171
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
172
            printf("driver_receive failed with %d", r);
173
            continue;
174
        }
175
        if (is_ipc_notify(ipc_status)) { /* received notification */
176
            switch (_ENDPOINT_P(msg.m_source)) {
177
                case HARDWARE: /* hardware interrupt notification */
178
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
179
                        if (msg.m_notify.interrupts & n) {
180
                            interrupt_handler(i);
181
                            if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
182
                            #ifdef TELMO
183
                            if (i == 0) {
184
                                if (no_interrupts % refresh_count_value == 0) {
185
                                    update_movement(map1, shooter1);
186
                                    //bullet_update_movement(bullet);
187
                                    update_game_state(map1, shooter2, bullet_list);
188

    
189
                                    if(map_collides_gunner(map1, shooter1)){
190
                                        printf("COLLIDING\n");
191
                                    }
192

    
193
                                    /*if (gunner_collides_bullet(shooter1, bullet)) {
194
                                        printf("Bullet Collide with Shooter\n");
195
                                        gunner_set_curr_health(shooter1, gunner_get_curr_health(shooter1) - bullet_get_damage(bullet));
196
                                    }*/
197

    
198
                                    update_scale();
199

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

    
203
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
204
                                    double angle = get_mouse_angle(shooter1);
205
                                    gunner_set_angle(shooter1, angle - M_PI_2);
206
                                    graph_clear_screen();
207

    
208
                                    clock_t t = clock();
209

    
210
                                    map_draw   (map1);
211
                                    gunner_draw(shooter2);
212
                                    gunner_draw(shooter1);
213
                                    bullet_draw_list(bullet_list);
214

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

    
217
                                    sprite_draw(sp_crosshair);
218
                                    graph_draw();
219
                                }
220
                            }
221
                            if (i == 12) {
222
                                if (counter_mouse_ih >= 3) {
223
                                    struct packet pp = mouse_parse_packet(packet_mouse_ih);
224
                                    update_mouse(&pp);
225
                                    //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
226
                                    counter_mouse_ih = 0;
227

    
228
                                    if (last_lb ^ get_key_presses()->lb_pressed && get_key_presses()->lb_pressed) {
229
                                        shoot_bullet(shooter1, bullet_list);
230
                                    }
231

    
232
                                    last_lb = get_key_presses()->lb_pressed;
233
                                }
234
                            }
235
                            #endif
236
                        }
237
                    }
238

    
239
                    break;
240
                default:
241
                    break; /* no other notifications expected: do nothing */
242
            }
243
        } else { /* received standart message, not a notification */
244
            /* no standart message expected: do nothing */
245
        }
246
    }
247

    
248
    #ifdef TELMO
249
        gunner_dtor(shooter1); shooter1 = NULL;
250
        gunner_dtor(shooter2); shooter2 = NULL;
251

    
252
        while(list_size(bullet_list) > 0){
253
            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
254
            free(p);
255
        }
256
        if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
257
    #endif
258

    
259
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
260
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
261
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
262
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
263
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
264
    map_dtor               (map1         ); map1          = NULL;
265
    font_dtor              (consolas     ); consolas      = NULL;
266

    
267
    // Unsubscribe interrupts
268
    if (unsubscribe_all()) {
269
        if (cleanup())
270
            printf("%s: failed to cleanup.\n", __func__);
271
        return 1;
272
    }
273

    
274

    
275
    if (cleanup()) {
276
        printf("%s: failed to cleanup.\n", __func__);
277
        return 1;
278
    }
279

    
280
    return 0;
281
}