Skip to content

Commit

Permalink
lib: rename Bitmap to Image and move it to display module
Browse files Browse the repository at this point in the history
lua: ditto
doc: ditto
  • Loading branch information
and3rson committed Mar 1, 2024
1 parent fb8c516 commit 7b6b222
Show file tree
Hide file tree
Showing 18 changed files with 222 additions and 178 deletions.
3 changes: 3 additions & 0 deletions docs/library/display.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@

.. doxygenclass:: lilka::Canvas
:members:

.. doxygenclass:: lilka::Image
:members:
4 changes: 0 additions & 4 deletions docs/library/resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@
.. doxygenclass:: lilka::Resources
:members:
:undoc-members:

.. doxygenclass:: lilka::Bitmap
:members:
:undoc-members:
4 changes: 2 additions & 2 deletions docs/lua/display.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
local color = display.color565(255, 0, 0)
display.draw_line(0, 0, 100, 100, color)
local face = resources.load_bitmap("face.bmp", display.color565(0, 0, 0))
display.draw_bitmap(face, 50, 80)
local face = resources.load_image("face.bmp", display.color565(0, 0, 0))
display.draw_image(face, 50, 80)
display.set_buffered(true)
display.fill_rect(0, 0, 100, 100, color)
Expand Down
4 changes: 2 additions & 2 deletions docs/lua/resources.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
.. code-block:: lua
:linenos:
local face = resources.load_bitmap("face.bmp", display.color565(0, 0, 0))
display.draw_bitmap(face, 50, 80)
local face = resources.load_image("face.bmp", display.color565(0, 0, 0))
display.draw_image(face, 50, 80)
.. lua:autoclass:: resources
4 changes: 2 additions & 2 deletions docs/manual/lua.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
local ball_x = display.width / 2
local ball_y = display.height / 2
local ball = resources.load_bitmap("ball.bmp", display.color565(255, 255, 255))
local ball = resources.load_image("ball.bmp", display.color565(255, 255, 255))
function lilka._update()
local ball_speed_x = 0
Expand Down Expand Up @@ -91,7 +91,7 @@
-- Малюємо графіку:
display.fill_screen(display.color565(0, 0, 0))
display.draw_bitmap(ball, ball_x, ball_y)
display.draw_image(ball, ball_x, ball_y)
-- Оновлюємо екран:
display.render()
Expand Down
4 changes: 2 additions & 2 deletions firmware/main/sdcard/movement/movement.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local ball_x = display.width / 2
local ball_y = display.height / 2

local ball = resources.load_bitmap("ball.bmp", display.color565(255, 255, 255))
local ball = resources.load_image("ball.bmp", display.color565(255, 255, 255))

function lilka._update()
local ball_speed_x = 0
Expand Down Expand Up @@ -30,7 +30,7 @@ function lilka._update()

-- Малюємо графіку:
display.fill_screen(display.color565(0, 0, 0))
display.draw_bitmap(ball, ball_x, ball_y)
display.draw_image(ball, ball_x, ball_y)

-- Оновлюємо екран:
display.render()
Expand Down
20 changes: 10 additions & 10 deletions firmware/main/sdcard/runner/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Player = {
width = 32, -- Розмір спрайту - 32x32
height = 32,
sprites = {
stand = { resources.load_bitmap(ROOT .. "boy_stand.bmp") },
stand = { resources.load_image(ROOT .. "boy_stand.bmp") },
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),
resources.load_image(ROOT .. "boy_run_1.bmp", BLACK),
resources.load_image(ROOT .. "boy_run_2.bmp", BLACK),
resources.load_image(ROOT .. "boy_run_3.bmp", BLACK),
},
jump = { resources.load_bitmap(ROOT .. "boy_jump.bmp", BLACK) },
jump = { resources.load_image(ROOT .. "boy_jump.bmp", BLACK) },
},
state = PlayerState.RUN,
}
Expand All @@ -40,17 +40,17 @@ function Player:update(delta)
end

