Initial engine repository

This commit is contained in:
2026-06-08 19:58:35 +08:00
commit 7a925c3736
137 changed files with 162421 additions and 0 deletions
@@ -0,0 +1,63 @@
#pragma once
#include <frostbite2D/types/type_alias.h>
namespace frostbite2D {
class Actor;
enum class EventType : int32 {
None = 0,
WindowClose,
WindowResize,
WindowFocus,
WindowMinimize,
WindowMaximize,
WindowRestore,
KeyDown,
KeyUp,
KeyText,
MouseMove,
MouseButtonDown,
MouseButtonUp,
MouseWheel,
TouchDown,
TouchUp,
TouchMove,
JoystickAxis,
JoystickBall,
JoystickHat,
JoystickButtonDown,
JoystickButtonUp,
Custom
};
class Event {
public:
virtual ~Event() = default;
virtual EventType getType() const = 0;
bool isHandled() const { return handled_; }
void setHandled(bool handled) { handled_ = handled; }
bool isPropagationStopped() const { return propagationStopped_; }
void stopPropagation() { propagationStopped_ = true; }
Actor* getSource() const { return source_; }
void setSource(Actor* actor) { source_ = actor; }
uint32 getTimestamp() const { return timestamp_; }
protected:
Event() = default;
Event(uint32 timestamp)
: timestamp_(timestamp) {}
bool handled_ = false;
bool propagationStopped_ = false;
Actor* source_ = nullptr;
uint32 timestamp_ = 0;
};
}
@@ -0,0 +1,112 @@
#pragma once
#include <frostbite2D/event/event.h>
#include <SDL2/SDL_joystick.h>
namespace frostbite2D {
class JoystickEvent : public Event {
public:
JoystickEvent(EventType type, uint32 timestamp, int32 deviceId)
: Event(timestamp)
, type_(type)
, deviceId_(deviceId) {}
EventType getType() const override {
return type_;
}
int32 getDeviceId() const { return deviceId_; }
protected:
EventType type_;
int32 deviceId_;
};
class JoystickAxisEvent : public JoystickEvent {
public:
JoystickAxisEvent(uint32 timestamp, int32 deviceId, uint8 axis, int16 value)
: JoystickEvent(EventType::JoystickAxis, timestamp, deviceId)
, axis_(axis)
, value_(value) {}
uint8 getAxis() const { return axis_; }
int16 getValue() const { return value_; }
float getNormalizedValue() const {
return static_cast<float>(value_) / 32767.0f;
}
private:
uint8 axis_;
int16 value_;
};
class JoystickBallEvent : public JoystickEvent {
public:
JoystickBallEvent(uint32 timestamp, int32 deviceId, uint8 ball, int16 dx, int16 dy)
: JoystickEvent(EventType::JoystickBall, timestamp, deviceId)
, ball_(ball)
, dx_(dx)
, dy_(dy) {}
uint8 getBall() const { return ball_; }
int16 getDX() const { return dx_; }
int16 getDY() const { return dy_; }
private:
uint8 ball_;
int16 dx_;
int16 dy_;
};
class JoystickHatEvent : public JoystickEvent {
public:
JoystickHatEvent(uint32 timestamp, int32 deviceId, uint8 hat, uint8 value)
: JoystickEvent(EventType::JoystickHat, timestamp, deviceId)
, hat_(hat)
, value_(value) {}
uint8 getHat() const { return hat_; }
uint8 getValue() const { return value_; }
bool isCentered() const { return value_ == SDL_HAT_CENTERED; }
bool isUp() const { return (value_ & SDL_HAT_UP) != 0; }
bool isRight() const { return (value_ & SDL_HAT_RIGHT) != 0; }
bool isDown() const { return (value_ & SDL_HAT_DOWN) != 0; }
bool isLeft() const { return (value_ & SDL_HAT_LEFT) != 0; }
private:
uint8 hat_;
uint8 value_;
};
class JoystickButtonEvent : public JoystickEvent {
public:
JoystickButtonEvent(EventType type, uint32 timestamp, int32 deviceId, uint8 button)
: JoystickEvent(type, timestamp, deviceId)
, button_(button) {}
EventType getType() const override {
return type_;
}
uint8 getButton() const { return button_; }
private:
uint8 button_;
};
class JoystickButtonDownEvent : public JoystickButtonEvent {
public:
JoystickButtonDownEvent(uint32 timestamp, int32 deviceId, uint8 button)
: JoystickButtonEvent(EventType::JoystickButtonDown, timestamp, deviceId, button) {}
};
class JoystickButtonUpEvent : public JoystickButtonEvent {
public:
JoystickButtonUpEvent(uint32 timestamp, int32 deviceId, uint8 button)
: JoystickButtonEvent(EventType::JoystickButtonUp, timestamp, deviceId, button) {}
};
}
@@ -0,0 +1,208 @@
#pragma once
#include <frostbite2D/event/event.h>
#include <SDL2/SDL_keyboard.h>
#include <string>
namespace frostbite2D {
enum class KeyCode {
Unknown = SDLK_UNKNOWN,
Return = SDLK_RETURN,
Escape = SDLK_ESCAPE,
Backspace = SDLK_BACKSPACE,
Tab = SDLK_TAB,
Space = SDLK_SPACE,
Exclaim = SDLK_EXCLAIM,
Quotedbl = SDLK_QUOTEDBL,
Hash = SDLK_HASH,
Percent = SDLK_PERCENT,
Dollar = SDLK_DOLLAR,
Ampersand = SDLK_AMPERSAND,
Quote = SDLK_QUOTE,
LeftParen = SDLK_LEFTPAREN,
RightParen = SDLK_RIGHTPAREN,
Asterisk = SDLK_ASTERISK,
Plus = SDLK_PLUS,
Comma = SDLK_COMMA,
Minus = SDLK_MINUS,
Period = SDLK_PERIOD,
Slash = SDLK_SLASH,
Num0 = SDLK_0,
Num1 = SDLK_1,
Num2 = SDLK_2,
Num3 = SDLK_3,
Num4 = SDLK_4,
Num5 = SDLK_5,
Num6 = SDLK_6,
Num7 = SDLK_7,
Num8 = SDLK_8,
Num9 = SDLK_9,
Colon = SDLK_COLON,
Semicolon = SDLK_SEMICOLON,
Less = SDLK_LESS,
Equals = SDLK_EQUALS,
Greater = SDLK_GREATER,
Question = SDLK_QUESTION,
At = SDLK_AT,
LeftBracket = SDLK_LEFTBRACKET,
Backslash = SDLK_BACKSLASH,
RightBracket = SDLK_RIGHTBRACKET,
Caret = SDLK_CARET,
Underscore = SDLK_UNDERSCORE,
Backquote = SDLK_BACKQUOTE,
a = SDLK_a,
b = SDLK_b,
c = SDLK_c,
d = SDLK_d,
e = SDLK_e,
f = SDLK_f,
g = SDLK_g,
h = SDLK_h,
i = SDLK_i,
j = SDLK_j,
k = SDLK_k,
l = SDLK_l,
m = SDLK_m,
n = SDLK_n,
o = SDLK_o,
p = SDLK_p,
q = SDLK_q,
r = SDLK_r,
s = SDLK_s,
t = SDLK_t,
u = SDLK_u,
v = SDLK_v,
w = SDLK_w,
x = SDLK_x,
y = SDLK_y,
z = SDLK_z,
CapsLock = SDLK_CAPSLOCK,
F1 = SDLK_F1,
F2 = SDLK_F2,
F3 = SDLK_F3,
F4 = SDLK_F4,
F5 = SDLK_F5,
F6 = SDLK_F6,
F7 = SDLK_F7,
F8 = SDLK_F8,
F9 = SDLK_F9,
F10 = SDLK_F10,
F11 = SDLK_F11,
F12 = SDLK_F12,
PrintScreen = SDLK_PRINTSCREEN,
ScrollLock = SDLK_SCROLLLOCK,
Pause = SDLK_PAUSE,
Insert = SDLK_INSERT,
Home = SDLK_HOME,
PageUp = SDLK_PAGEUP,
Delete = SDLK_DELETE,
End = SDLK_END,
PageDown = SDLK_PAGEDOWN,
Right = SDLK_RIGHT,
Left = SDLK_LEFT,
Down = SDLK_DOWN,
Up = SDLK_UP,
Numpad0 = SDLK_KP_0,
Numpad1 = SDLK_KP_1,
Numpad2 = SDLK_KP_2,
Numpad3 = SDLK_KP_3,
Numpad4 = SDLK_KP_4,
Numpad5 = SDLK_KP_5,
Numpad6 = SDLK_KP_6,
Numpad7 = SDLK_KP_7,
Numpad8 = SDLK_KP_8,
Numpad9 = SDLK_KP_9,
NumpadDecimal = SDLK_KP_DECIMAL,
NumpadDivide = SDLK_KP_DIVIDE,
NumpadMultiply = SDLK_KP_MULTIPLY,
NumpadMinus = SDLK_KP_MINUS,
NumpadPlus = SDLK_KP_PLUS,
NumpadEnter = SDLK_KP_ENTER,
NumpadEquals = SDLK_KP_EQUALS,
LShift = SDLK_LSHIFT,
RShift = SDLK_RSHIFT,
LCtrl = SDLK_LCTRL,
RCtrl = SDLK_RCTRL,
LAlt = SDLK_LALT,
RAlt = SDLK_RALT,
LGui = SDLK_LGUI,
RGui = SDLK_RGUI
};
enum class KeyMod {
None = KMOD_NONE,
LShift = KMOD_LSHIFT,
RShift = KMOD_RSHIFT,
Shift = KMOD_SHIFT,
LCtrl = KMOD_LCTRL,
RCtrl = KMOD_RCTRL,
Ctrl = KMOD_CTRL,
LAlt = KMOD_LALT,
RAlt = KMOD_RALT,
Alt = KMOD_ALT,
LGui = KMOD_LGUI,
RGui = KMOD_RGUI,
Gui = KMOD_GUI,
Num = KMOD_NUM,
Caps = KMOD_CAPS
};
class KeyEvent : public Event {
public:
KeyEvent(uint32 timestamp, KeyCode keyCode, KeyMod modifiers, bool repeat)
: Event(timestamp)
, keyCode_(keyCode)
, modifiers_(modifiers)
, repeat_(repeat) {}
EventType getType() const override {
return EventType::KeyDown;
}
KeyCode getKeyCode() const { return keyCode_; }
KeyMod getModifiers() const { return modifiers_; }
bool isRepeat() const { return repeat_; }
private:
KeyCode keyCode_;
KeyMod modifiers_;
bool repeat_;
};
class KeyUpEvent : public Event {
public:
KeyUpEvent(uint32 timestamp, KeyCode keyCode, KeyMod modifiers)
: Event(timestamp)
, keyCode_(keyCode)
, modifiers_(modifiers) {}
EventType getType() const override {
return EventType::KeyUp;
}
KeyCode getKeyCode() const { return keyCode_; }
KeyMod getModifiers() const { return modifiers_; }
private:
KeyCode keyCode_;
KeyMod modifiers_;
};
class KeyTextEvent : public Event {
public:
KeyTextEvent(uint32 timestamp, const std::string& text)
: Event(timestamp)
, text_(text) {}
EventType getType() const override {
return EventType::KeyText;
}
const std::string& getText() const { return text_; }
private:
std::string text_;
};
}
@@ -0,0 +1,108 @@
#pragma once
#include <frostbite2D/event/event.h>
#include <frostbite2D/types/type_math.h>
#include <SDL2/SDL_mouse.h>
namespace frostbite2D {
enum class MouseButton {
Left = SDL_BUTTON_LEFT,
Right = SDL_BUTTON_RIGHT,
Middle = SDL_BUTTON_MIDDLE,
X1 = SDL_BUTTON_X1,
X2 = SDL_BUTTON_X2
};
class MouseEvent : public Event {
public:
MouseEvent(EventType type, uint32 timestamp, const Vec2& position)
: Event(timestamp)
, type_(type)
, position_(position) {}
virtual ~MouseEvent() = default;
EventType getType() const override { return type_; }
const Vec2& getPosition() const { return position_; }
int32 getX() const { return static_cast<int32>(position_.x); }
int32 getY() const { return static_cast<int32>(position_.y); }
protected:
EventType type_;
Vec2 position_;
};
class MouseMoveEvent : public Event {
public:
MouseMoveEvent(uint32 timestamp, const Vec2& position, const Vec2& relative)
: Event(timestamp)
, position_(position)
, relative_(relative) {}
EventType getType() const override {
return EventType::MouseMove;
}
const Vec2& getPosition() const { return position_; }
const Vec2& getRelative() const { return relative_; }
int32 getX() const { return static_cast<int32>(position_.x); }
int32 getY() const { return static_cast<int32>(position_.y); }
private:
Vec2 position_;
Vec2 relative_;
};
class MouseButtonEvent : public Event {
public:
MouseButtonEvent(uint32 timestamp, MouseButton button, const Vec2& position, bool down)
: Event(timestamp)
, button_(button)
, position_(position)
, down_(down) {}
EventType getType() const override {
return down_ ? EventType::MouseButtonDown : EventType::MouseButtonUp;
}
MouseButton getButton() const { return button_; }
const Vec2& getPosition() const { return position_; }
bool isDown() const { return down_; }
int32 getX() const { return static_cast<int32>(position_.x); }
int32 getY() const { return static_cast<int32>(position_.y); }
private:
MouseButton button_;
Vec2 position_;
bool down_;
};
class MouseWheelEvent : public Event {
public:
MouseWheelEvent(uint32 timestamp, const Vec2& position, const Vec2& direction, bool flipped)
: Event(timestamp)
, position_(position)
, direction_(direction)
, flipped_(flipped) {}
EventType getType() const override {
return EventType::MouseWheel;
}
const Vec2& getPosition() const { return position_; }
const Vec2& getDirection() const { return direction_; }
bool isFlipped() const { return flipped_; }
int32 getX() const { return static_cast<int32>(direction_.x); }
int32 getY() const { return static_cast<int32>(direction_.y); }
private:
Vec2 position_;
Vec2 direction_;
bool flipped_;
};
}
@@ -0,0 +1,64 @@
#pragma once
#include <frostbite2D/event/event.h>
#include <frostbite2D/types/type_math.h>
#include <SDL2/SDL_touch.h>
namespace frostbite2D {
class TouchEvent : public Event {
public:
TouchEvent(EventType type, uint32 timestamp, int64 touchId, int64 fingerId,
const Vec2& position, const Vec2& delta, float pressure)
: Event(timestamp)
, type_(type)
, touchId_(touchId)
, fingerId_(fingerId)
, position_(position)
, delta_(delta)
, pressure_(pressure) {}
EventType getType() const override {
return type_;
}
int64 getTouchId() const { return touchId_; }
int64 getFingerId() const { return fingerId_; }
const Vec2& getPosition() const { return position_; }
const Vec2& getDelta() const { return delta_; }
float getPressure() const { return pressure_; }
private:
EventType type_;
int64 touchId_;
int64 fingerId_;
Vec2 position_;
Vec2 delta_;
float pressure_;
};
class TouchDownEvent : public TouchEvent {
public:
TouchDownEvent(uint32 timestamp, int64 touchId, int64 fingerId,
const Vec2& position, float pressure)
: TouchEvent(EventType::TouchDown, timestamp, touchId, fingerId,
position, Vec2(0, 0), pressure) {}
};
class TouchUpEvent : public TouchEvent {
public:
TouchUpEvent(uint32 timestamp, int64 touchId, int64 fingerId,
const Vec2& position, float pressure)
: TouchEvent(EventType::TouchUp, timestamp, touchId, fingerId,
position, Vec2(0, 0), pressure) {}
};
class TouchMoveEvent : public TouchEvent {
public:
TouchMoveEvent(uint32 timestamp, int64 touchId, int64 fingerId,
const Vec2& position, const Vec2& delta, float pressure)
: TouchEvent(EventType::TouchMove, timestamp, touchId, fingerId,
position, delta, pressure) {}
};
}
@@ -0,0 +1,77 @@
#pragma once
#include <frostbite2D/event/event.h>
#include <SDL2/SDL_events.h>
namespace frostbite2D {
class WindowEvent : public Event {
public:
WindowEvent(EventType type, uint32 timestamp, int32 windowId)
: Event(timestamp)
, type_(type)
, windowId_(windowId) {}
EventType getType() const override {
return type_;
}
int32 getWindowId() const { return windowId_; }
protected:
EventType type_;
int32 windowId_;
};
class WindowCloseEvent : public WindowEvent {
public:
WindowCloseEvent(uint32 timestamp, int32 windowId)
: WindowEvent(EventType::WindowClose, timestamp, windowId) {}
};
class WindowResizeEvent : public WindowEvent {
public:
WindowResizeEvent(uint32 timestamp, int32 windowId, int32 width, int32 height)
: WindowEvent(EventType::WindowResize, timestamp, windowId)
, width_(width)
, height_(height) {}
int32 getWidth() const { return width_; }
int32 getHeight() const { return height_; }
private:
int32 width_;
int32 height_;
};
class WindowFocusEvent : public WindowEvent {
public:
WindowFocusEvent(uint32 timestamp, int32 windowId, bool focused)
: WindowEvent(EventType::WindowFocus, timestamp, windowId)
, focused_(focused) {}
bool isFocused() const { return focused_; }
private:
bool focused_;
};
class WindowMinimizeEvent : public WindowEvent {
public:
WindowMinimizeEvent(uint32 timestamp, int32 windowId)
: WindowEvent(EventType::WindowMinimize, timestamp, windowId) {}
};
class WindowMaximizeEvent : public WindowEvent {
public:
WindowMaximizeEvent(uint32 timestamp, int32 windowId)
: WindowEvent(EventType::WindowMaximize, timestamp, windowId) {}
};
class WindowRestoreEvent : public WindowEvent {
public:
WindowRestoreEvent(uint32 timestamp, int32 windowId)
: WindowEvent(EventType::WindowRestore, timestamp, windowId) {}
};
}