mirror of
https://gitlab.com/octospacc/MultiSpaccSDK
synced 2025-06-05 22:09:21 +02:00
WIP Browser example; WIP init API; Partial update keys API
This commit is contained in:
133
LibMultiSpacc/Examples/Browser/Browser.c
Normal file
133
LibMultiSpacc/Examples/Browser/Browser.c
Normal file
@ -0,0 +1,133 @@
|
||||
#include "../../LibMultiSpacc/MultiSpacc.h"
|
||||
#include <dirent.h>
|
||||
|
||||
#define AppName "Browser"
|
||||
#define TilesImgFile "./Assets/CHARS.png"
|
||||
|
||||
#define TileSize 8
|
||||
#define ScreenTilesH (windowConfig.width / TileSize)
|
||||
|
||||
MultiSpacc_KeysStates buttonsStates;
|
||||
MultiSpacc_SurfaceConfig windowConfig = {0};
|
||||
MultiSpacc_Window *window;
|
||||
MultiSpacc_Surface *screen;
|
||||
MultiSpacc_Surface *tilesImg;
|
||||
|
||||
struct dirent *de;
|
||||
DIR *dr;
|
||||
|
||||
bool TryReadDirectory( char path[] )
|
||||
{
|
||||
int i = 0;
|
||||
char previousPath[1024];
|
||||
char workingPath[1024];
|
||||
char displayPath[ScreenTilesH+1];
|
||||
|
||||
memset( displayPath, 0, ScreenTilesH+1 );
|
||||
|
||||
if( dr != NULL ){
|
||||
closedir(dr);
|
||||
}
|
||||
|
||||
if( (dr = opendir(path)) != NULL ){
|
||||
strcpy( previousPath, path );
|
||||
}
|
||||
else{
|
||||
dr = opendir(previousPath);
|
||||
}
|
||||
|
||||
if( strlen(getcwd( workingPath, sizeof(workingPath) )) > ScreenTilesH )
|
||||
{
|
||||
strncpy( displayPath, "...", 3 );
|
||||
i = 3;
|
||||
}
|
||||
|
||||
// TODO: fix bug, if path is too short we have parts of it to the right
|
||||
while( workingPath[i] )
|
||||
{
|
||||
if( strlen(workingPath) - ScreenTilesH <= i-3 )
|
||||
{
|
||||
strcat( displayPath, &workingPath[i] );
|
||||
}
|
||||
++i;
|
||||
}
|
||||
|
||||
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0, 0, 0 ) );
|
||||
MultiSpacc_PrintText( displayPath, screen, &windowConfig, 0, 0, tilesImg );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool RealUpdate( void *args, double deltaTime )
|
||||
{
|
||||
int entriesCount = 0;
|
||||
|
||||
MultiSpacc_PollButtons( 0, &buttonsStates );
|
||||
|
||||
// TODO: show cursor, allow moving it in list and to select entries
|
||||
if( MultiSpacc_CheckKeyPress( MultiSpacc_Key_Confirm, &buttonsStates ) )
|
||||
{
|
||||
chdir("..");
|
||||
TryReadDirectory(".");
|
||||
}
|
||||
else if( MultiSpacc_CheckKeyPress( MultiSpacc_Key_Up, &buttonsStates ) )
|
||||
{
|
||||
|
||||
}
|
||||
else if( MultiSpacc_CheckKeyPress( MultiSpacc_Key_Down, &buttonsStates ) )
|
||||
{
|
||||
|
||||
}
|
||||
// TODO: listen for OS terminate signal
|
||||
else if( MultiSpacc_CheckKeyPress( MultiSpacc_Key_Pause, &buttonsStates ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
de = readdir(dr); // "delete" first entry (".", meaning current directory) from list
|
||||
while ( (de = readdir(dr)) != NULL )
|
||||
{
|
||||
MultiSpacc_PrintText( de->d_name, screen, &windowConfig, 2, 2+i, tilesImg );
|
||||
++i;
|
||||
}
|
||||
// TODO: handle directories with more entries than screen height
|
||||
|
||||
if( !MultiSpacc_UpdateDisplay(window) )
|
||||
{
|
||||
MultiSpacc_PrintDebug("[E] Error Updating Screen.\n");
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
if( !MultiSpacc_InitSystem() )
|
||||
{
|
||||
MultiSpacc_PrintDebug("[E] Error Initializing System Core.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
window = MultiSpacc_SetWindow( &windowConfig );
|
||||
screen = MultiSpacc_GetWindowSurface( window );
|
||||
if( window == NULL || screen == NULL )
|
||||
{
|
||||
MultiSpacc_PrintDebug("[E] Error Initializing Video System.\n");
|
||||
return -1;
|
||||
};
|
||||
|
||||
// Bitmap font forked from: <https://github.com/nesdoug/01_Hello/blob/master/Alpha.chr>
|
||||
// Original copyright (c) 2018 Doug Fraker www.nesdoug.com (MIT)
|
||||
tilesImg = MultiSpacc_LoadImage( TilesImgFile, screen, NULL );
|
||||
if( tilesImg == NULL )
|
||||
{
|
||||
MultiSpacc_PrintDebug("[E] Error Loading tilesImg (%s).\n", TilesImgFile);
|
||||
return -1;
|
||||
}
|
||||
|
||||
TryReadDirectory(".");
|
||||
MultiSpacc_SetAppTitle( window, AppName );
|
||||
MultiSpacc_PrintDebug("[I] Ready!\n");
|
||||
return MultiSpacc_SetMainLoop( NULL, RealUpdate, NULL );
|
||||
}
|
1
LibMultiSpacc/Examples/Browser/Makefile
Normal file
1
LibMultiSpacc/Examples/Browser/Makefile
Normal file
@ -0,0 +1 @@
|
||||
include ../Common.mk
|
18
LibMultiSpacc/Examples/Clock/Clock.c
Normal file
18
LibMultiSpacc/Examples/Clock/Clock.c
Normal file
@ -0,0 +1,18 @@
|
||||
#include "../../LibMultiSpacc/MultiSpacc.h"
|
||||
|
||||
#define AppName "Clock"
|
||||
|
||||
|
||||
|
||||
int main( int argc, char *argv[] )
|
||||
{
|
||||
// ... do setup things
|
||||
|
||||
if( !MultiSpacc_SystemClockAvailable )
|
||||
{
|
||||
// ... make the user set time in-app
|
||||
}
|
||||
|
||||
// ... retrieve system time by common API or keep a clock manually
|
||||
// ... display and update a clock
|
||||
}
|
1
LibMultiSpacc/Examples/Clock/Makefile
Normal file
1
LibMultiSpacc/Examples/Clock/Makefile
Normal file
@ -0,0 +1 @@
|
||||
include ../Common.mk
|
@ -88,6 +88,8 @@ int main( int argc, char *argv[] )
|
||||
windowConfig.height = 240;
|
||||
memcpy( windowConfig.palette, palette, 32 );
|
||||
|
||||
MultiSpacc_InitSystem();
|
||||
// TODO: integrate into LibMultiSpacc and make cross-platform to change into current directory
|
||||
//romfsInit();
|
||||
//chdir("romfs:/");
|
||||
|
||||
@ -109,6 +111,7 @@ int main( int argc, char *argv[] )
|
||||
margs.tilesImg = MultiSpacc_LoadImage( "./Assets/CHARS.png", margs.background, NULL );
|
||||
if( margs.tilesImg == NULL )
|
||||
{
|
||||
MultiSpacc_PrintDebug("[E] Error Loading tilesImg (%s).\n", TilesImgFile);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -1,10 +1,10 @@
|
||||
#include "../../LibMultiSpacc/MultiSpacc.h"
|
||||
|
||||
#define AppName "Pong"
|
||||
|
||||
#define iabs(x) (((x) >= 0) ? (x) : -(x))
|
||||
#define maxOf2(a, b) (((a + b) / 2) + (iabs(a - b) / 2))
|
||||
|
||||
#define AppName "Pong"
|
||||
|
||||
bool paused = false;
|
||||
int scoreSx = 0;
|
||||
int scoreDx = 0;
|
||||
@ -322,6 +322,7 @@ int main( int argc, char *argv[] )
|
||||
windowConfig.height = 240;
|
||||
memcpy( windowConfig.palette, palette, 32 );
|
||||
|
||||
MultiSpacc_InitSystem();
|
||||
window = MultiSpacc_SetWindow( &windowConfig );
|
||||
screen = MultiSpacc_GetWindowSurface( window );
|
||||
background = MultiSpacc_CreateSurface( &windowConfig );
|
||||
@ -332,13 +333,13 @@ int main( int argc, char *argv[] )
|
||||
};
|
||||
|
||||
MultiSpacc_SetAppTitle( window, AppName );
|
||||
MultiSpacc_PrintDebug("[I] Ready!\n");
|
||||
|
||||
// Bitmap font forked from: <https://github.com/nesdoug/01_Hello/blob/master/Alpha.chr>
|
||||
// Original copyright (c) 2018 Doug Fraker www.nesdoug.com (MIT)
|
||||
tilesImg = MultiSpacc_LoadImage( "./Assets/CHARS.png", screen, NULL );
|
||||
if( tilesImg == NULL )
|
||||
{
|
||||
MultiSpacc_PrintDebug("[E] Error Loading tilesImg (%s).\n", TilesImgFile);
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -347,5 +348,6 @@ int main( int argc, char *argv[] )
|
||||
paddleSxY = windowConfig.height/2 - PaddleHeightPx;
|
||||
paddleDxY = windowConfig.height/2 - PaddleHeightPx;
|
||||
|
||||
MultiSpacc_PrintDebug("[I] Ready!\n");
|
||||
return MultiSpacc_SetMainLoop( FixedUpdate, RealUpdate, NULL );
|
||||
}
|
||||
|
Reference in New Issue
Block a user