Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 216

History | View | Annotate | Download (8.01 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 202 up20180655
#include "bullet.h"
27 216 up20180642
#include "map1.h"
28 183 up20180642
29 144 up20180655
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 189 up20180655
    lcf_log_output("/home/lcom/labs/proj/output.txt");
36 144 up20180655
37
    if (lcf_start(argc, argv)) return 1;
38
39
    lcf_cleanup();
40
41
    return 0;
42
}
43 147 up20180655
44 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
45 170 up20180642
46
    int r;
47
48 192 up20180642
    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 170 up20180642
    /// subscribe interrupts
52
    if (subscribe_all()) { return 1; }
53
54
    /// initialize graphics
55 166 up20180642
    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 152 up20180642
        return 1;
59
    }
60
61 188 up20180642
    /// Load stuff
62 216 up20180642
    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
    map_t                *map1      = NULL;
67
    sprite_t             *sp_crosshair  = NULL;
68 188 up20180642
    {
69
        graph_clear_screen();
70
        text_t *txt = text_ctor(consolas, "Loading...");
71
        text_draw(txt);
72
        text_dtor(txt);
73
        graph_draw();
74 192 up20180642
75
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
76
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
77
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
78
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
79 216 up20180642
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
80 192 up20180642
81
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
82 188 up20180642
    }
83 192 up20180642
84 159 up20180642
    #ifdef DIOGO
85 188 up20180642
        graph_clear_screen();
86 183 up20180642
87 188 up20180642
        rectangle_t *rect = rectangle_ctor(0,0,400,100);
88
        rectangle_set_pos(rect,
89
                          graph_get_XRes()/2    - rectangle_get_w(rect)/2,
90
                          graph_get_YRes()*0.25 - rectangle_get_h(rect)/2);
91
        rectangle_set_fill_color(rect, BLACK);
92 179 up20180642
        rectangle_set_outline_width(rect, 2);
93 188 up20180642
        rectangle_set_outline_color(rect, WHITE);
94 179 up20180642
        rectangle_draw(rect);
95
96 188 up20180642
        text_t *txt  = text_ctor(consolas, "Hello world!");
97
        text_set_color(txt, 0x888888);
98 182 up20180642
99 188 up20180642
        text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
100
                          rectangle_get_y(rect)+rectangle_get_h(rect)/2);
101
        text_set_valign(txt, text_valign_center);
102
        text_set_halign(txt, text_halign_center);
103 183 up20180642
        text_draw(txt);
104
        text_dtor(txt);
105
106
        graph_draw();
107 188 up20180642
        rectangle_dtor(rect);
108 183 up20180642
109
        tickdelay(micros_to_ticks(1000000));
110
111 159 up20180642
    #endif
112 152 up20180642
113 171 up20180655
    #ifdef TELMO
114 216 up20180642
        ent_set_scale(DEFAULT_SCALE);
115 192 up20180642
116 201 up20180642
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
117
        gunner_set_pos(shooter1, 0, 0);
118 192 up20180642
119 201 up20180642
        gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
120
        gunner_set_pos(shooter2, -50, -50);
121 192 up20180642
122 204 up20180655
        bullet_t *bullet = bullet_ctor(get_bullet());
123
        bullet_set_pos(bullet, 400, 400);
124 202 up20180655
125 216 up20180642
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
126
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
127 214 up20180642
128 200 up20180655
        uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
129 171 up20180655
    #endif
130
131 149 up20180655
    /// loop stuff
132 170 up20180642
    int ipc_status;
133 149 up20180655
    message msg;
134 147 up20180655
    int good = 1;
135 168 up20180642
136 183 up20180642
    #ifdef DIOGO
137
        good = 0;
138
    #endif
139
140 147 up20180655
    while (good) {
141
        /* Get a request message. */
142
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
143
            printf("driver_receive failed with %d", r);
144
            continue;
145
        }
146
        if (is_ipc_notify(ipc_status)) { /* received notification */
147
            switch (_ENDPOINT_P(msg.m_source)) {
148
                case HARDWARE: /* hardware interrupt notification */
149 153 up20180655
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
150
                        if (msg.m_notify.interrupts & n) {
151
                            interrupt_handler(i);
152 167 up20180655
                            if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
153 171 up20180655
                            #ifdef TELMO
154 187 up20180655
                            if (i == 0) {
155
                                if (no_interrupts % refresh_count_value == 0) {
156
                                    update_movement(shooter1);
157 216 up20180642
158
                                    if(map_collides(map1, gunner_get_x(shooter1), gunner_get_y(shooter1))){
159
                                        printf("COLLIDING\n");
160
                                    }
161
162 200 up20180655
                                    update_scale();
163 192 up20180642
164 216 up20180642
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
165
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
166 214 up20180642
167 192 up20180642
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
168 187 up20180655
                                    double angle = get_mouse_angle(shooter1);
169 201 up20180642
                                    gunner_set_angle(shooter1, angle - M_PI_2);
170 187 up20180655
                                    graph_clear_screen();
171 207 up20180642
172
                                    clock_t t = clock();
173
174 216 up20180642
                                    map_draw   (map1);
175 201 up20180642
                                    gunner_draw(shooter2);
176
                                    gunner_draw(shooter1);
177 204 up20180655
                                    bullet_draw(bullet);
178 207 up20180642
179
                                    t = clock()-t; printf("%d\n", (t*1000)/CLOCKS_PER_SEC);
180
181 192 up20180642
                                    sprite_draw(sp_crosshair);
182 187 up20180655
                                    graph_draw();
183
                                }
184 184 up20180655
                            }
185 171 up20180655
                            #endif
186 153 up20180655
                        }
187 147 up20180655
                    }
188 187 up20180655
                    #ifdef TELMO
189
                    if (counter_mouse_ih >= 3) {
190
                        struct packet pp = mouse_parse_packet(packet_mouse_ih);
191
                        update_mouse_position(&pp);
192 189 up20180655
                        //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
193 187 up20180655
                        counter_mouse_ih = 0;
194
                    }
195
                    #endif
196 167 up20180655
197 147 up20180655
                    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 149 up20180655
206 192 up20180642
    #ifdef TELMO
207 201 up20180642
        gunner_dtor(shooter1); shooter1 = NULL;
208 216 up20180642
        gunner_dtor(shooter2); shooter2 = NULL;
209
        bullet_dtor(bullet); bullet = NULL;
210 192 up20180642
    #endif
211 188 up20180642
212 216 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
213
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
214
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
215
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
216
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
217
    map_dtor               (map1         ); map1          = NULL;
218
    font_dtor              (consolas     ); consolas      = NULL;
219 192 up20180642
220 153 up20180655
    // Unsubscribe interrupts
221 155 up20180655
    if (unsubscribe_all()) {
222
        if (cleanup())
223
            printf("%s: failed to cleanup.\n", __func__);
224 152 up20180642
        return 1;
225
    }
226
227 155 up20180655
228
    if (cleanup()) {
229
        printf("%s: failed to cleanup.\n", __func__);
230 152 up20180642
        return 1;
231
    }
232
233 149 up20180655
    return 0;
234 147 up20180655
}