aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2021-01-16 10:21:39 -0500
committerClyne Sullivan <clyne@bitgloo.com>2021-01-16 10:21:39 -0500
commit7c8a16289cadf193fdd43e45e855529d6047150d (patch)
treed5d2b61dda6f3e2f3d2748c78542d90efb9555be
parent245866891a98fca3cacffdfb0a27b3505c7d404b (diff)
modular games; draft 2048
-rw-r--r--2048.c104
-rw-r--r--2048.h3
-rw-r--r--Makefile2
-rw-r--r--buttons.h5
-rw-r--r--dogs.c2
-rw-r--r--dogs.h2
-rw-r--r--flapbird.c80
-rw-r--r--flapbird.h8
-rw-r--r--main.c84
9 files changed, 220 insertions, 70 deletions
diff --git a/2048.c b/2048.c
new file mode 100644
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
index 0000000..b8bdf14
--- /dev/null
+++ b/2048.h
@@ -0,0 +1,3 @@
+void g2048_init();
+int g2048_loop();
+
diff --git a/Makefile b/Makefile
index 4eb5c40..cf0172d 100644
--- a/Makefile
+++ b/Makefile
@@ -123,6 +123,8 @@ CSRC = $(ALLCSRC) \
$(TESTSRC) \
dogs.c \
buttons.c \
+ flapbird.c \
+ 2048.c \
main.c
# C++ sources that can be compiled in ARM or THUMB mode depending on the global
diff --git a/buttons.h b/buttons.h
index 62fc364..ba7e02d 100644
--- a/buttons.h
+++ b/buttons.h
@@ -21,6 +21,11 @@
#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 12482f5..a29ee2c 100644
--- 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 194c3e1..8af24fd 100644
--- 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
index 0000000..f2dd2cf
--- /dev/null
+++ b/flapbird.c
@@ -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
index 0000000..7ab4199
--- /dev/null
+++ b/flapbird.h
@@ -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 bc8fec5..5c963fc 100644
--- 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);
}
}