Give the option to add new players, start the game and procede to the next turn using text input.

This commit is contained in:
Tim Schubert 2016-01-28 19:41:28 +01:00
parent 921c937439
commit a2ddbcbeeb
6 changed files with 68 additions and 36 deletions

View file

@ -45,7 +45,6 @@ void Game::handle_event(SDL_Event *event)
this->grid->handle_event(event); this->grid->handle_event(event);
this->field_box->handle_event(event); this->field_box->handle_event(event);
this->upgrade_box->handle_event(event); this->upgrade_box->handle_event(event);
this->next_turn_button->handle_event(event);
} }
break; break;
case SDL_KEYDOWN: case SDL_KEYDOWN:
@ -72,8 +71,7 @@ void Game::handle_event(SDL_Event *event)
{ {
window_size = this->window->toggle_fullscreen(); window_size = this->window->toggle_fullscreen();
this->grid->update_dimensions(window_size); this->grid->update_dimensions(window_size);
this->text_input_box->update_dimensions({0, window_size.y - 20, 0, 20}); this->text_input_box->update_dimensions({0, 0, window_size.x, 0});
this->next_turn_button->update_position({window_size.x - 100, window_size.y - 100});
this->upgrade_box->set_visible(false); this->upgrade_box->set_visible(false);
} }
break; break;
@ -162,35 +160,56 @@ void Game::command(std::string input)
{ {
prompt << "This is a test!"; prompt << "This is a test!";
} }
else if (input == "next" && this->started)
{
Player *last_player = Player::current_player;
this->turn = this->turn + 1;
if (this->turn == players.size())
{
this->turn = 0;
trigger_event(BOB_NEXTROUNDEVENT, 0, (void *) last_player, (void *) Player::current_player);
}
else
{
trigger_event(BOB_NEXTTURNEVENT, 0, (void *) last_player, (void *) Player::current_player);
}
Player::current_player = players[turn];
prompt << "Next player is: " << (Player::current_player)->get_name();
}
else if (input == "surrender") else if (input == "surrender")
{ {
//Player::current_player->surrender(); //Player::current_player->surrender();
} }
else if (!this->started) else if (!this->started && input.substr(0, 10) == "add player")
{
if (input.substr(0, 11) == "add player")
{ {
Player *added = new Player(input.substr(11, std::string::npos)); Player *added = new Player(input.substr(11, std::string::npos));
/*if (!this->grid->place(added)) if (!this->grid->place(added))
{ {
this->text_input_box->output << "Failed to add player:" << added->get_name(); prompt << "Failed to add player:" << added->get_name();
delete added; delete added;
} }
else else
{ {
this->players.push_back(added); this->players.push_back(added);
prompt << "Added player " << added->get_name();
} }
*/
} }
else if (input == "start") else if (input == "start" && !this->started)
{ {
//this->start_game(); this->start();
this->started = true;
prompt << "Started the game."; prompt << "Started the game.";
} }
}
this->text_input_box->prompt(prompt.str()); this->text_input_box->prompt(prompt.str());
} }
void Game::start()
{
std::random_shuffle(players.begin(), players.end());
this->turn = 0;
Player::current_player = players[0];
}
int Game::game_loop() int Game::game_loop()
{ {
this->frame_timer->start_timer(); this->frame_timer->start_timer();
@ -234,7 +253,6 @@ void Game::render()
this->test_box->render(this->renderer); this->test_box->render(this->renderer);
this->field_box->render(this->renderer); this->field_box->render(this->renderer);
this->upgrade_box->render(this->renderer); this->upgrade_box->render(this->renderer);
this->next_turn_button->render(this->renderer);
this->text_input_box->render(this->renderer); this->text_input_box->render(this->renderer);
this->renderer->present(); this->renderer->present();
} }

View file

