diff --git a/CMakeLists.txt b/CMakeLists.txt index 88bc931..e49f4b8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,19 +1,22 @@ cmake_minimum_required(VERSION 3.10) -project(MyProject) + +project(Sandbox) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED ON) set(BUILD_SHARED_LIBS OFF) set(CMAKE_POSITION_INDEPENDENT_CODE ON) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251") -add_subdirectory(Hazel/vendor/spdlog) +if(MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4251") + add_compile_options(/utf-8) +endif () + set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin-int) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin-int) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) -add_compile_options(/utf-8) add_subdirectory(Hazel) diff --git a/Hazel/CMakeLists.txt b/Hazel/CMakeLists.txt index 4e3fd37..6a0d672 100644 --- a/Hazel/CMakeLists.txt +++ b/Hazel/CMakeLists.txt @@ -1,28 +1,32 @@ -project(Hazel) +set(PROJECT_NAME Hazel) +project(${PROJECT_NAME}) file(GLOB_RECURSE SOURCES "src/*.cpp") add_library(Hazel SHARED ${SOURCES}) + +add_subdirectory(vendor/spdlog) + + set(BUILD_SHARED_LIBS ON CACHE BOOL "Build shared libraries" FORCE) add_subdirectory(vendor/SDL) set(SDL3_SHARED ON CACHE BOOL "Build SDL as shared library" FORCE) -target_include_directories(Hazel +target_include_directories(${PROJECT_NAME} PUBLIC - vendor/spdlog/include - vendor/SDL/include ${CMAKE_CURRENT_SOURCE_DIR}/src # 暴露头文件给其他项目 ) -target_link_libraries(Hazel PUBLIC spdlog::spdlog SDL3::SDL3) +target_link_libraries(${PROJECT_NAME} PUBLIC spdlog::spdlog SDL3::SDL3) -target_compile_definitions(Hazel PRIVATE HZ_BUILD_DLL)# 编译DLL时定义 +target_compile_definitions(${PROJECT_NAME} PRIVATE HZ_BUILD_DLL)# 编译DLL时定义 if(WIN32) - target_compile_definitions(Hazel PUBLIC HZ_PLATFORM_WINDOWS)# 编译DLL时定义 + target_compile_definitions(${PROJECT_NAME} PUBLIC HZ_PLATFORM_WINDOWS)# 编译DLL时定义 + set(CMAKE_SHARED_LIBRARY_PREFIX "") endif () diff --git a/Hazel/src/Hazel.h b/Hazel/src/Hazel.h index 7fbf837..258b68b 100644 --- a/Hazel/src/Hazel.h +++ b/Hazel/src/Hazel.h @@ -3,6 +3,7 @@ // For use by Hazel Applications #include "Hazel/Application.h" #include "Hazel/Log.h" +#include "Hazel/Window.h" // ------------------------ Entry Point ------------------------ diff --git a/Hazel/src/Hazel/Application.cpp b/Hazel/src/Hazel/Application.cpp index 73a6cb5..0109743 100644 --- a/Hazel/src/Hazel/Application.cpp +++ b/Hazel/src/Hazel/Application.cpp @@ -1,16 +1,39 @@ #include "Application.h" +#include + +#include "Log.h" + namespace Hazel { - Application::Application() = default; + Application::Application() + { + m_Window = std::unique_ptr(Window::Create()); + m_Window->SetEventCallback(std::bind(&Application::OnEvent, this, std::placeholders::_1)); + } Application::~Application() = default; void Application::Run() { - while (true) + while (m_Running) { + m_Window->OnUpdate(); } } + void Application::OnEvent(SDL_Event& e) + { + while (SDL_PollEvent(&e)) + { + switch (e.type) + { + case SDL_EVENT_QUIT: + m_Running = false; + break; + default: + break; + } + } + } } diff --git a/Hazel/src/Hazel/Application.h b/Hazel/src/Hazel/Application.h index d167de7..ebc9ce4 100644 --- a/Hazel/src/Hazel/Application.h +++ b/Hazel/src/Hazel/Application.h @@ -1,4 +1,7 @@ #pragma once +#include +#include + #include "Core.h" namespace Hazel { @@ -11,7 +14,11 @@ namespace Hazel { void Run(); + void OnEvent(SDL_Event& e); + private: + std::unique_ptr m_Window; + bool m_Running = true; }; // to be defined int CLIENT diff --git a/Hazel/src/Hazel/EntryPoint.h b/Hazel/src/Hazel/EntryPoint.h index 1ec3a50..69f7646 100644 --- a/Hazel/src/Hazel/EntryPoint.h +++ b/Hazel/src/Hazel/EntryPoint.h @@ -2,7 +2,6 @@ #ifdef HZ_PLATFORM_WINDOWS -#include extern Hazel::Application* Hazel::CreateApplication(); @@ -23,23 +22,9 @@ int main(int argc, char* argv[]) { HZ_CLIENT_ERROR("hello"); HZ_CLIENT_FATAL("hello"); - SDL_Init(SDL_INIT_VIDEO); - SDL_Window* window = SDL_CreateWindow("demo", 800, 600, 0); - - bool should_quit = false; - - while (!should_quit) { - SDL_Event event; - while (SDL_PollEvent(&event)) { - if (event.type == SDL_EVENT_QUIT) { - should_quit = true; - } - } - } - auto app = Hazel::CreateApplication(); -// app->Run(); + app->Run(); } #endif // HZ_PLATFORM_WINDOWS diff --git a/Hazel/src/Hazel/Layer.cpp b/Hazel/src/Hazel/Layer.cpp new file mode 100644 index 0000000..e926a5d --- /dev/null +++ b/Hazel/src/Hazel/Layer.cpp @@ -0,0 +1,20 @@ +// +// Created by sfd on 25-4-17. +// + +#include "Layer.h" + + +namespace Hazel +{ + Layer::Layer(const std::string& debugName) : m_DebugName(debugName) + { + + } + + Layer::~Layer() + { + + } + +} \ No newline at end of file diff --git a/Hazel/src/Hazel/Layer.h b/Hazel/src/Hazel/Layer.h new file mode 100644 index 0000000..d4c8be9 --- /dev/null +++ b/Hazel/src/Hazel/Layer.h @@ -0,0 +1,35 @@ +// +// Created by sfd on 25-4-17. +// + +#ifndef LAYER_H +#define LAYER_H + +#include + +#include "Core.h" +#include + +namespace Hazel { + + class HAZEL_API Layer + { + public: + Layer(const std::string& debugName = "Layer"); + virtual ~Layer(); + + virtual void OnAttach() {} + virtual void OnDetech() {} + virtual void OnUpdate() {} + virtual void OnEvent(SDL_Event& event) {} + + inline const std::string& GetName() const { return m_DebugName; } + + protected: + std::string m_DebugName; + }; + +} + + +#endif //LAYER_H diff --git a/Hazel/src/Hazel/LayerStack.cpp b/Hazel/src/Hazel/LayerStack.cpp new file mode 100644 index 0000000..c9b1fc3 --- /dev/null +++ b/Hazel/src/Hazel/LayerStack.cpp @@ -0,0 +1,5 @@ +// +// Created by sfd on 25-4-17. +// + +#include "LayerStack.h" diff --git a/Hazel/src/Hazel/LayerStack.h b/Hazel/src/Hazel/LayerStack.h new file mode 100644 index 0000000..b1c20e4 --- /dev/null +++ b/Hazel/src/Hazel/LayerStack.h @@ -0,0 +1,37 @@ +// +// Created by sfd on 25-4-17. +// + +#ifndef LAYERSTACK_H +#define LAYERSTACK_H + +#include + +#include "Core.h" +#include "Layer.h" + +namespace Hazel +{ + // class HAZEL_API LayerStack + // { + // public: + // LayerStack(); + // ~LayerStack(); + // + // void PushLayer(Layer* layer); + // void PushOverlay(Layer* layer); + // void PopLayer(Layer* layer); + // void PopOverlay(Layer* layer); + // + // std::vector::iterator begin() {} + // std::vector::iterator end() {} + // + // private: + // std::vector m_layers; + // std::vector::iterator m_LayerInsert; + // + // }; +} + + +#endif //LAYERSTACK_H diff --git a/Hazel/src/Hazel/Window.h b/Hazel/src/Hazel/Window.h new file mode 100644 index 0000000..e34d9a2 --- /dev/null +++ b/Hazel/src/Hazel/Window.h @@ -0,0 +1,50 @@ +// +// Created by sfd on 25-4-17. +// + +#ifndef WINDOW_H +#define WINDOW_H +#include +#include +#include + +#include "Core.h" + +namespace Hazel +{ + struct WindowProps + { + std::string title; + unsigned int width; + unsigned int height; + + WindowProps(const std::string& title = "Hazel", + unsigned int width = 1280, + unsigned int height = 720) : title(title), width(width), height(height) {} + + }; + + class HAZEL_API Window + { + public: + using EventCallbackFn = std::function; + + virtual ~Window() = default; + + virtual void OnUpdate() = 0; + + virtual unsigned int GetWidth() const = 0; + virtual unsigned int GetHeight() const = 0; + + // Window attributes + virtual void SetEventCallback(const EventCallbackFn& callback) = 0; + virtual void SetVSync(bool enabled) = 0; + virtual bool IsVSync() const = 0; + + static Window* Create(const WindowProps& props = WindowProps()); + + }; + +} + +#endif //WINDOW_H diff --git a/Hazel/src/Platform/Windows/WindowsWindow.cpp b/Hazel/src/Platform/Windows/WindowsWindow.cpp new file mode 100644 index 0000000..c400c6a --- /dev/null +++ b/Hazel/src/Platform/Windows/WindowsWindow.cpp @@ -0,0 +1,110 @@ +// +// Created by sfd on 25-4-17. +// + +#include "WindowsWindow.h" + +#include + + +namespace Hazel +{ + static bool s_SDLInitialized = false; + + WindowsWindow::WindowsWindow(const WindowProps& props) + { + Init(props); + } + + Window* Window::Create(const WindowProps& props) { + return new WindowsWindow(props); + } + + + WindowsWindow::~WindowsWindow() + { + Shutdown(); + } + + void WindowsWindow::Init(const WindowProps& props) + { + m_Data.title = props.title; + m_Data.width = props.width; + m_Data.height = props.height; + + HZ_CORE_INFO("Creating window {0} ( {1}, {2})", props.title, props.width, props.height); + + if (!s_SDLInitialized) + { + if (!SDL_Init(SDL_INIT_VIDEO)) + { + HZ_CORE_ERROR("Could not initialize SDL: {}", SDL_GetError()); + exit(EXIT_FAILURE); + } + + + s_SDLInitialized = true; + } + + m_Window = SDL_CreateWindow(m_Data.title.c_str(), props.width, props.height, 0); + + HZ_CORE_INFO("Creating Renderer..."); + m_Renderer = SDL_CreateRenderer(m_Window, nullptr); + if (m_Renderer == nullptr) + { + HZ_CORE_ERROR("Could not create renderer: {}", SDL_GetError()); + exit(EXIT_FAILURE); + } + + + SetVSync(true); + } + + void WindowsWindow::Shutdown() + { + SDL_DestroyRenderer(m_Renderer); + SDL_DestroyWindow(m_Window); + SDL_Quit(); + } + + void WindowsWindow::OnUpdate() + { + SDL_Event event; + if (m_Data.eventCallback != nullptr) + { + m_Data.eventCallback(event); + } + + while (SDL_PollEvent(&event)) + { + if (event.type == SDL_EVENT_QUIT) + { + //TODO: quit this window + } + } + + SDL_SetRenderDrawColor(m_Renderer, 0, 0, 0, 0xFF); + SDL_RenderClear(m_Renderer); + SDL_RenderPresent(m_Renderer); + } + + void WindowsWindow::SetVSync(bool enabled) + { + if (enabled) + { + SDL_GL_SetSwapInterval(enabled); + } + else + { + SDL_GL_SetSwapInterval(false); + } + + m_Data.vSync = enabled; + } + + bool WindowsWindow::IsVSync() const + { + return m_Data.vSync; + } + +} diff --git a/Hazel/src/Platform/Windows/WindowsWindow.h b/Hazel/src/Platform/Windows/WindowsWindow.h new file mode 100644 index 0000000..06dda5f --- /dev/null +++ b/Hazel/src/Platform/Windows/WindowsWindow.h @@ -0,0 +1,52 @@ +// +// Created by sfd on 25-4-17. +// + +#ifndef WINDOWSWINDOW_H +#define WINDOWSWINDOW_H + +#include "Hazel/Window.h" + +#include + +namespace Hazel +{ + class WindowsWindow : public Window + { + public: + WindowsWindow(const WindowProps& props); + virtual ~WindowsWindow(); + + void OnUpdate() override; + + inline unsigned int GetWidth() const override { return m_Data.width; } + inline unsigned int GetHeight() const override { return m_Data.height; } + + + // Window attributes + inline void SetEventCallback(const EventCallbackFn& callback) override { m_Data.eventCallback = callback; } + void SetVSync(bool enabled) override; + bool IsVSync() const override; + + private: + virtual void Init(const WindowProps& props); + virtual void Shutdown(); + + private: + SDL_Window* m_Window; + SDL_Renderer* m_Renderer; + + struct WindowData + { + std::string title; + unsigned int width; + unsigned int height; + bool vSync; + EventCallbackFn eventCallback; + }; + + WindowData m_Data; + }; +} + +#endif //WINDOWSWINDOW_H diff --git a/README.md b/README.md index 8039826..9e8787d 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,20 @@ # Hazel +## To clone this repository +> ```bash +> git clone --recurse-submodule http://116.62.144.93:3000/atdunbg/Hazel.git # recommend +> ``` +> or +> ```bash +> git clone http://116.62.144.93:3000/atdunbg/Hazel.git +> git submodule init +> git submodule update +> ``` + + +## build + +```bash +cmake -B build +cmake --build build --config Release -j18 +```