From c8e9e0554e88a608f31e21e70061bd1292bf3575 Mon Sep 17 00:00:00 2001 From: Sergey Date: Wed, 22 Apr 2020 21:41:51 +0300 Subject: [PATCH] Added new examples --- README.md | 16 ++++++ drawpixels/src/drawpixels.cpp | 4 -- example/images/fullscreen512.png | Bin 0 -> 1832 bytes examples/arcs/arcs.collection | 57 +++++++++++++++++++++ examples/arcs/arcs.script | 30 ++++++++++++ examples/fills/fills.collection | 57 +++++++++++++++++++++ examples/fills/fills.script | 32 ++++++++++++ examples/full.atlas | 7 +++ examples/lines_aa/lines.script | 26 ++++++++++ examples/lines_aa/lines_aa.collection | 57 +++++++++++++++++++++ examples/sprite.material | 32 ++++++++++++ examples/togui/togui.collection | 37 ++++++++++++++ examples/togui/togui.gui | 68 ++++++++++++++++++++++++++ examples/togui/togui.gui_script | 36 ++++++++++++++ game.project | 2 +- 15 files changed, 456 insertions(+), 5 deletions(-) create mode 100644 example/images/fullscreen512.png create mode 100644 examples/arcs/arcs.collection create mode 100644 examples/arcs/arcs.script create mode 100644 examples/fills/fills.collection create mode 100644 examples/fills/fills.script create mode 100644 examples/full.atlas create mode 100644 examples/lines_aa/lines.script create mode 100644 examples/lines_aa/lines_aa.collection create mode 100644 examples/sprite.material create mode 100644 examples/togui/togui.collection create mode 100644 examples/togui/togui.gui create mode 100644 examples/togui/togui.gui_script diff --git a/README.md b/README.md index c2c10ad..4df581e 100644 --- a/README.md +++ b/README.md @@ -238,6 +238,22 @@ Fills an area at specified boundaries. Only for 4 channels: `blue` - blue channel of the color 0..255
`alpha` - alpha channel 0..255
+In order to draw this into a sprite, you need a separate atlas with power of two texture. Then we can use `resource.set_texture`: + +```lua +resource.set_texture(go.get("#sprite", "texture0"), self.header, self.buffer_info.buffer) +``` + +In order to render this in gui, we just need to create a box and create a new texture. +We can use `gui.new_texture`. Then you need to set this texture to the box using `gui.set_texture`: + +```lua +local data = buffer.get_bytes(self.buffer_info.buffer, hash("rgba")) +gui.new_texture("name", width, height, image.TYPE_RGBA, data) +gui.set_texture(gui.get_node("box"), "name") +gui.set_size(gui.get_node("box"), vmath.vector3(width, height, 0)) +``` + -------- If you have any questions or suggestions contact me: me@agulev.com diff --git a/drawpixels/src/drawpixels.cpp b/drawpixels/src/drawpixels.cpp index b226a16..0d7c2df 100755 --- a/drawpixels/src/drawpixels.cpp +++ b/drawpixels/src/drawpixels.cpp @@ -881,8 +881,6 @@ static void draw_gradient_arc_lines(int _x, int _y, int radius, float from, floa float ty = radius * sin(to + M_PI / 2); draw_gradient_line_vu(_x, _y, fx + _x, fy + _y, c1, c2, a, 1); draw_gradient_line_vu(_x, _y, tx + _x, ty + _y, c1, c2, a, 1); - // mixpixel(fx + _x, fy + _y, c1.r, c1.g, c1.b, a); - // mixpixel(tx + _x, ty + _y, c2.r, c2.g, c2.b, a); } static void draw_arc_lines(int _x, int _y, int radius, float from, float to, int r, int g, int b, int a) @@ -899,8 +897,6 @@ static void draw_arc_lines(int _x, int _y, int radius, float from, float to, int float ty = radius * sin(to + M_PI / 2); draw_line_vu(_x, _y, fx + _x, fy + _y, r, g, b, a, 1); draw_line_vu(_x, _y, tx + _x, ty + _y, r, g, b, a, 1); - // mixpixel(fx + _x, fy + _y, r, g, b, a); - // mixpixel(tx + _x, ty + _y, r, g, b, a); } static void draw_arc_vu(int _x, int _y, int radius, float from, float to, int r, int g, int b, int a) diff --git a/example/images/fullscreen512.png b/example/images/fullscreen512.png new file mode 100644 index 0000000000000000000000000000000000000000..3e0c232689ebe017edae921a2fd07821c1e179b2 GIT binary patch literal 1832 zcmeAS@N?(olHy`uVBq!ia0y~yU;;9k7&zE~)R&4Yzkn2Hfk$L90|U1Z2s2)~Tla^7 zf$f&3i(^Q|t+&?<85tNj4jUZU|2(m&-^FQt*(4^0I!R8313;ZXLkt)g8h|Qz7#I?O z0?Z5y5)1^SNK}qw2{EQ4DMz@8EO&xTLKs2NTc{RGFpP!}{=gdzAwtP(G=xS&2wKYG m&q{bo-qAFKmWCdjV_Ncq?XS=4Uyi`~iow&>&t;ucLK6VD1fX{S literal 0 HcmV?d00001 diff --git a/examples/arcs/arcs.collection b/examples/arcs/arcs.collection new file mode 100644 index 0000000..c69073f --- /dev/null +++ b/examples/arcs/arcs.collection @@ -0,0 +1,57 @@ +name: "default" +scale_along_z: 0 +embedded_instances { + id: "canvas" + data: "components {\n" + " id: \"arcs\"\n" + " component: \"/examples/arcs/arcs.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: \\\"/examples/sprite.material\\\"\\n" + "blend_mode: BLEND_MODE_ALPHA\\n" + "\"\n" + " position {\n" + " x: 8.572\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: 248.0 + y: 500.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 + } +} diff --git a/examples/arcs/arcs.script b/examples/arcs/arcs.script new file mode 100644 index 0000000..2031e28 --- /dev/null +++ b/examples/arcs/arcs.script @@ -0,0 +1,30 @@ +function init(self) + msg.post("@render:", "clear_color", {color = vmath.vector4(1, 1, 1, 1)}) + -- size of texture when scaled to nearest power of two + local width = 512 + local height = 512 + local channels = 4 + + self.resource_path = go.get("#sprite", "texture0") + + self.buffer_info = { + buffer = buffer.create(width * height, {{name = hash("rgba"), type = buffer.VALUE_TYPE_UINT8, count = channels}}), + width = width, + height = height, + channels = channels + } + + self.header = {width = width, height = height, type = resource.TEXTURE_TYPE_2D, format = resource.TEXTURE_FORMAT_RGBA, num_mip_maps = 1} + drawpixels.fill(self.buffer_info, 255, 255, 0, 255) + + local step = 30; + for angle = 0, 330 - step, step do + -- drawpixels.gradient_arc(self.buffer_info, width/2, width/2, width * 0.4, (angle) * math.pi / 180, (angle + step) * math.pi / 180, 120, 150, 48, 50, 180, 200, 255) + drawpixels.filled_arc(self.buffer_info, width / 2, width / 2, width * 0.4, (angle) * math.pi / 180, (angle + step) * math.pi / 180, 255, 0, 0, + 255) + drawpixels.arc(self.buffer_info, width / 2, width / 2, width * 0.4, (angle) * math.pi / 180, (angle + step) * math.pi / 180, 0, 0, 255, 255) + end + + drawpixels.arc(self.buffer_info, width / 2, width / 2, width * 0.4, -20 * math.pi / 180, -50 * math.pi / 180, 0, 255, 0, 255) + resource.set_texture(self.resource_path, self.header, self.buffer_info.buffer) +end diff --git a/examples/fills/fills.collection b/examples/fills/fills.collection new file mode 100644 index 0000000..f92763f --- /dev/null +++ b/examples/fills/fills.collection @@ -0,0 +1,57 @@ +name: "default" +scale_along_z: 0 +embedded_instances { + id: "canvas" + data: "components {\n" + " id: \"fills\"\n" + " component: \"/examples/fills/fills.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: \\\"/examples/sprite.material\\\"\\n" + "blend_mode: BLEND_MODE_ALPHA\\n" + "\"\n" + " position {\n" + " x: 8.572\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: 248.0 + y: 500.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 + } +} diff --git a/examples/fills/fills.script b/examples/fills/fills.script new file mode 100644 index 0000000..c3438d0 --- /dev/null +++ b/examples/fills/fills.script @@ -0,0 +1,32 @@ +function init(self) + msg.post("@render:", "clear_color", {color = vmath.vector4(1, 1, 1, 1)}) + -- size of texture when scaled to nearest power of two + local width = 512 + local height = 512 + local channels = 4 + + self.resource_path = go.get("#sprite", "texture0") + + self.buffer_info = { + buffer = buffer.create(width * height, {{name = hash("rgba"), type = buffer.VALUE_TYPE_UINT8, count = channels}}), + width = width, + height = height, + channels = channels + } + + self.header = {width = width, height = height, type = resource.TEXTURE_TYPE_2D, format = resource.TEXTURE_FORMAT_RGBA, num_mip_maps = 1} + drawpixels.fill(self.buffer_info, 255, 255, 0, 255) + + drawpixels.start_fill() + drawpixels.line(self.buffer_info, 20, 20, 500, 20, 255, 0, 0, 255, true) + drawpixels.line(self.buffer_info, 500, 20, 400, 358, 255, 0, 0, 255, true) + drawpixels.line(self.buffer_info, 400, 358, 500, 500, 255, 0, 0, 255, true) + drawpixels.line(self.buffer_info, 500, 500, 258, 453, 255, 0, 0, 255, true) + drawpixels.line(self.buffer_info, 258, 453, 20, 20, 255, 0, 0, 255, true) + -- drawpixels.line(self.buffer_info,10, 250, 510, 250, 255, 0, 0, 255, true) + drawpixels.circle(self.buffer_info, 200, 200, 100, 0, 255, 255, 255, true) + drawpixels.arc(self.buffer_info, width / 2, width / 2, width * 0.4, -20 * math.pi / 180, -50 * math.pi / 180, 0, 255, 0, 255) + drawpixels.fill_area(self.buffer_info, 300, 300, 255, 0, 0, 255) + drawpixels.end_fill() + resource.set_texture(self.resource_path, self.header, self.buffer_info.buffer) +end diff --git a/examples/full.atlas b/examples/full.atlas new file mode 100644 index 0000000..467aeed --- /dev/null +++ b/examples/full.atlas @@ -0,0 +1,7 @@ +images { + image: "/example/images/fullscreen512.png" + sprite_trim_mode: SPRITE_TRIM_MODE_OFF +} +margin: 0 +extrude_borders: 0 +inner_padding: 0 diff --git a/examples/lines_aa/lines.script b/examples/lines_aa/lines.script new file mode 100644 index 0000000..338845d --- /dev/null +++ b/examples/lines_aa/lines.script @@ -0,0 +1,26 @@ +function init(self) + msg.post("@render:", "clear_color", {color = vmath.vector4(1, 1, 1, 1)}) + -- size of texture when scaled to nearest power of two + local width = 512 + local height = 512 + local channels = 4 + + self.resource_path = go.get("#sprite", "texture0") + + self.buffer_info = { + buffer = buffer.create(width * height, {{name = hash("rgba"), type = buffer.VALUE_TYPE_UINT8, count = channels}}), + width = width, + height = height, + channels = channels + } + + self.header = {width = width, height = height, type = resource.TEXTURE_TYPE_2D, format = resource.TEXTURE_FORMAT_RGBA, num_mip_maps = 1} + drawpixels.fill(self.buffer_info, 255, 255, 0, 255) + + drawpixels.gradient_line(self.buffer_info, width * 0.2, width * 0.1, width * 0.9, width * 0.9, 255, 0, 0, 0, 0, 255, 255, 12) + drawpixels.line(self.buffer_info, width * 0.2, width * 0.5, width * 0.9, width * 0.9, 0, 0, 255, 255, true, 5) + drawpixels.line(self.buffer_info, width * 0.1, width * 0.5, width * 0.9, width * 0.5, 255, 0, 0, 255, true, 10) + drawpixels.line(self.buffer_info, width * 0.5, width * 0.1, width * 0.5, width * 0.9, 0, 0, 255, 255, true, 10) + + resource.set_texture(self.resource_path, self.header, self.buffer_info.buffer) +end diff --git a/examples/lines_aa/lines_aa.collection b/examples/lines_aa/lines_aa.collection new file mode 100644 index 0000000..6cb01b8 --- /dev/null +++ b/examples/lines_aa/lines_aa.collection @@ -0,0 +1,57 @@ +name: "default" +scale_along_z: 0 +embedded_instances { + id: "canvas" + data: "components {\n" + " id: \"lines\"\n" + " component: \"/examples/lines_aa/lines.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: \\\"/examples/sprite.material\\\"\\n" + "blend_mode: BLEND_MODE_ALPHA\\n" + "\"\n" + " position {\n" + " x: 8.572\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: 248.0 + y: 500.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 + } +} diff --git a/examples/sprite.material b/examples/sprite.material new file mode 100644 index 0000000..4136d29 --- /dev/null +++ b/examples/sprite.material @@ -0,0 +1,32 @@ +name: "sprite" +tags: "tile" +vertex_program: "/builtins/materials/sprite.vp" +fragment_program: "/builtins/materials/sprite.fp" +vertex_space: VERTEX_SPACE_WORLD +vertex_constants { + name: "view_proj" + type: CONSTANT_TYPE_VIEWPROJ + value { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } +} +fragment_constants { + name: "tint" + type: CONSTANT_TYPE_USER + value { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } +} +samplers { + name: "texture_sampler" + wrap_u: WRAP_MODE_CLAMP_TO_EDGE + wrap_v: WRAP_MODE_CLAMP_TO_EDGE + filter_min: FILTER_MODE_MIN_LINEAR + filter_mag: FILTER_MODE_MAG_LINEAR +} diff --git a/examples/togui/togui.collection b/examples/togui/togui.collection new file mode 100644 index 0000000..8963a83 --- /dev/null +++ b/examples/togui/togui.collection @@ -0,0 +1,37 @@ +name: "default" +scale_along_z: 0 +embedded_instances { + id: "go" + data: "components {\n" + " id: \"togui\"\n" + " component: \"/examples/togui/togui.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 + } +} diff --git a/examples/togui/togui.gui b/examples/togui/togui.gui new file mode 100644 index 0000000..f6f618c --- /dev/null +++ b/examples/togui/togui.gui @@ -0,0 +1,68 @@ +script: "/examples/togui/togui.gui_script" +textures { + name: "full" + texture: "/example/draw_pixels.atlas" +} +background_color { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 +} +nodes { + position { + x: 256.0 + y: 459.0 + z: 0.0 + w: 1.0 + } + rotation { + x: 0.0 + y: 0.0 + z: 0.0 + w: 1.0 + } + scale { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + size { + x: 200.0 + y: 100.0 + z: 0.0 + w: 1.0 + } + color { + x: 1.0 + y: 1.0 + z: 1.0 + w: 1.0 + } + type: TYPE_BOX + blend_mode: BLEND_MODE_ALPHA + texture: "full/metalPanel_green" + id: "box" + xanchor: XANCHOR_NONE + yanchor: YANCHOR_NONE + pivot: PIVOT_CENTER + adjust_mode: ADJUST_MODE_FIT + layer: "" + inherit_alpha: true + slice9 { + x: 0.0 + y: 0.0 + z: 0.0 + w: 0.0 + } + clipping_mode: CLIPPING_MODE_NONE + clipping_visible: true + clipping_inverted: false + alpha: 1.0 + template_node_child: false + size_mode: SIZE_MODE_MANUAL +} +material: "/builtins/materials/gui.material" +adjust_reference: ADJUST_REFERENCE_PARENT +max_nodes: 512 diff --git a/examples/togui/togui.gui_script b/examples/togui/togui.gui_script new file mode 100644 index 0000000..01f1744 --- /dev/null +++ b/examples/togui/togui.gui_script @@ -0,0 +1,36 @@ +function init(self) + msg.post("@render:", "clear_color", {color = vmath.vector4(1, 1, 1, 1)}) + -- size of texture when scaled to nearest power of two + local width = 512 + local height = 512 + local channels = 4 + + self.resource_path = gui.get_node("box") + + self.buffer_info = { + buffer = buffer.create(width * height, {{name = hash("rgba"), type = buffer.VALUE_TYPE_UINT8, count = channels}}), + width = width, + height = height, + channels = channels + } + + drawpixels.fill(self.buffer_info, 255, 255, 0, 255) + + drawpixels.start_fill() + drawpixels.line(self.buffer_info, width * 0.2, width * 0.1, width * 0.9, width * 0.9, 255, 0, 0, 255, true) + drawpixels.line(self.buffer_info, width * 0.3, width * 0.8, width * 0.9, width * 0.9, 255, 0, 0, 255, true) + drawpixels.line(self.buffer_info, width * 0.3, width * 0.8, width * 0.2, width * 0.1, 255, 0, 0, 255, true) + drawpixels.fill_area(self.buffer_info, width * 0.1, width * 0.5, 255, 0, 0, 255) + drawpixels.end_fill() + -- Fills everything around because point is not in a triangle + + local data = buffer.get_bytes(self.buffer_info.buffer, hash("rgba")) + + if gui.new_texture("oh_hi_mark", width, height, image.TYPE_RGBA, data) then + gui.set_texture(self.resource_path, "oh_hi_mark") + gui.set_size(self.resource_path, vmath.vector3(width, height, 0)) + else + error("Unable to create texture") + end + +end diff --git a/game.project b/game.project index 863784b..52b813f 100644 --- a/game.project +++ b/game.project @@ -3,7 +3,7 @@ title = Draw Pixels version = 0.5 [bootstrap] -main_collection = /example/example.collectionc +main_collection = /examples/togui/togui.collectionc [input] game_binding = /input/game.input_bindingc