basic movement
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
#include <object/entity.hpp>
|
||||
#include <renderer/renderer.hpp>
|
||||
#include <renderer/texture.hpp>
|
||||
#include <object/camera.hpp>
|
||||
#include <window/window.hpp>
|
||||
#include <cmath>
|
||||
|
||||
namespace Game::Object {
|
||||
Entity::~Entity() = default;
|
||||
@@ -35,32 +38,64 @@ namespace Game::Object {
|
||||
return *this;
|
||||
}
|
||||
|
||||
void Entity::render(Game::Renderer::Renderer* renderer) {
|
||||
void Entity::render(Game::Renderer::Renderer* renderer, Game::Renderer::RendererConfig config) {
|
||||
if (!mIsActive || !mTex) return; // Don't render if not active or if there's no texture
|
||||
|
||||
float w, h;
|
||||
SDL_GetTextureSize(mTex->getSDLTexture(), &w, &h);
|
||||
if (!mTex->isTiled()) {
|
||||
float w, h;
|
||||
SDL_GetTextureSize(mTex->getSDLTexture(), &w, &h);
|
||||
|
||||
SDL_FRect dst;
|
||||
dst.w = w * mTransform.scaleX * mScaleConstant; // 1.f is HUGE, so this is just a constant to make the default scale more reasonable
|
||||
dst.h = h * mTransform.scaleY * mScaleConstant;
|
||||
SDL_FRect dst;
|
||||
dst.w = w * mTransform.scaleX * UNIVERSAL_SCALE_COEFFICIENT; // 1.f is HUGE, so this is just a constant to make the default scale more reasonable
|
||||
dst.h = h * mTransform.scaleY * UNIVERSAL_SCALE_COEFFICIENT;
|
||||
|
||||
// Top-left origin
|
||||
dst.x = mTransform.x;
|
||||
dst.y = mTransform.y;
|
||||
// Top-left origin; Account for camera position (center the camera on the screen)
|
||||
dst.x = mTransform.x - config.camX + config.screenW / 2.f;
|
||||
dst.y = mTransform.y - config.camY + config.screenH / 2.f;
|
||||
|
||||
SDL_FPoint center;
|
||||
center.x = dst.w / 2.f;
|
||||
center.y = dst.h / 2.f;
|
||||
SDL_FPoint center;
|
||||
center.x = dst.w / 2.f;
|
||||
center.y = dst.h / 2.f;
|
||||
|
||||
SDL_RenderTextureRotated(
|
||||
renderer->getSDLRenderer(),
|
||||
mTex->getSDLTexture(),
|
||||
nullptr,
|
||||
&dst,
|
||||
mTransform.rotation,
|
||||
¢er,
|
||||
SDL_FLIP_NONE
|
||||
);
|
||||
SDL_RenderTextureRotated(
|
||||
renderer->getSDLRenderer(),
|
||||
mTex->getSDLTexture(),
|
||||
nullptr,
|
||||
&dst,
|
||||
mTransform.rotation,
|
||||
¢er,
|
||||
SDL_FLIP_NONE
|
||||
);
|
||||
} else {
|
||||
// Tiled rendering - render the texture repeatedly to fill the area defined by the entity's transform
|
||||
// We assume that we always render scaleX by scaleY tiles, and that the texture should be rendered at its original size (i.e., the scaleX and scaleY of the entity only affect how many times the texture is tiled, not the size of each tile)
|
||||
float tileW, tileH;
|
||||
SDL_GetTextureSize(mTex->getSDLTexture(), &tileW, &tileH);
|
||||
|
||||
SDL_FRect dst;
|
||||
dst.w = tileW * mTiledScale; // Tile size is the original texture size multiplied by the universal scale coefficient (ignoring the entity's scale, since that only affects how many times the texture is tiled, not the size of each tile)
|
||||
dst.h = tileH * mTiledScale;
|
||||
|
||||
// Top-left origin; Account for camera position (center the camera on the screen)
|
||||
dst.x = mTransform.x - config.camX + config.screenW / 2.f;
|
||||
dst.y = mTransform.y - config.camY + config.screenH / 2.f;
|
||||
|
||||
for (int i = 0; i < mTransform.scaleX; i++) {
|
||||
for (int j = 0; j < mTransform.scaleY; j++) {
|
||||
SDL_RenderTextureRotated(
|
||||
renderer->getSDLRenderer(),
|
||||
mTex->getSDLTexture(),
|
||||
nullptr,
|
||||
&dst,
|
||||
mTransform.rotation,
|
||||
nullptr, // No rotation center since each tile is rendered independently
|
||||
SDL_FLIP_NONE
|
||||
);
|
||||
dst.y += dst.h; // Move down for the next tile in the column
|
||||
}
|
||||
dst.y = mTransform.y - config.camY + config.screenH / 2.f; // Reset y to the top of the column
|
||||
dst.x += dst.w; // Move right for the next column of tiles
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user