Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 203

History | View | Annotate | Download (7.2 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

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

    
30
    lcf_set_language("EN-US");
31

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

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

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

    
38
    lcf_cleanup();
39

    
40
    return 0;
41
}
42

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

    
45
    int r;
46

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

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

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

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

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

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

    
81
    #ifdef DIOGO
82
        graph_clear_screen();
83

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

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

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

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

    
106
        tickdelay(micros_to_ticks(1000000));
107

    
108
    #endif
109

    
110
    #ifdef TELMO
111
        ent_set_scale(2.0);
112

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

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

    
121
        ent_t *bullet = ent_ctor(get_bullet(), bsp_nothing);
122
        ent_set_pos(bullet, 400, 400);
123

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

    
129
        uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
130
    #endif
131

    
132
    /// loop stuff
133
    int ipc_status;
134
    message msg;
135
    int good = 1;
136

    
137
    #ifdef DIOGO
138
        good = 0;
139
    #endif
140

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

    
162
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
163
                                    double angle = get_mouse_angle(shooter1);
164
                                    gunner_set_angle(shooter1, angle - M_PI_2);
165
                                    graph_clear_screen();
166
                                    gunner_draw(shooter2);
167
                                    gunner_draw(shooter1);
168
                                    ent_draw(bullet);
169
                                    sprite_draw(sp_crosshair);
170
                                    graph_draw();
171
                                }
172
                            }
173
                            #endif
174
                        }
175
                    }
176
                    #ifdef TELMO
177
                    if (counter_mouse_ih >= 3) {
178
                        struct packet pp = mouse_parse_packet(packet_mouse_ih);
179
                        update_mouse_position(&pp);
180
                        //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
181
                        counter_mouse_ih = 0;
182
                    }
183
                    #endif
184

    
185
                    break;
186
                default:
187
                    break; /* no other notifications expected: do nothing */
188
            }
189
        } else { /* received standart message, not a notification */
190
            /* no standart message expected: do nothing */
191
        }
192
    }
193

    
194
    #ifdef TELMO
195
        gunner_dtor(shooter1); shooter1 = NULL;
196
    #endif
197

    
198
    basic_sprite_dtor(bsp_crosshair); bsp_crosshair = NULL;
199
    basic_sprite_dtor(bsp_shooter  ); bsp_shooter   = NULL;
200
    sprite_dtor      (sp_crosshair ); sp_crosshair  = NULL;
201
    font_dtor        (consolas     ); consolas      = NULL;
202

    
203
    // Unsubscribe interrupts
204
    if (unsubscribe_all()) {
205
        if (cleanup())
206
            printf("%s: failed to cleanup.\n", __func__);
207
        return 1;
208
    }
209

    
210

    
211
    if (cleanup()) {
212
        printf("%s: failed to cleanup.\n", __func__);
213
        return 1;
214
    }
215

    
216
    return 0;
217
}