From 7f21c3bb3ff28bed6bc3257b5065835c7f43cf05 Mon Sep 17 00:00:00 2001 From: Tim Schubert Date: Tue, 26 Jan 2016 06:44:36 +0100 Subject: [PATCH] Fixed some issues with displaying the upgrade menu. --- src/Bob.cpp | 20 +++--- src/Bob.hpp | 37 ++--------- src/Events.cpp | 4 ++ src/Events.hpp | 3 + src/Gameplay.cpp | 52 +++++++-------- src/Gui.cpp | 160 ++++++++++++++++++++++++++--------------------- src/Gui.hpp | 25 ++------ 7 files changed, 146 insertions(+), 155 deletions(-) diff --git a/src/Bob.cpp b/src/Bob.cpp index 1b32f64..7d2bf66 100644 --- a/src/Bob.cpp +++ b/src/Bob.cpp @@ -23,7 +23,7 @@ void Game::handle_event(SDL_Event *event) break; case SDL_MOUSEMOTION: grid->handle_event(event); - this->side_bar->handle_event(event); + this->field_box->handle_event(event); this->upgrade_box->handle_event(event); break; case SDL_MOUSEWHEEL: @@ -31,7 +31,7 @@ void Game::handle_event(SDL_Event *event) break; case SDL_MOUSEBUTTONDOWN: this->grid->handle_event(event); - this->side_bar->handle_event(event); + this->field_box->handle_event(event); this->upgrade_box->handle_event(event); break; case SDL_KEYDOWN: @@ -83,10 +83,11 @@ void Game::handle_event(SDL_Event *event) break; default: if (event->type == BOB_MARKERUPDATE || event->type == BOB_NEXTTURNEVENT || - event->type == BOB_FIELDUPDATEEVENT || event->type == BOB_FIELDSELECTED) + event->type == BOB_FIELDUPDATEEVENT || event->type == BOB_FIELDSELECTED + || event->type == BOB_FIELDUPGRADEVENT) { this->grid->handle_event(event); - this->side_bar->handle_event(event); + this->field_box->handle_event(event); this->upgrade_box->handle_event(event); } break; @@ -131,9 +132,10 @@ void Game::render() { this->renderer->set_draw_color({0x0, 0x0, 0x0, 0x0}); this->renderer->clear(); - this->grid->render(this->renderer->get_renderer()); - this->side_bar->render(this->renderer); + this->test_box->render(this->renderer); + this->field_box->render(this->renderer); this->upgrade_box->render(this->renderer); + this->grid->render(this->renderer->get_renderer()); this->renderer->present(); } catch (const SDL_RendererException &err) @@ -170,8 +172,10 @@ int main(int, char **) std::cerr << sdl_err.what() << std::endl; SDL_Quit(); } - SDL_Rect window_dimensions = {SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGTH}; - Game *game = new Game(&window_dimensions, 2); + SDL_Rect bounds; + SDL_GetDisplayBounds(0, &bounds); + SDL_Rect window_dimensions = {SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, bounds.w, bounds.h}; + Game *game = new Game(&window_dimensions, 6); int exit_status = 1; exit_status = game->game_loop(); delete game; diff --git a/src/Bob.hpp b/src/Bob.hpp index 55a2d52..ae0f4b6 100644 --- a/src/Bob.hpp +++ b/src/Bob.hpp @@ -12,9 +12,6 @@ #include "Events.hpp" #include "Gui.hpp" -const int SCREEN_WIDTH = 800; -const int SCREEN_HEIGTH = 600; -const int SIDEBAR_WIDTH = 300; const std::string TITLE = "Bob - Battles of Bacteria"; class Game @@ -25,14 +22,14 @@ public: { this->layout = new Layout(pointy_orientation, 20, {window_dimensions->w / 2, window_dimensions->h / 2}, - {SIDEBAR_WIDTH, 0, window_dimensions->w - SIDEBAR_WIDTH, window_dimensions->h}); + {0, 0, window_dimensions->w, window_dimensions->h}); this->grid = new HexagonGrid(size, this->layout); for (int i = 0; i < 4; i++) { this->move[i] = false; } this->quit = false; - SDL_Color fg = {0xff, 0xff, 0xff, 0xff}; + SDL_Color fg = {0x00, 0x00, 0x00, 0xff}; try { this->font = load_font_from_file("/usr/share/fonts/dejavu/DejaVuSans.ttf", 12); @@ -40,31 +37,10 @@ public: this->renderer = new Renderer(this->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_TARGETTEXTURE); FieldMeta *center = this->grid->get_field({0, 0, 0}); - SDL_Rect side_bar_dimensions = {window_dimensions->x - SIDEBAR_WIDTH, 0, SIDEBAR_WIDTH, - window_dimensions->y}; - this->side_bar = new Container(this->window, this->renderer, side_bar_dimensions); - this->field_box = new FieldBox(this->renderer, - {0, 20, 0, 0}, - fg, - this->font, - center - ); - this->upgrade_box = new UpgradeBox(this->renderer, - {0, 0, 210, 140}, - fg, - this->font, - center - ); - this->test_box = new TextBox(this->renderer, - {0, 0, SIDEBAR_WIDTH, SCREEN_HEIGTH}, - fg, - this->font - ); + this->field_box = new FieldBox(this->renderer, {0, 0, 1, 1}, fg, this->font, center); + this->upgrade_box = new UpgradeBox(this->renderer, {0, 0, 1, 1}, fg, this->font, center); + this->test_box = new TextBox(this->renderer, {0, 0, 1, 1}, fg, this->font); this->test_box->set_visible(true); - this->side_bar->add(test_box); - this->side_bar->add(field_box); - //this->side_bar->add(upgrade_box); - //this->side_bar->set_visible(true); } catch (const SDL_Exception &sdl_except) { @@ -77,15 +53,14 @@ public: ~Game() { delete this->test_box; + delete this->upgrade_box; delete this->field_box; delete this->move_timer; delete this->frame_timer; - delete this->side_bar; delete this->grid; delete this->renderer; delete this->window; delete this->layout; - //delete this->events; } void render(); diff --git a/src/Events.cpp b/src/Events.cpp index a8a4f11..4b2ae19 100644 --- a/src/Events.cpp +++ b/src/Events.cpp @@ -4,6 +4,10 @@ const Uint32 BOB_NEXTTURNEVENT = register_events(1); const Uint32 BOB_MARKERUPDATE = register_events(1); const Uint32 BOB_FIELDUPDATEEVENT = register_events(1); const Uint32 BOB_FIELDSELECTED = register_events(1); +const Uint32 BOB_FIELDUPGRADEVENT = register_events(1); + + +bool Timer::MOUSE_LOCKED = false; void Timer::start_timer() { diff --git a/src/Events.hpp b/src/Events.hpp index 61b6506..38984a1 100644 --- a/src/Events.hpp +++ b/src/Events.hpp @@ -11,6 +11,7 @@ extern const Uint32 BOB_NEXTTURNEVENT; extern const Uint32 BOB_MARKERUPDATE; extern const Uint32 BOB_FIELDUPDATEEVENT; extern const Uint32 BOB_FIELDSELECTED; +extern const Uint32 BOB_FIELDUPGRADEVENT; #endif Uint32 register_events(Uint32 n); @@ -25,6 +26,8 @@ public: Uint32 get_timer(); Uint32 reset_timer(); + + static bool MOUSE_LOCKED; }; struct NextTurn diff --git a/src/Gameplay.cpp b/src/Gameplay.cpp index 03ccf41..3922a24 100644 --- a/src/Gameplay.cpp +++ b/src/Gameplay.cpp @@ -144,11 +144,11 @@ void FieldMeta::render(SDL_Renderer *renderer, Layout *layout) } if (this->owner->get_id().is_nil()) color = {0x77, 0x77, 0x77, 0x77}; - filledPolygonRGBA(renderer, vx, vy, 6, color.r, color.g, color.b, 0x33); + filledPolygonRGBA(renderer, vx, vy, 6, color.r, color.g, color.b, 0x22); SDL_Color inverse; - inverse.r = (Uint8) (0xff - color.r); - inverse.g = (Uint8) (0xff - color.g); - inverse.b = (Uint8) (0xff - color.b); + inverse.r = (Uint8) (color.r + 0x77); + inverse.g = (Uint8) (color.g + 0x77); + inverse.b = (Uint8) (color.b + 0x77); inverse.a = 0xff; double resource_size = layout->size / 4; if (this->resources_base.triangle > 0) @@ -237,7 +237,7 @@ bool FieldMeta::upgrade(Upgrade upgrade) this->upgrades[upgrade] = true; } } - this->trigger_event(BOB_FIELDUPDATEEVENT, 0); + this->trigger_event(BOB_FIELDUPGRADEVENT, 0); return this->upgrades[upgrade]; } @@ -359,7 +359,7 @@ bool HexagonGrid::render(SDL_Renderer *renderer) polygonRGBA(renderer, x, y, 6, color.r, color.g, color.b, color.a); if (elem.first == this->marker->get_field()) { - filledPolygonRGBA(renderer, x, y, 6, 0x77, 0x77, 0x77, 0x77); + filledPolygonRGBA(renderer, x, y, 6, 0x77, 0x77, 0x77, 0x22); } elem.second->render(renderer, this->layout); } @@ -376,13 +376,13 @@ void HexagonGrid::handle_event(SDL_Event *event) switch (event->type) { case SDL_MOUSEWHEEL: - if (old_size + scroll < 10) + if (old_size + scroll < 20) { - this->layout->size = 10; + this->layout->size = 20; } - else if (old_size + scroll > 100) + else if (old_size + scroll > 1000) { - this->layout->size = 100; + this->layout->size = 1000; } else { @@ -412,9 +412,6 @@ void HexagonGrid::handle_event(SDL_Event *event) case SDL_BUTTON_RIGHT: this->marker->trigger_event(BOB_FIELDSELECTED, 0); break; - //case SDL_BUTTON_LEFT: - // this->marker->trigger_event(BOB_FIELDSELECTED, 1); - // break; default: break; } @@ -439,20 +436,23 @@ void HexagonGrid::move(SDL_Point m) void HexagonGrid::update_marker() { - SDL_Point m = {0, 0}; - SDL_GetMouseState(&(m.x), &(m.y)); - Point p = {0.0, 0.0}; - p.x = m.x; - p.y = m.y; - FieldMeta *n_marker = this->point_to_field(p); - if (n_marker != nullptr) + if (!Timer::MOUSE_LOCKED) { - this->marker = n_marker; - n_marker->trigger_event(BOB_MARKERUPDATE, 0); - } - else - { - marker->trigger_event(BOB_MARKERUPDATE, 1); + SDL_Point m = {0, 0}; + SDL_GetMouseState(&(m.x), &(m.y)); + Point p = {0.0, 0.0}; + p.x = m.x; + p.y = m.y; + FieldMeta *n_marker = this->point_to_field(p); + if (n_marker != nullptr) + { + this->marker = n_marker; + n_marker->trigger_event(BOB_MARKERUPDATE, 0); + } + else + { + marker->trigger_event(BOB_MARKERUPDATE, 1); + } } } diff --git a/src/Gui.cpp b/src/Gui.cpp index 325f503..bb71e23 100644 --- a/src/Gui.cpp +++ b/src/Gui.cpp @@ -28,11 +28,6 @@ bool TextBox::load_text(std::string text) SDL_FreeSurface(surface); throw SDL_TTFException(); } - /*else if (surface->w > this->dimensions.w || surface->h > this->dimensions.h) - { - std::cerr << "Overfull TextBox!" << std::endl; - } - */ this->dimensions.w = surface->w; this->dimensions.h = surface->h; if (this->texture != nullptr) @@ -118,10 +113,20 @@ void FieldBox::handle_event(const SDL_Event *event) else if (event->type == BOB_MARKERUPDATE) { if (event->user.code == 0) + { + if (!Timer::MOUSE_LOCKED) + { + SDL_Point mouse; + SDL_GetMouseState(&mouse.x, &mouse.y); + this->update_position(mouse); + this->field = static_cast(event->user.data1); + } this->visible = true; - else + } + else if (!Timer::MOUSE_LOCKED) + { this->visible = false; - this->field = static_cast(event->user.data1); + } this->update(); } } @@ -145,74 +150,76 @@ void UpgradeButtonBox::set_active(bool state) this->active = state; if (state) { - this->color = {0x0, 0x0, 0xff, this->color.a}; + this->color.b = 0xff; this->load_text(UPGRADE_NAMES.at(this->upgrade)); } } void UpgradeBox::handle_event(const SDL_Event *event) { - if (event->type == SDL_MOUSEBUTTONDOWN && this->visible) - { - SDL_Point m_pos; - SDL_GetMouseState(&m_pos.x, &m_pos.y); - if (inside_target(&(this->dimensions), &m_pos)) - { - if (marked_upgrade != nullptr) - { - this->marked_upgrade->handle_event(event); - } - } - else - { - this->set_visible(false); - } - } - else if (event->type == BOB_FIELDSELECTED) - { - FieldMeta *selected = static_cast(event->user.data1); - if (selected != nullptr && event->user.code == 0x0) - { - this->set_visible(true); - this->field = selected; - Point p_pos = selected->get_grid()->field_to_point(selected); - SDL_Point point = {(int) p_pos.x, (int) p_pos.y}; - this->update_position(point); - } - else - { - this->set_visible(false); - } - } - else if (event->type == BOB_FIELDUPDATEEVENT) + if (event->type == BOB_FIELDUPDATEEVENT) { // mark updates active / inactive this->field = static_cast(event->user.data1); UpgradeFlags updated_upgrades = this->field->get_upgrades(); for (auto upgrade : this->upgrades) { - upgrade->set_active(updated_upgrades[upgrade->get_upgrade()]); + Upgrade up = upgrade->get_upgrade(); + bool active = updated_upgrades[up]; + upgrade->set_active(active); } } - else if (event->type == SDL_MOUSEMOTION && this->visible) + else if (this->visible) { - SDL_Point pos; - SDL_GetMouseState(&(pos.x), &(pos.y)); - // update the info text field for the selected update - for (auto box : this->upgrades) + if (event->type == SDL_MOUSEBUTTONDOWN) { - SDL_Rect target = box->get_dimensions(); - if (this->marked_upgrade != box && inside_target(&target, &pos)) + this->marked_upgrade->handle_event(event); + SDL_Point mouse; + SDL_GetMouseState(&mouse.x, &mouse.y); + if (!inside_target(&(this->dimensions), &mouse)) { - this->marked_upgrade = box; - Resource costs = UPGRADE_COSTS.at(box->get_upgrade()); - std::ostringstream output; - output //<< UPGRADE_NAMES.at(box->get_upgrade()) << "\n" - << "● " << (int) costs.circle << "\n" - << "▲ " << (int) costs.triangle << "\n" - << "■ " << (int) costs.square << "\n" - << UPGRADE_TEXTS.at(box->get_upgrade()); - this->upgrade_info->load_text(output.str()); + Timer::MOUSE_LOCKED = false; + this->set_visible(false); + } + } + else if (event->type == SDL_MOUSEMOTION && this->visible) + { + SDL_Point pos; + SDL_GetMouseState(&(pos.x), &(pos.y)); + // update the info text field for the selected update + for (auto box : this->upgrades) + { + SDL_Rect target = box->get_dimensions(); + if (this->marked_upgrade != box && inside_target(&target, &pos)) + { + this->marked_upgrade = box; + Resource costs = UPGRADE_COSTS.at(box->get_upgrade()); + std::ostringstream output; + output //<< UPGRADE_NAMES.at(box->get_upgrade()) << "\n" + << "● " << (int) costs.circle << "\n" + << "▲ " << (int) costs.triangle << "\n" + << "■ " << (int) costs.square << "\n" + << UPGRADE_TEXTS.at(box->get_upgrade()); + SDL_Rect box_dim = box->get_dimensions(); + this->upgrade_info->update_position({box_dim.x + box_dim.w + 6, box_dim.y}); + this->upgrade_info->load_text(output.str()); + } + } + } + } + else // NOT visible + { + if (event->type == BOB_FIELDSELECTED) + { + FieldMeta *selected = static_cast(event->user.data1); + if (selected != nullptr && event->user.code == 0x0) + { + Timer::MOUSE_LOCKED = true; + SDL_Point mouse; + SDL_GetMouseState(&mouse.x, &mouse.y); + this->update_position(mouse); + this->field = selected; + this->set_visible(true); } } } @@ -226,26 +233,22 @@ void UpgradeButtonBox::handle_event(const SDL_Event *event) { if (inside_target(&(this->dimensions), &pos)) { - this->box->get_field()->upgrade(this->upgrade); + FieldMeta *field = this->box->get_field(); + field->upgrade(this->upgrade); } } } void UpgradeBox::render(Renderer *ext_renderer) { - if (this->visible && this->texture != nullptr) + this->upgrade_info->render(ext_renderer); + for (auto box : this->upgrades) { - // copy background - if (SDL_RenderCopy(ext_renderer->get_renderer(), this->texture, nullptr, &(this->dimensions)) < 0) - { - throw SDL_RendererException(); - } - this->upgrade_info->render(ext_renderer); - for (auto box : this->upgrades) - { - box->render(ext_renderer); - } + box->render(ext_renderer); } + SDL_Rect dim = this->upgrades[0]->get_dimensions(); + this->dimensions.w = dim.w; + this->dimensions.h = (dim.h + 4) * (int) this->upgrades.size(); } void Container::render(Renderer *renderer) @@ -260,7 +263,16 @@ void Box::render(Renderer *ext_renderer) { if (this->visible && this->texture != nullptr) { - if (SDL_RenderCopy(ext_renderer->get_renderer(), this->texture, nullptr, &(this->dimensions)) < 0) + SDL_Color bg = {0xff, 0xff, 0xff, 0xff}; + renderer->set_draw_color(bg); + if (SDL_SetRenderDrawBlendMode(ext_renderer->get_renderer(), SDL_BLENDMODE_NONE) < 0 + || SDL_RenderFillRect(ext_renderer->get_renderer(), &(this->dimensions)) < 0) + { + throw SDL_Exception("Failed to draw rectangle background!"); + } + renderer->set_draw_color(this->color); + if (SDL_SetTextureBlendMode(this->texture, SDL_BLENDMODE_BLEND) < 0 + || SDL_RenderCopy(ext_renderer->get_renderer(), this->texture, nullptr, &(this->dimensions)) < 0) { throw SDL_RendererException(); } @@ -309,3 +321,9 @@ void UpgradeBox::set_visible(bool status) box->set_visible(status); } } + +void FieldBox::update_position(SDL_Point point) +{ + this->dimensions.x = point.x; + this->dimensions.y = point.y - dimensions.h - 6; +} \ No newline at end of file diff --git a/src/Gui.hpp b/src/Gui.hpp index fe44ad3..10f871d 100644 --- a/src/Gui.hpp +++ b/src/Gui.hpp @@ -80,7 +80,7 @@ private: class Box { public: - Box(Renderer *renderer_, SDL_Rect dimensions_, SDL_Color color_) + Box(Renderer *renderer_, const SDL_Rect dimensions_, const SDL_Color color_) : renderer(renderer_), color(color_), dimensions(dimensions_) { this->texture = nullptr; @@ -100,8 +100,6 @@ public: SDL_Rect get_dimensions() { return this->dimensions; } - void set_dimensions(SDL_Rect dimensions) { this->dimensions = dimensions; } - virtual void handle_event(const SDL_Event *event) = 0; protected: Renderer *renderer; @@ -117,7 +115,7 @@ public: TextBox(Renderer *renderer, SDL_Rect dimensions, SDL_Color color, TTF_Font *font_) : Box(renderer, dimensions, color), font(font_) { } - virtual bool load_text(std::string text); + bool load_text(std::string text); virtual void handle_event(const SDL_Event *event) { } @@ -135,6 +133,8 @@ public: virtual void update(); + void update_position(SDL_Point point); + protected: FieldMeta *field; }; @@ -166,29 +166,16 @@ public: UpgradeBox(Renderer *renderer, SDL_Rect dimensions, SDL_Color color, TTF_Font *font, FieldMeta *field_) : Box(renderer, dimensions, color), field(field_) { - ; - this->texture = SDL_CreateTexture(renderer->get_renderer(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, - dimensions.w, dimensions.h); int y = dimensions.y; for (Upgrade upgrade : UPGRADES) { - UpgradeButtonBox *box = new UpgradeButtonBox(renderer, {0, y, 0, 0}, color, font, this, upgrade); + UpgradeButtonBox *box = new UpgradeButtonBox(renderer, {0, y, 1, 1}, color, font, this, upgrade); box->load_text(UPGRADE_NAMES.at(upgrade)); y += 20; this->marked_upgrade = box; this->upgrades.push_back(box); } - this->upgrade_info = new TextBox(renderer, {0, 0, 0, 0}, color, font); - renderer->set_draw_color({0x77, 0x77, 0x77, 0x77}); - SDL_SetRenderTarget(renderer->get_renderer(), this->texture); - const SDL_Rect bg = dimensions; - if (SDL_RenderFillRect(renderer->get_renderer(), &bg) < 0) - { - SDL_DestroyTexture(this->texture); - SDL_SetRenderTarget(renderer->get_renderer(), nullptr); - throw SDL_RendererException(); - } - SDL_SetRenderTarget(renderer->get_renderer(), nullptr); + this->upgrade_info = new TextBox(renderer, {0, 0, 1, 1}, color, font); } ~UpgradeBox()