aboutsummaryrefslogtreecommitdiff
path: root/src/main.c
blob: 1ee9eaec2b054df3dd941fdbdfc060b8de39122f (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
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <crt0.h>

#include "timer.h"
#include "keyb.h"
#include "sound.h"
#include "control.h"
#include "vga.h"
#include "data.h"
#include "menu.h"
#include "game.h"

#define GAME_NAME "Gold Mine Run!"
#define GAME_VERSION "1.6 (2023-08-19)"
#define GAME_URL "https://www.usebox.net/jjm/gold-mine-run/"

/* disable paging because our int handlers are written in C */
int _crt0_startup_flags = _CRT0_FLAG_LOCK_MEMORY;

static void run_help(const char *argv0)
{
    printf("usage: %s [OPTION]\n\n"
           "Valid options:\n\n"
           "  -h, /?  this help screen\n"
           "  -v      print version and exit\n"
           "  -ns     run the game with no sound\n\n"
           "More info and updates:\n\n"
           "  " GAME_URL "\n\n",
           argv0);
}

static void free_all()
{
    timer_free();
    keyb_free();
    sound_free();
}

int main(int argc, char *argv[])
{
    uint8_t nosound = 0;

    if (argc > 1)
    {
        for (uint8_t i = 1; i < argc; i++)
            if (!strcmp(argv[i], "-h")
                    || !strcmp(argv[i], "/?"))
            {
                run_help(argv[0]);
                return 0;
            }
            else if (!strcmp(argv[i], "-v"))
            {
                printf(GAME_NAME " version " GAME_VERSION "\n");
                return 0;
            }
            else if (!strcmp(argv[i], "-ns"))
                nosound = 1;
            else
            {
                fprintf(stderr, "ERROR: invalid option, try -h\n");
                return 1;
            }
    }

    if (!nosound && !sound_init())
    {
        fprintf(stderr, "ERROR: failed to init sound\n");
        return 1;
    }

    timer_init();
    keyb_init();
    atexit(free_all);

    /* requires keyboard */
    control_init();

    /* to update mikmod */
    if (!nosound)
        timer_user_fn(sound_update);

    /* set VGA 320x200, 256 col */
    if (!set_mode(0x13))
    {
        fprintf(stderr, "ERROR: failed to init the VGA card\n");
        return 1;
    }

    set_palette(binary_palette_start);

    if (!open_framebuffer())
    {
        set_mode(3);
        fprintf(stderr, "ERROR: failed to open the framebuffer\n");
        return 1;
    }

    while (run_menu())
        run_game();

    blit_target(TARGET_SCREEN);
    wait_vsync();
    blit_erase(0);

    set_mode(3);
    close_framebuffer();

    return 0;
}