diff --git a/LibMultiSpacc/Examples/Common.mk b/LibMultiSpacc/Examples/Common.mk index 6f2bf43..cb0fe42 100644 --- a/LibMultiSpacc/Examples/Common.mk +++ b/LibMultiSpacc/Examples/Common.mk @@ -1,6 +1,9 @@ AppName = $(notdir ${CURDIR}) -Sources = $(wildcard *.c ../../LibMultiSpacc/*.c) -CFlags = -O2 -Wpedantic -Werror +AppSources = $(wildcard *.c) +AppHeaders = $(wildcard *.h) +SpaccSources = $(wildcard ../../LibMultiSpacc/*.c) +SpaccHeaders = $(wildcard ../../LibMultiSpacc/*.h) +CFlags = -Os -Wpedantic -Werror # Default build is always for the host system ifndef Target @@ -23,6 +26,9 @@ ifdef Target else ifeq ($(Target), NDS) Defines += -DTarget_NDS MultiSpacc_Target = NDS + else ifeq ($(Target), NES) + Defines += -DTarget_NES + MultiSpacc_Target = NES endif endif @@ -30,21 +36,23 @@ ifeq ($(MultiSpacc_Target), SDL12) Defines += -DMultiSpacc_Target_SDL12 -DMultiSpacc_Target_SDLCom CFlags += $(shell sdl-config --cflags) LdFlags += $(shell sdl-config --libs) -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf - #Sources += $(wildcard ../../LibMultiSpacc/SDLCom/*.c ../../LibMultiSpacc/SDL12/*.c) BuildProcess = Normal else ifeq ($(MultiSpacc_Target), SDL20) Defines += -DMultiSpacc_Target_SDL20 -DMultiSpacc_Target_SDLCom CFlags += $(shell sdl2-config --cflags) LdFlags += $(shell sdl2-config --libs) -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf - #Sources += $(wildcard ../../LibMultiSpacc/SDLCom/*.c ../../LibMultiSpacc/SDL20/*.c) BuildProcess = Normal else ifeq ($(MultiSpacc_Target), NDS) Defines += -DMultiSpacc_Target_NDS BuildProcess = NDS +else ifeq ($(MultiSpacc_Target), NES) + Defines += -DMultiSpacc_Target_NES + BuildProcess = NES endif CC = $(ToolsPrefix)gcc $(CFlags) $(Defines) -Objects = $(Sources:.c=.o) +BuildSources = $(AppSources) $(SpaccSources) +Objects = $(BuildSources:.c=.o) All all: $(BuildProcess) @@ -55,18 +63,17 @@ NDS: $(eval VirtualBuildDir = ./Build/NDS) mkdir -p $(VirtualBuildDir)/source/.tmp cp ../NDS.mk $(VirtualBuildDir)/Makefile - cp $(Sources) $(VirtualBuildDir)/source/ - cp $(wildcard ../../LibMultiSpacc/*.*) $(VirtualBuildDir)/source/.tmp/ - # rm of the last file is temporary fix for a strange file duplication bug - cd $(VirtualBuildDir)/source/.tmp; for i in *; do mv $$i ../LibMultiSpacc_$$i; done; rm ../MultiSpacc.c # - #cp $(wildcard ../../LibMultiSpacc/NDS/*.*) $(VirtualBuildDir)/source/.tmp/ - #cd $(VirtualBuildDir)/source/.tmp; for i in *; do mv $$i ../LibMultiSpacc_NDS_$$i; done + cp $(SpaccSources) $(SpaccHeaders) $(VirtualBuildDir)/source/.tmp/ + cd $(VirtualBuildDir)/source/.tmp; for i in *; do mv ./$$i ../LibMultiSpacc_$$i; done + cp $(AppSources) $(AppHeaders) $(VirtualBuildDir)/source/ for i in $(VirtualBuildDir)/source/*; do sed -i 's|#include[ \t]"../../LibMultiSpacc/|#include "LibMultiSpacc_|g' $$i; done for i in $(VirtualBuildDir)/source/*; do sed -i 's|#include[ \t]"../MultiSpacc|#include "LibMultiSpacc_MultiSpacc|g' $$i; done - for i in $(VirtualBuildDir)/source/*; do sed -i 's|#include[ \t]"NDS/|#include "LibMultiSpacc_NDS_|g' $$i; done for i in $(VirtualBuildDir)/source/*; do sed -i 's|#include[ \t]"./|#include "./LibMultiSpacc_|g' $$i; done cd $(VirtualBuildDir); make +NES: + # + Run run: All ./$(AppName)$(ExeSuffix) diff --git a/LibMultiSpacc/Examples/HelloWorld/HelloWorld.c b/LibMultiSpacc/Examples/HelloWorld/HelloWorld.c index 758efc9..bba4f0c 100644 --- a/LibMultiSpacc/Examples/HelloWorld/HelloWorld.c +++ b/LibMultiSpacc/Examples/HelloWorld/HelloWorld.c @@ -4,6 +4,11 @@ int main( int argc, char *argv[] ) { + int spriteX = 0; + int spriteY = 0; + int accelX = +2; + int accelY = +2; + MultiSpacc_SurfaceConfig WindowConfig = { .Width = 320, .Height = 240, .Bits = 16 }; MultiSpacc_Window *Window = MultiSpacc_SetWindow( WindowConfig ); MultiSpacc_Surface *Screen = MultiSpacc_GetWindowSurface( Window ); @@ -20,13 +25,41 @@ int main( int argc, char *argv[] ) // Copyright (c) 2018 Doug Fraker www.nesdoug.com (MIT) MultiSpacc_Surface *TilesImg = MultiSpacc_LoadImage( "Tiles.png", Screen, NULL ); MultiSpacc_PrintText( "Hello, World!", Screen, WindowConfig, 0, 0, TilesImg ); + MultiSpacc_PrintDebug("[I] Ready!\n"); - if( MultiSpacc_UpdateWindowSurface( Window ) != 0 ) + // if( MultiSpacc_UpdateWindowSurface(Window) != 0 ) + // { + // MultiSpacc_PrintDebug("[E] Error Updating Screen.\n"); + // return -1; + // }; + + while(true) { - MultiSpacc_PrintDebug("[E] Error Updating Screen.\n"); - return -1; - }; + MultiSpacc_Sprite( 0, spriteX, spriteY, 0x80, TilesImg, Screen ); - MultiSpacc_Sleep( 4000 ); + spriteX += accelX; + spriteY += accelY; + + if( spriteX >= WindowConfig.Width ) + { + spriteX = 0; + } + + if( spriteY == 0 || spriteY == (WindowConfig.Height - 8) ) + { + accelY *= -1; + } + + if( MultiSpacc_UpdateWindowSurface(Window) != 0 ) + { + MultiSpacc_PrintDebug("[E] Error Updating Screen.\n"); + return -1; + }; + + // TODO: Implement cross-platform vblank-wait + MultiSpacc_Sleep(16); + } + + //MultiSpacc_Sleep(4000); return 0; } diff --git a/LibMultiSpacc/Examples/HelloWorld/Tiles.png b/LibMultiSpacc/Examples/HelloWorld/Tiles.png index 9bcbc13..80c3c5a 100644 Binary files a/LibMultiSpacc/Examples/HelloWorld/Tiles.png and b/LibMultiSpacc/Examples/HelloWorld/Tiles.png differ diff --git a/LibMultiSpacc/LibMultiSpacc/MultiSpacc.c b/LibMultiSpacc/LibMultiSpacc/MultiSpacc.c index f06b9b9..d0b7611 100644 --- a/LibMultiSpacc/LibMultiSpacc/MultiSpacc.c +++ b/LibMultiSpacc/LibMultiSpacc/MultiSpacc.c @@ -1,20 +1,6 @@ #include "./MultiSpacc.h" #ifdef MultiSpacc_Target_SDL12 - MultiSpacc_Window *MultiSpacc_SetWindow( MultiSpacc_SurfaceConfig WindowConfig ) { - return SDL_SetVideoMode( WindowConfig.Width, WindowConfig.Height, WindowConfig.Bits, WindowConfig.Flags ); - } - MultiSpacc_Surface *MultiSpacc_GetWindowSurface( MultiSpacc_Window *Window ) { - return Window; - } - - void MultiSpacc_SetAppTitle( MultiSpacc_Window *Window, const char *Title ) { - SDL_WM_SetCaption( Title, NULL ); - } - void MultiSpacc_SetAppIcon( MultiSpacc_Window *Window, MultiSpacc_Surface *Icon ) { - SDL_WM_SetIcon( Icon, NULL ); - } - int MultiSpacc_SetColorKey( MultiSpacc_Surface *Surface, bool Flag, Uint32 Key ) { if( Flag ) return SDL_SetColorKey( Surface, SDL_SRCCOLORKEY, Key ); @@ -24,20 +10,6 @@ #endif #ifdef MultiSpacc_Target_SDL20 - MultiSpacc_Window *MultiSpacc_SetWindow( MultiSpacc_SurfaceConfig WindowConfig ) { - return SDL_CreateWindow( NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WindowConfig.Width, WindowConfig.Height, WindowConfig.Flags ); - } - MultiSpacc_Surface *MultiSpacc_GetWindowSurface( MultiSpacc_Window *Window ) { - return SDL_GetWindowSurface( Window ); - } - - void MultiSpacc_SetAppTitle( MultiSpacc_Window *Window, const char Title[] ) { - SDL_SetWindowTitle( Window, Title ); - } - void MultiSpacc_SetAppIcon( MultiSpacc_Window *Window, MultiSpacc_Surface *Icon ) { - SDL_SetWindowIcon( Window, Icon ); - } - int MultiSpacc_SetColorKey( MultiSpacc_Surface *Surface, bool Flag, Uint32 Key ) { if ( Flag ) { return SDL_SetColorKey( Surface, SDL_TRUE, Key ); @@ -64,12 +36,12 @@ MultiSpacc_Surface *Final = NULL; MultiSpacc_Surface *Raw = IMG_Load( FilePath ); if( Raw == NULL ) { - printf("[E] Error reading image %s.\n", FilePath); + MultiSpacc_PrintDebug("[E] Error reading image %s.\n", FilePath); } else { Final = SDL_ConvertSurface( Raw, Screen->format, 0 ); SDL_FreeSurface( Raw ); if( Final == NULL ) { - printf("[E] Error adapting image %s.\n", FilePath); + MultiSpacc_PrintDebug("[E] Error adapting image %s.\n", FilePath); } else { Uint32 FinalColorKey = SDL_MapRGB( Final->format, 0xFF, 0x00, 0xFF ); // Magenta if( ColorKey != NULL ) @@ -79,65 +51,14 @@ }; return Final; } - - void MultiSpacc_PrintText( char Text[], MultiSpacc_Surface *Surface, MultiSpacc_SurfaceConfig WindowConfig, int x, int y, MultiSpacc_Surface *Tiles /*, int FontSize, int Color */ ) - { - for( int i = 0; i < strlen( Text ); i++ ) - { - MultiSpacc_Rect Offset = { - .x = (x * 8) + (8 * i), - .y = (y * 8), - }; - MultiSpacc_Rect Clip = { - .x = 8 * (Text[i] % 16), - .y = 8 * (Text[i] / 16), - .w = 8, - .h = 8, - }; - SDL_BlitSurface( Tiles, &Clip, Surface, &Offset ); - }; - } - - void MultiSpacc_PrintDebug( const char *format, ... ) - { - va_list args; - va_start(args, format); - fprintf(stderr, format, args); - va_end(args); - } #endif #ifdef MultiSpacc_Target_NDS - MultiSpacc_Window *MultiSpacc_SetWindow( MultiSpacc_SurfaceConfig WindowConfig ) - { - PrintConsole *bottomScreen = NULL; - - videoSetModeSub(MODE_0_2D); - vramSetBankC(VRAM_C_SUB_BG); - - bottomScreen = consoleInit(bottomScreen, 3, BgType_Text4bpp, BgSize_T_256x256, 31, 0, false, true); - consoleSelect(bottomScreen); - return bottomScreen; - } - - MultiSpacc_Surface *MultiSpacc_GetWindowSurface( MultiSpacc_Window *Window ) - { - return Window; - } - - void MultiSpacc_SetAppTitle( MultiSpacc_Window *Window, const char Title[] ){} - void MultiSpacc_PrintDebug( const char *format, ... ){} - int MultiSpacc_UpdateWindowSurface( MultiSpacc_Window *Window ) { return 0; } - void MultiSpacc_PrintText( char Text[], MultiSpacc_Surface *Surface, MultiSpacc_SurfaceConfig WindowConfig, int x, int y, MultiSpacc_Surface *Tiles /*, int FontSize, int Color */ ) - { - iprintf(Text); - } - void MultiSpacc_Sleep( int milliseconds ) { int frames = (60 * milliseconds / 1000); @@ -152,3 +73,17 @@ return NULL; }; #endif + +void MultiSpacc_Sprite( int id, int x, int y, int sprite, MultiSpacc_Surface *Tiles, MultiSpacc_Surface *Surface ) +{ + #ifdef MultiSpacc_Target_SDLCom + MultiSpacc_Rect Offset = { .x = x, .y = y, }; + MultiSpacc_Rect Clip = { + .x = (8 * (sprite % 16)), + .y = (8 * (sprite / 16)), + .w = 8, + .h = 8, + }; + SDL_BlitSurface( Tiles, &Clip, Surface, &Offset ); + #endif +} diff --git a/LibMultiSpacc/LibMultiSpacc/MultiSpacc.h b/LibMultiSpacc/LibMultiSpacc/MultiSpacc.h index e389e01..7196fb9 100644 --- a/LibMultiSpacc/LibMultiSpacc/MultiSpacc.h +++ b/LibMultiSpacc/LibMultiSpacc/MultiSpacc.h @@ -51,6 +51,11 @@ void MultiSpacc_Sleep( int milliseconds ); #endif +#ifdef MultiSpacc_Target_NES + #include + #include "neslib.h" +#endif + typedef struct MultiSpacc_SurfaceConfig { int Width; int Height; @@ -71,3 +76,5 @@ int MultiSpacc_PollEvent( MultiSpacc_Event *Event ); void MultiSpacc_PrintDebug( const char *format, ... ); void MultiSpacc_PrintText( char Text[], MultiSpacc_Surface *Surface, MultiSpacc_SurfaceConfig WindowConfig, int x, int y, MultiSpacc_Surface *Tiles /*, int FontSize, int Color */ ); // WIP + +void MultiSpacc_Sprite( int id, int x, int y, int sprite, MultiSpacc_Surface *Tiles, MultiSpacc_Surface *Surface ); diff --git a/LibMultiSpacc/LibMultiSpacc/Print.c b/LibMultiSpacc/LibMultiSpacc/Print.c new file mode 100644 index 0000000..480f0d0 --- /dev/null +++ b/LibMultiSpacc/LibMultiSpacc/Print.c @@ -0,0 +1,35 @@ +#include "./MultiSpacc.h" + +void MultiSpacc_PrintText( char Text[], MultiSpacc_Surface *Surface, MultiSpacc_SurfaceConfig WindowConfig, int x, int y, MultiSpacc_Surface *Tiles /*, int FontSize, int Color */ ) +{ + #ifdef MultiSpacc_Target_SDLCom + for( int i = 0; i < strlen(Text); i++ ) + { + MultiSpacc_Rect Offset = { + .x = (x * 8) + (8 * i), + .y = (y * 8), + }; + MultiSpacc_Rect Clip = { + .x = 8 * (Text[i] % 16), + .y = 8 * (Text[i] / 16), + .w = 8, + .h = 8, + }; + SDL_BlitSurface( Tiles, &Clip, Surface, &Offset ); + }; + #endif + + #ifdef MultiSpacc_Target_NDS + iprintf("%s", Text); + #endif +} + +void MultiSpacc_PrintDebug( const char *format, ... ) +{ + #ifdef MultiSpacc_Target_SDLCom + va_list args; + va_start(args, format); + fprintf(stderr, format, args); + va_end(args); + #endif +} diff --git a/LibMultiSpacc/LibMultiSpacc/Setup.c b/LibMultiSpacc/LibMultiSpacc/Setup.c new file mode 100644 index 0000000..36d347b --- /dev/null +++ b/LibMultiSpacc/LibMultiSpacc/Setup.c @@ -0,0 +1,56 @@ +#include "./MultiSpacc.h" + +MultiSpacc_Window *MultiSpacc_SetWindow( MultiSpacc_SurfaceConfig WindowConfig ) +{ + #ifdef MultiSpacc_Target_SDL12 + return SDL_SetVideoMode( WindowConfig.Width, WindowConfig.Height, WindowConfig.Bits, WindowConfig.Flags ); + #endif + + #ifdef MultiSpacc_Target_SDL20 + return SDL_CreateWindow( NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WindowConfig.Width, WindowConfig.Height, WindowConfig.Flags ); + #endif + + #ifdef MultiSpacc_Target_NDS + PrintConsole *bottomScreen = NULL; + + videoSetModeSub(MODE_0_2D); + vramSetBankC(VRAM_C_SUB_BG); + + bottomScreen = consoleInit(bottomScreen, 3, BgType_Text4bpp, BgSize_T_256x256, 31, 0, false, true); + consoleSelect(bottomScreen); + return bottomScreen; + #endif +} + +MultiSpacc_Surface *MultiSpacc_GetWindowSurface( MultiSpacc_Window *Window ) +{ + #ifdef MultiSpacc_Target_SDL12 + return Window; + #endif + #ifdef MultiSpacc_Target_SDL20 + return SDL_GetWindowSurface(Window); + #endif + #ifdef MultiSpacc_Target_NDS + return Window; + #endif +} + +void MultiSpacc_SetAppTitle( MultiSpacc_Window *Window, const char *Title ) +{ + #ifdef MultiSpacc_Target_SDL12 + SDL_WM_SetCaption( Title, NULL ); + #endif + #ifdef MultiSpacc_Target_SDL20 + SDL_SetWindowTitle( Window, Title ); + #endif +} + +void MultiSpacc_SetAppIcon( MultiSpacc_Window *Window, MultiSpacc_Surface *Icon ) +{ + #ifdef MultiSpacc_Target_SDL12 + SDL_WM_SetIcon( Icon, NULL ); + #endif + #ifdef MultiSpacc_Target_SDL20 + SDL_SetWindowIcon( Window, Icon ); + #endif +} diff --git a/LibMultiSpacc/README.md b/LibMultiSpacc/README.md index 9a3ba22..ad7120d 100644 --- a/LibMultiSpacc/README.md +++ b/LibMultiSpacc/README.md @@ -7,10 +7,10 @@ The idea is simple: to build an universal abstraction layer on top of other exis The list of supported (or planned) backend libraries follows: - SDL 1.2 (WIP) -- SDL 2.0/3.0 (WIP)[^1] +- SDL 2.0/3.0 (WIP/?)[^1] - NDS (WIP) - GBA (Planned) -- NES (Planned) +- NES (WIP) SDL is used as the main cross-platform library (covering many old systems with v1.2, and all modern PC, embedded, and virtualized systems with v2.0). Specific platform that require special code are handled separately via other base abstraction layers.