Merge pull request #690 from MihailRis/more-canvas-methods

Add more canvas methods
This commit is contained in:
MihailRis 2025-11-22 18:13:05 +03:00 committed by GitHub
commit acea4604fe
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 2 deletions

View File

@ -194,6 +194,7 @@ Here, *color* can be specified in the following ways:
| data:blit(src: Canvas, dst_x: int, dst_y: int) | draws the src canvas at the specified coordinates | | data:blit(src: Canvas, dst_x: int, dst_y: int) | draws the src canvas at the specified coordinates |
| data:clear() | clears the canvas | | data:clear() | clears the canvas |
| data:clear(*color*) | fills the canvas with the specified RGBA color | | data:clear(*color*) | fills the canvas with the specified RGBA color |
| data:rect(x: int, y: int, w: int, h: int, *color*) | fills the rectangle with the specified RGBA color |
| data:update() | applies changes to the canvas and uploads it to the GPU | | data:update() | applies changes to the canvas and uploads it to the GPU |
| data:set_data(data: Bytearray | table<int>) | replaces pixel data (width * height * 4 numbers) | | data:set_data(data: Bytearray | table<int>) | replaces pixel data (width * height * 4 numbers) |
| data:get_data() | creates a Bytearray object with the image's pixel data | | data:get_data() | creates a Bytearray object with the image's pixel data |

View File

@ -194,6 +194,7 @@ document["worlds-panel"]:clear()
| data:blit(src: Canvas, dst_x: int, dst_y: int) | рисует src-холст на указанных координатах | | data:blit(src: Canvas, dst_x: int, dst_y: int) | рисует src-холст на указанных координатах |
| data:clear() | очищает холст | | data:clear() | очищает холст |
| data:clear(*цвет*) | заполняет холст указанным RGBA цветом | | data:clear(*цвет*) | заполняет холст указанным RGBA цветом |
| data:rect(x: int, y: int, w: int, h: int, *цвет*) | заполняет прямоугольник указанным RGBA цветом |
| data:update() | применяет изменения и загружает холст в видеопамять | | data:update() | применяет изменения и загружает холст в видеопамять |
| data:set_data(data: bytearray | table<int>) | заменяет данные пикселей (ширина * высота * 4 чисел) | | data:set_data(data: bytearray | table<int>) | заменяет данные пикселей (ширина * высота * 4 чисел) |
| data:get_data() | создаёт объект Bytearray с пиксельными данными изображения | | data:get_data() | создаёт объект Bytearray с пиксельными данными изображения |

View File

@ -16,7 +16,7 @@ ImageData::ImageData(ImageFormat format, uint width, uint height)
default: default:
throw std::runtime_error("format is not supported"); throw std::runtime_error("format is not supported");
} }
data = std::make_unique<ubyte[]>(width * height * pixsize); data = std::make_unique<ubyte[]>((width + width % 2) * (height + width % 2) * pixsize);
} }
ImageData::ImageData(ImageFormat format, uint width, uint height, std::unique_ptr<ubyte[]> data) ImageData::ImageData(ImageFormat format, uint width, uint height, std::unique_ptr<ubyte[]> data)

View File

@ -125,7 +125,9 @@ static RGBA get_rgba(State* L, int first) {
rgba.rgba = static_cast<uint>(tointeger(L, first)); rgba.rgba = static_cast<uint>(tointeger(L, first));
break; break;
case 3: case 3:
rgba.a = static_cast<ubyte>(tointeger(L, first + 3)); if (lua::isnumber(L, first + 3)) {
rgba.a = static_cast<ubyte>(tointeger(L, first + 3));
}
[[fallthrough]]; [[fallthrough]];
case 2: case 2:
rgba.r = static_cast<ubyte>(tointeger(L, first)); rgba.r = static_cast<ubyte>(tointeger(L, first));
@ -199,6 +201,18 @@ static int l_blit(State* L) {
return 0; return 0;
} }
static int l_rect(State* L) {
auto& canvas = require_canvas(L, 1);
auto& image = canvas.getData();
int x = tointeger(L, 2);
int y = tointeger(L, 3);
int w = tointeger(L, 4);
int h = tointeger(L, 5);
RGBA rgba = get_rgba(L, 6);
image.drawRect(x, y, w, h, glm::ivec4 {rgba.r, rgba.g, rgba.b, rgba.a});
return 0;
}
static int l_set_data(State* L) { static int l_set_data(State* L) {
auto& canvas = require_canvas(L, 1); auto& canvas = require_canvas(L, 1);
auto& image = canvas.getData(); auto& image = canvas.getData();
@ -316,6 +330,7 @@ static std::unordered_map<std::string, lua_CFunction> methods {
{"line", lua::wrap<l_line>}, {"line", lua::wrap<l_line>},
{"blit", lua::wrap<l_blit>}, {"blit", lua::wrap<l_blit>},
{"clear", lua::wrap<l_clear>}, {"clear", lua::wrap<l_clear>},
{"rect", lua::wrap<l_rect>},
{"update", lua::wrap<l_update>}, {"update", lua::wrap<l_update>},
{"create_texture", lua::wrap<l_create_texture>}, {"create_texture", lua::wrap<l_create_texture>},
{"unbind_texture", lua::wrap<l_unbind_texture>}, {"unbind_texture", lua::wrap<l_unbind_texture>},