Hi everyone, I wanted to share another Nim repo from @treeform and I. 
[Boxy](https://github.com/treeform/boxy) is for doing GPU accelerated 2D 
graphics and should work well for games or UI uses. It is built to complement 
[Pixie](https://github.com/treeform/pixie).

The model for using [Boxy](https://github.com/treeform/boxy) goes like this:

  * Open a window and prepare an OpenGL context.
  * Load image files like .png using Pixie.
  * Render any dynamic assets (such as text) into images once using Pixie.
  * Add these images to Boxy, where they are put into a tiling atlas texture.
  * Draw these images to screen each frame.



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.

### Example using Boxy and Pixie

[Here is what the atlas texture looks like for this 
example.](https://dl3.pushbulletusercontent.com/lZNaItwRn0D0ChVclPmdRXO8MFxS9Y4l/image.png)

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()
    
    
    Run

[See the full example source 
here.](https://github.com/treeform/boxy/blob/master/examples/spinner.nim)

### "When will Pixie do GPU rendering?"

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.

### Will Pixie never do GPU rendering itself then?

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! 

Reply via email to