summaryrefslogtreecommitdiff
path: root/game/src/main.c
blob: 310cbe1cb5ee62eb845fc5cd1862146bbfbd7f88 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdint.h>

#include "ubox.h"
#include "mplayer.h"

#include "helpers.h"
#include "game.h"

#define LOCAL
#include "main.h"

// generated
#include "tiles.h"

void draw_menu()
{
    uint8_t i;

    ubox_disable_screen();

    ubox_fill_screen(WHITESPACE_TILE);

    // game logo; starts in tile 32 and it is 10 x 3 tiles
    for (i = 0; i < 10; ++i)
    {
        ubox_put_tile(11 + i, 6, 32 + i);
        ubox_put_tile(11 + i, 7, 64 + i);
        ubox_put_tile(11 + i, 8, 96 + i);
    }

    put_text(11, 11, "PRESS FIRE");

    put_text(7, 2, "UBOX MSX LIB DEMO!");
    put_text(4, 16, "CODE, GRAPHICS AND SOUND");
    put_text(8, 17, "JUAN J. MARTINEZ");
    // 037 is ASCII 31 in octal, our Copyright sign
    put_text(8, 21, "\0372020 USEBOX.NET");

    ubox_enable_screen();
}

void draw_end_game()
{
    ubox_disable_screen();

    ubox_fill_screen(WHITESPACE_TILE);

    put_text(3, 9, "WELL DONE!");
    put_text(3, 10, "YOU HAVE FINISHED THE DEMO.");
    put_text(3, 12, "NOW GO AND MAKE GAMES :)");

    put_text(3, 14, "(PRESS ESC)");

    ubox_enable_screen();

    // wait until ESC is pressed
    while (1)
    {
        if (ubox_read_keys(7) == UBOX_MSX_KEY_ESC)
            break;

        ubox_wait();
    }
}

void draw_game_over()
{
    ubox_disable_screen();

    ubox_fill_screen(WHITESPACE_TILE);
    put_text(11, 10, "GAME  OVER");

    ubox_enable_screen();

    // play game over music
    mplayer_init(SONG, SONG_GAME_OVER);

    ubox_wait_for(128);
}

void main()
{
    //  PAL: 50/2 = 25 FPS
    // NTSC: 60/2 = 30 FPS
    ubox_init_isr(2);

    // set screen 2
    ubox_set_mode(2);
    // all black
    ubox_set_colors(1, 1, 1);

    // avoid showing garbage on the screen
    // while setting up the tiles
    ubox_disable_screen();

    // upload our tileset
    ubox_set_tiles(tiles);
    // and the colour information
    ubox_set_tiles_colors(tiles_colors);

    // clear the screen
    ubox_fill_screen(WHITESPACE_TILE);

    ubox_enable_screen();

    // reg 1: activate sprites, v-blank int on, 16x16 sprites
    ubox_wvdp(1, 0xe2);

    // init the player
    mplayer_init(SONG, SONG_SILENCE);
    mplayer_init_effects(EFFECTS);

    // we don't need anything in our ISR
    // other than the play function, so we
    // use that!
    ubox_set_user_isr(mplayer_play);

redraw_menu:
    draw_menu();

    // wait until fire is pressed
    // can be space (control will be cursors), or any fire button on a joystick
    while (1)
    {
        ctl = ubox_select_ctl();
        if (ctl != UBOX_MSX_CTL_NONE)
        {
            mplayer_play_effect_p(EFX_START, EFX_CHAN_NO, 0);
            ubox_wait_for(16);

            // play the game
            run_game();

            if (!lives)
                draw_game_over();
            else if (!batteries)
                draw_end_game();

            goto redraw_menu;
        }
        ubox_wait();
    }
}