Skip to content

Google Chrome dinosaur game as a standalone C library

License

Notifications You must be signed in to change notification settings

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.

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

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_canvas_clear(void* user_data);
unsigned char dinorunner_draw(enum dinorunner_sprite_e sprite, const struct pos_s* pos, 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. This project 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

Alternaively, using CMake:

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

while requiring libsdl2-dev libsdl2-image-dev libsdl2-gfx-dev installable with:

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

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_opacity(struct dinorunner_s* dinorunner, unsigned char* opacity);
unsigned char dinorunner_isinverted(struct dinorunner_s* dinorunner, unsigned char* night_mode);
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 (vibration support)
  • Improve configurability
  • Add night mode
  • Add Python/Pygame demo (No time! Implementation trivial.)