A fully functional multiplayer game and low level game/physicsl engine written in C and SDL2 and compiled to the browser with Emscripten. Play the game here! render/SLYCE
This repository contains...
- the core game/physics engine in
/game/library
and documentation see Table of Contents - the game, SLYCE in
/game
- physics demos in
/demos
Slyce.Demo.mp4
Also available at https://www.youtube.com/watch?v=8B1DQi7yPfE
pacman.mp4
damping.mp4
nbodies.mp4
gravity.mp4
bounce.mp4
breakout.mp4
invaders.mp4
- game: Contains the final product, the game, SLYCE.
- game/assets: Game assets, audio, images, fonts, etc.
- game/bin: Final static targets.
- game/demo/slyce.c: Main entry, game loop.
- game/include: Library headers.
- game/library: Library implementations.
- game/out: Build artifacts.
- demos: Selected video demonstrations.
- archive: Contains archives of physics engine demos.
The library is directly documented in the source code. The following summaries are LLM generated and give an overview of each module of the full library.
This module handles auxiliary data structures and operations. It manages lists of constants and bodies used throughout the simulation.
#include "aux.h"
#include "list.h"
aux_t
: This structure holds lists of constants and bodies.
aux_t *aux_init(list_t *constants, list_t *bodies)
list_t *aux_get_bodies(aux_t *aux)
list_t *aux_get_constants(aux_t *aux)
void aux_free(void *aux)
This module defines the body structure and related functions. It represents physical bodies in the simulation, including their shapes, masses, and colors.
#include "body.h"
#include "list.h"
body_t
: Represents a physical body in the simulation.
body_t *body_init(list_t *shape, double mass, rgb_color_t color)
void body_free(void *body)
list_t *body_get_shape(body_t *body)
double body_get_mass(body_t *body)
This module contains functions related to collision detection between shapes. It helps in determining if and where collisions occur within the simulation.
#include "collision.h"
bool find_collision(list_t *shape1, list_t *shape2)
bool find_collision_aux(list_t *shape1, list_t *shape2)
This module manages collision packages which encapsulate collision information between two bodies. It provides structures and functions to handle collision data efficiently.
#include "collision_package.h"
collision_package_t
: Encapsulates collision information between two bodies.
collision_package_t *collision_package_init(body_t *body1, body_t *body2)
void collision_package_free(void *collision_package)
body_t *collision_package_get_body1(collision_package_t *collision_package)
body_t *collision_package_get_body2(collision_package_t *collision_package)
This module handles color data, specifically dealing with the rgb_color_t
structure which represents a color with red, green, and blue components.
#include "color.h"
rgb_color_t
: Represents a color with red, green, and blue components.
rgb_color_t color_init(double r, double g, double b)
This module provides functions to initialize and manage the Emscripten environment, facilitating the interaction between C code and JavaScript.
#include "emscripten.h"
void emscripten_init(void)
void emscripten_free(void)
This module defines forces and their application on bodies within the simulation. It includes structures and functions to represent and manipulate physical forces.
#include "forces.h"
force_t
: Represents a physical force.
force_t *force_init(double magnitude, double direction)
void force_free(void *force)
void force_apply(force_t *force, body_t *body)
This module manages force wrappers which encapsulate forces for easy manipulation and application.
#include "force_wrapper.h"
force_wrapper_t
: Wraps a force for easier management.
force_wrapper_t *force_wrapper_init(force_t *force)
void force_wrapper_free(void *force_wrapper)
This module handles image data, providing structures and functions to manage images within the simulation.
#include "image_wrapper.h"
image_wrapper_t
: Wraps an image for easier management.
image_wrapper_t *image_wrapper_init(const char *filename)
void image_wrapper_free(void *image_wrapper)
This module defines and manages dynamic arrays, providing structures and functions to handle lists of data.
#include "list.h"
list_t
: Represents a dynamic array.
list_t *list_init(size_t initial_size)
void list_free(list_t *list)
void list_add(list_t *list, void *value)
void *list_get(list_t *list, size_t index)
void list_remove(list_t *list, size_t index)
This module manages player data within the game, including player initialization, data retrieval, and modification.
#include "player.h"
player_t
: Represents a player in the game.
player_t *player_init(const char *name, int score)
void player_free(void *player)
const char *player_get_name(player_t *player)
int player_get_score(player_t *player)
void player_set_score(player_t *player, int score)
This module provides functions for polygon operations, including initialization, area calculation, and vertex management.
#include "polygon.h"
polygon_t *polygon_init(list_t *vertices)
void polygon_free(polygon_t *polygon)
list_t *polygon_get_vertices(polygon_t *polygon)
double polygon_area(polygon_t *polygon)
This module manages scenes containing multiple bodies. It provides structures and functions to handle scene initialization, body management, and scene updates.
#include "scene.h"
scene_t
: Represents a scene containing multiple bodies.
scene_t *scene_init(void)
void scene_free(scene_t *scene)
void scene_add_body(scene_t *scene, body_t *body)
body_t *scene_get_body(scene_t *scene, size_t index)
This module contains functions for initializing and managing the SDL environment, including drawing operations and screen updates.
#include "sdl_wrapper.h"
void sdl_init(void)
void sdl_clear(void)
void sdl_draw_polygon(list_t *points, rgb_color_t color)
void sdl_show(void)
This module contains functions related to server operations, including initialization, listening, sending, and receiving data.
#include "server.h"
void server_init(void)
void server_listen(void)
void server_send(const char *message)
char *server_receive(void)
This module manages the state of the game or simulation, including state initialization, updates, and rendering.
#include "state.h"
state_t
: Represents the state of the game or simulation.
state_t *state_init(void)
void state_free(state_t *state)
void state_update(state_t *state)
void state_render(state_t *state)
This module contains utility functions for testing, including assertion checks and running test suites.
#include "test_util.h"
bool test_util_approx_eq(double a, double b)
void test_util_assert(bool condition)
void test_util_run_tests(void)
This module manages text rendering within the simulation, including text initialization, drawing, and freeing resources.
#include "text.h"
void text_init(void)
void text_draw(const char *text, double x, double y, rgb_color_t color)
void text_free(void)
This module contains various utility functions, including random number generation, clamping values, and sleeping.
#include "utils.h"
double utils_rand_range(double min, double max)
int utils_clamp(int value, int min, int max)
void utils_sleep(int milliseconds)
This module deals with 2D vector operations, providing structures and functions for vector arithmetic and manipulation.
#include "vector.h"
vector_t
: Represents a 2D vector.
vector_t vector_add(vector_t v1, vector_t v2)
vector_t vector_subtract(vector_t v1, vector_t v2)
vector_t vector_scale(vector_t v, double scalar)
double vector_dot(vector_t v1, vector_t v2)
double vector_cross(vector_t v1, vector_t v2)
double vector_magnitude(vector_t v)
vector_t vector_normalize(vector_t v)
- Install SDL2 for your system.
sudo apt-get install libsdl2-dev
-
Next install SDL2_mixer from https://github.com/libsdl-org/SDL_mixer/releases
On a Debian Linux based machine, this requires cloning the repo, running
bash configure.sh
and thenmake install
Install emscripten for your system.
git clone https://github.com/emscripten-core/emsdk.git
cd emsdk
./emsdk install latest
./emsdk activate latest
source ./emsdk_env.sh
- Clone this repository
-
git clone <link>
- Change directory
cd game-engine/slyce
- Run build command
make NO_ASAN=true all
ASAN is a debugging tool and not needed in the final product.
If you forget to include the NO_ASAN flag, performance will suffer and demos will stutter.
5. Open game in browser. Choose fullscreen mode (show cursor).
http://localhost:8000/bin/slyce.html
```