Thursday, 28 January 2016

Adding a setup window to our tutorial project

I've just checked in some changes to our tutorial project that I'm going to keep aside of our tutorial series. I'm not entirely sure if I'm going to keep this, enhance it or replace it.

The problem I kept running into is that we had hard coded our window mode and window size in our main.c source code. This mainly because writing a GUI could be a tutorial series all by itself and it detracts to what I'm trying to do here.

But then my eye fell on this library called AntTweakBar which is listed on the GLFW site. It is a really neat little library but it has a few rather troublesome drawbacks:
  1. it doesn't compile on the Mac anymore, lucky these were easy to fix up after a bit of reading on the support forum
  2. the mappings to GLFW are still based on GLFW 2.7, not 3.0. I've got them working except for our keyboard mappings. To properly fix this I'll also have to recompile the windows binaries
  3. there isn't a lot of freedom in the controls and layout of the window
  4. it is no longer maintained
Also the library compiled as a dylib which I wasn't too fond of. I changed the makefile to output a static library instead and that is included in the mac build on my github site.

I'm planning to do more with the library as time goes by and do a proper tutorial writeup if I do enjoy using it. If so I might be tempted to start enhancing it. But equally so I may end up replacing it.

So for now I'm just going to give a very brief overview of the changes that I've checked into our tutorial project.
First off, all the new support code is embedded into two new files:
  • setup.h - our header
  • setup.c - our source code
Yes, no single file approach for this one.
The library has a single public function which is SetupGLFW and all it does is open up a window that provides 3 dropdowns:
  • choosing the monitor to use for running in fullscreen, or the option to choosing windowed mode
  • choosing the resolution (if fullscreen)
  • choosing the stereo mode for optional 3D stereo rendering
After the user makes a selection a structure is populated which can be used in the calling function to properly setup a window like so:
  ...
  if (!SetupGLFW(&info)) {
    glfwTerminate();
    exit(EXIT_FAILURE);
  };
  
  // make sure we're using OpenGL 3.2+
  glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
  glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  if (info.monitor != NULL) {
    glfwWindowHint(GLFW_RED_BITS, info.vidmode.redBits);
    glfwWindowHint(GLFW_GREEN_BITS, info.vidmode.greenBits);
    glfwWindowHint(GLFW_BLUE_BITS, info.vidmode.blueBits);
    glfwWindowHint(GLFW_REFRESH_RATE, info.vidmode.refreshRate);
    if (info.stereomode == 2) {
      glfwWindowHint(GLFW_STEREO, GL_TRUE);    
    };

    errorlog(0, "Initializing fullscreen mode %i, %i - %i",info.vidmode.width, info.vidmode.height, info.stereomode);
    window = glfwCreateWindow(info.vidmode.width, info.vidmode.height, "GLFW Tutorial", info.monitor, NULL);
  } else {
    errorlog(0, "Initializing windowed mode %i, %i - %i",info.vidmode.width, info.vidmode.height, info.stereomode);
    window = glfwCreateWindow(info.vidmode.width, info.vidmode.height, "GLFW Tutorial", NULL, NULL);
  };
  
  if (window == NULL) {
    errorlog(-1, "Couldn''t construct GLFW window");
  } else {
    ...

The end result looks like this:

No comments:

Post a Comment