Skip to content

Commit

Permalink
fix canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
AGulev committed Apr 13, 2018
1 parent 2363608 commit 955619f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 25 deletions.
23 changes: 12 additions & 11 deletions drawpixels/src/drawpixels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ struct BufferInfo
dmBuffer::HBuffer buffer;
int width;
int height;
int colors_count;
int chanels;
uint8_t* bytes;
uint32_t src_size;
};
Expand All @@ -24,15 +24,15 @@ static int xytoi(int x, int y) {
if (y < 0) y = 0;
if (x >= buffer_info.width) x = buffer_info.width - 1;
if (y >= buffer_info.height) y = buffer_info.height - 1;
return (y * buffer_info.width * buffer_info.colors_count) + (x * buffer_info.colors_count);
return (y * buffer_info.width * buffer_info.chanels) + (x * buffer_info.chanels);
}

static void read_and_validate_buffer_info(lua_State* L, int index) {
luaL_checktype(L, index, LUA_TTABLE);
lua_getfield(L, index, "buffer");
lua_getfield(L, index, "width");
lua_getfield(L, index, "height");
lua_getfield(L, index, "colors_count");
lua_getfield(L, index, "chanels");
dmScript::LuaHBuffer *lua_buffer = dmScript::CheckBuffer(L, -4);
buffer_info.buffer = lua_buffer->m_Buffer;

Expand All @@ -55,9 +55,9 @@ static void read_and_validate_buffer_info(lua_State* L, int index) {
luaL_error(L, "'height' of the buffer should be an integer and > 0");
}

buffer_info.colors_count = lua_tointeger(L, -1);
if (buffer_info.colors_count == 0) {
luaL_error(L, "'colors_count' of the buffer should be an integer and > 0");
buffer_info.chanels = lua_tointeger(L, -1);
if (buffer_info.chanels == 0) {
luaL_error(L, "'chanels' of the buffer should be an integer and > 0");
}
}

Expand All @@ -66,7 +66,7 @@ static void putpixel(int x, int y, int r, int g,int b, int a){
buffer_info.bytes[i] = r;
buffer_info.bytes[i + 1] = g;
buffer_info.bytes[i + 2] = b;
if (buffer_info.colors_count == 4) {
if (buffer_info.chanels == 4) {
buffer_info.bytes[i + 3] = a;
}
}
Expand All @@ -76,7 +76,7 @@ static void fill_line(int from, int to, int r, int g,int b, int a ){
buffer_info.bytes[i] = r;
buffer_info.bytes[i + 1] = g;
buffer_info.bytes[i + 2] = b;
if (buffer_info.colors_count == 4) {
if (buffer_info.chanels == 4) {
buffer_info.bytes[i + 3] = a;
}
}
Expand Down Expand Up @@ -149,6 +149,7 @@ static int draw_filled_circle(lua_State* L) {
{
a = luaL_checknumber(L, 8);
}

int radius = diametr/2;
int x = radius-1;
int y = 0;
Expand Down Expand Up @@ -195,11 +196,11 @@ static int fill_texture(lua_State* L) {
a = luaL_checknumber(L, 5);
}

for(int i = 0; i < buffer_info.src_size; i += buffer_info.colors_count) {
for(int i = 0; i < buffer_info.src_size; i += buffer_info.chanels) {
buffer_info.bytes[i] = r;
buffer_info.bytes[i + 1] = g;
buffer_info.bytes[i + 2] = b;
if (buffer_info.colors_count == 4) {
if (buffer_info.chanels == 4) {
buffer_info.bytes[i + 3] = a;
}
}
Expand Down Expand Up @@ -237,7 +238,7 @@ static int draw_rect(lua_State* L) {
buffer_info.bytes[i] = r;
buffer_info.bytes[i + 1] = g;
buffer_info.bytes[i + 2] = b;
if (buffer_info.colors_count == 4) {
if (buffer_info.chanels == 4) {
buffer_info.bytes[i + 3] = a;
}
}
Expand Down
21 changes: 9 additions & 12 deletions example/canvas.script
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ function init(self)
msg.post(".", "acquire_input_focus")

-- size of texture when scaled to nearest power of two
local width = 512
local height = 1024
local width = 1024
local height = 2048
local chanels = 4--- 3 for rgb, 4 for rgba

local our_buffer = buffer.create(width * height, {{name = hash("rgba"), type = buffer.VALUE_TYPE_UINT8, count = 4}})
local our_buffer = buffer.create(width * height, {{name = hash("rgba"), type = buffer.VALUE_TYPE_UINT8, count = chanels}})

self.buffer_info = {
buffer = our_buffer,
width = width,
height = height,
colors_count = 4 --- 3 for rgb, 4 for rgba
chanels = chanels
}

drawpixels.fill(self.buffer_info, 128, 128, 128)
-- drawpixels.fill(self.buffer_info, 128, 128, 128)
self.dirty = true

-- drawing params
Expand Down Expand Up @@ -62,17 +63,13 @@ function on_input(self, action_id, action)
-- use current tool from the previous touch position to
-- the current touch position0
while length > 0 do
drawpixels.rect(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 40, 40, 255, 255, 255, 255)
-- drawpixels.circle(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 120, 255, 255, 255, 255)
-- drawpixels.rect(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 40, 40, 255, 255, 255, 255)
-- drawpixels.filled_circle(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 80, 255, 255, 255, 255)
drawpixels.circle(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 80, 255, 255, 255, 255)
self.dirty = true
self.touch_pos = self.touch_pos - dir
length = length - 1
end
end
end
end

function on_reload(self)
-- Add reload-handling code here
-- Remove this function if not needed
end
4 changes: 2 additions & 2 deletions example/example.collection
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ embedded_instances {
"}\n"
""
position {
x: 256.0
y: 512.0
x: 512.0
y: 1024.0
z: 0.0
}
rotation {
Expand Down
Binary file modified example/images/fullscreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 955619f

Please sign in to comment.