Skip to content

AKJ7/dinorunner

Repository files navigation

Dinorunner Dinorunner

Chrome's Dinorunner game as a standalone C library.

Description

This project contains a port of the famous Chrome's Dinosaur Game written in C, C++.

It is designed to be used as a configurable backend library accessable to a wide range of programming languages (using i.e. CFFIs) and hardware architectures.

Features:

  • Hardware-agnostic: No hardware-specific dependencies - Runs everywhere
  • Compiler-agnostic: No compiler-specific dependencies - Compile and go
  • No stdlib or header lib requirement
  • No heap allocations
  • No typedefs
  • Supports nightmode
  • Only requires 1200 bytes to store data structure on x86_64
  • Supports variadic jump heights

The dinorunner was inspired by https://github.com/wayou/t-rex-runner from which the trex-assets were fetched.
The sound assets used in the demo originates from: https://www.sounds-resource.com/browser_games/googlechromedinosaurgame/sound/18002/ .

Requirements

The following functions need to be defined:

unsigned char dinorunner_writehighscore(unsigned long high_score, void* user_data);
unsigned char dinorunner_readhighscore(unsigned long* high_score, void* user_data);
unsigned long dinorunner_gettimestamp(void* user_data);
unsigned char dinorunner_playsound(enum dinorunner_sound_e sound, void* user_data);
unsigned char dinorunner_vibrate(unsigned duration, void* user_data);
unsigned char dinorunner_clearcanvas(void* user_data);
unsigned char dinorunner_draw(enum dinorunner_sprite_e sprite, const struct pos_s* pos, unsigned char opacity, void* user_data);
unsigned char dinorunner_log(void* user_data, const char* format, ...);

Demo

Usage

This project is subdivided into two parts:

  1. libdinorunner: The actual library. It is written without any external dependency and is targetted to be used with any consivable system. It can directly be compiled into a project or as shared and static libraries using:
cmake -DCMAKE_BUILD_TYPE=Release -S dinorunner -B dinorunner/build && cmake --build dinorunner/build

See dinorunner/lib for the generated libraries objects. With

sudo make install -C dinorunner/build/

the libraries can be installed system-wide.

  1. dinorunner-sdl: This is a running example of the project. It uses sdl2 to process user input and display the output of libdinorunner to the screen. Using CMake, the dependencies can be automatically downloaded, locally built, then linked to the demos. This can be done using
cmake -DCMAKE_BUILD_TYPE=Release -DDINORUNNER_SDL_EXAMPLE_VENDORED=ON -S demo -B demo/build && cmake --build demo/build && demo/sdl/bin/dinorunner_sdl

In case SDL2, SDL2-Image and SDL2-gfx are already installed, simply run

cmake -DCMAKE_BUILD_TYPE=Release -S demo -B demo/build && cmake --build demo/build && demo/sdl/bin/dinorunner_sdl

to build and run the executable. The dependencies can manually be installed using

sudo apt -y install libsdl2-dev libsdl2-image-dev libsdl2-gfx-dev

This project also provides a preset docker container into which the program compiles and runs. Before running the examples in a docker container, the x-server needs to permit access to client outside its host. This is done using: xhost +. The simplest way to run the program is using docker-compose:

docker compose -f docker-compose.yml up dinorunner

API

The following functions can be used to interact with the dinorunner engine. See the demo for example.

unsigned char dinorunner_init(struct dinorunner_s* dinorunner, const struct dimension_s* dimension, void* user_data);
unsigned char dinorunner_update(struct dinorunner_s* dinorunner);
unsigned char dinorunner_getversion(struct version_s* version);
unsigned char dinorunner_isinverted(const struct dinorunner_s* dinorunner, unsigned char* night_mode);
unsigned char dinorunner_isalive(const struct dinorunner_s* dinorunner, unsigned char* activation_status);
void dinorunner_seed(unsigned short random_seed);
void dinorunner_onkeyup(struct dinorunner_s* dinorunner);
void dinorunner_onkeydown(struct dinorunner_s* dinorunner);
void dinorunner_onkeynone(struct dinorunner_s* dinorunner);

TODO

  • Add sound and vibration support
  • Improve configurability
  • Add night mode