-2

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

273K
  • 29,503
  • 10
  • 41
  • 64
RomektBoi
  • 21
  • 2
  • I am curious, have you tried to read the error messages? The answer is there! – 273K Apr 30 '23 at 01:24
  • what do you think `target_include_directories(${PROJECT_NAME} PRIVATE template_cpp_engine)` does? – starball Apr 30 '23 at 01:28
  • @273k I understand that you're trying to help, but if you could tell me what the problem is and where in the error message it says that, it would be a lot more helpful. I already found the answer, but this vague answer did not help. – RomektBoi Apr 30 '23 at 03:34
  • @user I really don't know what it does, I just copied it from the tutorial. I agree that it doesn't look right, but it's not what's causing my errors. – RomektBoi Apr 30 '23 at 03:35
  • Don't study C++ with video tutorials of unknown authors with unmkonwn skills. You waste you time. Invest it in one of the books in [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Reading errors is one of must have skills of successful developers. Try to read them carefully. The hint: the answer is in the second JSON entry. Just trust me, if you fix the error yourself, you get more profit than you get a ready fix. – 273K Apr 30 '23 at 03:45

1 Answers1

0

Your ServiceLocator class definition is missing a semicolon after its closing curly brace. I'm guessing that's what 273K was referencing? The second error message says that the compiler didn't expect to see the class keyword for your CustomWindow class and that a semicolon is probably missing. I'm guessing if we could see a full trace of what source file is being compiled when that error is found, it would be in the compilation of engine.cpp, where you #include <template_engine/service_locator.h> and then #include "platform/glfw_window.h" right after.

starball
  • 20,030
  • 7
  • 43
  • 238