-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_str.c
417 lines (396 loc) · 13.3 KB
/
hello_str.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
// The nolibgs 2021 demo disc !
#include "OVL/common.h"
// str playback
#include "src/str.h"
#include "src/mod.h"
#include "third_party/nugget/common/syscalls/syscalls.h"
#define printf ramsyscall_printf
#define DBG_FNTX 32
#define DBG_FNTY 200
//#define DEBUG
DISPENV disp[2]; // Double buffered DISPENV and DRAWENV
DRAWENV draw[2];
char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes
u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes
char * nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0]
uint8_t db = 1; // index of which buffer is used, values 0, 1
RECT tw = {0,0,0,0};
// STR playback
STR menu[4] = {
{ "\\MENU.STR;1", 256, 240, 30, 0, 0 },
{ "\\MENU.STR;1", 256, 240, 30, 1, 0 },
{ "\\MENU1.STR;1", 256, 240, 30, 0, 0 },
{ "\\MENU1.STR;1", 256, 240, 30, 1, 0 },
};
STR * curStr;
StHEADER * sectorHeader;
int8_t drawMenu = 0;
u_long * nextFrame = 0;
// Ring buffer frame address
u_long * frameAddr = 0;
// OVERLAYS
extern u_long load_all_overlays_here;
typedef struct OVERLAY {
char filename[0x7c];
int (*main)();
char commandline[0x180];
char title[0xc];
CVECTOR BGcolor;
} OVERLAY;
int ovl_main_hello();
int ovl_main_tile();
int ovl_main_poly();
int ovl_main_sprt();
int ovl_main_cubetex();
int ovl_main_light();
int ovl_main_pad();
OVERLAY menu_items[] = {
{"\\HELLO.OVL;1", ovl_main_hello, "", "HELLO WORLD!", { 225, 220, 40 } },
{"\\TILE.OVL;1", ovl_main_tile, "", "HELLO TILE! ", { 150, 30 , 40 } },
{"\\POLY.OVL;1", ovl_main_poly, "", "HELLO POLY! ", { 60 , 127, 0 } },
{"\\SPRT.OVL;1", ovl_main_sprt, "", "HELLO SPRT! ", { 0 , 108, 255 } },
{"\\CUBTX.OVL;1", ovl_main_cubetex, "", "HELLO CUBE! ", { 255 , 200, 0 } },
{"\\CUBLT.OVL;1", ovl_main_light, "", "HELLO LIGHT!", { 16 , 90 , 110 } },
{"\\PAD.OVL;1", ovl_main_pad, "", "HELLO PAD! ", { 100, 40 , 140 } },
};
int curItem = 0;
#define MAX_ITEMS 6
int timeout = 200;
enum OverlayNumber next_overlay = MOTHERSHIP;
enum OverlayNumber prev_overlay = MOTHERSHIP;
CVECTOR BGcolor, MScolor = { 24, 108, 76 };
uint8_t Xpressed = 0;
// FONT COLOR
CVECTOR fntColor = { 115, 215, 45 };
CVECTOR fntColorBG = { 0, 0, 0 };
uint8_t useOT = 0;
int CDreadOK = 0;
// FUNCTIONS
void LoadTexture(u_long * tim, TIM_IMAGE * tparam);
void FntColor(CVECTOR fgcol, CVECTOR bgcol );
void init(void);
void display(void);
void drawBG(void);
void checkPad(void);
void clearVRAM(void);
int loadOverlayAndStart(OVERLAY *);
void EmptyOTag(u_long ot[2][OTLEN]);
void EmptyPrimBuf(char primbuff[2][32768], char ** nextpri);
void preInitOvl(OVERLAY * overlay);
void postInitOvl(OVERLAY * overlay);
void (*dispp) (void);
void LoadTexture(u_long * tim, TIM_IMAGE * tparam){
OpenTIM(tim);
ReadTIM(tparam);
LoadImage(tparam->prect, tparam->paddr);
DrawSync(0);
if (tparam->mode & 0x8){ // check 4th bit
LoadImage(tparam->crect, tparam->caddr);
DrawSync(0);
}
}
void FntColor(CVECTOR fgcol, CVECTOR bgcol )
{
// The debug font clut is at tx, ty + 128
// tx = bg color
// tx + 1 = fg color
// We can override the color by drawing a rect at these coordinates
//
RECT fg = { FONTX+1, FONTY + 128, 1, 1 };
RECT bg = { FONTX, FONTY + 128, 1, 1 };
ClearImage(&fg, fgcol.r, fgcol.g, fgcol.b);
ClearImage(&bg, bgcol.r, bgcol.g, bgcol.b);
}
void init(void)
{
ResetCallback();
ResetGraph(0); // Initialize drawing engine with a complete reset (0)
InitGeom();
SetGeomOffset(CENTERX,CENTERY);
SetGeomScreen(CENTERX);
SetDefDispEnv(&disp[0], 0, 0 , SCREENXRES, SCREENYRES); // Set display area for both &disp[0] and &disp[1]
SetDefDispEnv(&disp[1], 0, SCREENYRES, SCREENXRES, SCREENYRES); // &disp[0] is on top of &disp[1]
SetDefDrawEnv(&draw[0], 0, SCREENYRES, SCREENXRES, SCREENYRES); // Set draw for both &draw[0] and &draw[1]
SetDefDrawEnv(&draw[1], 0, 0 , SCREENXRES, SCREENYRES); // &draw[0] is below &draw[1]
// Set video mode
#if VMODE
SetVideoMode(MODE_PAL);
disp[0].disp.y = 8;
disp[1].disp.y = 8;
#endif
SetDispMask(1); // Display on screen
setRGB0(&draw[0], BGcolor.r, BGcolor.g, BGcolor.b); // set color for first draw area
setRGB0(&draw[1], BGcolor.r, BGcolor.g, BGcolor.b); // set color for second draw area
draw[0].isbg = 1; // set mask for draw areas. 1 means repainting the area with the RGB color each frame
draw[1].isbg = 1;
draw[0].dfe = 0; // set mask for draw areas. 1 means repainting the area with the RGB color each frame
draw[1].dfe = 0;
draw[0].dtd = 1;
draw[1].dtd = 1;
PutDispEnv(&disp[db]); // set the disp and draw environnments
PutDrawEnv(&draw[db]);
FntLoad(FONTX, FONTY); // Load font to vram at 960,0(+128)
// Debug text
FntOpen(DBG_FNTX, DBG_FNTY, 256, 120, 0, 220 ); // FntOpen(x, y, width, height, black_bg, max. nbr. chars
// Menu text
FntOpen(106, 166, 48, 20, 0, 12 ); // FntOpen(x, y, width, height, black_bg, max. nbr. chars
// Change font color
FntColor(fntColor, fntColorBG);
}
void display(void)
{
DrawSync(0); // Wait for all drawing to terminate
VSync(0); // Wait for the next vertical blank
PutDispEnv(&disp[db]); // set alternate disp and draw environnments
PutDrawEnv(&draw[db]);
if (useOT) { DrawOTag(&ot[db][OTLEN - 1]); }
db = !db;
if (useOT) { nextpri = primbuff[db]; } // flip db value (0 or 1)
}
void drawBG(void)
{
POLY_FT4 * poly = 0; // pointer to a POLY_G4
ClearOTagR(ot[db], OTLEN);
poly = (POLY_FT4 *)nextpri; // Set poly to point to the address of the next primitiv in the buffer
// Set transform matrices for this polygon
setPolyFT4(poly); // Initialize poly as a POLY_F4
poly->tpage = getTPage(2,0,STR_POS_X, STR_POS_Y);
setRGB0(poly, 128, 128, 128); // Set poly color (neutral here)
setXY4(poly,
0, 0,
0, SCREENYRES,
SCREENXRES, 0,
SCREENXRES, SCREENYRES);
setUV4(poly, 0, 0,
0, 240,
255, 0,
255, 240); // Set UV coordinates in order Top Left, Bottom Left, Top Right, Bottom Right
addPrim(ot[db][OTLEN-1], poly); // add poly to the Ordering table
}
void clearVRAM(void)
{
RECT vram = {0,0,1024,512};
ClearImage(&vram,0,0,0);
}
void preInitOvl(OVERLAY * overlay)
{
//~ clearVRAM();
ResetCallback();
ResetGraph(3);
EmptyPrimBuf(primbuff, &nextpri);
EmptyOTag(&ot[db]);
setRGB(&BGcolor, overlay->BGcolor.r, overlay->BGcolor.g, overlay->BGcolor.b );
}
void postInitOvl(OVERLAY * overlay)
{
clearVRAM();
//~ ResetGraph(3);
EmptyPrimBuf(primbuff, &nextpri);
EmptyOTag(&ot[db]);
setRGB(&BGcolor, MScolor.r, MScolor.g, MScolor.b);
setRGB0(&draw[0], BGcolor.r, BGcolor.g, BGcolor.b); // set color for first draw area
setRGB0(&draw[1], BGcolor.r, BGcolor.g, BGcolor.b);
FntLoad(FONTX, FONTY); // Load font to vram at 960,0(+128)
FntOpen(DBG_FNTX, DBG_FNTY, 256, 120, 0, 220 ); // FntOpen(x, y, width, height, black_bg, max. nbr. chars
// Menu text
FntOpen(106, 166, 48, 20, 0, 12); // FntOpen(x, y, width, height, black_bg, max. nbr. chars
// Change font color
FntColor(fntColor, fntColorBG);
}
int loadOverlayAndStart(OVERLAY * overlay)
{
int CDreadResult = 0;
int next_ovl = -1;
// Load overlay file from CD
CDreadResult = CdReadFile(overlay->filename, &load_all_overlays_here, 0);
CdReadSync(0, 0);
printf(0,"CD read: %d", CDreadResult);
// If file loaded sucessfully
if (CDreadResult)
{
// Pre OVL
preInitOvl(overlay);
// Exec OVL
next_ovl = overlay->main();
// Post OVL
postInitOvl(overlay);
}
return next_ovl;
}
//~ void EmptyOTag(u_long ot[2][OTLEN], char * primbuff, char ** nextpri )
void EmptyPrimBuf(char primbuff[2][32768], char ** nextpri)
{
for(uint16_t p; p < 32768; p++)
{
primbuff[0][p] = 0;
primbuff[1][p] = 0;
}
*nextpri = primbuff[0];
}
void EmptyOTag(u_long ot[2][OTLEN])
{
for (uint16_t p; p < OTLEN; p++)
{
ot[0][p] = 0;
ot[1][p] = 0;
}
}
void checkPad(void)
{
u_long pad = 0;
static u_long oldPad;
pad = PadRead(0);
if (drawMenu){
if ( pad & PADLleft && !(oldPad & PADLleft) )
{
// Channel 1 is transition anim, only take input when !transition
if ( curStr == &menu[IDLE] )
{
ramsyscall_printf("Left\n");
// Switch sound
MOD_PlayNote(22, 12, 15, 63);
switchStr(&curStr, LEFT);
if ( curItem < MAX_ITEMS ){
curItem++;
} else {
curItem = 0;
}
ramsyscall_printf("curItem: %d\n", curItem);
}
oldPad = pad;
}
if ( !(pad & PADLleft) && oldPad & PADLleft )
{
oldPad = pad;
}
// Right
if ( pad & PADLright && !(oldPad & PADLright) )
{
// Channel 1 is transition anim, only take input when !transition
if( curStr == &menu[IDLE] )
{
ramsyscall_printf("Right\n");
// Switch sound
MOD_PlayNote(23, 12, 15, 63);
switchStr(&curStr, RIGHT);
if ( curItem > 0 ){
curItem--;
} else {
curItem = MAX_ITEMS;
}
ramsyscall_printf("curItem: %d\n", curItem);
}
oldPad = pad;
}
if ( !(pad & PADLright) && oldPad & PADLright )
{
oldPad = pad;
}
}
// Cross button
if ( pad & PADRdown && !(oldPad & PADRdown) )
{
Xpressed = 1;
oldPad = pad;
}
if ( !(pad & PADRdown) && oldPad & PADRdown )
{
Xpressed = 0;
oldPad = pad;
}
if ( pad & PADstart && !(oldPad & PADstart) )
{
drawMenu = !drawMenu;
oldPad = pad;
}
if ( !(pad & PADstart) && oldPad & PADstart )
{
oldPad = pad;
}
}
int main() {
int t = 0;
// STR
curStr = &(menu[0]);
// OVL
prev_overlay = next_overlay;
// Set bg color
setRGB(&BGcolor, MScolor.r, MScolor.g, MScolor.b);
// Mute spu to get rid of startup sound
spuMute();
// Init display
init();
// Init input
PadInit(0);
VSyncCallback(checkPad);
// Init CDrom system
CdInit();
// Init MDEC and load STR
initSTR(curStr);
// Set Channel
StSetChannel( curStr->channel );
// Mod Playback
loadMod();
startMusic();
// Main loop
while (1)
{
// Only display background STR if drawMenu is set
if (drawMenu = 1)
{
useOT = 1;
// Draw menu prim once
drawBG();
drawMenu = -1;
}
// While end of str is not reached, play it
if (curStr->endPlayback == 1 && next_overlay == MOTHERSHIP )
{
// Replay STR
resetSTR(curStr);
}
if ( curStr->endPlayback == 0 )
{
playSTR(&curStr);
if ( curStr->channel != 1 && drawMenu )
{
// Display title
FntPrint(1, "%s", menu_items[curItem].title);
// Flickering text
if ( sectorHeader->frameCount > 5 )
{
FntFlush(1);
} else if ( (sectorHeader->frameCount % 2) && sectorHeader->frameCount < 5 )
{
FntFlush(1);
}
// If the cross button is pressed
if( Xpressed ) {
// If we're on the menu, animate menu
if ( next_overlay == MOTHERSHIP ){
// Select sound
MOD_PlaySoundEffect(22, 7, 15, 63);
drawMenu = 0;
curStr->endPlayback = 1;
stopSTR(curStr);
useOT = 0;
next_overlay = curItem;
}
}
}
}
// If next overlay is not current overlay, load it
if (next_overlay != MOTHERSHIP ){
prev_overlay = next_overlay;
next_overlay = loadOverlayAndStart(&menu_items[next_overlay]);
t = 0;
}
t++;
#ifdef DEBUG
FntPrint(0, "Mothership: %d\nOvl: %d %d %d\n%x %x %x\nstr x: %d", t, prev_overlay, next_overlay, curItem, nextpri, primbuff[0], primbuff[1], curStr->x);
FntFlush(0);
#endif
display();
}
return 0;
}