Move code to functions

This commit is contained in:
ABelliqueux 2021-10-24 19:39:44 +02:00
parent 42529842bb
commit 6a4bd5dba2
9 changed files with 377 additions and 212 deletions

View File

@ -9,5 +9,6 @@ cleansub:
TARGET = hello_str TARGET = hello_str
SRCS = hello_str.c \ SRCS = hello_str.c \
src/str.c \
include common.mk include common.mk

View File

@ -5,6 +5,7 @@
// Video to STR conversion : https://github.com/ABelliqueux/nolibgs_hello_worlds/tree/main/hello_str // Video to STR conversion : https://github.com/ABelliqueux/nolibgs_hello_worlds/tree/main/hello_str
#include <sys/types.h> #include <sys/types.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h>
#include <libgte.h> #include <libgte.h>
#include <libetc.h> #include <libetc.h>
#include <libgpu.h> #include <libgpu.h>
@ -12,58 +13,49 @@
#include <libcd.h> #include <libcd.h>
// CODEC library // CODEC library
#include <libpress.h> #include <libpress.h>
// printf
#include "../nolibgs_hello_worlds/thirdparty/nugget/common/syscalls/syscalls.h" #include "../nolibgs_hello_worlds/thirdparty/nugget/common/syscalls/syscalls.h"
// str playback
#include "src/str.h"
#define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL #define VMODE 0 // Video Mode : 0 : NTSC, 1: PAL
#define TRUECOL 0 // 0 : 16bpp, 1: 24bpp
#define SCREENXRES 320 // Screen width #define SCREENXRES 320 // Screen width
#define SCREENYRES 240 + (VMODE << 4) // Screen height : If VMODE is 0 = 240, if VMODE is 1 = 256 #define SCREENYRES 240 // Screen height : If VMODE is 0 = 240, if VMODE is 1 = 256
#define CENTERX SCREENXRES/2 // Center of screen on x #define CENTERX SCREENXRES/2 // Center of screen on x
#define CENTERY SCREENYRES/2 // Center of screen on y #define CENTERY SCREENYRES/2 // Center of screen on y
#define MARGINX 8 // margins for text display #define MARGINX 8 // margins for text display
#define MARGINY 16 #define MARGINY 16
#define FONTSIZE 8 * 7 // Text Field Height #define FONTSIZE 8 * 7 // Text Field Height
#define OTLEN 8
DISPENV disp[2]; // Double buffered DISPENV and DRAWENV DISPENV disp[2]; // Double buffered DISPENV and DRAWENV
DRAWENV draw[2]; DRAWENV draw[2];
short db = 0; // index of which buffer is used, values 0, 1 u_long ot[2][OTLEN]; // double ordering table of length 8 * 32 = 256 bits / 32 bytes
char primbuff[2][32768]; // double primitive buffer of length 32768 * 8 = 262.144 bits / 32,768 Kbytes
char *nextpri = primbuff[0]; // pointer to the next primitive in primbuff. Initially, points to the first bit of primbuff[0]
short db = 1; // index of which buffer is used, values 0, 1
#define STR_POS_X 0 STR menu[2] = {
// If PAL mode, add 8 pixels offset on Y (256-240)/2 { "\\MENU.STR;1", 256, 240, 30, 0, 0 },
#define STR_POS_Y (VMODE << 4)/2 { "\\MENU.STR;1", 256, 240, 30, 1, 0 },
// Ring Buffer size (32 sectors seems good enough) };
#define RING_SIZE 32 STR * curStr;
StHEADER * sectorHeader;
#if TRUECOL uint8_t drawMenu = 0;
// pixels per short word (16b/2B) u_long * nextFrame = 0;
// 1px is 3B in 24bpp // Ring buffer frame address
// 1px is 2B(one word) in 16bpp u_long * frameAddr = 0;
// therefore 2B will hold 3/2 pixels void init(void);
#define PPW 3/2 void display(void);
// DCT mode - bit 0 : depth (0 = 16b, 1 = 24b), bit 1: in 16b mode, set STP(Semi-Transparency) bit 15. void drawBG(void);
// 24bpp = 01b => 1 void checkPad(void);
#define DCT_MODE 1
#else
#define PPW 1
// 16bpp = 10b => 2
#define DCT_MODE 2
#endif
// Stop playback if set
static int endPlayback = 0;
// STR file infos : Filename on CD, Width, Height, Length in frames
static char * StrFileName = "\\MENU.STR;1";
static int StrFileX = 320;
static int StrFileY = 240;
static int StrFileLength = 30;
// When using RGB24, a special routine has to be setup as a callback function to avoid MDEC/CDROM conflict
// See http://psx.arthus.net/sdk/Psy-Q/DOCS/LibRef47.pdf , p.713
static void strCheckRGB24();
void init(void) void init(void)
{ {
ResetCallback(); ResetCallback();
ResetGraph(0); // Initialize drawing engine with a complete reset (0) 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[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] 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[0], 0, SCREENYRES, SCREENXRES, SCREENYRES); // Set draw for both &draw[0] and &draw[1]
@ -74,19 +66,15 @@ void init(void)
disp[0].disp.y = 8; disp[0].disp.y = 8;
disp[1].disp.y = 8; disp[1].disp.y = 8;
#endif #endif
SetDispMask(1); // Display on screen SetDispMask(0); // Display on screen
setRGB0(&draw[0], 155, 0, 150); // set color for first draw area setRGB0(&draw[0], 0, 0, 0); // set color for first draw area
setRGB0(&draw[1], 155, 0, 150); // set color for second draw area setRGB0(&draw[1], 0, 0, 0); // set color for second draw area
draw[0].isbg = 0; // set mask for draw areas. 1 means repainting the area with the RGB color each frame draw[0].isbg = 1; // set mask for draw areas. 1 means repainting the area with the RGB color each frame
draw[1].isbg = 0; draw[1].isbg = 1;
#if TRUECOL
disp[0].isrgb24 = 1;
disp[1].isrgb24 = 1;
#endif
PutDispEnv(&disp[db]); // set the disp and draw environnments PutDispEnv(&disp[db]); // set the disp and draw environnments
PutDrawEnv(&draw[db]); PutDrawEnv(&draw[db]);
//~ FntLoad(960, 0); // Load font to vram at 960,0(+128) FntLoad(960, 0); // Load font to vram at 960,0(+128)
//~ FntOpen(MARGINX, MARGINY, SCREENXRES - MARGINX * 2, FONTSIZE, 0, 280 ); // FntOpen(x, y, width, height, black_bg, max. nbr. chars FntOpen(112, 168, 48, 16, 0, 20 ); // FntOpen(x, y, width, height, black_bg, max. nbr. chars
} }
void display(void) void display(void)
{ {
@ -94,182 +82,104 @@ void display(void)
VSync(0); // Wait for the next vertical blank VSync(0); // Wait for the next vertical blank
PutDispEnv(&disp[db]); // set alternate disp and draw environnments PutDispEnv(&disp[db]); // set alternate disp and draw environnments
PutDrawEnv(&draw[db]); PutDrawEnv(&draw[db]);
//~ db = !db; // flip db value (0 or 1) DrawOTag(&ot[db][OTLEN - 1]);
db = !db;
nextpri = primbuff[db]; // flip db value (0 or 1)
} }
void drawBG(void)
{
POLY_FT4 *poly = {0}; // pointer to a POLY_G4
SVECTOR RotVector = {0, 0, 0}; // Initialize rotation vector {x, y, z}
VECTOR MovVector = {0, 0, CENTERX/2, 0}; // Initialize translation vector {x, y, z, pad}
SVECTOR VertPos[4] = { // Set initial vertices position relative to 0,0 - see here : https://psx.arthus.net/docs/poly_f4.jpg
{-CENTERX/2, -CENTERY/2, 1 }, // Vert 1
{-CENTERX/2, CENTERY/2, 1 }, // Vert 2
{ CENTERX/2, -CENTERY/2, 1 }, // Vert 3
{ CENTERX/2, CENTERY/2, 1 } // Vert 4
};
MATRIX PolyMatrix = {0};
long polydepth;
long polyflag;
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
RotMatrix(&RotVector, &PolyMatrix); // Apply rotation matrix
TransMatrix(&PolyMatrix, &MovVector); // Apply translation matrix
SetRotMatrix(&PolyMatrix); // Set default rotation matrix
SetTransMatrix(&PolyMatrix); // Set default transformation matrix
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 (neutra here)
RotTransPers4(
&VertPos[0], &VertPos[1], &VertPos[2], &VertPos[3],
(long*)&poly->x0, (long*)&poly->x1, (long*)&poly->x2, (long*)&poly->x3,
&polydepth,
&polyflag
); // Perform coordinate and perspective transformation for 4 vertices
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], poly); // add poly to the Ordering table
nextpri += sizeof(POLY_FT4); // increment nextpri address with size of a POLY_F4 struct
}
int main() { int main() {
// CD File descriptor curStr = &(menu[0]);
CdlFILE STRfile; // Set pointers to the relevant buffer addresses
// Parameter we want to set the CDROM with u_long * curVLCptr = &VlcBuff[0];
u_char param=CdlModeSpeed; u_short * curIMGptr = &ImgBuff[0];
// Buffers total ~110KB in memory
// SECTOR_SIZE is defined in words : 512 * 4 == 2048 Bytes/sector
// Ring buffer : 32 * 2048 = 65536 Bytes
u_long RingBuff[ RING_SIZE * SECTOR_SIZE ];
// VLC buffers : display area in words (hence /2), 160*320 == 38400 Bytes
u_long VlcBuff[2][ SCREENXRES / 2 * StrFileY ];
// If using 16bpp, fetch 16xYres strips, if 24bpp, fetch 24xYres strips, 5120*PPW Bytes
u_short ImgBuff[2][ 16 * PPW * StrFileY];
//~ u_long * curVLCptr = VlcBuff[0];
// Init Disp/Draw env, Font, etc.
init(); init();
PadInit(0);
VSyncCallback(checkPad);
// Init CDrom system // Init CDrom system
CdInit(); CdInit();
// Reset the MDEC // Init MDEC and load STR
DecDCTReset(0); initSTR(curStr);
// Set callback routine
DecDCToutCallback(strCheckRGB24);
// Set ring buffer
StSetRing(RingBuff, RING_SIZE);
// Set streaming parameters
StSetStream(TRUECOL, 1, StrFileLength, 0, 0);
// Get the CD location of the STR file to play
if ( CdSearchFile(&STRfile, StrFileName) == 0 ) {
FntPrint("File not found :%s\n", StrFileName);
}
// Set the seek target position
CdControl(CdlSetloc, (u_char *)&STRfile.pos, 0);
// Set CD mode to CdlModeSpeed
CdControl(CdlSetmode, &param, 0);
// Read from CD at position &STRfile.pos
// Enable streaming, double speed and ADPCM playback
CdRead2(CdlModeStream|CdlModeSpeed|CdlModeRT);
// Use a counter to avoid deadlocks
int wait = WAIT_TIME;
// Next Ring Buffer Frame address
u_long * nextFrame = 0;
// Ring buffer frame address
u_long * frameAddr = 0;
// Ring buffer frame header
StHEADER * sectorHeader;
// Set Channel // Set Channel
//~ StSetChannel( 0 ); StSetChannel( curStr->channel );
u_long * curVLCptr = &VlcBuff[!db][0];
u_short * curIMGptr = &ImgBuff[db][0];
// Main loop // Main loop
while (1) { while (1) {
// Set some pointers to the relevant buffer addresses // Only display background STR if drawMenu is set
//~ u_long * curVLCptr = &VlcBuff[!db][0]; if (drawMenu)
//~ u_short * curIMGptr = &ImgBuff[db][0];
// While end of str is not reached, play it
if (endPlayback == 1)
{ {
u_long * curVLCptr = &VlcBuff[!db][0]; SetDispMask(1);
u_short * curIMGptr = &ImgBuff[db][0]; drawBG();
// Set the seek target position
CdControl(CdlSetloc, (u_char *)&STRfile.pos, 0);
// Set CD mode to CdlModeSpeed
CdControl(CdlSetmode, &param, 0);
// Read from CD at position &STRfile.pos
// Enable streaming, double speed and ADPCM playback
CdRead2(CdlModeStream|CdlModeSpeed|CdlModeRT);
endPlayback = 0;
} }
// While end of str is not reached, play it
while ( endPlayback == 0) if (curStr->endPlayback == 1)
{ {
// Use this area to draw the slices // Replay STR
RECT curSlice = { 0, resetSTR(curStr);
(db * StrFileY) + STR_POS_Y, }
// In 24bpp, use 24 pixels wide slices while ( curStr->endPlayback == 0)
16 * PPW , {
StrFileY}; playSTR(&curStr);
int frameDone = 0;
// Reset counter
wait = WAIT_TIME;
// Dont try decoding if not data has been loaded from ring buffer
if ( frameAddr ){
// Begin decoding RLE-encoded MDEC image data
DecDCTin( curVLCptr , DCT_MODE);
// Prepare to receive the decoded image data from the MDEC
while (curSlice.x < STR_POS_X + SCREENXRES * PPW) {
// Receive decoded data : a 16*ppw*240 px slice
DecDCTout( (u_long *) curIMGptr, curSlice.w * curSlice.h / 2);
// Wait for transfer end
DecDCToutSync(1);
// Transfer data from main memory to VRAM
LoadImage(&curSlice, (u_long *) curIMGptr );
// Increment drawArea's X with slice width (16 or 24 pix)
curSlice.x += 16 * PPW;
}
// Set frameDone flag to 1
frameDone = 1;
curSlice.x = STR_POS_X;
curSlice.y = (db * StrFileY) + STR_POS_Y;
}
// Get one frame of ring buffer data
// StGetNext is non-blocking, so we wait for it to return 0.
// StGetNext will lock the region at &frameAddr until StFreeRing() is called.
while ( StGetNext((u_long **)&frameAddr,(u_long **)&sectorHeader) ) {
wait--;
if (wait == 0)
break;
}
// Grab a frame from the stream
wait = WAIT_TIME;
while ((nextFrame = frameAddr) == 0) {
wait--;
if ( wait == 0 ){
break;
}
}
// Decode the Huffman/VLC compressed data
DecDCTvlc(nextFrame, curVLCptr);
// Unlock area obtained by StGetNext()
StFreeRing(nextFrame);
// Reset counter
wait = WAIT_TIME;
// Wait until the whole frame is loaded to VRAM
while ( frameDone == 0 ) {
wait--;
if ( wait == 0 ) {
// If a timeout occurs, force switching buffers
frameDone = 1;
curSlice.x = STR_POS_X;
curSlice.y = (db * StrFileY) + STR_POS_Y;
}
}
// If the current frame's number is bigger than the number of frames in STR,
// set the endPlayback flag.
ramsyscall_printf("Frame %d / %d\n", sectorHeader->frameCount, StrFileLength);
if (sectorHeader->frameCount >= (StrFileLength - 1) )
{
endPlayback = 1;
}
display(); display();
} }
display();
// Disable callback
//~ DecDCToutCallback(0);
// Release two interrupt functions CdDataCallback() and CdReadyCallback() hooked by CDRead2()
//~ StUnSetRing();
// Put CDROM on pause at current position
//~ CdControlB(CdlPause, 0, 0);
} }
return 0; return 0;
}; }
static void strCheckRGB24() { void checkPad(void)
{
/* From http://psx.arthus.net/sdk/Psy-Q/DOCS/, p.713 u_short pad;
* When playing a movie in 24-bit mode, there is a potential hardware conflict between the CD subsystem static u_short oldPad;
* and the MDEC image decompression system which can result in corrupted data. To avoid this, pad = PadRead(0);
* StCdInterrupt() may defer transferring a sector and instead set a flag variable called StCdInterFlag to
* indicate that a CD sector is ready to be transferred. Once the MDEC is finished transferring data, your if ( pad & PADLleft && !(oldPad & PADLleft) )
* application should check StCdIntrFlag and call StCdInterrupt() directly if it is set. {
*/ // Channel 1 is transition anim, only take input when !transition
#if TRUECOL if(curStr->channel != 1)
extern int StCdIntrFlag; {
// If flag was set ramsyscall_printf("Left: change channel\n");
if ( StCdIntrFlag ) { switchStrCh(&curStr);
// Trigger data transfer oldPad = pad;
StCdInterrupt(); }
// Reset flag }
StCdIntrFlag = 0; if ( !(pad & PADLleft) && oldPad & PADLleft )
} {
#endif oldPad = pad;
}
} }

