From 194b40d1ee021bc5e561c7259ffa21d394395e48 Mon Sep 17 00:00:00 2001 From: Furkan Mudanyali Date: Sat, 27 May 2023 17:44:20 +0300 Subject: [PATCH] Initial commit --- .clang-format | 8 +++ .clangd | 18 +++++ .gitignore | 3 + .vscode/c_cpp_properties.json | 16 +++++ .vscode/extensions.json | 9 +++ .vscode/launch.json | 14 ++++ .vscode/settings.json | 31 +++++++++ .vscode/tasks.json | 40 +++++++++++ CMakeLists.txt | 127 ++++++++++++++++++++++++++++++++++ debug.entitlements | 8 +++ src/game/game.cpp | 26 +++++++ src/game/game.h | 29 ++++++++ src/input/input.cpp | 24 +++++++ src/input/input.h | 24 +++++++ src/main.cpp | 16 +++++ src/scene/scene.cpp | 6 ++ src/scene/scene.h | 16 +++++ src/window/window.cpp | 34 +++++++++ src/window/window.h | 37 ++++++++++ 19 files changed, 486 insertions(+) create mode 100644 .clang-format create mode 100644 .clangd create mode 100644 .gitignore create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/extensions.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 CMakeLists.txt create mode 100644 debug.entitlements create mode 100644 src/game/game.cpp create mode 100644 src/game/game.h create mode 100644 src/input/input.cpp create mode 100644 src/input/input.h create mode 100644 src/main.cpp create mode 100644 src/scene/scene.cpp create mode 100644 src/scene/scene.h create mode 100644 src/window/window.cpp create mode 100644 src/window/window.h diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..b53a3a4 --- /dev/null +++ b/.clang-format @@ -0,0 +1,8 @@ +BasedOnStyle: Chromium +IndentWidth: 4 +SortIncludes: false + +Language: Cpp +# Force pointers to the type for C++. +DerivePointerAlignment: false +PointerAlignment: Left \ No newline at end of file diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..f4c3933 --- /dev/null +++ b/.clangd @@ -0,0 +1,18 @@ +Diagnostics: + ClangTidy: + Add: [ + modernize*, + bugprone*, + performance*, + readability* + ] + Remove: [ + modernize-use-trailing-return-type, + modernize-avoid-c-arrays, + readability-magic-numbers, + readability-implicit-bool-conversion, + readability-identifier-length + ] + +Completion: + AllScopes: Yes \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1963447 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/build +/.cache +**/.DS_Store \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..6e97d9c --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,16 @@ +{ + "configurations": [ + { + "name": "Mac", + "includePath": ["${workspaceFolder}/**", "/opt/homebrew/include"], + "defines": [], + "macFrameworkPath": [ + "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "c17", + "cppStandard": "c++17" + } + ], + "version": 4 +} diff --git a/.vscode/extensions.json b/.vscode/extensions.json new file mode 100644 index 0000000..e85b6be --- /dev/null +++ b/.vscode/extensions.json @@ -0,0 +1,9 @@ +{ + "recommendations": [ + "llvm-vs-code-extensions.vscode-clangd", + "ms-vscode.cpptools-extension-pack", + "vadimcn.vscode-lldb", + "fabiospampinato.vscode-statusbar-debugger", + "runarsf.platform-settings" + ] +} diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..07333af --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Homd Engine Debug", + "preLaunchTask": "CMake: build", + "program": "${workspaceFolder}/build/HomdEngine", + "args": [], + "cwd": "${workspaceFolder}/build" + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..19a12f7 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,31 @@ +{ + "C_Cpp.intelliSenseEngine": "disabled", + "cmake.statusbar.advanced": { + "debug": { + "visibility": "hidden" + }, + "buildTarget": { + "visibility": "hidden" + }, + "launchTarget": { + "visibility": "hidden" + }, + "kit": { + "visibility": "hidden" + } + }, + "clangd.arguments": ["--compile-commands-dir=build"], + "editor.formatOnSave": true, + "platformSettings.autoLoad": true, + "search.exclude": { + "**/build": true + }, + "platformSettings.platforms": { + "win32": { + "nodes": { + "cmake.cmakePath": "C:\\msys64\\clang64\\bin\\cmake", + "clangd.path": "C:\\msys64\\clang64\\bin\\clangd.exe" + } + } + } +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..820c0d8 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,40 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "type": "cmake", + "label": "CMake: configure", + "command": "configure", + "problemMatcher": [], + "detail": "CMake template configure task", + "presentation": { + "echo": true, + "reveal": "always", + "focus": false, + "panel": "shared", + "showReuseMessage": true, + "clear": true + } + }, + { + "type": "cmake", + "label": "CMake: build", + "command": "build", + "targets": ["all"], + "group": "build", + "problemMatcher": [], + "detail": "CMake template build task", + "dependsOn": ["CMake: configure"], + "presentation": { + "echo": true, + "reveal": "always", + "focus": false, + "panel": "shared", + "showReuseMessage": true, + "clear": true + } + } + ] +} diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..fbd1344 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,127 @@ +# Copyright (c) 2023, Furkan Mudanyali +# All rights reserved + +cmake_minimum_required(VERSION 3.13) +project(HomdEngine) + +SET(CMAKE_CXX_STANDARD 17) +SET(CMAKE_EXPORT_COMPILE_COMMANDS 1) +# Change this if youre using other environments than MSYS2 CLANG64 +# or it is in a different path. +SET(win_environment "C:/msys64/clang64") + +# Set compiler to clang++ +IF(WIN32) + SET(CMAKE_CXX_COMPILER "${win_environment}/bin/clang++") +ELSE() + SET(CMAKE_CXX_COMPILER "/usr/bin/clang++") +ENDIF() + +# Apple M1 does not support -march=native in clang version 14 and before. +IF(APPLE AND CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64") + SET(arch_flags "-mcpu=apple-m1 -mtune=native") +ELSE() + SET(arch_flags "-march=native -mtune=native") +ENDIF() + +# Whether to optimize or leave debugging information depending on +# build type +IF(CMAKE_BUILD_TYPE STREQUAL "Debug") + SET(LINK_FLAGS "${debug_linker}") + SET(optimize_flags "-Og -g3 -ffunction-sections -fdata-sections") +ELSE() + SET(optimize_flags "-O3 -ffast-math") +ENDIF() + +# Needed for ImGui under Windows, also have to rename main() to wmain() +# in main.cpp +IF(WIN32) + SET(win_flags "-municode") +ENDIF() + +# Dead code stripping in the linker part +SET(debug_linker "-Wl,-dead_strip") + +# Enable all kinds of warnings and treat them as errors for a better code +# Some warnings are disabled to prevent making changes on dependencies. +SET(warn_flags "\ + -Wall\ + -fno-common\ + -Wextra\ + -Werror\ + -Wshadow\ + -Wformat=2\ + -Wundef\ + -Wno-unused-parameter\ + -Wno-sign-conversion\ + -Wno-conversion\ + -Wno-format-nonliteral\ +") + +# Put all the flags we have gathered +SET(CMAKE_CXX_FLAGS + "${arch_flags} ${optimize_flags} ${warn_flags} ${win_flags}" +) + +# The main project files +SET(SOURCE_FILES + src/game/game.cpp + + src/input/input.cpp + + src/scene/scene.cpp + + src/window/window.cpp + + src/main.cpp +) + +# /SDL2 and /opencv4 are for using same imports +# under all environments. +IF(APPLE) + SET(includes + # M1 Mac with Homebrew + "/opt/homebrew/include" + # Intel Mac with Homebrew / Macports + "/opt/local/include" + ) + SET(links + # M1 Mac with Homebrew + "/opt/homebrew/lib" + # Intel Mac with Homebrew / Macports + "/opt/local/lib" + ) +ENDIF() + +INCLUDE_DIRECTORIES( + "${CMAKE_CURRENT_SOURCE_DIR}/src" + ${includes} +) + +LINK_DIRECTORIES( + ${links} +) + +ADD_EXECUTABLE(HomdEngine + ${SOURCE_FILES} +) + +IF(WIN32) + SET(win32_link -mwindows, -mconsole) +ENDIF() + +TARGET_LINK_LIBRARIES(HomdEngine + sdl2 + ${win32_link} +) + +# To profile the debug build of the application using Instruments +# under macOS, we need to replace its signature to allow profiling. +IF(APPLE AND CMAKE_BUILD_TYPE STREQUAL "Debug") + ADD_CUSTOM_COMMAND( + TARGET GlandMiner POST_BUILD + COMMAND /usr/bin/codesign -s - + --entitlements ${CMAKE_SOURCE_DIR}/debug.entitlements + -f ${CMAKE_CURRENT_BINARY_DIR}/HomdEngine + ) +ENDIF() \ No newline at end of file diff --git a/debug.entitlements b/debug.entitlements new file mode 100644 index 0000000..3842541 --- /dev/null +++ b/debug.entitlements @@ -0,0 +1,8 @@ + + + + + com.apple.security.get-task-allow + + + diff --git a/src/game/game.cpp b/src/game/game.cpp new file mode 100644 index 0000000..b5907c6 --- /dev/null +++ b/src/game/game.cpp @@ -0,0 +1,26 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#include +#include + +Game::Game() { + this->pWindow = new Window(this); + this->pInput = new Input(this); + this->scenes.push(new Scene); +} + +void Game::loop() { + while (!this->done && !this->scenes.empty()) { + this->pWindow->updateDimensions(); + this->done = this->pInput->pollEvent(); + + if (this->scenes.top()->destroy) { + delete this->scenes.top(); + this->scenes.pop(); + continue; + } + } +} \ No newline at end of file diff --git a/src/game/game.h b/src/game/game.h new file mode 100644 index 0000000..df8055a --- /dev/null +++ b/src/game/game.h @@ -0,0 +1,29 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#ifndef _HOMD_GAME +#define _HOMD_GAME + +#include +#include +#include + +class Scene; + +class Game { + bool done = false; + std::stack scenes; + + public: + Window* pWindow; + Input* pInput; + + Game(); + ~Game() = default; + + void loop(); +}; + +#endif \ No newline at end of file diff --git a/src/input/input.cpp b/src/input/input.cpp new file mode 100644 index 0000000..b6b02a0 --- /dev/null +++ b/src/input/input.cpp @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#include +#include + +Input::Input(Game* pGame) { + this->game = pGame; +} + +bool Input::pollEvent() { + while (SDL_PollEvent(&this->event)) { + switch (event.type) { + case SDL_QUIT: + return true; + } + } + return event.type == SDL_WINDOWEVENT && + event.window.event == SDL_WINDOWEVENT_CLOSE && + event.window.windowID == + SDL_GetWindowID(this->game->pWindow->window); +} \ No newline at end of file diff --git a/src/input/input.h b/src/input/input.h new file mode 100644 index 0000000..eb6377c --- /dev/null +++ b/src/input/input.h @@ -0,0 +1,24 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#ifndef _HOMD_INPUT +#define _HOMD_INPUT + +#include +#include + +class Game; + +class Input { + Game* game = nullptr; + SDL_Event event; + + public: + Input(Game*); + ~Input() = default; + bool pollEvent(); +}; + +#endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..b7e0d8b --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#include + +#ifdef __WIN32 +int wmain(int argc, char** argv) { +#else +int main(int argc, char** argv) { +#endif + auto* game = new Game; + game->loop(); + return 0; +} \ No newline at end of file diff --git a/src/scene/scene.cpp b/src/scene/scene.cpp new file mode 100644 index 0000000..6eef621 --- /dev/null +++ b/src/scene/scene.cpp @@ -0,0 +1,6 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#include \ No newline at end of file diff --git a/src/scene/scene.h b/src/scene/scene.h new file mode 100644 index 0000000..e00a7bf --- /dev/null +++ b/src/scene/scene.h @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#ifndef _HOMD_SCENE +#define _HOMD_SCENE + +class Scene { + public: + bool destroy = false; + Scene() = default; + ~Scene() = default; +}; + +#endif \ No newline at end of file diff --git a/src/window/window.cpp b/src/window/window.cpp new file mode 100644 index 0000000..d086a74 --- /dev/null +++ b/src/window/window.cpp @@ -0,0 +1,34 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#include + +Window::Window(Game* pGame) { + this->game = pGame; + if (SDL_Init(SDL_FLAGS) != 0) { + throw SDL_GetError(); + } + + this->window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, WINDOW_INITIAL_W, + WINDOW_INITIAL_H, SDL_WINDOW_FLAGS); +} + +Window::~Window() { + SDL_DestroyWindow(this->window); + SDL_Quit(); +} + +void Window::updateDimensions() { + SDL_GL_GetDrawableSize(this->window, &this->width, &this->height); +} + +int Window::getWidth() const { + return this->width; +} + +int Window::getHeight() const { + return this->height; +} \ No newline at end of file diff --git a/src/window/window.h b/src/window/window.h new file mode 100644 index 0000000..e2b110c --- /dev/null +++ b/src/window/window.h @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2023, Furkan Mudanyali + * All rights reserved + */ + +#ifndef _HOMD_WINDOW +#define _HOMD_WINDOW + +#include +#include + +#define SDL_FLAGS SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER +#define SDL_WINDOW_FLAGS \ + SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_MAXIMIZED + +#define WINDOW_TITLE "Homd Engine" +#define WINDOW_INITIAL_W 960 +#define WINDOW_INITIAL_H 540 + +class Game; + +class Window { + Game* game; + int width; + int height; + + public: + SDL_Window* window; + + Window(Game*); + ~Window(); + void updateDimensions(); + [[nodiscard]] int getWidth() const; + [[nodiscard]] int getHeight() const; +}; + +#endif \ No newline at end of file