Part 1 introduced the basics of the plugin system. Part 2 created a class of shared libraries, then showed a sample program to load the plugins and execute them. Finally, here in part 3, we'll make a custom plugin and run it through our program from the last example. This final set of code is the simplest, check it out.
Sunday, November 2, 2014
Creating Basic C++ Plugins (Part 2 of 3)
In the first part of this tutorial, we covered the basic setup of what it takes to start on a plugin system in C++. This meant getting used to function calling conventions, why they matter, and how they apply to executables vs libraries. Part 1 then went on to create the basic API that will be used throughout the remainder of the tutorial. Part 2 will now go over some skeleton code to contain a handle to a shared library (using RAII), then walk through a method to load and verify a library at runtime.
*Make sure you have SDL2 installed before continuing.
Creating Basic C++ Plugins (Part 1 of 3)
One of the things that I plan on introducing into LightSky is a basic plugin system. If you want to insert a custom renderer, audio library, file loader, or more into the game engine, chances are that you would rather not recompile the entire library in order to allow it. In this basic tutorial, I'll cover the basics of making a shared library loader that uses RAII in order to close resources automatically. This tutorial will also use G++ (through MinGW) and SDL in order to load/unload shared libraries on different platforms such as Windows, Linux, and OSX. Now to get started, I'll assume an advanced level of C++ knowledge. You should be familiar with the difference between a static and shared library, pointer arithmetic, and function calling conventions.
The program in this tutorial is split into 3 parts; the first part is as follows:
Part 2 dives into the runtime environment:
Simple, right? Let's get started.
The program in this tutorial is split into 3 parts; the first part is as follows:
- Design an abstract base class which will be used to create client plugins.
- Add OS-specific bindings to the API in order to improve cross-platform functionality.
- Create a private library-loader class which will handle all resource loading/unloading using RAII.
Part 2 dives into the runtime environment:
- Start
main()with the input parametersargcandargv. - For each input string in
argc: - Verify the parameter is a valid path to a shared libary,
- Attempt to load a shared library using the input path name.
- Run the plugin.
For part 3:
- Create a plugin class which inherits from a pre-defined API base type.
- Create a factory method to instantiate the derived class from within the shared library.
- Create a deletion method which will destroy any instantiated class methods using the shared library.
- Compile the library as either a DLL or SO file.
- Run the file using the program created in part 1.
Thursday, August 21, 2014
Developing LightSky
I decided to make something simple for the first test of LightSky while also making a fast, stable desktop game framework that builds on OpenGL 3.3. DirectX and GLES are not really a big concern of mine because if I branched out too much then development would become extremely slow and whatever progress I've made on my portfolio would suffer as well. For now, LightSky is my test framework that I want to grow into something "official" that can be used for commercial development. Of course, any progress made towards being a reputable framework should have some sort of documenting along the way, just for fun.
So here's just a quick example of where the framework stands right now :)
Today's test makes use of a few awesome graphics features. Modern OpenGL allows for a technique called instancing which allows you to render thousands of copies of geometry at once, greatly increasing performance when you're drawing things such as crowds or particles. I wanted this to be in LightSky from the start in order to make it easier to handle such tasks in the future.
Another thing that's in the test is Perlin Noise. As you can see in the screenshot, there are thousands of tiny gray balls being rendered and each uses a texture that's been randomly generated using Perlin Noise. This type of technique can also be applied to terrain geometry/texturing, sky boxes, and water.
Finally, I wanted to be able to easily render to the screen, then apply what was rendered to a texture. OpenGL let me do this through framebuffers. If you've ever played Half-Life 2 or Portal, you can see framebuffers in action whenever watching a TV or looking through a portal. First, something is rendered to a texture through a framebuffer, then you attach the same texture to a portal. This gives you the illusion of looking at yourself through a mirror.
These are just a few things that I've been focusing on in the early stages of LightSky and there's A LOT more that needs to be added before it's ready to be used in production. For now though, here's a demo. You can make the test program fulllscreen by pressing the F11 key, right click on the window in order to use your mouse, or left click to put it back in the window. Press the escape key to quit. A new noise texture can be generated by pressing the space bar. You can also change the resolution dynamically by rolling your mouse wheel (this lets you see the framebuffer in action).
I'll keep posting updates as I add more to the framework. Thanks for reading!
So here's just a quick example of where the framework stands right now :)
Today's test makes use of a few awesome graphics features. Modern OpenGL allows for a technique called instancing which allows you to render thousands of copies of geometry at once, greatly increasing performance when you're drawing things such as crowds or particles. I wanted this to be in LightSky from the start in order to make it easier to handle such tasks in the future.
Another thing that's in the test is Perlin Noise. As you can see in the screenshot, there are thousands of tiny gray balls being rendered and each uses a texture that's been randomly generated using Perlin Noise. This type of technique can also be applied to terrain geometry/texturing, sky boxes, and water.
Finally, I wanted to be able to easily render to the screen, then apply what was rendered to a texture. OpenGL let me do this through framebuffers. If you've ever played Half-Life 2 or Portal, you can see framebuffers in action whenever watching a TV or looking through a portal. First, something is rendered to a texture through a framebuffer, then you attach the same texture to a portal. This gives you the illusion of looking at yourself through a mirror.
These are just a few things that I've been focusing on in the early stages of LightSky and there's A LOT more that needs to be added before it's ready to be used in production. For now though, here's a demo. You can make the test program fulllscreen by pressing the F11 key, right click on the window in order to use your mouse, or left click to put it back in the window. Press the escape key to quit. A new noise texture can be generated by pressing the space bar. You can also change the resolution dynamically by rolling your mouse wheel (this lets you see the framebuffer in action).
I'll keep posting updates as I add more to the framework. Thanks for reading!
Subscribe to:
Posts (Atom)