View File

@ -86,7 +86,7 @@
<!-- Stores system.txt as system.cnf --> <!-- Stores system.txt as system.cnf -->
<file name="system.cnf" type="data" source="system.cnf"/> <file name="system.cnf" type="data" source="system.cnf"/>
<file name="SCES_313.37" type="data" source="hello_str.ps-exe"/> <file name="SCES_313.37" type="data" source="hello_str.ps-exe"/>
<file name="MENU.STR" type="str" source="render/bg_only_mc.str"/> <file name="MENU.STR" type="str" source="render/menu.str"/>
<dummy sectors="1024"/> <dummy sectors="1024"/>

5
render/pack2.scr Normal file
View File

@ -0,0 +1,5 @@
Pack2ch(
z:\home\arthus\build\psxdev\nolibgs_demo\render\bg_only_i.str, FALSE, # Output file name(Fn), Subheader(Sub)
z:\home\arthus\build\psxdev\nolibgs_demo\render\bg_only_1_mc.str, FALSE, 0, 2, # ch0; Fn,sub,TermType,TermLength
z:\home\arthus\build\psxdev\nolibgs_demo\render\bg_only_mc.str, FALSE, 0, 2 # ch1; Fn sub,TermType,TermLength
);

42
render/png2str.sh Executable file
View File

