forked from Namaneo/Junie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
152 lines (116 loc) · 3.24 KB
/
main.c
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
144
145
146
147
148
149
150
151
152
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "enums.h"
#include "filesystem.h"
#include "app.h"
JUN_App *app;
static bool environment(unsigned cmd, void *data)
{
return JUN_AppEnvironment(app, cmd, data);
}
static void video_refresh(const void *data, unsigned width, unsigned height, size_t pitch)
{
JUN_VideoUpdateContext(app->video, width, height, pitch);
JUN_VideoDrawFrame(app->video, data);
JUN_VideoDrawUI(app->video, JUN_StateHasGamepad(app->state));
}
static void input_poll()
{
}
static int16_t input_state(unsigned port, unsigned device, unsigned index, unsigned id)
{
if (port != 0)
return 0;
return JUN_InputGetStatus(app->input, device, id);
}
static size_t audio_sample_batch(const int16_t *data, size_t frames)
{
if (JUN_StateHasAudio(app->state))
JUN_AudioQueue(app->audio, data, frames);
return frames;
}
static void audio_sample(int16_t left, int16_t right)
{
int16_t buf[2] = {left, right};
audio_sample_batch(buf, 1);
}
static bool start_game()
{
JUN_CoreSetCallbacks(app->core, &(JUN_CoreCallbacks)
{
environment,
video_refresh,
audio_sample,
audio_sample_batch,
input_poll,
input_state,
});
if (!JUN_CoreStartGame(app->core))
return false;
double sample_rate = JUN_CoreGetSampleRate(app->core);
double frames_per_second = JUN_CoreGetFramesPerSecond(app->core);
JUN_AudioPrepare(app->audio, sample_rate, frames_per_second);
JUN_CoreRestoreMemories(app->core);
return true;
}
static bool app_func(void *opaque)
{
if (!JUN_CoreHasStarted(app->core))
{
JUN_VideoDrawLoadingScreen(app->video);
if (JUN_FilesystemReady() && !start_game())
return false;
}
else
{
for (int i = 0; i < JUN_StateGetFastForward(app->state); ++i)
{
JUN_CoreRun(app->core);
}
JUN_CoreRun(app->core);
JUN_CoreSaveMemories(app->core);
if (JUN_StateShouldSaveState(app->state))
{
JUN_CoreSaveState(app->core);
JUN_StateToggleSaveState(app->state);
}
if (JUN_StateShouldRestoreState(app->state))
{
JUN_CoreRestoreState(app->core);
JUN_StateToggleRestoreState(app->state);
}
}
JUN_VideoPresent(app->video);
return !app->quit;
}
static void event_func(const MTY_Event *event, void *opaque)
{
JUN_InputSetStatus(app->input, event);
if (event->type == MTY_EVENT_CLOSE)
app->quit = true;
}
static void log_func(const char *message, void *opaque)
{
if (message[strlen(message) - 1] != '\n')
printf("%s\n", message);
else
printf("%s", message);
}
static void configure(JUN_File *file, void *opaque)
{
JUN_AppConfigure((JUN_App *)opaque, (char *)file->buffer);
}
__attribute__((export_name("_main"))) int main(int argc, char *argv[])
{
MTY_SetLogFunc(log_func, NULL);
JUN_EnumsInitialize();
JUN_FilesystemInitialize();
app = JUN_AppInitialize(app_func, event_func);
JUN_FilesystemDownload("/settings.json", configure, app);
JUN_VideoStart(app->video);
JUN_AppDestroy(&app);
JUN_FilesystemDestroy();
JUN_EnumsDestroy();
return 0;
}