Project

General

Profile

Revision 251

menu work

View differences:

proj/include/proj_func.h
56 56
 */
57 57
double get_mouse_angle(gunner_t *p);
58 58

  
59
typedef struct menu menu_t;
60

  
61
menu_t* (menu_ctor)(const font_t *fnt);
62

  
63
int (menu_update_state)(menu_t *menu, int click);
64

  
65
void (menu_draw)(menu_t *menu);
66

  
67
void (menu_dtor)(menu_t *p);
68

  
59 69
typedef struct timer {
60 70
    int time;
61 71
    text_t *text;
......
66 76

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

  
79
void (timer_reset)(text_timer_t *p);
80

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

  
71 83
#endif /* end of include guard: PROJ_FUNC_H_INCLUDED */
proj/include/proj_macros.h
30 30
#define SHOOTER_SPEED   5   /** @brief Shooter speed */
31 31
#define BULLET_SPEED    8   /** @brief Bullet speet */
32 32

  
33
// Game States
34
#define MENU            0
35
#define GAME            1
36
#define TEST            2
37
#define EXIT            3
38

  
33 39
// Extra Keys
34 40
#define ESC_MAKE_CODE   0x01    /** @brief ESC Make Code */
35 41
#define ESC_BREAK_CODE  0x81    /** @brief ESC Break Code */
......
43 49
#define DEFAULT_SCALE   0.5                  /** @brief Default zoom */
44 50
#define MAX_SCALE       10.0                 /** @brief Maximum zoom */
45 51

  
52
// Colors
53
#define TEXT_COLOR              0x888888
54
#define HIGHLIGHT_COLOR         0xA0A0A0
55
#define DELIMITER_COLOR         0x404040
56

  
46 57
#endif /* end of include guard: PROJ_MACROS_H_INCLUDED */
proj/include/rectangle.h
17 17
int16_t  (rectangle_get_y)(const rectangle_t *p);
18 18
uint16_t (rectangle_get_w)(const rectangle_t *p);
19 19
uint16_t (rectangle_get_h)(const rectangle_t *p);
20
int      (rectangle_collide_point)(const rectangle_t *p, int x, int y);
20 21

  
21 22
void (rectangle_draw)(const rectangle_t *p);
22 23

  
proj/src/proj.c
7 7
#include "proj_func.h"
8 8

  
9 9
#include "kbc.h"
10
#include "kbc_macros.h"
11
#include "i8254.h"
10 12
#include "timer.h"
11 13
#include "keyboard.h"
12 14
#include "mouse.h"
......
173 175

  
174 176
        text_timer_t *in_game_timer = timer_ctor(consolas);
175 177

  
178
        menu_t *main_menu = menu_ctor(consolas);
179

  
176 180
        list_t *shooter_list = list_ctor();
177 181

  
178 182
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
......
201 205
        /// loop stuff
202 206
        int ipc_status;
203 207
        message msg;
204
        int good = 1;
208
        int game_state = MENU, click = 0;
205 209

  
206
        while (good) {
210
        while (game_state != EXIT) {
207 211
            /* Get a request message. */
208 212
            if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
209 213
                printf("driver_receive failed with %d", r);
......
215 219
                        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
216 220
                            if (msg.m_notify.interrupts & n) {
217 221
                                interrupt_handler(i);
218
                                if ((scancode[0]) == ESC_BREAK_CODE) good = 0;
219
                                if (i == 0) {
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);
222
                                switch (i) {
223
                                case TIMER0_IRQ:
224
                                    switch (game_state) {
225
                                    case MENU:
226
                                        if (no_interrupts % 2 == 0){
227
                                            graph_clear_screen();
228
                                            game_state = menu_update_state(main_menu, click);
229
                                            menu_draw(main_menu);
223 230

  
224
                                            update_game_state(map1, shooter_list, bullet_list);
231
                                            click = 0;
225 232

  
226
                                            //update_scale();
227 233
                                            sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
228
                                            angle = get_mouse_angle(shooter1);
229
                                            gunner_set_angle(shooter1, angle - M_PI_2);
230
                                            graph_clear_screen();
234
                                            sprite_draw(sp_crosshair);
235
                                            graph_draw();
236
                                        }
237
                                        break;
238
                                    case GAME:
239
                                        if (no_interrupts % 60 == 0) timer_update(in_game_timer);
240
                                        update_movement(map1, shooter1, keys, shooter_list);
231 241

  
232
                                            ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
233
                                                           gunner_get_y(shooter1)-ent_get_YLength()/2.0);
242
                                        update_game_state(map1, shooter_list, bullet_list);
234 243

  
235
                                            map_draw   (map1);
236
                                            gunner_draw_list(shooter_list);
237
                                            bullet_draw_list(bullet_list);
244
                                        //update_scale();
245
                                        angle = get_mouse_angle(shooter1);
246
                                        gunner_set_angle(shooter1, angle - M_PI_2);
238 247

  
239
                                            text_draw(in_game_timer->text);
240
                                            sprite_draw(sp_crosshair);
241
                                            graph_draw();
242
                                        //}
248
                                        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
249
                                                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
250

  
251
                                        graph_clear_screen();
252
                                        map_draw   (map1);
253
                                        gunner_draw_list(shooter_list);
254
                                        bullet_draw_list(bullet_list);
255

  
256
                                        text_draw(in_game_timer->text);
257

  
258
                                        sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
259
                                        sprite_draw(sp_crosshair);
260
                                        graph_draw();
261
                                        break;
243 262
                                    }
244
                                    else if (i == 12) {
245
                                        if (counter_mouse_ih >= 3) {
246
                                            mouse_parse_packet(packet_mouse_ih, &pp);
247
                                            update_mouse(&pp);
248

  
249
                                            if (last_lb ^ keys->lb_pressed && keys->lb_pressed) {
250
                                                shoot_bullet(shooter1, bullet_list, bsp_bullet);
263
                                    break;
264
                                case KBC_IRQ:
265
                                    switch (game_state) {
266
                                    case MENU:
267
                                        if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
268
                                        break;
269
                                    case GAME:
270
                                        if ((scancode[0]) == ESC_BREAK_CODE) {
271
                                            game_state = MENU;
272
                                            // reset game
273
                                            while(list_size(bullet_list) > 0){
274
                                                bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
275
                                                bullet_dtor(p);
251 276
                                            }
252
                                            last_lb = keys->lb_pressed;
253
                                            counter_mouse_ih = 0;
254

  
277
                                            list_node_t *it = list_begin(shooter_list);
278
                                            while (it != list_end(shooter_list)) {
279
                                                gunner_t *p = *(gunner_t**)list_node_val(it);
280
                                                get_random_spawn(map1, p);
281
                                                gunner_set_curr_health(p, gunner_get_health(p));
282
                                                it = list_node_next(it);
283
                                            }
284
                                            timer_reset(in_game_timer);
255 285
                                        }
286
                                        break;
256 287
                                    }
288
                                    break;
289
                                case MOUSE_IRQ:
290
                                    if (counter_mouse_ih >= 3) {
291
                                        mouse_parse_packet(packet_mouse_ih, &pp);
292
                                        update_mouse(&pp);
293
                                        switch (game_state) {
294
                                        case MENU:
295
                                            if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
296
                                            break;
297
                                        case GAME:
298
                                            if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
299
                                                shoot_bullet(shooter1, bullet_list, bsp_bullet);
300
                                            break;
301
                                        }
302
                                        last_lb = keys->lb_pressed;
303
                                        counter_mouse_ih = 0;
304

  
305
                                    }
306
                                    break;
257 307
                                }
258 308
                            }
309
                        }
259 310

  
260
                            break;
261
                        default:
262
                            break; /* no other notifications expected: do nothing */
263
                    }
311
                        break;
312
                    default:
313
                        break; /* no other notifications expected: do nothing */
314
                }
264 315
            } else { /* received standart message, not a notification */
265 316
                /* no standart message expected: do nothing */
266 317
            }
proj/src/proj_func.c
10 10
#include "utils.h"
11 11
#include "ent.h"
12 12
#include "fast_math.h"
13
#include "rectangle.h"
14
#include "font.h"
13 15

  
14 16
#include "kbc_macros.h"
15 17

  
......
189 191
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
190 192
}
191 193

  
194
struct menu {
195
    rectangle_t *r0, *r1, *r2;
196
    text_t      *t0, *t1, *t2;
197
    rectangle_t *frame;
198
};
199

  
200
menu_t* (menu_ctor)(const font_t *fnt){
201
    if(fnt == NULL) return NULL;
202
    menu_t *ret = (menu_t*)malloc(sizeof(menu_t));
203
    if (ret == NULL) return NULL;
204
    ret->r0 = rectangle_ctor(0, 0, 400, 100);
205
    ret->r1 = rectangle_ctor(0, 0, 400, 100);
206
    ret->r2 = rectangle_ctor(0, 0, 400, 100);
207
    ret->t0 = text_ctor(fnt, "PLAY");
208
    ret->t1 = text_ctor(fnt, "TEST");
209
    ret->t2 = text_ctor(fnt, "EXIT");
210
    ret->frame = rectangle_ctor(0, 0, 800, 500);
211
    if (ret->r0 == NULL || ret->r1 == NULL || ret->r2 == NULL ||
212
        ret->t0 == NULL || ret->t1 == NULL || ret->t2 == NULL ||
213
        ret->frame == NULL) return NULL;
214
    // VISUAL
215
    rectangle_set_fill_color(ret->r0, BLACK);
216
    rectangle_set_outline_width(ret->r0, 2);
217
    rectangle_set_outline_color(ret->r0, WHITE);
218
    rectangle_set_fill_color(ret->r1, BLACK);
219
    rectangle_set_outline_width(ret->r1, 2);
220
    rectangle_set_outline_color(ret->r1, WHITE);
221
    rectangle_set_fill_color(ret->r2, BLACK);
222
    rectangle_set_outline_width(ret->r2, 2);
223
    rectangle_set_outline_color(ret->r2, WHITE);
224
    text_set_valign(ret->t0, text_valign_center);
225
    text_set_halign(ret->t0, text_halign_center);
226
    text_set_color(ret->t0, TEXT_COLOR);
227
    text_set_valign(ret->t1, text_valign_center);
228
    text_set_halign(ret->t1, text_halign_center);
229
    text_set_color(ret->t1, TEXT_COLOR);
230
    text_set_valign(ret->t2, text_valign_center);
231
    text_set_halign(ret->t2, text_halign_center);
232
    text_set_color(ret->t2, TEXT_COLOR);
233
    rectangle_set_fill_color(ret->frame, BLACK);
234
    rectangle_set_outline_width(ret->frame, 6);
235
    rectangle_set_outline_color(ret->frame, WHITE);
236
    // POSITIONS
237
    rectangle_set_pos(ret->r0,
238
                      graph_get_XRes()/2    - rectangle_get_w(ret->r0)/2,
239
                      graph_get_YRes()*0.35 - rectangle_get_h(ret->r0)/2);
240

  
241

  
242
    rectangle_set_pos(ret->r1,
243
                    graph_get_XRes()/2    - rectangle_get_w(ret->r1)/2,
244
                    graph_get_YRes()*0.5 - rectangle_get_h(ret->r1)/2);
245

  
246

  
247
    rectangle_set_pos(ret->r2,
248
                  graph_get_XRes()/2    - rectangle_get_w(ret->r2)/2,
249
                  graph_get_YRes()*0.65 - rectangle_get_h(ret->r2)/2);
250

  
251
    text_set_pos(ret->t0, rectangle_get_x(ret->r0)+rectangle_get_w(ret->r0)/2,
252
                          rectangle_get_y(ret->r0)+rectangle_get_h(ret->r0)/2);
253

  
254
    text_set_pos(ret->t1, rectangle_get_x(ret->r1)+rectangle_get_w(ret->r1)/2,
255
                          rectangle_get_y(ret->r1)+rectangle_get_h(ret->r1)/2);
256

  
257
    text_set_pos(ret->t2, rectangle_get_x(ret->r2)+rectangle_get_w(ret->r2)/2,
258
                          rectangle_get_y(ret->r2)+rectangle_get_h(ret->r2)/2);
259

  
260
    rectangle_set_pos(ret->frame,
261
                  graph_get_XRes()/2    - rectangle_get_w(ret->frame)/2,
262
                  graph_get_YRes()*0.5 - rectangle_get_h(ret->frame)/2);
263
    return ret;
264
}
265

  
266
static int highlighted = -1;
267

  
268
int (menu_update_state)(menu_t *menu, int click) {
269

  
270
    if (rectangle_collide_point(menu->r0, mouse_x, mouse_y)) {
271
        highlighted = GAME;
272
        if (click) return GAME;
273
    } else if (rectangle_collide_point(menu->r1, mouse_x, mouse_y)) {
274
        highlighted = TEST;
275
        if (click) return TEST;
276
    } else if (rectangle_collide_point(menu->r2, mouse_x, mouse_y)) {
277
        highlighted = EXIT;
278
        if (click) return EXIT;
279
    } else {
280
        highlighted = -1;
281
    }
282
    return MENU;
283
}
284

  
285
void (menu_draw)(menu_t *menu) {
286
    rectangle_draw(menu->frame);
287
    switch (highlighted) {
288
    case GAME:
289
        rectangle_set_fill_color(menu->r0, HIGHLIGHT_COLOR);
290
        rectangle_draw(menu->r0);
291
        rectangle_draw(menu->r1);
292
        rectangle_draw(menu->r2);
293
        rectangle_set_fill_color(menu->r0, BLACK);
294
        break;
295
    case TEST:
296
        rectangle_set_fill_color(menu->r1, HIGHLIGHT_COLOR);
297
        rectangle_draw(menu->r0);
298
        rectangle_draw(menu->r1);
299
        rectangle_draw(menu->r2);
300
        rectangle_set_fill_color(menu->r1, BLACK);
301
        break;
302
    case EXIT:
303
        rectangle_set_fill_color(menu->r2, HIGHLIGHT_COLOR);
304
        rectangle_draw(menu->r0);
305
        rectangle_draw(menu->r1);
306
        rectangle_draw(menu->r2);
307
        rectangle_set_fill_color(menu->r2, BLACK);
308
        break;
309
    default:
310
        rectangle_draw(menu->r0);
311
        rectangle_draw(menu->r1);
312
        rectangle_draw(menu->r2);
313
        break;
314
    }
315
    text_draw(menu->t0);
316
    text_draw(menu->t1);
317
    text_draw(menu->t2);
318
}
319

  
320
void (menu_dtor)(menu_t *p){
321
    rectangle_dtor(p->r0);
322
    rectangle_dtor(p->r1);
323
    rectangle_dtor(p->r2);
324
    rectangle_dtor(p->frame);
325
    text_dtor(p->t0);
326
    text_dtor(p->t1);
327
    text_dtor(p->t2);
328
    free(p);
329
}
330

  
192 331
text_timer_t* (timer_ctor)(const font_t *fnt){
193 332
    if(fnt == NULL) return NULL;
194 333
    text_timer_t *ret = malloc(sizeof(timer_t));
......
196 335
    ret->time = 0;
197 336
    ret->text = text_ctor(fnt, "000s");
198 337
    ret->array = text_get_string(ret->text);
199
    text_set_color(ret->text, 0x888888);
338
    text_set_color(ret->text, TEXT_COLOR);
200 339
    return ret;
201 340
}
202 341

  
......
208 347
    p->array[0] = (p->time/100) % 10 + '0';
209 348
}
210 349

  
350
void (timer_reset)(text_timer_t *p){
351
    p->time = 0;
352
    p->array[2] = '0';
353
    p->array[1] = '0';
354
    p->array[0] = '0';
355
}
356

  
211 357
void (timer_dtor)(text_timer_t *p){
212 358
    if (p == NULL) return;
213 359
    text_dtor(p->text);
proj/src/rectangle.c
41 41
uint16_t (rectangle_get_w)(const rectangle_t *p){ return p->w; }
42 42
uint16_t (rectangle_get_h)(const rectangle_t *p){ return p->h; }
43 43

  
44
int (rectangle_collide_point)(const rectangle_t *p, int x, int y) {
45
    int16_t x0 = p->x, y0 = p->y;
46
    return (x >= x0 && x <= x0 + p->w) && (y >= y0 && y <= y0 + p->h);
47
}
48

  
44 49
static void (rectangle_draw_hline)(int16_t x, int16_t y, int16_t l, uint32_t color){
45 50
    if(l < 0){ rectangle_draw_hline(x+l, y, -l, color); return; }
46 51
    for(int16_t x_ = max(0,x); x_ < min(x+l,graph_get_XRes()); ++x_){

Also available in: Unified diff