Blame view

emulateur/epsilon-nofrendo/escher/src/run_loop.cpp 1.69 KB
6663b6c9   adorian   projet complet av...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
  #include <escher/run_loop.h>
  #include <assert.h>
  #ifdef __EMSCRIPTEN__
  #include <emscripten.h>
  #endif
  
  RunLoop::RunLoop() :
    m_time(0) {
  }
  
  int RunLoop::numberOfTimers() {
    return 0;
  }
  
  Timer * RunLoop::timerAtIndex(int i) {
    assert(false);
    return nullptr;
  }
  
  void RunLoop::run() {
    runWhile(nullptr, nullptr);
  }
  
  void RunLoop::runWhile(bool (*callback)(void * ctx), void * ctx) {
    while ((callback == nullptr || callback(ctx)) && step()) {
    }
  }
  
  bool RunLoop::step() {
    // Fetch the event, if any
    int eventDuration = Timer::TickDuration;
    int timeout = eventDuration;
  
    Ion::Events::Event event = Ion::Events::getEvent(&timeout);
    assert(event.isDefined());
  
    eventDuration -= timeout;
    assert(eventDuration >= 0);
    assert(eventDuration <= Timer::TickDuration);
  
    /* At this point, eventDuration contains the time it took to retrieve the
     * event. It can be zero, and is at most equal to the timeout value, Timer::
     * TickDuration.  The event returned can be None if nothing worth taking care
     * of happened. In other words, getEvent is a blocking call with a timeout. */
  
    m_time += eventDuration;
  
    if (m_time >= Timer::TickDuration) {
      m_time -= Timer::TickDuration;
      for (int i=0; i<numberOfTimers(); i++) {
        Timer * timer = timerAtIndex(i);
        if (timer->tick()) {
          dispatchEvent(Ion::Events::TimerFire);
        }
      }
    }
  
  #if ESCHER_LOG_EVENTS_BINARY
    Ion::Console::writeChar((char)event.id());
  #endif
  #if ESCHER_LOG_EVENTS_NAME
    const char * name = event.name();
    if (name == nullptr) {
      name = "UNDEFINED";
    }
    Ion::Console::writeLine(name);
  #endif
  
    if (event != Ion::Events::None) {
      dispatchEvent(event);
    }
  
    return event != Ion::Events::Termination;
  }