Fixed some issues with displaying the upgrade menu.
This commit is contained in:
parent
78c33d84b6
commit
7f21c3bb3f
7 changed files with 146 additions and 155 deletions
20
src/Bob.cpp
20
src/Bob.cpp
|
@ -23,7 +23,7 @@ void Game::handle_event(SDL_Event *event)
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEMOTION:
|
case SDL_MOUSEMOTION:
|
||||||
grid->handle_event(event);
|
grid->handle_event(event);
|
||||||
this->side_bar->handle_event(event);
|
this->field_box->handle_event(event);
|
||||||
this->upgrade_box->handle_event(event);
|
this->upgrade_box->handle_event(event);
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEWHEEL:
|
case SDL_MOUSEWHEEL:
|
||||||
|
@ -31,7 +31,7 @@ void Game::handle_event(SDL_Event *event)
|
||||||
break;
|
break;
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
this->grid->handle_event(event);
|
this->grid->handle_event(event);
|
||||||
this->side_bar->handle_event(event);
|
this->field_box->handle_event(event);
|
||||||
this->upgrade_box->handle_event(event);
|
this->upgrade_box->handle_event(event);
|
||||||
break;
|
break;
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
|
@ -83,10 +83,11 @@ void Game::handle_event(SDL_Event *event)
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
if (event->type == BOB_MARKERUPDATE || event->type == BOB_NEXTTURNEVENT ||
|
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->grid->handle_event(event);
|
||||||
this->side_bar->handle_event(event);
|
this->field_box->handle_event(event);
|
||||||
this->upgrade_box->handle_event(event);
|
this->upgrade_box->handle_event(event);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -131,9 +132,10 @@ void Game::render()
|
||||||
{
|
{
|
||||||
this->renderer->set_draw_color({0x0, 0x0, 0x0, 0x0});
|
this->renderer->set_draw_color({0x0, 0x0, 0x0, 0x0});
|
||||||
this->renderer->clear();
|
this->renderer->clear();
|
||||||
this->grid->render(this->renderer->get_renderer());
|
this->test_box->render(this->renderer);
|
||||||
this->side_bar->render(this->renderer);
|
this->field_box->render(this->renderer);
|
||||||
this->upgrade_box->render(this->renderer);
|
this->upgrade_box->render(this->renderer);
|
||||||
|
this->grid->render(this->renderer->get_renderer());
|
||||||
this->renderer->present();
|
this->renderer->present();
|
||||||
}
|
}
|
||||||
catch (const SDL_RendererException &err)
|
catch (const SDL_RendererException &err)
|
||||||
|
@ -170,8 +172,10 @@ int main(int, char **)
|
||||||
std::cerr << sdl_err.what() << std::endl;
|
std::cerr << sdl_err.what() << std::endl;
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
SDL_Rect window_dimensions = {SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGTH};
|
SDL_Rect bounds;
|
||||||
Game *game = new Game(&window_dimensions, 2);
|
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;
|
int exit_status = 1;
|
||||||
exit_status = game->game_loop();
|
exit_status = game->game_loop();
|
||||||
delete game;
|
delete game;
|
||||||
|
|
37
src/Bob.hpp
37
src/Bob.hpp
|
@ -12,9 +12,6 @@
|
||||||
#include "Events.hpp"
|
#include "Events.hpp"
|
||||||
#include "Gui.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";
|
const std::string TITLE = "Bob - Battles of Bacteria";
|
||||||
|
|
||||||
class Game
|
class Game
|
||||||
|
@ -25,14 +22,14 @@ public:
|
||||||
{
|
{
|
||||||
this->layout = new Layout(pointy_orientation, 20,
|
this->layout = new Layout(pointy_orientation, 20,
|
||||||
{window_dimensions->w / 2, window_dimensions->h / 2},
|
{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);
|
this->grid = new HexagonGrid(size, this->layout);
|
||||||
for (int i = 0; i < 4; i++)
|
for (int i = 0; i < 4; i++)
|
||||||
{
|
{
|
||||||
this->move[i] = false;
|
this->move[i] = false;
|
||||||
}
|
}
|
||||||
this->quit = false;
|
this->quit = false;
|
||||||
SDL_Color fg = {0xff, 0xff, 0xff, 0xff};
|
SDL_Color fg = {0x00, 0x00, 0x00, 0xff};
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
this->font = load_font_from_file("/usr/share/fonts/dejavu/DejaVuSans.ttf", 12);
|
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
|
this->renderer = new Renderer(this->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
|
||||||
| SDL_RENDERER_TARGETTEXTURE);
|
| SDL_RENDERER_TARGETTEXTURE);
|
||||||
FieldMeta *center = this->grid->get_field({0, 0, 0});
|
FieldMeta *center = this->grid->get_field({0, 0, 0});
|
||||||
SDL_Rect side_bar_dimensions = {window_dimensions->x - SIDEBAR_WIDTH, 0, SIDEBAR_WIDTH,
|
this->field_box = new FieldBox(this->renderer, {0, 0, 1, 1}, fg, this->font, center);
|
||||||
window_dimensions->y};
|
this->upgrade_box = new UpgradeBox(this->renderer, {0, 0, 1, 1}, fg, this->font, center);
|
||||||
this->side_bar = new Container(this->window, this->renderer, side_bar_dimensions);
|
this->test_box = new TextBox(this->renderer, {0, 0, 1, 1}, fg, this->font);
|
||||||
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->test_box->set_visible(true);
|
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)
|
catch (const SDL_Exception &sdl_except)
|
||||||
{
|
{
|
||||||
|
@ -77,15 +53,14 @@ public:
|
||||||
~Game()
|
~Game()
|
||||||
{
|
{
|
||||||
delete this->test_box;
|
delete this->test_box;
|
||||||
|
delete this->upgrade_box;
|
||||||
delete this->field_box;
|
delete this->field_box;
|
||||||
delete this->move_timer;
|
delete this->move_timer;
|
||||||
delete this->frame_timer;
|
delete this->frame_timer;
|
||||||
delete this->side_bar;
|
|
||||||
delete this->grid;
|
delete this->grid;
|
||||||
delete this->renderer;
|
delete this->renderer;
|
||||||
delete this->window;
|
delete this->window;
|
||||||
delete this->layout;
|
delete this->layout;
|
||||||
//delete this->events;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void render();
|
void render();
|
||||||
|
|
|
@ -4,6 +4,10 @@ const Uint32 BOB_NEXTTURNEVENT = register_events(1);
|
||||||
const Uint32 BOB_MARKERUPDATE = register_events(1);
|
const Uint32 BOB_MARKERUPDATE = register_events(1);
|
||||||
const Uint32 BOB_FIELDUPDATEEVENT = register_events(1);
|
const Uint32 BOB_FIELDUPDATEEVENT = register_events(1);
|
||||||
const Uint32 BOB_FIELDSELECTED = 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()
|
void Timer::start_timer()
|
||||||
{
|
{
|
||||||
|
|
|
@ -11,6 +11,7 @@ extern const Uint32 BOB_NEXTTURNEVENT;
|
||||||
extern const Uint32 BOB_MARKERUPDATE;
|
extern const Uint32 BOB_MARKERUPDATE;
|
||||||
extern const Uint32 BOB_FIELDUPDATEEVENT;
|
extern const Uint32 BOB_FIELDUPDATEEVENT;
|
||||||
extern const Uint32 BOB_FIELDSELECTED;
|
extern const Uint32 BOB_FIELDSELECTED;
|
||||||
|
extern const Uint32 BOB_FIELDUPGRADEVENT;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Uint32 register_events(Uint32 n);
|
Uint32 register_events(Uint32 n);
|
||||||
|
@ -25,6 +26,8 @@ public:
|
||||||
Uint32 get_timer();
|
Uint32 get_timer();
|
||||||
|
|
||||||
Uint32 reset_timer();
|
Uint32 reset_timer();
|
||||||
|
|
||||||
|
static bool MOUSE_LOCKED;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct NextTurn
|
struct NextTurn
|
||||||
|
|
|
@ -144,11 +144,11 @@ void FieldMeta::render(SDL_Renderer *renderer, Layout *layout)
|
||||||
}
|
}
|
||||||
if (this->owner->get_id().is_nil())
|
if (this->owner->get_id().is_nil())
|
||||||
color = {0x77, 0x77, 0x77, 0x77};
|
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;
|
SDL_Color inverse;
|
||||||
inverse.r = (Uint8) (0xff - color.r);
|
inverse.r = (Uint8) (color.r + 0x77);
|
||||||
inverse.g = (Uint8) (0xff - color.g);
|
inverse.g = (Uint8) (color.g + 0x77);
|
||||||
inverse.b = (Uint8) (0xff - color.b);
|
inverse.b = (Uint8) (color.b + 0x77);
|
||||||
inverse.a = 0xff;
|
inverse.a = 0xff;
|
||||||
double resource_size = layout->size / 4;
|
double resource_size = layout->size / 4;
|
||||||
if (this->resources_base.triangle > 0)
|
if (this->resources_base.triangle > 0)
|
||||||
|
@ -237,7 +237,7 @@ bool FieldMeta::upgrade(Upgrade upgrade)
|
||||||
this->upgrades[upgrade] = true;
|
this->upgrades[upgrade] = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this->trigger_event(BOB_FIELDUPDATEEVENT, 0);
|
this->trigger_event(BOB_FIELDUPGRADEVENT, 0);
|
||||||
return this->upgrades[upgrade];
|
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);
|
polygonRGBA(renderer, x, y, 6, color.r, color.g, color.b, color.a);
|
||||||
if (elem.first == this->marker->get_field())
|
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);
|
elem.second->render(renderer, this->layout);
|
||||||
}
|
}
|
||||||
|
@ -376,13 +376,13 @@ void HexagonGrid::handle_event(SDL_Event *event)
|
||||||
switch (event->type)
|
switch (event->type)
|
||||||
{
|
{
|
||||||
case SDL_MOUSEWHEEL:
|
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
|
else
|
||||||
{
|
{
|
||||||
|
@ -412,9 +412,6 @@ void HexagonGrid::handle_event(SDL_Event *event)
|
||||||
case SDL_BUTTON_RIGHT:
|
case SDL_BUTTON_RIGHT:
|
||||||
this->marker->trigger_event(BOB_FIELDSELECTED, 0);
|
this->marker->trigger_event(BOB_FIELDSELECTED, 0);
|
||||||
break;
|
break;
|
||||||
//case SDL_BUTTON_LEFT:
|
|
||||||
// this->marker->trigger_event(BOB_FIELDSELECTED, 1);
|
|
||||||
// break;
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -438,6 +435,8 @@ void HexagonGrid::move(SDL_Point m)
|
||||||
}
|
}
|
||||||
|
|
||||||
void HexagonGrid::update_marker()
|
void HexagonGrid::update_marker()
|
||||||
|
{
|
||||||
|
if (!Timer::MOUSE_LOCKED)
|
||||||
{
|
{
|
||||||
SDL_Point m = {0, 0};
|
SDL_Point m = {0, 0};
|
||||||
SDL_GetMouseState(&(m.x), &(m.y));
|
SDL_GetMouseState(&(m.x), &(m.y));
|
||||||
|
@ -455,6 +454,7 @@ void HexagonGrid::update_marker()
|
||||||
marker->trigger_event(BOB_MARKERUPDATE, 1);
|
marker->trigger_event(BOB_MARKERUPDATE, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
FieldMeta *HexagonGrid::point_to_field(const Point p)
|
FieldMeta *HexagonGrid::point_to_field(const Point p)
|
||||||
{
|
{
|
||||||
|
|
124
src/Gui.cpp
124
src/Gui.cpp
|
@ -28,11 +28,6 @@ bool TextBox::load_text(std::string text)
|
||||||
SDL_FreeSurface(surface);
|
SDL_FreeSurface(surface);
|
||||||
throw SDL_TTFException();
|
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.w = surface->w;
|
||||||
this->dimensions.h = surface->h;
|
this->dimensions.h = surface->h;
|
||||||
if (this->texture != nullptr)
|
if (this->texture != nullptr)
|
||||||
|
@ -118,10 +113,20 @@ void FieldBox::handle_event(const SDL_Event *event)
|
||||||
else if (event->type == BOB_MARKERUPDATE)
|
else if (event->type == BOB_MARKERUPDATE)
|
||||||
{
|
{
|
||||||
if (event->user.code == 0)
|
if (event->user.code == 0)
|
||||||
this->visible = true;
|
{
|
||||||
else
|
if (!Timer::MOUSE_LOCKED)
|
||||||
this->visible = false;
|
{
|
||||||
|
SDL_Point mouse;
|
||||||
|
SDL_GetMouseState(&mouse.x, &mouse.y);
|
||||||
|
this->update_position(mouse);
|
||||||
this->field = static_cast<FieldMeta *>(event->user.data1);
|
this->field = static_cast<FieldMeta *>(event->user.data1);
|
||||||
|
}
|
||||||
|
this->visible = true;
|
||||||
|
}
|
||||||
|
else if (!Timer::MOUSE_LOCKED)
|
||||||
|
{
|
||||||
|
this->visible = false;
|
||||||
|
}
|
||||||
this->update();
|
this->update();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,53 +150,36 @@ void UpgradeButtonBox::set_active(bool state)
|
||||||
this->active = state;
|
this->active = state;
|
||||||
if (state)
|
if (state)
|
||||||
{
|
{
|
||||||
this->color = {0x0, 0x0, 0xff, this->color.a};
|
this->color.b = 0xff;
|
||||||
this->load_text(UPGRADE_NAMES.at(this->upgrade));
|
this->load_text(UPGRADE_NAMES.at(this->upgrade));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpgradeBox::handle_event(const SDL_Event *event)
|
void UpgradeBox::handle_event(const SDL_Event *event)
|
||||||
{
|
{
|
||||||
if (event->type == SDL_MOUSEBUTTONDOWN && this->visible)
|
if (event->type == BOB_FIELDUPDATEEVENT)
|
||||||
{
|
|
||||||
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<FieldMeta *>(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)
|
|
||||||
{
|
{
|
||||||
// mark updates active / inactive
|
// mark updates active / inactive
|
||||||
this->field = static_cast<FieldMeta *>(event->user.data1);
|
this->field = static_cast<FieldMeta *>(event->user.data1);
|
||||||
UpgradeFlags updated_upgrades = this->field->get_upgrades();
|
UpgradeFlags updated_upgrades = this->field->get_upgrades();
|
||||||
for (auto upgrade : this->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 (this->visible)
|
||||||
|
{
|
||||||
|
if (event->type == SDL_MOUSEBUTTONDOWN)
|
||||||
|
{
|
||||||
|
this->marked_upgrade->handle_event(event);
|
||||||
|
SDL_Point mouse;
|
||||||
|
SDL_GetMouseState(&mouse.x, &mouse.y);
|
||||||
|
if (!inside_target(&(this->dimensions), &mouse))
|
||||||
|
{
|
||||||
|
Timer::MOUSE_LOCKED = false;
|
||||||
|
this->set_visible(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (event->type == SDL_MOUSEMOTION && this->visible)
|
else if (event->type == SDL_MOUSEMOTION && this->visible)
|
||||||
|
@ -212,11 +200,30 @@ void UpgradeBox::handle_event(const SDL_Event *event)
|
||||||
<< "▲ " << (int) costs.triangle << "\n"
|
<< "▲ " << (int) costs.triangle << "\n"
|
||||||
<< "■ " << (int) costs.square << "\n"
|
<< "■ " << (int) costs.square << "\n"
|
||||||
<< UPGRADE_TEXTS.at(box->get_upgrade());
|
<< 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());
|
this->upgrade_info->load_text(output.str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else // NOT visible
|
||||||
|
{
|
||||||
|
if (event->type == BOB_FIELDSELECTED)
|
||||||
|
{
|
||||||
|
FieldMeta *selected = static_cast<FieldMeta *>(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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void UpgradeButtonBox::handle_event(const SDL_Event *event)
|
void UpgradeButtonBox::handle_event(const SDL_Event *event)
|
||||||
{
|
{
|
||||||
|
@ -226,26 +233,22 @@ void UpgradeButtonBox::handle_event(const SDL_Event *event)
|
||||||
{
|
{
|
||||||
if (inside_target(&(this->dimensions), &pos))
|
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)
|
void UpgradeBox::render(Renderer *ext_renderer)
|
||||||
{
|
{
|
||||||
if (this->visible && this->texture != nullptr)
|
|
||||||
{
|
|
||||||
// copy background
|
|
||||||
if (SDL_RenderCopy(ext_renderer->get_renderer(), this->texture, nullptr, &(this->dimensions)) < 0)
|
|
||||||
{
|
|
||||||
throw SDL_RendererException();
|
|
||||||
}
|
|
||||||
this->upgrade_info->render(ext_renderer);
|
this->upgrade_info->render(ext_renderer);
|
||||||
for (auto box : this->upgrades)
|
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)
|
void Container::render(Renderer *renderer)
|
||||||
|
@ -260,7 +263,16 @@ void Box::render(Renderer *ext_renderer)
|
||||||
{
|
{
|
||||||
if (this->visible && this->texture != nullptr)
|
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();
|
throw SDL_RendererException();
|
||||||
}
|
}
|
||||||
|
@ -309,3 +321,9 @@ void UpgradeBox::set_visible(bool status)
|
||||||
box->set_visible(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;
|
||||||
|
}
|
25
src/Gui.hpp
25
src/Gui.hpp
|
@ -80,7 +80,7 @@ private:
|
||||||
class Box
|
class Box
|
||||||
{
|
{
|
||||||
public:
|
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_)
|
: renderer(renderer_), color(color_), dimensions(dimensions_)
|
||||||
{
|
{
|
||||||
this->texture = nullptr;
|
this->texture = nullptr;
|
||||||
|
@ -100,8 +100,6 @@ public:
|
||||||
|
|
||||||
SDL_Rect get_dimensions() { return this->dimensions; }
|
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;
|
virtual void handle_event(const SDL_Event *event) = 0;
|
||||||
protected:
|
protected:
|
||||||
Renderer *renderer;
|
Renderer *renderer;
|
||||||
|
@ -117,7 +115,7 @@ public:
|
||||||
TextBox(Renderer *renderer, SDL_Rect dimensions, SDL_Color color, TTF_Font *font_)
|
TextBox(Renderer *renderer, SDL_Rect dimensions, SDL_Color color, TTF_Font *font_)
|
||||||
: Box(renderer, dimensions, color), 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) { }
|
virtual void handle_event(const SDL_Event *event) { }
|
||||||
|
|
||||||
|
@ -135,6 +133,8 @@ public:
|
||||||
|
|
||||||
virtual void update();
|
virtual void update();
|
||||||
|
|
||||||
|
void update_position(SDL_Point point);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
FieldMeta *field;
|
FieldMeta *field;
|
||||||
};
|
};
|
||||||
|
@ -166,29 +166,16 @@ public:
|
||||||
UpgradeBox(Renderer *renderer, SDL_Rect dimensions, SDL_Color color, TTF_Font *font, FieldMeta *field_)
|
UpgradeBox(Renderer *renderer, SDL_Rect dimensions, SDL_Color color, TTF_Font *font, FieldMeta *field_)
|
||||||
: Box(renderer, dimensions, color), field(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;
|
int y = dimensions.y;
|
||||||
for (Upgrade upgrade : UPGRADES)
|
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));
|
box->load_text(UPGRADE_NAMES.at(upgrade));
|
||||||
y += 20;
|
y += 20;
|
||||||
this->marked_upgrade = box;
|
this->marked_upgrade = box;
|
||||||
this->upgrades.push_back(box);
|
this->upgrades.push_back(box);
|
||||||
}
|
}
|
||||||
this->upgrade_info = new TextBox(renderer, {0, 0, 0, 0}, color, font);
|
this->upgrade_info = new TextBox(renderer, {0, 0, 1, 1}, 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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
~UpgradeBox()
|
~UpgradeBox()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue