-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.c
More file actions
161 lines (150 loc) · 4.84 KB
/
Copy pathengine.c
File metadata and controls
161 lines (150 loc) · 4.84 KB
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
153
154
155
156
157
158
159
160
161
/* SPDX-License-Identifier: AGPL-3.0-or-later
* Copyright (C) 2026 Cycl0o0
*
* Preferred minimal host using liboot's versioned engine-neutral API.
*/
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "liboot_engine.h"
static int read_file(const char *path, uint8_t **outData, size_t *outSize)
{
FILE *file = fopen(path, "rb");
long length;
uint8_t *data;
if (file == NULL) {
fprintf(stderr, "%s: %s\n", path, strerror(errno));
return 0;
}
if (fseek(file, 0, SEEK_END) != 0) {
fprintf(stderr, "%s: could not determine ROM size\n", path);
fclose(file);
return 0;
}
length = ftell(file);
if (length < (long)OOT_ENGINE_MIN_ROM_SIZE ||
(uintmax_t)length > (uintmax_t)OOT_ENGINE_MAX_ROM_SIZE) {
fprintf(stderr, "%s: ROM size must be %u..%u bytes\n", path,
(unsigned)OOT_ENGINE_MIN_ROM_SIZE,
(unsigned)OOT_ENGINE_MAX_ROM_SIZE);
fclose(file);
return 0;
}
if (fseek(file, 0, SEEK_SET) != 0) {
fprintf(stderr, "%s: could not rewind ROM\n", path);
fclose(file);
return 0;
}
data = (uint8_t *)malloc((size_t)length);
if (data == NULL) {
fprintf(stderr, "%s: could not allocate %ld bytes\n", path, length);
fclose(file);
return 0;
}
if (fread(data, 1, (size_t)length, file) != (size_t)length) {
fprintf(stderr, "%s: could not read ROM\n", path);
free(data);
fclose(file);
return 0;
}
if (fgetc(file) != EOF) {
fprintf(stderr, "%s: ROM size changed while it was being read\n", path);
free(data);
fclose(file);
return 0;
}
if (ferror(file)) {
fprintf(stderr, "%s: could not finish reading ROM\n", path);
free(data);
fclose(file);
return 0;
}
fclose(file);
*outData = data;
*outSize = (size_t)length;
return 1;
}
static int require_ok(OoTResult result, const char *operation)
{
if (result == OOT_ENGINE_RESULT_OK) {
return 1;
}
fprintf(stderr, "%s: %s\n", operation, oot_engine_result_string(result));
return 0;
}
int main(int argc, char **argv)
{
static const struct OoTSurface floor[] = {
{ 0, {{ -1000, 0, -1000 }, { -1000, 0, 1000 }, { 1000, 0, 1000 }} },
{ 0, {{ -1000, 0, -1000 }, { 1000, 0, 1000 }, { 1000, 0, -1000 }} },
};
uint8_t *rom = NULL;
size_t romSize = 0u;
OoTEngineConfig config;
OoTEngineInput input;
OoTEngine *engine = NULL;
const OoTEngineFrame *frame = NULL;
int tick;
int exitCode = 1;
if (argc != 2) {
fprintf(stderr, "usage: %s <legally-obtained-oot-rom>\n", argv[0]);
return 2;
}
if (!read_file(argv[1], &rom, &romSize)) {
return 1;
}
if (oot_engine_api_version() != OOT_ENGINE_API_VERSION) {
fprintf(stderr, "liboot engine API version mismatch\n");
free(rom);
return 1;
}
if (!require_ok(oot_engine_config_init(&config), "config init")) {
free(rom);
return 1;
}
config.romData = rom;
config.romSize = romSize;
config.renderFlags = OOT_ENGINE_RENDER_NAVI | OOT_ENGINE_RENDER_ACTORS;
if (!require_ok(oot_engine_create(&config, &engine), "engine create")) {
free(rom);
return 1;
}
free(rom); /* creation copied the ROM synchronously */
if (!require_ok(oot_engine_static_world_load(
engine, floor, 2u, NULL, 0u), "world load") ||
!require_ok(oot_engine_link_create(engine, 0.0f, 0.0f, 0.0f),
"Link create") ||
!require_ok(oot_engine_link_set_equipment(
engine, OOT_SWORD_MASTER, OOT_SHIELD_HYLIAN,
OOT_TUNIC_KOKIRI, OOT_BOOTS_KOKIRI), "equipment")) {
goto done;
}
if (!require_ok(oot_engine_input_init(&input), "input init")) {
goto done;
}
for (tick = 0; tick < 20; ++tick) {
input.stickY = tick < 12 ? 1.0f : 0.0f;
input.buttons = tick == 14 ? OOT_ENGINE_BUTTON_A : 0u;
if (!require_ok(oot_engine_step(engine, &input, &frame), "simulation step")) {
goto done;
}
}
printf("Link position: %.2f %.2f %.2f\n", frame->link.position[0],
frame->link.position[1], frame->link.position[2]);
printf("Frame: %u triangles, %u actors, tick %llu\n",
frame->geometry.numTriangles, frame->actorCount,
(unsigned long long)frame->simulationTick);
exitCode = 0;
done:
if (engine != NULL) {
OoTResult destroyResult = oot_engine_destroy(engine);
if (destroyResult != OOT_ENGINE_RESULT_OK) {
fprintf(stderr, "engine destroy: %s\n",
oot_engine_result_string(destroyResult));
exitCode = 1;
}
}
return exitCode;
}