I'm following this tutorial for cross-platform game dev, which is exactly what I want to do. However, I am not able to open a window. My code structure:
build
external
engine
external
glfw
glm
include
template_engine
platform
window.h
engine.h
service_locator.h
src
platform
glfw_window.h
glfw_window.cpp
engine.cpp
CMakeLists.txt
src
main.cpp
CMakeLists.txt
window.h:
#pragma once
class Window
{
public:
virtual void OpenWindow() = 0;
virtual bool Update() = 0;
};
engine.h:
#pragma once
struct TemplateEngine
{
static void Init();
};
service_locator.h (slightly modified from the tutorial, should still work):
#pragma once
#include <memory>
#include <template_engine/platform/window.h>
class ServiceLocator
{
public:
static inline const std::unique_ptr<Window>& GetWindow() { return _window; }
static inline void Provide(Window* window)
{
if(_window == nullptr)
{
_window = std::unique_ptr<Window>(window);
}
}
private:
static inline std::unique_ptr<Window> _window = nullptr;
}
glfw_window.h:
#pragma once
#include <template_engine/platform/window.h>
#include <GLFW/glfw3.h>
class CustomWindow : public Window
{
public:
CustomWindow();
virtual void OpenWindow() override;
virtual bool Update() override;
private:
GLFWwindow* _window;
};
glfw_window.cpp:
#include "glfw_window.h"
CustomWindow::CustomWindow()
{
_window = nullptr;
}
void CustomWindow::OpenWindow()
{
glfwInit();
_window = glfwCreateWindow(800, 600, "template engine window", nullptr, nullptr);
}
bool CustomWindow::Update()
{
glfwPollEvents();
return glfwWindowShouldClose(_window);
}
engine.cpp:
#include <template_engine/engine.h>
#include <iostream>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <template_engine/service_locator.h>
#include "platform/glfw_window.h"
void TemplateEngine::Init()
{
std::cout << "Initializing window" << std::endl;
ServiceLocator::Provide(new CustomWindow());
}
external/engine/CMakeLists.txt:
cmake_minimum_required(VERSION 3.26)
project(template_cpp_engine VERSION 0.0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
# add external libraries
add_subdirectory(external/glm)
add_subdirectory(external/glfw)
add_library(${PROJECT_NAME}
src/engine.cpp
src/platform/glfw_window.cpp
src/platform/glfw_window.h)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
glm
PRIVATE
glfw
)
target_link_libraries(${PROJECT_NAME}
PUBLIC
glm
PRIVATE
glfw
)
main.cpp:
#include <iostream>
#include <template_engine/engine.h>
#include <glm/glm.hpp>
#include <template_engine/service_locator.h>
int main(int argc, char** argv)
{
std::cout << "Hello, world!\n";
TemplateEngine::Init();
ServiceLocator::GetWindow()->OpenWindow();
while(!ServiceLocator::GetWindow()->Update())
{
}
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.26)
project(template_cpp_application VERSION 0.0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 23)
add_subdirectory(external/engine)
# template_cpp_application.exe
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} template_cpp_engine)
target_include_directories(${PROJECT_NAME} PRIVATE template_cpp_engine)
I'm still fairly inexperienced, but this looks like it should work to me, and it worked in the tutorial. However, I get some errors whenever I try to build this program:
[{
"resource": "/D:/Dev/CMake Template/external/engine/src/engine.cpp",
"owner": "cmake-build-diags",
"code": "C2061",
"severity": 8,
"message": "syntax error: identifier 'CustomWindow' [D:\\Dev\\CMake Template\\build\\external\\engine\\template_cpp_engine.vcxproj]",
"source": "MSVC",
"startLineNumber": 13,
"startColumn": 33,
"endLineNumber": 13,
"endColumn": 33
}]
[{
"resource": "/d:/Dev/CMake Template/external/engine/src/platform/glfw_window.h",
"owner": "cmake-build-diags",
"code": "C2236",
"severity": 8,
"message": "unexpected token 'class'. Did you forget a ';'? [D:\\Dev\\CMake Template\\build\\external\\engine\\template_cpp_engine.vcxproj]",
"source": "MSVC",
"startLineNumber": 6,
"startColumn": 1,
"endLineNumber": 6,
"endColumn": 1
}]
[{
"resource": "/d:/Dev/CMake Template/external/engine/src/platform/glfw_window.h",
"owner": "cmake-build-diags",
"code": "C2059",
"severity": 8,
"message": "syntax error: ':' [D:\\Dev\\CMake Template\\build\\external\\engine\\template_cpp_engine.vcxproj]",
"source": "MSVC",
"startLineNumber": 6,
"startColumn": 20,
"endLineNumber": 6,
"endColumn": 20
}]
[{
"resource": "/d:/Dev/CMake Template/external/engine/src/platform/glfw_window.h",
"owner": "cmake-build-diags",
"code": "C2059",
"severity": 8,
"message": "syntax error: 'public' [D:\\Dev\\CMake Template\\build\\external\\engine\\template_cpp_engine.vcxproj]",
"source": "MSVC",
"startLineNumber": 6,
"startColumn": 22,
"endLineNumber": 6,
"endColumn": 22
}]
[{
"resource": "/d:/Dev/CMake Template/external/engine/src/platform/glfw_window.h",
"owner": "cmake-build-diags",
"code": "C2143",
"severity": 8,
"message": "syntax error: missing ';' before '{' [D:\\Dev\\CMake Template\\build\\external\\engine\\template_cpp_engine.vcxproj]",
"source": "MSVC",
"startLineNumber": 7,
"startColumn": 1,
"endLineNumber": 7,
"endColumn": 1
}]
[{
"resource": "/d:/Dev/CMake Template/external/engine/src/platform/glfw_window.h",
"owner": "cmake-build-diags",
"code": "C2447",
"severity": 8,
"message": "'{': missing function header (old-style formal list?) [D:\\Dev\\CMake Template\\build\\external\\engine\\template_cpp_engine.vcxproj]",
"source": "MSVC",
"startLineNumber": 7,
"startColumn": 1,
"endLineNumber": 7,
"endColumn": 1
}]
I've already checked all the obvious problems, and was able to solve some problems in service_locator.h using Phind, but I can't figure out how to fix these errors, which all seem to be related. if it's any help, here's my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"-std=c++23",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
and my c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.22000.0",
"compilerPath": "cl.exe",
"cStandard": "c23",
"cppStandard": "c++23",
"intelliSenseMode": "windows-msvc-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}
Any help would be greatly appreciated