@ -0,0 +1,42 @@
#!/bin/bash
# Render blender files
if [ "$1" == "-b" ] || [ "$1" == "-a" ]; then
echo "Rendering blender files..."
prime-run blender29 -b ../sources/nolibgsdemo-idle_loop.blend -a --cycles-device CUDA+CPU
prime-run blender29 -b ../sources/nolibgsdemo-trans.blend -a --cycles-device CUDA+CPU
# unused
# prime-run blender29 -b ../sources/nolibgsdemo-trans_alpha.blend -a --cycles-device CUDA+CPU
# prime-run blender29 -b ../sources/nolibgsdemo-bg_only.blend -a --cycles-device CUDA+CPU
fi
# Convert PNGs to AVI
if [ "$1" == "-v" ] || [ "$1" == "-a" ]; then
echo "Converting png to avi..."
# idle loop
ffmpeg -r 15 -f image2 -s 320x240 -i idle_loop/idle_loop%04d.png -vcodec rawvideo -pix_fmt bgr24 -vf scale=256:240,setsar=1:1,vflip -an -r 15 idle_loop.avi -y
# transition
ffmpeg -r 15 -f image2 -s 320x240 -i trans/trans%04d.png -vcodec rawvideo -pix_fmt bgr24 -vf scale=256:240,setsar=1:1,vflip -an -r 15 transition.avi -y
# unused
# upper part
# ffmpeg -r 15 -f image2 -s 320x240 -i idle_loop/idle_loop%04d.png -vcodec rawvideo -pix_fmt bgr24 -vf scale=256:240,setsar=1:1,crop=320:144:0:0,vflip -an -r 15 idle_loop_up.avi -y
# down part idle
# ffmpeg -r 15 -f image2 -s 320x240 -i idle_loop/idle_loop%04d.png -vcodec rawvideo -pix_fmt bgr24 -vf scale=320:240,setsar=1:1,crop=320:96:0:144,vflip -an -r 15 idle_loop_down.avi -y
# down part trans-aplpha
# ffmpeg -r 15 -f image2 -s 320x240 -i trans_alpha/trans_alpha%04d.png -vcodec rawvideo -pix_fmt bgr24 -vf scale=320:240,setsar=1:1,crop=320:96:0:140,vflip -an -r 15 trans_alpha_down.avi -y
# bg_only
# ffmpeg -r 15 -f image2 -s 320x240 -i bg_only/bg_only%04d.png -vcodec rawvideo -pix_fmt bgr24 -vf scale=320:240,setsar=1:1,vflip -an -r 15 bg_only.avi -y
# Fix AVI files with mencoder
# mencoder -force-avi-aspect 1.33
rm *_mc.avi
for vid in *.avi;
do mencoder $vid -ovc copy -o ${vid%%.*}_mc.avi;
done
fi
# Convert AVI to STR
if [ "$1" == "-s" ] || [ "$1" == "-a" ]; then
echo "Converting avi to str..."
MC32.EXE -s avi2str.scr
fi

