Scenic as a game dev tool

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

28 Likes

This is awesome - well done!! :023:

2 Likes

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.

5 Likes

Please keep us posted @amnu3387! :smiley:

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

3 Likes

@AstonJ @amnu3387

The code is online :slight_smile:

6 Likes

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.

3 Likes

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

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

2 Likes

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 Likes

Awesome stuff Massimo :023:

2 Likes

Code is online for those interested :slight_smile:

8 Likes

This is just amazing, congratulations! I’ve just recently had a blast writing a snake clone in Scenic and took the opportunity to write a tutorial on it, it’s a fantastic tool indeed!

Are you going to release ScenicME as a library? That high resolution Timer module is looking really handy!

6 Likes

Thanks!

I’m not planning to release ScenicME as a library yet, Scenic it’s still in an a very early stage and Boyd is still working hard on it, but I’ll release the Timer module as a package as soon as possible.

Your tutorial looks really good!

1 Like

@giandr

Hi, I just published the timer module on hex.

You can find it at https://hex.pm/packages/micro_timer

It’s the first release but given how simple it is, it’s not gonna change much in the future, except maybe the order of the parameters.

I followed the Erlang :timer module docs, but usually in Elixir the order is reversed.

In the GitLab repository (it’s automatically mirrored on GitHub if you prefer it) you can find the complete source code including a bench folder with some script I used to collect data to measure the timer’s correctness.

A more thorough analysis of the results will follow soon in a post.

4 Likes

Any chance you got around to writing anything else up on this? I’m working on some game dev work with Scenic and would love to see anything else you’ve written or come up with. :slightly_smiling_face:

Hi!

Sorry for being so late, unfortunately I’ve been really busy at work and couldn’t find enough time to write something on the topic.

In the meantime I’ve experimented with some ideas and adapted the code to the new version of Scenic and will be back ASAP.

Thanks for your interest!

1 Like