22 September 2016
Introducing external bitmap textures
The Corona engineers have been doing some wonderful things lately, especially on the graphics engine side. One of these items is a feature which allows plugin creators and Corona Enterprise users to push textures directly into the Corona Graphics pipeline, effectively providing bitmap data. Resulting textures will behave similarly to those created by graphics.newTexture().
Below the surface
Normally with Corona display APIs, images are loaded and textures are created in memory, processed via the graphics processing unit (GPU). Once the image is in that realm, it becomes difficult to do pixel-level manipulation — setting and getting individual pixel information is a time-consuming effort, making it impractical for most operations.
Fortunately, the Corona Graphics API set provides capabilities to create textures with custom data from C-based plugins. The resulting objects inherit properties of TextureResourceExternal as well as TextureResource properties. Once created, these objects have filename
and baseDir
properties which can be used in any display API or for assigning fills.
Possibilities?
An example of a plugin which might utilize these capabilities is a plugin which loads SVG (Scalable Vector Graphics) files, rasterizes them on the native C side, and pushes the results to Corona to return a texture object. That texture object could then be used to create typical display objects.
Other examples include image manipulation plugins based on ImageMagick or another C library, custom text and font renderers, and importing images from unsupported formats — the possibilities are endless!
Take it for a test drive!
To demonstrate this technology, our engineers have created a very simple plugin called memoryBitmap. Available in the Corona Marketplace, this plugin has just a single method, memoryBitmap.newTexture(). This creates a blank texture of specified dimensions and the result is a new TextureResourceExternal object with two additional methods: :setPixel() and :getPixel().
Directly “out of the box,” this plugin isn’t particularly useful to Corona SDK developers, since it cannot access your existing images. However, its source code may be useful to Corona Enterprise plugin developers as a template for cross-platform Corona plugins supported on macOS, iOS, tvOS, Android, and Windows.
Example
We made a quick demo to create Spirograph™ style drawings using this new plugin’s :setPixel() method. To test it, you’ll need to activate the plugin in the Corona Marketplace and add the plugin to your build.settings
.
Below is the code for creating the texture and manipulating it by setting pixels:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
local memoryBitmap = require( "plugin.memoryBitmap" ) local tex = memoryBitmap.newTexture( { width = 1024, height = 1024, format = "rgba" }) local function makeSpirals( texture, xOffset, yOffset, innerRadius, outerRadius, penOffset, color ) -- Based on: http://introcs.cs.princeton.edu/java/15inout/Spirograph.java.html local R = outerRadius local r = innerRadius local a = penOffset for t = 0.0, 100, 0.01 do local x = (R+r) * math.cos(t) - (r+a) * math.cos(((R+r)/r)*t) local y = (R+r) * math.sin(t) - (r+a) * math.sin(((R+r)/r)*t) local degrees = -1 * math.deg((R+r)/r)*t texture:setPixel( x + xOffset, y + yOffset, color ) end texture:invalidate() end makeSpirals( tex, 512, 512, 100, 55, 20, { 1, 0, 0, 1 } ) |
In this example, the memoryBitmap.newTexture() function is returning a blank slate to work with. Essentially, you tell it the size of the object and the color depth rules and then you can start manipulating pixels as much as you like.
Conclusion
We are excited to see what enterprising Corona developers will create with TextureResourceExternal and the memoryBitmap plugin as a basis for amazing features. Got a great idea? Please join us in the Corona forums!
Sorry, the comment form is closed at this time.