Add DRM rendering & clean up

This commit is contained in:
Maple Redleaf
2025-12-09 14:13:36 -06:00
parent f97e8fe9d8
commit 694411efdf
7 changed files with 17 additions and 107 deletions

View File

@@ -1,12 +0,0 @@
#pragma once
class Entity;
class Component {
public:
Entity *owner = nullptr;
virtual ~Component() = default;
virtual void Update(float dt) {}
virtual void Draw() {}
};

View File

@@ -1,13 +0,0 @@
#include "entity.h"
void Entity::Update(float dt) {
for (auto &comp : components) {
comp->Update(dt);
}
}
void Entity::Draw() {
for (auto &comp : components) {
comp->Draw();
}
}

View File

@@ -1,21 +0,0 @@
#pragma once
#define ENTITY_H_INCLUDED
#include "component.h"
#include <memory>
#include <vector>
class Entity {
public:
template <typename T, typename... Args> T *AddComponent(Args &&...args);
template <typename T> T *GetComponent();
template <typename T> bool HasComponent();
template <typename T> void RemoveComponent();
void Update(float dt);
void Draw();
private:
std::vector<std::unique_ptr<Component>> components;
};
#include "entity.ipp"

View File

@@ -1,36 +0,0 @@
#pragma once
#include <algorithm>
#ifndef ENTITY_H_INCLUDED
#include "entity.h"
#endif
template <typename T, typename... Args>
T *Entity::AddComponent(Args &&...args) {
T *component = new T(std::forward<Args>(args)...);
component->owner = this;
components.emplace_back(component);
return component;
}
template <typename T> T *Entity::GetComponent() {
for (auto &comp : components) {
if (T *casted = dynamic_cast<T *>(comp.get())) {
return casted;
}
}
return nullptr;
}
template <typename T> bool Entity::HasComponent() {
return GetComponent<T>() != nullptr;
}
template <typename T> void Entity::RemoveComponent() {
components.erase(std::remove_if(components.begin(), components.end(),
[](const std::unique_ptr<Component> &comp) {
return dynamic_cast<T *>(comp.get()) !=
nullptr;
}),
components.end());
}

View File

View File

@@ -1,11 +0,0 @@
#include "entity.h"
#include <vector>
class World {
public:
void Update(float dt);
void Draw();
void AddEntity(Entity entity) {}
private:
std::vector<Entity> entities;
};

View File

@@ -1,4 +1,5 @@
#include "../data/cube.png.h" #include "../data/cube.png.h"
#include "raylib.h"
#include <cmath> #include <cmath>
#include <raylib-cpp.hpp> #include <raylib-cpp.hpp>
@@ -8,17 +9,18 @@
#define SCREEN_WIDTH (320) #define SCREEN_WIDTH (320)
#define SCREEN_HEIGHT (240) #define SCREEN_HEIGHT (240)
#define WIN_WIDTH (1280)
#define WIN_HEIGHT (720)
#define MAX(a, b) ((a) > (b) ? (a) : (b)) #define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define MIN(a, b) ((a) < (b) ? (a) : (b))
void MainLoop(); void MainLoop();
raylib::Window window(SCREEN_WIDTH, SCREEN_HEIGHT, "game", raylib::Window window(WIN_WIDTH, WIN_HEIGHT, "game",
FLAG_MSAA_4X_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_RESIZABLE | FLAG_VSYNC_HINT);
FLAG_WINDOW_MAXIMIZED | FLAG_VSYNC_HINT);
raylib::RenderTexture2D target(320, 240); raylib::RenderTexture2D target(SCREEN_WIDTH, SCREEN_HEIGHT);
raylib::Image texImg = raylib::Image texImg =
raylib::LoadImageFromMemory(".png", cube_png, cube_png_len); raylib::LoadImageFromMemory(".png", cube_png, cube_png_len);
@@ -43,7 +45,7 @@ int main(void) {
#if defined(PLATFORM_WEB) #if defined(PLATFORM_WEB)
emscripten_set_main_loop(MainLoop, 0, 1); emscripten_set_main_loop(MainLoop, 0, 1);
#else #else
while (!window.ShouldClose()) while (!window.ShouldClose() && !raylib::Keyboard::IsKeyDown(KEY_ESCAPE))
MainLoop(); MainLoop();
#endif #endif
@@ -54,8 +56,9 @@ int main(void) {
void MainLoop() { void MainLoop() {
{ {
float scale = MIN((float)window.GetWidth() / SCREEN_WIDTH, int winWidth = GetScreenWidth(), winHeight = GetScreenHeight();
(float)window.GetHeight() / SCREEN_HEIGHT); float scale =
MIN((float)winWidth / SCREEN_WIDTH, (float)winHeight / SCREEN_HEIGHT);
cube.SetTransform( cube.SetTransform(
raylib::Matrix::RotateXYZ( raylib::Matrix::RotateXYZ(
@@ -77,14 +80,14 @@ void MainLoop() {
window.BeginDrawing(); window.BeginDrawing();
{ {
ClearBackground(BLACK); ClearBackground(GREEN);
target.GetTexture().Draw( target.GetTexture().Draw(
raylib::Rectangle(0.0f, 0.0f, (float)target.GetTexture().width, raylib::Rectangle(0.0f, 0.0f, (float)SCREEN_WIDTH,
(float)-target.GetTexture().height), (float)-SCREEN_HEIGHT),
raylib::Rectangle( raylib::Rectangle((winWidth - ((float)SCREEN_WIDTH * scale)) * 0.5f,
(window.GetWidth() - ((float)SCREEN_WIDTH * scale)) * 0.5f, (winHeight - ((float)SCREEN_HEIGHT * scale)) * 0.5f,
(window.GetHeight() - ((float)SCREEN_HEIGHT * scale)) * 0.5f, (float)SCREEN_WIDTH * scale,
(float)SCREEN_WIDTH * scale, (float)SCREEN_HEIGHT * scale), (float)SCREEN_HEIGHT * scale),
raylib::Vector2(0, 0), 0.0f, WHITE); raylib::Vector2(0, 0), 0.0f, WHITE);
} }
window.EndDrawing(); window.EndDrawing();