@ -58,6 +58,8 @@ public:
} }
this->frame_timer = new Timer(); this->frame_timer = new Timer();
this->move_timer = new Timer(); this->move_timer = new Timer();
this->turn = 0;
Player::current_player = this->players[turn];
} }
~Game() ~Game()
@ -79,6 +81,8 @@ public:
delete this->layout; delete this->layout;
} }
void start();
void command(std::string command); void command(std::string command);
void render(); void render();
@ -89,6 +93,7 @@ public:
private: private:
bool started; bool started;
Uint64 turn;
TextInputBox *text_input_box; TextInputBox *text_input_box;
std::vector<Player *> players; std::vector<Player *> players;
NextTurnButtonBox *next_turn_button; NextTurnButtonBox *next_turn_button;

View file

@ -598,3 +598,8 @@ bool inside_target(const SDL_Rect *target, const SDL_Point *position)
return target->x < position->x && target->x + target->w > position->x && target->y < position->y && return target->x < position->x && target->x + target->w > position->x && target->y < position->y &&
target->y + target->h > position->y; target->y + target->h > position->y;
} }
bool HexagonGrid::place(Player *player)
{
return true;
}

View file

@ -605,6 +605,7 @@ public:
bool get_fighting() { return this->fighting; } bool get_fighting() { return this->fighting; }
void set_fighting(bool state) { this->fighting = state; } void set_fighting(bool state) { this->fighting = state; }
private: private:
bool fighting; bool fighting;
const Field field; const Field field;
@ -685,6 +686,8 @@ public:
void handle_event(SDL_Event *event); void handle_event(SDL_Event *event);
bool place(Player *player);
private: private:
bool changed; bool changed;
FieldMeta *first_attack; FieldMeta *first_attack;

View file

