aboutsummaryrefslogtreecommitdiff
path: root/src/game.c
blob: b49e859831bc6cd81f94086a00d780b213a70b15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdint.h>
#include <stdio.h>

#include "keyb.h"
#include "vga.h"
#include "text.h"
#include "map.h"
#include "data.h"

#include "game.h"

static uint32_t hiscore = 15000;

static uint8_t hud;
static volatile uint8_t clock_updated;

/* game variables */
static uint8_t lives;
static uint32_t score;
static uint8_t stage;
static uint8_t gold;
static uint8_t time;

void hud_render()
{
    char b[32];

    if (hud & HUD_ALL)
    {
        Rect src = { 128, 32, 144, 144};
        Rect dst = { 0, 0, 16, 16};

        /* lives */
        blitrc(binary_sprites_start, &src, &dst);

        put_text(132, 4, "TIME");
        put_text(256, 4, "STAGE");
    }

    if (hud & HUD_LIVES)
    {
        sprintf(b, "%d", lives);
        put_text(14, 4, b);
    }

    if (hud & HUD_SCORE)
    {
        sprintf(b, "%06li", score);
        put_text(30, 4, b);
    }

    if (hud & HUD_TIME)
    {
        sprintf(b, "%02d", time);
        put_text(172, 4, b);
    }

    if (hud & HUD_STAGE)
    {
        sprintf(b, "%02d", stage);
        put_text(304, 4, b);
    }

    hud = HUD_CLEAN;
}

void run_game()
{
    lives = GAME_LIVES_START;
    score = 0;
    stage = 0;
    gold = 30;
    time = GAME_TIME_MAX;

    hud = HUD_ALL;

    map_init(binary_stage_start);
    map_render();

    timer_start(GAME_TIME_MAX, &clock_updated);

    while (!keys[KEY_ESC])
    {
        if (clock_updated)
        {
            time = timer_value();
            hud |= HUD_TIME;
        }

        if (hud)
            hud_render();

        wait_vsync();
        blit_update();
    }
}