Project

General

Profile

Revision 250

in-game timer

View differences:

proj/include/font.h
22 22
typedef struct text text_t;
23 23
text_t* (text_ctor)(const font_t *fnt, const char *txt);
24 24
void    (text_dtor)(text_t *p);
25
char* (text_get_string)(const text_t *p);
25 26
void (text_set_text)  (text_t *p, const char *txt);
26 27
void (text_set_pos)   (text_t *p, int16_t x, int16_t y);
27 28
void (text_set_size)  (text_t *p, unsigned size);
proj/include/mouse.h
18 18
/**
19 19
 * @brief   Parse 3 bytes and returns it as a parsed, struct packet.
20 20
 * @param   packet_bytes    array of bytes to parse
21
 * @return  parsed struct packet
21
 * @param   pp              Pointer to packet to store the information.
22 22
 */
23
struct packet (mouse_parse_packet)(const uint8_t *packet_bytes);
23
void (mouse_parse_packet)(const uint8_t *packet_bytes, struct packet *pp);
24 24

  
25 25
/**
26 26
 * @brief Polls mouse for data. Blocks execution until a valid mouse packet is obtained.
proj/include/proj_func.h
2 2
#define PROJ_FUNC_H_INCLUDED
3 3

  
4 4
#include "ent.h"
5
#include "font.h"
5 6

  
6 7
#include <stdint.h>
7 8

  
......
42 43

  
43 44
void update_scale(void);
44 45

  
45
int32_t get_mouse_X(void);
46
int32_t* get_mouse_X(void);
46 47

  
47
int32_t get_mouse_Y(void);
48
int32_t* get_mouse_Y(void);
48 49

  
49 50
/**
50 51
 * @brief
......
55 56
 */