Binary file not shown.

Binary file not shown.

153
src/str.c Normal file
View File

@ -0,0 +1,153 @@
#include "str.h"
CdlFILE STRfile;
// Parameter we want to set the CDROM with
u_char param = CdlModeSpeed;
// Buffers total ~110KB in memory
// SECTOR_SIZE is defined in words : 512 * 4 == 2048 Bytes/sector
// Ring buffer : 32 * 2048 = 65536 Bytes
u_long RingBuff[ RING_SIZE * SECTOR_SIZE ];
// VLC buffers : display area in words (hence /2), 160*320 == 38400 Bytes
u_long VlcBuff[ SCREENXRES / 2 * SCREENYRES ];
// If using 16bpp, fetch 16xYres strips, if 24bpp, fetch 24xYres strips, 5120*PPW Bytes
u_short ImgBuff[ 16 * SCREENYRES];
u_long * curVLCptr = &VlcBuff[0];
u_short * curIMGptr = &ImgBuff[0];
void initSTR(STR * str)
{
DecDCTReset(0);
StSetRing(RingBuff, RING_SIZE);
StSetStream(0, 1, str->length, 0, 0);
if ( CdSearchFile(&STRfile, str->name) == 0 ) {
FntPrint("File not found :%s\n", str->name);
}
CdControl(CdlSetloc, (u_char *)&STRfile.pos, 0);
//CdControl(CdlSetmode, &param, 0);
CdRead2(CdlModeStream|CdlModeSpeed);
}
void resetSTR(STR * str)
{
u_long * curVLCptr = &VlcBuff[0];
u_short * curIMGptr = &ImgBuff[0];
// Set the seek target position
CdControl(CdlSetloc, (u_char *)&STRfile.pos, 0);
// Set CD mode to CdlModeSpeed
CdControl(CdlSetmode, &param, 0);
// Read from CD at position &STRfile.pos
// Enable streaming, double speed and ADPCM playback
CdRead2(CdlModeStream|CdlModeSpeed|CdlModeRT);
str->endPlayback = 0;
}
void switchStrCh(STR ** str)
{
// Switch current STR channel
//~ ramsyscall_printf("p0: %p - %d - ", *str, (*str)->channel);
sectorHeader->frameCount = 0;
*str = &menu[!((*str)->channel)];
//~ ramsyscall_printf("p1: %p\n", *str);
StSetChannel( (*str)->channel );
(*str)->endPlayback = 1;
}
void playSTR(STR ** str)
{
//~ ramsyscall_printf("Frame %d / %d,ch: %d, p: %p, m0: %p, m1: %p\n", sectorHeader->frameCount, (*str)->length, (*str)->channel, str, &menu[0], &menu[1]);
// Use this area to draw the slices
RECT curSlice = { STR_POS_X,
STR_POS_Y,
16,
(*str)->y};
int frameDone = 0;
// Reset counter
int wait = WAIT_TIME;
// Dont try decoding if not data has been loaded from ring buffer
if ( frameAddr ){
// Begin decoding RLE-encoded MDEC image data
DecDCTin( curVLCptr , DCT_MODE);
// Prepare to receive the decoded image data from the MDEC
while (curSlice.x < ( STR_POS_X + (*str)->x ) )
{
// Receive decoded data : a 16*ppw*240 px slice
DecDCTout( (u_long *) curIMGptr, curSlice.w * curSlice.h / 2);
// Wait for transfer end
DecDCToutSync(1);
// Transfer data from main memory to VRAM
LoadImage(&curSlice, (u_long *) curIMGptr );
// Increment drawArea's X with slice width (16 or 24 pix)
curSlice.x += 16;
}
// Set frameDone flag to 1
frameDone = 1;
curSlice.x = STR_POS_X;
curSlice.y = STR_POS_Y;
}
// Get one frame of ring buffer data
// StGetNext is non-blocking, so we wait for it to return 0.
// StGetNext will lock the region at &frameAddr until StFreeRing() is called.
while ( StGetNext((u_long **)&frameAddr,(u_long **)&sectorHeader) )
{
wait--;
if (wait == 0)
break;
}
// Grab a frame from the stream
wait = WAIT_TIME;
while ((nextFrame = frameAddr) == 0)
{
wait--;
if ( wait == 0 ){
break;
}
}
// Decode the Huffman/VLC compressed data
DecDCTvlc(nextFrame, curVLCptr);
// Unlock area obtained by StGetNext()
StFreeRing(nextFrame);
// Reset counter
wait = WAIT_TIME;
// Wait until the whole frame is loaded to VRAM
while ( frameDone == 0 )
{
wait--;
if ( wait == 0 ) {
// If a timeout occurs, force switching buffers
frameDone = 1;
curSlice.x = STR_POS_X;
curSlice.y = STR_POS_Y;
}
}
// If the current frame's number is bigger than the number of frames in STR,
// set the endPlayback flag.
if ( sectorHeader->frameCount >= ((*str)->length - 1) )
{
// If on channel 1, go back to channel 0 after 30 frames
if ((*str)->channel)
{
switchStrCh(str);
} else {
// if on channel 0, set end flag for loop
(*str)->endPlayback = 1;
}
if (!drawMenu)
{
drawMenu = 1;
}
}
if ( (!(*str)->channel) )
{
FntPrint("Hello menu!\n");
if ( sectorHeader->frameCount > 5 )
{
FntFlush(-1);
} else if ( (sectorHeader->frameCount % 2) && sectorHeader->frameCount < 5 )
{
FntFlush(-1);
}
}
}

