DEV Community

estrolabz
estrolabz

Posted on

DAY 02 - From Code to Canvas: My SDK Renders Its First Window (OpenGL)

I'm building a high-performance C++ SDK for making native, OpenGL-accelerated Windows apps — with security and modularity in mind.

Recap

Hello everyone and welcome back to another devlog! Yesterday we managed to setup the project and get everything working ready to start creating a window with an OpenGL context. We managed to create our project structure (Lib / DLL) ELBZ and (Test App) called TestLab then create our application and entry-point ready for use when we use ELBZ to create real-world applications.

If you have not seen the last dev log here is a link:

DAY 01 - I setup my SDK Project and Created the Entry Point!

Todays Objectives

  • Create a Windows Window Natively
  • Setup Glad as a Static Library
  • Create an OpenGL Render Context with OpenGL 4.6
  • Change the background screen to be another colour (Proves OpenGL is working!)

Progress

Creating the Native Window in Windows

So today I started by creating a simple header file with my window class structure like this:

class Window {

public:

    Window(const wchar_t* _title, unsigned short _width, unsigned short _height);
    void Run();
    ~Window();

private:
    void* window_instance = nullptr;
    void* device_context = nullptr;
    void* render_context = nullptr;
    const wchar_t* title;
    unsigned short width = 400;
    unsigned short height = 200;
};
Enter fullscreen mode Exit fullscreen mode

I decided to have three void pointers for storing important links to different aspects of the window that I may need to keep track of for the future.

I also decided to just take a title, width and height for the window for now as that is all I really need for this current moment in time.

As for the functions I am using a init, run and shutdown sequence of code. The constructor is the init method, the run is the run and the de-constructor is the shutdown method, used to handle the life cycle of the native windows window system.


Adding Default OpenGL Context

Next I added the default OpenGL Context to my window which resulted in this:

// Render: clear black
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Enter fullscreen mode Exit fullscreen mode

Fantastic its working!!!!!!

As you can see I have successfully got a window with OpenGL but… here is the thing this is using legacy OpenGL not modern OpenGL 4.6 so now its time to fix that.


Adding Glad to Enable Modern OpenGL 4.6

Adding glad caused quite a lot of headaches but after using different versions and figuring out why things don’t map correctly to each other I decided that I would create a new static library and link it to my dll and it seemed to fix my issue:


Now that I have created the new static lib I configured it with all the linking, defines build procedures etc…

Then after that I Generated the Glad Code for OpenGL 4.6 and WGL 1.0 - this was necessary because I was setting up a GL Context natively not using GLFW or SDL2 which means I needed to manually configure WGL to get OpenGL working with windows.


Setting Up the Modern OpenGL Context

Now that I had Glad fully working and setup I could use it to create the context for the native window to finally render graphics using Modern OpenGL.

I did this by firstly setting up a Pixel Descriptor, then setting it to the device context of the window then I had to create a temp context which is just to setup OpenGL and will be deleted after making it the current context.

Once that was done I could setup some attributes for OpenGL like setting the version I want to use etc…

Then I proceeded to create the context attrib arbs which just creates a gl context for me which will allow me to test OpenGL and ensure it is all working correctly.

Once that was done I could delete the temp context as it was not needed any more and create a new one which was my main context used for my actuall SDK.

Then I proceeded to check glad is loaded and working correctly and set the different pointers of the window class to be what I need such as device context and render context.

I also output some debug info to the screen just so that I could see some useful information such as:

  • OpenGL Version
  • OpenGL Renderer - which is the graphics card
  • OpenGL Shading Language Version

Configuring OpenGL On-Update

Now that the context was setup I could go to my main loop and set OpenGL background colour clear and swap buffers to ensure it actually renders on the window I want.

// Render: clear black
glClearColor(0.3f, 0.3f, 0.5f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Swap buffers
SwapBuffers((HDC)device_context);
Enter fullscreen mode Exit fullscreen mode

With that the results are as shown here:

I successfully got a OpenGL window rendered with some debug info in the console for reassurance everything is working as expected! 😊


Final Thoughts

Conclusion

After implementing these features it was a little challenging at times but overall not too difficult and now I am more than every excited to start developing a 2D renderer and UI library etc… I cannot wait to make this SDK powerful with security, efficiency and practicality.

Tomorrows Objectives:

  • Add a OpenGL API Layer (Raw OpenGL Code)
  • Add a Graphics Render API Layer (Interfaces between all possible graphics apis and the renderer)
  • Add a Renderer 2D Layer (Allows things like creating rectangles on the screen something simple)
  • Create a Logger (I need the ability to log to the console more professionally and not just std::couts everywhere)

Support

This SDK is still early in development, and I’m not ready to charge for it yet.

But if you'd like to support development or get early access when builds drop, you can follow or join here: my Patreon

Contact & Other Socials

https://www.instagram.com/estrolabz/

https://www.patreon.com/c/ESTROLABZ

https://www.youtube.com/@estrolabzuk

https://github.com/estrolabz

https://dev.to/estrolabz

https://www.reddit.com/r/ESTROLABZ/

https://autumn-oboe-c16.notion.site/ESTROLABZ-Blog-237763e1ef908090a5fec3ea8c04331e?source=copy_link

Top comments (0)