Compare commits
3 Commits
458891b609
...
ce07b55c34
Author | SHA1 | Date | |
---|---|---|---|
|
ce07b55c34 | ||
|
b5bc16afcf | ||
|
de16b0acbb |
@ -1,3 +0,0 @@
|
||||
[LINK] g++ src\build\src\Engine.o src\build\src\main.o src\build\src\Renderer.o src\build\src\Components\CameraComponent.o src\build\src\Components\LightComponent.o src\build\src\Components\SpriteComponent.o src\build\src\Entitys\Object.o src\build\src\utils\FileDialog.o src\build\src\utils\Logging.o src\build\src\utils\Shader.o src\build\src\utils\utils.o src\build\vendor\imgui\imgui.o src\build\vendor\imgui\imgui_demo.o src\build\vendor\imgui\imgui_draw.o src\build\vendor\imgui\imgui_impl_glfw.o src\build\vendor\imgui\imgui_impl_opengl3.o src\build\vendor\imgui\imgui_tables.o src\build\vendor\imgui\imgui_widgets.o -o src\build\app.exe -LC:\msys64\mingw64\lib -lglfw3 -lglew32 -lopengl32 -lgdi32 -lyaml-cpp -lcomdlg32 -lssl -lcrypto
|
||||
[ERROR] Runtime crash
|
||||
Command 'src\build\app.exe' returned non-zero exit status 3221225477.
|
30
imgui.ini
30
imgui.ini
@ -10,24 +10,24 @@ Collapsed=1
|
||||
|
||||
[Window][WindowOverViewport_11111111]
|
||||
Pos=0,19
|
||||
Size=1920,1158
|
||||
Size=1280,701
|
||||
Collapsed=0
|
||||
|
||||
[Window][Inspector]
|
||||
Pos=1408,19
|
||||
Size=512,835
|
||||
Pos=768,19
|
||||
Size=512,505
|
||||
Collapsed=0
|
||||
DockId=0x00000005,0
|
||||
|
||||
[Window][Scene Tree]
|
||||
Pos=0,19
|
||||
Size=263,1158
|
||||
Size=263,701
|
||||
Collapsed=0
|
||||
DockId=0x00000001,0
|
||||
|
||||
[Window][Viewport]
|
||||
Pos=265,19
|
||||
Size=1141,869
|
||||
Size=501,590
|
||||
Collapsed=0
|
||||
DockId=0x00000007,0
|
||||
|
||||
@ -36,24 +36,30 @@ Size=1280,19
|
||||
Collapsed=0
|
||||
|
||||
[Window][Performance Info]
|
||||
Pos=1408,856
|
||||
Size=512,321
|
||||
Pos=768,526
|
||||
Size=512,194
|
||||
Collapsed=0
|
||||
DockId=0x00000006,0
|
||||
|
||||
[Window][Console]
|
||||
Pos=265,890
|
||||
Size=1141,287
|
||||
Pos=265,611
|
||||
Size=501,109
|
||||
Collapsed=0
|
||||
DockId=0x00000008,0
|
||||
|
||||
[Window][Tilemap Editor]
|
||||
Pos=265,19
|
||||
Size=1141,1047
|
||||
Collapsed=0
|
||||
DockId=0x00000007,1
|
||||
|
||||
[Docking][Data]
|
||||
DockSpace ID=0x11111111 Window=0x1BBC0F80 Pos=0,19 Size=1920,1158 Split=X
|
||||
DockSpace ID=0x11111111 Window=0x1BBC0F80 Pos=0,19 Size=1280,701 Split=X
|
||||
DockNode ID=0x00000003 Parent=0x11111111 SizeRef=1406,1158 Split=X
|
||||
DockNode ID=0x00000001 Parent=0x00000003 SizeRef=263,701 HiddenTabBar=1 Selected=0x12EF0F59
|
||||
DockNode ID=0x00000002 Parent=0x00000003 SizeRef=1141,701 Split=Y Selected=0xC450F867
|
||||
DockNode ID=0x00000007 Parent=0x00000002 SizeRef=606,869 CentralNode=1 HiddenTabBar=1 Selected=0xC450F867
|
||||
DockNode ID=0x00000008 Parent=0x00000002 SizeRef=606,287 HiddenTabBar=1 Selected=0xEA83D666
|
||||
DockNode ID=0x00000007 Parent=0x00000002 SizeRef=606,1047 CentralNode=1 Selected=0xC450F867
|
||||
DockNode ID=0x00000008 Parent=0x00000002 SizeRef=606,109 Selected=0xEA83D666
|
||||
DockNode ID=0x00000004 Parent=0x11111111 SizeRef=512,1158 Split=Y Selected=0x36DC96AB
|
||||
DockNode ID=0x00000005 Parent=0x00000004 SizeRef=407,835 HiddenTabBar=1 Selected=0x36DC96AB
|
||||
DockNode ID=0x00000006 Parent=0x00000004 SizeRef=407,321 HiddenTabBar=1 Selected=0x3FC1A724
|
||||
|
Binary file not shown.
3
remake/build.log
Normal file
3
remake/build.log
Normal file
@ -0,0 +1,3 @@
|
||||
[COMPILE] g++ -std=c++20 -Wall -Isrc/include -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -Isrc/include -Isrc/vendor -Isrc/vendor/imgui -IC:/msys64/mingw64/include -IC:\msys64\mingw64\lib\libyaml-cpp.a -Isrc\vendor\imgui -MMD -MP -c src\src\Components\TilemapComponent.cpp -o src\build\src\Components\TilemapComponent.o
|
||||
[LINK] g++ src\build\src\Engine.o src\build\src\main.o src\build\src\Renderer.o src\build\src\Components\CameraComponent.o src\build\src\Components\LightComponent.o src\build\src\Components\SpriteComponent.o src\build\src\Components\TilemapComponent.o src\build\src\Entitys\Object.o src\build\src\utils\EngineConfig.o src\build\src\utils\FileDialog.o src\build\src\utils\Logging.o src\build\src\utils\Shader.o src\build\src\utils\utils.o src\build\vendor\imgui\imgui.o src\build\vendor\imgui\imgui_demo.o src\build\vendor\imgui\imgui_draw.o src\build\vendor\imgui\imgui_impl_glfw.o src\build\vendor\imgui\imgui_impl_opengl3.o src\build\vendor\imgui\imgui_tables.o src\build\vendor\imgui\imgui_widgets.o -o src\build\app.exe -LC:\msys64\mingw64\lib -lglfw3 -lglew32 -lopengl32 -lgdi32 -lyaml-cpp -lcomdlg32 -lssl -lcrypto
|
||||
[RUN] Executed app.exe successfully.
|
@ -20,7 +20,7 @@ LIB_DIRS = [
|
||||
# Compiler and build options
|
||||
BUILD_DIR = Path("src/build")
|
||||
TARGET = BUILD_DIR / "app.exe"
|
||||
LOG_FILE = Path("build.log")
|
||||
LOG_FILE = Path("./remake/build.log")
|
||||
CACHE_FILE = Path("./remake/.remake_cache.json")
|
||||
CXX = "g++"
|
||||
CXXFLAGS = ["-std=c++20", "-Wall"] + [f"-I{inc}" for inc in INCLUDE_DIRS]
|
||||
|
@ -32,36 +32,6 @@ objects:
|
||||
texture: C:\Users\spenc\OneDrive\Pictures\49555.jpg
|
||||
normalMap: ""
|
||||
children: []
|
||||
- name: Bark
|
||||
position: [1024, 0]
|
||||
layer: -1
|
||||
components:
|
||||
- type: SpriteComponent
|
||||
texture: C:\Users\spenc\OneDrive\Pictures\textures\bark_willow_02_diff_1k.png
|
||||
normalMap: C:\Users\spenc\OneDrive\Pictures\textures\bark_willow_02_nor_gl_1k.png
|
||||
children: []
|
||||
- name: Sun
|
||||
position: [-5000, -5000]
|
||||
layer: 1
|
||||
components:
|
||||
- type: LightComponent
|
||||
color:
|
||||
- 0.990196049
|
||||
- 0.943370163
|
||||
- 0.791186035
|
||||
intensity: 2.0999999
|
||||
radius: 100000000
|
||||
falloff: 0.100000001
|
||||
type: 0
|
||||
children: []
|
||||
- name: Rocks
|
||||
position: [0, 0]
|
||||
layer: -1
|
||||
components:
|
||||
- type: SpriteComponent
|
||||
texture: C:\Users\spenc\OneDrive\Pictures\ganges_river_pebbles_diff_1k.png
|
||||
normalMap: C:\Users\spenc\OneDrive\Pictures\ganges_river_pebbles_nor_gl_1k.png
|
||||
children: []
|
||||
- name: World
|
||||
position: [-436, 248]
|
||||
layer: 0
|
||||
|
122
src/assets/scenes/tilemap.cene
Normal file
122
src/assets/scenes/tilemap.cene
Normal file
@ -0,0 +1,122 @@
|
||||
engine_version: 0.1.0
|
||||
scene_name: tilemap
|
||||
scene_hash: 0ffd3035689b66c87af235dc22993fa7dac416ee447c93a1c4594a6b8aafc289
|
||||
format_version: 1
|
||||
objects:
|
||||
- name: Hello, Create
|
||||
position: [0, 0]
|
||||
layer: 0
|
||||
components:
|
||||
- type: TilemapComponent
|
||||
gridSize:
|
||||
- 10
|
||||
- 10
|
||||
tileSize:
|
||||
- 32
|
||||
- 32
|
||||
tiles:
|
||||
- 961
|
||||
- 961
|
||||
- 961
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- 816
|
||||
- -1
|
||||
- 945
|
||||
- 970
|
||||
- 970
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- 813
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- 970
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- 748
|
||||
- 840
|
||||
- -1
|
||||
- 970
|
||||
- -1
|
||||
- 840
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- 812
|
||||
- -1
|
||||
- 805
|
||||
- -1
|
||||
- 845
|
||||
- 877
|
||||
- -1
|
||||
- 904
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- 970
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- 970
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- 875
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
- -1
|
||||
atlasPath: C:\Users\spenc\OneDrive\Pictures\6656e7221e49a1774d2fb280357e56f8d25d9d95.png
|
||||
atlasTileSize:
|
||||
- 32
|
||||
- 32
|
||||
children: []
|
@ -40,36 +40,6 @@ objects:
|
||||
texture: C:\Users\spenc\OneDrive\Pictures\blue_logo.png
|
||||
normalMap: C:\Users\spenc\OneDrive\Pictures\images.jpg
|
||||
children: []
|
||||
- name: Bark
|
||||
position: [1024, 0]
|
||||
layer: -1
|
||||
components:
|
||||
- type: SpriteComponent
|
||||
texture: C:\Users\spenc\OneDrive\Pictures\textures\bark_willow_02_diff_1k.png
|
||||
normalMap: C:\Users\spenc\OneDrive\Pictures\textures\bark_willow_02_nor_gl_1k.png
|
||||
children: []
|
||||
- name: Sun
|
||||
position: [-5000, -5000]
|
||||
layer: 1
|
||||
components:
|
||||
- type: LightComponent
|
||||
color:
|
||||
- 0.992156863
|
||||
- 0.984313726
|
||||
- 0.827450991
|
||||
intensity: 1.25
|
||||
radius: 100000000
|
||||
falloff: 0.100000001
|
||||
type: 0
|
||||
children: []
|
||||
- name: Rocks
|
||||
position: [0, 0]
|
||||
layer: -1
|
||||
components:
|
||||
- type: SpriteComponent
|
||||
texture: C:\Users\spenc\OneDrive\Pictures\ganges_river_pebbles_diff_1k.png
|
||||
normalMap: C:\Users\spenc\OneDrive\Pictures\ganges_river_pebbles_nor_gl_1k.png
|
||||
children: []
|
||||
- name: World
|
||||
position: [-436, 248]
|
||||
layer: 0
|
||||
|
12
src/assets/shaders/tilemap.frag
Normal file
12
src/assets/shaders/tilemap.frag
Normal file
@ -0,0 +1,12 @@
|
||||
#version 330 core
|
||||
in vec2 vUV;
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform sampler2D uTex;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec4 tex = texture(uTex, vUV);
|
||||
if (tex.a < 0.1) discard;
|
||||
FragColor = tex;
|
||||
}
|
20
src/assets/shaders/tilemap.vert
Normal file
20
src/assets/shaders/tilemap.vert
Normal file
@ -0,0 +1,20 @@
|
||||
#version 330 core
|
||||
layout(location = 0) in vec2 aPos;
|
||||
layout(location = 1) in vec2 aUV;
|
||||
|
||||
out vec2 vUV;
|
||||
|
||||
uniform vec2 uPos;
|
||||
uniform vec2 uSize;
|
||||
uniform vec2 uScreen;
|
||||
uniform vec2 uUVMin;
|
||||
uniform vec2 uUVMax;
|
||||
|
||||
void main()
|
||||
{
|
||||
vec2 pos = uPos + aPos * uSize;
|
||||
gl_Position = vec4((pos / uScreen) * 2.0 - 1.0, 0.0, 1.0);
|
||||
|
||||
// Remap UV to tile slice
|
||||
vUV = mix(uUVMin, uUVMax, aUV);
|
||||
}
|
12
src/assets/shaders/unlit.frag
Normal file
12
src/assets/shaders/unlit.frag
Normal file
@ -0,0 +1,12 @@
|
||||
#version 330 core
|
||||
in vec2 vUV;
|
||||
out vec4 FragColor;
|
||||
uniform sampler2D uTex;
|
||||
void main() {
|
||||
vec4 color = texture(uTex, vUV);
|
||||
|
||||
if (color.a < 0.01)
|
||||
discard;
|
||||
|
||||
FragColor = color;
|
||||
}
|
13
src/assets/shaders/unlit.vert
Normal file
13
src/assets/shaders/unlit.vert
Normal file
@ -0,0 +1,13 @@
|
||||
#version 330 core
|
||||
layout (location = 0) in vec2 aPos;
|
||||
layout (location = 1) in vec2 aUV;
|
||||
out vec2 vUV;
|
||||
uniform vec2 uPos;
|
||||
uniform vec2 uSize;
|
||||
uniform vec2 uScreen;
|
||||
void main() {
|
||||
vec2 worldPos = aPos * uSize + uPos;
|
||||
vUV = aUV;
|
||||
vec2 ndc = (worldPos / uScreen) * 2.0 - 1.0;
|
||||
gl_Position = vec4(ndc.x, -ndc.y, 0.0, 1.0);
|
||||
}
|
262
src/src/Components/TilemapComponent.cpp
Normal file
262
src/src/Components/TilemapComponent.cpp
Normal file
@ -0,0 +1,262 @@
|
||||
#include "TilemapComponent.h"
|
||||
#include "../utils/Logging.h"
|
||||
#include "../utils/FileDialog.h"
|
||||
#include "../utils/utils.h"
|
||||
#include <stb_image.h>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
static int g_selectedTileIndex = 0;
|
||||
|
||||
|
||||
// Constructor: initialize with a default grid (optional)
|
||||
TilemapComponent::TilemapComponent(Object *owner)
|
||||
: Component(owner)
|
||||
{
|
||||
// For example, create an empty 10x10 tilemap
|
||||
SetGridSize(glm::ivec2(10, 10));
|
||||
// Optionally set a default tile size
|
||||
m_TileSize = glm::ivec2(32, 32);
|
||||
}
|
||||
|
||||
// Set grid dimensions and resize tile array; initially all -1 (empty)
|
||||
void TilemapComponent::SetGridSize(const glm::ivec2 &size)
|
||||
{
|
||||
m_GridSize = size;
|
||||
m_Tiles.assign(size.x * size.y, -1);
|
||||
}
|
||||
|
||||
// Set individual tile at (x, y)
|
||||
void TilemapComponent::SetTile(int x, int y, int tileIndex)
|
||||
{
|
||||
if (x < 0 || y < 0 || x >= m_GridSize.x || y >= m_GridSize.y)
|
||||
return;
|
||||
m_Tiles[y * m_GridSize.x + x] = tileIndex;
|
||||
}
|
||||
|
||||
int TilemapComponent::GetTile(int x, int y) const
|
||||
{
|
||||
if (x < 0 || y < 0 || x >= m_GridSize.x || y >= m_GridSize.y)
|
||||
return -1;
|
||||
return m_Tiles[y * m_GridSize.x + x];
|
||||
}
|
||||
|
||||
void TilemapComponent::SetTileSize(const glm::ivec2 &size)
|
||||
{
|
||||
m_TileSize = size;
|
||||
}
|
||||
|
||||
// Set atlas (tileset) settings; also update dimensions
|
||||
void TilemapComponent::SetAtlas(const std::string &path, int atlasTileWidth, int atlasTileHeight)
|
||||
{
|
||||
m_AtlasPath = path;
|
||||
m_AtlasTileWidth = atlasTileWidth;
|
||||
m_AtlasTileHeight = atlasTileHeight;
|
||||
UpdateAtlasDimensions();
|
||||
}
|
||||
|
||||
// Helper: load atlas image info using stbi_info to compute columns and rows
|
||||
void TilemapComponent::UpdateAtlasDimensions()
|
||||
{
|
||||
if (m_AtlasPath.empty())
|
||||
return;
|
||||
int texW, texH, comp;
|
||||
if (stbi_info(m_AtlasPath.c_str(), &texW, &texH, &comp))
|
||||
{
|
||||
m_AtlasCols = texW / m_AtlasTileWidth;
|
||||
m_AtlasRows = texH / m_AtlasTileHeight;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_AtlasCols = m_AtlasRows = 0;
|
||||
Logger::LogError("Failed to get atlas info: %s", m_AtlasPath.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool TilemapComponent::IsTileFullyTransparent(int col, int row, int texW, int texH, unsigned char* imageData)
|
||||
{
|
||||
int x0 = col * m_AtlasTileWidth;
|
||||
int y0 = row * m_AtlasTileHeight;
|
||||
|
||||
for (int y = 0; y < m_AtlasTileHeight; ++y)
|
||||
{
|
||||
for (int x = 0; x < m_AtlasTileWidth; ++x)
|
||||
{
|
||||
int px = x0 + x;
|
||||
int py = y0 + y;
|
||||
int idx = (py * texW + px) * 4;
|
||||
if (imageData[idx + 3] > 0)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Serialization: store grid size, tile size, tile data, and atlas settings.
|
||||
void TilemapComponent::Save(YAML::Emitter &out) const
|
||||
{
|
||||
out << YAML::BeginMap;
|
||||
out << YAML::Key << "type" << YAML::Value << "TilemapComponent";
|
||||
out << YAML::Key << "gridSize" << YAML::Value << std::vector<int>{m_GridSize.x, m_GridSize.y};
|
||||
out << YAML::Key << "tileSize" << YAML::Value << std::vector<int>{m_TileSize.x, m_TileSize.y};
|
||||
out << YAML::Key << "tiles" << YAML::Value << m_Tiles;
|
||||
out << YAML::Key << "atlasPath" << YAML::Value << m_AtlasPath;
|
||||
out << YAML::Key << "atlasTileSize" << YAML::Value << std::vector<int>{m_AtlasTileWidth, m_AtlasTileHeight};
|
||||
out << YAML::EndMap;
|
||||
}
|
||||
|
||||
// Deserialization: load saved data
|
||||
void TilemapComponent::Load(const YAML::Node &node)
|
||||
{
|
||||
if (node["gridSize"])
|
||||
{
|
||||
auto vec = node["gridSize"].as<std::vector<int>>();
|
||||
if (vec.size() == 2)
|
||||
m_GridSize = glm::ivec2(vec[0], vec[1]);
|
||||
}
|
||||
if (node["tileSize"])
|
||||
{
|
||||
auto vec = node["tileSize"].as<std::vector<int>>();
|
||||
if (vec.size() == 2)
|
||||
m_TileSize = glm::ivec2(vec[0], vec[1]);
|
||||
}
|
||||
if (node["tiles"])
|
||||
m_Tiles = node["tiles"].as<std::vector<int>>();
|
||||
if (node["atlasPath"])
|
||||
m_AtlasPath = node["atlasPath"].as<std::string>();
|
||||
if (node["atlasTileSize"])
|
||||
{
|
||||
auto vec = node["atlasTileSize"].as<std::vector<int>>();
|
||||
if (vec.size() == 2)
|
||||
{
|
||||
m_AtlasTileWidth = vec[0];
|
||||
m_AtlasTileHeight = vec[1];
|
||||
}
|
||||
}
|
||||
// Update atlas dimensions after loading settings
|
||||
UpdateAtlasDimensions();
|
||||
}
|
||||
|
||||
void TilemapComponent::DrawEditorUI()
|
||||
{
|
||||
ImGui::Begin("Tilemap Editor");
|
||||
|
||||
if (ImGui::BeginTabBar("TilemapEditorTabs"))
|
||||
{
|
||||
// --- Atlas Settings ---
|
||||
if (ImGui::BeginTabItem("Atlas Settings"))
|
||||
{
|
||||
ImGui::Text("Current Atlas: %s", m_AtlasPath.empty() ? "None" : m_AtlasPath.c_str());
|
||||
if (ImGui::Button("Import Atlas"))
|
||||
{
|
||||
std::string path = OpenFileDialog(FileDialogType::Images);
|
||||
if (!path.empty())
|
||||
{
|
||||
m_AtlasPath = path;
|
||||
UpdateAtlasDimensions();
|
||||
}
|
||||
}
|
||||
|
||||
int atlasW = m_AtlasTileWidth, atlasH = m_AtlasTileHeight;
|
||||
if (ImGui::InputInt("Atlas Tile Width", &atlasW))
|
||||
{
|
||||
m_AtlasTileWidth = atlasW;
|
||||
UpdateAtlasDimensions();
|
||||
}
|
||||
if (ImGui::InputInt("Atlas Tile Height", &atlasH))
|
||||
{
|
||||
m_AtlasTileHeight = atlasH;
|
||||
UpdateAtlasDimensions();
|
||||
}
|
||||
|
||||
ImGui::Text("Atlas Grid: %d x %d", m_AtlasCols, m_AtlasRows);
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
|
||||
// --- Map Settings ---
|
||||
if (ImGui::BeginTabItem("Map Settings"))
|
||||
{
|
||||
ImGui::Text("Map Size: %d x %d", m_GridSize.x, m_GridSize.y);
|
||||
ImGui::Text("Tile Size: %d x %d", m_TileSize.x, m_TileSize.y);
|
||||
ImGui::Text("Tile Count: %zu", m_Tiles.size());
|
||||
|
||||
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
|
||||
// --- Tile Tools ---
|
||||
if (ImGui::BeginTabItem("Tiles"))
|
||||
{
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
if (m_AtlasPath.empty())
|
||||
{
|
||||
ImGui::Text("No atlas loaded.");
|
||||
}
|
||||
else
|
||||
{
|
||||
int texW, texH, comp;
|
||||
unsigned char* imageData = stbi_load(m_AtlasPath.c_str(), &texW, &texH, &comp, 4);
|
||||
if (imageData)
|
||||
{
|
||||
float uvTileW = float(m_AtlasTileWidth) / float(texW);
|
||||
float uvTileH = float(m_AtlasTileHeight) / float(texH);
|
||||
GLuint textureID = LoadTextureIfNeeded(m_AtlasPath);
|
||||
|
||||
const float tileDisplaySize = 48;
|
||||
ImGui::BeginChild("TilesetView");
|
||||
for (int row = 0; row < m_AtlasRows; ++row)
|
||||
{
|
||||
for (int col = 0; col < m_AtlasCols; ++col)
|
||||
{
|
||||
int tileIndex = row * m_AtlasCols + col;
|
||||
float uvX = float(col) * uvTileW;
|
||||
float uvY = float(row) * uvTileH;
|
||||
ImVec2 uv0(uvX, uvY);
|
||||
ImVec2 uv1(uvX + uvTileW, uvY + uvTileH);
|
||||
|
||||
char id[32];
|
||||
sprintf(id, "##tile_%d", tileIndex);
|
||||
|
||||
bool isEmpty = IsTileFullyTransparent(col, row, texW, texH, imageData);
|
||||
ImVec4 tint = isEmpty ? ImVec4(0.3f, 0.3f, 0.3f, 0.5f) : ImVec4(1, 1, 1, 1);
|
||||
if (ImGui::ImageButton(id, (ImTextureID)(uintptr_t)textureID, ImVec2(tileDisplaySize, tileDisplaySize), uv0, uv1))
|
||||
{
|
||||
g_selectedTileIndex = tileIndex;
|
||||
}
|
||||
|
||||
if (ImGui::IsItemHovered())
|
||||
ImGui::SetTooltip("Tile %d", tileIndex);
|
||||
|
||||
ImGui::SameLine();
|
||||
}
|
||||
ImGui::NewLine();
|
||||
}
|
||||
stbi_image_free(imageData);
|
||||
ImGui::EndChild();
|
||||
}
|
||||
else
|
||||
{
|
||||
ImGui::Text("Failed to load image.");
|
||||
}
|
||||
|
||||
ImGui::Text("Selected Tile: %d", g_selectedTileIndex);
|
||||
}
|
||||
|
||||
ImGui::EndTabItem();
|
||||
}
|
||||
|
||||
ImGui::EndTabBar();
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
58
src/src/Components/TilemapComponent.h
Normal file
58
src/src/Components/TilemapComponent.h
Normal file
@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
#include "Component.h"
|
||||
#include <glm/glm.hpp>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <imgui.h>
|
||||
|
||||
class TilemapComponent : public Component
|
||||
{
|
||||
public:
|
||||
TilemapComponent(Object* owner);
|
||||
|
||||
// Editor UI for tilemap
|
||||
void DrawEditorUI();
|
||||
|
||||
// Set up grid and tile dimensions
|
||||
void SetGridSize(const glm::ivec2& size);
|
||||
void SetTileSize(const glm::ivec2& size);
|
||||
void SetTile(int x, int y, int tileIndex);
|
||||
int GetTile(int x, int y) const;
|
||||
|
||||
// Getters
|
||||
const glm::ivec2& GetGridSize() const { return m_GridSize; }
|
||||
const glm::ivec2& GetTileSize() const { return m_TileSize; }
|
||||
const std::vector<int>& GetTiles() const { return m_Tiles; }
|
||||
const std::string& GetAtlasPath() const { return m_AtlasPath; }
|
||||
|
||||
int GetAtlasTileWidth() const { return m_AtlasTileWidth; }
|
||||
int GetAtlasTileHeight() const { return m_AtlasTileHeight; }
|
||||
int GetAtlasCols() const { return m_AtlasCols; }
|
||||
int GetAtlasRows() const { return m_AtlasRows; }
|
||||
|
||||
// Atlas setup: import an atlas image and configure its tile dimensions.
|
||||
void SetAtlas(const std::string& path, int atlasTileWidth, int atlasTileHeight);
|
||||
|
||||
// Serialization
|
||||
void Save(YAML::Emitter& out) const override;
|
||||
void Load(const YAML::Node& node) override;
|
||||
std::string GetName() const override { return "TilemapComponent"; }
|
||||
|
||||
private:
|
||||
// Grid and tile data
|
||||
glm::ivec2 m_GridSize {0, 0};
|
||||
glm::ivec2 m_TileSize {32, 32};
|
||||
std::vector<int> m_Tiles;
|
||||
|
||||
// Atlas (tileset) settings
|
||||
std::string m_AtlasPath;
|
||||
int m_AtlasTileWidth = 32;
|
||||
int m_AtlasTileHeight = 32;
|
||||
int m_AtlasCols = 0;
|
||||
int m_AtlasRows = 0;
|
||||
|
||||
void UpdateAtlasDimensions();
|
||||
bool IsTileFullyTransparent(int col, int row, int texW, int texH, unsigned char* imageData);
|
||||
|
||||
};
|
@ -4,6 +4,8 @@
|
||||
#include "components/SpriteComponent.h"
|
||||
#include "components/CameraComponent.h"
|
||||
#include "components/LightComponent.h"
|
||||
#include "components/TilemapComponent.h"
|
||||
|
||||
|
||||
#include "utils/FileDialog.h"
|
||||
#include "utils/Logging.h"
|
||||
@ -162,6 +164,11 @@ void DrawInspectorUI(std::shared_ptr<Object> selected)
|
||||
if (!selected->GetComponent<LightComponent>())
|
||||
selected->AddComponent<LightComponent>();
|
||||
}
|
||||
if (ImGui::Button("Add TilemapComponent"))
|
||||
{
|
||||
if (!selected->GetComponent<TilemapComponent>())
|
||||
selected->AddComponent<TilemapComponent>();
|
||||
}
|
||||
|
||||
// Sprite UI...
|
||||
if (auto sprite = selected->GetComponent<SpriteComponent>())
|
||||
@ -230,7 +237,23 @@ void DrawInspectorUI(std::shared_ptr<Object> selected)
|
||||
selected->RemoveComponent<LightComponent>();
|
||||
}
|
||||
|
||||
if (auto tilemap = selected->GetComponent<TilemapComponent>())
|
||||
{
|
||||
ImGui::SeparatorText("Tilemap Component");
|
||||
|
||||
ImGui::Text("Refer to Tilemap Editor Window");
|
||||
|
||||
if (ImGui::Button("Remove TilemapComponent"))
|
||||
selected->RemoveComponent<TilemapComponent>();
|
||||
}
|
||||
|
||||
ImGui::End();
|
||||
|
||||
if (auto tilemap = selected->GetComponent<TilemapComponent>())
|
||||
{
|
||||
tilemap->DrawEditorUI();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
@ -4,6 +4,8 @@
|
||||
#include "../Components/SpriteComponent.h"
|
||||
#include "../Components/CameraComponent.h"
|
||||
#include "../Components/LightComponent.h"
|
||||
#include "../Components/TilemapComponent.h"
|
||||
|
||||
|
||||
|
||||
#include <algorithm>
|
||||
@ -90,6 +92,9 @@ void Object::Load(const YAML::Node& node) {
|
||||
} else if (type == "LightComponent") {
|
||||
auto comp = AddComponent<LightComponent>();
|
||||
comp->Load(compNode);
|
||||
} else if (type == "TilemapComponent") {
|
||||
auto comp = AddComponent<TilemapComponent>();
|
||||
comp->Load(compNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,18 +2,22 @@
|
||||
#include "Components/SpriteComponent.h"
|
||||
#include "utils/Shader.h"
|
||||
#include "utils/Logging.h"
|
||||
#include "utils/EngineConfig.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
#include "stb_image.h"
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <glm/glm.hpp>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
static Shader spriteShader;
|
||||
static Shader unlitShader;
|
||||
|
||||
GLuint Renderer::fbo = 0;
|
||||
GLuint Renderer::textureColorBuffer = 0;
|
||||
GLuint Renderer::defaultNormalMap = 0;
|
||||
|
||||
GLuint Renderer::rbo = 0;
|
||||
GLuint Renderer::quadVAO = 0;
|
||||
GLuint Renderer::quadVBO = 0;
|
||||
@ -22,9 +26,7 @@ int Renderer::height = 720;
|
||||
int Renderer::s_DrawCalls = 0;
|
||||
std::vector<Light> Renderer::s_Lights;
|
||||
|
||||
|
||||
|
||||
|
||||
static Shader tilemapShader;
|
||||
|
||||
|
||||
void Renderer::InitQuad() {
|
||||
@ -74,7 +76,10 @@ void Renderer::Init() {
|
||||
|
||||
InitQuad();
|
||||
|
||||
// Load lit shader
|
||||
spriteShader.LoadFromFile("src/assets/shaders/sprite.vert", "src/assets/shaders/sprite.frag");
|
||||
// Load unlit shader
|
||||
unlitShader.LoadFromFile("src/assets/shaders/unlit.vert", "src/assets/shaders/unlit.frag");
|
||||
|
||||
// Create a 1x1 flat normal map (RGB: 128,128,255)
|
||||
unsigned char flatNormal[3] = { 128, 128, 255 };
|
||||
@ -115,33 +120,64 @@ void Renderer::End() {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
//void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float scale) {
|
||||
// GLuint tex = sprite->GetTextureID();
|
||||
// if (!tex) return;
|
||||
//
|
||||
// glBindTexture(GL_TEXTURE_2D, tex);
|
||||
// glBegin(GL_QUADS);
|
||||
// float size = 100.0f * scale;
|
||||
//
|
||||
// glTexCoord2f(0, 0); glVertex2f(pos.x, pos.y);
|
||||
// glTexCoord2f(1, 0); glVertex2f(pos.x + size, pos.y);
|
||||
// glTexCoord2f(1, 1); glVertex2f(pos.x + size, pos.y + size);
|
||||
// glTexCoord2f(0, 1); glVertex2f(pos.x, pos.y + size);
|
||||
// glEnd();
|
||||
//}
|
||||
|
||||
|
||||
void Renderer::ClearLights() {
|
||||
s_Lights.clear();
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Renderer::AddLight(const glm::vec2& screenPos, const glm::vec3& color, float intensity, float radius) {
|
||||
if (s_Lights.size() >= 8) return;
|
||||
s_Lights.push_back({screenPos, color, intensity, radius});
|
||||
}
|
||||
|
||||
void Renderer::DrawTilemap(TilemapComponent* tilemap, const glm::vec2& worldPos, float zoom, const glm::vec2& cameraPos) {
|
||||
if (!tilemap || tilemap->GetAtlasPath().empty()) return;
|
||||
|
||||
glm::ivec2 grid = tilemap->GetGridSize();
|
||||
glm::ivec2 tileSize = tilemap->GetTileSize();
|
||||
int cols = tilemap->GetAtlasCols();
|
||||
int rows = tilemap->GetAtlasRows();
|
||||
|
||||
const std::string& atlasPath = tilemap->GetAtlasPath();
|
||||
GLuint atlasTex = LoadTextureIfNeeded(atlasPath);
|
||||
if (atlasTex == 0) return;
|
||||
|
||||
tilemapShader.Use();
|
||||
tilemapShader.SetVec2("uScreen", glm::vec2(width, height));
|
||||
tilemapShader.SetInt("uTex", 0);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, atlasTex);
|
||||
|
||||
glBindVertexArray(quadVAO);
|
||||
|
||||
for (int y = 0; y < grid.y; ++y) {
|
||||
for (int x = 0; x < grid.x; ++x) {
|
||||
int index = tilemap->GetTile(x, y);
|
||||
if (index < 0 || index >= cols * rows) continue;
|
||||
|
||||
int atlasX = index % cols;
|
||||
int atlasY = index / cols;
|
||||
|
||||
glm::vec2 uvMin = glm::vec2(atlasX, atlasY) / glm::vec2(cols, rows);
|
||||
glm::vec2 uvMax = (glm::vec2(atlasX + 1, atlasY + 1)) / glm::vec2(cols, rows);
|
||||
|
||||
tilemapShader.SetVec2("uUVMin", uvMin);
|
||||
tilemapShader.SetVec2("uUVMax", uvMax);
|
||||
|
||||
glm::vec2 tileWorld = worldPos + glm::vec2(x * tileSize.x, y * tileSize.y);
|
||||
glm::vec2 screenPos = (tileWorld - cameraPos) * zoom + glm::vec2(width, height) * 0.5f;
|
||||
|
||||
tilemapShader.SetVec2("uPos", screenPos);
|
||||
tilemapShader.SetVec2("uSize", glm::vec2(tileSize) * zoom);
|
||||
|
||||
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
||||
s_DrawCalls++;
|
||||
}
|
||||
}
|
||||
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float zoom, glm::vec2& CameraPos) {
|
||||
if (!sprite->HasTexture()) {
|
||||
@ -149,11 +185,18 @@ void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float z
|
||||
return;
|
||||
}
|
||||
|
||||
// Choose the shader based on engine configuration
|
||||
if (g_engineConfig.lighting_enabled) {
|
||||
spriteShader.Use();
|
||||
} else {
|
||||
unlitShader.Use();
|
||||
}
|
||||
|
||||
glm::vec2 size = sprite->GetSize();
|
||||
glm::vec2 screenPos = (pos - CameraPos) * zoom + glm::vec2(width, height) * 0.5f - (size * zoom * 0.5f);
|
||||
|
||||
// Set common uniforms
|
||||
if (g_engineConfig.lighting_enabled) {
|
||||
spriteShader.SetVec2("uPos", screenPos);
|
||||
spriteShader.SetVec2("uSize", size * zoom);
|
||||
spriteShader.SetVec2("uScreen", glm::vec2(width, height));
|
||||
@ -165,19 +208,31 @@ void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float z
|
||||
spriteShader.SetFloat(("uLightIntensity[" + std::to_string(i) + "]").c_str(), s_Lights[i].intensity);
|
||||
spriteShader.SetFloat(("uLightRadius[" + std::to_string(i) + "]").c_str(), s_Lights[i].radius);
|
||||
}
|
||||
} else {
|
||||
// Unlit shader uniforms
|
||||
unlitShader.SetVec2("uPos", screenPos);
|
||||
unlitShader.SetVec2("uSize", size * zoom);
|
||||
unlitShader.SetVec2("uScreen", glm::vec2(width, height));
|
||||
}
|
||||
|
||||
// Bind the diffuse texture (common to both shaders)
|
||||
if (g_engineConfig.lighting_enabled) {
|
||||
spriteShader.SetInt("uTex", 0);
|
||||
spriteShader.SetInt("uNormalMap", 1);
|
||||
|
||||
} else {
|
||||
unlitShader.SetInt("uTex", 0);
|
||||
}
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, sprite->GetTextureID());
|
||||
|
||||
if (g_engineConfig.lighting_enabled) {
|
||||
spriteShader.SetInt("uNormalMap", 1);
|
||||
glActiveTexture(GL_TEXTURE1);
|
||||
if (sprite->GetNormalMapID()) {
|
||||
glBindTexture(GL_TEXTURE_2D, sprite->GetNormalMapID());
|
||||
} else {
|
||||
glBindTexture(GL_TEXTURE_2D, defaultNormalMap);
|
||||
}
|
||||
}
|
||||
|
||||
glBindVertexArray(quadVAO);
|
||||
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
|
||||
@ -185,18 +240,10 @@ void Renderer::DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float z
|
||||
s_DrawCalls++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
int Renderer::GetDrawCallCount()
|
||||
{
|
||||
int Renderer::GetDrawCallCount() {
|
||||
return s_DrawCalls;
|
||||
}
|
||||
|
||||
|
||||
void Renderer::DrawEditorGrid(const glm::vec2& cameraPos, float zoom) {
|
||||
glUseProgram(0);
|
||||
glColor4f(0.5f, 0.5f, 0.5f, 0.25f);
|
||||
@ -226,11 +273,9 @@ void Renderer::DrawEditorGrid(const glm::vec2& cameraPos, float zoom) {
|
||||
glVertex2f(left, (float)y);
|
||||
glVertex2f(right, (float)y);
|
||||
}
|
||||
|
||||
glEnd();
|
||||
}
|
||||
|
||||
|
||||
GLuint Renderer::GetRenderTexture() {
|
||||
return textureColorBuffer;
|
||||
}
|
||||
|
@ -3,8 +3,11 @@
|
||||
#include <glm/glm.hpp>
|
||||
#include <vector>
|
||||
|
||||
#include "Components/TilemapComponent.h"
|
||||
#include "Components/SpriteComponent.h"
|
||||
|
||||
|
||||
|
||||
class SpriteComponent;
|
||||
|
||||
struct Light {
|
||||
glm::vec2 screenPos;
|
||||
@ -21,6 +24,7 @@ public:
|
||||
static void End();
|
||||
static void DrawSprite(SpriteComponent* sprite, const glm::vec2& pos, float zoom, glm::vec2& CameraPos);
|
||||
static void AddLight(const glm::vec2& screenPos, const glm::vec3& color, float intensity, float radius);
|
||||
static void DrawTilemap(TilemapComponent* tilemap, const glm::vec2& worldPos, float zoom, const glm::vec2& cameraPos);
|
||||
static void ClearLights();
|
||||
static void DrawEditorGrid(const glm::vec2& cameraPos, float zoom);
|
||||
static GLuint GetRenderTexture();
|
||||
|
8
src/src/utils/EngineConfig.cpp
Normal file
8
src/src/utils/EngineConfig.cpp
Normal file
@ -0,0 +1,8 @@
|
||||
// EngineConfig.cpp
|
||||
#include "EngineConfig.h"
|
||||
|
||||
|
||||
EngineConfig g_engineConfig {
|
||||
.lighting_enabled = false,
|
||||
|
||||
};
|
7
src/src/utils/EngineConfig.h
Normal file
7
src/src/utils/EngineConfig.h
Normal file
@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
struct EngineConfig {
|
||||
bool lighting_enabled;
|
||||
};
|
||||
extern EngineConfig g_engineConfig;
|
@ -1,14 +1,41 @@
|
||||
#include "utils.h"
|
||||
#include <map>
|
||||
#include "stb_image.h"
|
||||
|
||||
static std::map<std::string, GLuint> textureCache;
|
||||
|
||||
GLuint LoadTextureIfNeeded(const std::string& path) {
|
||||
if (textureCache.count(path))
|
||||
return textureCache[path];
|
||||
|
||||
int w, h, channels;
|
||||
stbi_set_flip_vertically_on_load(false);
|
||||
unsigned char* data = stbi_load(path.c_str(), &w, &h, &channels, 4);
|
||||
if (!data)
|
||||
return 0;
|
||||
|
||||
GLuint id;
|
||||
glGenTextures(1, &id);
|
||||
glBindTexture(GL_TEXTURE_2D, id);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
stbi_image_free(data);
|
||||
|
||||
textureCache[path] = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
std::string GetFilenameFromPath(const std::string& path)
|
||||
{
|
||||
// Find the last slash or backslash
|
||||
size_t lastSlash = path.find_last_of("/\\");
|
||||
std::string filename = (lastSlash == std::string::npos) ? path : path.substr(lastSlash + 1);
|
||||
|
||||
// Strip trailing slashes (if any)
|
||||
// Strip trailing slashes
|
||||
while (!filename.empty() && (filename.back() == '/' || filename.back() == '\\'))
|
||||
filename.pop_back();
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,4 +1,10 @@
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <GL/glew.h>
|
||||
|
||||
// Returns filename from a full path (e.g., "C:/foo/bar.png" → "bar.png")
|
||||
std::string GetFilenameFromPath(const std::string& path);
|
||||
|
||||
// Loads a texture (with caching). Returns OpenGL texture ID
|
||||
GLuint LoadTextureIfNeeded(const std::string& path);
|
||||
|
Loading…
Reference in New Issue
Block a user