diff --git a/.gitignore b/.gitignore index d493eb1..0817540 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ /libboids.so /game.fasl /writeimage +*.fasl diff --git a/c-version/Makefile b/c-version/Makefile index f997825..e7241d6 100644 --- a/c-version/Makefile +++ b/c-version/Makefile @@ -5,11 +5,8 @@ CC=gcc all: main dod boids_main -main: main.c lib.h sprites.o sprites.h game_data.h ./lib/libraylib.a - $(CC) $(CFLAGS) -Iinclude/ -lm main.c -o main sprites.o ./lib/libraylib.a - -sprites.o: sprites.c sprites.h lib.h - $(CC) $(CFLAGS) -c sprites.c -o sprites.o +main: main.c lib.h sprites.h game_data.h ./lib/libraylib.a Makefile + $(CC) $(CFLAGS) -Iinclude/ -lm main.c -o main ./lib/libraylib.a dod: dod.c ./lib/libraylib.a $(CC) $(CFLAGS) -Iinclude/ -lm dod.c -o dod ./lib/libraylib.a diff --git a/c-version/base.c b/c-version/base.c deleted file mode 100644 index 6304ccc..0000000 --- a/c-version/base.c +++ /dev/null @@ -1,29 +0,0 @@ -#include "include/raylib.h" -#include "include/raymath.h" -#include -#include "lib.h" - -#define SCREEN_WIDTH 1300 -#define SCREEN_HEIGHT 1000 -#define TARGET_FPS 60 - - -int main(void) { - InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Boids"); - - SetTargetFPS(TARGET_FPS); - - while (!WindowShouldClose()) { - float dt = GetFrameTime(); - - // Update - - BeginDrawing(); - { - ClearBackground(RAYWHITE); - } - EndDrawing(); - } - - CloseWindow(); -} diff --git a/c-version/lib.h b/c-version/lib.h index 06f44e5..0b211ae 100644 --- a/c-version/lib.h +++ b/c-version/lib.h @@ -60,6 +60,34 @@ inline void D_DrawTextureV(Texture2D texture, Vector2 position, Color tint) { #endif } +// Vector2 operations +#define v2_add Vector2Add +#define v2_sub Vector2Subtract +#define v2_mul Vector2Scale +#define v2_div Vector2Divide +#define v2_dot Vector2DotProduct +#define v2_len Vector2Length +#define v2_norm Vector2Normalize +#define v2_dist Vector2Distance +#define v2_lerp Vector2Lerp + +// Vector3 operations +#define v3_add Vector3Add +#define v3_sub Vector3Subtract +#define v3_mul Vector3Scale +#define v3_div Vector3Divide +#define v3_dot Vector3DotProduct +#define v3_cross Vector3CrossProduct +#define v3_len Vector3Length +#define v3_norm Vector3Normalize +#define v3_dist Vector3Distance +#define v3_lerp Vector3Lerp + +// Constructor-style macros +#define v2(x, y) ((Vector2){x, y}) +#define v3(x, y, z) ((Vector3){x, y, z}) + + inline void D_DrawTextureRec(Texture2D texture, Rectangle source, Vector2 position, Color tint) { DrawTextureRec(texture, source, position, tint); #ifdef DEBUG_MODE_ENABLED diff --git a/c-version/main.c b/c-version/main.c index fb252cc..f8f304c 100644 --- a/c-version/main.c +++ b/c-version/main.c @@ -6,7 +6,7 @@ #define TEXTURES_BUF_SIZE 16 #define TARGET_FPS 60 -#define MAX_KNIGHTS 10000 +#define MAX_KNIGHTS 5000 #define SCREEN_WIDTH 1300 #define SCREEN_HEIGHT 1080 @@ -75,7 +75,7 @@ Assets Init() { Assets assets = {0}; assets.textures = malloc(sizeof(Texture2D) * TEXTURES_BUF_SIZE); - assets.textures[TEX_GROUND] = LoadTexture(../"assets/Terrain/Ground/Tilemap_Flat.png"); + assets.textures[TEX_GROUND] = LoadTexture("../assets/Terrain/Ground/Tilemap_Flat.png"); assets.textures[TEX_KNIGHT] = LoadTexture("../assets/Factions/Knights/Troops/Warrior/Blue/Warrior_Blue.png"); assets.textures[TEX_MOUSE_CURSOR] = LoadTexture("../assets/UI/Pointers/01.png"); @@ -349,6 +349,7 @@ int main(void) { printf("Direction Size: %ld \n", sizeof(Direction)); printf("KnightState Size: %ld \n", sizeof(KnightState)); + SetTraceLogLevel(4); InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Tiny Knights"); int monitor = GetCurrentMonitor(); diff --git a/c-version/sprites.c b/c-version/sprites.c deleted file mode 100644 index 02143c7..0000000 --- a/c-version/sprites.c +++ /dev/null @@ -1,31 +0,0 @@ -#include "sprites.h" -#include "include/raylib.h" - -inline void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback) { - playback->time_elapsed = 0.0f; - playback->current_frame = 0; - playback->row = animation; - playback->total_frames = anims[animation].total_frames; - playback->loop = anims[animation].loop; -} - -void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) { - const f32 anim_speed = 1.0f / 10.0f; - for (int i = 0; i < len; i++) { - SpriteAnimationPlayback *playback = &playbacks[i]; - playback->time_elapsed += GetFrameTime(); - if (playback->time_elapsed >= anim_speed) { - playback->time_elapsed = 0.0f; - playback->current_frame++; - if (playback->current_frame >= playback->total_frames) { - if (playback->loop) { - playback->current_frame = 0; - } - } - } - } -} - -inline bool IsAnimationFinished(SpriteAnimationPlayback playback) { - return playback.current_frame == playback.total_frames; -} diff --git a/c-version/sprites.h b/c-version/sprites.h index 4692e65..de8508d 100644 --- a/c-version/sprites.h +++ b/c-version/sprites.h @@ -20,3 +20,32 @@ typedef struct { void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback); void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len); bool IsAnimationFinished(SpriteAnimationPlayback playback); + +inline void PlayAnimation(int animation, SpriteAnimation *anims, SpriteAnimationPlayback *playback) { + playback->time_elapsed = 0.0f; + playback->current_frame = 0; + playback->row = animation; + playback->total_frames = anims[animation].total_frames; + playback->loop = anims[animation].loop; +} + +void TickSpriteAnimations(SpriteAnimationPlayback *playbacks, int len) { + const f32 anim_speed = 1.0f / 10.0f; + for (int i = 0; i < len; i++) { + SpriteAnimationPlayback *playback = &playbacks[i]; + playback->time_elapsed += GetFrameTime(); + if (playback->time_elapsed >= anim_speed) { + playback->time_elapsed = 0.0f; + playback->current_frame++; + if (playback->current_frame >= playback->total_frames) { + if (playback->loop) { + playback->current_frame = 0; + } + } + } + } +} + +inline bool IsAnimationFinished(SpriteAnimationPlayback playback) { + return playback.current_frame == playback.total_frames; +} diff --git a/c-version/include/raylib.h b/include/raylib.h similarity index 100% rename from c-version/include/raylib.h rename to include/raylib.h diff --git a/c-version/include/raymath.h b/include/raymath.h similarity index 100% rename from c-version/include/raymath.h rename to include/raymath.h diff --git a/c-version/include/rlgl.h b/include/rlgl.h similarity index 100% rename from c-version/include/rlgl.h rename to include/rlgl.h diff --git a/c-version/lib/libraylib.a b/lib/libraylib.a similarity index 100% rename from c-version/lib/libraylib.a rename to lib/libraylib.a diff --git a/c-version/lib/libraylib.so b/lib/libraylib.so similarity index 100% rename from c-version/lib/libraylib.so rename to lib/libraylib.so diff --git a/c-version/lib/libraylib.so.5.0.0 b/lib/libraylib.so.5.0.0 similarity index 100% rename from c-version/lib/libraylib.so.5.0.0 rename to lib/libraylib.so.5.0.0 diff --git a/c-version/lib/libraylib.so.500 b/lib/libraylib.so.500 similarity index 100% rename from c-version/lib/libraylib.so.500 rename to lib/libraylib.so.500 diff --git a/ECL-INSTALL b/lisp-version/ECL-INSTALL similarity index 100% rename from ECL-INSTALL rename to lisp-version/ECL-INSTALL index 261fffb..4e25d47 100644 --- a/ECL-INSTALL +++ b/lisp-version/ECL-INSTALL @@ -2,6 +2,104 @@ You will find detailed installation instructions in the ECL manual https://common-lisp.net/project/ecl/static/manual/Building-ECL.html If you do not have access to the online version, follow the following recipies. +* Cross-compile for the WASM platform (via emscripten) + +Emscripten target is a little fickle so keep in mind that: + +- shared libraries are supported but come with some drawbacks (e.g. + increased code size if used together with -sASYNCIFY), therefore the + build instructions default to disable-shared + +- disable-tcp is needed, because accept can't be found (lack of + -lsockets or something in this spirit?) + +- select for interactive streams does not work, because reading operations are + blocking (as they should be!), so there is no EOF returned -- clear-input will + hang without a proper file-cnt + +- to build emscripten you need to use their SDK that provides the toolchain, and + set the environment variable EMSDK_PATH + +- for the garbage collector to be able to identify roots on the stack, + you need to pass the -sBINARYEN_EXTRA_PASSES=--spill-pointers option + to the linker for all of your code that might store pointer on the + stack (for instance when embedding ECL) + +- the optimization level -O0 is used because higher optimization + levels seem to interfere with the binaryen options needed to get the + garbage collector to work correctly and tend slow down the program + (might be worth experimenting with the optimization options) + +1. Build the host ECL + +#+begin_src shell-script + ./configure ABI=32 CFLAGS="-m32 -g -O2 -DECL_C_COMPATIBLE_VARIADIC_DISPATCH" LDFLAGS="-m32 -g -O2" \ + --prefix=`pwd`/ecl-emscripten-host --disable-threads + + make -j16 && make install + rm -rf build/ +#+end_src + +2. Configure the toolchain + +Install the Emscripten SDK using the official instructions: + + https://emscripten.org/docs/getting_started/downloads.html + +These build instructions were tested against ~emsdk 3.1.41~. If things doesn't +work try that version instead of ~latest~. + +After that activate the toolchain and configure build flags: + +#+begin_src shell-script + source ${EMSDK_PATH}/emsdk_env.sh + export ECL_TO_RUN=`pwd`/ecl-emscripten-host/bin/ecl + # You may customize various emscripten flags here, i.e: + # export LDFLAGS="-sASYNCIFY=1" +#+end_src + +3. Build the core environment and install it + +#+begin_src shell-script + emconfigure ./configure \ + --host=wasm32-unknown-emscripten \ + --build=x86_64-pc-linux-gnu \ + --with-cross-config=`pwd`/src/util/wasm32-unknown-emscripten.cross_config \ + --prefix=`pwd`/ecl-emscripten \ + --disable-shared \ + --with-tcp=no \ + --with-cmp=no + + emmake make && emmake make install + + # some files need to be copied manually + cp build/bin/ecl.js build/bin/ecl.wasm ecl-emscripten/ +#+end_src + +4. ECL may be hosted on a web page. Assuming that you have quicklisp installed: + +#+begin_src shell-script + export WEBSERVER=`pwd`/src/util/webserver.lisp + pushd ecl-emscripten/ + lisp --load $WEBSERVER + # After the server is loaded run: + # firefox localhost:8888/ecl.html + popd +#+end_src + +If the output does not show on the webpage then open the javascript console. +This is a default html website produced by emscripten. + +5. Build an external program linked against libecl.a + +The default stack size proposed by emscripten is 64KB. This is too little for +ECL, so when you build a program that is linked against libecl.a, then it is +imoprtant to specify a different size. For example: + +#+begin_src shell-script + emcc program.c -sSTACK_SIZE=1048576 lib/*.a -I./include -o program.o +#+end_src + * Unix and similar platforms. 1. Type ./configure --help @@ -111,104 +209,6 @@ Hint provided by Pascal J. Bourguignon. 4. Library and assets in the ecl-iOS directory are ready to run on the iOS system. -* Cross-compile for the WASM platform (via emscripten) - -Emscripten target is a little fickle so keep in mind that: - -- shared libraries are supported but come with some drawbacks (e.g. - increased code size if used together with -sASYNCIFY), therefore the - build instructions default to disable-shared - -- disable-tcp is needed, because accept can't be found (lack of - -lsockets or something in this spirit?) - -- select for interactive streams does not work, because reading operations are - blocking (as they should be!), so there is no EOF returned -- clear-input will - hang without a proper file-cnt - -- to build emscripten you need to use their SDK that provides the toolchain, and - set the environment variable EMSDK_PATH - -- for the garbage collector to be able to identify roots on the stack, - you need to pass the -sBINARYEN_EXTRA_PASSES=--spill-pointers option - to the linker for all of your code that might store pointer on the - stack (for instance when embedding ECL) - -- the optimization level -O0 is used because higher optimization - levels seem to interfere with the binaryen options needed to get the - garbage collector to work correctly and tend slow down the program - (might be worth experimenting with the optimization options) - -1. Build the host ECL - -#+begin_src shell-script - ./configure ABI=32 CFLAGS="-m32 -g -O2 -DECL_C_COMPATIBLE_VARIADIC_DISPATCH" LDFLAGS="-m32 -g -O2" \ - --prefix=`pwd`/ecl-emscripten-host --disable-threads - - make -j16 && make install - rm -rf build/ -#+end_src - -2. Configure the toolchain - -Install the Emscripten SDK using the official instructions: - - https://emscripten.org/docs/getting_started/downloads.html - -These build instructions were tested against ~emsdk 3.1.41~. If things doesn't -work try that version instead of ~latest~. - -After that activate the toolchain and configure build flags: - -#+begin_src shell-script - source ${EMSDK_PATH}/emsdk_env.sh - export ECL_TO_RUN=`pwd`/ecl-emscripten-host/bin/ecl - # You may customize various emscripten flags here, i.e: - # export LDFLAGS="-sASYNCIFY=1" -#+end_src - -3. Build the core environment and install it - -#+begin_src shell-script - emconfigure ./configure \ - --host=wasm32-unknown-emscripten \ - --build=x86_64-pc-linux-gnu \ - --with-cross-config=`pwd`/src/util/wasm32-unknown-emscripten.cross_config \ - --prefix=`pwd`/ecl-emscripten \ - --disable-shared \ - --with-tcp=no \ - --with-cmp=no - - emmake make && emmake make install - - # some files need to be copied manually - cp build/bin/ecl.js build/bin/ecl.wasm ecl-emscripten/ -#+end_src - -4. ECL may be hosted on a web page. Assuming that you have quicklisp installed: - -#+begin_src shell-script - export WEBSERVER=`pwd`/src/util/webserver.lisp - pushd ecl-emscripten/ - lisp --load $WEBSERVER - # After the server is loaded run: - # firefox localhost:8888/ecl.html - popd -#+end_src - -If the output does not show on the webpage then open the javascript console. -This is a default html website produced by emscripten. - -5. Build an external program linked against libecl.a - -The default stack size proposed by emscripten is 64KB. This is too little for -ECL, so when you build a program that is linked against libecl.a, then it is -imoprtant to specify a different size. For example: - -#+begin_src shell-script - emcc program.c -sSTACK_SIZE=1048576 lib/*.a -I./include -o program.o -#+end_src - * Build using Cosmopolitan toolchain (experimental) Binaries built with cosmopolitan toolchain can be executed on numerous platforms diff --git a/README.org b/lisp-version/README.org similarity index 100% rename from README.org rename to lisp-version/README.org diff --git a/base.c b/lisp-version/base.c similarity index 100% rename from base.c rename to lisp-version/base.c diff --git a/ecl_test.c b/lisp-version/ecl_test.c similarity index 100% rename from ecl_test.c rename to lisp-version/ecl_test.c