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
36 changes: 30 additions & 6 deletions engine/include/velos/app/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,57 @@
#include "velos/events/event.h"
#include "velos/input/key.h"
#include "velos/actions/actions.h"
#include "velos/time/timer.h"
#include "velos/entity/entity_manager.h"

namespace velos {
class Engine {
public:
explicit Engine(const EngineConfig& config);
explicit Engine(const EngineConfig& config = EngineConfig{});
~Engine();

void run();
void stop();

void onEvent(Event& e);

using EventCallback = std::function<void(Event&)>;
void addEventListener(EventType type, EventCallback callback);

void setAction(Key key, const ActionId& action, ActionType type, float holdThreshold, std::function<void(float)> callback);
void processActions(float dt);

std::shared_ptr<Entity> createEntity() { return m_entityManager->createEntity(); }
const std::vector<std::shared_ptr<Entity>>& entities() const { return m_entityManager->entities(); }
EntityManager* entityManager() { return m_entityManager.get(); }

void attachCamera(Entity& entity);
void detachCamera();

void setDebugRender(bool state);
Camera* camera() { return m_camera.get(); }

// APIs
void setTargetFPS(int fps) { m_targetFPS = fps; }
int targetFPS() const { return m_targetFPS; }

void addMovement(std::shared_ptr<Entity> entity, float speed = 200.f, Key up = Key::W, Key left = Key::A, Key down = Key::S, Key right = Key::D);
void addRotationControl(std::shared_ptr<Entity> entity, float speed = 3.f, Key left = Key::Q, Key right = Key::E);
void addScaleControl(std::shared_ptr<Entity> entity, float speed = 1.f, Key down = Key::Z, Key up = Key::X);
void addCameraControl(float speed = 250.f, Key up = Key::ArrowUp, Key left = Key::ArrowLeft, Key down = Key::ArrowDown, Key right = Key::ArrowRight);
void addCameraZoomControl(float speed = 1.5f, Key zoomOut = Key::Q, Key zoomIn = Key::E);

void onEntityClick(std::function<void(std::shared_ptr<Entity>)>);
void onHoldAction(Key key, const ActionId& action, float threshold, std::function<void()> callback);

void setTimeout(float seconds, std::function<void()> callback);

static Engine* instance() { return s_instance; }
Camera* camera() { return m_camera.get(); }
EntityManager* entityManager() { return m_entityManager.get(); }
private:
void processActions(float dt);
int m_targetFPS;

// timer variables
std::vector<Timer> m_timers;
void updateTimers(float dt);

std::unique_ptr<Window> m_window;
std::unique_ptr<Camera> m_camera;
std::unique_ptr<Renderer> m_renderer;
Expand Down
2 changes: 2 additions & 0 deletions engine/include/velos/app/engine_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ namespace velos {
int window_width = 1280;
int window_height = 720;
bool enable_vsync = true;

int target_fps = 60;
};
}
5 changes: 5 additions & 0 deletions engine/include/velos/core/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ namespace velos {
#define VELOS_INFO(...) ::velos::Log::engine()->info(__VA_ARGS__)
#define VELOS_WARN(...) ::velos::Log::engine()->warn(__VA_ARGS__)
#define VELOS_ERROR(...) ::velos::Log::engine()->error(__VA_ARGS__)

#define APP_TRACE(...) ::velos::Log::app()->trace(__VA_ARGS__)
#define APP_INFO(...) ::velos::Log::app()->info(__VA_ARGS__)
#define APP_WARN(...) ::velos::Log::app()->warn(__VA_ARGS__)
#define APP_ERROR(...) ::velos::Log::app()->error(__VA_ARGS__)
12 changes: 11 additions & 1 deletion engine/include/velos/core/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,15 @@ namespace velos {
Unknown
};

Platform current_platform();
constexpr Platform current_platform() {
#if defined(_WIN32)
return Platform::Windows;
#elif defined(__linux__)
return Platform::Linux;
#elif defined(__APPLE__)
return Platform::MacOS;
#else
return Platform::Unknown;
#endif
}
}
59 changes: 54 additions & 5 deletions engine/include/velos/entity/entity.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#pragma once
#include <vector>
#include <memory>
#include "../physics/collider.h"
#include "../graphics/shapes/shape.h"
#include "../graphics/renderer.h"
#include "../math/transform.h"
#include "../graphics/color.h"

namespace velos {
class Collider;

class Entity {
public:
Entity() = default;
Expand All @@ -19,10 +19,29 @@ namespace velos {
void debugRender(Renderer& renderer);
bool containsPoint(const Vec2& worldPos) const;

template<typename T, typename... Args>
std::shared_ptr<T> addShape(Args&&... args) {
auto shape = std::make_shared<T>(std::forward<Args>(args)...);
m_shapes.push_back(shape);
return shape;
}

template<typename T, typename... Args>
T* addCollider(Args&&... args) {
auto collider = std::make_unique<T>(std::forward<Args>(args)...);
T* raw = collider.get();
collider->setOwner(this);
m_colliders.push_back(std::move(collider));
return raw;
}

void addShape(std::shared_ptr<Shape> shape) { m_shapes.push_back(shape); }
void addCollider(std::unique_ptr<Collider> collider) {
collider->setOwner(this);
m_colliders.push_back(std::move(collider));
}

void addCollider(std::unique_ptr<Collider> collider);
const std::vector<std::unique_ptr<Collider>>& colliders() const;
const std::vector<std::unique_ptr<Collider>>& colliders() const { return m_colliders; }
void updateColliders();

void setVelocity(Vec2 velocity) { m_velocity = velocity; }
Expand All @@ -31,11 +50,41 @@ namespace velos {
Vec2& velocity() { return m_velocity; }

Transform& transform() { return m_transform; }
const Vec2& position() const { return m_transform.position; }
void setPosition(const Vec2& position) { m_transform.position = position; }

void setRotation(float rot) { m_transform.rotation = rot; }
void setScale(const Vec2& scale) { m_transform.scale = scale; }

// set color for all entity's shapes
void setColor(const Color& color);

void move(const Vec2& offset) { m_transform.position += offset; }
void rotate(float angle) { m_transform.rotation += angle; }
void addScale(float scale) { m_transform.scale += Vec2{scale, scale}; }

void setAngularVelocity(float value) { m_angularVelocity = value; }
float angularVelocity() const { return m_angularVelocity; }

void setScaleVelocity(const Vec2& scale) { m_scaleVelocity = scale; }
Vec2 scaleVelocity() const { return m_scaleVelocity; }

bool isStatic() const { return m_isStatic; }
void setStatic(bool state) { m_isStatic = state; }

bool isDestroyed() const { return m_isDistroyed; }
void destroy() { m_isDistroyed = true; }

private:
Vec2 m_velocity;

Transform m_transform;

float m_angularVelocity = 0.f;
Vec2 m_scaleVelocity{0.f, 0.f};

std::vector<std::shared_ptr<Shape>> m_shapes;
std::vector<std::unique_ptr<Collider>> m_colliders;
bool m_isStatic = false;
bool m_isDistroyed = false;
};
}
9 changes: 8 additions & 1 deletion engine/include/velos/entity/entity_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,20 @@
namespace velos {
class EntityManager {
public:
std::shared_ptr<Entity> createEntity() {
auto entity = std::make_shared<Entity>();
m_entities.push_back(entity);
return entity;
}

void addEntity(std::shared_ptr<Entity> entity) { m_entities.push_back(entity); }

void update(float dt);
void render(Renderer& renderer);

void setDebugRender(bool state);

std::vector<std::shared_ptr<Entity>> entities() { return m_entities; }
const std::vector<std::shared_ptr<Entity>>& entities() const { return m_entities; }
private:
std::vector<std::shared_ptr<Entity>> m_entities;
PhysicsSystem m_physics;
Expand Down
13 changes: 11 additions & 2 deletions engine/include/velos/graphics/camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ namespace velos {
Camera() = default;

Transform& transform() { return m_transform; }
const Transform& transform() const { return m_transform; }

void follow(const Transform* target);
void unfollow();
bool isFollowing();
bool isFollowing() const;

void setFollowMode(CameraFollowMode mode);
void setSmoothSpeed(float speed);
Expand All @@ -22,8 +23,13 @@ namespace velos {

void update(float dt);

float zoom() { return m_zoom; }
void setVelocity(const Vec2& vel) { m_velocity = vel; }
void setVelocityX(float x) { m_velocity.x = x; }
void setVelocityY(float y) { m_velocity.y = y; }

float zoom() const { return m_zoom; }
void setZoom(float zoom) { m_zoom = std::max(0.01f, zoom); }
void setZoomVelocity(float zoomVelocity) { m_zoomVelocity = zoomVelocity; }

Vec2 worldToScreen(const Vec2& world, const Vec2& screenCenter) const;
Vec2 screenToWorld(const Vec2& screen, const Vec2& windowCenter) const;
Expand All @@ -32,6 +38,9 @@ namespace velos {
Transform m_transform;
const Transform* m_target = nullptr;

Vec2 m_velocity{0.f, 0.f};
float m_zoomVelocity;

CameraFollowMode m_mode = CameraFollowMode::Instant;
float m_smoothSpeed = 8.f;

Expand Down
20 changes: 19 additions & 1 deletion engine/include/velos/graphics/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,27 @@

namespace velos {
struct Color {
uint8_t r, g, b, a;
uint8_t r = 255;
uint8_t g = 255;
uint8_t b = 255;
uint8_t a = 255;

Color() = default;
Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha = 255)
: r(red), g(green), b(blue), a(alpha) {}

static const Color White;
static const Color Black;
static const Color Red;
static const Color Green;
static const Color Blue;
static const Color Yellow;
static const Color Grey;
static const Color Brown;
static const Color Pink;
static const Color Cyan;
static const Color Orange;
static const Color Magenta;
static const Color Transparent;
};
}
2 changes: 2 additions & 0 deletions engine/include/velos/graphics/renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ namespace velos {
explicit Renderer(SDL_Window* window);
~Renderer();

Renderer(const Renderer&) = delete;

void beginFrame();
void endFrame();

Expand Down
4 changes: 4 additions & 0 deletions engine/include/velos/graphics/shapes/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ namespace velos {
void setColor(const Color& color) { m_color = color; }

Transform& transform() { return m_localTransform; }
void setPosition(const Vec2& position) { m_localTransform.position = position; }
void setRotation(float rot) { m_localTransform.rotation = rot; }
void setScale(const Vec2& scale) { m_localTransform.scale = scale; }

protected:
Transform m_localTransform;
Color m_color{255, 255, 255, 255};
Expand Down
10 changes: 5 additions & 5 deletions engine/include/velos/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace velos {
enum class MouseButton {
Left, Right, Middle
Left, Right, Middle, Unknown
};

class Input {
Expand All @@ -18,10 +18,10 @@ namespace velos {
static void getMousePosition(float& x, float& y);

private:
inline static std::unordered_map<Key, bool> s_keyStates;
static std::unordered_map<Key, bool> s_keyStates;
static std::unordered_map<MouseButton, bool> s_mouseStates;

inline static std::unordered_map<MouseButton, bool> s_mouseStates;
inline static float s_mouseX;
inline static float s_mouseY;
static float s_mouseX;
static float s_mouseY;
};
}
Loading
Loading