use liblotus

This commit is contained in:
waozi committed 2026-05-21 22:33:06 -03:00
1 parent f189300079
commit bb40a2822d
6 files changed
+489 -343

No files matched your search

+17 -2
View File
@@ -1,13 +1,28 @@
cmake_minimum_required(VERSION 3.22)
project(InnerBreezeNative C)
add_library(inbe SHARED main.c)
set(REPO_ROOT "${CMAKE_CURRENT_LIST_DIR}/../../../../..")
set(LOTUS_DIR "${REPO_ROOT}/liblotus")
add_library(lotus STATIC
"${LOTUS_DIR}/lotus.c"
)
target_include_directories(lotus PUBLIC
"${LOTUS_DIR}"
)
add_library(inbe SHARED
main.c
)
target_compile_options(inbe PRIVATE -fno-builtin -fno-stack-protector)
target_link_options(inbe PRIVATE -nostdlib)
target_link_libraries(inbe
lotus
android
atomic
m
c)
c
)
+196 -152
View File
@@ -1,200 +1,244 @@
#include <android/native_activity.h>
#include <android/native_window.h>
#include <android/input.h>
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include "font8x8.h"
#include "lotus.h"
#define Rscale 6
typedef struct AppState AppState;
#define MINR 150
#define MAXR 300
struct AppState {
ANativeWindow *window;
AInputQueue *input_queue;
volatile bool running;
pthread_t thread;
typedef struct {
ANativeWindow* window;
AInputQueue* input_queue;
volatile bool running;
pthread_t thread;
char count[4];
int radius;
int dir;
int speed;
} AppState;
static AppState app = {
.count = {'0', '0', '0', '\0'},
.radius = MINR,
.speed = 5,
.dir = 1
Lotus lotus;
};
static void draw_char(ANativeWindow_Buffer* buffer, unsigned char c, int x, int y, int scale, uint32_t color) {
const uint8_t* glyph = font_8x8[c];
static AppState app;
uint32_t* pixels = (uint32_t*)buffer->bits;
uint32_t stride = buffer->stride;
static void
draw_char(ANativeWindow_Buffer *buffer, unsigned char c, int x, int y, int scale, uint32_t color)
{
const uint8_t *glyph;
uint32_t *pixels;
uint32_t stride;
uint8_t glyph_row;
int row;
int col;
int sx;
int sy;
int px;
int py;
for (int row = 0; row < 8; ++row) {
uint8_t glyph_row = glyph[row];
for (int col = 0; col < 8; ++col) {
if (glyph_row & (1 << (7 - col))) {
for (int sy = 0; sy < scale; ++sy) {
for (int sx = 0; sx < scale; ++sx) {
int px = x + (col * scale) + sx;
int py = y + (row * scale) + sy;
glyph = font_8x8[c];
pixels = (uint32_t*)buffer->bits;
stride = buffer->stride;
if (px >= 0 && px < buffer->width && py >= 0 && py < buffer->height) {
pixels[py * stride + px] = color;
}
}
}
}
}
}
for(row = 0; row < 8; row++){
glyph_row = glyph[row];
for(col = 0; col < 8; col++){
if(glyph_row & (1 << (7 - col))){
for(sy = 0; sy < scale; sy++){
for(sx = 0; sx < scale; sx++){
px = x + col * scale + sx;
py = y + row * scale + sy;
if(px >= 0 && px < buffer->width && py >= 0 && py < buffer->height)
pixels[py * stride + px] = color;
}
}
}
}
}
}
static void draw_string(ANativeWindow_Buffer* buffer, const char* str, int x, int y, int scale, uint32_t color) {
int text_width = strlen(str) * 8 * scale;
int text_height = 8 * scale;
int cursor_x = x - (text_width / 2);
y = y - (text_height / 2);
static void
draw_string(ANativeWindow_Buffer *buffer, const char *str, int x, int y, int scale, uint32_t color)
{
int text_width;
int text_height;
int cursor_x;
int i;
for (int i = 0; str[i] != '\0'; ++i) {
draw_char(buffer, (unsigned char)str[i], cursor_x, y, scale, color);
cursor_x += (8 * scale);
}
text_width = strlen(str) * 8 * scale;
text_height = 8 * scale;
cursor_x = x - text_width / 2;
y = y - text_height / 2;
for(i = 0; str[i] != 0; i++){
draw_char(buffer, (unsigned char)str[i], cursor_x, y, scale, color);
cursor_x += 8 * scale;
}
}
static void draw_circle(ANativeWindow_Buffer* buffer, int cx, int cy, int r) {
uint32_t* pixels = (uint32_t*)buffer->bits;
uint32_t stride = buffer->stride;
static void
draw_circle(ANativeWindow_Buffer *buffer, int cx, int cy, int r)
{
uint32_t *pixels;
uint32_t stride;
int r2;
int x;
int y;
int dx;
int dy;
int r_squared = r * r;
pixels = (uint32_t*)buffer->bits;
stride = buffer->stride;
r2 = r * r;
for (int y = cy - r; y <= cy + r; ++y) {
for (int x = cx - r; x <= cx + r; ++x) {
if (x >= 0 && x < buffer->width && y >= 0 && y < buffer->height) {
int dx = x - cx;
int dy = y - cy;
for(y = cy - r; y <= cy + r; y++){
for(x = cx - r; x <= cx + r; x++){
if(x >= 0 && x < buffer->width && y >= 0 && y < buffer->height){
dx = x - cx;
dy = y - cy;
if ((dx * dx) + (dy * dy) <= r_squared) {
pixels[y * stride + x] = 0xFFFF0000;
}
}
}
}
if(dx * dx + dy * dy <= r2)
pixels[y * stride + x] = 0xFFFF0000;
}
}
}
}
static void clear_buffer(ANativeWindow_Buffer* buffer) {
uint32_t* pixels = (uint32_t*)buffer->bits;
uint32_t stride = buffer->stride;
static void
clear_buffer(ANativeWindow_Buffer *buffer)
{
uint32_t *pixels;
uint32_t stride;
int x;
int y;
pixels = (uint32_t*)buffer->bits;
stride = buffer->stride;
for (int y = 0; y < buffer->height; ++y) {
for (int x = 0; x < buffer->width; ++x) {
pixels[y * stride + x] = 0xFF000000;
}
}
}
static void incrstr(char *str) {
int len = 0;
while (str[len] != '\0') {
len++;
}
for (int i = len - 1; i >= 0; i--) {
if (str[i] < '9') {
str[i]++;
return;
}
str[i] = '0';
}
// fallback
str[0] = '0';
str[1] = '0';
str[2] = '0';
str[3] = '\0';
for(y = 0; y < buffer->height; y++){
for(x = 0; x < buffer->width; x++)
pixels[y * stride + x] = 0xFF000000;
}
}
static void* render_loop(void* arg) {
while (app.running) {
if (app.input_queue != NULL) {
AInputEvent* event = NULL;
while (AInputQueue_getEvent(app.input_queue, &event) >= 0) {
if (AInputQueue_preDispatchEvent(app.input_queue, event)) {
continue;
}
AInputQueue_finishEvent(app.input_queue, event, 1);
}
}
static void
drain_input(void)
{
AInputEvent *event;
if (app.window != NULL) {
ANativeWindow_Buffer buffer;
if (ANativeWindow_lock(app.window, &buffer, NULL) == 0) {
clear_buffer(&buffer);
if(app.input_queue == 0)
return;
int cx = buffer.width / 2;
int cy = buffer.height / 2;
event = 0;
while(AInputQueue_getEvent(app.input_queue, &event) >= 0){
if(AInputQueue_preDispatchEvent(app.input_queue, event))
continue;
draw_string(&buffer, "INNER BREEZE", cx, 150, 6, 0xFF00FF00);
draw_circle(&buffer, cx, cy, app.radius);
draw_string(&buffer, app.count, cx, cy, 6, 0xFF00FF00);
ANativeWindow_unlockAndPost(app.window);
}
}
app.radius += app.dir * app.speed;
if (app.radius > MAXR) {
app.dir = -1;
}
if (app.radius < MINR) {
app.dir = 1;
incrstr(app.count);
}
usleep(16000);
}
AInputQueue_finishEvent(app.input_queue, event, 1);
}
}
static void onNativeWindowCreated(ANativeActivity* activity, ANativeWindow* window) {
ANativeWindow_setBuffersGeometry(window, 0, 0, 1);
static void*
render_loop(void *arg)
{
ANativeWindow_Buffer buffer;
int cx;
int cy;
int r;
app.window = window;
app.running = true;
pthread_create(&app.thread, NULL, render_loop, NULL);
(void)arg;
while(app.running){
drain_input();
if(app.window != 0){
if(ANativeWindow_lock(app.window, &buffer, 0) == 0){
clear_buffer(&buffer);
cx = buffer.width / 2;
cy = buffer.height / 2;
draw_string(&buffer, "INNER BREEZE", cx, 150, 6, 0xFF00FF00);
r = app.lotus.r * Rscale;
draw_circle(&buffer, cx, cy, r);
draw_string(&buffer, app.lotus.count, cx, cy, 6, 0xFF00FF00);
ANativeWindow_unlockAndPost(app.window);
}
}
lotusbreath(&app.lotus);
app.lotus.frame++;
usleep(16000);
}
return 0;
}
static void onNativeWindowDestroyed(ANativeActivity* activity, ANativeWindow* window) {
app.running = false;
pthread_join(app.thread, NULL);
app.window = NULL;
static void
onNativeWindowCreated(ANativeActivity *activity, ANativeWindow *window)
{
(void)activity;
ANativeWindow_setBuffersGeometry(window, 0, 0, 1);
app.window = window;
app.running = true;
lotusinit(&app.lotus);
app.lotus.screen = LotusScreenSession;
pthread_create(&app.thread, 0, render_loop, 0);
}
static void onInputQueueCreated(ANativeActivity* activity, AInputQueue* queue) {
app.input_queue = queue;
static void
onNativeWindowDestroyed(ANativeActivity *activity, ANativeWindow *window)
{
(void)activity;
(void)window;
app.running = false;
pthread_join(app.thread, 0);
app.window = 0;
}
static void onInputQueueDestroyed(ANativeActivity* activity, AInputQueue* queue) {
app.input_queue = NULL;
static void
onInputQueueCreated(ANativeActivity *activity, AInputQueue *queue)
{
(void)activity;
app.input_queue = queue;
}
static void
onInputQueueDestroyed(ANativeActivity *activity, AInputQueue *queue)
{
(void)activity;
(void)queue;
app.input_queue = 0;
}
__attribute__((visibility("default")))
void ANativeActivity_onCreate(ANativeActivity* activity, void* savedState, size_t sazedStateSize) {
activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
activity->callbacks->onInputQueueCreated = onInputQueueCreated;
activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
void
ANativeActivity_onCreate(ANativeActivity *activity, void *savedState, size_t savedStateSize)
{
(void)savedState;
(void)savedStateSize;
activity->callbacks->onNativeWindowCreated = onNativeWindowCreated;
activity->callbacks->onNativeWindowDestroyed = onNativeWindowDestroyed;
activity->callbacks->onInputQueueCreated = onInputQueueCreated;
activity->callbacks->onInputQueueDestroyed = onInputQueueDestroyed;
}
Binary file not shown.
+73
View File
@@ -1,5 +1,20 @@
#include "lotus.h"
static void
cpcount(char dst[CountSize], const char src[CountSize])
{
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = 0;
}
static int
eqcount(const char a[CountSize], const char b[CountSize])
{
return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
}
void
inccount(char v[CountSize])
{
@@ -21,3 +36,61 @@ inccount(char v[CountSize])
}
v[0] = '0';
}
void
lotusinit(Lotus *l)
{
if(l == 0)
return;
l->screen = LotusScreenStart;
l->phase = LotusPhaseBreathe;
l->round = 0;
l->r = 25;
l->rmin = 25;
l->rmax = 50;
l->dir = 0;
l->speed = 2;
l->frame = 0;
cpcount(l->count, "000");
cpcount(l->maxbreaths, "030");
}
void
lotusbreath(Lotus *l)
{
if(l == 0)
return;
if(l->phase != LotusPhaseBreathe)
return;
l->breathtick++;
if(l->breathtick < l->speed)
return;
l->breathtick = 0;
if(l->dir == 0){
if(l->r < l->rmax)
l->r++;
else
l->dir = 1;
}else{
if(l->r > l->rmin){
l->r--;
}else{
l->dir = 0;
inccount(l->count);
}
}
if(eqcount(l->count, l->maxbreaths)){
cpcount(l->count, "000");
l->phase = LotusPhaseHold;
}
}
+33 -1
View File
@@ -1,6 +1,11 @@
#ifndef LOTUS_H
#define LOTUS_H
enum {
CountSize = 4,
MaxRounds = 4
};
enum {
LotusScreenStart = 0,
LotusScreenSession,
@@ -8,9 +13,36 @@ enum {
};
enum {
CountSize = 4
LotusPhaseBreathe = 0,
LotusPhaseHold,
LotusPhaseRecover,
LotusPhaseNext
};
typedef struct Lotus Lotus;
struct Lotus {
int screen;
int phase;
int round;
int r;
int rmin;
int rmax;
int dir;
int speed;
int frame;
int breathtick;
int sectick;
int halftick;
char count[CountSize];
char maxbreaths[CountSize];
};
void lotusinit(Lotus *l);
void lotusbreath(Lotus *l);
void inccount(char v[CountSize]);
#endif
+170 -188
View File
@@ -1,214 +1,196 @@
#include "wasm4.h"
#include "lotus.h"
void* memset(void* dest, int byte, unsigned long n) {
uint8_t* d = dest;
while (n--) {
*d++ = (unsigned char)byte;
}
return dest;
}
void*
memset(void *dest, int byte, unsigned long n)
{
uint8_t *d;
void start() {
PALETTE[0] = 0xfff6d3;
PALETTE[1] = 0xf9a875;
PALETTE[2] = 0xeb6b6f;
PALETTE[3] = 0x7c3f58;
}
d = dest;
while(n--)
*d++ = (unsigned char)byte;
void pixel(int x, int y) {
int idx = (y*160 + x) >> 2;
int shift = (x & 0b11) << 1;
int mask = 0b11 << shift;
int palette_color = *DRAW_COLORS & 0b1111;
if (palette_color == 0) {
return;
}
int color = (palette_color - 1) & 0b11;
FRAMEBUFFER[idx] = (uint8_t)((color << shift) | (FRAMEBUFFER[idx] & ~mask));
return dest;
}
#define WINDOW_WIDTH 160
#define WINDOW_HEIGHT 160
#define MAX_ROUNDS 4
uint32_t radius = 25;
uint8_t dir = 0;
Lotus lotus;
uint8_t minr = 25;
uint8_t maxr = 50;
uint8_t speed = 2;
int frame_count = 0;
char count[4] = "000";
char max_breaths[4] = "030";
uint8_t phase = 0;
uint8_t round = 0;
uint8_t state = 0;
char results[MAX_ROUNDS][4] = {
"052",
"040",
"064",
"020"
char results[MaxRounds][CountSize] = {
"052",
"040",
"064",
"020"
};
int strcmp(const char *s1, const char *s2) {
while (*s1 != '\0' && *s1 == *s2) {
s1++;
s2++;
}
return *(const uint8_t*)s1 - *(const uint8_t*)s2;
}
char* strcpy(char *dest, const char *src) {
char *start = dest;
while (*src != '\0') {
*dest = *src;
dest++;
src++;
}
*dest = '\0';
return start;
static void
cpcount(char dst[CountSize], const char src[CountSize])
{
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
dst[3] = 0;
}
int strlen(const char *s1) {
int len = 0;
while (s1[len] != '\0') {
len++;
}
return len;
int
strcmp(const char *s1, const char *s2)
{
while(*s1 != 0 && *s1 == *s2){
s1++;
s2++;
}
return *(const uint8_t*)s1 - *(const uint8_t*)s2;
}
int drawbtn(int16_t mx, int16_t my, uint8_t mb, int32_t x, int32_t y, const char *label) {
uint32_t w = (uint32_t)strlen(label) * 8;
uint32_t h = 10;
x = x - (int32_t)(w / 2);
if( mx > x && mx < x + (int16_t)w && my > y && my < y + (int16_t)h) {
*DRAW_COLORS = 4;
if (mb & MOUSE_LEFT) {
return 1;
}
} else {
*DRAW_COLORS = 3;
}
rect(x, y, w, h);
int
strlen(const char *s1)
{
int len;
*DRAW_COLORS = 2;
text(label, x, y + 1);
return 0;
len = 0;
while(s1[len] != 0)
len++;
return len;
}
void update () {
*DRAW_COLORS = 4;
int16_t mx = *MOUSE_X;
int16_t my = *MOUSE_Y;
uint8_t mb = *MOUSE_BUTTONS;
int
drawbtn(int16_t mx, int16_t my, uint8_t mb, int32_t x, int32_t y, const char *label)
{
uint32_t w;
uint32_t h;
int32_t center_x = (int32_t) (WINDOW_WIDTH * 0.5);
int32_t center_y = (int32_t) (WINDOW_HEIGHT* 0.5);
w = (uint32_t)strlen(label) * 8;
h = 10;
x = x - (int32_t)(w / 2);
if (state == 0) {
if (drawbtn(mx, my, mb, center_x, center_y + 40, "PLAY") == 1) {
state++;
}
}
else if (state == 1) {
switch (phase) {
case 0:
if (frame_count % speed == 0) {
if (dir == 0) {
if (radius < maxr) {
radius++;
} else {
dir = 1;
}
} else {
if (radius > minr) {
radius--;
} else {
dir = 0;
inccount(count);
}
}
}
if(mx > x && mx < x + (int16_t)w && my > y && my < y + (int16_t)h){
*DRAW_COLORS = 4;
if(mb & MOUSE_LEFT)
return 1;
}else{
*DRAW_COLORS = 3;
}
if (strcmp(count, max_breaths) == 0) {
strcpy(count, "000");
phase++;
}
break;
case 1:
if (frame_count % 60 == 0)
inccount(count);
rect(x, y, w, h);
if (drawbtn(mx, my, mb, center_x, center_y + 40, "BREATH") == 1) {
strcpy(results[round], count);
strcpy(count, "000");
phase++;
}
break;
case 2:
if (radius < maxr && strcmp(count, "000") == 0) {
if (frame_count % 30 == 0)
radius++;
*DRAW_COLORS = 2;
text(label, x, y + 1);
} else {
if (strcmp(count, "015") == 0 && radius > minr) {
if (frame_count % 30 == 0)
radius--;
if (radius == minr)
phase++;
}
if (frame_count % 60 == 0)
inccount(count);
}
break;
case 3:
if (round < MAX_ROUNDS) {
strcpy(count, "000");
round++;
phase = 0;
} else {
state++;
}
break;
}
} else {
}
if (state < 2) {
*DRAW_COLORS = 3;
int32_t current_x = center_x - (int32_t)radius;
int32_t current_y = center_y - (int32_t)radius;
oval(current_x, current_y, radius * 2, radius * 2);
*DRAW_COLORS = 2;
text(count, center_x - (3 * 4), center_y - 4);
} else {
text("RESULTS", center_x - 4 * 7, 10);
for (int i = 0; i < MAX_ROUNDS; i++) {
char txt_r[4];
txt_r[0] = 'R';
txt_r[1] = (char)(i + '1');
txt_r[2] = ':';
txt_r[3] = '\0';
text(txt_r, center_x - 30, 40 + i * 20);
text(&results[i][0], center_x - 4*3 + 20, 40 +i * 20);
}
if (drawbtn(mx, my, mb, center_x, WINDOW_HEIGHT - 40, "RESTART") == 1) {
state = 1;
round = 0;
}
}
frame_count++;
return 0;
}
void
start(void)
{
PALETTE[0] = 0xfff6d3;
PALETTE[1] = 0xf9a875;
PALETTE[2] = 0xeb6b6f;
PALETTE[3] = 0x7c3f58;
lotusinit(&lotus);
}
void
update(void)
{
int16_t mx;
int16_t my;
uint8_t mb;
int32_t center_x;
int32_t center_y;
int32_t current_x;
int32_t current_y;
int i;
char txt_r[4];
*DRAW_COLORS = 4;
mx = *MOUSE_X;
my = *MOUSE_Y;
mb = *MOUSE_BUTTONS;
center_x = (int32_t)(WINDOW_WIDTH * 0.5);
center_y = (int32_t)(WINDOW_HEIGHT * 0.5);
if(lotus.screen == LotusScreenStart){
if(drawbtn(mx, my, mb, center_x, center_y + 40, "PLAY") == 1)
lotus.screen = LotusScreenSession;
}else if(lotus.screen == LotusScreenSession){
switch(lotus.phase){
case LotusPhaseBreathe:
lotusbreath(&lotus);
break;
case LotusPhaseHold:
if(lotus.frame % 60 == 0)
inccount(lotus.count);
if(drawbtn(mx, my, mb, center_x, center_y + 40, "BREATH") == 1){
cpcount(results[lotus.round], lotus.count);
cpcount(lotus.count, "000");
lotus.phase = LotusPhaseRecover;
}
break;
case LotusPhaseRecover:
if(lotus.r < lotus.rmax && strcmp(lotus.count, "000") == 0){
if(lotus.frame % 30 == 0)
lotus.r++;
}else{
if(strcmp(lotus.count, "015") == 0 && lotus.r > lotus.rmin){
if(lotus.frame % 30 == 0)
lotus.r--;
if(lotus.r == lotus.rmin)
lotus.phase = LotusPhaseNext;
}
if(lotus.frame % 60 == 0)
inccount(lotus.count);
}
break;
case LotusPhaseNext:
if(lotus.round < MaxRounds - 1){
cpcount(lotus.count, "000");
lotus.round++;
lotus.phase = LotusPhaseBreathe;
}else{
lotus.screen = LotusScreenResults;
}
break;
}
}
if(lotus.screen < LotusScreenResults){
*DRAW_COLORS = 3;
current_x = center_x - (int32_t)lotus.r;
current_y = center_y - (int32_t)lotus.r;
oval(current_x, current_y, (uint32_t)lotus.r * 2, (uint32_t)lotus.r * 2);
*DRAW_COLORS = 2;
text(lotus.count, center_x - (3 * 4), center_y - 4);
}else{
text("RESULTS", center_x - 4 * 7, 10);
for(i = 0; i < MaxRounds; i++){
txt_r[0] = 'R';
txt_r[1] = (char)(i + '1');
txt_r[2] = ':';
txt_r[3] = 0;
text(txt_r, center_x - 30, 40 + i * 20);
text(results[i], center_x - 4 * 3 + 20, 40 + i * 20);
}
if(drawbtn(mx, my, mb, center_x, WINDOW_HEIGHT - 40, "RESTART") == 1)
lotusinit(&lotus);
}
lotus.frame++;
}