Skip to content

Commit

Permalink
example added.
Browse files Browse the repository at this point in the history
bugfixes
  • Loading branch information
AGulev committed Apr 13, 2018
1 parent 955619f commit 8f88a1d
Show file tree
Hide file tree
Showing 15 changed files with 1,376 additions and 26 deletions.
75 changes: 60 additions & 15 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 chanels;
int channels;
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.chanels) + (x * buffer_info.chanels);
return (y * buffer_info.width * buffer_info.channels) + (x * buffer_info.channels);
}

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, "chanels");
lua_getfield(L, index, "channels");
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.chanels = lua_tointeger(L, -1);
if (buffer_info.chanels == 0) {
luaL_error(L, "'chanels' of the buffer should be an integer and > 0");
buffer_info.channels = lua_tointeger(L, -1);
if (buffer_info.channels == 0) {
luaL_error(L, "'channels' of should be an integer and 3 or 4");
}
}

Expand All @@ -66,17 +66,17 @@ 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.chanels == 4) {
if (buffer_info.channels == 4) {
buffer_info.bytes[i + 3] = a;
}
}

static void fill_line(int from, int to, int r, int g,int b, int a ){
for (int i = from; i < to; i++) {
static void fill_line(int from, int to, int r, int g, int b, int a){
for (int i = from; i <= to; i += buffer_info.channels) {
buffer_info.bytes[i] = r;
buffer_info.bytes[i + 1] = g;
buffer_info.bytes[i + 2] = b;
if (buffer_info.chanels == 4) {
if (buffer_info.channels == 4) {
buffer_info.bytes[i + 3] = a;
}
}
Expand Down Expand Up @@ -156,7 +156,6 @@ static int draw_filled_circle(lua_State* L) {
int dx = 1;
int dy = 1;
int err = dx - (radius << 1);

while (x >= y)
{
fill_line(xytoi(posx - x, posy + y), xytoi(posx + x, posy + y), r, g, b, a);
Expand Down Expand Up @@ -195,12 +194,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.chanels) {
for(int i = 0; i < buffer_info.src_size; i += buffer_info.channels) {
buffer_info.bytes[i] = r;
buffer_info.bytes[i + 1] = g;
buffer_info.bytes[i + 2] = b;
if (buffer_info.chanels == 4) {
if (buffer_info.channels == 4) {
buffer_info.bytes[i + 3] = a;
}
}
Expand All @@ -226,6 +224,52 @@ static int draw_rect(lua_State* L) {
a = luaL_checknumber(L, 9);
}

int half_size_x = sizex/2;
int half_size_y = sizey/2;
int newposx = 0;
int newposy = 0;
for(int y = -half_size_y; y < half_size_y; y++) {
if (y == -half_size_y || y == half_size_y - 1 ) {
for(int x = -half_size_x; x < half_size_x; x++) {
newposx = x + posx;
newposy = y + posy;
int i = xytoi(newposx, newposy);
buffer_info.bytes[i] = r;
buffer_info.bytes[i + 1] = g;
buffer_info.bytes[i + 2] = b;
if (buffer_info.channels == 4) {
buffer_info.bytes[i + 3] = a;
}
}
}
else
{
putpixel(-half_size_x + posx, y + posy, r, g, b, a);
putpixel(half_size_x - 1 + posx, y + posy, r, g, b, a);
}
}

assert(top == lua_gettop(L));
return 0;
}

static int draw_filled_rect(lua_State* L) {
int top = lua_gettop(L) + 4;

read_and_validate_buffer_info(L, 1);
uint32_t posx = luaL_checknumber(L, 2);
uint32_t posy = luaL_checknumber(L, 3);
uint32_t sizex = luaL_checknumber(L, 4);
uint32_t sizey = luaL_checknumber(L, 5);
uint32_t r = luaL_checknumber(L, 6);
uint32_t g = luaL_checknumber(L, 7);
uint32_t b = luaL_checknumber(L, 8);
uint32_t a = 0;
if (lua_isnumber(L, 9) == 1)
{
a = luaL_checknumber(L, 9);
}

int half_size_x = sizex/2;
int half_size_y = sizey/2;
int newposx = 0;
Expand All @@ -238,7 +282,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.chanels == 4) {
if (buffer_info.channels == 4) {
buffer_info.bytes[i + 3] = a;
}
}
Expand All @@ -254,6 +298,7 @@ static const luaL_reg Module_methods[] = {
{"filled_circle", draw_filled_circle},
{"fill", fill_texture},
{"rect", draw_rect},
{"filled_rect", draw_filled_rect},
{0, 0}
};

Expand Down
50 changes: 39 additions & 11 deletions example/canvas.script
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
function init(self)
msg.post(".", "acquire_input_focus")

msg.post("@render:", "clear_color", {color = vmath.vector4(1, 1, 1, 1)})
-- size of texture when scaled to nearest power of two
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 = chanels}})
local channels = 4

-- we have to create table with next fields: buffer, width, height, channels
self.buffer_info = {
buffer = our_buffer,
buffer = buffer.create(width * height, {{name = hash("rgba"), type = buffer.VALUE_TYPE_UINT8, count = channels}}),
width = width,
height = height,
chanels = chanels
channels = channels -- 3 for rgb, 4 for rgba
}

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

self.current_color = vmath.vector4(0, 0, 0, 1)
self.current_tool = "pencil"
-- drawing params
self.prev_pos = vmath.vector3()
self.resource_path = go.get("#sprite", "texture0")
Expand All @@ -38,6 +36,24 @@ function update(self, dt)
end
end

function on_message(self, message_id, message, sender)
if message_id == hash("change_color") then
self.current_color = message.color
if self.current_tool == "eraser" then
self.current_tool = "pencil"
end
elseif message_id == hash("change_tool") then
self.current_tool = message.tool
elseif message_id == hash("clear") then
drawpixels.fill(self.buffer_info, 0, 0, 0, 0)
self.dirty = true
end
end

local function color_vector_to_bytes(color)
return color.x * 255, color.y * 255, color.z * 255, color.w * 255
end

function on_input(self, action_id, action)
if action_id == hash("touch") then
local pos = vmath.vector3(action.x, action.y, 0)
Expand All @@ -64,8 +80,20 @@ function on_input(self, action_id, action)
-- 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.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)
local r, g, b, a = color_vector_to_bytes(self.current_color)
if self.current_tool == "pencil" then
drawpixels.filled_rect(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 4, 4, r, g, b, a)
elseif self.current_tool == "circle" then
drawpixels.circle(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 40, r, g, b, a)
elseif self.current_tool == "filled_circle" then
drawpixels.filled_circle(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 40, r, g, b, a)
elseif self.current_tool == "rect" then
drawpixels.rect(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 40, 60, r, g, b, a)
elseif self.current_tool == "filled_rect" then
drawpixels.filled_rect(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 60, 40, r, g, b, a)
elseif self.current_tool == "eraser" then
drawpixels.rect(self.buffer_info, self.touch_pos.x, self.touch_pos.y, 40, 40, 0, 0, 0, 0)
end
self.dirty = true
self.touch_pos = self.touch_pos - dir
length = length - 1
Expand Down
30 changes: 30 additions & 0 deletions example/draw_pixels.atlas
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
images {
image: "/example/images/gimp-tool-eraser_bw.png"
}
images {
image: "/example/images/gimp-tool-pencil_bw.png"
}
images {
image: "/example/images/logo.png"
}
images {
image: "/example/images/metalPanel_green.png"
}
images {
image: "/example/images/transparent1px.png"
}
images {
image: "/example/images/circle.png"
}
images {
image: "/example/images/filled_circle.png"
}
images {
image: "/example/images/rect.png"
}
images {
image: "/example/images/filled_rect.png"
}
margin: 0
extrude_borders: 2
inner_padding: 0
55 changes: 55 additions & 0 deletions example/example.collection
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@ embedded_instances {
" w: 1.0\n"
" }\n"
"}\n"
"embedded_components {\n"
" id: \"sprite1\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/example/draw_pixels.atlas\\\"\\n"
"default_animation: \\\"logo\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"\"\n"
" position {\n"
" x: -256.0\n"
" y: -512.0\n"
" z: -0.1\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 512.0
Expand All @@ -55,3 +75,38 @@ embedded_instances {
z: 1.0
}
}
embedded_instances {
id: "gui"
data: "components {\n"
" id: \"toolbox\"\n"
" component: \"/example/toolbox.gui\"\n"
" position {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" }\n"
" rotation {\n"
" x: 0.0\n"
" y: 0.0\n"
" z: 0.0\n"
" w: 1.0\n"
" }\n"
"}\n"
""
position {
x: 0.0
y: 0.0
z: 0.0
}
rotation {
x: 0.0
y: 0.0
z: 0.0
w: 1.0
}
scale3 {
x: 1.0
y: 1.0
z: 1.0
}
}
Binary file added example/images/circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/images/filled_circle.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/images/filled_rect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/images/gimp-tool-eraser_bw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/images/gimp-tool-pencil_bw.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/images/metalPanel_green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/images/rect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added example/images/transparent1px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8f88a1d

Please sign in to comment.