@@ -20,7 +20,7 @@ public: | |||
void onResize(int w, int h); | |||
SwanCommon::Vec2i size() { return { w_, h_ }; } | |||
SDL_Window *getWindow(); | |||
SDL_Window *sdlWindow(); | |||
private: | |||
std::unique_ptr<WindowState> state_; |
@@ -32,7 +32,6 @@ void TileAtlas::addTile(size_t tileId, const void *data) { | |||
const unsigned char *bytes = (const unsigned char *)data; | |||
size_t x = tileId % state_->tilesPerLine; | |||
size_t y = tileId / state_->tilesPerLine; | |||
std::cerr << "Tile " << tileId << " to " << x << ", " << y << '\n'; | |||
if (state_->width <= x) { | |||
state_->width = x + 1; |
@@ -67,7 +67,7 @@ void Window::onResize(int w, int h) { | |||
glCheck(); | |||
} | |||
SDL_Window *Window::getWindow() { | |||
SDL_Window *Window::sdlWindow() { | |||
return state_->window; | |||
} | |||
@@ -12,7 +12,6 @@ add_library(libswan SHARED | |||
src/gfxutil.cc | |||
src/ItemStack.cc | |||
src/LightServer.cc | |||
src/Mod.cc | |||
src/OS.cc | |||
src/World.cc | |||
src/WorldPlane.cc) |
@@ -17,20 +17,21 @@ | |||
namespace Swan { | |||
class ModWrapper; | |||
class Mod { | |||
public: | |||
Mod(std::string name): name_(std::move(name)) {} | |||
virtual ~Mod() = default; | |||
void registerTile(Tile::Builder tile); | |||
void registerItem(Item::Builder item); | |||
void registerWorldGen(std::string name, std::unique_ptr<WorldGen::Factory> gen); | |||
void registerSprite(std::string sprite); | |||
void registerTile(Tile::Builder tile) { tiles_.push_back(tile); } | |||
void registerItem(Item::Builder item) { items_.push_back(item); } | |||
void registerSprite(std::string sprite) { sprites_.push_back(sprite); } | |||
template<typename WG> | |||
void registerWorldGen(std::string name) { | |||
worldGens_.push_back(WorldGen::Factory{ | |||
.name = name_ + "::" + name, | |||
.name = name, | |||
.create = [](World &world) -> std::unique_ptr<WorldGen> { | |||
return std::make_unique<WG>(world); | |||
} | |||
@@ -43,20 +44,22 @@ public: | |||
std::is_move_constructible_v<Ent>, | |||
"Entities must be movable"); | |||
entities_.push_back(EntityCollection::Factory{ | |||
.name = name_ + "::" + name, | |||
.name = name, | |||
.create = [](std::string name) -> std::unique_ptr<EntityCollection> { | |||
return std::make_unique<EntityCollectionImpl<Ent>>(std::move(name)); | |||
} | |||
}); | |||
} | |||
private: | |||
const std::string name_; | |||
std::vector<std::string> images_; | |||
std::vector<Tile::Builder> tiles_; | |||
std::vector<Item::Builder> items_; | |||
std::vector<std::string> sprites_; | |||
std::vector<WorldGen::Factory> worldGens_; | |||
std::vector<EntityCollection::Factory> entities_; | |||
friend ModWrapper; | |||
}; | |||
class ModWrapper { | |||
@@ -72,6 +75,13 @@ public: | |||
mod_.reset(); | |||
} | |||
const std::string &name() { return mod_->name_; } | |||
const std::vector<Tile::Builder> &tiles() { return mod_->tiles_; } | |||
const std::vector<Item::Builder> &items() { return mod_->items_; } | |||
const std::vector<std::string> &sprites() { return mod_->sprites_; } | |||
const std::vector<WorldGen::Factory> &worldGens() { return mod_->worldGens_; } | |||
const std::vector<EntityCollection::Factory> &entities() { return mod_->entities_; } | |||
std::unique_ptr<Mod> mod_; | |||
std::string path_; | |||
OS::Dynlib dynlib_; |
@@ -94,7 +94,7 @@ public: | |||
return *this; | |||
} | |||
operator bool() { return isOk_; } | |||
explicit operator bool() { return isOk_; } | |||
bool isOk() { return isOk_; } | |||
Err &err() { return v_.err; } |
@@ -31,6 +31,9 @@ SDL_Color Game::backgroundColor() { | |||
void Game::draw() { | |||
world_->draw(renderer_); | |||
renderer_.draw(cam_); | |||
info << "Rendered cam pos " << cam_.pos << " size " << cam_.size << " zoom " << cam_.zoom; | |||
} | |||
void Game::update(float dt) { |
@@ -1,26 +0,0 @@ | |||
#include "Mod.h" | |||
#include <stdio.h> | |||
#include <algorithm> | |||
#include "util.h" | |||
#include "log.h" | |||
namespace Swan { | |||
void Mod::registerTile(Tile::Builder tile) { | |||
tiles_.push_back(tile); | |||
info << " Adding tile: " << name_ << "::" << tile.name; | |||
} | |||
void Mod::registerItem(Item::Builder item) { | |||
items_.push_back(item); | |||
info << " Adding item: " << name_ << "::" << item.name; | |||
} | |||
void Mod::registerSprite(std::string path) { | |||
sprites_.push_back(path); | |||
info << "Adding sprite: " << name_ << "::" << path; | |||
} | |||
} |
@@ -87,7 +87,7 @@ Cygnet::ResourceManager World::buildResources() { | |||
// "core::stone", we need to know which directory the "core" mod is in | |||
std::unordered_map<std::string, std::string> modPaths; | |||
for (auto &mod: mods_) { | |||
modPaths[mod.mod_->name_] = mod.path_; | |||
modPaths[mod.name()] = mod.path_; | |||
} | |||
auto loadTileImage = [&](std::string path) -> Result<ImageAsset> { | |||
@@ -122,10 +122,10 @@ Cygnet::ResourceManager World::buildResources() { | |||
// In the rendering system, there's no real difference between a tile | |||
// and an item. | |||
for (auto &mod: mods_) { | |||
for (auto &tileBuilder: mod.mod_->tiles_) { | |||
for (auto &tileBuilder: mod.tiles()) { | |||
auto image = loadTileImage(tileBuilder.image); | |||
std::string tileName = mod.mod_->name_ + "::" + tileBuilder.name; | |||
std::string tileName = mod.name() + "::" + tileBuilder.name; | |||
Tile::ID tileId = tiles_.size(); | |||
if (image) { | |||
@@ -152,10 +152,10 @@ Cygnet::ResourceManager World::buildResources() { | |||
// Load all items which aren't just tiles in disguise. | |||
for (auto &mod: mods_) { | |||
for (auto &itemBuilder: mod.mod_->items_) { | |||
for (auto &itemBuilder: mod.items()) { | |||
auto image = loadTileImage(itemBuilder.image); | |||
std::string itemName = mod.mod_->name_ + "::" + itemBuilder.name; | |||
std::string itemName = mod.name() + "::" + itemBuilder.name; | |||
Tile::ID itemId = nextItemId++; | |||
if (image) { | |||
@@ -171,8 +171,8 @@ Cygnet::ResourceManager World::buildResources() { | |||
// Load sprites | |||
for (auto &mod: mods_) { | |||
for (auto spritePath: mod.mod_->sprites_) { | |||
std::string path = mod.mod_->name_ + "::" + spritePath; | |||
for (auto spritePath: mod.sprites()) { | |||
std::string path = mod.name() + "::" + spritePath; | |||
auto image = loadImageAsset(modPaths, path); | |||
if (image) { | |||
@@ -192,13 +192,13 @@ Cygnet::ResourceManager World::buildResources() { | |||
// Load world gens and entities | |||
for (auto &mod: mods_) { | |||
for (auto &worldGenFactory: mod.mod_->worldGens_) { | |||
std::string name = mod.mod_->name_ + "::" + worldGenFactory.name; | |||
for (auto &worldGenFactory: mod.worldGens()) { | |||
std::string name = mod.name() + "::" + worldGenFactory.name; | |||
worldGenFactories_.emplace(name, worldGenFactory); | |||
} | |||
for (auto &entCollFactory: mod.mod_->entities_) { | |||
std::string name = mod.mod_->name_ + "::" + entCollFactory.name; | |||
for (auto &entCollFactory: mod.entities()) { | |||
std::string name = mod.name() + "::" + entCollFactory.name; | |||
entCollFactories_.emplace(name, entCollFactory); | |||
} | |||
} |
@@ -16,7 +16,7 @@ Result<ImageAsset> loadImageAsset( | |||
} | |||
auto modPart = path.substr(0, sep); | |||
auto pathPart = path.substr(sep, path.size() - sep - 2); | |||
auto pathPart = path.substr(sep + 2, path.size() - sep - 2); | |||
auto modPath = modPaths.find(modPart); | |||
if (modPath == modPaths.end()) { | |||
@@ -57,7 +57,6 @@ Result<ImageAsset> loadImageAsset( | |||
}; | |||
// TODO: Pixel formats? | |||
asset.data.reset(); | |||
for (size_t y = 0; y < (size_t)surface->h; ++y) { | |||
unsigned char *src = (unsigned char *)surface->pixels + y * surface->pitch; | |||
unsigned char *dest = asset.data.get() + y * surface->w * 4; |
@@ -85,22 +85,22 @@ int main(int argc, char **argv) { | |||
CPtr<SDL_Surface, SDL_FreeSurface> icon( | |||
IMG_Load("assets/icon.png")); | |||
sdlassert(icon, "Could not load icon"); | |||
SDL_SetWindowIcon(window.getWindow(), icon.get()); | |||
SDL_SetWindowIcon(window.sdlWindow(), icon.get()); | |||
// Init ImGUI and ImGUI_SDL | |||
/* | |||
IMGUI_CHECKVERSION(); | |||
CPtr<ImGuiContext, ImGui::DestroyContext> context( | |||
ImGui::CreateContext()); | |||
/* | |||
ImGuiSDL::Initialize(renderer.get(), (int)win.getPixSize().x, (int)win.getPixSize().y); | |||
Deferred<ImGuiSDL::Deinitialize> imguiSDL; | |||
info << "Initialized with window size " << win.getPixSize(); | |||
TODO */ | |||
// ImGuiIO is to glue SDL and ImGUI together | |||
ImGuiIO& imguiIO = ImGui::GetIO(); | |||
imguiIO.BackendPlatformName = "imgui_sdl + Project: SWAN"; | |||
TODO */ | |||
// Create a world | |||
Game game; | |||
@@ -126,8 +126,8 @@ int main(int argc, char **argv) { | |||
case SDL_WINDOWEVENT: | |||
if (evt.window.event == SDL_WINDOWEVENT_RESIZED) { | |||
imguiIO.DisplaySize.x = (float)evt.window.data1; | |||
imguiIO.DisplaySize.y = (float)evt.window.data2; | |||
//imguiIO.DisplaySize.x = (float)evt.window.data1; | |||
//imguiIO.DisplaySize.y = (float)evt.window.data2; | |||
window.onResize(evt.window.data1, evt.window.data2); | |||
} | |||
break; | |||
@@ -141,27 +141,31 @@ int main(int argc, char **argv) { | |||
break; | |||
case SDL_MOUSEMOTION: | |||
/* | |||
imguiIO.MousePos.x = (float)evt.motion.x; | |||
imguiIO.MousePos.y = (float)evt.motion.y; | |||
if (!imguiIO.WantCaptureMouse) | |||
if (!imguiIO.WantCaptureMouse) */ | |||
game.onMouseMove(evt.motion.x, evt.motion.y); | |||
break; | |||
case SDL_MOUSEBUTTONDOWN: | |||
/* | |||
imguiIO.MouseDown[sdlButtonToImGuiButton(evt.button.button)] = true; | |||
if (!imguiIO.WantCaptureMouse) | |||
if (!imguiIO.WantCaptureMouse) */ | |||
game.onMouseDown(evt.button.x, evt.button.y, evt.button.button); | |||
break; | |||
case SDL_MOUSEBUTTONUP: | |||
/* | |||
imguiIO.MouseDown[sdlButtonToImGuiButton(evt.button.button)] = false; | |||
if (!imguiIO.WantCaptureMouse) | |||
if (!imguiIO.WantCaptureMouse) */ | |||
game.onMouseUp(evt.button.x, evt.button.y, evt.button.button); | |||
break; | |||
case SDL_MOUSEWHEEL: | |||
/* | |||
imguiIO.MouseWheel += (float)evt.wheel.y; | |||
if (!imguiIO.WantCaptureMouse) | |||
if (!imguiIO.WantCaptureMouse) */ | |||
game.onScrollWheel(evt.wheel.y); | |||
break; | |||
} | |||
@@ -230,19 +234,20 @@ int main(int argc, char **argv) { | |||
} | |||
// ImGUI | |||
imguiIO.DeltaTime = dt; | |||
ImGui::NewFrame(); | |||
//imguiIO.DeltaTime = dt; | |||
//ImGui::NewFrame(); | |||
{ | |||
ZoneScopedN("game draw"); | |||
game.cam_.size = window.size(); | |||
game.draw(); | |||
} | |||
// Render ImGUI | |||
{ | |||
ZoneScopedN("imgui render"); | |||
ImGui::Render(); | |||
ImGuiSDL::Render(ImGui::GetDrawData()); | |||
//ImGui::Render(); | |||
//ImGuiSDL::Render(ImGui::GetDrawData()); | |||
} | |||
{ |