]> code.bitgloo.com Git - clyne/stm-game.git/commitdiff
modular games; draft 2048
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 16 Jan 2021 15:21:39 +0000 (10:21 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 16 Jan 2021 15:21:39 +0000 (10:21 -0500)
2048.c [new file with mode: 0644]
2048.h [new file with mode: 0644]
Makefile
buttons.h
dogs.c
dogs.h
flapbird.c [new file with mode: 0644]
flapbird.h [new file with mode: 0644]
main.c

diff --git a/2048.c b/2048.c
new file mode 100644 (file)
index 0000000..ce73844
--- /dev/null
+++ b/2048.c
@@ -0,0 +1,104 @@
+#include "buttons.h"
+#include "dogs.h"
+
+static int values[16];
+
+void g2048_init()
+{
+    for (int i = 0; i < 16; i++)
+        values[i] = 0;
+}
+
+int g2048_loop()
+{
+    if ((button_state & BUTTON_JOYUP) == BUTTON_JOYUP) {
+        for (int x = 0; x < 3; x++) {
+            for (int y = 0; y < 3; y++) {
+
+                if (values[4 * y + x] != 0)
+                    continue;
+            }
+        }
+
+        for (int y = 3; y > 0; y--) {
+            for (int x = 0; x < 4; x++) {
+                if (values[4 * y + x] != 0) {
+                    if (values[4 * (y - 1) + x] == 0)
+                        values[4 * (y - 1) + x] = values[4 * y + x], values[4 * y + x] = 0;
+                    else if (values[4 * (y - 1) + x] == values[4 * y + x])
+                        values[4 * (y - 1) + x]++, values[4 * y + x] = 0;
+                }
+            }
+        }
+
+        for (int i = 15; i >= 0; i--) {
+            if (values[i] == 0) {
+                values[i] = 1;
+                break;
+            }
+        }
+    } else if ((button_state & BUTTON_JOYDOWN) == BUTTON_JOYDOWN) {
+        for (int y = 0; y < 3; y++) {
+            for (int x = 0; x < 4; x++) {
+                if (values[4 * y + x] != 0) {
+                    if (values[4 * (y + 1) + x] == 0)
+                        values[4 * (y + 1) + x] = values[4 * y + x], values[4 * y + x] = 0;
+                    else if (values[4 * (y + 1) + x] == values[4 * y + x])
+                        values[4 * (y + 1) + x]++, values[4 * y + x] = 0;
+                }
+            }
+        }
+
+        for (int i = 15; i >= 0; i--) {
+            if (values[i] == 0) {
+                values[i] = 1;
+                break;
+            }
+        }
+    } else if ((button_state & BUTTON_JOYLEFT) == BUTTON_JOYLEFT) {
+        for (int x = 3; x > 0; x--) {
+            for (int y = 0; y < 4; y++) {
+                if (values[4 * y + x] != 0) {
+                    if (values[4 * y + x - 1] == 0)
+                        values[4 * y + x - 1] = values[4 * y + x], values[4 * y + x] = 0;
+                    else if (values[4 * y + x - 1] == values[4 * y + x])
+                        values[4 * y + x - 1]++, values[4 * y + x] = 0;
+                }
+            }
+        }
+
+        for (int i = 15; i >= 0; i--) {
+            if (values[i] == 0) {
+                values[i] = 1;
+                break;
+            }
+        }
+    } else if ((button_state & BUTTON_JOYRIGHT) == BUTTON_JOYRIGHT) {
+        for (int x = 0; x < 3; x++) {
+            for (int y = 0; y < 4; y++) {
+                if (values[4 * y + x] != 0) {
+                    if (values[4 * y + x + 1] == 0)
+                        values[4 * y + x + 1] = values[4 * y + x], values[4 * y + x] = 0;
+                    else if (values[4 * y + x + 1] == values[4 * y + x])
+                        values[4 * y + x + 1]++, values[4 * y + x] = 0;
+                }
+            }
+        }
+
+        for (int i = 15; i >= 0; i--) {
+            if (values[i] == 0) {
+                values[i] = 1;
+                break;
+            }
+        }
+    }
+
+    dogs_clear();
+    for (int i = 0; i < 16; i++) {
+        if (values[i] != 0)
+            draw_number(31 + 10 * (i % 4), 12 + 10 * (3 - (i / 4)), values[i]);
+    }
+    dogs_flush();
+
+    return 500;
+}
diff --git a/2048.h b/2048.h
new file mode 100644 (file)
index 0000000..b8bdf14
--- /dev/null
+++ b/2048.h
@@ -0,0 +1,3 @@
+void g2048_init();
+int g2048_loop();
+
index 4eb5c4090ca88c385f1b0ea7d77fdafc4cb46318..cf0172d5c4129a20041641e7745e987a645ecf98 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -123,6 +123,8 @@ CSRC = $(ALLCSRC) \
        $(TESTSRC) \\r
           dogs.c \\r
           buttons.c \\r
+          flapbird.c \\r
+          2048.c \\r
        main.c\r
 \r
 # C++ sources that can be compiled in ARM or THUMB mode depending on the global\r
index 62fc364ed2092e34fe56941830789607ea63d577..ba7e02d87f2d1ce779e3e27307d9ed54869566f0 100644 (file)
--- a/buttons.h
+++ b/buttons.h
 #define BUTTON_2     (1 << 1)
 #define BUTTON_3     (1 << 0)
 
+#define BUTTON_JOYUP    (BUTTON_JOYUL | BUTTON_JOYUR)
+#define BUTTON_JOYDOWN  (BUTTON_JOYDL | BUTTON_JOYDR)
+#define BUTTON_JOYLEFT  (BUTTON_JOYUL | BUTTON_JOYDL)
+#define BUTTON_JOYRIGHT (BUTTON_JOYUR | BUTTON_JOYDR)
+
 extern unsigned char button_state;
 
 void buttons_init();
diff --git a/dogs.c b/dogs.c
index 12482f51a60cf8cb4b14c05bd6769ca1f7713564..a29ee2cd78e99879098c455cd8a0d494a7f89823 100644 (file)
--- a/dogs.c
+++ b/dogs.c
@@ -166,7 +166,7 @@ void dogs_clear()
         *ptr++ = 0;
         *ptr++ = 0;
     }
