Skip to content

Commit

Permalink
lua: add lilka.show_fps
Browse files Browse the repository at this point in the history
  • Loading branch information
and3rson committed Mar 3, 2024
1 parent 4cca744 commit 72df6f0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
9 changes: 7 additions & 2 deletions docs/lua/lilka.rst
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
``lilka``: Автоматичні функції
==============================
``lilka``: Спеціальні функції
=============================

Цей модуль описує функції, які ви можете визначити в своєму коді, щоб вони викликались автоматично в певних ситуаціях.

Expand Down Expand Up @@ -27,3 +27,8 @@
Ця функція автоматично викликається після `lilka.update`. Тут відбувається відображення графіки.

Ви повинні визначити її в своєму коді, якщо ви хочете використовувати її для малювання вашої програми.

.. lua:autoclass:: lilka
.. Does not work with aliases... So we have to copy-paste the stuff above.
.. :members: init, update, render
2 changes: 2 additions & 0 deletions firmware/main/sdcard/asteroids/asteroids.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ MAGENTA = display.color565(255, 0, 255)
-- ROOT = 'asteroids/'
ROOT = ''

-- lilka.show_fps = true

-------------------------------------------------------------------------------
-- Загальні константи
-------------------------------------------------------------------------------
Expand Down
9 changes: 6 additions & 3 deletions sdk/addons/lualilka/library/lilka.lua
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
---@meta

---@class lilka
---@field show_fps boolean Показувати частоту кадрів на екрані, якщо встановлено в ``true``. Наприклад: ``lilka.show_fps = true``
lilka = {}

---Ця функція автоматично викликається один раз при запуску програми.
---
---Ви повинні визначити її в своєму коді, якщо ви хочете використовувати її для ініціалізації вашої програми.
---lilka.init fun()
---@alias lilka.init fun()

---Ця функція автоматично викликається кожен кадр. Тут виконується ігрова логіка. В параметрі delta передається час, який пройшов з останнього кадру.
---
---Ви повинні визначити її в своєму коді, якщо ви хочете використовувати її для оновлення вашої програми.
---lilka.update fun(delta: number)
---@alias lilka.update fun(delta: number)

---Ця функція автоматично викликається після `lilka.update`. Тут відбувається відображення графіки.
---
---Ви повинні визначити її в своєму коді, якщо ви хочете використовувати її для малювання вашої програми.
---lilka.render fun()
---@alias lilka.render fun()

return lilka
14 changes: 13 additions & 1 deletion sdk/lib/lilka/src/lua/luarunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool callUpdate(lua_State* L, uint32_t delta) {
lua_pop(L, 1);
return false;
}
lua_pushnumber(L, delta / 1000.0);
lua_pushnumber(L, ((float)delta) / 1000.0);
int retCode = lua_pcall(L, 1, 0, 0);
lua_Debug dbg;
if (retCode) {
Expand Down Expand Up @@ -129,6 +129,15 @@ int execute(lua_State* L) {
serial_log("lua: no update or draw function");
}

// Check if show_fps is true and render FPS
lua_getfield(L, -1, "show_fps");
if (lua_toboolean(L, -1)) {
canvas->setCursor(24, 24);
canvas->setTextColor(0xFFFF, 0);
canvas->print(String("FPS: ") + (1000 / (delta > 0 ? delta : 1)) + " ");
}
lua_pop(L, 1);

display.renderCanvas(*canvas);

// Calculate time spent in update
Expand Down Expand Up @@ -202,6 +211,9 @@ lua_State* lua_setup(const char* dir) {

// Set global "lilka" table for user stuff
lua_newtable(L);
// Add show_fps attribute to lilka table that defaults to false
lua_pushboolean(L, false);
lua_setfield(L, -2, "show_fps");
lua_setglobal(L, "lilka");

return L;
Expand Down

0 comments on commit 72df6f0

Please sign in to comment.