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


Ignore:
Timestamp:
May 10, 2019, 6:18:31 PM (6 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
155a7cf
Parents:
c55614a
Message:

Add a callback for the window resize event

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rc55614a re6bc0f4  
    167167void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
    168168void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
     169void window_size_callback(GLFWwindow* window, int width, int height);
    169170
    170171void APIENTRY debugGlCallback(
     
    261262bool key_down[NUM_KEYS];
    262263
    263 int width = 640;
    264 int height = 480;
     264int windowWidth = 640;
     265int windowHeight = 480;
    265266
    266267vec3 cam_pos;
     
    329330      const GLFWvidmode* vmode = glfwGetVideoMode(mon);
    330331
    331       width = vmode->width;
    332       height = vmode->height;
     332      windowWidth = vmode->width;
     333      windowHeight = vmode->height;
    333334      cout << "Fullscreen resolution " << vmode->width << "x" << vmode->height << endl;
    334335   }
    335    window = glfwCreateWindow(width, height, "New OpenGL Game", mon, NULL);
     336   window = glfwCreateWindow(windowWidth, windowHeight, "New OpenGL Game", mon, NULL);
    336337
    337338   if (!window) {
     
    342343
    343344   glfwMakeContextCurrent(window);
     345   glViewport(0, 0, windowWidth, windowHeight);
     346
    344347   glewExperimental = GL_TRUE;
    345348   glewInit();
     
    351354      cout << "Bound debug callback" << endl;
    352355   } else {
    353      cout << "OpenGL debugg message callback is not supported" << endl;
     356      cout << "OpenGL debug message callback is not supported" << endl;
    354357   }
    355358
     
    394397   glfwSetMouseButtonCallback(window, mouse_button_callback);
    395398   glfwSetKeyCallback(window, key_callback);
     399
     400   glfwSetWindowSizeCallback(window, window_size_callback);
    396401
    397402   const GLubyte* renderer = glGetString(GL_RENDERER);
     
    590595   // (Maybe I should just use glm::perspective, after making sure it matches what I have now)
    591596   float fov = 67.0f * ONE_DEG_IN_RAD;
    592    float aspect = (float)width / (float)height;
     597   float aspect = (float)windowWidth / (float)windowHeight;
    593598
    594599   float range = tan(fov * 0.5f) * NEAR_CLIP;
     
    961966      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    962967
     968      // Anton's book suggests placing this here, after glClear(). Check it's impact on framerate
     969      //glViewport(0, 0, windowWidth, windowHeight);
     970
    963971      switch (curState) {
    964972         case STATE_MAIN_MENU:
     
    10021010      selectedObject = NULL;
    10031011
    1004       float x = (2.0f*mouse_x) / width - 1.0f;
    1005       float y = 1.0f - (2.0f*mouse_y) / height;
     1012      float x = (2.0f*mouse_x) / windowWidth - 1.0f;
     1013      float y = 1.0f - (2.0f*mouse_y) / windowHeight;
    10061014
    10071015      cout << "x: " << x << ", y: " << y << endl;
     
    10511059   // should be true for GLFW_PRESS and GLFW_REPEAT
    10521060   key_down[key] = (action != GLFW_RELEASE);
     1061}
     1062
     1063void window_size_callback(GLFWwindow* window, int width, int height) {
     1064   cout << "Window resized to (" << width << ", " << height << ")" << endl;
     1065
     1066   windowWidth = width;
     1067   windowHeight = height;
     1068
     1069   // TODO: Ideally, remove the window title bar when the window is maximized
     1070   // Check https://github.com/glfw/glfw/issues/778
     1071
     1072   // This requires glfw3.3. I think I have to upgrade
     1073   // Doesn't seem to be needed in OSX and also causes a segfault there
     1074   //glfwSetWindowAttrib(window, GLFW_DECORATED, GLFW_FALSE);
    10531075}
    10541076
     
    24442466      int padding = 4;
    24452467      ImGui::SetNextWindowPos(ImVec2(-padding, -padding), ImGuiCond_Once);
    2446       ImGui::SetNextWindowSize(ImVec2(width + 2 * padding, height + 2 * padding), ImGuiCond_Once);
     2468      ImGui::SetNextWindowSize(ImVec2(windowWidth + 2 * padding, windowHeight + 2 * padding), ImGuiCond_Always);
    24472469      ImGui::Begin("WndMain", NULL,
    24482470         ImGuiWindowFlags_NoTitleBar |
Note: See TracChangeset for help on using the changeset viewer.