Skip to content

Commit

Permalink
main: fix Lua Live mode
Browse files Browse the repository at this point in the history
main: WIP runner game
lib: add support for 32-bit BMP images
  • Loading branch information
and3rson committed Feb 29, 2024
1 parent a52f38c commit 7962761
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .luarc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
"workspace.library": ["sdk/addons/lualilka"],
"workspace.library": ["sdk/addons/lualilka/library"],
"runtime.version": "Lua 5.4",
"hint.enable": false
}
23 changes: 23 additions & 0 deletions firmware/main/sdcard/runner/1.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Player = {
x = 2,
y = 3,
}

function Player:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end

function Player:move(dx, dy)
self.x = self.x + dx
self.y = self.y + dy
end

-- local player = Player:new({})

Player:move(1, 1)

-- print(player.x)
-- player:move(1, 1)
Binary file added firmware/main/sdcard/runner/boy_jump.bmp
Binary file not shown.
Binary file added firmware/main/sdcard/runner/boy_run_1.bmp
Binary file not shown.
Binary file added firmware/main/sdcard/runner/boy_run_2.bmp
Binary file not shown.
Binary file added firmware/main/sdcard/runner/boy_run_3.bmp
Binary file not shown.
Binary file added firmware/main/sdcard/runner/boy_stand.bmp
Binary file not shown.
48 changes: 48 additions & 0 deletions firmware/main/sdcard/runner/runner.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- Character sprites by https://laredgames.itch.io/coins-free

local WHITE = display.color565(255, 255, 255)
local BLACK = display.color565(0, 0, 0)

local ROOT = 'runner/'

local Player = {
x = 0,
y = 0,
width = 32, -- Розмір спрайту - 32x32
height = 32,
sprites = {
-- stand = { resources.load_bitmap(ROOT .. "boy_stand.bmp") },
stand = { resources.load_bitmap("face.bmp", BLACK) },
run = {
resources.load_bitmap(ROOT .. "boy_run_1.bmp", BLACK),
resources.load_bitmap(ROOT .. "boy_run_2.bmp", BLACK),
resources.load_bitmap(ROOT .. "boy_run_3.bmp", BLACK),
},
},
}

