Skip to content

Commit

Permalink
Merge pull request #2 from d954mas/master
Browse files Browse the repository at this point in the history
Performance and IDEA autocomplete
  • Loading branch information
AGulev authored Jul 18, 2018
2 parents b438465 + db9e239 commit 9568749
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 18 deletions.
22 changes: 22 additions & 0 deletions drawpixels/idea_autocomplite.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--AUTOCOMPLETE FOR IDEA DO NOT REMOVE OR REQUIRE
---@class DRAW_PIXELS
drawpixels = {}

---Method for drawing circle:
function drawpixels.circle(buffer_info, pox_x, pox_y, diameter, red, green, blue, alpha)end
---Method for drawing filled circle:
function drawpixels.filled_circle(buffer_info, pos_x, pos_y, diameter, red, green, blue, alpha) end
---Method for drawing rectangle:
function drawpixels.rect(buffer_info, pos_x, pos_y, rect_width, rect_height, red, green, blue, alpha) end
---Method for drawing filled rectangle:
function drawpixels.filled_rect(buffer_info, pos_x, pos_y, rect_width, rect_height, red, green, blue, alpha, angle) end
---Fill buffer with the color:
function drawpixels.fill(buffer_info, red, green, blue, alpha) end
---Draw a line between two points:
function drawpixels.line(buffer_info, x0, y0, x1, y1, red, green, blue, alpha) end
---Draw a pixel:
function drawpixels.pixel(buffer_info, x, y, red, green, blue, alpha) end
---Read color from a position in the buffer:
function drawpixels.color(buffer_info, x, y) end

return
63 changes: 45 additions & 18 deletions drawpixels/src/drawpixels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,33 @@ static void fill_line(int fromx, int tox, int y, int r, int g, int b, int a){
fromx = tox;
tox = temp;
}
for (int x = fromx; x <= tox; x++) {
putpixel(x, y, r, g, b, a);
fromx = fmax(0, fromx);
tox = fmin(buffer_info.width-1, tox);
int size = 10;
//prepare line for memcpy
int line_size = 10*buffer_info.channels;
uint8_t* line = new uint8_t[line_size];
for (int i=0; i<line_size; i +=buffer_info.channels){
line[i] = r;
line[i + 1] = g;
line[i + 2] = b;
if (buffer_info.channels == 4) {
line[i + 3] = a;
}
}
int start = xytoi(fromx, y);
int end = xytoi(tox, y);
int width = (end - start+buffer_info.channels);
for(int i = start; i < end; i +=line_size) {
if(width >= line_size){
memcpy(&buffer_info.bytes[i], line, line_size);
}else{
memcpy(&buffer_info.bytes[i], line, width);
}
width = width - line_size;
}
delete[] line;

}

//http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html
Expand Down Expand Up @@ -336,23 +360,28 @@ static int fill_texture(lua_State* L) {
int top = lua_gettop(L) + 4;

read_and_validate_buffer_info(L, 1);
uint32_t r = luaL_checknumber(L, 2);
uint32_t g = luaL_checknumber(L, 3);
uint32_t b = luaL_checknumber(L, 4);
uint32_t a = 0;
uint8_t r = luaL_checknumber(L, 2);
uint8_t g = luaL_checknumber(L, 3);
uint8_t b = luaL_checknumber(L, 4);
uint8_t a = 0;
if (lua_isnumber(L, 5) == 1)
{
a = luaL_checknumber(L, 5);
}
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;
int line_size = buffer_info.width*buffer_info.channels;
uint8_t* line = new uint8_t[line_size];
for (int i=0; i<line_size; i +=buffer_info.channels){
line[i] = r;
line[i + 1] = g;
line[i + 2] = b;
if (buffer_info.channels == 4) {
buffer_info.bytes[i + 3] = a;
line[i + 3] = a;
}
}

for(int i = 0; i < buffer_info.src_size; i +=line_size) {
memcpy(&buffer_info.bytes[i], line, line_size);
}
delete[] line;
assert(top == lua_gettop(L));
return 0;
}
Expand Down Expand Up @@ -428,12 +457,10 @@ static int draw_filled_rect(lua_State* L) {
if (angle == 0) {
int newposx = 0;
int newposy = 0;
for(int x = -half_size_x; x < half_size_x; x++) {
for(int y = -half_size_y; y < half_size_y; y++) {
newposx = x + posx;
newposy = y + posy;
putpixel(newposx, newposy, r, g, b, a);
}
for(int y = -half_size_y; y < half_size_y; y++) {
newposy = y + posy;
fill_line(posx-half_size_x,posx+half_size_x,newposy,r,g,b,a);

}
}
else{
Expand Down
77 changes: 77 additions & 0 deletions tests/performance/performance.collection
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: "main"
scale_along_z: 0
embedded_instances {
id: "canvas"
data: "components {\n"
" id: \"script\"\n"
" component: \"/tests/performance/performance.script\"\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"
"embedded_components {\n"
" id: \"sprite\"\n"
" type: \"sprite\"\n"
" data: \"tile_set: \\\"/example/img.atlas\\\"\\n"
"default_animation: \\\"fullscreen\\\"\\n"
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
"blend_mode: BLEND_MODE_ALPHA\\n"
"\"\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"
"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
y: 1024.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
}
}
64 changes: 64 additions & 0 deletions tests/performance/performance.script
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
function init(self)
msg.post(".", "acquire_input_focus")
msg.post("@system:", "toggle_profile")
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 channels = 4
-- we have to create table with next fields: buffer, width, height, channels
self.buffer_info = {
buffer = buffer.create(width * height, {{name = hash("rgba"), type = buffer.VALUE_TYPE_UINT8, count = channels}}),
width = width,
height = height,
channels = channels -- 3 for rgb, 4 for rgba
}
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")
self.header = {
width = width,
height = height,
type = resource.TEXTURE_TYPE_2D,
format = resource.TEXTURE_FORMAT_RGBA,
num_mip_maps = 1
}
end

local function mesure_time(self, times, fun, ...)
local time = os.clock()
for i=1, times do
fun(self, ...)
end
print("time:" .. os.clock() - time)
end

local function fill(self)
drawpixels.fill(self.buffer_info, 255,0,0,0)
end
local function circle(self)
drawpixels.filled_circle(self.buffer_info, 222,222,400,255,0,0,255)
end
local function rect(self)
drawpixels.filled_rect(self.buffer_info, 222,222,400,255,255,0,0,255)
end

function update(self, dt)
mesure_time(self, 1000, rect)
-- mesure_time(self, 100, fill)
resource.set_texture(self.resource_path, self.header, self.buffer_info.buffer)
end

function on_message(self, message_id, message, sender)
end

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

local function bytes_to_color_vector(r, g, b, a)
return vmath.vector4(r / 255, g / 255, b / 255, a / 255)
end

0 comments on commit 9568749

Please sign in to comment.