const bool is_solid_; | const bool is_solid_; | ||||
const std::optional<std::string> dropped_item_; | const std::optional<std::string> dropped_item_; | ||||
static std::unique_ptr<Tile> createInvalid(Context &ctx); | |||||
static std::unique_ptr<Tile> createInvalid(const ResourceManager &ctx); | |||||
static ID INVALID_ID; | static ID INVALID_ID; | ||||
}; | }; | ||||
#pragma once | #pragma once | ||||
#include "log.h" | |||||
#include "common.h" | #include "common.h" | ||||
#include <SDL2/SDL.h> | #include <SDL2/SDL.h> | ||||
return Vec2(10, 10); | return Vec2(10, 10); | ||||
} | } | ||||
void showTexture(const Vec2 &pos, SDL_Texture *tex, SDL_Rect *srcrect) { | |||||
SDL_Rect destrect{ (int)pos.x, (int)pos.y, srcrect->w, srcrect->h }; | |||||
if (SDL_RenderCopy(renderer_, tex, srcrect, &destrect) < 0) { | |||||
panic << "RenderCopy failed: " << SDL_GetError(); | |||||
abort(); | |||||
} | |||||
} | |||||
float scale_ = 2; | float scale_ = 2; | ||||
Vec2 cam_; | Vec2 cam_; | ||||
SDL_Renderer *renderer_; | SDL_Renderer *renderer_; |
compressed_size_ = destlen; | compressed_size_ = destlen; | ||||
info | info | ||||
<< "Compressed chunk " << pos_ << " " | |||||
<< "from " << CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID) << "bytes " | |||||
<< "Compressed chunk " << pos_ << " from " | |||||
<< CHUNK_WIDTH * CHUNK_HEIGHT * sizeof(Tile::ID) << " bytes " | |||||
<< "to " << destlen << " bytes."; | << "to " << destlen << " bytes."; | ||||
} else if (ret == Z_BUF_ERROR) { | } else if (ret == Z_BUF_ERROR) { | ||||
info | info | ||||
auto &tilesurf = tile->image_.surface_; | auto &tilesurf = tile->image_.surface_; | ||||
for (int imgy = 0; imgy < TILE_SIZE; ++imgy) { | for (int imgy = 0; imgy < TILE_SIZE; ++imgy) { | ||||
uint8_t *tilepix = (uint8_t *)tilesurf->pixels + (imgy * tilesurf->pitch) * 4; | |||||
uint8_t *destpix = pixels + (y * pitch + x * TILE_SIZE) * 4; | |||||
uint8_t *tilepix = (uint8_t *)tilesurf->pixels + imgy * tilesurf->pitch; | |||||
uint8_t *destpix = pixels + y * pitch + (x * TILE_SIZE) * 4; | |||||
memcpy(destpix, tilepix, TILE_SIZE * 4); | memcpy(destpix, tilepix, TILE_SIZE * 4); | ||||
} | } | ||||
} | } | ||||
return; | return; | ||||
if (need_render_) { | if (need_render_) { | ||||
info << "OK need render, so we create texture"; | |||||
render(ctx); | |||||
need_render_ = false; | need_render_ = false; | ||||
} | } | ||||
visuals_->dirty_ = false; | visuals_->dirty_ = false; | ||||
} | } | ||||
win.setPos(pos_ * Vec2i(CHUNK_WIDTH, CHUNK_HEIGHT)); | |||||
SDL_Rect rect{ 0, 0, CHUNK_WIDTH * TILE_SIZE, CHUNK_HEIGHT * TILE_SIZE }; | |||||
win.showTexture(pos_, visuals_->texture_.get(), &rect); | |||||
//win.draw(visuals_->sprite_); | //win.draw(visuals_->sprite_); | ||||
} | } | ||||
void Mod::registerImage(const std::string &name, const std::string &path, int frame_height) { | void Mod::registerImage(const std::string &name, const std::string &path, int frame_height) { | ||||
images_.push_back(std::make_unique<ImageResource>( | images_.push_back(std::make_unique<ImageResource>( | ||||
renderer_, name_ + "::" + name, path_ + "/assets/" + path, frame_height)); | renderer_, name_ + "::" + name, path_ + "/assets/" + path, frame_height)); | ||||
info << "Adding image: " << name_ << "::" << name; | |||||
info << " Adding image: " << name_ << "::" << name << " (" << path << ')'; | |||||
} | } | ||||
void Mod::registerTile(Tile::Builder tile) { | void Mod::registerTile(Tile::Builder tile) { | ||||
tile.name = name_ + "::" + tile.name; | tile.name = name_ + "::" + tile.name; | ||||
tiles_.push_back(tile); | tiles_.push_back(tile); | ||||
info << "Adding tile: " << tile.name; | |||||
info << " Adding tile: " << tile.name; | |||||
} | } | ||||
void Mod::registerItem(Item::Builder item) { | void Mod::registerItem(Item::Builder item) { | ||||
item.name = name_ + "::" + item.name; | item.name = name_ + "::" + item.name; | ||||
items_.push_back(item); | items_.push_back(item); | ||||
info << "Adding item: " << item.name; | |||||
info << " Adding item: " << item.name; | |||||
} | } | ||||
void Mod::registerWorldGen(const std::string &name, std::unique_ptr<WorldGen::Factory> gen) { | void Mod::registerWorldGen(const std::string &name, std::unique_ptr<WorldGen::Factory> gen) { | ||||
gen->name_ = name_ + "::" + name; | gen->name_ = name_ + "::" + name; | ||||
info << "Adding world gen: " << gen->name_; | |||||
info << " Adding world gen: " << gen->name_; | |||||
worldgens_.push_back(std::move(gen)); | worldgens_.push_back(std::move(gen)); | ||||
} | } | ||||
void Mod::registerEntity(const std::string &name, std::unique_ptr<Entity::Factory> ent) { | void Mod::registerEntity(const std::string &name, std::unique_ptr<Entity::Factory> ent) { | ||||
ent->name_ = name_ + "::" + name; | ent->name_ = name_ + "::" + name; | ||||
info << "Adding entity: " << ent->name_; | |||||
info << " Adding entity: " << ent->name_; | |||||
entities_.push_back(std::move(ent)); | entities_.push_back(std::move(ent)); | ||||
} | } | ||||
Tile::ID Tile::INVALID_ID = 0; | Tile::ID Tile::INVALID_ID = 0; | ||||
std::unique_ptr<Tile> Tile::createInvalid(Context &ctx) { | |||||
return std::make_unique<Tile>(ctx.resources, Builder{ | |||||
std::unique_ptr<Tile> Tile::createInvalid(const ResourceManager &resources) { | |||||
return std::make_unique<Tile>(resources, Builder{ | |||||
.name = "@internal::invalid", | .name = "@internal::invalid", | ||||
.image = "@internal::invalid", | .image = "@internal::invalid", | ||||
}); | }); |
} | } | ||||
World::World(Game *game, unsigned long rand_seed): | World::World(Game *game, unsigned long rand_seed): | ||||
game_(game), random_(rand_seed), resources_(game->win_) {} | |||||
game_(game), random_(rand_seed), resources_(game->win_) { | |||||
std::unique_ptr<Tile> invalid_tile = Tile::createInvalid(resources_); | |||||
tiles_map_[invalid_tile->name_] = 0; | |||||
// tiles_ is empty, so pushing back now will ensure invalid_tile | |||||
// ends up at location 0 | |||||
tiles_.push_back(std::move(invalid_tile)); | |||||
} | |||||
void World::ChunkRenderer::tick(WorldPlane &plane, ChunkPos abspos) { | void World::ChunkRenderer::tick(WorldPlane &plane, ChunkPos abspos) { | ||||
int l = 0; | int l = 0; |