From 15075e33af8c35e03b71c91c8041d561d5052f08 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Tue, 22 Apr 2025 14:40:12 +1000 Subject: [PATCH] Fix pickatile & some warnings --- ADG5.cpp | 2 +- Actor.cpp | 2 +- Ai.cpp | 4 +-- CMakeLists.txt | 11 ++------ Engine.cpp | 26 ++++++++++++------ Gui.cpp | 19 +++++++------ .../menu_background1.png | Bin .../terminal10x16_gs_ro.png | Bin 8 files changed, 35 insertions(+), 29 deletions(-) rename menu_background1.png => imgs/menu_background1.png (100%) rename terminal10x16_gs_ro.png => imgs/terminal10x16_gs_ro.png (100%) diff --git a/ADG5.cpp b/ADG5.cpp index 3b4e8b2..cf687a4 100644 --- a/ADG5.cpp +++ b/ADG5.cpp @@ -14,7 +14,7 @@ int main(int argc, char **argv) { auto console = tcod::Console(80, 50); auto params = TCOD_ContextParams{}; - auto tileset = tcod::load_tilesheet("terminal10x16_gs_ro.png", { 16, 16 }, tcod::CHARMAP_CP437); + auto tileset = tcod::load_tilesheet("imgs/terminal10x16_gs_ro.png", { 16, 16 }, tcod::CHARMAP_CP437); params.tileset = tileset.get(); params.console = console.get(); params.window_title = "Andrew's Dungeon Game 5"; diff --git a/Actor.cpp b/Actor.cpp index 3e5b2cb..2bed709 100644 --- a/Actor.cpp +++ b/Actor.cpp @@ -41,7 +41,7 @@ Actor::~Actor() { float Actor::getDistance(int cx, int cy) const { int dx = x - cx; int dy = y - cy; - return sqrtf(dx * dx + dy * dy); + return sqrtf((float)(dx * dx + dy * dy)); } void Actor::save(TCODZip& zip) { diff --git a/Ai.cpp b/Ai.cpp index ff8943a..564c8c8 100644 --- a/Ai.cpp +++ b/Ai.cpp @@ -68,8 +68,8 @@ void PlayerAi::update(Actor* owner) { engine->context->convert_event_coordinates(event); switch (event.type) { case SDL_EVENT_MOUSE_MOTION: - engine->mouse.cx = event.motion.x; - engine->mouse.cy = event.motion.y; + engine->mouse.cx = (int)event.motion.x; + engine->mouse.cy = (int)event.motion.y; break; case SDL_EVENT_KEY_UP: switch (event.key.key) { diff --git a/CMakeLists.txt b/CMakeLists.txt index d808d63..a9af411 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,12 +27,7 @@ endif() add_custom_command( TARGET ${PROJECT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy - ${CMAKE_CURRENT_SOURCE_DIR}/menu_background1.png - ${CMAKE_CURRENT_BINARY_DIR}/menu_background1.png) -add_custom_command( - TARGET ${PROJECT_NAME} POST_BUILD - COMMAND ${CMAKE_COMMAND} -E copy - ${CMAKE_CURRENT_SOURCE_DIR}/terminal10x16_gs_ro.png - ${CMAKE_CURRENT_BINARY_DIR}/terminal10x16_gs_ro.png) + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${CMAKE_CURRENT_SOURCE_DIR}/imgs + ${CMAKE_CURRENT_BINARY_DIR}/imgs) # TODO: Add tests and install targets if needed. diff --git a/Engine.cpp b/Engine.cpp index 2c83580..c272107 100644 --- a/Engine.cpp +++ b/Engine.cpp @@ -24,6 +24,8 @@ Engine::Engine(int screenWidth, int screenHeight, tcod::Context *context, tcod:: mouse.cy = 0; mouse.rbutton_pressed = false; mouse.lbutton_pressed = false; + gameStatus = STARTUP; + stairs = nullptr; } void Engine::init() { @@ -147,22 +149,29 @@ bool Engine::pickATile(int* x, int* y, float maxRange) { while (true) { render(false); + int offset_x = engine->player->x - Engine::VIEW_WIDTH / 2; + int offset_y = engine->player->y - Engine::VIEW_HEIGHT / 2; + // highlight the possible range - for (int cx = 0; cx < map->width; cx++) { - for (int cy = 0; cy < map->height; cy++) { + for (int cx = offset_x; cx < offset_x + engine->VIEW_WIDTH; cx++) { + for (int cy = offset_y; cy < offset_y + engine->VIEW_HEIGHT; cy++) { if (map->isInFov(cx, cy) && (maxRange == 0 || player->getDistance(cx, cy) <= maxRange)) { - TCOD_ColorRGBA col = console->at(cx, cy).bg; + TCOD_ColorRGBA col = console->at(cx - offset_x, cy - offset_y).bg; col.r = (int)((float)col.r * 1.2f); col.g = (int)((float)col.g * 1.2f); col.b = (int)((float)col.b * 1.2f); - console->at(cx, cy).bg = col; + console->at(cx - offset_x, cy - offset_y).bg = col; } } } SDL_Event event; + + mouse.lbutton_pressed = false; + mouse.rbutton_pressed = false; + while (SDL_PollEvent(&event)) { engine->context->convert_event_coordinates(event); switch (event.type) { @@ -180,16 +189,17 @@ bool Engine::pickATile(int* x, int* y, float maxRange) { } } - if (map->isInFov(mouse.cx, mouse.cy) - && (maxRange == 0 || player->getDistance(mouse.cx, mouse.cy) <= maxRange)) { + if (map->isInFov(mouse.cx + offset_x, mouse.cy + offset_y) + && (maxRange == 0 || player->getDistance(mouse.cx + offset_x, mouse.cy + offset_y) <= maxRange)) { console->at(mouse.cx, mouse.cy).bg = TCOD_ColorRGBA(100, 100, 100, 255); if (mouse.lbutton_pressed) { - *x = mouse.cx; - *y = mouse.cy; + *x = mouse.cx + offset_x; + *y = mouse.cy + offset_y; return true; } } if (mouse.rbutton_pressed) { + return false; } context->present(*console); diff --git a/Gui.cpp b/Gui.cpp index 8e0b8e8..f956b49 100644 --- a/Gui.cpp +++ b/Gui.cpp @@ -48,8 +48,8 @@ void Gui::render() { TCOD_ColorRGB(255,100,100), TCOD_ColorRGB(100, 0, 0)); // draw the XP bar PlayerAi* ai = (PlayerAi*)engine->player->ai; - renderBar(1, 5, BAR_WIDTH, "XP(" + std::to_string(ai->xpLevel) + ")", engine->player->destructible->xp, - ai->getNextLevelXp(), + renderBar(1, 5, BAR_WIDTH, "XP(" + std::to_string(ai->xpLevel) + ")", (float)engine->player->destructible->xp, + (float)ai->getNextLevelXp(), TCOD_ColorRGB(255, 100, 255), TCOD_ColorRGB(100,0,100)); tcod::print(con, { 3, 3 }, "Dungeon level " + std::to_string(engine->level), TCOD_ColorRGB(255, 255, 255), std::nullopt); @@ -59,7 +59,7 @@ void Gui::render() { float colorCoef = 0.4f; for (Message** it = log.begin(); it != log.end(); it++) { Message* message = *it; - tcod::print(con, { MSG_X, y }, message->text, TCOD_ColorRGB(message->col.r * colorCoef, message->col.g * colorCoef, message->col.b * colorCoef), std::nullopt); + tcod::print(con, { MSG_X, y }, message->text, TCOD_ColorRGB((int)((float)message->col.r * colorCoef), (int)((float)message->col.g * colorCoef), (int)((float)message->col.b * colorCoef)), std::nullopt); y++; if (colorCoef < 1.0f) { colorCoef += 0.3f; @@ -97,7 +97,7 @@ void Gui::message(const TCOD_ColorRGB& col, const char* text, ...) { va_list ap; char buf[128]; va_start(ap, text); - vsprintf(buf, text, ap); + vsnprintf(buf, 128, text, ap); va_end(ap); char* lineBegin = buf; @@ -130,20 +130,21 @@ void Gui::renderMouseLook() { } char buf[128] = ""; bool first = true; + std::stringstream ss; for (Actor** it = engine->actors.begin(); it != engine->actors.end(); it++) { Actor* actor = *it; // find actors under the mouse cursor if (actor->x == engine->mouse.cx && actor->y == engine->mouse.cy) { if (!first) { - strcat(buf, ", "); + ss << ", "; } else { first = false; } - strcat(buf, actor->name.c_str()); + ss << actor->name; } } - tcod::print(con, { 1, 0 }, buf, TCOD_ColorRGB(200, 200, 200), std::nullopt); + tcod::print(con, { 1, 0 }, ss.str(), TCOD_ColorRGB(200, 200, 200), std::nullopt); } void Gui::save(TCODZip& zip) { @@ -198,8 +199,8 @@ Menu::MenuItemCode Menu::pick(tcod::Context *ctx, tcod::Console *con, DisplayMod menuy += 3; } else { - static TCODImage img("menu_background1.png"); - img.blit2x(*con, 0, 0); + static TCODImage img("imgs/menu_background1.png"); + tcod::draw_quartergraphics(*con, img); TCOD_ColorRGB fg(200, 180, 50); TCOD_ColorRGB bg(0, 0, 0); menux = 7; diff --git a/menu_background1.png b/imgs/menu_background1.png similarity index 100% rename from menu_background1.png rename to imgs/menu_background1.png diff --git a/terminal10x16_gs_ro.png b/imgs/terminal10x16_gs_ro.png similarity index 100% rename from terminal10x16_gs_ro.png rename to imgs/terminal10x16_gs_ro.png