Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 193

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

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

    
29
    lcf_set_language("EN-US");
30

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

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

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

    
37
    lcf_cleanup();
38

    
39
    return 0;
40
}
41

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

    
44
    int r;
45

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

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

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

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

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

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

    
80
    #ifdef DIOGO
81
        graph_clear_screen();
82

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

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

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

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

    
105
        tickdelay(micros_to_ticks(1000000));
106

    
107
    #endif
108

    
109
    #ifdef TELMO
110
        ent_set_scale(2.0);
111

    
112
        ent_t *shooter1 = ent_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
113
        ent_set_pos(shooter1, 0, 0);
114
        ent_set_origin(ent_get_x(shooter1)-ent_get_XLength()/2.0,
115
                       ent_get_y(shooter1)-ent_get_YLength()/2.0);
116

    
117
        ent_t *shooter2 = ent_ctor(bsp_shooter, bsp_nothing);
118
        ent_set_pos(shooter2, -50, -50);
119

    
120
        graph_clear_screen();
121
        ent_draw(shooter1);
122
        sprite_draw(sp_crosshair);
123
        graph_draw();
124
    #endif
125

    
126
    /// loop stuff
127
    int ipc_status;
128
    message msg;
129
    int good = 1;
130

    
131
    #ifdef DIOGO
132
        good = 0;
133
    #endif
134

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

    
156
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
157
                                    double angle = get_mouse_angle(shooter1);
158
                                    ent_set_angle(shooter1, angle - M_PI_2);
159
                                    graph_clear_screen();
160
                                    ent_draw(shooter2);
161
                                    ent_draw(shooter1);
162
                                    sprite_draw(sp_crosshair);
163
                                    graph_draw();
164
                                }
165
                            }
166
                            #endif
167
                        }
168
                    }
169
                    #ifdef TELMO
170
                    if (counter_mouse_ih >= 3) {
171
                        struct packet pp = mouse_parse_packet(packet_mouse_ih);
172
                        update_mouse_position(&pp);
173
                        //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
174
                        counter_mouse_ih = 0;
175
                    }
176
                    #endif
177

    
178
                    break;
179
                default:
180
                    break; /* no other notifications expected: do nothing */
181
            }
182
        } else { /* received standart message, not a notification */
183
            /* no standart message expected: do nothing */
184
        }
185
        #ifdef TELMO
186
            switch (get_hor_movement()) {
187
                case LEFT:
188
                    printf("GOING LEFT.\n");
189
                    break;
190
                case RIGHT:
191
                    printf("GOING RIGHT.\n");
192
                    break;
193
            }
194
            switch (get_ver_movement()) {
195
                case UP:
196
                    printf("GOING UP.\n");
197
                    break;
198
                case DOWN:
199
                    printf("GOING DOWN.\n");
200
                    break;
201
            }
202
        #endif
203
    }
204

    
205
    #ifdef TELMO
206
        ent_dtor(shooter1); shooter1 = NULL;
207
    #endif
208

    
209
    basic_sprite_dtor(bsp_crosshair); bsp_crosshair = NULL;
210
    basic_sprite_dtor(bsp_shooter  ); bsp_shooter   = NULL;
211
    sprite_dtor      (sp_crosshair ); sp_crosshair  = NULL;
212
    font_dtor        (consolas     ); consolas      = NULL;
213

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

    
221

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

    
227
    return 0;
228
}