Skip to content

Commit

Permalink
Merge pull request #63 from CapsCollective/feature/tilemap-example
Browse files Browse the repository at this point in the history
Created a tilemap example for testing texture atlases
  • Loading branch information
Raelr committed Nov 14, 2023
2 parents 449e1c0 + 6d208fc commit 8f92f40
Show file tree
Hide file tree
Showing 16 changed files with 206 additions and 25 deletions.
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,16 @@ export renderLib := $(libDir)/librender.a
export testApp := $(binDir)/tests/build/app
export exampleGameApp := $(binDir)/examples/game/build/app
export exampleRenderApp := $(binDir)/examples/render/build/app
export exampleTilemapApp := $(binDir)/examples/tilemap/build/app

# Set build vars
export compileFlags := -Wall -std=c++17
export linkFlags += -L $(libDir)
buildFlagsFile:=.buildflags

.PHONY: all testapp gameapp renderapp package-gameapp package-renderapp buildFlags clean format
.PHONY: all testapp gameapp renderapp tilemapapp package-gameapp package-renderapp package-tilemapapp buildFlags clean format

all: testapp package-gameapp package-renderapp
all: testapp package-gameapp package-renderapp package-tilemapapp

$(utilsLib): buildFlags
"$(MAKE)" -C $(engineDir)/utils CXXFLAGS="$(CXXFLAGS)"
Expand All @@ -78,18 +79,26 @@ $(exampleGameApp): buildFlags $(renderLib) $(coreLib)
$(exampleRenderApp): buildFlags $(renderLib)
"$(MAKE)" -C $(examplesDir)/render CXXFLAGS="$(CXXFLAGS)"

$(exampleTilemapApp): buildFlags $(renderLib)
"$(MAKE)" -C $(examplesDir)/tilemap CXXFLAGS="$(CXXFLAGS)"

testapp: $(testApp)

gameapp: $(exampleGameApp)

renderapp: $(exampleRenderApp)

tilemapapp: $(exampleTilemapApp)

package-gameapp: gameapp
"$(MAKE)" package -C $(examplesDir)/game CXXFLAGS="$(CXXFLAGS)"

package-renderapp: renderapp
"$(MAKE)" package -C $(examplesDir)/render CXXFLAGS="$(CXXFLAGS)"

package-tilemapapp: tilemapapp
"$(MAKE)" package -C $(examplesDir)/tilemap CXXFLAGS="$(CXXFLAGS)"

# Check to invalidate the build if flags have changed
buildFlags:
$(call BUILD_FLAGS_SCRIPT, $(buildFlagsFile), $(CXXFLAGS), $(libDir) $(binDir))
Expand Down
4 changes: 2 additions & 2 deletions engine/render/renderer/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void Renderer::DrawFrame()

void Renderer::RecreateSwapChain()
{
ClearDeviceQueue();
ClearQueues();
auto extent = window.GetExtents();

while (!window.IsVisible() || extent.width == 0.0 || extent.height == 0.0)
Expand Down Expand Up @@ -175,7 +175,7 @@ void Renderer::EndFrame()
renderer2D.Flush();
}

void Renderer::ClearDeviceQueue()
void Renderer::ClearQueues()
{
Vulkan::Context::GetCurrentDevice()->WaitIdle();
}
Expand Down
2 changes: 1 addition & 1 deletion engine/render/renderer/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Renderer
bool StartFrame();
void EndFrame();

void ClearDeviceQueue();
void ClearQueues();

void DrawQuad(const Vec2 position,
const Vec2 scale = {1.f, 1.f},
Expand Down
3 changes: 1 addition & 2 deletions engine/render/renderer/platform/vulkan/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Font::Font(const char* filePath)
sizeof(uint8_t) * extent.width * extent.height,
extent.width,
extent.height,
Utils::TEXTURE_FILTER_LINEAR,
Texture2D::Usage::TEX_USAGE_FONT);

PopulateTextureAtlas(buffer);
Expand Down Expand Up @@ -143,8 +144,6 @@ void Font::PopulateTextureAtlas(uint8_t* buffer)
auto& glyph = glyphs[c];
float xPos {glyph.uvxMin}, yPos {glyph.uvyMin};

// TODO: Add an extent type which uses floating point numbers

glyph.uvxMin /= extent.width;
glyph.uvyMin /= extent.height;