-    for (; count >= 0; count--)
+    for (; --count >= 0;)
         *ptr++ = 0;
 }
 
diff --git a/dogs.h b/dogs.h
index 194c3e1985016d44e3fcc1a0595b771cd9c46e32..8af24fd62572f27bb43b2a2682045e3e738ec76c 100644 (file)
--- a/dogs.h
+++ b/dogs.h
@@ -12,6 +12,8 @@
 #ifndef DOGS_H_
 #define DOGS_H_
 
+#include <stdbool.h>
+
 #define DISP_WIDTH  102
 #define DISP_HEIGHT 64
 
diff --git a/flapbird.c b/flapbird.c
new file mode 100644 (file)
index 0000000..f2dd2cf
--- /dev/null
@@ -0,0 +1,80 @@
+#include "buttons.h"
+#include "dogs.h"
+
+static const unsigned char bird[] = {
+    8, 8,
+    0b00111100,
+    0b01100110,
+    0b01000010,
+    0b01111110,
+    0b01100110,
+    0b01000010,
+    0b11000000,
+    0b11000000,
+};
+
+static int score;
+static int t1x, t1o;
+static int t2x, t2o;
+static int py;
+static int vy;
+static int counter;
+
+void flapbird_init()
+{
+    score = 0;
+    t1x = DISP_WIDTH / 2, t1o = 15;
+    t2x = DISP_WIDTH, t2o = 49;
+    py = DISP_HEIGHT / 2 - 4;
+    vy = 0;
+    counter = 0;
+}
+
+int flapbird_loop()
+{
+    // Player logic
+    if (py > 0) {
+        py += vy;
+        if (vy > -3)
+            vy--;
+    } else {
+        if (py < 0)
+            py = 0;
+        if (score > 0)
+            score = 0;
+    }
+
+    if (button_state & BUTTON_2) {
+        vy = 5;
+        if (py <= 0)
+            py = 1;
+    }
+
+    // Rendering
+    dogs_clear();
+
+    draw_rect(t1x, 0, 4, t1o - 10);
+    draw_rect(t1x, t1o + 10, 4, DISP_HEIGHT - t1o + 10);
+    draw_rect(t2x, 0, 4, t2o - 10);
+    draw_rect(t2x, t2o + 10, 4, DISP_HEIGHT - t2o + 10);
+    draw_bitmap(4, py, bird);
+
+    draw_number(DISP_WIDTH - 25, DISP_HEIGHT - 8, score);
+    dogs_flush();
+
+    // Game logic
+    if (t1x == 4)
+        score = (py + 2 > t1o - 10 && py + 6 < t1o + 10) ? score + 1 : 0;
+    if (t2x == 4)
+        score = (py + 2 > t2o - 10 && py + 6 < t2o + 10) ? score + 1 : 0;
+
+    t1x -= 2;
+    if (t1x <= -5)
+        t1x = DISP_WIDTH;
+    t2x -= 2;
+    if (t2x <= -5)
+        t2x = DISP_WIDTH;
+
+    return 100;
+}
+
diff --git a/flapbird.h b/flapbird.h
new file mode 100644 (file)
index 0000000..7ab4199
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef FLAPBIRD_H_
+#define FLAPBIRD_H_
+
+void flapbird_init();
+int flapbird_loop();
+
+#endif // FLAPBIRD_H_
+
diff --git a/main.c b/main.c
index bc8fec5b36f475051da5cd3bbff73e1f19f32361..5c963fc5b264e048bc1df93d29b85306240b994d 100644 (file)
--- a/main.c
+++ b/main.c
@@ -14,6 +14,9 @@
 #include "dogs.h"
 #include "hal.h"
 
