diff --git a/.gitignore b/.gitignore index 3ee1a83..8800298 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,8 @@ *.exe +*.js +*.wasm +*.data +*.run *.o Source/LibMultiSpacc Source/LibMultiSpacc/* diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..6fe66b7 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,22 @@ +image: debian:latest + +before_script: | + apt update + apt install -y git + git clone --depth 1 https://gitlab.com/octospacc/MultiSpaccSDK + bash ./MultiSpaccSDK/Tools/InstallBuildEnvironment.sh + mv ./MultiSpaccSDK/LibMultiSpacc/LibMultiSpacc ./Source/LibMultiSpacc + +pages: + stage: deploy + script: | + make -j$(nproc --all) Target=Web + mkdir -p ./public + mv ./Emscripten.* ./public/ + mv ./MultiSpaccSDK/LibMultiSpacc/Examples/Emscripten.html ./public/index.html + artifacts: + paths: + - public + publish: public + rules: + - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH diff --git a/Assets/Blocks.png b/Assets/Blocks.png index 56d5428..d8f9ba9 100644 Binary files a/Assets/Blocks.png and b/Assets/Blocks.png differ diff --git a/Makefile b/Makefile index 69ce229..e64bef6 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ AppName = BloccSpacc -ExeSuffix = .exe +ExeSuffix = .run -Sources = $(wildcard Source/*.c) +Sources = $(wildcard Source/*.c Source/LibMultiSpacc/*.c) CFlags = -O2 CC = gcc $(Defines) @@ -13,6 +13,12 @@ ifdef Target ifeq ($(Target), PC) Defines = -DTarget_PC MultiSpacc_Target = SDL20 + else ifeq ($(Target), Web) + Defines = -DTarget_PC -DTarget_Web + MultiSpacc_Target = Web + AppName = Emscripten + ExeSuffix = .js + CC = emcc $(Defines) else ifeq ($(Target), PocketGo) ExeSuffix = .PocketGo.exe PathPrefix = /opt/miyoo/bin/arm-miyoo-linux-uclibcgnueabi @@ -25,20 +31,26 @@ endif ifeq ($(MultiSpacc_Target), SDL12) Defines += -DMultiSpacc_Target_SDL12 LdFlags += -lSDL -lSDL_image -lSDL_mixer -lSDL_ttf - Sources += $(wildcard Source/LibMultiSpacc/*.c Source/LibMultiSpacc/SDL12/*.c) + Sources += $(wildcard Source/LibMultiSpacc/SDLCom/*.c Source/LibMultiSpacc/SDL12/*.c) else ifeq ($(MultiSpacc_Target), SDL20) Defines += -DMultiSpacc_Target_SDL20 LdFlags += -lSDL2 -lSDL2_image -lSDL2_mixer -lSDL2_ttf - Sources += $(wildcard Source/LibMultiSpacc/*.c Source/LibMultiSpacc/SDL20/*.c) + Sources += $(wildcard Source/LibMultiSpacc/SDLCom/*.c Source/LibMultiSpacc/SDL20/*.c) +else ifeq ($(MultiSpacc_Target), Web) + Defines += -DMultiSpacc_Target_Web -DMultiSpacc_Target_SDL20 -DMultiSpacc_Target_SDLWeb + LdFlags += -sWASM=1 -sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sSDL2_IMAGE_FORMATS='["png"]' -sUSE_SDL_TTF=2 -sUSE_SDL_MIXER=2 --preload-file Assets/Blocks.png --preload-file Assets/Cursorset.png --preload-file Assets/Icon.png --preload-file Assets/LiberationMono-Regular.ttf endif Objects = $(Sources:.c=.o) -all: $(AppName) +all All: $(AppName) $(AppName): $(Objects) $(CC) $^ $(CFlags) $(LdFlags) -o $(AppName)$(ExeSuffix) -clean: +run Run: All + ./$(AppName)$(ExeSuffix) + +clean Clean clear Clear: find -L . -name "*.o" -type f -delete - rm -f $(AppName)$(ExeSuffix) $(AppName).*$(ExeSuffix) + rm -f $(AppName)$(ExeSuffix) $(AppName).*$(ExeSuffix) *.js *.wasm *.data diff --git a/Source/Blocks.h b/Source/Blocks.h index 92c69d7..bcaf645 100644 --- a/Source/Blocks.h +++ b/Source/Blocks.h @@ -1,25 +1,26 @@ #pragma once #include "LibMultiSpacc/MultiSpacc.h" -//#include "SDL/SDL.h" -#define BlocksetNum 6 -#define BlockSize 32 -#define ChunkSize 16 +#define BlocksetNum 8 +#define BlockSize 32 +#define ChunkSize 16 struct Block { - int Id; - char Name[63]; + int Id; + char Name[63]; SDL_Rect Img; - int Light; - bool Falling; - bool Fluid; + int Light; + bool Falling; + bool Fluid; }; struct Block Blocks[BlocksetNum] = { - { 0, "Air", {}, 0, false, false }, - { 1, "White", {}, 0, false, false }, - { 2, "Black", {}, 0, false, false }, - { 3, "Gray", {}, 0, false, false }, - { 4, "Green", {}, 0, false, false }, - { 5, "Blue", {}, 0, false, false }, + { 0, "Air", {}, 0, false, false }, + { 1, "White", {}, 0, false, false }, + { 2, "Black", {}, 0, false, false }, + { 3, "Gray", {}, 0, false, false }, + { 4, "Green", {}, 0, false, false }, + { 5, "Blue", {}, 0, false, false }, + { 6, "Purple", {}, 0, false, false }, + { 7, "Red", {}, 0, false, false }, }; diff --git a/Source/Keys.h b/Source/Keys.h index ed16ee0..aa80f3d 100644 --- a/Source/Keys.h +++ b/Source/Keys.h @@ -1,31 +1,31 @@ #pragma once -#define KeyUp SDLK_UP -#define KeyDown SDLK_DOWN -#define KeyLeft SDLK_LEFT -#define KeyRight SDLK_RIGHT -#define KeyDebug SDLK_F3 +#define KeyUp SDLK_UP +#define KeyDown SDLK_DOWN +#define KeyLeft SDLK_LEFT +#define KeyRight SDLK_RIGHT +#define KeyDebug SDLK_F3 #ifdef Target_PocketGo -#define KeyEsc SDLK_RCTRL // Reset -#define KeyInventory SDLK_ESCAPE // Select -#define KeyGenFlatMap SDLK_LSHIFT // X -#define KeyGenNoiseMap SDLK_SPACE // Y -#define KeyAbove SDLK_LALT // A -#define KeyBelow SDLK_LCTRL // B -#define KeyPlace SDLK_TAB // L -#define KeyBreak SDLK_BACKSPACE // R +#define KeyEsc SDLK_RCTRL // Reset +#define KeyInventory SDLK_ESCAPE // Select +#define KeyGenFlatMap SDLK_LSHIFT // X +#define KeyGenNoiseMap SDLK_SPACE // Y +#define KeyAbove SDLK_LALT // A +#define KeyBelow SDLK_LCTRL // B +#define KeyPlace SDLK_TAB // L +#define KeyBreak SDLK_BACKSPACE // R #else -#define KeyEsc SDLK_ESCAPE -#define KeyInventory SDLK_i -#define KeyGenFlatMap SDLK_F6 -#define KeyGenNoiseMap SDLK_F7 -#define KeyAbove SDLK_LSHIFT -#define KeyBelow SDLK_LCTRL -#define KeyPlace SDLK_z -#define KeyBreak SDLK_x +#define KeyEsc SDLK_ESCAPE +#define KeyInventory SDLK_i +#define KeyGenFlatMap SDLK_F6 +#define KeyGenNoiseMap SDLK_F7 +#define KeyAbove SDLK_LSHIFT +#define KeyBelow SDLK_LCTRL +#define KeyPlace SDLK_z +#define KeyBreak SDLK_x #endif diff --git a/Source/Main.c b/Source/Main.c index 384f258..6310f48 100644 --- a/Source/Main.c +++ b/Source/Main.c @@ -2,29 +2,28 @@ #include #include #include "LibMultiSpacc/MultiSpacc.h" -//#include "SDL/SDL.h" -//#include "SDL/SDL_image.h" -//#include "SDL/SDL_ttf.h" #include "TargetsConfigs.h" #include "Blocks.h" #include "Keys.h" #include "Util.h" +#define swcase break; case + #define AppName "BloccSpacc" MultiSpacc_Window *Window = NULL; MultiSpacc_Surface *Screen = NULL; -SDL_Event Event; +MultiSpacc_Event Event; #define GameTick 30 -SDL_Surface * Cursorset = NULL; +MultiSpacc_Surface *Cursorset = NULL; #define CursorsNum 2 SDL_Rect Cursors[CursorsNum]; -SDL_Surface * BlocksImg = NULL; +MultiSpacc_Surface *BlocksImg = NULL; -SDL_Surface * DebugMsg = NULL; -TTF_Font * DebugFont = NULL; +MultiSpacc_Surface *DebugMsg = NULL; +TTF_Font *DebugFont = NULL; SDL_Color DebugTextColor = { 80, 80, 80 }; int SelectedBlock; @@ -32,16 +31,16 @@ bool InGame, InTitleMenu, InInventory; bool Quit, Recalc, DebugMode; // -static Uint32 NextTickTime; +Uint32 NextTickTime; Uint32 CalcTimeLeft() { Uint32 Now; - Now = SDL_GetTicks(); + Now = MultiSpacc_GetTicks(); if ( NextTickTime <= Now ) { return 0; } else { return NextTickTime - Now; - } -} + }; +}; struct UsedKeys { bool Up, Down, Left, Right, Above, Below; @@ -63,20 +62,21 @@ struct xyz GetBlocksOnScreenNum() { } bool SysInit() { - if ( SDL_Init ( SDL_INIT_EVERYTHING ) != 0 ) { - printf("[E] Error initializing SDL.\n"); - return false; - } - Window = MultiSpacc_SetWindow( ScreenWidth, ScreenHeight, ScreenBits, 0 ); + //if ( MultiSpacc_Init( SDL_INIT_EVERYTHING ) != 0 ) { + // printf("[E] Error initializing Core.\n"); + // return false; + //}; + MultiSpacc_SurfaceConfig windowConfig = {0}; + Window = MultiSpacc_SetWindow( &windowConfig ); Screen = MultiSpacc_GetWindowSurface( Window ); if ( Screen == NULL ) { - printf("[E] Error initializing screen.\n"); + printf("[E] Error initializing Video System.\n"); return false; - } + }; if( TTF_Init() != 0 ) { - printf("[E] Error initializing SDL_TTF.\n"); + printf("[E] Error initializing Font System.\n"); return false; - } + }; MultiSpacc_SetAppTitle( Window, AppName ); MultiSpacc_SetAppIcon( Window, LoadImage( "Assets/Icon.png", Screen ) ); return true; @@ -87,19 +87,19 @@ bool LoadAssets() { Cursorset = LoadImage( "Assets/Cursorset.png", Screen ); if ( Cursorset == NULL ) { Error = true; - } + }; BlocksImg = LoadImage( "Assets/Blocks.png", Screen ); if ( BlocksImg == NULL ) { Error = true; - } + }; DebugFont = TTF_OpenFont( "Assets/LiberationMono-Regular.ttf", 12 ); if ( DebugFont == NULL ) { Error = true; - } + }; if ( Error ) { printf("[E] Error loading assets.\n"); return false; - } + }; return true; } @@ -247,50 +247,54 @@ void DrawCursor() { } } -void DrawDebug() { // There's a memory leak somewhere here - char Str[127]; - - snprintf( Str, sizeof(Str), "CursorPos: x:%d y:%d z:%d", CursorPos.x, CursorPos.y, CursorPos.z ); - DebugMsg = TTF_RenderText_Blended( DebugFont, Str, DebugTextColor ); - DrawSurf( 8, 8, DebugMsg, NULL, Screen ); - - snprintf( Str, sizeof(Str), "Camera: x:%d y:%d z:%d", Camera.x, Camera.y, Camera.z ); - DebugMsg = TTF_RenderText_Blended( DebugFont, Str, DebugTextColor ); - DrawSurf( 8, 20, DebugMsg, NULL, Screen ); - - struct xyz CursorCoords = OrthoToIso ( CursorPos.x, CursorPos.y, CursorPos.z, 1 ); - snprintf( Str, sizeof(Str), "CursorCoords: x:%d y:%d z:%d", CursorCoords.x, CursorCoords.y, CursorCoords.z ); - DebugMsg = TTF_RenderText_Blended( DebugFont, Str, DebugTextColor ); - DrawSurf( 8, 32, DebugMsg, NULL, Screen ); +void DrawString( char Str[], int x, int y, TTF_Font* Font, SDL_Color Color ) { + MultiSpacc_Surface *MsgSurf = TTF_RenderText_Blended( Font, Str, Color ); + DrawSurf( x, y, MsgSurf, NULL, Screen ); + SDL_FreeSurface( MsgSurf ); } -void SetSuperflatMap() { +void DrawDebug() { + char Str[128]; + + snprintf( Str, sizeof(Str), "CursorPos: x:%d y:%d z:%d", CursorPos.x, CursorPos.y, CursorPos.z ); + DrawString( Str, 8, 8, DebugFont, DebugTextColor ); + + snprintf( Str, sizeof(Str), "Camera: x:%d y:%d z:%d", Camera.x, Camera.y, Camera.z ); + DrawString( Str, 8, 20, DebugFont, DebugTextColor ); + + struct xyz CursorCoords = OrthoToIso( CursorPos.x, CursorPos.y, CursorPos.z, 1 ); + snprintf( Str, sizeof(Str), "CursorCoords: x:%d y:%d z:%d", CursorCoords.x, CursorCoords.y, CursorCoords.z ); + DrawString( Str, 8, 32, DebugFont, DebugTextColor ); +} + +void SetSuperflatMap(){ for ( int z = 0; z < ChunksNum.z; z++ ) { for ( int x = 0; x < ChunksNum.x; x++ ) { Map[0][z][x] = 4; - } - } + }; + }; for ( int y = 1; y < ChunksNum.y; y++ ) { for ( int z = 0; z < ChunksNum.z; z++ ) { for ( int x = 0; x < ChunksNum.x; x++ ) { Map[y][z][x] = 0; - } - } - } -} -void SetRandomNoiseMap() { + }; + }; + }; +}; + +void SetRandomNoiseMap(){ for ( int y = 0; y < ChunksNum.y; y++ ) { for ( int z = 0; z < ChunksNum.z; z++ ) { for ( int x = 0; x < ChunksNum.x; x++ ) { int r = rand() % BlocksetNum; if ( r == 1 ) { // Avoid block 1 (all white, hard to see) for testing r = 2; - } + }; Map[y][z][x] = r; - } - } - } -} + }; + }; + }; +}; void GameInit() { for ( int i = 0; i < CursorsNum; i++ ) { @@ -318,95 +322,73 @@ void GameInit() { SelectedBlock = 2; Recalc = true; InGame = true; +}; + +bool GameLoop ( void *args ) { + NextTickTime = MultiSpacc_GetTicks() + GameTick; + while ( MultiSpacc_PollEvent( &Event ) ) { + if ( Event.Type == SDL_QUIT ) { + Quit = true; + } else + if ( Event.Type == SDL_KEYUP ) { + Recalc = true; + switch( Event.Key ){ + swcase KeyUp : UsedKeys.Up = true; + swcase KeyRight : UsedKeys.Right = true; + swcase KeyDown : UsedKeys.Down = true; + swcase KeyLeft : UsedKeys.Left = true; + swcase KeyAbove : UsedKeys.Above = true; + swcase KeyBelow : UsedKeys.Below = true; + swcase KeyPlace : UsedKeys.Place = true; + swcase KeyBreak : UsedKeys.Break = true; + swcase KeyEsc : UsedKeys.Esc = true; + swcase KeyInventory : UsedKeys.Inventory = true; + swcase KeyDebug : DebugMode = !DebugMode; + swcase KeyGenFlatMap : SetSuperflatMap(); + swcase KeyGenNoiseMap : SetRandomNoiseMap(); + }; + }; + }; + EventHandle(); + if ( Recalc ) { + FillSurfRGB ( 0xFF, 0xFF, 0xFF, Screen ); + if ( InGame && !InInventory ) { + SetCamera(); + DrawMap(); + DrawCursor(); + if ( DebugMode ) { + DrawDebug(); + }; + }; + if ( InInventory ) { + DrawInventory(); + }; + if ( !FlipScreen( Window ) ) { + return 1; + }; + Recalc = false; + }; + //MultiSpacc_Sleep( CalcTimeLeft() ); + NextTickTime += GameTick; + return !Quit; } -int main( int argc, char* args[] ) { +int main( int argc, char *args[] ) { printf("[I] Starting!\n"); srand( time( NULL ) ); if ( !SysInit() ) { - printf("[E] Error initializing SDL.\n"); + printf("[E] Error initializing System.\n"); return 1; - } + }; if ( !LoadAssets() ) { printf("[E] Error loading assets.\n"); return 1; - } + }; GameInit(); - - while ( !Quit ) { - NextTickTime = SDL_GetTicks() + GameTick; - while ( SDL_PollEvent( & Event ) ) { - if ( Event.type == SDL_QUIT ) { - Quit = true; - } - else if ( Event.type == SDL_KEYUP ) { - Recalc = true; - if ( Event.key.keysym.sym == KeyEsc ) { - UsedKeys.Esc = true; - } - else if ( Event.key.keysym.sym == KeyDebug ) { - DebugMode = !DebugMode; - } - else if ( Event.key.keysym.sym == KeyInventory ) { - UsedKeys.Inventory = true; - } - else if ( Event.key.keysym.sym == KeyGenFlatMap ) { - SetSuperflatMap(); - } - else if ( Event.key.keysym.sym == KeyGenNoiseMap ) { - SetRandomNoiseMap(); - } - else if ( Event.key.keysym.sym == KeyUp ) { - UsedKeys.Up = true; - } - else if ( Event.key.keysym.sym == KeyRight ) { - UsedKeys.Right = true; - } - else if ( Event.key.keysym.sym == KeyDown ) { - UsedKeys.Down = true; - } - else if ( Event.key.keysym.sym == KeyLeft ) { - UsedKeys.Left = true; - } - else if ( Event.key.keysym.sym == KeyAbove ) { - UsedKeys.Above = true; - } - else if ( Event.key.keysym.sym == KeyBelow ) { - UsedKeys.Below = true; - } - else if ( Event.key.keysym.sym == KeyPlace ) { - UsedKeys.Place = true; - } - else if ( Event.key.keysym.sym == KeyBreak ) { - UsedKeys.Break = true; - } - } - } - EventHandle(); - if ( Recalc ) { - FillSurfRGB ( 0xFF, 0xFF, 0xFF, Screen ); - if ( InGame && !InInventory ) { - SetCamera(); - DrawMap(); - DrawCursor(); - if ( DebugMode ) { - DrawDebug(); - } - } - if ( InInventory ) { - DrawInventory(); - } - if ( !FlipScreen( Window ) ) { - return 1; - } - Recalc = false; - } - SDL_Delay( CalcTimeLeft() ); - NextTickTime += GameTick; - } + MultiSpacc_SetMainLoop( GameLoop, NULL, NULL ); printf("[I] Exiting!\n"); - SDL_Quit(); + //SDL_Quit(); return 0; -} +}; diff --git a/Source/TargetsConfigs.h b/Source/TargetsConfigs.h index 35040c4..8c25c34 100644 --- a/Source/TargetsConfigs.h +++ b/Source/TargetsConfigs.h @@ -1,13 +1,13 @@ #ifdef Target_PocketGo -#define ScreenBits 16 -int ScreenWidth = 320; +#define ScreenBits 16 +int ScreenWidth = 320; int ScreenHeight = 240; #else -#define ScreenBits 32 -int ScreenWidth = 512; +#define ScreenBits 32 +int ScreenWidth = 512; int ScreenHeight = 512; #endif diff --git a/Source/Util.c b/Source/Util.c index e70da23..ab4c726 100644 --- a/Source/Util.c +++ b/Source/Util.c @@ -1,11 +1,9 @@ #include #include "LibMultiSpacc/MultiSpacc.h" -//#include "SDL/SDL.h" -//#include "SDL/SDL_image.h" -SDL_Surface * LoadImage ( char *FilePath, MultiSpacc_Surface *Screen ) { - SDL_Surface *a = NULL; - SDL_Surface *b = NULL; +MultiSpacc_Surface * LoadImage ( char *FilePath, MultiSpacc_Surface *Screen ) { + MultiSpacc_Surface *a = NULL; + MultiSpacc_Surface *b = NULL; a = IMG_Load ( FilePath );///* if ( a == NULL ) { printf("[E] Error reading image %s.\n", FilePath); @@ -22,18 +20,18 @@ SDL_Surface * LoadImage ( char *FilePath, MultiSpacc_Surface *Screen ) { return b;//*/return a; } -void DrawSurf ( int x, int y, SDL_Surface * Src, SDL_Rect * Clip, SDL_Surface * Dst ) { +void DrawSurf ( int x, int y, MultiSpacc_Surface * Src, SDL_Rect * Clip, MultiSpacc_Surface * Dst ) { SDL_Rect Offset; Offset.x = x; Offset.y = y; SDL_BlitSurface( Src, Clip, Dst, &Offset ); } -void FillSurfRGB ( int R, int G, int B, SDL_Surface * Dst ) { +void FillSurfRGB ( int R, int G, int B, MultiSpacc_Surface * Dst ) { SDL_FillRect( Dst, &Dst->clip_rect, SDL_MapRGB( Dst->format, R, G, B ) ); } -void DrawOutlineRect ( int x, int y, int w, int h, int Size, int R, int G, int B, SDL_Surface * Dst ) { +void DrawOutlineRect ( int x, int y, int w, int h, int Size, int R, int G, int B, MultiSpacc_Surface * Dst ) { SDL_Rect Rect; Rect.x = x; Rect.y = y;