54
src/str.h Normal file
View File

@ -0,0 +1,54 @@
#pragma once
#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <libgte.h>
#include <libetc.h>
#include <libgpu.h>
// CD library
#include <libcd.h>
// CODEC library
#include <libpress.h>
// printf
#include "../../nolibgs_hello_worlds/thirdparty/nugget/common/syscalls/syscalls.h"
#define SCREENXRES 320
#define SCREENYRES 240
#define STR_POS_X SCREENXRES
#define STR_POS_Y 0
// Ring Buffer size (reduce if flickering occurs)
#define RING_SIZE 16
#define PPW 1
#define DCT_MODE 2
typedef struct STR {
char * name;
uint16_t x,y;
uint16_t length;
uint8_t channel, endPlayback;
} STR;
// 2 channels STR: ch0 = idle loop, ch1 = transition anim
extern STR menu[2];
// CD File descriptor
extern CdlFILE STRfile;
extern StHEADER * sectorHeader;
// Parameter we want to set the CDROM with
extern u_char param;
// Ring buffer : 32 * 2048 = 65536 Bytes
extern u_long RingBuff[ RING_SIZE * SECTOR_SIZE ];
// VLC buffers : display area in words (hence /2), 160*320 == 38400 Bytes
extern u_long VlcBuff[ SCREENXRES / 2 * SCREENYRES ];
// If using 16bpp, fetch 16xYres strips, if 24bpp, fetch 24xYres strips, 5120*PPW Bytes
extern u_short ImgBuff[ 16 * SCREENYRES];
extern u_long * curVLCptr;
extern u_short * curIMGptr;
// Next Ring Buffer Frame address
extern u_long * nextFrame;
// Ring buffer frame address
extern u_long * frameAddr;
extern uint8_t drawMenu;
void initSTR(STR * str);
void resetSTR(STR * str);
void playSTR(STR ** str);
void switchStrCh(STR ** curStr);