Browse Source

more cygnet stuff

fix/style
Martin Dørum 3 years ago
parent
commit
7edfaed271

+ 4
- 0
CMakeLists.txt View File

@@ -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)

libswan/include/swan/Vector2.h → include/swan-common/Vector2.h View File

@@ -5,7 +5,7 @@
#include <cmath>
#include <msgpack.hpp>

namespace Swan {
namespace SwanCommon {

template<typename T>
struct Vector2 {

+ 13
- 0
include/swan-common/constants.h View File

@@ -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;

}

+ 2
- 1
libcygnet/CMakeLists.txt View File

@@ -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)

+ 20
- 0
libcygnet/include/cygnet/TileAtlas.h View File

@@ -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_;
};

}

+ 3
- 0
libcygnet/src/Renderer.cc View File

@@ -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>()) {}

+ 59
- 0
libcygnet/src/TileAtlas.cc View File

@@ -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);
}
}

}

+ 1
- 1
libswan/CMakeLists.txt View File

@@ -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)


+ 0
- 1
libswan/include/swan/WorldGen.h View File

@@ -8,7 +8,6 @@
#include "Entity.h"
#include "Collection.h"
#include "traits/BodyTrait.h"
#include "Vector2.h"

namespace Swan {


+ 3
- 8
libswan/include/swan/common.h View File

@@ -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;

+ 0
- 1
libswan/include/swan/swan.h View File

@@ -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
- 1
libswan/include/swan/traits/BodyTrait.h View File

@@ -1,6 +1,6 @@
#pragma once

#include "../Vector2.h"
#include "../common.h"

namespace Swan {


+ 1
- 1
libswan/include/swan/traits/InventoryTrait.h View File

@@ -3,7 +3,7 @@
#include <vector>
#include <stdlib.h>

#include "../Vector2.h"
#include "../common.h"
#include "../ItemStack.h"

namespace Swan {

+ 0
- 1
libswan/include/swan/traits/PhysicsTrait.h View File

@@ -1,7 +1,6 @@
#pragma once

#include "../traits/BodyTrait.h"
#include "../Vector2.h"
#include "../common.h"

namespace Swan {

Loading…
Cancel
Save