stb_image Wrapper for Nim

Have you ever used any of Sean Barret's (a.k.a "nothings") stb libraries at all in a C/C++ program before?  They are all really lovely.  My favorite part of about them is that they all consist of a single header file.  No messy linking commands at compile time, just #include them wherever you want.  Nothing could be simpler than that.  I was a little surprised that no one had made a Nim friendly wrapper for stb_image yet.  So I made one myself.  It can be found here, or on nimble as the stb_image package.

If you're not familiar with stb_image, it's a nice little bitmap loading utility.  It supports many file formats (or at least the ones people care about,) such as PNG, JPEG, PSD, etc.  The API is pretty clean.  e.g.:

 

 

I designed the wrapper to be easy to use and make it seamless for translating between C and Nim.  It uses Nim types (handling all those C conversions for you) and returns data as sequences.  This is how that snippet above would look in Nim:

 

 

I also spent some time binding the stb_image_write library too:

 

 

I also had some plans to bind stb_image_resize but it looked like a lot of extra work and I thought it would be better to keep this wrapper right now to only image IO.  Though if there is some demand for that to be bound I can look into it. Though I'd appreciate some help with it.  The only things that haven't been wrapped are the callback functions and the ZLIB client.  I have no intention of adding those things in myself, but I'm always open to pull requests.  These are the limitations of the wrapper yet it should cover most of your use cases.  Before using it I'd also recommend reading through the documentation at the top of stb_image.h and stb_image_write.h too, so you know what it can and cannot do.

As of right now the wrapper uses these versions from stb:

  • stb_image: v2.13
  • stb_image_write: v1.02

They're hard coded into the *_header.nim files (to make it easier on the user).  If there is a version change it should be easy to swap out for yourself, but dropping me a line on the issue tracker so I can update the package would be cool.

Once again, you can find the repo here on GitLab, but I've got a mirror on GitHub too.  Enjoy.

Tags: Nim
A Stopwatch for Nim

As I mentioned in the previous post, I've been playing around with the Nim language for a little less than a month now.  So far I really like it and have used it for messing with computer graphics.  I like to record how long things take to compute so I set out to look for a Timer of sorts.  I was a little surprised to find out that it didn't have a built-in timing/benchmarking mechanism (like C# has with the Stopwatch class).  There was a package made available by rbmz under the name of stopwatch that did some very basic stuff for me, but I was still unsatisfied.  So I decided to do a fork of it and made all of the changes that I wanted.

There were two main things that I changed.  the clock object was renamed to Stopwatch, and the Stopwatch can now record multiple laps.  What I mean by "laps," is that you can now start and stop the Stopwatch multiple times and it will remember those timings for you.  Some other minor additions include methods to convert nanoseconds over to milli- and microseconds.  To see everything that was added check the source code here, everything should be documented and easy to follow (there isn't much).

If you want to grab the package, it's up on nimble as stopwatch.  Yes, one of the maintainers was nice enough to let me hijack the original package name.  Here are some code samples (ripped from the README) that show you how to use this package:

 

 

You can find the repo here.  If you have any requests or find any bugs please tell me.

Tags: Nim
An OpenGL Shader Example in Nim

It's been a while since I've posted something.  It's mainly because I've been busy for the past couple of weeks.  I've had to put my C# Networking Tutorials series on hiatus for the time being.  But I do have plans to finish it with about three more articles (they'll cover things like async).

In the meantime, I've been playing around with this language called "Nim."  I first heard about it from /r/programming.  It was popping up every so often so I decided to finally check it out about two or three weeks ago.  So far, I've found it very pleasant to work in despite being fairly young.  I've been reading through Peter Shirley's Raytracing Minibooks but writing the code in Nim instead of C++; it's been pretty good so far (I'll post about that on a later date).

You probably can guess that I really like graphics.  So one of the first things I looked for were OpenGL examples.  I had a lot of trouble trying to find a solid sample that showed you how to use shaders, so I made one myself.  You can find a link to it here, but the source for it is also included in this post.  I've tried to keep things as simple as possible, so there is duplicate code (e.g. the shader construction code), but it makes it easier to follow.  You can compile the program with nim c nim_opengl_shader_example.nim.  I built this on Linux so I also had to add in a {.passL: "-lGL".} call at the beginning.  Change it for the platform of your choice.   It also uses this GLFW binding, though it should be simple to change it out for SDL2.  It should show a colourful rotating hexagon.  You can change it's speed by passing in a non-negative float at the command line, e.g. "0.5," or "10".  Pass in "0" to make it not rotate.  If you want anti-aliasing, add "aa" to the command line arguments.  Here is what it looks like:

Edit: The official opengl package handles the linking for you.  If you're using a different OpenGL binding (like those from nimios,) you'll need to add the passL in manually for your OS.

 

OpenGL Shader Example in Nim

 

And the source:

 

© 16BPP.net – Made using & love.
Back to Top of Page
This site uses cookies.