test push

This commit is contained in:
2025-04-17 21:37:38 +08:00
parent aa48bc82d8
commit 899c62bfeb
14 changed files with 379 additions and 29 deletions

View File

@ -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)

View File

@ -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 ()

View File

@ -3,6 +3,7 @@
// For use by Hazel Applications
#include "Hazel/Application.h"
#include "Hazel/Log.h"
#include "Hazel/Window.h"
// ------------------------ Entry Point ------------------------

View File

@ -1,16 +1,39 @@
#include "Application.h"
#include <SDL3/SDL.h>
#include "Log.h"
namespace Hazel {
Application::Application() = default;
Application::Application()
{
m_Window = std::unique_ptr<Window>(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;
}
}
}
}

View File

@ -1,4 +1,7 @@
#pragma once
#include <memory>
#include <Platform/Windows/WindowsWindow.h>
#include "Core.h"
namespace Hazel {
@ -11,7 +14,11 @@ namespace Hazel {
void Run();
void OnEvent(SDL_Event& e);
private:
std::unique_ptr<Hazel::Window> m_Window;
bool m_Running = true;
};
// to be defined int CLIENT

View File

@ -2,7 +2,6 @@
#ifdef HZ_PLATFORM_WINDOWS
#include <SDL3/SDL.h>
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

20
Hazel/src/Hazel/Layer.cpp Normal file
View File

@ -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()
{
}
}

35
Hazel/src/Hazel/Layer.h Normal file
View File

@ -0,0 +1,35 @@
//
// Created by sfd on 25-4-17.
//
#ifndef LAYER_H
#define LAYER_H
#include <string>
#include "Core.h"
#include <SDL3/SDL.h>
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

View File

@ -0,0 +1,5 @@
//
// Created by sfd on 25-4-17.
//
#include "LayerStack.h"

View File

@ -0,0 +1,37 @@
//
// Created by sfd on 25-4-17.
//
#ifndef LAYERSTACK_H
#define LAYERSTACK_H
#include <vector>
#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<Layer*>::iterator begin() {}
// std::vector<Layer*>::iterator end() {}
//
// private:
// std::vector<Layer*> m_layers;
// std::vector<Layer*>::iterator m_LayerInsert;
//
// };
}
#endif //LAYERSTACK_H

50
Hazel/src/Hazel/Window.h Normal file
View File

@ -0,0 +1,50 @@
//
// Created by sfd on 25-4-17.
//
#ifndef WINDOW_H
#define WINDOW_H
#include <functional>
#include <string>
#include <SDL3/SDL_events.h>
#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<void(SDL_Event&)>;
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

View File

@ -0,0 +1,110 @@
//
// Created by sfd on 25-4-17.
//
#include "WindowsWindow.h"
#include <Hazel/Log.h>
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;
}
}

View File

@ -0,0 +1,52 @@
//
// Created by sfd on 25-4-17.
//
#ifndef WINDOWSWINDOW_H
#define WINDOWSWINDOW_H
#include "Hazel/Window.h"
#include <SDL3/SDL.h>
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

View File

@ -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
```