Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ file(GLOB_RECURSE APP_SOURCES CONFIGURE_DEPENDS
add_executable(CMaterial
src/main.cpp
src/main.h

src/gl.c

include/imgui/imgui.cpp
include/imgui/imgui_draw.cpp
include/imgui/imgui_tables.cpp
Expand Down Expand Up @@ -95,6 +97,7 @@ if(MSVC AND CMAKE_BUILD_TYPE MATCHES "Release")
)

set_target_properties(CMaterial PROPERTIES LINK_FLAGS "/INCLUDE:malloc")
target_sources(CMaterial PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/version.rc)

elseif((CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang") AND CMAKE_BUILD_TYPE MATCHES "Release")
target_compile_options(CMaterial PRIVATE
Expand Down
6 changes: 6 additions & 0 deletions src/content/window/BasicWindow/BasicWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ namespace cmaterial::window {
void BasicWindow::render(ImGuiIO *io) {
ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings;

ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 10.0f);
ImGui::PushStyleVar(ImGuiStyleVar_WindowBorderSize, 0.0f);
ImGui::PushStyleVar(ImGuiStyleVar_FrameBorderSize, 1.0f);
if (ImGui::Begin(this->name.c_str(), nullptr, flags)) {
ImGui::Text("Hello, Engine!");
ImGui::Text("Frame time: %.3f ms/frame (%.1f FPS)", 1000.0f / io->Framerate, io->Framerate);
Expand All @@ -39,5 +42,8 @@ namespace cmaterial::window {
}

ImGui::End();
ImGui::PopStyleVar();
ImGui::PopStyleVar();
ImGui::PopStyleVar();
}
}
1 change: 0 additions & 1 deletion src/content/window/BasicWindow/BasicWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#ifndef CMATERIAL_BASICWINDOW_H
#define CMATERIAL_BASICWINDOW_H

#include "engine/component/IComponent.h"
#include "engine/window/IWindow.h"
#include "imgui.h"

Expand Down
32 changes: 32 additions & 0 deletions src/engine/animation/IAnimation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Created by Earth_Studio on 2026/4/26.
//

#include "IAnimation.h"

#include <unordered_map>

#include "tween.h"

namespace cmaterial::animation {
void IAnimation::reset() {
for (auto binder : animationBinders) {
if (_isReverse)
binder.second->seek(1);
else
binder.second->seek(0);
}
}

std::unordered_map<double *, tweeny::tween<double> *>* IAnimation::getAnimationBinders() {
return &animationBinders;
}

void IAnimation::bind(double* property, tweeny::tween<double>* tween) {
animationBinders[property] = tween;
}

IAnimation::~IAnimation() {
animationBinders.clear();
}
}
34 changes: 34 additions & 0 deletions src/engine/animation/IAnimation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Created by Earth_Studio on 2026/4/26.
//

#ifndef CMATERIAL_IANIMATION_H
#define CMATERIAL_IANIMATION_H


#include <unordered_map>

#include "tween.h"


namespace cmaterial::animation {
class IAnimation {
public:
virtual ~IAnimation();

void reset();
std::unordered_map<double *, tweeny::tween<double> *>* getAnimationBinders();

bool isLoop = false;
bool _isReverse = false;

private:
void bind(double* property, tweeny::tween<double>* tween);

std::unordered_map<double *, tweeny::tween<double> *> animationBinders;
};
}



#endif //CMATERIAL_IANIMATION_H
82 changes: 82 additions & 0 deletions src/engine/animation/Player.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// Created by Earth_Studio on 2026/4/22.
//

#include "Player.h"

#include <vector>
#include <imgui.h>

namespace cmaterial::animation {
std::vector<IAnimation *> Player::playingAnimations;
std::vector<IAnimation *> Player::finishedAnimations;

void Player::play(IAnimation *animation) {
if (!animation) return;

playingAnimations.push_back(animation);
}

void Player::reverse(IAnimation *animation) {
if (!animation) return;

for (auto binder : *animation->getAnimationBinders()) {
if (!animation->_isReverse)
binder.second->backward();
else
binder.second->forward();
}

animation->_isReverse = !animation->_isReverse;
}

void Player::stop(IAnimation *animation) {
if (!animation) return;

animation->reset();
std::erase(playingAnimations, animation);
}

bool Player::update() {
if (playingAnimations.empty())
return false;

int dtTime = static_cast<int>(ImGui::GetIO().DeltaTime * 100000.0f);
if (dtTime <= 0) dtTime = 1; // 1 step = 0.01 ms

for (auto animation : playingAnimations) {
bool allTweensFinished = true;
for (auto binder : *animation->getAnimationBinders()) {
*binder.first = binder.second->step(dtTime);

if (!binder.second->isFinished()) {
allTweensFinished = false;
}
}

if (allTweensFinished) {
if (animation->isLoop) {
animation->reset();
} else
finishedAnimations.push_back(animation);
}
}

for (auto animation : finishedAnimations) {
std::erase(playingAnimations, animation);
}

finishedAnimations.clear();

return true;
}

void Player::shutdown() {
playingAnimations.clear();
finishedAnimations.clear();
}

Player::~Player() {
shutdown();
}
}
36 changes: 36 additions & 0 deletions src/engine/animation/Player.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Created by Earth_Studio on 2026/4/22.
//

#ifndef CMATERIAL_ANIMATION_H
#define CMATERIAL_ANIMATION_H


#include <vector>

#include "IAnimation.h"


namespace cmaterial::animation {
class Player{
public:
Player() = default;
~Player();

static void play(IAnimation* animation);
static void reverse(IAnimation* animation);
static void stop(IAnimation* animation);

static bool update();

static void shutdown();

private:
static std::vector<IAnimation *> playingAnimations;
static std::vector<IAnimation *> finishedAnimations;
};
}



#endif //CMATERIAL_ANIMATION_H
3 changes: 2 additions & 1 deletion src/engine/component/IComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "GLFW/glfw3.h"
#include "engine/eventbus/EventBus.h"
#include "engine/eventbus/internal/event/EmptyEvent.hpp"

namespace cmaterial::component {
void IComponent::addComponent(IComponent *component) {
Expand All @@ -16,7 +17,7 @@ namespace cmaterial::component {
return;

components.insert({component->name, component});
glfwPostEmptyEvent();
event::EventBus::postEvent(new event::internal::EmptyEvent);
}

bool IComponent::getIsDead() {
Expand Down
6 changes: 6 additions & 0 deletions src/engine/component/IComponent.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
#include "imgui.h"

#include <unordered_map>
#include <vector>
#include <string>

#include "ILayer.h"


namespace cmaterial::component {
class IComponent {
Expand All @@ -20,6 +23,8 @@ namespace cmaterial::component {
virtual void render(ImGuiIO *io) = 0;
virtual void addComponent(IComponent *component);

void addLayer();

bool getIsDead();

std::string name;
Expand All @@ -30,6 +35,7 @@ namespace cmaterial::component {
protected:
bool isDead = false;
std::unordered_map<std::string, IComponent *> components;
std::vector<ILayer *> layers;
};
}

Expand Down
9 changes: 9 additions & 0 deletions src/engine/component/ILayer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//
// Created by Earth_Studio on 2026/4/26.
//

#include "ILayer.h"

namespace cmaterial::component {

}
25 changes: 25 additions & 0 deletions src/engine/component/ILayer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//
// Created by Earth_Studio on 2026/4/26.
//

#ifndef CMATERIAL_ILAYER_H
#define CMATERIAL_ILAYER_H


#include "imgui.h"


namespace cmaterial::component {
class ILayer {
public:
virtual ~ILayer() = default;

virtual ImDrawData render() = 0;

int priority = 0;
};
}



#endif //CMATERIAL_ILAYER_H
21 changes: 14 additions & 7 deletions src/engine/eventbus/EventBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,21 @@ namespace cmaterial::event {
processingQueue.swap(eventQueue);
}

std::lock_guard<std::mutex> lock(registerMutex);

for (auto* event : processingQueue) {
const void* type = event->getEventType();

std::vector<IHandler*> handlersToCall;

std::lock_guard<std::mutex> lock(registerMutex);
if (subscriberMap.contains(type)) {
for (auto* handler : subscriberMap[type]) {
// 支持取消逻辑
if (event->getIsCancelled()) break;
handler->execute(event);
}
handlersToCall = subscriberMap[type];
}

for (auto* handler : handlersToCall) {
if (event->getIsCancelled()) break;
handler->execute(event);
}

delete event;
}

Expand Down Expand Up @@ -98,9 +102,12 @@ namespace cmaterial::event {
for (auto* e : eventQueue) delete e;
eventQueue.clear();

subscriberMap.clear();

for (IListener* listener : registeredListeners) {
delete listener;
}
registeredListeners.clear();
}

EventBus::~EventBus() {
Expand Down
Loading
Loading