Expand Down
25 changes: 16 additions & 9 deletions engine/render/renderer/platform/vulkan/Texture2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "Context.h"
#include "render/renderer/buffer/Buffer.h"
#include "utils/Descriptor.h"
#include "utils/TypeAdaptor.h"

#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
Expand All @@ -21,11 +22,12 @@

namespace Siege::Vulkan
{
Texture2D::Texture2D(const char* name, Usage texUsage)
Texture2D::Texture2D(const char* name, Utils::TextureFilter filter, Usage texUsage)
{
LoadTexture(Constants::DEFAULT_TEXTURE_2D, Constants::DEFAULT_TEXTURE_SIZE, 16, 16, texUsage);

VkSamplerCreateInfo samplerInfo = Utils::Descriptor::SamplerCreateInfo(VK_FILTER_LINEAR);
VkSamplerCreateInfo samplerInfo =
Utils::Descriptor::SamplerCreateInfo(Utils::ToVkFilter(filter));

info = {image.GetInfo()};

Expand All @@ -37,11 +39,12 @@ Texture2D::Texture2D(const char* name, Usage texUsage)
info.usage = texUsage;
}

Texture2D::Texture2D(const char* name, const char* filePath)
Texture2D::Texture2D(const char* name, const char* filePath, Utils::TextureFilter filter)
{
LoadFromFile(filePath);

VkSamplerCreateInfo samplerInfo = Utils::Descriptor::SamplerCreateInfo(VK_FILTER_LINEAR);
VkSamplerCreateInfo samplerInfo =
Utils::Descriptor::SamplerCreateInfo(Utils::ToVkFilter(filter));

info = {image.GetInfo()};

Expand All @@ -55,11 +58,13 @@ Texture2D::Texture2D(const char* name,
size_t size,
uint32_t width,
uint32_t height,
Utils::TextureFilter filter,
Usage texUsage)
{
LoadTexture(pixels, size, width, height, texUsage);

VkSamplerCreateInfo samplerInfo = Utils::Descriptor::SamplerCreateInfo(VK_FILTER_LINEAR);
VkSamplerCreateInfo samplerInfo =
Utils::Descriptor::SamplerCreateInfo(Utils::ToVkFilter(filter));

info = {image.GetInfo()};

Expand Down Expand Up @@ -91,6 +96,10 @@ void Texture2D::LoadFromFile(const char* filePath)
int texWidth, texHeight, texChannels;

stbi_uc* pixels = stbi_load(filePath, &texWidth, &texHeight, &texChannels, STBI_rgb_alpha);
defer([pixels] {
stbi_image_free(pixels);
;
});

CC_ASSERT(pixels, "Failed to load image file!")

Expand All @@ -104,6 +113,8 @@ void Texture2D::LoadFromFile(const char* filePath)
memcpy(pixelPtr, pixels, sizeof(uint8_t) * imageSize);

Buffer::Buffer stagingBuffer;
defer([&stagingBuffer] { Buffer::DestroyBuffer(stagingBuffer); });

Buffer::CreateBuffer(imageSize,
VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
// specifies that data is accessible on the CPU.
Expand All @@ -115,8 +126,6 @@ void Texture2D::LoadFromFile(const char* filePath)

Buffer::CopyData(stagingBuffer, imageSize, pixelPtr);

stbi_image_free(pixels);

extent = {static_cast<uint32_t>(texWidth), static_cast<uint32_t>(texHeight)};

Utils::Extent3D imageExtent {static_cast<uint32_t>(texWidth),
Expand All @@ -125,8 +134,6 @@ void Texture2D::LoadFromFile(const char* filePath)
image = Image({Utils::RGBASRGB, imageExtent, Vulkan::Utils::USAGE_TEXTURE, 1, 1});

image.CopyBuffer(stagingBuffer.buffer, imageExtent);

Buffer::DestroyBuffer(stagingBuffer);
}

void Texture2D::LoadTexture(const uint8_t* pixels,
Expand Down
9 changes: 7 additions & 2 deletions engine/render/renderer/platform/vulkan/Texture2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,26 @@ class Texture2D
* default white texture
* @param name the name of the texture being created
*/
Texture2D(const char* name, Usage texUsage = TEX_USAGE_TEX2D);
Texture2D(const char* name,
Utils::TextureFilter filter = Utils::TEXTURE_FILTER_LINEAR,
Usage texUsage = TEX_USAGE_TEX2D);

/**
* The texture constructor used for loading a texture from file. This loads data from a given
* file path and stores pixel data in the class
* @param name the name of the texture
* @param filePath the path of the PNG or JPG file
*/
Texture2D(const char* name, const char* filePath);
Texture2D(const char* name,
const char* filePath,
Utils::TextureFilter filter = Utils::TEXTURE_FILTER_LINEAR);

Texture2D(const char* name,
const uint8_t* pixels,
size_t size,
uint32_t width,
uint32_t height,
Utils::TextureFilter filter = Utils::TEXTURE_FILTER_LINEAR,
Usage texUsage = Usage::TEX_USAGE_TEX2D);

/**
Expand Down
15 changes: 15 additions & 0 deletions engine/render/renderer/platform/vulkan/utils/TypeAdaptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,21 @@ DECL_VULKAN_SWITCH_FUN(VertexInputRate,
VK_VERTEX_INPUT_RATE_INSTANCE,
INPUT_RATE_INSTANCE) SWITCH_DEFAULT(INPUT_RATE_VERTEX))

DECL_VULKAN_SWITCH_FUN(VkFilter,
TextureFilter,
SWITCH_MEM(TextureFilter, TEXTURE_FILTER_NEAREST, VK_FILTER_NEAREST)
SWITCH_MEM(TextureFilter, TEXTURE_FILTER_LINEAR, VK_FILTER_LINEAR)
SWITCH_MEM(TextureFilter,
TEXTURE_FILTER_MAX_ENUM,
VK_FILTER_MAX_ENUM) SWITCH_DEFAULT(VK_FILTER_CUBIC_EXT))

DECL_VULKAN_SWITCH_FUN(TextureFilter,
VkFilter,
SWITCH_MEM(VkFilter, VK_FILTER_NEAREST, TEXTURE_FILTER_NEAREST)
SWITCH_MEM(VkFilter, VK_FILTER_LINEAR, TEXTURE_FILTER_LINEAR)
SWITCH_MEM(VkFilter, VK_FILTER_MAX_ENUM, TEXTURE_FILTER_MAX_ENUM)
SWITCH_DEFAULT(TEXTURE_FILTER_CUBIC_EXT))

//----------------------------------------- Structs -----------------------------------------------

DECL_CONVERSION_FUN(VkExtent2D, Extent2D, {GET_MEMBER(width), GET_MEMBER(height)})
Expand Down
9 changes: 9 additions & 0 deletions engine/render/renderer/platform/vulkan/utils/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ enum MemoryProperty
MEMORY_HOST_VISIBLE = 0x00000004
};

enum TextureFilter
{
TEXTURE_FILTER_NEAREST = 0,
TEXTURE_FILTER_LINEAR = 1,
TEXTURE_FILTER_CUBIC_EXT = 1000015000,
TEXTURE_FILTER_CUBIC_IMG = TEXTURE_FILTER_CUBIC_EXT,
TEXTURE_FILTER_MAX_ENUM = 0x7FFFFFFF
};

//----------------------------------------- Structs -----------------------------------------------

struct Extent2D
Expand Down
4 changes: 0 additions & 4 deletions engine/utils/Macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,4 @@
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)

// Macro for declaring a binary operator
#define DECL_BINARY_OP_NO_IMPL(op, returnType, lhsType, rhsType) \
returnType operator op(lhsType lhs, rhsType rhs) _SEMICOLON

#endif // SIEGE_ENGINE_MACROS_H
1 change: 1 addition & 0 deletions engine/utils/collections/ArrayUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cstdint>
#include <cstring>
#include <initializer_list>
#include <iterator>
#include <utility>

namespace Siege
Expand Down
2 changes: 1 addition & 1 deletion examples/game/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ int main(int argc, char* argv[])
Siege::Statics::Scene().LoadNextScene();
}

renderer.ClearDeviceQueue();
renderer.ClearQueues();

return 0;
}
3 changes: 1 addition & 2 deletions examples/render/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// https://opensource.org/licenses/Zlib
//

#include <GLFW/glfw3.h>
#include <render/renderer/ObjectLoader.h>
#include <render/renderer/Renderer.h>
#include <render/renderer/platform/vulkan/Material.h>
Expand Down Expand Up @@ -369,7 +368,7 @@ int main()
renderer.EndFrame();
}

renderer.ClearDeviceQueue();
renderer.ClearQueues();

return 0;
}
79 changes: 79 additions & 0 deletions examples/tilemap/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright (c) 2020-present Caps Collective & contributors
# Originally authored by Jonathan Moallem (@jonjondev) & Aryeh Zinn (@Raelr)
#
# This code is released under an unmodified zlib license.
# For conditions of distribution and use, please see:
# https://opensource.org/licenses/Zlib

include $(makeDir)/Functions.mk
include $(makeDir)/Platform.mk

# Set source build vars
exampleTilemapSrcDir := .
exampleTilemapBinDir := $(binDir)/examples/tilemap
exampleTilemapSources := $(call rwildcard,$(exampleTilemapSrcDir)/,*.cpp)
exampleTilemapObjects := $(call findobjs,$(exampleTilemapSrcDir),$(exampleTilemapBinDir),$(exampleTilemapSources))
exampleTilemapDepends := $(patsubst %.o, %.d, $(call rwildcard,$(exampleTilemapBinDir)/,*.o))
exampleTilemapBuildDir := $(exampleTilemapBinDir)/build
exampleTilemapOutputName := "Tilemap Example"

# Set packaging vars
ifeq ($(platform), linux)
packagingEnvVars := \
export DYLD_LIBRARY_PATH='./lib'; \
export VK_LAYER_PATH='./lib/explicit_layer.d';
else ifeq ($(platform), macos)
packagingEnvVars := \
export DYLD_LIBRARY_PATH='./lib'; \
export VK_LAYER_PATH='./lib/explicit_layer.d'; \
export VK_ICD_FILENAMES='./lib/icd.d/MoltenVK_icd.json'
endif

ifneq ($(ENABLE_VALIDATION_LAYERS), 1)
packagingExcludes := $(VALIDATION_LAYERS_INSTALL_DIR)
endif

ifneq ("$(packagingEnvVars)","")
ENV_VARS = --env-vars "$(packagingEnvVars)"
endif

packagingFlags = $(ENV_VARS) --excludes "$(packagingExcludes)"

# Set build vars
compileFlags += -I $(vendorDir)/glfw/include -I $(vendorDir)/glm \
-I $(vendorDir)/tinyobjloader -I $(vendorDir)/freetype/include
linkFlags += -l render -l window -l utils

.PHONY: all render-assets tilemap-assets

all: $(exampleTilemapApp) tilemap-assets render-assets

# Link the object files and create an executable
$(exampleTilemapApp): $(renderLib) $(exampleTilemapObjects)
$(MKDIR) $(call platformpth, $(@D))
$(CXX) $(exampleTilemapObjects) -o $(exampleTilemapApp) $(linkFlags)

# Add all rules from dependency files
-include $(exampleTilemapDepends)

# Compile object files to the bin directory
$(exampleTilemapBinDir)/%.o: $(exampleTilemapSrcDir)/%.cpp
$(MKDIR) $(call platformpth, $(@D))
$(CXX) -MMD -MP -c $(compileFlags) -I $(engineDir) $< -o $@ $(CXXFLAGS)

# Copy assets directory to the build directory
tilemap-assets:
$(MKDIR) $(call platformpth, $(exampleTilemapBuildDir)/assets)
$(call COPY,$(exampleTilemapSrcDir)/assets,$(exampleTilemapBuildDir)/assets,**)

# Copy assets (and other required build files) directory to the build directory
render-assets:
$(MKDIR) $(call platformpth,$(exampleTilemapBuildDir))
$(call COPY,$(binDir)/engine/render/build,$(exampleTilemapBuildDir),**)
$(MKDIR) $(call platformpth,$(exampleTilemapBuildDir)/assets)
$(call COPY,$(exampleTilemapSrcDir)/assets,$(exampleTilemapBuildDir)/assets,**)

# Package the built application and all its assets to the output directory
package:
$(RM) "$(outputDir)/$(exampleTilemapOutputName)"
$(call PACKAGE_SCRIPT, $(exampleTilemapOutputName), $(shell basename $(exampleTilemapApp)), $(outputDir), $(exampleTilemapBuildDir), $(packagingFlags))
Binary file added examples/tilemap/assets/fonts/PublicPixel.ttf
Binary file not shown.
Binary file added examples/tilemap/assets/textures/tilemap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 8f92f40

Please sign in to comment.