+#include "2048.h"
+#include "flapbird.h"
+
 /*
  * Progress:
  *  - Serial through LPUART1 works (38400 baud, takes over swdio pins)
@@ -24,6 +27,9 @@
  *  - Run at 512kHz, only use HSI for ADC: 360uA (jumpy)
  *  - Drop to 1.2V Vcore (range 3), enable low-V detector: 375uA (steady) (440uA at 1MHz)
  *  - Run at 4MHz, drop to low-power run/sleep @ 64kHz for idle: 375uA (also lowered contrast)
+ *  - Sleep display for 'pause': ~240uA
+ *
+ *  - Flappy bird is going, 2048 next
  */
 
 static volatile bool adc_is_complete = false;
@@ -76,86 +82,26 @@ THD_FUNCTION(Thread2, arg)
 
     dogs_init();
 
-    const unsigned char testbitmap[] = {
-        8, 8,
-        0b00111100,
-        0b01100110,
-        0b01000010,
-        0b01111110,
-        0b01100110,
-        0b01000010,
-        0b11000000,
-        0b11000000,
-    };
+    flapbird_init();
 
     bool sleep = false;
-    int score = 0;
-
-    int t1x = DISP_WIDTH / 2, t1o = 15;
-    int t2x = DISP_WIDTH, t2o = 49;
-
-    int py = DISP_HEIGHT / 2 - 4;
-    int vy = 0;
-    int counter = 0;
-    int mv = readVddmv();
     while (1) {
         if (button_state & BUTTON_1) {
             sleep ^= true;
+            if (sleep) {
+                draw_number(DISP_WIDTH - 33, 0,
+                            !(PWR->CSR & PWR_CSR_PVDO) ? readVddmv() : 1);
+                dogs_flush();
+            }
             dogs_set_sleep(sleep);
         }
 
+        int dtime = 100;
         if (!sleep) {
-            // Player logic
-            if (py > 0) {
-                py += vy;
-                if (vy > -3)
-                    vy--;
-            } else {
-                if (py < 0)
-                    py = 0;
-                if (score > 0)
-                    score = 0;
-            }
-    
-            if (button_state & BUTTON_2) {
-                vy = 5;
-                if (py <= 0)
-                    py = 1;
-            }
-    
-            // Rendering
-            dogs_clear();
-    
-            draw_rect(t1x, 0, 4, t1o - 10);
-            draw_rect(t1x, t1o + 10, 4, DISP_HEIGHT - t1o + 10);
-            draw_rect(t2x, 0, 4, t2o - 10);
-            draw_rect(t2x, t2o + 10, 4, DISP_HEIGHT - t2o + 10);
-            draw_bitmap(4, py, testbitmap);
-    
-            draw_number(DISP_WIDTH - 33, DISP_HEIGHT - 8, mv);
-            draw_number(DISP_WIDTH - 33, DISP_HEIGHT - 16, score);
-            dogs_flush();
-    
-            // Game logic
-            if (++counter == 50) {
-                counter = 0;
-                mv = !(PWR->CSR & PWR_CSR_PVDO) ? readVddmv() : 1;
-            }
-    
-            if (t1x == 4)
-                score = (py + 2 > t1o - 10 && py + 6 < t1o + 10) ? score + 1 : 0;
-            if (t2x == 4)
-                score = (py + 2 > t2o - 10 && py + 6 < t2o + 10) ? score + 1 : 0;
-    
-            t1x -= 2;
-            if (t1x <= -5)
-                t1x = DISP_WIDTH;
-            t2x -= 2;
-            if (t2x <= -5)
-                t2x = DISP_WIDTH;
+            dtime = flapbird_loop();
         }
 
-        chThdSleepS(TIME_MS2I(100) / 64);
+        chThdSleepS(TIME_MS2I(dtime) / 64);
     }
 }