Browse Source

modifyChunk

feature/replace-renderer
Martin Dørum 3 years ago
parent
commit
d308ae6c9f
3 changed files with 32 additions and 0 deletions
  1. 1
    0
      libcygnet/include/cygnet/Renderer.h
  2. 24
    0
      libcygnet/src/Renderer.cc
  3. 7
    0
      src/cygnet-test.cc

+ 1
- 0
libcygnet/include/cygnet/Renderer.h View File

@@ -38,6 +38,7 @@ public:

RenderChunk createChunk(
TileID tiles[SwanCommon::CHUNK_WIDTH * SwanCommon::CHUNK_HEIGHT]);
void modifyChunk(RenderChunk chunk, SwanCommon::Vec2i pos, TileID id);
void destroyChunk(RenderChunk chunk);

private:

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

@@ -218,11 +218,13 @@ RenderChunk Renderer::createChunk(
RenderChunk chunk;
glGenTextures(1, &chunk.tex);
glCheck();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, chunk.tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glCheck();

static_assert(
std::endian::native == std::endian::big ||
@@ -254,6 +256,28 @@ RenderChunk Renderer::createChunk(
return chunk;
}

void Renderer::modifyChunk(RenderChunk chunk, SwanCommon::Vec2i pos, TileID id) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, chunk.tex);
glCheck();

static_assert(
std::endian::native == std::endian::big ||
std::endian::native == std::endian::little,
"Expected either big or little endian");

if constexpr (std::endian::native == std::endian::little) {
glTexSubImage2D(
GL_TEXTURE_2D, 0, pos.x, pos.y, 1, 1,
GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, &id);
} else if constexpr (std::endian::native == std::endian::big) {
uint8_t buf[] = { (uint8_t)(id & 0xff), (uint8_t)((id & 0xff00) >> 8) };
glTexSubImage2D(
GL_TEXTURE_2D, 0, pos.x, pos.y, 1, 1,
GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, buf);
}
}

void Renderer::destroyChunk(RenderChunk chunk) {
glDeleteTextures(1, &chunk.tex);
}

+ 7
- 0
src/cygnet-test.cc View File

@@ -46,6 +46,8 @@ int main() {
.zoom = 0.125,
};

unsigned int lol = 0;

while (true) {
SDL_Event evt;
while (SDL_PollEvent(&evt)) {
@@ -55,6 +57,11 @@ int main() {
}
}

lol += 1;
rnd.modifyChunk(chunk, {0, 0}, (lol / 20) % 6);
rnd.modifyChunk(chunk, {4, 4}, ((lol / 20) + 3) % 6);
rnd.modifyChunk(chunk, {3, 2}, ((lol / 25) + 7) % 6);

rnd.drawChunk({0, 0}, chunk);

win.clear();

Loading…
Cancel
Save