function Player:draw()
local bitmap
local image
if self.state == PlayerState.STAND then
bitmap = self.sprites.stand[1]
image = self.sprites.stand[1]
elseif self.state == PlayerState.RUN then
-- Перемикаємо спрайти бігу кожні 0.25 секунди
bitmap = self.sprites.run[math.floor(util.time() * 4) % #self.sprites.run + 1]
image = self.sprites.run[math.floor(util.time() * 4) % #self.sprites.run + 1]
elseif self.state == PlayerState.JUMP then
bitmap = self.sprites.jump[1]
image = self.sprites.jump[1]
end
-- Малюємо гравця на екрані так, щоб середина нижнього краю спрайту була в координатах (x, y)
display.draw_bitmap(bitmap, self.x - self.width / 2, self.y - self.height)
display.draw_image(image, self.x - self.width / 2, self.y - self.height)
end

local player = Player:new({ x = 128, y = 128 })
Expand Down
12 changes: 6 additions & 6 deletions firmware/main/sdcard/spacewar/spacewar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ ROOT = 'spacewar/'
ANGLE_COUNT = 60
ANGLE_STEP = 360 / ANGLE_COUNT

ship_sprites = { resources.load_bitmap(ROOT .. "ship.bmp", MAGENTA) }
SHIP_SPRITES = { resources.load_image(ROOT .. "ship.bmp", MAGENTA) }
for i = 2, ANGLE_COUNT do
local angle = i - 1
ship_sprites[i] = resources.rotate_bitmap(ship_sprites[1], angle * ANGLE_STEP, MAGENTA)
SHIP_SPRITES[i] = display.rotate_image(SHIP_SPRITES[1], angle * ANGLE_STEP, MAGENTA)
end

Ship = {
x = display.width / 2,
y = display.height / 2,
width = 32, -- Розмір спрайту - 32x32
height = 32,
sprites = ship_sprites,
sprites = SHIP_SPRITES,
rotation = 0, -- Поворот корабля в градусах
speed_x = 0,
speed_y = 0,
Expand Down Expand Up @@ -70,13 +70,13 @@ function Ship:draw()
-- Координати верхнього лівого кута спрайту
local cx = math.floor(self.x - self.width / 2)
local cy = math.floor(self.y - self.height / 2)
display.draw_bitmap(self.sprites[rotation_index], cx, cy)
display.draw_image(self.sprites[rotation_index], cx, cy)
-- Якщо ми біля краю екрану, малюємо корабель ще раз на протилежному боці
if self.x < self.width / 2 or self.x > display.width - self.width / 2 then
display.draw_bitmap(self.sprites[rotation_index], cx + display.width, cy)
display.draw_image(self.sprites[rotation_index], cx + display.width, cy)
end
if self.y < self.height / 2 or self.y > display.height - self.height / 2 then
display.draw_bitmap(self.sprites[rotation_index], cx, cy + display.height)
display.draw_image(self.sprites[rotation_index], cx, cy + display.height)
end
end

Expand Down
6 changes: 3 additions & 3 deletions firmware/main/sdcard/test.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
print("Printing stuff to console, yay!")

local face = resources.load_bitmap("face.bmp", display.color565(0, 0, 0))
local face = resources.load_image("face.bmp", display.color565(0, 0, 0))

print('Face size: ' .. face.width .. 'x' .. face.height)

Expand All @@ -11,7 +11,7 @@ for i = 10, 1, -1 do

local x = math.random(240 - 64)
local y = math.random(280 - 64)
display.draw_bitmap(face, x, y)
display.draw_image(face, x, y)

display.render()

Expand All @@ -32,7 +32,7 @@ while not key.a.just_pressed do

local x = math.random(240 - 64)
local y = math.random(280 - 64)
display.draw_bitmap(face, x, y)
display.draw_image(face, x, y)

key = controller.get_state()
end
15 changes: 11 additions & 4 deletions sdk/addons/lualilka/library/display.lua
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,20 @@ function display.draw_arc(x, y, r1, r2, start_angle, end_angle, color) end
function display.fill_arc(x, y, r1, r2, start_angle, end_angle, color) end

---Малює зображення на екрані.
---@param bitmap table ідентифікатор зображення
---@param image table ідентифікатор зображення
---@param x integer координата x лівого верхнього кута зображення
---@param y integer координата y лівого верхнього кута зображення
---@usage
--- local bitmap = resource.load_bitmap("face.bmp", display.color565(0, 0, 0))
--- display.draw_bitmap(bitmap, 50, 80) -- малює зображення в точці (50, 80)
function display.draw_bitmap(bitmap, x, y) end
--- local image = resource.load_image("face.bmp", display.color565(0, 0, 0))
--- display.draw_image(image, 50, 80) -- малює зображення в точці (50, 80)
function display.draw_image(image, x, y) end

---Повертає зображення на певну кількість градусів.
---
---@param image table ідентифікатор зображення
---@param angle integer кут (в градусах)
---@param blank_color integer колір для пікселів, які залишаться незаповненими
function display.rotate_image(image, angle, blank_color) end

---Буферизація дисплея дозволяє малювати всю графіку на полотні-буфері в пам'яті, а потім виводити цей буфер на екран. Це дозволяє уникнути мерехтіння зображення на екрані.
---Щоб вивести буфер на екран, використовуйте функцію :lua:func:`display.render`.
Expand Down
13 changes: 3 additions & 10 deletions sdk/addons/lualilka/library/resources.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,10 @@ resources = {}
---@param transparent_color? integer колір, який буде використаний для прозорості (5-6-5). Якщо цей параметр не вказаний, зображення буде виводитись без прозорості
---@return table
---@usage
--- local face = resources.load_bitmap("face.bmp", display.color565(0, 0, 0))
--- local face = resources.load_image("face.bmp", display.color565(0, 0, 0))
--- print(face.width, face.height) -- Виведе розміри зображення
--- display.draw_bitmap(face, 50, 80) -- Виведе зображення на екран у позицію (50, 80)
function resources.load_bitmap(filename, transparent_color) end

---Повертає зображення на певну кількість градусів.
---
---@param bitmap table ідентифікатор зображення
---@param angle integer кут (в градусах)
---@param blank_color integer колір для пікселів, які залишаться незаповненими
function resources.rotate_bitmap(bitmap, angle, blank_color) end
--- display.draw_image(face, 50, 80) -- Виведе зображення на екран у позицію (50, 80)
function resources.load_image(filename, transparent_color) end

---Читає вміст файлу і повертає його як текст.
---@param filename string шлях до файлу (відносно місця знаходження скрипта, що виконується)
Expand Down
49 changes: 47 additions & 2 deletions sdk/lib/lilka/src/lilka/display.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "display.h"

#include "splash.h"

#include "spi.h"
#include "serial.h"
#include "fmath.h"

namespace lilka {

Expand Down Expand Up @@ -66,6 +66,14 @@ void Display::begin() {
serial_log("display ok");
}

void Display::drawImage(Image *image, int16_t x, int16_t y) {
if (image->transparentColor == -1) {
draw16bitRGBBitmap(x, y, image->pixels, image->width, image->height);
} else {
draw16bitRGBBitmapWithTranColor(x, y, image->pixels, image->transparentColor, image->width, image->height);
}
}

// Чомусь в Arduino_GFX немає варіанту цього методу для const uint16_t[] - є лише для uint16_t.
void Display::draw16bitRGBBitmapWithTranColor(
int16_t x, int16_t y, const uint16_t bitmap[], uint16_t transparent_color, int16_t w, int16_t h
Expand All @@ -83,11 +91,48 @@ Canvas::Canvas() : Arduino_Canvas(LILKA_DISPLAY_WIDTH, LILKA_DISPLAY_HEIGHT, NUL
setUTF8Print(true);
}

void Canvas::drawImage(Image *image, int16_t x, int16_t y) {
if (image->transparentColor == -1) {
draw16bitRGBBitmap(x, y, image->pixels, image->width, image->height);
} else {
draw16bitRGBBitmapWithTranColor(x, y, image->pixels, image->transparentColor, image->width, image->height);
}
}

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.
Arduino_Canvas::draw16bitRGBBitmapWithTranColor(x, y, const_cast<uint16_t * const>(bitmap), transparent_color, w, h);
Arduino_Canvas::draw16bitRGBBitmapWithTranColor(x, y, const_cast<uint16_t *const>(bitmap), transparent_color, w, h);
}

Image::Image(uint32_t width, uint32_t height, int32_t transparentColor)
: width(width), height(height), transparentColor(transparentColor) {
pixels = new uint16_t[width * height];
}

Image::~Image() {
delete[] pixels;
}

void Image::rotate(int16_t angle, Image *dest, int32_t blankColor) {
// Rotate the image
int cx = width / 2;
int cy = height / 2;

for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int dx = x - cx;
int dy = y - cy;
int x2 = cx + dx * fCos360(angle) - dy * fSin360(angle);
int y2 = cy + dx * fSin360(angle) + dy * fCos360(angle);
if (x2 >= 0 && x2 < width && y2 >= 0 && y2 < height) {
dest->pixels[x + y * width] = pixels[x2 + y2 * width];
} else {
dest->pixels[x + y * width] = blankColor;
}
}
}
}

Display display;
Expand Down
Loading

0 comments on commit 7b6b222

Please sign in to comment.