Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 197

History | View | Annotate | Download (7.5 KB)

1 144 up20180655
#include <lcom/lcf.h>
2
#include <lcom/proj.h>
3
#include <lcom/liblm.h>
4 185 up20180655
#include <math.h>
5 144 up20180655
6 149 up20180655
#include "proj_macros.h"
7 179 up20180642
#include "proj_func.h"
8 147 up20180655
9 149 up20180655
#include "kbc.h"
10
#include "timer.h"
11
#include "keyboard.h"
12 150 up20180655
#include "mouse.h"
13 189 up20180655
#include "graph.h"
14 153 up20180655
#include "interrupts_func.h"
15 149 up20180655
16 179 up20180642
#include "graph.h"
17
#include "sprite.h"
18
#include "rectangle.h"
19 182 up20180642
#include "font.h"
20 192 up20180642
#include "ent.h"
21 168 up20180642
22 192 up20180642
#include "crosshair.h"
23
#include "shooter.h"
24
#include "pistol.h"
25
#include "nothing.h"
26 183 up20180642
27 144 up20180655
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 189 up20180655
    lcf_log_output("/home/lcom/labs/proj/output.txt");
34 144 up20180655
35
    if (lcf_start(argc, argv)) return 1;
36
37
    lcf_cleanup();
38
39
    return 0;
40
}
41 147 up20180655
42 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
43 170 up20180642
44
    int r;
45
46 192 up20180642
    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 170 up20180642
    /// subscribe interrupts
50
    if (subscribe_all()) { return 1; }
51
52
    /// initialize graphics
53 166 up20180642
    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 152 up20180642
        return 1;
57
    }
58
59 188 up20180642
    /// Load stuff
60 192 up20180642
    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 188 up20180642
    {
66
        graph_clear_screen();
67
        text_t *txt = text_ctor(consolas, "Loading...");
68
        text_draw(txt);
69
        text_dtor(txt);
70
        graph_draw();
71 192 up20180642
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 188 up20180642
    }
79 192 up20180642
80 159 up20180642
    #ifdef DIOGO
81 188 up20180642
        graph_clear_screen();
82 183 up20180642
83 188 up20180642
        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 179 up20180642
        rectangle_set_outline_width(rect, 2);
89 188 up20180642
        rectangle_set_outline_color(rect, WHITE);
90 179 up20180642
        rectangle_draw(rect);
91
92 188 up20180642
        text_t *txt  = text_ctor(consolas, "Hello world!");
93
        text_set_color(txt, 0x888888);
94 182 up20180642
95 188 up20180642
        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 183 up20180642
        text_draw(txt);
100
        text_dtor(txt);
101
102
        graph_draw();
103 188 up20180642
        rectangle_dtor(rect);
104 183 up20180642
105
        tickdelay(micros_to_ticks(1000000));
106
107 159 up20180642
    #endif
108 152 up20180642
109 171 up20180655
    #ifdef TELMO
110 192 up20180642
        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 174 up20180642
        graph_clear_screen();
121 192 up20180642
        ent_draw(shooter1);
122
        sprite_draw(sp_crosshair);
123 174 up20180642
        graph_draw();
124 171 up20180655
    #endif
125
126 149 up20180655
    /// loop stuff
127 170 up20180642
    int ipc_status;
128 149 up20180655
    message msg;
129 147 up20180655
    int good = 1;
130 168 up20180642
131 183 up20180642
    #ifdef DIOGO
132
        good = 0;
133
    #endif
134
135 147 up20180655
    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 153 up20180655
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
145
                        if (msg.m_notify.interrupts & n) {
146
                            interrupt_handler(i);
147 167 up20180655
                            if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
148 171 up20180655
                            #ifdef TELMO
149 187 up20180655
                            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 192 up20180642
                                    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 187 up20180655
                                    double angle = get_mouse_angle(shooter1);
158 192 up20180642
                                    ent_set_angle(shooter1, angle - M_PI_2);
159 187 up20180655
                                    graph_clear_screen();
160 192 up20180642
                                    ent_draw(shooter2);
161
                                    ent_draw(shooter1);
162
                                    sprite_draw(sp_crosshair);
163 187 up20180655
                                    graph_draw();
164
                                }
165 184 up20180655
                            }
166 171 up20180655
                            #endif
167 153 up20180655
                        }
168 147 up20180655
                    }
169 187 up20180655
                    #ifdef TELMO
170
                    if (counter_mouse_ih >= 3) {
171
                        struct packet pp = mouse_parse_packet(packet_mouse_ih);
172
                        update_mouse_position(&pp);
173 189 up20180655
                        //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
174 187 up20180655
                        counter_mouse_ih = 0;
175
                    }
176
                    #endif
177 167 up20180655
178 147 up20180655
                    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 174 up20180642
        #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 147 up20180655
    }
204 149 up20180655
205 192 up20180642
    #ifdef TELMO
206
        ent_dtor(shooter1); shooter1 = NULL;
207
    #endif
208 188 up20180642
209 192 up20180642
    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 153 up20180655
    // Unsubscribe interrupts
215 155 up20180655
    if (unsubscribe_all()) {
216
        if (cleanup())
217
            printf("%s: failed to cleanup.\n", __func__);
218 152 up20180642
        return 1;
219
    }
220
221 155 up20180655
222
    if (cleanup()) {
223
        printf("%s: failed to cleanup.\n", __func__);
224 152 up20180642
        return 1;
225
    }
226
227 149 up20180655
    return 0;
228 147 up20180655
}