Skip to content

Commit

Permalink
[#190]: Initial work for zoom agnostic Sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobDomagala committed Feb 5, 2024
1 parent 11c8435 commit 134a779
Show file tree
Hide file tree
Showing 27 changed files with 199 additions and 162 deletions.
4 changes: 3 additions & 1 deletion assets/shaders/default.vert
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
layout(set = 0, binding = 0) uniform UniformBufferObject
{
mat4 u_projectionMat;
mat4 u_projectionNoZoomMat;
mat4 u_viewMat;
vec4 u_cameraPos;
}
Expand Down Expand Up @@ -38,6 +39,7 @@ vs_out;

layout (push_constant) uniform PushConstants {
float selectedIdx;
float meshType;
} pushConstants;

void
Expand All @@ -55,5 +57,5 @@ main(void)
mat4 modelMat = curInstanceData.modelMat;
vec3 position = a_position;

gl_Position = ubo.u_projectionMat * ubo.u_viewMat * modelMat * vec4(position.xyz, 1.0f);
gl_Position = (pushConstants.meshType != 1.0f ? ubo.u_projectionMat : ubo.u_projectionNoZoomMat) * ubo.u_viewMat * modelMat * vec4(position.xyz, 1.0f);
}
Binary file modified assets/shaders/frag.spv
Binary file not shown.
Binary file modified assets/shaders/line.frag.spv
Binary file not shown.
2 changes: 2 additions & 0 deletions assets/shaders/line.vert
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ layout(location = 0) in vec3 a_position;
layout(set = 0, binding = 0) uniform UniformBufferObject
{
mat4 u_projectionMat;
mat4 u_projectionNoZoomMat;
mat4 u_viewMat;
vec4 u_cameraPos;
}
ubo;

Expand Down
Binary file modified assets/shaders/line.vert.spv
Binary file not shown.
Binary file modified assets/shaders/ui.frag.spv
Binary file not shown.
Binary file modified assets/shaders/ui.vert.spv
Binary file not shown.
Binary file modified assets/shaders/vert.spv
Binary file not shown.
16 changes: 13 additions & 3 deletions editor/editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,6 @@ Editor::Render(VkCommandBuffer cmdBuffer)

currentLevel_->GetSprite().Render();

gizmo_.Render();
DrawAnimationPoints();

currentLevel_->RenderGameObjects();
Expand All @@ -912,6 +911,7 @@ Editor::Render(VkCommandBuffer cmdBuffer)
nullptr);
renderer::QuadShader::PushConstants pushConstants = {};
pushConstants.selectedIdx = -1.0f;
pushConstants.meshType = 0.0f;

