Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Squeak Smalltalk: Game loop

In many languages you can do something like the following:

while true:
  handle events like keyboard input
  update game world
  draw screen
  (optional: delay execution)

while this is far from optimal it should suffice for simple games.

How do you do this in Squeak Smalltalk?

I can read keyboard input and react to it as described on wiki.squeak.org. But if I try to execute something like

1 to: 10 do: [ :i | game updateAndDraw ]

all the events are only ever handled after the loop has executed.

like image 429
Higemaru Avatar asked May 11 '17 16:05

Higemaru


1 Answers

Morphic already provides that main loop. It's in MorphicProject class>>spawnNewProcess:

uiProcess := [
    [ world doOneCycle.  Processor yield ] repeat.
] newProcess ...

And if you dig into doOneCycle you will find it

  • (optionally) does a delay (interCyclePause:)
  • checks for screen resize
  • processes events
  • processes step methods
  • re-displays the world

Your code should hook into these phases by adding mouse/keyboard event handlers, step methods for animation, and draw methods for redisplaying. All of these should be methods in your own game morph. You can find examples throughout the system.

like image 102
Vanessa Freudenberg Avatar answered Sep 28 '22 08:09

Vanessa Freudenberg