nolibgs_demo/hello_str.c

415 lines
13 KiB
C
Raw Normal View History

2021-10-29 15:16:29 +02:00
// The nolibgs 2021 demo disc !
2021-10-29 18:25:04 +02:00
#include "OVL/common.h"
2021-10-24 19:39:44 +02:00
// str playback
#include "src/str.h"
2021-10-26 18:24:12 +02:00
#include "src/mod.h"
2021-10-29 18:25:04 +02:00
#include "third_party/nugget/common/syscalls/syscalls.h"
#define printf ramsyscall_printf
2021-11-14 18:04:11 +01:00
#define DBG_FNTX 32
#define DBG_FNTY 200
2021-10-21 16:35:08 +02:00
DISPENV disp[2]; // Double buffered DISPENV and DRAWENV
DRAWENV draw[2];
2021-10-29 18:25:04 +02:00
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
2021-11-14 18:04:11 +01:00
char * nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0]
2021-10-29 18:25:04 +02:00
uint8_t db = 1; // index of which buffer is used, values 0, 1
2021-11-14 18:04:11 +01:00
RECT tw = {0,0,0,0};
2021-10-29 18:25:04 +02:00
// STR playback
2021-10-28 18:28:04 +02:00
STR menu[4] = {
2021-10-24 19:39:44 +02:00
{ "\\MENU.STR;1", 256, 240, 30, 0, 0 },
{ "\\MENU.STR;1", 256, 240, 30, 1, 0 },
2021-10-28 18:28:04 +02:00
{ "\\MENU1.STR;1", 256, 240, 30, 0, 0 },
{ "\\MENU1.STR;1", 256, 240, 30, 1, 0 },
2021-10-24 19:39:44 +02:00
};
STR * curStr;
StHEADER * sectorHeader;
2021-11-14 18:04:11 +01:00
int8_t drawMenu = 0;
2021-10-24 19:39:44 +02:00
u_long * nextFrame = 0;
// Ring buffer frame address
u_long * frameAddr = 0;
2021-10-24 20:32:52 +02:00
2021-10-29 18:25:04 +02:00
// OVERLAYS
extern u_long load_all_overlays_here;
2021-10-21 16:35:08 +02:00
2021-10-24 20:32:52 +02:00
typedef struct OVERLAY {
char filename[0x7c];
int (*main)();
char commandline[0x180];
char title[0xc];
2021-11-14 18:04:11 +01:00
CVECTOR BGcolor;
2021-10-24 20:32:52 +02:00
} OVERLAY;
2021-10-29 18:25:04 +02:00
int ovl_main_hello();
int ovl_main_tile();
int ovl_main_poly();
2021-11-14 18:04:11 +01:00
int ovl_main_sprt();
int ovl_main_cubetex();
int ovl_main_light();
int ovl_main_pad();
2021-10-29 18:25:04 +02:00
OVERLAY menu_items[] = {
2021-11-14 18:04:11 +01:00
{"\\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 } },
2021-10-24 20:32:52 +02:00
};
2021-11-14 18:04:11 +01:00
int curItem = 0;
#define MAX_ITEMS 6
int timeout = 200;
2021-10-24 20:32:52 +02:00
2021-10-29 18:25:04 +02:00
enum OverlayNumber next_overlay = MOTHERSHIP;
enum OverlayNumber prev_overlay = MOTHERSHIP;
2021-11-14 18:04:11 +01:00
CVECTOR BGcolor, MScolor = { 24, 108, 76 };
uint8_t Xpressed = 0;
2021-10-29 18:25:04 +02:00
// FONT COLOR
2021-10-24 20:32:52 +02:00
CVECTOR fntColor = { 115, 215, 45 };
CVECTOR fntColorBG = { 0, 0, 0 };
2021-10-29 18:25:04 +02:00
2021-11-14 18:04:11 +01:00
uint8_t useOT = 0;
int CDreadOK = 0;
2021-10-29 18:25:04 +02:00
2021-11-14 18:04:11 +01:00
// FUNCTIONS
void LoadTexture(u_long * tim, TIM_IMAGE * tparam);
2021-10-29 18:25:04 +02:00
void FntColor(CVECTOR fgcol, CVECTOR bgcol );
void init(void);
void display(void);
void drawBG(void);
void checkPad(void);
2021-11-14 18:04:11 +01:00
void clearVRAM(void);
2021-10-29 18:25:04 +02:00
int loadOverlayAndStart(OVERLAY *);
2021-11-14 18:04:11 +01:00
void EmptyOTag(u_long ot[2][OTLEN]);
void EmptyPrimBuf(char primbuff[2][32768], char ** nextpri);
void preInitOvl(OVERLAY * overlay);
void postInitOvl(OVERLAY * overlay);
2021-10-29 18:25:04 +02:00
void (*dispp) (void);
2021-11-14 18:04:11 +01:00
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);
}
}
2021-10-29 18:25:04 +02:00
2021-10-24 20:32:52 +02:00
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);
}
2021-10-21 16:35:08 +02:00
void init(void)
{
ResetCallback();
ResetGraph(0); // Initialize drawing engine with a complete reset (0)
2021-10-29 18:25:04 +02:00
2021-10-24 19:39:44 +02:00
InitGeom();
SetGeomOffset(CENTERX,CENTERY);
SetGeomScreen(CENTERX);
2021-10-21 16:35:08 +02:00
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
2021-10-29 18:25:04 +02:00
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
2021-10-24 19:39:44 +02:00
draw[0].isbg = 1; // set mask for draw areas. 1 means repainting the area with the RGB color each frame
draw[1].isbg = 1;
2021-11-14 18:04:11 +01:00
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;
2021-10-21 16:35:08 +02:00
PutDispEnv(&disp[db]); // set the disp and draw environnments
PutDrawEnv(&draw[db]);
2021-10-24 20:32:52 +02:00
FntLoad(FONTX, FONTY); // Load font to vram at 960,0(+128)
2021-11-14 18:04:11 +01:00
// 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
2021-10-24 20:32:52 +02:00
FntColor(fntColor, fntColorBG);
2021-10-21 16:35:08 +02:00
}
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]);
2021-11-14 18:04:11 +01:00
if (useOT) { DrawOTag(&ot[db][OTLEN - 1]); }
2021-10-24 19:39:44 +02:00
db = !db;
2021-11-14 18:04:11 +01:00
if (useOT) { nextpri = primbuff[db]; } // flip db value (0 or 1)
2021-10-24 19:39:44 +02:00
}
void drawBG(void)
{
2021-11-14 18:04:11 +01:00
POLY_FT4 * poly = 0; // pointer to a POLY_G4
2021-10-24 19:39:44 +02:00
ClearOTagR(ot[db], OTLEN);
2021-11-14 18:04:11 +01:00
2021-10-24 19:39:44 +02:00
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);
2021-11-14 18:04:11 +01:00
setRGB0(poly, 128, 128, 128); // Set poly color (neutral here)
setXY4(poly,
0, 0,
0, SCREENYRES,
SCREENXRES, 0,
SCREENXRES, SCREENYRES);
2021-10-24 19:39:44 +02:00
setUV4(poly, 0, 0,
0, 240,
255, 0,
2021-11-14 18:04:11 +01:00
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
2021-10-21 16:35:08 +02:00
}
2021-10-29 18:25:04 +02:00
void clearVRAM(void)
{
RECT vram = {0,0,1024,512};
ClearImage(&vram,0,0,0);
}
2021-11-14 18:04:11 +01:00
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);
}
2021-10-29 18:25:04 +02:00
int loadOverlayAndStart(OVERLAY * overlay)
{
int CDreadResult = 0;
int next_ovl = -1;
2021-11-14 18:04:11 +01:00
// Load overlay file from CD
2021-10-29 18:25:04 +02:00
CDreadResult = CdReadFile(overlay->filename, &load_all_overlays_here, 0);
CdReadSync(0, 0);
2021-11-14 18:04:11 +01:00
printf(0,"CD read: %d", CDreadResult);
2021-10-29 18:25:04 +02:00
// If file loaded sucessfully
if (CDreadResult)
{
2021-11-14 18:04:11 +01:00
// Pre OVL
preInitOvl(overlay);
// Exec OVL
2021-10-29 18:25:04 +02:00
next_ovl = overlay->main();
2021-11-14 18:04:11 +01:00
// Post OVL
postInitOvl(overlay);
2021-10-29 18:25:04 +02:00
}
return next_ovl;
}
2021-11-14 18:04:11 +01:00
//~ 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])
2021-10-29 18:25:04 +02:00
{
for (uint16_t p; p < OTLEN; p++)
{
ot[0][p] = 0;
ot[1][p] = 0;
}
}
2021-10-26 18:24:12 +02:00
void checkPad(void)
{
2021-10-29 18:25:04 +02:00
u_long pad = 0;
static u_long oldPad;
2021-10-26 18:24:12 +02:00
pad = PadRead(0);
2021-11-14 18:04:11 +01:00
if (drawMenu){
if ( pad & PADLleft && !(oldPad & PADLleft) )
2021-10-26 18:24:12 +02:00
{
2021-11-14 18:04:11 +01:00
// 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;
2021-10-26 18:24:12 +02:00
}
2021-11-14 18:04:11 +01:00
if ( !(pad & PADLleft) && oldPad & PADLleft )
2021-10-28 18:28:04 +02:00
{
2021-11-14 18:04:11 +01:00
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;
2021-10-28 18:28:04 +02:00
}
}
2021-11-14 18:04:11 +01:00
// 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;
}
2021-10-26 18:24:12 +02:00
}
2021-10-21 16:35:08 +02:00
int main() {
2021-10-29 18:25:04 +02:00
int t = 0;
// STR
2021-10-24 19:39:44 +02:00
curStr = &(menu[0]);
2021-10-29 18:25:04 +02:00
// OVL
prev_overlay = next_overlay;
2021-11-14 18:04:11 +01:00
// Set bg color
setRGB(&BGcolor, MScolor.r, MScolor.g, MScolor.b);
// Mute spu to get rid of startup sound
spuMute();
// Init display
2021-10-21 16:35:08 +02:00
init();
2021-11-14 18:04:11 +01:00
// Init input
2021-10-24 19:39:44 +02:00
PadInit(0);
VSyncCallback(checkPad);
2021-10-21 16:35:08 +02:00
// Init CDrom system
CdInit();
2021-10-24 19:39:44 +02:00
// Init MDEC and load STR
initSTR(curStr);
2021-10-21 16:35:08 +02:00
// Set Channel
2021-10-24 19:39:44 +02:00
StSetChannel( curStr->channel );
2021-10-28 18:28:04 +02:00
// Mod Playback
loadMod();
startMusic();
2021-10-21 16:35:08 +02:00
// Main loop
2021-10-26 18:24:12 +02:00
while (1)
{
2021-11-14 18:04:11 +01:00
// Only display background STR if drawMenu is set
if (drawMenu = 1)
{
useOT = 1;
// Draw menu prim once
2021-10-24 19:39:44 +02:00
drawBG();
2021-11-14 18:04:11 +01:00
drawMenu = -1;
2021-10-24 19:39:44 +02:00
}
2021-11-14 18:04:11 +01:00
// While end of str is not reached, play it
2021-10-29 18:25:04 +02:00
if (curStr->endPlayback == 1 && next_overlay == MOTHERSHIP )
2021-10-21 16:35:08 +02:00
{
2021-10-24 19:39:44 +02:00
// Replay STR
resetSTR(curStr);
2021-10-21 16:35:08 +02:00
}
2021-10-29 18:25:04 +02:00
if ( curStr->endPlayback == 0 )
2021-10-24 19:39:44 +02:00
{
playSTR(&curStr);
2021-11-14 18:04:11 +01:00
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;
}
}
}
2021-10-29 18:25:04 +02:00
}
2021-11-14 18:04:11 +01:00
// If next overlay is not current overlay, load it
if (next_overlay != MOTHERSHIP ){
2021-10-29 18:25:04 +02:00
prev_overlay = next_overlay;
next_overlay = loadOverlayAndStart(&menu_items[next_overlay]);
t = 0;
2021-10-21 16:35:08 +02:00
}
2021-10-29 18:25:04 +02:00
t++;
2021-11-14 18:04:11 +01:00
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);
2021-10-26 18:24:12 +02:00
display();
2021-10-21 16:35:08 +02:00
}
return 0;
2021-10-28 18:28:04 +02:00
}