Index: new-game.cpp
===================================================================
--- new-game.cpp	(revision aaf27cd6df9ae2ad862a15113ce300175efaad7a)
+++ new-game.cpp	(revision a2f62d7e0b4b68fcc0c54acb1c1200b2bf9af80d)
@@ -172,5 +172,5 @@
 void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
 void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
-void window_size_callback(GLFWwindow* window, int width, int height);
+void framebuffer_size_callback(GLFWwindow* window, int width, int height);
 
 void APIENTRY debugGlCallback(
@@ -262,6 +262,6 @@
 
 /*** START OF REFACTORED CODE ***/
-int windowWidth = 640;
-int windowHeight = 480;
+unsigned int windowWidth = 640;
+unsigned int windowHeight = 480;
 
 vec3 cam_pos;
@@ -333,4 +333,5 @@
    }
 
+   // Currently, this is just for IMGUI
    string glsl_version = "#version 410";
 
@@ -345,9 +346,5 @@
 #endif
 
-   GLFWwindow* window = NULL;
    GLFWmonitor* mon = NULL;
-
-   glfwWindowHint(GLFW_SAMPLES, 16);
-   glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
 
    if (FULLSCREEN) {
@@ -359,7 +356,11 @@
       cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
    }
-   window = glfwCreateWindow(windowWidth, windowHeight, "New OpenGL Game", mon, NULL);
-
-   if (!window) {
+
+   glfwWindowHint(GLFW_SAMPLES, 16);
+   glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
+
+   GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "New OpenGL Game", mon, NULL);
+
+   if (window == NULL) {
       gl_log_err("ERROR: could not open window with GLFW3");
       cerr << "ERROR: could not open window with GLFW3" << endl;
@@ -370,5 +371,4 @@
 
    glfwMakeContextCurrent(window);
-   glViewport(0, 0, windowWidth, windowHeight);
 
    glewExperimental = GL_TRUE;
@@ -378,9 +378,13 @@
       cout << "FOUND GLEW debug extension" << endl;
       glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
-      glDebugMessageCallback((GLDEBUGPROC)debugGlCallback, nullptr);
+      glDebugMessageCallback((GLDEBUGPROC)debugGlCallback, NULL);
       cout << "Bound debug callback" << endl;
    } else {
       cout << "OpenGL debug message callback is not supported" << endl;
    }
+
+   glfwSetMouseButtonCallback(window, mouse_button_callback);
+   glfwSetKeyCallback(window, key_callback);
+   glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
 
    srand(time(0));
@@ -418,15 +422,12 @@
    //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls
 
+   // Setup Platform/Renderer backends
+   ImGui_ImplGlfw_InitForOpenGL(window, true);
+   ImGui_ImplOpenGL3_Init(glsl_version.c_str());
+
    // Setup Dear ImGui style
    ImGui::StyleColorsDark();
    //ImGui::StyleColorsClassic();
 
-   // Setup Platform/Renderer backends
-   ImGui_ImplGlfw_InitForOpenGL(window, true);
-   ImGui_ImplOpenGL3_Init(glsl_version.c_str());
-
-   glfwSetMouseButtonCallback(window, mouse_button_callback);
-   glfwSetKeyCallback(window, key_callback);
-   glfwSetWindowSizeCallback(window, window_size_callback);
 /*** END OF REFACTORED CODE ***/
 
@@ -1000,4 +1001,8 @@
    }
 
+   for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) {
+      delete* it;
+   }
+
    ImGui_ImplOpenGL3_Shutdown();
    ImGui_ImplGlfw_Shutdown();
@@ -1008,10 +1013,4 @@
 
    close_log();
-
-   // free memory
-
-   for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) {
-      delete *it;
-   }
 
    return 0;
@@ -1084,9 +1083,13 @@
 
 /*** START OF REFACTORED CODE ***/
-void window_size_callback(GLFWwindow* window, int width, int height) {
+void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    cout << "Window resized to (" << width << ", " << height << ")" << endl;
 
    windowWidth = width;
    windowHeight = height;
+
+   // make sure the viewport matches the new window dimensions; note that width and
+   // height will be significantly larger than specified on retina displays.
+   glViewport(0, 0, width, height);
 
    // TODO: Ideally, remove the window title bar when the window is maximized
@@ -1241,41 +1244,44 @@
 
 unsigned char* loadImage(string file_name, int* x, int* y) {
-  int n;
-  int force_channels = 4; // This forces RGBA (4 bytes per pixel)
-  unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
-
-  int width_in_bytes = *x * 4;
-  unsigned char *top = NULL;
-  unsigned char *bottom = NULL;
-  unsigned char temp = 0;
-  int half_height = *y / 2;
-
-  // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
-  for (int row = 0; row < half_height; row++) {
-     top = image_data + row * width_in_bytes;
-     bottom = image_data + (*y - row - 1) * width_in_bytes;
-     for (int col = 0; col < width_in_bytes; col++) {
-        temp = *top;
-        *top = *bottom;
-        *bottom = temp;
-        top++;
-        bottom++;
-     }
-  }
-
-  if (!image_data) {
-    gl_log_err("ERROR: could not load %s", file_name.c_str());
-    cerr << "ERROR: could not load " << file_name << endl;
-    get_log() << "ERROR: could not load " << file_name << endl;
-  }
-
-  // Not Power-of-2 check
-  if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
-     gl_log_err("WARNING: texture %s is not power-of-2 dimensions", file_name.c_str());
-     cerr << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl;
-     get_log() << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl;
-  }
-
-  return image_data;
+   int n;
+   int force_channels = 4; // This forces RGBA (4 bytes per pixel)
+   unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
+
+   int width_in_bytes = *x * 4;
+   unsigned char *top = NULL;
+   unsigned char *bottom = NULL;
+   unsigned char temp = 0;
+   int half_height = *y / 2;
+
+   // TODO: I should be able to use the line below instead of doing the flip
+   // stbi_set_flip_vertically_on_load(true);
+
+   // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
+   for (int row = 0; row < half_height; row++) {
+      top = image_data + row * width_in_bytes;
+      bottom = image_data + (*y - row - 1) * width_in_bytes;
+      for (int col = 0; col < width_in_bytes; col++) {
+         temp = *top;
+         *top = *bottom;
+         *bottom = temp;
+         top++;
+         bottom++;
+      }
+   }
+
+   if (!image_data) {
+      gl_log_err("ERROR: could not load %s", file_name.c_str());
+      cerr << "ERROR: could not load " << file_name << endl;
+      get_log() << "ERROR: could not load " << file_name << endl;
+   }
+
+   // Not Power-of-2 check
+   if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
+      gl_log_err("WARNING: texture %s is not power-of-2 dimensions", file_name.c_str());
+      cerr << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl;
+      get_log() << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl;
+   }
+
+   return image_data;
 }
 /*** END OF REFACTORED CODE ***/
@@ -2373,5 +2379,4 @@
 
 void renderScene(map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo) {
-
    glUseProgram(modelGroups[ObjectType::SHIP].shaderProgram);
    glBindVertexArray(modelGroups[ObjectType::SHIP].vao);
