@@ -31,7 +31,7 @@ public: | |||
const bool is_solid_; | |||
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; | |||
}; | |||
@@ -1,5 +1,6 @@ | |||
#pragma once | |||
#include "log.h" | |||
#include "common.h" | |||
#include <SDL2/SDL.h> | |||
@@ -22,6 +23,14 @@ public: | |||
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; | |||
Vec2 cam_; | |||
SDL_Renderer *renderer_; |
@@ -54,8 +54,8 @@ void Chunk::compress() { | |||
compressed_size_ = destlen; | |||
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."; | |||
} else if (ret == Z_BUF_ERROR) { | |||
info | |||
@@ -125,8 +125,8 @@ void Chunk::render(const Context &ctx) { | |||
auto &tilesurf = tile->image_.surface_; | |||
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); | |||
} | |||
} | |||
@@ -140,7 +140,7 @@ void Chunk::draw(const Context &ctx, Win &win) { | |||
return; | |||
if (need_render_) { | |||
info << "OK need render, so we create texture"; | |||
render(ctx); | |||
need_render_ = false; | |||
} | |||
@@ -149,7 +149,8 @@ void Chunk::draw(const Context &ctx, Win &win) { | |||
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_); | |||
} | |||
@@ -18,30 +18,30 @@ void Mod::init(const std::string &name) { | |||
void Mod::registerImage(const std::string &name, const std::string &path, int frame_height) { | |||
images_.push_back(std::make_unique<ImageResource>( | |||
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) { | |||
tile.name = name_ + "::" + tile.name; | |||
tiles_.push_back(tile); | |||
info << "Adding tile: " << tile.name; | |||
info << " Adding tile: " << tile.name; | |||
} | |||
void Mod::registerItem(Item::Builder item) { | |||
item.name = name_ + "::" + item.name; | |||
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) { | |||
gen->name_ = name_ + "::" + name; | |||
info << "Adding world gen: " << gen->name_; | |||
info << " Adding world gen: " << gen->name_; | |||
worldgens_.push_back(std::move(gen)); | |||
} | |||
void Mod::registerEntity(const std::string &name, std::unique_ptr<Entity::Factory> ent) { | |||
ent->name_ = name_ + "::" + name; | |||
info << "Adding entity: " << ent->name_; | |||
info << " Adding entity: " << ent->name_; | |||
entities_.push_back(std::move(ent)); | |||
} | |||
@@ -7,8 +7,8 @@ namespace Swan { | |||
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", | |||
.image = "@internal::invalid", | |||
}); |
@@ -22,7 +22,16 @@ static bool chunkLine(int l, WorldPlane &plane, ChunkPos &abspos, const Vec2i &d | |||
} | |||
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) { | |||
int l = 0; |