GLFW 无故开放和关闭

问题描述 投票:0回答:1

项目设置:在Windows机器上设置如下。

问题:

我运行的是 Windows 11,文件夹中有以下文件。我正在尝试在全屏模式下运行

GLFWwindow
并在上面、中间和底部绘制一些四边形。由于某种原因,没有渲染颜色,移动鼠标只是“关闭”程序。我一直在调试并达到这一点。我不知道为什么这段代码不起作用。 在我的系统中,我使用

msys2

mingw-86-64
以及
GNU 编译器集合

include/game.h

#ifndef H_GAME #define H_GAME #include <Windows.h> #include <memory> #include "../vendors/glfw/include/GLFW/glfw3.h" class Game { #define rgb2f(x) ((int)x/255.0f) public: Game(); ~Game(); void Run(); private: GLFWwindow* m_window = nullptr; const GLFWvidmode* m_video = nullptr; }; #endif // H_GAME

src/game.cpp

#include "../include/Game.h" Game::Game(){} Game::~Game(){ glfwDestroyWindow(m_window); glfwTerminate(); } void Game::Run(){ int width = 0; int height = 0; glfwInit(); glfwWindowHint(GLFW_CENTER_CURSOR, GL_TRUE); glfwWindowHint(GLFW_FOCUSED, GLFW_FALSE); glfwWindowHint(GLFW_AUTO_ICONIFY, GL_FALSE); glfwWindowHint(GLFW_FLOATING, GLFW_TRUE); glfwWindowHint(GLFW_DECORATED, GL_FALSE); #if defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__) width = GetSystemMetrics(SM_CXSCREEN); height = GetSystemMetrics(SM_CYSCREEN); #else m_video = glfwGetVideoMode(glfwGetPrimaryMonitor()); width = m_video->width; height = m_video->height; #endif glfwSwapInterval(1); m_window = glfwCreateWindow( width, height, "Video Game", glfwGetPrimaryMonitor(), nullptr); if (!m_window) { glfwTerminate(); } glfwSetWindowUserPointer(m_window, this); glfwSetKeyCallback(m_window, [](GLFWwindow* window, int key, int scancode, int action, int mods){ if(key == GLFW_KEY_ESCAPE && action == GLFW_PRESS){ } }); glfwSetWindowAspectRatio(m_window, 16, 9); glfwMakeContextCurrent(m_window); GLfloat color[]{ rgb2f(0), rgb2f(170), rgb2f(170) }; float pos[2][2] = { {0.0f, -1.0f},{0.0f, 1.0f} }; float dim[2] = { 1.0f, 0.1f }; GLfloat obj_color[]{ rgb2f(0), rgb2f(0), rgb2f(0) }; float obj_pos[2][2] = { {0.0f, 1.0f}, {1.0f, 0.0f} }; float obj_dim[2] = { 1.0f, 0.1f }; while (!glfwWindowShouldClose(m_window)){ glfwSwapBuffers(m_window); glClearColor(rgb2f(105), rgb2f(96), rgb2f(219), 1.0f); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glBegin(GL_QUADS); glColor3fv(color); glVertex2f(pos[0][0] - dim[0], pos[0][1] - dim[1]); glVertex2f(pos[0][0] + dim[0], pos[0][1] - dim[1]); glVertex2f(pos[0][0] + dim[0], pos[0][1] + dim[1]); glVertex2f(pos[0][0] - dim[0], pos[0][1] + dim[1]); glEnd(); glPopMatrix(); glMatrixMode(GL_MODELVIEW); glPushMatrix(); glLoadIdentity(); glBegin(GL_QUADS); glColor3fv(obj_color); glVertex2f(obj_pos[0][0] - obj_dim[0], obj_pos[0][1] - obj_dim[1]); glVertex2f(obj_pos[0][0] + obj_dim[0], obj_pos[0][1] - obj_dim[1]); glVertex2f(obj_pos[0][0] + obj_dim[0], obj_pos[0][1] + obj_dim[1]); glVertex2f(obj_pos[0][0] - obj_dim[0], obj_pos[0][1] + obj_dim[1]); glEnd(); glPopMatrix(); glfwPollEvents(); } }

src/main.cpp

#include "../include/Game.h" int main() { std::shared_ptr<Game> m_game; m_game->Run(); return 0; }

CMakeLists.txt

cmake_minimum_required(VERSION 3.27) project(GAME VERSION 0.0 DESCRIPTION "Game" LANGUAGES CXX) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Debug") endif() if(MSVC) add_compile_options(/EHsc /Za /Wall /WX) else() add_compile_options( -std=c++17 -D_GLIBCXX_USE_CXX11_ABI=0 ) endif() if(WIN32) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) endif() set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) find_package(OpenGL REQUIRED) include_directories(${OPENGL_INCLUDE_DIR}) add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/vendors/glfw/") include_directories("${CMAKE_CURRENT_SOURCE_DIR}/vendors/glfw/include") set(SOURCES src/main.cpp src/game.cpp) add_executable(GAME ${SOURCES}) target_include_directories(GAME PUBLIC ${GLFW_INCLUDE_DIRS}) target_link_libraries(GAME ${OPENGL_gl_LIBRARY} glfw)


c++ opengl cmake shared-ptr glfw
1个回答
0
投票
shared_ptr

nullptr
,这可能不是您想要的:
std::shared_ptr<Game> m_game;

使用 

std::make_shared<>

 实际为 
Game 分配一个
m_game
来指向:
std::shared_ptr<Game> m_game = std::make_shared<Game>();

© www.soinside.com 2019 - 2024. All rights reserved.