@@ -59,6 +59,10 @@ add_compile_options(-Wno-c99-extensions) | |||
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib;${CMAKE_INSTALL_PREFIX}/lib64;${CMAKE_INSTALL_PREFIX}/swan/libswan;${CMAKE_INSTALL_PREFIX}/swan/libcygnet;${CMAKE_INSTALL_PREFIX}/swan/third-party") | |||
add_library(swan-common INTERFACE) | |||
target_include_directories(swan-common | |||
INTERFACE "include") | |||
add_subdirectory(third-party) | |||
add_subdirectory(tracy-tools) | |||
add_subdirectory(libswan) |
@@ -5,7 +5,7 @@ | |||
#include <cmath> | |||
#include <msgpack.hpp> | |||
namespace Swan { | |||
namespace SwanCommon { | |||
template<typename T> | |||
struct Vector2 { |
@@ -0,0 +1,13 @@ | |||
#pragma once | |||
namespace SwanCommon { | |||
static constexpr int TILE_SIZE = 32; | |||
static constexpr int TICK_RATE = 20; | |||
static constexpr int CHUNK_HEIGHT = 64; | |||
static constexpr int CHUNK_WIDTH = 64; | |||
static constexpr int PLACEHOLDER_RED = 245; | |||
static constexpr int PLACEHOLDER_GREEN = 66; | |||
static constexpr int PLACEHOLDER_BLUE = 242; | |||
} |
@@ -3,11 +3,12 @@ add_library(libcygnet SHARED | |||
src/Program.cc | |||
src/Renderer.cc | |||
src/shaders.cc | |||
src/TileAtlas.cc | |||
src/Window.cc) | |||
target_include_directories(libcygnet | |||
PUBLIC "include" | |||
PRIVATE "include/cygnet") | |||
set_target_properties(libcygnet PROPERTIES OUTPUT_NAME cygnet) | |||
target_link_libraries(libcygnet GLESv2 ${libraries}) | |||
target_link_libraries(libcygnet swan-common GLESv2 ${libraries}) | |||
install(TARGETS libcygnet DESTINATION swan/libcygnet) |
@@ -0,0 +1,20 @@ | |||
#pragma once | |||
#include <memory> | |||
namespace Cygnet { | |||
struct AtlasState; | |||
class TileAtlas { | |||
public: | |||
TileAtlas(); | |||
~TileAtlas(); | |||
void addTile(size_t tileId, const void *data, size_t len); | |||
private: | |||
std::unique_ptr<AtlasState> state_; | |||
}; | |||
} |
@@ -2,6 +2,7 @@ | |||
#include "shaders.h" | |||
#include "Program.h" | |||
#include "TileAtlas.h" | |||
#include "util.h" | |||
namespace Cygnet { | |||
@@ -29,6 +30,8 @@ struct RendererState { | |||
TexturedProg texturedProg{texturedVx, texturedFr}; | |||
SolidColorProg solidColorProg{basicVx, solidColorFr}; | |||
TileAtlas atlas; | |||
}; | |||
Renderer::Renderer(): state_(std::make_unique<RendererState>()) {} |
@@ -0,0 +1,59 @@ | |||
#include "TileAtlas.h" | |||
#include <iostream> | |||
#include <vector> | |||
#include <SDL_opengles2.h> | |||
#include <stdio.h> | |||
#include <string.h> | |||
#include <swan-common/constants.h> | |||
namespace Cygnet { | |||
struct AtlasState { | |||
size_t tilesPerLine; | |||
size_t width = 0; | |||
size_t height = 0; | |||
std::vector<unsigned char> data; | |||
}; | |||
TileAtlas::TileAtlas(): state_(std::make_unique<AtlasState>()) { | |||
GLint size; | |||
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size); | |||
state_->tilesPerLine = size / SwanCommon::TILE_SIZE; | |||
} | |||
TileAtlas::~TileAtlas() = default; | |||
void TileAtlas::addTile(size_t tileId, const void *data, size_t len) { | |||
size_t rows = len / (SwanCommon::TILE_SIZE * 4); | |||
const unsigned char *bytes = (const unsigned char *)data; | |||
size_t x = tileId % state_->tilesPerLine; | |||
size_t y = tileId / state_->tilesPerLine; | |||
if (y >= state_->tilesPerLine) { | |||
std::cerr << "Cygnet: Warning: Tile ID " << tileId << " too big for texture atlas\n"; | |||
return; | |||
} | |||
if (state_->width <= x) { | |||
state_->width = x + 1; | |||
} | |||
if (state_->height <= y) { | |||
state_->height = y + 1; | |||
} | |||
size_t tileImgSize = SwanCommon::TILE_SIZE * SwanCommon::TILE_SIZE * 4; | |||
size_t requiredSize = (x + 1) * (y + 1) * tileImgSize; | |||
state_->data.resize(requiredSize); | |||
for (size_t ty = 0; ty < rows; ++ty) { | |||
unsigned char *dest = state_->data.data() + | |||
((y + ty) * state_->width * SwanCommon::TILE_SIZE * 4) + | |||
(x * SwanCommon::TILE_SIZE * 4); | |||
const unsigned char *src = bytes + ty * SwanCommon::TILE_SIZE * 4; | |||
memcpy(dest, src, SwanCommon::TILE_SIZE * 4); | |||
} | |||
} | |||
} |
@@ -22,7 +22,7 @@ target_include_directories(libswan | |||
PUBLIC "include" | |||
PRIVATE "include/swan") | |||
set_target_properties(libswan PROPERTIES OUTPUT_NAME swan) | |||
target_link_libraries(libswan ${libraries}) | |||
target_link_libraries(libswan swan-common ${libraries}) | |||
install(TARGETS libswan DESTINATION swan/libswan) | |||
@@ -8,7 +8,6 @@ | |||
#include "Entity.h" | |||
#include "Collection.h" | |||
#include "traits/BodyTrait.h" | |||
#include "Vector2.h" | |||
namespace Swan { | |||
@@ -3,17 +3,12 @@ | |||
// We want every file to be able to easily add Tracy zones | |||
#include <tracy/Tracy.hpp> | |||
#include "Vector2.h" | |||
#include <swan-common/Vector2.h> | |||
#include <swan-common/constants.h> | |||
namespace Swan { | |||
static constexpr int TILE_SIZE = 32; | |||
static constexpr int TICK_RATE = 20; | |||
static constexpr int CHUNK_HEIGHT = 64; | |||
static constexpr int CHUNK_WIDTH = 64; | |||
static constexpr int PLACEHOLDER_RED = 245; | |||
static constexpr int PLACEHOLDER_GREEN = 66; | |||
static constexpr int PLACEHOLDER_BLUE = 242; | |||
using namespace SwanCommon; | |||
using TilePos = Vec2i; | |||
using ChunkPos = Vec2i; |
@@ -15,7 +15,6 @@ | |||
#include <swan/Resource.h> | |||
#include <swan/SlotVector.h> | |||
#include <swan/Tile.h> | |||
#include <swan/Vector2.h> | |||
#include <swan/Win.h> | |||
#include <swan/World.h> | |||
#include <swan/WorldGen.h> |
@@ -1,6 +1,6 @@ | |||
#pragma once | |||
#include "../Vector2.h" | |||
#include "../common.h" | |||
namespace Swan { | |||
@@ -3,7 +3,7 @@ | |||
#include <vector> | |||
#include <stdlib.h> | |||
#include "../Vector2.h" | |||
#include "../common.h" | |||
#include "../ItemStack.h" | |||
namespace Swan { |
@@ -1,7 +1,6 @@ | |||
#pragma once | |||
#include "../traits/BodyTrait.h" | |||
#include "../Vector2.h" | |||
#include "../common.h" | |||
namespace Swan { |