diff --git a/LibMultiSpacc/Examples/Common.mk b/LibMultiSpacc/Examples/Common.mk new file mode 100644 index 0000000..c74d49c --- /dev/null +++ b/LibMultiSpacc/Examples/Common.mk @@ -0,0 +1,44 @@ +AppName = $(notdir ${CURDIR}) +Sources = $(wildcard *.c ../../LibMultiSpacc/*.c) +CFlags = -O2 +CC = gcc $(Defines) + +ifndef Target + Target = PC +endif + +ifdef Target + ifeq ($(Target), PC) + ExeSuffix = .run + Defines = -DTarget_PC + MultiSpacc_Target = SDL20 + endif +endif + +ifeq ($(MultiSpacc_Target), SDL12) + Defines += -DMultiSpacc_Target_SDL12 + LdFlags += -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf + Sources += $(wildcard ../../LibMultiSpacc/SDLCom/*.c ../../LibMultiSpacc/SDL12/*.c) +else ifeq ($(MultiSpacc_Target), SDL20) + Defines += -DMultiSpacc_Target_SDL20 + LdFlags += -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf + Sources += $(wildcard ../../LibMultiSpacc/SDLCom/*.c ../../LibMultiSpacc/SDL20/*.c) +endif + +Objects = $(Sources:.c=.o) + +All: $(AppName) + +$(AppName): $(Objects) + $(CC) $^ $(CFlags) $(LdFlags) -o $(AppName)$(ExeSuffix) + +Run: All + ./$(AppName)$(ExeSuffix) + +Clean: + find -L . -name "*.o" -type f -delete + rm -f $(AppName)$(ExeSuffix) $(AppName).*$(ExeSuffix) + +all: All +run: Run +clean: Clean diff --git a/LibMultiSpacc/Examples/HelloWorld/HelloWorld.c b/LibMultiSpacc/Examples/HelloWorld/HelloWorld.c new file mode 100644 index 0000000..3cbc4b3 --- /dev/null +++ b/LibMultiSpacc/Examples/HelloWorld/HelloWorld.c @@ -0,0 +1,46 @@ +#include "../../LibMultiSpacc/MultiSpacc.h" + +#define AppName "Hello World" +#define ScreenWidth 320 +#define ScreenHeight 240 +#define ScreenBits 16 + +int main( int argc, char *args[] ) +{ + MultiSpacc_Window *Window = MultiSpacc_SetWindow( ScreenWidth, ScreenHeight, ScreenBits, 0 ); + MultiSpacc_Surface *Screen = MultiSpacc_GetWindowSurface( Window ); + + if( Screen == NULL ) + { + printf("[E] Error initializing Video System.\n"); + return -1; + }; + + MultiSpacc_SetAppTitle( Window, AppName ); + + // Bitmap font borrowed from: + // Copyright (c) 2018 Doug Fraker www.nesdoug.com (MIT) + MultiSpacc_Surface *TilesImg = MultiSpacc_LoadImage( "Tiles.png", Screen, NULL ); + + const char Text[] = "Hello, World!"; + for(int i = 0; i < sizeof(Text); i++){ + SDL_Rect Offset = { + .x = (8 * i) + (ScreenWidth / sizeof(Text)), + .y = ScreenHeight / 3, + }; + SDL_Rect Clip = { + .x = 8 * ((int)Text[i] % 16), + .y = 8 * ((int)Text[i] / 16), + .w = 8, + .h = 8, + }; + SDL_BlitSurface( TilesImg, &Clip, Screen, &Offset ); + }; + + if( MultiSpacc_UpdateWindowSurface( Window ) != 0 ) { + printf("[E] Error updating Screen.\n"); + }; + + MultiSpacc_Sleep( 3000 ); + return 0; +}; diff --git a/LibMultiSpacc/Examples/HelloWorld/HelloWorld.run b/LibMultiSpacc/Examples/HelloWorld/HelloWorld.run new file mode 100755 index 0000000..8e68411 Binary files /dev/null and b/LibMultiSpacc/Examples/HelloWorld/HelloWorld.run differ diff --git a/LibMultiSpacc/Examples/HelloWorld/Makefile b/LibMultiSpacc/Examples/HelloWorld/Makefile new file mode 100644 index 0000000..9d63a36 --- /dev/null +++ b/LibMultiSpacc/Examples/HelloWorld/Makefile @@ -0,0 +1 @@ +include ../Common.mk diff --git a/LibMultiSpacc/Examples/HelloWorld/Tiles.png b/LibMultiSpacc/Examples/HelloWorld/Tiles.png new file mode 100644 index 0000000..9bcbc13 Binary files /dev/null and b/LibMultiSpacc/Examples/HelloWorld/Tiles.png differ diff --git a/LibMultiSpacc/LibMultiSpacc/MultiSpacc.h b/LibMultiSpacc/LibMultiSpacc/MultiSpacc.h index e981e82..b06d440 100644 --- a/LibMultiSpacc/LibMultiSpacc/MultiSpacc.h +++ b/LibMultiSpacc/LibMultiSpacc/MultiSpacc.h @@ -4,16 +4,21 @@ #ifdef MultiSpacc_Target_SDL12 #include "SDL12/SDL.h" + #include "SDLCom/SDL.h" #endif #ifdef MultiSpacc_Target_SDL20 #include "SDL20/SDL.h" + #include "SDLCom/SDL.h" #endif MultiSpacc_Window *MultiSpacc_SetWindow( int Width, int Height, int Bits, Uint32 Flags ); MultiSpacc_Surface *MultiSpacc_GetWindowSurface( MultiSpacc_Window *Window ); -void MultiSpacc_SetAppTitle( MultiSpacc_Window *Window, const char *Title ); +void MultiSpacc_SetAppTitle( MultiSpacc_Window *Window, const char Title[] ); void MultiSpacc_SetAppIcon( MultiSpacc_Window *Window, MultiSpacc_Surface *Icon ); +MultiSpacc_Surface *MultiSpacc_LoadImage( char FilePath[], MultiSpacc_Surface *Screen, Uint32 *ColorKey ); int MultiSpacc_SetColorKey( MultiSpacc_Surface *Surface, bool Flag, Uint32 Key ); + +int MultiSpacc_PollEvent( MultiSpacc_Event *Event ); diff --git a/LibMultiSpacc/LibMultiSpacc/SDL12/SDL.c b/LibMultiSpacc/LibMultiSpacc/SDL12/SDL.c index 8e852a6..a2eaefd 100644 --- a/LibMultiSpacc/LibMultiSpacc/SDL12/SDL.c +++ b/LibMultiSpacc/LibMultiSpacc/SDL12/SDL.c @@ -3,6 +3,7 @@ #include "SDL/SDL_image.h" #include "SDL/SDL_mixer.h" #include "SDL/SDL_ttf.h" +#include "../SDLCom/SDL.h" MultiSpacc_Window *MultiSpacc_SetWindow( int Width, int Height, int Bits, Uint32 Flags ) { return SDL_SetVideoMode( Width, Height, Bits, Flags ); @@ -19,10 +20,8 @@ void MultiSpacc_SetAppIcon( MultiSpacc_Window *Window, MultiSpacc_Surface *Icon } int MultiSpacc_SetColorKey( MultiSpacc_Surface *Surface, bool Flag, Uint32 Key ) { - if ( Flag ) { + if( Flag ) return SDL_SetColorKey( Surface, SDL_SRCCOLORKEY, Key ); - } - else { + else return SDL_SetColorKey( Surface, 0, Key ); - } -} +}; diff --git a/LibMultiSpacc/LibMultiSpacc/SDL12/SDL.h b/LibMultiSpacc/LibMultiSpacc/SDL12/SDL.h index 3cd5099..0f15a7f 100644 --- a/LibMultiSpacc/LibMultiSpacc/SDL12/SDL.h +++ b/LibMultiSpacc/LibMultiSpacc/SDL12/SDL.h @@ -11,6 +11,9 @@ #include "SDL/SDL_ttf.h" #define MultiSpacc_Window SDL_Surface -#define MultiSpacc_Surface SDL_Surface - #define MultiSpacc_UpdateWindowSurface SDL_Flip + +typedef struct MultiSpacc_Event { + Uint32 Type; + SDLKey Key; +} MultiSpacc_Event; diff --git a/LibMultiSpacc/LibMultiSpacc/SDL20/SDL.c b/LibMultiSpacc/LibMultiSpacc/SDL20/SDL.c index 42e5447..13d14e6 100644 --- a/LibMultiSpacc/LibMultiSpacc/SDL20/SDL.c +++ b/LibMultiSpacc/LibMultiSpacc/SDL20/SDL.c @@ -3,6 +3,7 @@ #include "SDL2/SDL_image.h" #include "SDL2/SDL_mixer.h" #include "SDL2/SDL_ttf.h" +#include "../SDLCom/SDL.h" MultiSpacc_Window *MultiSpacc_SetWindow( int Width, int Height, int Bits, Uint32 Flags ) { return SDL_CreateWindow(NULL, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, Width, Height, Flags); @@ -11,7 +12,7 @@ MultiSpacc_Surface *MultiSpacc_GetWindowSurface( MultiSpacc_Window *Window ) { return SDL_GetWindowSurface( Window ); } -void MultiSpacc_SetAppTitle( MultiSpacc_Window *Window, const char *Title ) { +void MultiSpacc_SetAppTitle( MultiSpacc_Window *Window, const char Title[] ) { SDL_SetWindowTitle( Window, Title ); } void MultiSpacc_SetAppIcon( MultiSpacc_Window *Window, MultiSpacc_Surface *Icon ) { @@ -19,10 +20,8 @@ void MultiSpacc_SetAppIcon( MultiSpacc_Window *Window, MultiSpacc_Surface *Icon } int MultiSpacc_SetColorKey( MultiSpacc_Surface *Surface, bool Flag, Uint32 Key ) { - if ( Flag ) { + if ( Flag ) return SDL_SetColorKey( Surface, SDL_TRUE, Key ); - } - else { + else return SDL_SetColorKey( Surface, SDL_FALSE, Key ); - } -} +}; diff --git a/LibMultiSpacc/LibMultiSpacc/SDL20/SDL.h b/LibMultiSpacc/LibMultiSpacc/SDL20/SDL.h index 7081dd9..342db05 100644 --- a/LibMultiSpacc/LibMultiSpacc/SDL20/SDL.h +++ b/LibMultiSpacc/LibMultiSpacc/SDL20/SDL.h @@ -11,6 +11,9 @@ #include "SDL2/SDL_ttf.h" #define MultiSpacc_Window SDL_Window -#define MultiSpacc_Surface SDL_Surface - #define MultiSpacc_UpdateWindowSurface SDL_UpdateWindowSurface + +typedef struct MultiSpacc_Event { + Uint32 Type; + SDL_Keycode Key; +} MultiSpacc_Event; diff --git a/LibMultiSpacc/LibMultiSpacc/SDLCom/SDL.c b/LibMultiSpacc/LibMultiSpacc/SDLCom/SDL.c new file mode 100644 index 0000000..b4e56d0 --- /dev/null +++ b/LibMultiSpacc/LibMultiSpacc/SDLCom/SDL.c @@ -0,0 +1,33 @@ +#include "../MultiSpacc.h" + +int MultiSpacc_PollEvent( MultiSpacc_Event *Event ) +{ + SDL_Event FromEvent; + int Result = SDL_PollEvent( &FromEvent ); + *Event = (MultiSpacc_Event) { + .Type = FromEvent.type, + .Key = FromEvent.key.keysym.sym, + }; + return Result; +}; + +MultiSpacc_Surface *MultiSpacc_LoadImage( char FilePath[], MultiSpacc_Surface *Screen, Uint32 *ColorKey ) +{ + MultiSpacc_Surface *Final = NULL; + MultiSpacc_Surface *Raw = IMG_Load( FilePath ); + if( Raw == NULL ) { + printf("[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); + } else { + Uint32 FinalColorKey = SDL_MapRGB( Final->format, 0xFF, 0x00, 0xFF ); // Magenta + if( ColorKey != NULL ) + FinalColorKey = *ColorKey; + MultiSpacc_SetColorKey( Final, true, FinalColorKey ); + }; + }; + return Final; +}; diff --git a/LibMultiSpacc/LibMultiSpacc/SDLCom/SDL.h b/LibMultiSpacc/LibMultiSpacc/SDLCom/SDL.h new file mode 100644 index 0000000..53ccd83 --- /dev/null +++ b/LibMultiSpacc/LibMultiSpacc/SDLCom/SDL.h @@ -0,0 +1,8 @@ +#pragma once + +#include "../MultiSpacc.h" + +#define MultiSpacc_Init SDL_Init +#define MultiSpacc_Surface SDL_Surface +#define MultiSpacc_GetTicks SDL_GetTicks +#define MultiSpacc_Sleep SDL_Delay diff --git a/LibMultiSpacc/README.md b/LibMultiSpacc/README.md index a69306f..35c5a5e 100644 --- a/LibMultiSpacc/README.md +++ b/LibMultiSpacc/README.md @@ -7,9 +7,11 @@ 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 (WIP) -- NDS -- GBA -- NES +- SDL 2.0/3.0 (WIP)[^1] +- NDS (Planned) +- GBA (Planned) +- NES (Planned) Example programs (and makefiles) will come soon. + +[^1]: I just discovered that SDL 3.0 exists, I'm so tired, why would they make a new major, now I have to support 3 versions... or just drop 2.0 and only support 1.2 + 3.0.