Просмотр исходного кода

at least it now draws _something_...

opengl-renderer-broken
Martin Dørum 4 лет назад
Родитель
Сommit
0993872ad5
6 измененных файлов: 34 добавлений и 15 удалений
  1. 1
    1
      libswan/include/swan/Tile.h
  2. 9
    0
      libswan/include/swan/Win.h
  3. 7
    6
      libswan/src/Chunk.cc
  4. 5
    5
      libswan/src/Mod.cc
  5. 2
    2
      libswan/src/Tile.cc
  6. 10
    1
      libswan/src/World.cc

+ 1
- 1
libswan/include/swan/Tile.h Просмотреть файл

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;
}; };



+ 9
- 0
libswan/include/swan/Win.h Просмотреть файл

#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_;

+ 7
- 6
libswan/src/Chunk.cc Просмотреть файл

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_);
} }



+ 5
- 5
libswan/src/Mod.cc Просмотреть файл

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));
} }



+ 2
- 2
libswan/src/Tile.cc Просмотреть файл



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",
}); });

+ 10
- 1
libswan/src/World.cc Просмотреть файл

} }


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;

Загрузка…
Отмена
Сохранить