-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.c
297 lines (261 loc) · 6.76 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*****************************************************************************/
/*
* PiSHi LE (Lite edition) - Fundamentals of the King's Crook graphics engine.
*
* by EMMIR 2018-2022
*
* YouTube: https://www.youtube.com/c/LMP88
*
* This software is released into the public domain.
*/
/*****************************************************************************/
#include "pl.h"
/* main.c
*
* Basic demo showing how to define a 3D scene, generate geometry,
* import geometry, implement first person camera controls, and transform
* the geometry.
*
* Controls:
* Arrow keys - looking
* W/A/S/D keys - movement
* T/G keys - move up and down
* C - cycle through culling modes
* 1 - flat rendering
* 2 - textured rendering
* 3 - toggle between two FOVs
* SPACE - start/stop dynamic transformation
*
*/
#include "fw/fw.h"
#include <stdlib.h>
#include <stdio.h>
extern void *
EXT_calloc(unsigned n, unsigned esz)
{
return calloc(n, esz);
}
extern void
EXT_free(void *p)
{
free(p);
}
extern void
EXT_error(int err_id, char *modname, char *msg)
{
printf("vx error 0x%x in %s: %s\n", err_id, modname, msg);
sys_kill();
getchar();
exit(0);
}
#define VW 896
#define VH 504
/* cube size */
#define CUSZ 128
/* grid size */
#define GRSZ 1
/* movement speed */
#define MOVSPD 4
static struct PL_OBJ *floortile;
static struct PL_OBJ *texcube;
static struct PL_OBJ *imported;
static int camrx = 0, camry = 0;
static int x = 0, y = 200, z = 90;
static int rot = 1;
static int sinvar = 0;
static struct PL_TEX checktex;
static int checker[PL_REQ_TEX_DIM * PL_REQ_TEX_DIM];
static unsigned fpsclock = 0;
static void
maketex(void)
{
int i, j, c;
for (i = 0; i < PL_REQ_TEX_DIM; i++) {
for (j = 0; j < PL_REQ_TEX_DIM; j++) {
if (i < 0x10 || j < 0x10 ||
(i > ((PL_REQ_TEX_DIM - 1) - 0x10)) ||
(j > ((PL_REQ_TEX_DIM - 1) - 0x10))) {
/* border color */
c = 0x3f4f5f;
} else {
/* checkered pattern */
if (((((i & 0x10)) ^ ((j & 0x10))))) {
c = 0x3f4f5f;
} else {
c = 0xd4ccba;
}
}
if (i == j || abs(i - j) < 3) {
/* thick red line along diagonal */
checker[i + j * PL_REQ_TEX_DIM] = 0x902215;
} else {
checker[i + j * PL_REQ_TEX_DIM] = c;
}
}
}
checktex.data = checker;
}
static void
init(void)
{
maketex();
PL_texture(&checktex);
texcube = PL_gen_box(CUSZ, CUSZ, CUSZ, PL_ALL, 255, 255, 255);
PL_texture(NULL);
floortile = PL_gen_box(CUSZ, CUSZ, CUSZ, PL_TOP, 77, 101, 94);
import_dmdl("pots", &imported);
PL_fov = 9;
PL_cur_tex = NULL;
PL_cull_mode = PL_CULL_BACK;
PL_raster_mode = PL_TEXTURED;
fpsclock = clk_sample();
}
static void
update(void)
{
if (pkb_key_pressed(FW_KEY_ESCAPE)) {
sys_shutdown();
}
if (pkb_key_held(FW_KEY_ARROW_RIGHT)) {
camry += 1;
}
if (pkb_key_held(FW_KEY_ARROW_LEFT)) {
camry -= 1;
}
if (pkb_key_held(FW_KEY_ARROW_UP)) {
camrx -= 1;
}
if (pkb_key_held(FW_KEY_ARROW_DOWN)) {
camrx += 1;
}
if (pkb_key_held('w')) {
x += (MOVSPD * PL_sin[camry & PL_TRIGMSK]) >> PL_P;
y -= (MOVSPD * PL_sin[camrx & PL_TRIGMSK]) >> PL_P;
z += (MOVSPD * PL_cos[camry & PL_TRIGMSK]) >> PL_P;
}
if (pkb_key_held('s')) {
x -= (MOVSPD * PL_sin[camry & PL_TRIGMSK]) >> PL_P;
y += (MOVSPD * PL_sin[camrx & PL_TRIGMSK]) >> PL_P;
z -= (MOVSPD * PL_cos[camry & PL_TRIGMSK]) >> PL_P;
}
if (pkb_key_held('a')) {
x -= (MOVSPD * PL_cos[camry & PL_TRIGMSK]) >> PL_P;
z += (MOVSPD * PL_sin[camry & PL_TRIGMSK]) >> PL_P;
}
if (pkb_key_held('d')) {
x += (MOVSPD * PL_cos[camry & PL_TRIGMSK]) >> PL_P;
z -= (MOVSPD * PL_sin[camry & PL_TRIGMSK]) >> PL_P;
}
if (pkb_key_held('t')) {
y += MOVSPD;
}
if (pkb_key_held('g')) {
y -= MOVSPD;
}
if (pkb_key_pressed('c')) {
static int cmod = PL_CULL_BACK;
if (cmod == PL_CULL_BACK) {
cmod = PL_CULL_NONE;
} else if (cmod == PL_CULL_FRONT) {
cmod = PL_CULL_BACK;
} else {
cmod = PL_CULL_FRONT;
}
PL_cull_mode = cmod;
}
if (pkb_key_held('1')) {
PL_raster_mode = PL_FLAT;
}
if (pkb_key_held('2')) {
PL_raster_mode = PL_TEXTURED;
}
if (pkb_key_pressed('3')) {
if (PL_fov == 8) {
PL_fov = 9;
} else {
PL_fov = 8;
}
printf("fov: %d\n", PL_fov);
}
if (pkb_key_pressed(' ')) {
rot = !rot;
}
sinvar++;
}
static void
display(void)
{
int i = 0, j = 0;
int p1 = PL_P_ONE;
int mo;
/* clear viewport to black */
PL_clear_vp(0, 0, 0);
PL_polygon_count = 0;
/* define camera orientation */
PL_set_camera(x, y, z, camrx, camry);
{ /* draw imported model */
PL_mst_push();
if (rot) {
mo = (PL_sin[sinvar & PL_TRIGMSK] * 256) >> PL_P;
PL_mst_translate(mo, 400, 500);
} else {
PL_mst_translate(0, 400, 500);
}
PL_render_object(imported);
PL_mst_pop();
}
/* draw tile grid */
for (i = -GRSZ; i < GRSZ; i++) {
for (j = -GRSZ; j < GRSZ; j++) {
PL_mst_push();
PL_mst_translate(
0 + i * CUSZ,
0,
600 + j * CUSZ);
PL_render_object(floortile);
PL_mst_pop();
}
}
{ /* draw textured cube */
PL_mst_push();
PL_mst_translate(-100, 100, 500);
if (rot) {
PL_mst_rotatex(sinvar >> 2);
PL_mst_rotatey(sinvar >> 1);
PL_mst_scale(p1 * ((sinvar & 0xff) + 128) >> 8, p1, p1);
}
PL_render_object(texcube);
PL_mst_pop();
}
if (clk_sample() > fpsclock) {
fpsclock = clk_sample() + 1000;
printf("FPS: %d\n", sys_getfps());
}
/* update window and sync */
vid_blit();
vid_sync();
}
int
main(int argc, char **argv)
{
if (argc != 1) {
printf("note: %s does not take any arguments.\n", argv[0]);
}
sys_init();
sys_updatefunc(update);
sys_displayfunc(display);
clk_mode(FW_CLK_MODE_HIRES);
pkb_reset();
sys_sethz(70);
sys_capfps(0);
if (vid_open("PL", VW, VH, 1, FW_VFLAG_VIDFAST) != FW_VERR_OK) {
FW_error("unable to create window\n");
return 1;
}
/* give the video memory to PL */
PL_init(vid_getinfo()->video, VW, VH);
init();
sys_start();
sys_shutdown();
return 0;
}