Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 212

History | View | Annotate | Download (7.6 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 "map.h"
28

    
29
int main(int argc, char* argv[]) {
30

    
31
    lcf_set_language("EN-US");
32

    
33
    //lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
34

    
35
    lcf_log_output("/home/lcom/labs/proj/output.txt");
36

    
37
    if (lcf_start(argc, argv)) return 1;
38

    
39
    lcf_cleanup();
40

    
41
    return 0;
42
}
43

    
44
int(proj_main_loop)(int argc, char *argv[]) {
45

    
46
    int r;
47

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

    
51
    /// subscribe interrupts
52
    if (subscribe_all()) { return 1; }
53

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

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

    
74
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
75
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
76
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
77
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
78

    
79
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
80
    }
81

    
82
    #ifdef DIOGO
83
        graph_clear_screen();
84

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

    
94
        text_t *txt  = text_ctor(consolas, "Hello world!");
95
        text_set_color(txt, 0x888888);
96

    
97
        text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
98
                          rectangle_get_y(rect)+rectangle_get_h(rect)/2);
99
        text_set_valign(txt, text_valign_center);
100
        text_set_halign(txt, text_halign_center);
101
        text_draw(txt);
102
        text_dtor(txt);
103

    
104
        graph_draw();
105
        rectangle_dtor(rect);
106

    
107
        tickdelay(micros_to_ticks(1000000));
108

    
109
    #endif
110

    
111
    #ifdef TELMO
112
        ent_set_scale(1.0);
113

    
114
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
115
        gunner_set_pos(shooter1, 0, 0);
116
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
117
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
118

    
119
        gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
120
        gunner_set_pos(shooter2, -50, -50);
121

    
122
        bullet_t *bullet = bullet_ctor(get_bullet());
123
        bullet_set_pos(bullet, 400, 400);
124

    
125
        graph_clear_screen();
126
        gunner_draw(shooter1);
127
        sprite_draw(sp_crosshair);
128
        graph_draw();
129

    
130
        uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
131

    
132
        basic_sprite_t *bsp_pink = get_map();
133
        sprite_t *pink = sprite_ctor(bsp_pink);
134
        basic_sprite_dtor(bsp_pink);
135
    #endif
136

    
137
    /// loop stuff
138
    int ipc_status;
139
    message msg;
140
    int good = 1;
141

    
142
    #ifdef DIOGO
143
        good = 0;
144
    #endif
145

    
146
    while (good) {
147
        /* Get a request message. */
148
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
149
            printf("driver_receive failed with %d", r);
150
            continue;
151
        }
152
        if (is_ipc_notify(ipc_status)) { /* received notification */
153
            switch (_ENDPOINT_P(msg.m_source)) {
154
                case HARDWARE: /* hardware interrupt notification */
155
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
156
                        if (msg.m_notify.interrupts & n) {
157
                            interrupt_handler(i);
158
                            if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
159
                            #ifdef TELMO
160
                            if (i == 0) {
161
                                if (no_interrupts % refresh_count_value == 0) {
162
                                    update_movement(shooter1);
163
                                    update_scale();
164
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
165
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
166

    
167
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
168
                                    double angle = get_mouse_angle(shooter1);
169
                                    gunner_set_angle(shooter1, angle - M_PI_2);
170
                                    graph_clear_screen();
171

    
172
                                    clock_t t = clock();
173

    
174
                                    sprite_draw(pink);
175
                                    gunner_draw(shooter2);
176
                                    gunner_draw(shooter1);
177
                                    bullet_draw(bullet);
178

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

    
181
                                    sprite_draw(sp_crosshair);
182
                                    graph_draw();
183
                                }
184
                            }
185
                            #endif
186
                        }
187
                    }
188
                    #ifdef TELMO
189
                    if (counter_mouse_ih >= 3) {
190
                        struct packet pp = mouse_parse_packet(packet_mouse_ih);
191
                        update_mouse_position(&pp);
192
                        //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
193
                        counter_mouse_ih = 0;
194
                    }
195
                    #endif
196

    
197
                    break;
198
                default:
199
                    break; /* no other notifications expected: do nothing */
200
            }
201
        } else { /* received standart message, not a notification */
202
            /* no standart message expected: do nothing */
203
        }
204
    }
205

    
206
    #ifdef TELMO
207
        gunner_dtor(shooter1); shooter1 = NULL;
208
        sprite_dtor(pink); pink = NULL;
209
    #endif
210

    
211
    basic_sprite_dtor(bsp_crosshair); bsp_crosshair = NULL;
212
    basic_sprite_dtor(bsp_shooter  ); bsp_shooter   = NULL;
213
    sprite_dtor      (sp_crosshair ); sp_crosshair  = NULL;
214
    font_dtor        (consolas     ); consolas      = NULL;
215

    
216
    // Unsubscribe interrupts
217
    if (unsubscribe_all()) {
218
        if (cleanup())
219
            printf("%s: failed to cleanup.\n", __func__);
220
        return 1;
221
    }
222

    
223

    
224
    if (cleanup()) {
225
        printf("%s: failed to cleanup.\n", __func__);
226
        return 1;
227
    }
228

    
229
    return 0;
230
}