|
|
|
|
|
|
|
|
#include <stdint.h> |
|
|
#include <stdint.h> |
|
|
|
|
|
|
|
|
#include "log.h" |
|
|
#include "log.h" |
|
|
|
|
|
#include "gfxutil.h" |
|
|
#include "World.h" |
|
|
#include "World.h" |
|
|
#include "Game.h" |
|
|
#include "Game.h" |
|
|
#include "Win.h" |
|
|
#include "Win.h" |
|
|
|
|
|
|
|
|
CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE)); |
|
|
CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (pos_ != ChunkPos(0, 0)) |
|
|
|
|
|
return; |
|
|
|
|
|
info << "Rendering chunk " << pos_; |
|
|
|
|
|
|
|
|
|
|
|
Tile::ID prevID = Tile::INVALID_ID; |
|
|
Tile::ID prevID = Tile::INVALID_ID; |
|
|
Tile *tile = ctx.game.invalid_tile_.get(); |
|
|
Tile *tile = ctx.game.invalid_tile_.get(); |
|
|
|
|
|
|
|
|
|
|
|
// Get info about or texture for later |
|
|
|
|
|
uint32_t format; |
|
|
|
|
|
int access, texw, texh; |
|
|
|
|
|
if (SDL_QueryTexture(visuals_->texture_.get(), &format, &access, &texw, &texh) < 0) { |
|
|
|
|
|
panic << "Failed to query texture: " << SDL_GetError(); |
|
|
|
|
|
abort(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ...Now convert the format to an actually useful mask |
|
|
|
|
|
int bpp = 32; |
|
|
|
|
|
uint32_t rmask, gmask, bmask, amask; |
|
|
|
|
|
if (SDL_PixelFormatEnumToMasks(format, &bpp, &rmask, &gmask, &bmask, &amask) != SDL_TRUE) { |
|
|
|
|
|
panic << "Failed to get pixel mask: " << SDL_GetError(); |
|
|
|
|
|
abort(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// We lock the txture to get write access to its raw pixels |
|
|
SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE }; |
|
|
SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE }; |
|
|
uint8_t *pixels; |
|
|
uint8_t *pixels; |
|
|
int pitch; |
|
|
int pitch; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
auto lock = makeDeferred([this] { SDL_UnlockTexture(visuals_->texture_.get()); }); |
|
|
auto lock = makeDeferred([this] { SDL_UnlockTexture(visuals_->texture_.get()); }); |
|
|
|
|
|
|
|
|
|
|
|
// This surface will refer to the pixels of our texture which we want to update. |
|
|
|
|
|
auto destsurf = makeRaiiPtr( |
|
|
|
|
|
SDL_CreateRGBSurfaceFrom( |
|
|
|
|
|
pixels, TILE_SIZE, TILE_SIZE, 32, pitch, |
|
|
|
|
|
rmask, gmask, bmask, amask), |
|
|
|
|
|
SDL_FreeSurface); |
|
|
|
|
|
|
|
|
for (int y = 0; y < CHUNK_HEIGHT; ++y) { |
|
|
for (int y = 0; y < CHUNK_HEIGHT; ++y) { |
|
|
for (int x = 0; x < CHUNK_WIDTH; ++x) { |
|
|
for (int x = 0; x < CHUNK_WIDTH; ++x) { |
|
|
Tile::ID id = getTileID(RelPos(x, y)); |
|
|
Tile::ID id = getTileID(RelPos(x, y)); |
|
|
|
|
|
|
|
|
tile = &ctx.world.getTileByID(id); |
|
|
tile = &ctx.world.getTileByID(id); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
auto &tilesurf = tile->image_.surface_; |
|
|
|
|
|
|
|
|
auto &srcsurf = tile->image_.surface_; |
|
|
|
|
|
SDL_Rect srcrect{ 0, 0, srcsurf->w, srcsurf->h }; |
|
|
|
|
|
|
|
|
for (int imgy = 0; imgy < TILE_SIZE; ++imgy) { |
|
|
|
|
|
uint8_t *tilepix = (uint8_t *)tilesurf->pixels + imgy * tilesurf->pitch; |
|
|
|
|
|
uint8_t *destpix = pixels + |
|
|
|
|
|
((y * TILE_SIZE + imgy) * pitch) + |
|
|
|
|
|
(x * TILE_SIZE) * 4; |
|
|
|
|
|
memcpy(destpix, tilepix, TILE_SIZE * 4); |
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
SDL_Rect destrect{ x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE }; |
|
|
|
|
|
*/ |
|
|
|
|
|
destsurf->pixels = pixels + (y * TILE_SIZE * pitch) + x * TILE_SIZE * 4; |
|
|
|
|
|
SDL_Rect destrect{ 0, 0, TILE_SIZE, TILE_SIZE }; |
|
|
|
|
|
|
|
|
|
|
|
if (SDL_BlitSurface(srcsurf.get(), &srcrect, destsurf.get(), &destrect) < 0) { |
|
|
|
|
|
warn << "Failed to blit surface: " << SDL_GetError(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
visuals_->dirty_ = true; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void Chunk::draw(const Context &ctx, Win &win) { |
|
|
void Chunk::draw(const Context &ctx, Win &win) { |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (visuals_->dirty_) { |
|
|
if (visuals_->dirty_) { |
|
|
//visuals_->sprite_.setTexture(visuals_->tex_); |
|
|
|
|
|
|
|
|
need_render_ = true; |
|
|
visuals_->dirty_ = false; |
|
|
visuals_->dirty_ = false; |
|
|
} |
|
|
} |
|
|
|
|
|
|