-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-clut.c
283 lines (235 loc) · 7.55 KB
/
main-clut.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
/*
* APoV Project pspgu clut version
* By m-c/d in 2020
*/
#include <pspgu.h>
#include <pspgum.h>
#include <pspkernel.h>
#include <pspctrl.h>
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <psprtc.h>
#include <psppower.h>
#include <pspdisplay.h>
#define TEXTURE_BLOCK_SIZE 256
#define BUFFER_WIDTH 512
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 272
PSP_MODULE_INFO("APoV", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_KB(-1024);
void sceDmacMemcpy(void *dst, const void *src, int size);
typedef struct Vertex {
u16 u, v;
u16 x, y, z;
} Vertex;
#define S TEXTURE_BLOCK_SIZE
#define P (S / 16)
#define T (S / 16)
#define CLUT_COLOR_COUNT 256
static u32 __attribute__((aligned(16))) clut[CLUT_COLOR_COUNT] = {0};
static u16 VERTICES_COUNT;
static u16 TEXTURE_WIDTH;
static Vertex* surface;
void generateRenderSurface() {
const u8 VERTICES_BY_BLOCK = 2;
VERTICES_COUNT = VERTICES_BY_BLOCK * (TEXTURE_BLOCK_SIZE / P) * (TEXTURE_WIDTH / P);
surface = memalign(16, sizeof(Vertex) * VERTICES_COUNT);
const u16 X = (SCREEN_WIDTH - TEXTURE_WIDTH) / 2;
const u16 Y = (SCREEN_HEIGHT - TEXTURE_BLOCK_SIZE) / 2;
u16 x = 0;
u16 offset = 0;
while(x < TEXTURE_WIDTH) {
u16 y = 0;
while(y < TEXTURE_BLOCK_SIZE) {
const Vertex a = {x, y, X+x, Y+y, 0};
const Vertex b = {x+T, y+T, X+x+P, Y+y+P, 0};
surface[offset + 0] = a;
surface[offset + 1] = b;
offset += VERTICES_BY_BLOCK;
y += P;
}
x += P;
}
}
#define SPACE_BLOCK_SIZE 256
static u32 WIDTH_BLOCK_COUNT = 1;
static u32 DEPTH_BLOCK_COUNT = 1;
static u32 RAY_STEP = 1;
static u32 HORIZONTAL_POV_COUNT = 4;
static u32 VERTICAL_POV_COUNT = 1;
static u16 WIN_WIDTH;
static u16 WIN_HEIGHT = SPACE_BLOCK_SIZE;
static u32 WIN_PIXELS_COUNT;
static u32 FRAME_INDICES_COUNT;
static u32 SPACE_INDICES_COUNT;
static void initGuContext(void* list) {
sceGuStart(GU_DIRECT, list);
sceGuDrawBuffer(GU_PSM_8888, (void*)0, BUFFER_WIDTH);
sceGuDispBuffer(SCREEN_WIDTH, SCREEN_HEIGHT, (void*)(sizeof(u32) *
BUFFER_WIDTH * SCREEN_HEIGHT) , BUFFER_WIDTH);
sceGuClearColor(0xFF000000);
sceGuDisable(GU_SCISSOR_TEST);
sceGuEnable(GU_CULL_FACE);
sceGuFrontFace(GU_CW);
sceGuEnable(GU_CLIP_PLANES);
sceGuTexWrap(GU_CLAMP, GU_CLAMP);
sceGuEnable(GU_TEXTURE_2D);
sceGuClutLoad(CLUT_COLOR_COUNT / 8, clut);
sceGuClutMode(GU_PSM_8888, 0, CLUT_COLOR_COUNT - 1, 0);
sceGuTexMode(GU_PSM_T8, 0, 0, 0);
sceGuTexFunc(GU_TFX_REPLACE, GU_TCC_RGB);
sceGuTexFilter(GU_NEAREST, GU_NEAREST);
sceGuDisplay(GU_TRUE);
sceGuFinish();
sceGuSync(GU_SYNC_FINISH, GU_SYNC_WHAT_DONE);
}
static SceUID f;
static void openCloseIo(const u8 open) {
if(open) {
f = sceIoOpen("clut-indexes.bin", PSP_O_RDONLY, 0777);
} else sceIoClose(f);
}
static u8 readIo(u8* const frame, const u64 offset) {
static u64 loffset = -1;
if(offset != loffset) {
sceIoLseek(f, offset, SEEK_SET);
const u64 nbytes = WIN_PIXELS_COUNT * sizeof(u8);
if(nbytes != sceIoRead(f, frame, nbytes)) {
openCloseIo(1);
return 0;
}
loffset = offset;
return 1;
}
return 0;
}
void updateView(u8* const frame, u8* const base) {
sceKernelDcacheWritebackAll();
sceDmacMemcpy(base, frame, FRAME_INDICES_COUNT);
}
static int ajustCursor(const int value, const u8 mode) {
if(!mode) {
u16 max;
if(value < 0) {
return 0;
} else if(value >= (max = (SPACE_BLOCK_SIZE * DEPTH_BLOCK_COUNT) / RAY_STEP)) {
return max - 1;
}
} else if(mode == 1) {
if(value < 0) {
return HORIZONTAL_POV_COUNT - 1;
} else if(value >= HORIZONTAL_POV_COUNT) {
return 0;
}
} else if(mode == 2) {
if(value < 0) {
return VERTICAL_POV_COUNT - 1;
} else if(value >= VERTICAL_POV_COUNT) {
return 0;
}
}
return value;
}
static u64 getOffset(const int move, const int hrotate, const int vrotate) {
return FRAME_INDICES_COUNT * move + (hrotate * VERTICAL_POV_COUNT + vrotate) * SPACE_INDICES_COUNT;
}
SceCtrlData pad;
static u64 controls() {
static int move = 0;
static int hrotate = 0;
static int vrotate = 0;
sceCtrlReadBufferPositive(&pad, 1);
if(pad.Buttons & PSP_CTRL_TRIANGLE) { move++; }
if(pad.Buttons & PSP_CTRL_CROSS) { move--; }
if(pad.Buttons & PSP_CTRL_RIGHT) { hrotate--; }
if(pad.Buttons & PSP_CTRL_LEFT) { hrotate++; }
if(pad.Buttons & PSP_CTRL_UP) { vrotate--; }
if(pad.Buttons & PSP_CTRL_DOWN) { vrotate++; }
move = ajustCursor(move, 0);
hrotate = ajustCursor(hrotate, 1);
vrotate = ajustCursor(vrotate, 2);
return getOffset(move, hrotate, vrotate);
}
u8 getPower(u16 value) {
u8 power = 0;
while(value > 1) {
if((value & 1) != 0) {
return 0;
}
power++;
value >>= 1;
}
return power;
}
void getOptions() {
FILE* f = fopen("options.txt", "r");
if(f != NULL) {
char* options = (char*)memalign(16, 128);
fgets(options, 128, f);
sscanf(options, "HPOV:%u VPOV:%u RAYSTEP:%u WBCOUNT:%u DBCOUNT:%u",
&HORIZONTAL_POV_COUNT,
&VERTICAL_POV_COUNT,
&RAY_STEP,
&WIDTH_BLOCK_COUNT,
&DEPTH_BLOCK_COUNT);
fclose(f);
free(options);
}
f = fopen("clut.bin", "rb");
if(f != NULL) {
fread(clut, sizeof(u32), CLUT_COLOR_COUNT, f);
fclose(f);
}
}
int main() {
scePowerSetClockFrequency(333, 333, 166);
getOptions();
WIN_WIDTH = SPACE_BLOCK_SIZE * WIDTH_BLOCK_COUNT;
WIN_PIXELS_COUNT = WIN_WIDTH * WIN_HEIGHT;
FRAME_INDICES_COUNT = WIN_PIXELS_COUNT * sizeof(u8);
SPACE_INDICES_COUNT = ((DEPTH_BLOCK_COUNT * SPACE_BLOCK_SIZE) / RAY_STEP) * FRAME_INDICES_COUNT;
TEXTURE_WIDTH = TEXTURE_BLOCK_SIZE * WIDTH_BLOCK_COUNT;
u8* base = memalign(16, FRAME_INDICES_COUNT);
u8* frame = memalign(16, FRAME_INDICES_COUNT);
void* list = memalign(16, 262144);
generateRenderSurface();
sceGuInit();
initGuContext(list);
pspDebugScreenInitEx(NULL, PSP_DISPLAY_PIXEL_FORMAT_8888, 0);
pspDebugScreenEnableBackColor(0);
openCloseIo(1);
int dbuff = 0;
u64 prev, now, fps = 0;
const u64 tickResolution = sceRtcGetTickResolution();
do {
sceRtcGetCurrentTick(&prev);
sceGuStart(GU_DIRECT, list);
sceGuClear(GU_COLOR_BUFFER_BIT);
if(readIo(frame, controls())) {
updateView(frame, base);
}
sceGuTexImage(0, TEXTURE_WIDTH, TEXTURE_BLOCK_SIZE, TEXTURE_WIDTH, base);
sceGumDrawArray(GU_SPRITES, GU_TEXTURE_16BIT|GU_TRANSFORM_2D|GU_VERTEX_16BIT,
VERTICES_COUNT, 0, surface);
sceGuFinish();
sceGuSync(GU_SYNC_FINISH, GU_SYNC_WHAT_DONE);
pspDebugScreenSetOffset(dbuff);
pspDebugScreenSetXY(0, 0);
pspDebugScreenSetTextColor(0xFF00A0FF);
pspDebugScreenPrintf("Fps: %llu\n", fps);
sceDisplayWaitVblankStart();
dbuff = (int)sceGuSwapBuffers();
sceRtcGetCurrentTick(&now);
fps = tickResolution / (now - prev);
} while(!(pad.Buttons & PSP_CTRL_SELECT));
sceGuTerm();
free(surface);
free(list);
free(base);
free(frame);
openCloseIo(0);
sceKernelExitGame();
return 0;
}