56 57
double get_mouse_angle(gunner_t *p);
57 58

  
59
typedef struct timer {
60
    int time;
61
    text_t *text;
62
    char *array;
63
} text_timer_t;
64

  
65
text_timer_t* (timer_ctor)(const font_t *fnt);
66

  
67
void (timer_update)(text_timer_t *p);
68

  
69
void (timer_dtor)(text_timer_t *p);
70

  
58 71
#endif /* end of include guard: PROJ_FUNC_H_INCLUDED */
proj/include/utils.h
69 69
double max_d(double a, double b);
70 70

  
71 71

  
72
double abs_d(double a);
73

  
74

  
72 75
#endif //UTILS_H_INCLUDED
proj/src/ent.c
145 145
}
146 146

  
147 147
void (bullet_update_movement_list)(list_t *bullet_list){
148
    if (bullet_list == NULL) return;
149 148
    if (list_size(bullet_list) == 0) return;
150 149

  
151 150
    list_node_t *it = list_begin(bullet_list);
proj/src/fast_math.c
1 1
#include <lcom/lcf.h>
2 2

  
3 3
#include "fast_math.h"
4
#include "utils.h"
4 5

  
5 6
#include <math.h>
6 7

  
proj/src/font.c
128 128
    if(p->txt == NULL) return;
129 129
    strcpy(p->txt, txt);
130 130
}
131
char* (text_get_string)(const text_t *p){return p->txt; }
131 132
void (text_set_pos)   (text_t *p, int16_t x, int16_t y   ){ p->x = x; p->y = y; }
132 133
void (text_set_size)  (text_t *p, unsigned size          ){ p->size = size    ; }
133 134
void (text_set_color) (text_t *p, uint32_t color         ){ p->color = color  ; }
proj/src/graph.c
245 245

  
246 246
/// PIXEL DRAWING
247 247
int (graph_set_pixel)(uint16_t x, uint16_t y, uint32_t color) {
248
    if (x < 0 || vbe_mem_info.XResolution <= x || y < 0 || vbe_mem_info.YResolution <= y) {
248
    //pixels are certain to be inside can reduce lag
249
    /*if (x < 0 || vbe_mem_info.XResolution <= x || y < 0 || vbe_mem_info.YResolution <= y) {
249 250
        //printf("%s: invalid pixel.\n", __func__);
250 251
        return OUT_OF_RANGE;
251
    }
252
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * graph_get_bytes_pixel();
253
    memcpy(video_buf + pos, &color, graph_get_bytes_pixel());
252
    }*/
253
    unsigned int pos = (x + y * vbe_mem_info.XResolution) * 3/*graph_get_bytes_pixel()*/;
254
    memcpy(video_buf + pos, &color, 3/*graph_get_bytes_pixel()*/);
254 255
    return SUCCESS;
255 256
}
256 257
void (graph_set_pixel_pos)(unsigned pos, uint32_t color){
......
399 400
        ymin = max(ymin-(int16_t)p->scale-2, 0); ymax = min(ymax+(int16_t)p->scale+2, graph_get_YRes());
400 401
    }
401 402
    const uint8_t *map = basic_sprite_get_map(p->bsp);
402
    const uint16_t bytes_pixel = graph_get_bytes_pixel();
403
    const uint16_t bytes_pixel = 3/*graph_get_bytes_pixel()*/;
403 404
    for(int16_t u, v, y = ymin; y < ymax; ++y){
404 405
        uint8_t *place = video_buf + (xmin + y*graph_get_XRes())*bytes_pixel;
405 406
        for(int16_t x = xmin; x < xmax; ++x, place += bytes_pixel){
proj/src/interrupts_func.c
145 145
    return r;
146 146
}
147 147

  
148
void interrupt_handler(uint8_t handler) {
148
void (interrupt_handler)(uint8_t handler) {
149 149
    if (handler >= 32)              return;
150 150
    if ((*ih[handler]) == NULL)     return;
151 151
    (*ih[handler])();
152 152
}
153

  
154
void (timer_manager)() {
155

  
156
}
157

  
158
void (mouse_manager)() {
159

  
160
}
proj/src/mouse.c
43 43
    }
44 44
}
45 45

  
46
struct packet (mouse_parse_packet)(const uint8_t *packet_bytes){
47
    struct packet pp;
48
    pp.bytes[0] = packet_bytes[0];
49
    pp.bytes[1] = packet_bytes[1];
50
    pp.bytes[2] = packet_bytes[2];
51
    pp.rb       = pp.bytes[0] & RIGHT_BUTTON;
52
    pp.mb       = pp.bytes[0] & MIDDLE_BUTTON;
53
    pp.lb       = pp.bytes[0] & LEFT_BUTTON;
54
    pp.delta_x  = sign_extend_byte((packet_bytes[0] & MSB_X_DELTA) != 0, pp.bytes[1]);
55
    pp.delta_y  = sign_extend_byte((packet_bytes[0] & MSB_Y_DELTA) != 0, pp.bytes[2]);
56
    pp.x_ov     = pp.bytes[0] & X_OVERFLOW;
57
    pp.y_ov     = pp.bytes[0] & Y_OVERFLOW;
58
    return pp;
46
void (mouse_parse_packet)(const uint8_t *packet_bytes, struct packet *pp){
47
    pp->bytes[0] = packet_bytes[0];
48
    pp->bytes[1] = packet_bytes[1];
49
    pp->bytes[2] = packet_bytes[2];
50
    pp->rb       = pp->bytes[0] & RIGHT_BUTTON;
51
    pp->mb       = pp->bytes[0] & MIDDLE_BUTTON;
52
    pp->lb       = pp->bytes[0] & LEFT_BUTTON;
53
    pp->delta_x  = sign_extend_byte((packet_bytes[0] & MSB_X_DELTA) != 0, pp->bytes[1]);
54
    pp->delta_y  = sign_extend_byte((packet_bytes[0] & MSB_Y_DELTA) != 0, pp->bytes[2]);
55
    pp->x_ov     = pp->bytes[0] & X_OVERFLOW;
56
    pp->y_ov     = pp->bytes[0] & Y_OVERFLOW;
59 57
}
60 58

  
61 59
int mouse_poll(struct packet *pp, uint16_t period){
......
68 66
        if((ret = mouse_poll_byte(&byte, period))) return ret;
69 67
        packet[i] = byte;
70 68
    }
71
    *pp = mouse_parse_packet(packet);
69
    mouse_parse_packet(packet, pp);
72 70
    return SUCCESS;
73 71
}
74 72

  
proj/src/proj.c
171 171
    #ifndef DIOGO
172 172
        ent_set_scale(DEFAULT_SCALE);
173 173

  
174
        text_timer_t *in_game_timer = timer_ctor(consolas);
175

  
174 176
        list_t *shooter_list = list_ctor();
175 177

  
176 178
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
......
184 186
        list_insert(shooter_list, list_end(shooter_list), shooter1);
185 187
        list_insert(shooter_list, list_end(shooter_list), shooter2);
186 188

  
187
        //bullet_t *bullet = bullet_ctor(get_bullet(), 400.0, 400.0, 2.0, -1.0);
188

  
189 189
        list_t *bullet_list  = list_ctor();
190 190

  
191 191
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
192 192
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
193 193

  
194
        uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
195

  
194
        //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
195
        double angle; // mouse angle
196
        int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y();
196 197
        uint8_t last_lb = 0;
198
        struct packet pp;
199
        keys_t *keys = get_key_presses();
197 200

  
198 201
        /// loop stuff
199 202
        int ipc_status;
......
214 217
                                interrupt_handler(i);
215 218
                                if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
216 219
                                if (i == 0) {
217
                                    if (no_interrupts % refresh_count_value == 0) {
218
                                            update_movement(map1, shooter1, get_key_presses(), shooter_list);
220
                                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
221
                                    //if (no_interrupts % refresh_count_value == 0) {
222
                                            update_movement(map1, shooter1, keys, shooter_list);
219 223

  
220 224
                                            update_game_state(map1, shooter_list, bullet_list);
221 225

  
222
                                            update_scale();
223
                                            sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
224
                                            double angle = get_mouse_angle(shooter1);
226
                                            //update_scale();
227
                                            sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
228
                                            angle = get_mouse_angle(shooter1);
225 229
                                            gunner_set_angle(shooter1, angle - M_PI_2);
226 230
                                            graph_clear_screen();
227 231

  
......
232 236
                                            gunner_draw_list(shooter_list);
233 237
                                            bullet_draw_list(bullet_list);
234 238

  
239
                                            text_draw(in_game_timer->text);
235 240
                                            sprite_draw(sp_crosshair);
236 241
                                            graph_draw();
237
                                        }
242
                                        //}
238 243
                                    }
239
                                    if (i == 12) {
244
                                    else if (i == 12) {
240 245
                                        if (counter_mouse_ih >= 3) {
241
                                            struct packet pp = mouse_parse_packet(packet_mouse_ih);
246
                                            mouse_parse_packet(packet_mouse_ih, &pp);
242 247
                                            update_mouse(&pp);
243
                                            //printf("X: %d Y: %d\n", get_mouse_X(), get_mouse_Y());
244
                                            counter_mouse_ih = 0;
245
                                            if (last_lb ^ get_key_presses()->lb_pressed && get_key_presses()->lb_pressed) {
248

  
249
                                            if (last_lb ^ keys->lb_pressed && keys->lb_pressed) {
246 250
                                                shoot_bullet(shooter1, bullet_list, bsp_bullet);
247 251
                                            }
248
                                            last_lb = get_key_presses()->lb_pressed;
252
                                            last_lb = keys->lb_pressed;
253
                                            counter_mouse_ih = 0;
254

  
249 255
                                        }
250 256
                                    }
251 257
                                }
......
272 278
        if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
273 279
        if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
274 280

  
281
        timer_dtor(in_game_timer); in_game_timer = NULL;
282

  
275 283
        basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
276 284
        basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
277 285
        sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
proj/src/proj_func.c
40 40
        case D_BREAK_CODE       : key_presses.d_pressed     = 0;        break;
41 41
        case CTRL_MAKE_CODE     : key_presses.ctrl_pressed  = 1;        break;
42 42
        case CTRL_BREAK_CODE    : key_presses.ctrl_pressed  = 0;        break;
43
        case PLUS_MAKE_CODE     : key_presses.plus_pressed  = 1;        break;
44
        case PLUS_BREAK_CODE    : key_presses.plus_pressed  = 0;        break;
45
        case MINUS_MAKE_CODE    : key_presses.minus_pressed = 1;        break;
46
        case MINUS_BREAK_CODE   : key_presses.minus_pressed = 0;        break;
43
        case PLUS_MAKE_CODE     : key_presses.plus_pressed  = 1;    update_scale();        break;
44
        case PLUS_BREAK_CODE    : key_presses.plus_pressed  = 0;    update_scale();        break;
45
        case MINUS_MAKE_CODE    : key_presses.minus_pressed = 1;    update_scale();        break;
46
        case MINUS_BREAK_CODE   : key_presses.minus_pressed = 0;    update_scale();        break;
47 47
        }
48 48
    }
