Changeset a2f62d7 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Dec 16, 2020, 2:54:13 AM (4 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
feature/imgui-sdl, master
Children:
c324d6a
Parents:
e68d549
Message:

Make some minor updates to OpenGLReference

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    re68d549 ra2f62d7  
    172172void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
    173173void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
    174 void window_size_callback(GLFWwindow* window, int width, int height);
     174void framebuffer_size_callback(GLFWwindow* window, int width, int height);
    175175
    176176void APIENTRY debugGlCallback(
     
    262262
    263263/*** START OF REFACTORED CODE ***/
    264 int windowWidth = 640;
    265 int windowHeight = 480;
     264unsigned int windowWidth = 640;
     265unsigned int windowHeight = 480;
    266266
    267267vec3 cam_pos;
     
    333333   }
    334334
     335   // Currently, this is just for IMGUI
    335336   string glsl_version = "#version 410";
    336337
     
    345346#endif
    346347
    347    GLFWwindow* window = NULL;
    348348   GLFWmonitor* mon = NULL;
    349 
    350    glfwWindowHint(GLFW_SAMPLES, 16);
    351    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
    352349
    353350   if (FULLSCREEN) {
     
    359356      cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
    360357   }
    361    window = glfwCreateWindow(windowWidth, windowHeight, "New OpenGL Game", mon, NULL);
    362 
    363    if (!window) {
     358
     359   glfwWindowHint(GLFW_SAMPLES, 16);
     360   glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, true);
     361
     362   GLFWwindow* window = glfwCreateWindow(windowWidth, windowHeight, "New OpenGL Game", mon, NULL);
     363
     364   if (window == NULL) {
    364365      gl_log_err("ERROR: could not open window with GLFW3");
    365366      cerr << "ERROR: could not open window with GLFW3" << endl;
     
    370371
    371372   glfwMakeContextCurrent(window);
    372    glViewport(0, 0, windowWidth, windowHeight);
    373373
    374374   glewExperimental = GL_TRUE;
     
    378378      cout << "FOUND GLEW debug extension" << endl;
    379379      glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
    380       glDebugMessageCallback((GLDEBUGPROC)debugGlCallback, nullptr);
     380      glDebugMessageCallback((GLDEBUGPROC)debugGlCallback, NULL);
    381381      cout << "Bound debug callback" << endl;
    382382   } else {
    383383      cout << "OpenGL debug message callback is not supported" << endl;
    384384   }
     385
     386   glfwSetMouseButtonCallback(window, mouse_button_callback);
     387   glfwSetKeyCallback(window, key_callback);
     388   glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
    385389
    386390   srand(time(0));
     
    418422   //io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad;      // Enable Gamepad Controls
    419423
     424   // Setup Platform/Renderer backends
     425   ImGui_ImplGlfw_InitForOpenGL(window, true);
     426   ImGui_ImplOpenGL3_Init(glsl_version.c_str());
     427
    420428   // Setup Dear ImGui style
    421429   ImGui::StyleColorsDark();
    422430   //ImGui::StyleColorsClassic();
    423431
    424    // Setup Platform/Renderer backends
    425    ImGui_ImplGlfw_InitForOpenGL(window, true);
    426    ImGui_ImplOpenGL3_Init(glsl_version.c_str());
    427 
    428    glfwSetMouseButtonCallback(window, mouse_button_callback);
    429    glfwSetKeyCallback(window, key_callback);
    430    glfwSetWindowSizeCallback(window, window_size_callback);
    431432/*** END OF REFACTORED CODE ***/
    432433
     
    10001001   }
    10011002
     1003   for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) {
     1004      delete* it;
     1005   }
     1006
    10021007   ImGui_ImplOpenGL3_Shutdown();
    10031008   ImGui_ImplGlfw_Shutdown();
     
    10081013
    10091014   close_log();
    1010 
    1011    // free memory
    1012 
    1013    for (vector<SceneObject*>::iterator it = objects.begin(); it != objects.end(); it++) {
    1014       delete *it;
    1015    }
    10161015
    10171016   return 0;
     
    10841083
    10851084/*** START OF REFACTORED CODE ***/
    1086 void window_size_callback(GLFWwindow* window, int width, int height) {
     1085void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    10871086   cout << "Window resized to (" << width << ", " << height << ")" << endl;
    10881087
    10891088   windowWidth = width;
    10901089   windowHeight = height;
     1090
     1091   // make sure the viewport matches the new window dimensions; note that width and
     1092   // height will be significantly larger than specified on retina displays.
     1093   glViewport(0, 0, width, height);
    10911094
    10921095   // TODO: Ideally, remove the window title bar when the window is maximized
     
    12411244
    12421245unsigned char* loadImage(string file_name, int* x, int* y) {
    1243   int n;
    1244   int force_channels = 4; // This forces RGBA (4 bytes per pixel)
    1245   unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
    1246 
    1247   int width_in_bytes = *x * 4;
    1248   unsigned char *top = NULL;
    1249   unsigned char *bottom = NULL;
    1250   unsigned char temp = 0;
    1251   int half_height = *y / 2;
    1252 
    1253   // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
    1254   for (int row = 0; row < half_height; row++) {
    1255      top = image_data + row * width_in_bytes;
    1256      bottom = image_data + (*y - row - 1) * width_in_bytes;
    1257      for (int col = 0; col < width_in_bytes; col++) {
    1258         temp = *top;
    1259         *top = *bottom;
    1260         *bottom = temp;
    1261         top++;
    1262         bottom++;
    1263      }
    1264   }
    1265 
    1266   if (!image_data) {
    1267     gl_log_err("ERROR: could not load %s", file_name.c_str());
    1268     cerr << "ERROR: could not load " << file_name << endl;
    1269     get_log() << "ERROR: could not load " << file_name << endl;
    1270   }
    1271 
    1272   // Not Power-of-2 check
    1273   if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
    1274      gl_log_err("WARNING: texture %s is not power-of-2 dimensions", file_name.c_str());
    1275      cerr << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl;
    1276      get_log() << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl;
    1277   }
    1278 
    1279   return image_data;
     1246   int n;
     1247   int force_channels = 4; // This forces RGBA (4 bytes per pixel)
     1248   unsigned char* image_data = stbi_load(file_name.c_str(), x, y, &n, force_channels);
     1249
     1250   int width_in_bytes = *x * 4;
     1251   unsigned char *top = NULL;
     1252   unsigned char *bottom = NULL;
     1253   unsigned char temp = 0;
     1254   int half_height = *y / 2;
     1255
     1256   // TODO: I should be able to use the line below instead of doing the flip
     1257   // stbi_set_flip_vertically_on_load(true);
     1258
     1259   // flip image upside-down to account for OpenGL treating lower-left as (0, 0)
     1260   for (int row = 0; row < half_height; row++) {
     1261      top = image_data + row * width_in_bytes;
     1262      bottom = image_data + (*y - row - 1) * width_in_bytes;
     1263      for (int col = 0; col < width_in_bytes; col++) {
     1264         temp = *top;
     1265         *top = *bottom;
     1266         *bottom = temp;
     1267         top++;
     1268         bottom++;
     1269      }
     1270   }
     1271
     1272   if (!image_data) {
     1273      gl_log_err("ERROR: could not load %s", file_name.c_str());
     1274      cerr << "ERROR: could not load " << file_name << endl;
     1275      get_log() << "ERROR: could not load " << file_name << endl;
     1276   }
     1277
     1278   // Not Power-of-2 check
     1279   if ((*x & (*x - 1)) != 0 || (*y & (*y - 1)) != 0) {
     1280      gl_log_err("WARNING: texture %s is not power-of-2 dimensions", file_name.c_str());
     1281      cerr << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl;
     1282      get_log() << "WARNING: texture " << file_name << " is not power-of-2 dimensions" << endl;
     1283   }
     1284
     1285   return image_data;
    12801286}
    12811287/*** END OF REFACTORED CODE ***/
     
    23732379
    23742380void renderScene(map<ObjectType, ShaderModelGroup>& modelGroups, GLuint ubo) {
    2375 
    23762381   glUseProgram(modelGroups[ObjectType::SHIP].shaderProgram);
    23772382   glBindVertexArray(modelGroups[ObjectType::SHIP].vao);
Note: See TracChangeset for help on using the changeset viewer.