Hi everyone, I wanted to share another Nim repo from @treeform and I. Boxy is for doing GPU accelerated 2D graphics and should work well for games or UI uses. It is built to complement Pixie.
The model for using Boxy goes like this:
Note that Boxy is agnostic to how you get a window or OpenGL context. You can use it with GLFW or SDL2 or whatever. We just happen to use GLFW in our examples so far.
Here is what the atlas texture looks like for this example.
Here is the important code for this example:
let bxy = newBoxy()
# Load the images into Boxy.
bxy.addImage("bg", readImage("examples/data/bg.png"))
bxy.addImage("ring1", readImage("examples/data/ring1.png"))
bxy.addImage("ring2", readImage("examples/data/ring2.png"))
bxy.addImage("ring3", readImage("examples/data/ring3.png"))
var frame: int
# Called when it is time to draw a new frame.
proc display() =
# Clear the screen and begin a new frame.
bxy.beginFrame(windowSize)
# Draw the bg.
bxy.drawImage("bg", rect = rect(vec2(0, 0), windowSize))
# Draw the rings.
let center = windowSize.vec2 / 2
bxy.drawImage("ring1", center, angle = frame.float / 100)
bxy.drawImage("ring2", center, angle = -frame.float / 190)
bxy.drawImage("ring3", center, angle = frame.float / 170)
# End this frame, flushing the draw commands.
bxy.endFrame()
# Swap buffers displaying the new Boxy frame.
window.swapBuffers()
inc frame
while windowShouldClose(window) != 1:
pollEvents()
display()
See the full example source here.
Boxy is one way of addressing that question.
Very often, you only need to load or render an image into a texture once and then use it for many frames. This is true for backgrounds, most text, UI elements, tons of things. Having done the initial render once on the CPU is almost never a problem for these things (though there are of course some exceptions).
What really matters is getting ALL these images rendered on screen over and over again very fast every frame. This is where Boxy comes in.
We do want Pixie to use GPU hardware if it is available, certainly. However, even with faster GPU rasterizing, it is still a good idea to bake rasterized stuff into textures instead of rasterizing them every frame if you don't need to. With that in mind, you can see how Boxy still makes sense, even if the initial rasterizing was done by the GPU instead of the CPU.
Thanks for reading and taking a look at Boxy!