feat: add fullscreen support

This commit is contained in:
2026-03-28 16:52:55 +08:00
parent a7c42f249d
commit f41e9f7757
3 changed files with 51 additions and 5 deletions

View File

@@ -10,7 +10,7 @@ public:
GLFWwindow* get_glfw_window();
void init();
void update_viewport();
void toggle_fullscreen();
private:
float m_aspect;
GLFWwindow* m_window;

View File

@@ -40,6 +40,9 @@ void App::init() {
}
void App::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
CUBED_ASSERT_MSG(app, "nullptr");
switch(key) {
case GLFW_KEY_Q:
if (action == GLFW_PRESS) {
@@ -55,10 +58,14 @@ void App::key_callback(GLFWwindow* window, int key, int scancode, int action, in
if (action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
case GLFW_KEY_F11:
if (action == GLFW_PRESS) {
app->m_window.toggle_fullscreen();
}
break;
}
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
CUBED_ASSERT_MSG(app, "nullptr");
app->m_world.get_player("TestPlayer").update_player_move_state(key, action);
}

View File

@@ -39,12 +39,13 @@ void Window::init() {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
m_window = glfwCreateWindow(800, 600, "Cubed", NULL, NULL);
glfwMakeContextCurrent(m_window);
glfwSwapInterval(1);
glfwSetInputMode(m_window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (glfwRawMouseMotionSupported()) {
glfwSetInputMode(m_window, GLFW_RAW_MOUSE_MOTION, GLFW_TRUE);
@@ -52,6 +53,44 @@ void Window::init() {
LOG::warn("Don,t support Raw Mouse Motion");
}
GLFWmonitor* primary = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(primary);
glfwSetWindowPos(m_window, static_cast<int>(mode->width / 2.0f) - 400, static_cast<int>(mode->height / 2.0f) - 300);
//update_viewport();
}
void Window::toggle_fullscreen() {
static int windowed_xpos = 0, windowed_ypos = 0;
static int windowed_width = 800, windowed_height = 600;
GLFWmonitor* monitor = glfwGetWindowMonitor(m_window);
if (monitor != nullptr) {
glfwSetWindowMonitor(
m_window,
nullptr,
windowed_xpos,
windowed_ypos,
windowed_width,
windowed_height,
0
);
} else {
glfwGetWindowPos(m_window, &windowed_xpos, &windowed_ypos);
glfwGetWindowSize(m_window, &windowed_width, &windowed_height);
GLFWmonitor* primary = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(primary);
glfwSetWindowMonitor(
m_window,
primary,
0,
0,
mode->width,
mode->height,
GL_DONT_CARE
);
}
update_viewport();
}