@ -25,21 +25,19 @@ bool TextBox::load_text(std::string text)
const char *displayed_text = text.c_str(); const char *displayed_text = text.c_str();
SDL_Surface *text_surface = TTF_RenderUTF8_Blended_Wrapped(this->font, displayed_text, this->color, SDL_Surface *text_surface = TTF_RenderUTF8_Blended_Wrapped(this->font, displayed_text, this->color,
this->dimensions.w); this->dimensions.w);
this->dimensions.h = text_surface->h;
if (text_surface == nullptr) if (text_surface == nullptr)
{ {
SDL_FreeSurface(text_surface); SDL_FreeSurface(text_surface);
throw SDL_TTFException(); throw SDL_TTFException();
} }
//this->dimensions.w = text_surface->w; SDL_Surface *surface = SDL_CreateRGBSurface(0, this->dimensions.w, text_surface->h, 32, rmask, gmask, bmask,
//this->dimensions.h = text_surface->h;
SDL_Surface *surface = SDL_CreateRGBSurface(0, this->dimensions.w, this->dimensions.h, 32, rmask, gmask, bmask,
amask); amask);
if (surface == nullptr || SDL_LockSurface(surface) < 0 || if (surface == nullptr || SDL_LockSurface(surface) < 0 ||
SDL_FillRect(surface, nullptr, SDL_MapRGB(surface->format, 255, 255, 255)) < 0) SDL_FillRect(surface, nullptr, SDL_MapRGB(surface->format, 255, 255, 255)) < 0)
{ {
throw SDL_Exception("Failed to fill rect behind text!"); throw SDL_Exception("Failed to fill rect behind text!");
} }
//SDL_Rect text_rect = {this->dimensions.x, this->dimensions.y, surface->w, surface->h};
SDL_UnlockSurface(surface); SDL_UnlockSurface(surface);
if (SDL_BlitSurface(text_surface, nullptr, surface, nullptr) < 0) if (SDL_BlitSurface(text_surface, nullptr, surface, nullptr) < 0)
{ {
@ -220,7 +218,7 @@ void UpgradeBox::render(Renderer *ext_renderer)
} }
SDL_Rect dim = this->upgrades[0]->get_dimensions(); SDL_Rect dim = this->upgrades[0]->get_dimensions();
this->dimensions.w = dim.w; this->dimensions.w = dim.w;
this->dimensions.h = (dim.h + 4) * (int) this->upgrades.size(); this->dimensions.h = dim.h * (int) this->upgrades.size();
} }
void Container::render(Renderer *renderer) void Container::render(Renderer *renderer)
@ -267,9 +265,9 @@ void UpgradeBox::update_position(SDL_Point pos)
for (auto box : this->upgrades) for (auto box : this->upgrades)
{ {
box->update_position(d_pos); box->update_position(d_pos);
d_pos.y += 20; d_pos.y += box->get_dimensions().h;
} }
this->upgrade_info->update_position({pos.x + 110, pos.y}); this->upgrade_info->update_position({pos.x + this->upgrades[0]->get_dimensions().w, pos.y});
} }
void UpgradeBox::set_visible(bool status) void UpgradeBox::set_visible(bool status)
@ -339,8 +337,11 @@ void TextInputBox::handle_event(const SDL_Event *event)
} }
else if (event->key.keysym.sym == SDLK_BACKSPACE) else if (event->key.keysym.sym == SDLK_BACKSPACE)
{ {
input << '\b'; std::string foo = input.str();
input << " "; if (!foo.empty())
foo.pop_back();
input.str(foo);
input.seekp(foo.length());
changed = true; changed = true;
} }
else if (event->key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL) else if (event->key.keysym.sym == SDLK_c && SDL_GetModState() & KMOD_CTRL)
@ -379,11 +380,10 @@ void TextInputBox::handle_event(const SDL_Event *event)
void TextInputBox::prompt(std::string message) void TextInputBox::prompt(std::string message)
{ {
this->output << this->input.str() << "\n" << message << "\n# "; this->output << this->input.str() << "\n" << message << "\n" << Player::current_player->get_name() << "# ";
this->lines += 2; this->lines += 2;
this->dimensions.h = (this->font_height * lines);
this->load_text(output.str()); this->load_text(output.str());
if (this->dimensions.h > 500) if (lines > 20)
{ {
this->lines = 2; this->lines = 2;
output.str(""); output.str("");
@ -404,6 +404,7 @@ void TextInputBox::update_dimensions(SDL_Rect rect)
this->dimensions.x = rect.x; this->dimensions.x = rect.x;
this->dimensions.y = rect.y; this->dimensions.y = rect.y;
this->dimensions.w = rect.w; this->dimensions.w = rect.w;
this->load_text(output.str());
} }
bool TextInputBox::get_active() bool TextInputBox::get_active()

View file

@ -51,7 +51,7 @@ class TextBox : public Box
{ {
public: 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_), lines(1)
{ {
this->font_height = TTF_FontHeight(font); this->font_height = TTF_FontHeight(font);
} }
@ -62,6 +62,7 @@ public:
protected: protected:
TTF_Font *font; TTF_Font *font;
Uint16 lines;
int font_height; int font_height;
}; };
@ -69,10 +70,10 @@ class TextInputBox : TextBox
{ {
public: public:
TextInputBox(Renderer *renderer_, SDL_Rect dimensions_, SDL_Color color_, TTF_Font *font_) TextInputBox(Renderer *renderer_, SDL_Rect dimensions_, SDL_Color color_, TTF_Font *font_)
: TextBox(renderer_, dimensions_, color_, font_), input(""), lines(1) : TextBox(renderer_, dimensions_, color_, font_), input("")
{ {
this->visible = false; this->visible = false;
this->output << "# "; this->output << Player::current_player->get_name() << "# ";
this->load_text(output.str()); this->load_text(output.str());
} }
@ -93,7 +94,6 @@ public:
void prompt(std::string message); void prompt(std::string message);
private: private:
Uint16 lines;
std::ostringstream output; // what is loaded to the texture - currnt input std::ostringstream output; // what is loaded to the texture - currnt input
std::stringstream input; // editable command prompt std::stringstream input; // editable command prompt
}; };