From 099f70631484bf975c906421613bb02667e77365 Mon Sep 17 00:00:00 2001
From: Joshua Saxby <Joshua2.Saxby@live.uwe.ac.uk>
Date: Sun, 9 Feb 2020 02:23:20 +0000
Subject: [PATCH] Too bad, you can't create fibers from class methods, I'm
 binning this classical approach for my logic...

---
 source/main.cpp | 34 ++++++++++++++++++++--------------
 1 file changed, 20 insertions(+), 14 deletions(-)

diff --git a/source/main.cpp b/source/main.cpp
index f4b769a..cc011df 100755
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -59,6 +59,7 @@ public:
     /* end event handler methods */
 
 private:
+    bool on; // whether flasher should be on or off
     /**
      * @brief Draws whatever's in the screen buffer to display
      */
@@ -72,8 +73,18 @@ Game::Game()
     : screen_buffer(5, 5) // our screen buffer mirrors the display dimensions
 {}
 
+void Game::press_button_a(MicroBitEvent e) {
+    // XXX: debug
+    this->on = true;
+}
+
+void Game::press_button_b(MicroBitEvent e) {
+    // XXX: debug
+    this->on = false;
+}
+
 void Game::run() {
-    UBIT.display.scroll("NEW GAME!");
+    // UBIT.display.scroll("NEW GAME!");
     // init screen display mode, to be sure it is in a known-state
     // TODO: consider changing to greyscale to allow "mutli-coloured" blocks
     UBIT.display.setDisplayMode(DISPLAY_MODE_BLACK_AND_WHITE);
@@ -93,7 +104,6 @@ void Game::run() {
     while (true) { // TODO: change to use game-over condition
         // sleep to conserve CPU cycles and allow other fibers a chance to run
         UBIT.sleep(10); // 10ms sleep, or 100Hz tick rate
-        this->draw(); // render screen buffer
     }
     // TODO: add the rest of the game logic
     UBIT.display.scroll("GAME OVER!");
@@ -116,18 +126,11 @@ void Game::run() {
 void Game::draw() {
     // XXX: debug drawing, fill the screen buffer with black
     this->screen_buffer.clear();
+    if (this->on) {
+        this->screen_buffer.setPixelValue(2, 2, 255);
+    }
     // render our screen buffer
-    UBIT.display.image.paste(this->screen_buffer);
-}
-
-void Game::press_button_a(MicroBitEvent e) {
-    // XXX: debug
-    this->screen_buffer.setPixelValue(0, 2, 255);
-}
-
-void Game::press_button_b(MicroBitEvent e) {
-    // XXX: debug
-    this->screen_buffer.setPixelValue(4, 2, 255);
+    UBIT.display.print(this->screen_buffer);
 }
 
 int main() {
@@ -136,8 +139,11 @@ int main() {
 
     UBIT.display.scroll("BLOCKS!");
 
+    Game game;
+    
+    create_fiber(game.draw);
+
     while (true) { // indefinitely start new games
-        Game game;
         game.run();
     }
 
-- 
GitLab