0xE111 / cat-400

Game framework for nim programming language. Modular and extensible
Mozilla Public License 2.0
90 stars 5 forks source link

How to terminate game loop from message processing method? #30

Closed greenfork closed 4 years ago

greenfork commented 4 years ago

As I understand, I need to return from run proc in order to finish this thread's loop.

I can patch loop to have a bool variable which controls the while statement. Or I can patch process to return something and use it to return from run. What is the recommended design here? Both my suggestions require modifying the core framework systems.

method process(self: ref VideoSystem, message: ref Message) {.base.} =
  logging.warn &"No rule for processing {message}"
method process(self: ref VideoSystem, message: ref WindowQuitMessage) =
  logging.debug "processing quit event"

proc run*(self: ref VideoSystem) =
  loop(frequency = 30) do:
    while true:
      let message = tryRecv()
      if message.isNil: break
      self.process(message)
    self.update(dt)
0xE111 commented 4 years ago

Good question! I haven't chosen best way to do it yet, but currently there is a solution without any patches: just create VideoSystem.running property:

type VideoSystem
  running: bool

method init(self: ref VideoSystem) =
  self.running = True
  # ...

method process(self: ref VideoSystem, message: ref WindowQuitMessage) =
  self.running = False

proc run*(self: ref VideoSystem) =
  loop(frequency = 30) do:
    while true:
      let message = tryRecv()
      if message.isNil: break
      self.process(message)
    self.update(dt)
    if not self.running:
        break
greenfork commented 4 years ago

That works, thank you!