massimo

massimo

I’ve tweeted this yesterday and it’s getting some attention, so I decided to post it here to share comments, ideas and answer questions, if anybody got some :slight_smile:

The tweet can be found here
https://twitter.com/wstucco/status/1046485501774057473

What I did is write a custom Scene module and a custom Component model on top of Scenic, reusing and extending Scenic’s own Scene and Component modules, that automatically keep track of component creation and can communicate.
The Scene has an internal list of children components and send to each of them an :animation_frame message on regular intervals, the interval is the amount of milliseconds coming out from the formula

trunc(1_000 / FPS * multiplier)

Note:
The timing is not perfect yet, 60 FPS generate an interval of 16ms, which is actually 62.5 FPS
I’m starting a series of posts on this argument and the first one will about writing a Timer that has resolution in the microseconds instead of milliseconds (I’ve managed to have an std deviation of about 30 microseconds at 60 FPS)

You can find some more information on this thread
https://twitter.com/BoydMulterer/status/1046497642442678272

I will use this post as placeholder to post about progresses in the area of pixel animation without having access to pixels

I’ll post the code of the project as soon as I’ll be back home on my couch

With Scenic Elixir enters in the arena of GUI applications and it does in a pretty spectacular way

First 10 of 15 Posts Switch mode

AstonJ

AstonJ

This is awesome - well done!! :023:

amnu3387

amnu3387

This looks great! I’m re-writing the engine and html/js front-end for a game of mine and can’t wait to try doing the interface with Scenic.

AstonJ

AstonJ

Please keep us posted @amnu3387! :smiley:

I’m loving reading about all these Scenic projects :003:

massimo

massimo OP

@AstonJ @amnu3387

The code is online :slight_smile:

axelson

axelson

Scenic Core Team

I wonder if SchedEx would be a good fit for the scheduling for your game. It is what powers beats (a drum engine):

The talk also contains some interesting discussion on the impact of the current state of millisecond-level timers in Erlang.

massimo

massimo OP

SchedEx run the scheduled function with

Process.send_after(self(), :run, delay)

Still only milliseconds

To get strict 60FPS you need to run update every 16.666666[…] ms or 16,666.666[…] microseconds

Usually, in procedural languages, this is solved in the game loop in two ways

while(true)
  elapsed_time = last_time - current_time()       
  update(elapsed_time)

This way the update knows how much time has passed since the last update and can advance the game state proportionally to the amount of time passed

The other way is

while(true)
  elapsed_time = last_time - current_time()       
  while(elapsed_time < frame_duration)
    elapsed_time = last_time - current_time()       
  last_time = current_time()   

  update()

This way the game wait exactly (well, more or less) frame_duration ms (at 60FPS 16.66ms) before updating the world again

Elixir is not good in tight loops, it doesn’t even have while loops, but it’s good at sending async messages as fast as possible, so we need to use a different approach (but the logic is very similar)

If you look at the code there’s a Timer module that implements a timer with resolution in the microseconds range. It’s not used in the game, but I’ve started working on it.

I’ll be back soon with a short post that explains the internals of it.

To answer you question: music is played in BPM which is beats per minute
Having a timer that can send messages in milliseconds is more than enough, 120 BPM, which is very fast for music, sends only 2 messages per second or a message every 500ms

amnu3387

amnu3387

Thanks! I will check it once I have some time (now you made me want to write a dragon force clone… in elixir)

massimo

massimo OP

I’ve just completed another little challenge

This time I’ve implemented the color cycling effect natively in Elixir and Scenic

All the code, including decoding and re-encoding the PNGs and their palettes is pure Elixir

I will release it soon on Gitlab

For those who don’t know what color cycling is

Color cycling , also known as palette shifting , is a technique used in computer graphics in which colors are changed in order to give the impression of animation. This technique was mainly used in early computer games, as storing one image and changing its palette required less memory and processor power than storing the animation as several frames

I was heavily inspired by this demo

https://twitter.com/wstucco/status/1051168787347705856

16
Post #8
AstonJ

AstonJ

Awesome stuff Massimo :023:

massimo

massimo OP

Code is online for those interested :slight_smile:

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New

We're in Beta

About us Mission Statement