Browse Source

don't redraw the entire chunk when a tile changes

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
e1c9c0e4cd
2 changed files with 6 additions and 10 deletions
  1. 0
    2
      libswan/include/swan/Chunk.h
  2. 6
    8
      libswan/src/Chunk.cc

+ 0
- 2
libswan/include/swan/Chunk.h View File

@@ -20,7 +20,6 @@ public:
Chunk(ChunkPos pos): pos_(pos) {
data_.reset(new uint8_t[CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID)]);
visuals_.reset(new Visuals());
visuals_->dirty_ = false;
}

Tile::ID *getTileData();
@@ -53,7 +52,6 @@ private:

struct Visuals {
RaiiPtr<SDL_Texture> texture_ = makeRaiiPtr<SDL_Texture>(nullptr, SDL_DestroyTexture);
bool dirty_ = true;
};
std::unique_ptr<Visuals> visuals_;
};

+ 6
- 8
libswan/src/Chunk.cc View File

@@ -30,8 +30,12 @@ void Chunk::setTileID(RelPos pos, Tile::ID id) {
void Chunk::drawBlock(RelPos pos, const Tile &t) {
keepActive();

//visuals_->tex_.update(*t.image_, pos.x * TILE_SIZE, pos.y * TILE_SIZE);
visuals_->dirty_ = true;
SDL_Rect lockrect{ pos.x * TILE_SIZE, pos.y * TILE_SIZE, TILE_SIZE, TILE_SIZE };
TexLock lock(visuals_->texture_.get(), &lockrect);

SDL_Rect srcrect{ 0, 0, t.image_.surface_->w, t.image_.surface_->h };
SDL_Rect destrect{ 0, 0, TILE_SIZE, TILE_SIZE };
lock.blit(&destrect, t.image_.surface_.get(), &srcrect);
}

void Chunk::compress() {
@@ -86,7 +90,6 @@ void Chunk::decompress() {
data_ = std::move(dest);

visuals_.reset(new Visuals());
visuals_->dirty_ = true;
need_render_ = true;

info
@@ -141,11 +144,6 @@ void Chunk::draw(const Context &ctx, Win &win) {
need_render_ = false;
}

if (visuals_->dirty_) {
need_render_ = true;
visuals_->dirty_ = false;
}

SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE };
win.showTexture(
pos_ * Vec2i(CHUNK_WIDTH, CHUNK_HEIGHT),

Loading…
Cancel
Save