if (currentSelectedGameObject_ != Object::INVALID_ID)
{
Expand All @@ -925,6 +925,14 @@ Editor::Render(VkCommandBuffer cmdBuffer)

for (int32_t layer = renderer::NUM_LAYERS - 1; layer >= 0; --layer)
{
// On-top objects
if (layer == 0)
{
pushConstants.meshType = 1.0f;
vkCmdPushConstants(cmdBuffer, renderData.pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0,
sizeof(renderer::QuadShader::PushConstants), &pushConstants);
}

const auto idx = static_cast< size_t >(layer);
const auto& numObjects = renderData.numMeshes.at(idx);
const auto renderThisLayer = renderLayerToDraw_ == -1 ? true : renderLayerToDraw_ == layer;
Expand Down Expand Up @@ -967,6 +975,7 @@ Editor::Render(VkCommandBuffer cmdBuffer)
vkCmdDrawIndexed(cmdBuffer, renderer::EditorData::numGridLines * renderer::INDICES_PER_LINE,
1, 0, 0, 0);

// DYNAMIC LINES
linePushConstants.color = glm::vec4(0.5f, 0.0f, 0.0f, 1.0f);
vkCmdPushConstants(cmdBuffer, renderer::EditorData::linePipelineLayout_,
VK_SHADER_STAGE_FRAGMENT_BIT, 0,
Expand Down Expand Up @@ -1409,8 +1418,9 @@ Editor::Update()
}

auto& renderData = renderer::GetRenderData();
renderData.viewMat = camera_.GetViewMatrix();
renderData.projMat = camera_.GetProjectionMatrix();
renderData.viewMat = camera_.viewMatrix_;
renderData.projMat = camera_.projectionMatrix_;
renderData.projNoZoomMat = camera_.projectionWithoutZoom_;

renderer::UpdateData();
}
Expand Down
4 changes: 2 additions & 2 deletions editor/editor_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ EditorObject::EditorObject(Editor* editor, const glm::vec2& positionOnMap, const
objectID_(linkedObject),
hasLinkedObject_(true)
{
auto depth = 0.0f;
auto depth = 0.01f;

sprite_.SetSpriteTextured(glm::vec3{position_, depth}, size, sprite);
}
Expand Down Expand Up @@ -224,7 +224,7 @@ EditorObject::Rotate(float angle, bool cumulative)
{
cumulative ? sprite_.RotateCumulative(angle) : sprite_.Rotate(angle);

auto rotate = sprite_.GetRotation(renderer::Sprite::RotationType::DEGREES);
auto rotate = sprite_.GetRotation(renderer::RotationType::degrees);
if (hasLinkedObject_)
{
switch (Object::GetTypeFromID(objectID_))
Expand Down
14 changes: 9 additions & 5 deletions editor/gizmo.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
#include "gizmo.hpp"
#include "types.hpp"

namespace looper {

void
Gizmo::Initialize()
{
gizmoCenter_.SetSpriteTextured(glm::vec3{}, {16, 16}, "centered_move.png");
gizmoCenter_.SetSpriteTextured(glm::vec3{}, {16, 16}, "centered_move.png",
renderer::SpriteType::alwaysOnTop);
gizmoCenter_.SetColor({1.0f, 1.0f, 1.0f, 1.0f});

gizmoUp_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png");
gizmoUp_.Rotate(90.0f, renderer::Sprite::RotationType::DEGREES);
gizmoUp_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png",
renderer::SpriteType::alwaysOnTop);
gizmoUp_.Rotate(90.0f, renderer::RotationType::degrees);
gizmoUp_.SetColor({0.0f, 1.0f, 0.0f, 1.0f});

gizmoSide_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png");
gizmoSide_.SetSpriteTextured(glm::vec3{}, {96, 32}, "arrow.png",
renderer::SpriteType::alwaysOnTop);
gizmoSide_.SetColor({1.0f, 0.0f, 0.0f, 1.0f});

centerInitialSize_ = {{16, 16}, {96, 96}};
Expand Down Expand Up @@ -57,7 +61,7 @@ Gizmo::Update(const glm::vec2& centeredPos, float rotation)
gizmoCenter_.Rotate(currentRotation_);
}

gizmoCenter_.SetInitialPosition(glm::vec3{centeredPos, 0.0f});
gizmoCenter_.SetInitialPosition(glm::vec3{centeredPos, renderer::LAYER_0});

gizmoUp_.SetInitialPosition(
gizmoCenter_.GetPosition()
Expand Down
2 changes: 1 addition & 1 deletion editor/gui/editor_gui_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ EditorGUI::RenderGameObjectContent()

DrawWidget("Rotate", [&gameObject]() {
auto rotation =
gameObject.GetSprite().GetRotation(renderer::Sprite::RotationType::DEGREES);
gameObject.GetSprite().GetRotation(renderer::RotationType::degrees);
if (ImGui::InputFloat("##Rotate", &rotation,
glm::degrees(renderer::Sprite::ROTATION_RANGE.first),
glm::degrees(renderer::Sprite::ROTATION_RANGE.second)))
Expand Down
5 changes: 3 additions & 2 deletions engine/game/enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Enemy::Enemy(Application* context, const glm::vec3& pos, const glm::ivec2& size,
Enemy::Enemy(Application* context, const glm::vec2& pos, const glm::ivec2& size,
const std::string& textureName, const std::vector< AnimationPoint >& keypoints,
Animatable::ANIMATION_TYPE animationType)
: Enemy(context, glm::vec3{pos, 0.0f}, size, textureName, keypoints, animationType)
: Enemy(context, glm::vec3{pos, renderer::LAYER_1}, size, textureName, keypoints, animationType)
{
}

Expand All @@ -40,7 +40,8 @@ Enemy::Setup(Application* context, const glm::vec2& pos, const glm::ivec2& size,
const std::string& textureName, const std::vector< AnimationPoint >& keypoints,
Animatable::ANIMATION_TYPE animationType)
{
GameObject::Setup(context, glm::vec3{pos, 0.0f}, size, textureName, ObjectType::ENEMY);
GameObject::Setup(context, glm::vec3{pos, renderer::LAYER_1}, size, textureName,
ObjectType::ENEMY);
SetAnimationType(animationType);

initialPosition_ = currentGameObjectState_.position_;
Expand Down
4 changes: 2 additions & 2 deletions engine/game/game_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ GameObject::GameObject(Application* application, const glm::vec3& position, cons
switch (type)
{
case looper::ObjectType::OBJECT: {
newPosition.z = 0.1f;
newPosition.z = renderer::LAYER_2;
}
break;

Expand Down Expand Up @@ -57,7 +57,7 @@ GameObject::Setup(Application* application, const glm::vec3& position, const glm
switch (type)
{
case looper::ObjectType::OBJECT: {
newPosition.z = 0.1f;
newPosition.z = renderer::LAYER_2;
}
break;

Expand Down
18 changes: 9 additions & 9 deletions engine/game/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Level::Create(Application* context, const std::string& name, const glm::ivec2& s
levelSize_ = size;

background_.SetSpriteTextured(glm::vec3(static_cast< float >(levelSize_.x) / 2.0f,
static_cast< float >(levelSize_.y) / 2.0f, 0.3f),
static_cast< float >(levelSize_.y) / 2.0f, renderer::LAYER_10),
size, "white.png");

contextPointer_ = context;
Expand Down Expand Up @@ -64,10 +64,9 @@ Level::Load(Application* context, const std::string& pathToLevel)
// const auto weapons = json[key]["weapons"];
const auto& name = player["name"];

player_.Setup(context, glm::vec3(position[0], position[1], 0.0f),
player_.Setup(context, glm::vec3(position[0], position[1], renderer::LAYER_1),
glm::ivec2(size[0], size[1]), texture, name);
player_.Rotate(player["rotation"]);
player_.GetSprite().ChangeRenderLayer(player["render_layer"]);
}

// ENEMIES
Expand All @@ -87,11 +86,10 @@ Level::Load(Application* context, const std::string& pathToLevel)
const auto& name = enemy["name"];

auto& object = enemies_.at(i);
object.Setup(context, glm::vec3(position[0], position[1], 0.0f),
object.Setup(context, glm::vec3(position[0], position[1], renderer::LAYER_1),
glm::ivec2(size[0], size[1]), texture, std::vector< AnimationPoint >{});
object.SetName(name);
object.Rotate(enemy["rotation"]);
object.GetSprite().ChangeRenderLayer(enemy["render_layer"]);

std::vector< AnimationPoint > keypointsPositions = {};
glm::vec2 beginPoint = glm::vec2(position[0], position[1]);
Expand Down Expand Up @@ -127,12 +125,13 @@ Level::Load(Application* context, const std::string& pathToLevel)
const auto& name = object["name"];

auto& gameObject = objects_.at(i);
gameObject.Setup(context, glm::vec3(position[0], position[1], 0.0f),
glm::ivec2(size[0], size[1]), texture, ObjectType::OBJECT);
gameObject.Setup(
context,
glm::vec3(position[0], position[1], renderer::LAYERS.at(object["render_layer"])),
glm::ivec2(size[0], size[1]), texture, ObjectType::OBJECT);
objectToIdx_[gameObject.GetID()] = i;
gameObject.SetName(name);
gameObject.Rotate(object["rotation"]);
gameObject.GetSprite().ChangeRenderLayer(object["render_layer"]);
gameObject.SetHasCollision(object["has collision"]);
}
}
Expand Down Expand Up @@ -564,7 +563,8 @@ Level::LoadPremade(const std::string& fileName, const glm::ivec2& size)
levelSize_ = size;

background_.SetSpriteTextured(glm::vec3(static_cast< float >(levelSize_.x) / 2.0f,
static_cast< float >(levelSize_.y) / 2.0f, 0.5f),
static_cast< float >(levelSize_.y) / 2.0f,
renderer::LAYER_10),
size, fileName);

baseTexture_ = background_.GetTexture()->GetID();
Expand Down
2 changes: 1 addition & 1 deletion engine/game/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Player::Player(Application* game, const glm::vec3& position, const glm::ivec2& s

Player::Player(Application* game, const glm::vec2& position, const glm::ivec2& size,
const std::string& sprite, const std::string& name)
: Player(game, glm::vec3{position, 0.0f}, size, sprite, name)
: Player(game, glm::vec3{position, 0.01f}, size, sprite, name)
{
}

Expand Down
Loading

0 comments on commit 134a779

Please sign in to comment.