files
syscalls/src/graphic.c
2025-04-11 14:49:53 +05:00

209 lines
4.6 KiB
C

#include "graphic.h"
#include "debug.h"
/*
Кеш размера экрана.
Нужно для того чтобы не вызывать систменое прерывание лишний раз (другие функции тоже используют это значение)
Сомневаюсь что в размер экрана в колибри вообще может меняться без перезагрузки
обновляется функцией syscalls_updateScreenSize
*/
static ksys_pos_t syscalls_screenSizeCache = { 0 };
inline void syscalls_updateScreenSize()
{
syscalls_screenSizeCache = _ksys_screen_size();
}
int syscalls_drawLine(lua_State* L)
{
_ksys_draw_line(
luaL_checkinteger(L, 1),
luaL_checkinteger(L, 2),
luaL_checkinteger(L, 3),
luaL_checkinteger(L, 4),
luaL_checkinteger(L, 5));
return 0;
}
static inline void drawText(char* text, uint32_t x, uint32_t y, ksys_color_t color, size_t len, uint64_t backgroundColor)
{
if (backgroundColor < (1 << 32))
color |= (1 << 30);
if (len == 0)
color |= (1 << 31);
asm_inline(
"int $0x40"
::"a"(4),
"b"((x << 16) | y),
"c"(color),
"d"(text),
"S"(len),
"D"((uint32_t)backgroundColor));
}
static inline void drawTextFixSize(const char* text, uint32_t x, uint32_t y, ksys_color_t color, enum TextScale size, uint32_t len, uint64_t backgroundColor, enum DrawTextEncoding encoding)
{
enum DrawTextEncoding_
{
cp866_6x9 = 0,
cp866_8x16 = 1
};
enum scale
{
scale_x1 = 0,
scale_x2 = 1,
scale_x3 = 2,
scale_x4 = 3,
scale_x5 = 4,
scale_x6 = 5,
scale_x7 = 6,
scale_x8 = 7
};
color &= 0x00ffffff;
switch (size)
{
case TextScale_SIZE_6x9:
color |= (cp866_6x9 << 28) | (scale_x1 << 24);
break;
case TextScale_SIZE_8x16:
color |= (encoding << 28) | (scale_x1 << 24);
break;
case TextScale_SIZE_12x18:
color |= (cp866_6x9 << 28) | (scale_x2 << 24);
break;
case TextScale_SIZE_16x32:
color |= (encoding << 28) | (scale_x2 << 24);
break;
case TextScale_SIZE_18x27:
color |= (cp866_6x9 << 28) | (scale_x3 << 24);
break;
case TextScale_SIZE_24x36:
color |= (cp866_6x9 << 28) | (scale_x4 << 24);
break;
case TextScale_SIZE_24x48:
color |= (encoding << 28) | (scale_x3 << 24);
break;
case TextScale_SIZE_30x45:
color |= (cp866_6x9 << 28) | (scale_x5 << 24);
break;
case TextScale_SIZE_32x64:
color |= (encoding << 28) | (scale_x4 << 24);
break;
case TextScale_SIZE_36x54:
color |= (cp866_6x9 << 28) | (scale_x6 << 24);
break;
case TextScale_SIZE_40x80:
color |= (encoding << 28) | (scale_x5 << 24);
break;
case TextScale_SIZE_42x63:
color |= (cp866_6x9 << 28) | (scale_x7 << 24);
break;
case TextScale_SIZE_48x72:
color |= (cp866_6x9 << 28) | (scale_x8 << 24);
break;
case TextScale_SIZE_48x96:
color |= (encoding << 28) | (scale_x6 << 24);
break;
case TextScale_SIZE_56x112:
color |= (encoding << 28) | (scale_x7 << 24);
break;
case TextScale_SIZE_64x128:
color |= (encoding << 28) | (scale_x8 << 24);
break;
default:
_ksys_debug_puts("Unknown size");
break;
};
drawText(text, x, y, color, len, backgroundColor);
}
int syscalls_drawText(lua_State* L)
{
drawText(
luaL_checkstring(L, 1),
luaL_checkinteger(L, 2),
luaL_checkinteger(L, 3),
(luaL_checkinteger(L, 4) & 0x00ffffff) |
((luaL_optinteger(L, 8, DEFAULT_ENCODING) & 0b11) << 28) |
(luaL_optinteger(L, 5, 1) << 24),
luaL_optinteger(L, 6, 0),
luaL_optinteger(L, 7, 1 << 32)
);
return 0;
}
int syscalls_drawTextFixSize(lua_State* L)
{
drawTextFixSize(
luaL_checkstring(L, 1),
luaL_checkinteger(L, 2),
luaL_checkinteger(L, 3),
luaL_checkinteger(L, 4),
luaL_optinteger(L, 5, TextScale_SIZE_8x16),
luaL_optinteger(L, 6, 0),
luaL_optinteger(L, 7, 1 << 32),
luaL_optinteger(L, 8, DEFAULT_ENCODING)
);
return 0;
}
int syscalls_drawRectangle(lua_State* L)
{
_ksys_draw_bar(
luaL_checkinteger(L, 1),
luaL_checkinteger(L, 2),
luaL_checkinteger(L, 3),
luaL_checkinteger(L, 4),
luaL_checkinteger(L, 5));
return 0;
}
int syscalls_ReadPoint(lua_State* L)
{
ksys_color_t color;
if (syscalls_screenSizeCache.val == 0)
syscalls_updateScreenSize();
uint32_t x = luaL_checkinteger(L, 1);
uint32_t y = luaL_checkinteger(L, 2);
asm_inline(
"int $ 0x40"
: "=a"(color)
: "a"(35), "b"(x * syscalls_screenSizeCache.x + y));
lua_pushinteger(L, color);
return 1;
}
int syscalls_screenSize(lua_State* L)
{
syscalls_updateScreenSize();
lua_createtable(L, 0, 2);
lua_pushinteger(L, syscalls_screenSizeCache.x);
lua_setfield(L, -2, "x");
lua_pushinteger(L, syscalls_screenSizeCache.y);
lua_setfield(L, -2, "y");
return 1;
}