49 49
}
......
181 181
    return &key_presses;
182 182
}
183 183

  
184
int32_t get_mouse_X(void) { return mouse_x; }
184
int32_t* get_mouse_X(void) { return &mouse_x; }
185 185

  
186
int32_t get_mouse_Y(void) { return mouse_y; }
186
int32_t* get_mouse_Y(void) { return &mouse_y; }
187 187

  
188 188
double get_mouse_angle(gunner_t *p) {
189 189
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
190 190
}
191

  
192
text_timer_t* (timer_ctor)(const font_t *fnt){
193
    if(fnt == NULL) return NULL;
194
    text_timer_t *ret = malloc(sizeof(timer_t));
195
    if (ret == NULL) return NULL;
196
    ret->time = 0;
197
    ret->text = text_ctor(fnt, "000s");
198
    ret->array = text_get_string(ret->text);
199
    text_set_color(ret->text, 0x888888);
200
    return ret;
201
}
202

  
203
void (timer_update)(text_timer_t *p){
204
    if (p->time >= 999) return;
205
    p->time++;
206
    p->array[2] = p->time % 10 + '0';
207
    p->array[1] = (p->time/10) % 10 + '0';
208
    p->array[0] = (p->time/100) % 10 + '0';
209
}
210

  
211
void (timer_dtor)(text_timer_t *p){
212
    if (p == NULL) return;
213
    text_dtor(p->text);
214
    free(p);
215
}
proj/src/utils.c
36 36

  
37 37
double min_d(double a, double b){ return (b < a ? b : a); }
38 38
double max_d(double a, double b){ return (a < b ? b : a); }
39
double abs_d(double a)          { return (a < 0 ? -a: a); }

Also available in: Unified diff