function Player:new(o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end

function Player:draw()
-- Малюємо гравця на екрані так, щоб середина нижнього краю спрайту була в координатах (x, y)
display.draw_bitmap(self.sprites.run[1], self.x - self.width / 2, self.y - self.height)
end

local player = Player:new({ x = 128, y = 128 })

-- display.fill_screen(WHITE)
-- display.draw_bitmap(sprites.run[1], 128, 128)
-- display.render()
-- util.delay(1)

-- display.fill_screen(WHITE)
-- display.render()
-- util.delay(1)
-- display.fill_screen(BLACK)
-- display.render()
-- util.delay(1)
22 changes: 19 additions & 3 deletions firmware/main/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ void live_lua() {
if (lilka::controller.getState().a.justPressed) {
return;
}
String line = Serial.readStringUntil('\n');
String line = Serial.readString();
if (line.length() == 0) {
lilka::display.print("!");
break;
Expand All @@ -252,8 +252,24 @@ void live_lua() {
code += line;
}

Serial.println("Code received:");
Serial.println(code);
// Serial.println("Code received:");
// Serial.println(code);

// Those darn line ends...
// If code contains \r and \n - replace them with \n
// If code contains only \r - replace it with \n
// If code contains only \n - leave it as is
if (code.indexOf('\r') != -1) {
if (code.indexOf('\n') != -1) {
lilka::serial_log("Line ends: CR and LF");
code.replace("\r", "");
} else {
lilka::serial_log("Line ends: CR only");
code.replace("\r", "\n");
}
} else {
lilka::serial_log("Line ends: LF only");
}

// Run the code
int retCode = lilka::lua_runsource(code);
Expand Down
3 changes: 1 addition & 2 deletions sdk/lib/lilka/src/lilka/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ void Display::begin() {
#else
Arduino_ST7789::begin(80000000);
#endif
setFont(u8g2_font_10x20_t_cyrillic);
setFont(FONT_10x20);
setUTF8Print(true);
#ifndef LILKA_NO_SPLASH
uint16_t row[LILKA_DISPLAY_WIDTH];
Expand Down Expand Up @@ -87,7 +87,6 @@ void Canvas::draw16bitRGBBitmapWithTranColor(
int16_t x, int16_t y, const uint16_t bitmap[], uint16_t transparent_color, int16_t w, int16_t h
) {
// Цей cast безпечний, оскільки Arduino_GFX.draw16bitRGBBitmapWithTranColor не змінює bitmap.
Serial.println("const");
Arduino_Canvas::draw16bitRGBBitmapWithTranColor(x, y, const_cast<uint16_t * const>(bitmap), transparent_color, w, h);
}

Expand Down
15 changes: 14 additions & 1 deletion sdk/lib/lilka/src/lilka/resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Bitmap* Resources::loadBitmap(String filename, int32_t transparentColor) {
int dataOffset = fileheader[10] + (fileheader[11] << 8) + (fileheader[12] << 16) + (fileheader[13] << 24);
uint8_t fileinfo[40];
fread(fileinfo, 1, 40, file);
// Get number of bits per pixel (offset from start of file: 0x1C)
int bitsPerPixel = fileinfo[14] + (fileinfo[15] << 8);
uint32_t width = fileinfo[4] + (fileinfo[5] << 8) + (fileinfo[6] << 16) + (fileinfo[7] << 24);
uint32_t height = fileinfo[8] + (fileinfo[9] << 8) + (fileinfo[10] << 16) + (fileinfo[11] << 24);
// int bitsPerPixel = fileinfo[14] + (fileinfo[15] << 8);
Expand All @@ -39,7 +41,18 @@ Bitmap* Resources::loadBitmap(String filename, int32_t transparentColor) {
for (int y = height - 1; y >= 0; y--) {
for (int x = 0; x < width; x++) {
uint32_t color;
fread(&color, 1, 3, file);
if (bitsPerPixel == 24) {
fread(&color, 1, 3, file);
} else if (bitsPerPixel == 32) {
fread(&color, 1, 4, file);
} else {
// TODO
serial_err("Unsupported bits per pixel: %d\n", bitsPerPixel);
fclose(file);
delete bitmap;
return 0;
}

bitmap->pixels[x + y * width] = display.color565((color >> 16) & 0xFF, (color >> 8) & 0xFF, color & 0xFF);
}
}
Expand Down
8 changes: 4 additions & 4 deletions sdk/lib/lilka/src/lilka/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ int ui_menu(String title, String menu[], int menu_size, int cursor, const menu_i
);
canvas.setCursor(32, 48);
canvas.setTextColor(canvas.color565(255, 255, 255));
canvas.setFont(u8g2_font_6x13_t_cyrillic);
canvas.setFont(FONT_6x13);
canvas.setTextSize(2);
canvas.println(title);
canvas.println();
canvas.setTextSize(1);
canvas.setFont(u8g2_font_10x20_t_cyrillic);
canvas.setFont(FONT_10x20);

canvas.fillRect(0, cursorY - scroll * 24, LILKA_DISPLAY_WIDTH, 24, canvas.color565(255, 64, 0));
if (cursorY < desiredCursorY) {
Expand Down Expand Up @@ -121,14 +121,14 @@ void ui_alert(String title, String message) {
controller.resetState();

display.fillRect(left, top, width, mid - top, display.color565(32, 32, 128));
display.setFont(u8g2_font_6x13_t_cyrillic);
display.setFont(FONT_6x13);
display.setTextSize(2);
display.setTextBound(left + xMargin, top, width - xMargin * 2, mid - top);
display.setCursor(left + xMargin, top + 13 * 2);
display.println(title);

display.fillRect(left, mid, width, bottom - mid, display.color565(32, 96, 96));
display.setFont(u8g2_font_10x20_t_cyrillic);
display.setFont(FONT_9x15);
display.setTextSize(1);
display.setTextBound(left + xMargin, top, width - xMargin * 2, bottom - mid);
display.setCursor(left + xMargin, mid + 20);
Expand Down
7 changes: 7 additions & 0 deletions sdk/lib/lilka/src/lua/lualilka_display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,13 @@ int lualilka_display_drawBitmap(lua_State* L) {
// Args are bitmap table, X & Y
// First argument is table that contains bitmap width, height and pointer. We only need the pointer.
lua_getfield(L, 1, "pointer");
// Check if value is a valid pointer

// TODO: Check if crap ain't broken since the user may pass anything here and this often causes core panic
if (!lua_islightuserdata(L, -1)) {
return luaL_error(L, "Invalid bitmap");
}

Bitmap* bitmap = (Bitmap*)lua_touserdata(L, -1);
lua_pop(L, 1);

Expand Down
4 changes: 2 additions & 2 deletions sdk/lib/lilka/src/lua/luarunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ lua_State* lua_setup(const char* dir) {

lilka::serial_log("lua: init canvas");
lilka::Canvas* canvas = new lilka::Canvas();
lilka::display.setFont(u8g2_font_10x20_t_cyrillic);
canvas->setFont(u8g2_font_10x20_t_cyrillic);
lilka::display.setFont(FONT_10x20);
canvas->setFont(FONT_10x20);
canvas->begin();
// Store canvas in registry with "canvas" key
lilka::serial_log("lua: store canvas in registry");
Expand Down

0 comments on commit 7962761

Please sign in to comment.