Selaa lähdekoodia

Platform: add Timing API

Streetwalrus Einstein 10 vuotta sitten
vanhempi
commit
e36891b801

+ 20 - 0
platform/include/Timing.h

@@ -0,0 +1,20 @@
+#ifndef INCLUDE_TIMING_H
+#define INCLUDE_TIMING_H
+
+/*
+ * Timing.h
+ * Timing backend abstraction
+ */
+
+namespace WalrusRPG
+{
+    namespace Timing
+    {
+        void init();
+        void deinit();
+
+        unsigned gettime();
+    }
+}
+
+#endif

+ 1 - 1
platform/nspire/Timers.cpp

@@ -6,7 +6,7 @@ volatile uint32_t *timer_load = (uint32_t *) (TIMER);
 volatile uint32_t *timer_value = (uint32_t *) (TIMER + 0x04);
 uint32_t timer_ctl_bkp[2], timer_load_bkp[2];
 
-#define TIMERS WalrusRPG::Timers
+#define TIMERS Nspire::Timers
 
 void TIMERS::init(uint32_t timer)
 {

+ 1 - 3
platform/include/Timers.h → platform/nspire/Timers.h

@@ -3,9 +3,7 @@
 
 #include <cstdint>
 
-#define TIMER_FREQ 32768
-
-namespace WalrusRPG
+namespace Nspire
 {
     namespace Timers
     {

+ 23 - 0
platform/nspire/Timing.cpp

@@ -0,0 +1,23 @@
+#include "Timing.h"
+#include "Timers.h"
+
+#define TIMING WalrusRPG::Timing
+#define TIMER 1
+using namespace Nspire;
+
+void TIMING::init()
+{
+    Timers::init(TIMER);
+    Timers::mode(TIMER, true, false, false, 1, true);
+    Timers::load(TIMER, 0);
+}
+
+void TIMING::deinit()
+{
+    Timers::restore(TIMER);
+}
+
+unsigned TIMING::gettime()
+{
+    return Timers::read(TIMER);
+}

+ 6 - 0
platform/nspire/public/platform.h

@@ -0,0 +1,6 @@
+#ifndef INCLUDE_PLATFORM_H
+#define INCLUDE_PLATFORM_H
+
+#define TIMER_FREQ 32768
+
+#endif

+ 1 - 0
platform/nspire/rules.mk

@@ -2,6 +2,7 @@ nspire_LOCAL_PATH := $(call whereami)
 
 SRCS_C += $(wildcard $(nspire_LOCAL_PATH)/platform/*.c)
 SRCS_CPP += $(wildcard $(nspire_LOCAL_PATH)/*.cpp)
+INCLUDE += $(nspire_LOCAL_PATH)/public
 
 CFLAGS_COMMON += -marm
 

+ 7 - 8
src/engine/StateMachine.cpp

@@ -1,13 +1,14 @@
 #include <libndls.h>
 #include "StateMachine.h"
-#include "Timers.h"
+#include "Timing.h"
+#include "platform.h"
 #include "Graphics.h"
 #include "render/Text.h"
 #include "version.h"
 
 using namespace WalrusRPG::Graphics;
 using namespace WalrusRPG::States;
-using namespace WalrusRPG::Timers;
+using namespace WalrusRPG::Timing;
 
 #define STATEMACHINE WalrusRPG::StateMachine
 
@@ -33,8 +34,6 @@ void STATEMACHINE::pop()
 
 void STATEMACHINE::run()
 {
-    Timers::mode(0, true, false, false, 1, true);
-    Timers::load(0, 0);
     const unsigned loop_time = TIMER_FREQ / 60;
     unsigned loop_next = loop_time;
     unsigned last_update = 0, update_stamp, update_time;
@@ -42,14 +41,14 @@ void STATEMACHINE::run()
 
     while (!stack.empty())
     {
-        update_stamp = Timers::read(0);
+        update_stamp = Timing::gettime();
         update_time = update_stamp - last_update;
         stack.back()->update(update_time);
         last_update = update_stamp;
 
-        if (Timers::read(0) < loop_next)
+        if (Timing::gettime() < loop_next)
         {
-            frame_stamp = Timers::read(0);
+            frame_stamp = Timing::gettime();
             frame_time = frame_stamp - last_frame;
             Graphics::frame_begin();
             stack.back()->render(frame_time);
@@ -68,7 +67,7 @@ void STATEMACHINE::run()
             this->pop();
         }
 
-        while (Timers::read(0) < loop_next)
+        while (Timing::gettime() < loop_next)
             ;
         loop_next += loop_time;
     }

+ 3 - 3
src/engine/main.cpp

@@ -1,5 +1,5 @@
 #include "StateMachine.h"
-#include "Timers.h"
+#include "Timing.h"
 #include "Graphics.h"
 #include "Interrupts.h"
 #include "map/Map.h"
@@ -15,7 +15,7 @@ int main(int argc, char *argv[])
     UNUSED(argv);
 
     Graphics::init();
-    Timers::init(0);
+    Timing::init();
     Interrupts::init();
 
     uint16_t dungeonTest[] = {
@@ -81,7 +81,7 @@ int main(int argc, char *argv[])
     machine.run();
 
     Interrupts::off();
-    Timers::restore(0);
+    Timing::deinit();
     Graphics::deinit();
     return 0;
 }