优化体验及性能 #3

Merged
pointer-to-bios merged 5 commits from pointer-to-bios/JungleOpenSourceProjectRepo:main into main 2024-04-27 06:46:10 +08:00
1 changed files with 156 additions and 6 deletions
Showing only changes of commit 37765e7cab - Show all commits

View File

@ -11,11 +11,26 @@
#include "camera.h"
#include <iostream>
#include <pthread.h>
#ifdef __linux__
#include <unistd.h>
#endif
#ifdef _WIN32
#include <Windows.h>
#endif
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
void focus_callback(GLFWwindow *window, int focused);
void processInput(GLFWwindow *window);
void delay_fps(int fps, int render_time_us);
void *getting_fps(void *window);
bool running = true;
bool pausing = false;
bool just_dispaused = false;
// settings
const unsigned int SCR_WIDTH = 854;
@ -31,6 +46,22 @@ bool firstMouse = true;
float deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f;
// focuse controlling
bool window_focused = false;
// window managing
int curr_width;
int curr_height;
int curr_x;
int curr_y;
// monitors controlling
int monitor_count;
GLFWmonitor **monitors;
// fps control
int fps;
int main()
{
// std::cout << "debug";
@ -40,6 +71,8 @@ int main()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_FALSE);
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE);
#ifdef BLENDING
glEnable(GL_BLEND);
#endif
@ -57,10 +90,17 @@ int main()
glfwTerminate();
return -1;
}
monitors = glfwGetMonitors(&monitor_count);
pthread_t thr_getting_fps;
pthread_create(&thr_getting_fps, nullptr, getting_fps, window);
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetWindowFocusCallback(window, focus_callback);
// tell GLFW to capture our mouse
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
@ -251,6 +291,8 @@ int main()
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// glfwSetCursorPos(window, curr_x + curr_width / 2, curr_y + curr_height / 2);
// render loop
// -----------
while (!glfwWindowShouldClose(window))
@ -321,8 +363,13 @@ int main()
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
float renderTime = static_cast<float>(glfwGetTime()) - lastFrame;
delay_fps(fps, (int)(renderTime * 1000 * 1000));
}
running = false;
glDisable(GL_DEPTH_TEST);
// optional: de-allocate all resources once they've outlived their purpose:
@ -338,10 +385,28 @@ int main()
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
bool ESC_KEY_PRESSED = false;
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
int esc_key_evetype = glfwGetKey(window, GLFW_KEY_ESCAPE);
if (!ESC_KEY_PRESSED && esc_key_evetype == GLFW_PRESS)
{
ESC_KEY_PRESSED = true;
pausing = !pausing;
if (!pausing)
just_dispaused = true;
if (!pausing)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
else if (esc_key_evetype == GLFW_RELEASE)
{
ESC_KEY_PRESSED = false;
}
if (pausing)
return;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime);
@ -356,7 +421,6 @@ void processInput(GLFWwindow *window)
camera.ProcessKeyboard(UP, deltaTime);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
camera.ProcessKeyboard(DOWN, deltaTime);
}
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
@ -365,16 +429,39 @@ void framebuffer_size_callback(GLFWwindow *window, int width, int 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);
curr_width = width;
curr_height = height;
if ((double)width / height > (double)SCR_WIDTH / SCR_HEIGHT)
{ // When profiled screen size rate less than new size
int vpwidth = width;
int vpheight = width * SCR_HEIGHT / SCR_WIDTH;
glViewport(0, -(vpheight - height) / 2, vpwidth, vpheight);
}
else
{ // or more than
int vpheight = height;
int vpwidth = height * SCR_WIDTH / SCR_HEIGHT;
glViewport(-(vpwidth - width) / 2, 0, vpwidth, vpheight);
}
}
// glfw: whenever the mouse moves, this callback is called
// -------------------------------------------------------
void mouse_callback(GLFWwindow *window, double xposIn, double yposIn)
{
if (pausing)
return;
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
if (just_dispaused)
{
lastX = xpos;
lastY = ypos;
just_dispaused = false;
}
if (firstMouse)
{
lastX = xpos;
@ -397,3 +484,66 @@ void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
{
camera.ProcessMouseScroll(static_cast<float>(yoffset));
}
void focus_callback(GLFWwindow *window, int focused)
{
if (focused)
{
window_focused = true;
}
else
{
window_focused = false;
pausing = true;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
void *getting_fps(void *_window)
{
GLFWwindow *window = static_cast<GLFWwindow *>(_window);
while (running)
{
glfwGetWindowPos(window, &curr_x, &curr_y);
glfwGetWindowSize(window, &curr_width, &curr_height);
// get current monitor's scale
int on_which = -1;
GLFWvidmode *vmode;
for (int i = 0; i < monitor_count; i++)
{
int monx, mony;
glfwGetMonitorPos(monitors[i], &monx, &mony);
GLFWvidmode *mode = const_cast<GLFWvidmode *>(glfwGetVideoMode(monitors[i]));
if (curr_x >= monx && curr_x < mode->width && curr_y >= mony && curr_y < mode->height)
{
on_which = i;
vmode = mode;
break;
}
}
if (on_which != -1)
{
fps = vmode->refreshRate;
}
#ifdef __linux__
usleep(1000);
#endif
#ifdef _WIN32
Sleep(1);
#endif
}
std::cout << std::endl;
return nullptr;
}
void delay_fps(int fps, int render_time_us)
{
#ifdef __linux__
int del = 1000 * 1000 / fps;
usleep(del - render_time_us);
#endif
#ifdef _WIN32
int del = 1000 / fps;
Sleep(del - render_time_us / 1000);
#endif
}