Skip to content

Commit

Permalink
Make number of players/CPUs menu options
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Berntsen authored and stiell committed Jul 7, 2013
1 parent 159e9db commit 700e831
Show file tree
Hide file tree
Showing 11 changed files with 342 additions and 29 deletions.
5 changes: 4 additions & 1 deletion src/event_code.hxx
Expand Up @@ -21,7 +21,10 @@
#define CODE_HXX_

enum EventCode {
NEW_GAME
NEW_GAME,
CHANGE_PLAYERS,
CHANGE_CPUS,
NUM_EVENT_CODE
};

#endif /* CODE_HXX_ */
8 changes: 5 additions & 3 deletions src/game.hxx
Expand Up @@ -29,7 +29,7 @@
class Game: public EventHandler {
public:
/** Initialise the game. */
Game(Screen* screen);
Game(Screen* screen, int numPlayers, int numCPUs);
virtual ~Game();
/** Handle input. */
bool handle(const SDL_Event& event);
Expand All @@ -46,8 +46,6 @@ private:
static const int _MAX_PC;
/** Max planets. */
static const int _MAX_PLAN;
/** Number of players. */
static const int _NUM_PLAYERS;
/** Planet colour. */
static const float _COL_PLANET[];
/** Planet gravity "mass". */
Expand All @@ -58,6 +56,10 @@ private:
static const phys_t _S;
/** Planet radius. */
static const phys_t _PR;
/** Number of players. */
int numPlayers_;
/** Number of AIs. */
int numCPUs_;
std::vector<AstroBody*> planets_;
BackgroundModifier* backgroundModifier_;
Camera* camera_;
Expand Down
37 changes: 23 additions & 14 deletions src/game/game.cxx
Expand Up @@ -31,7 +31,6 @@

const int Game::_MAX_PC = 16;
const int Game::_MAX_PLAN = 1;
const int Game::_NUM_PLAYERS = 3;
const float Game::_COL_PLANET[] = { 0.4, 0.8, 0.4 };
const phys_t Game::_GM = 628;
const phys_t Game::_R = 9.0;
Expand All @@ -46,7 +45,7 @@ bool Game::handle(const SDL_Event& event) {
return true;
}

Game::Game(Screen* screen) :
Game::Game(Screen* screen, int numPlayers, int numCPUs) :
screen_(screen),
planets_(),
backgroundModifier_(NULL),
Expand All @@ -56,6 +55,8 @@ Game::Game(Screen* screen) :
planetColour_(NULL),
universe_(NULL),
tex_(0),
numPlayers_(numPlayers),
numCPUs_(numCPUs),
planetFixture_(NULL),
massIndicatorLabels_(),
planetCircle_(NULL),
Expand All @@ -80,12 +81,21 @@ Game::Game(Screen* screen) :
planetDisk_->addModifier(planetFixture_);
planetDisk_->getDisk()->addModifier(planetColour_);
// Bad hard coding incoming. The game is hard coded for three players.
ConfigParser::readBindings(players_[0],
PACKAGE_CFG_DIR "controllers1.conf");
ConfigParser::readBindings(players_[1],
PACKAGE_CFG_DIR "controllers2.conf");
ConfigParser::readBindings(players_[2],
PACKAGE_CFG_DIR "controllers3.conf");
switch (numPlayers_) {
default:
if (numPlayers_ < 1)
break;
;
case 3:
ConfigParser::readBindings(players_[2],
PACKAGE_CFG_DIR "controllers3.conf");
case 2:
ConfigParser::readBindings(players_[1],
PACKAGE_CFG_DIR "controllers2.conf");
case 1:
ConfigParser::readBindings(players_[0],
PACKAGE_CFG_DIR "controllers1.conf");
}
// TODO: When the game is abstracted for more players, fix ^
#if VERBOSE
printf("controllers player 1:\n"
Expand Down Expand Up @@ -173,19 +183,19 @@ Game::~Game() {

void Game::conceive() {
// Characters
phys_t angle = 2 * PI / _NUM_PLAYERS;
phys_t angle = 2 * PI / numPlayers_;
vector2p pos = { _R, 0 }, vel = { 0, _S }, a = vector2p::fromAngle(angle);
matCharBody_ = new Material(100.0, 0.5);
matCharHead_ = new Material(10000.0, 0.1);
matCharLimbs_ = new Material(50000.0, 1.5);
matCharLimbsOff_ = new Material(500.0, 1.5);
for (int i = 0; i < _NUM_PLAYERS; ++i) {
char font[256];
getFont(font, sizeof(font));
for (int i = 0; i < numPlayers_; ++i) {
characters_.push_back(new Character(state2p()(pos, vel), i * angle,
matCharBody_, matCharHead_, matCharLimbs_, matCharLimbsOff_));
players_.push_back(new Player(characters_[i]));
characterGraphics_.push_back(new CharacterGraphic(characters_[i]));
char font[256];
getFont(font, sizeof(font));
char mass [4];
snprintf(mass, sizeof(mass), "%.0f", characters_[i]->getMass());
massIndicatorLabels_.push_back(new Label(font, mass, 74, .05,
Expand All @@ -194,7 +204,7 @@ void Game::conceive() {
massIndicatorGfx_.push_back(new MassIndicatorGraphic(0.05f, 0.02f,
massIndicators_[i], massIndicatorLabels_[i]));
massIndicatorPosMods_.push_back(new PositionModifier(i + 1,
_NUM_PLAYERS, true, -0.9));
numPlayers_, true, -0.9));
massIndicatorGfx_[i]->addModifier(massIndicatorPosMods_[i]);
massIndicatorGfx_[i]->addModifier(
characterGraphics_[i]->getColourModifier());
Expand Down Expand Up @@ -284,4 +294,3 @@ void Game::draw() {
massIndicatorLabels_[i]->setText(mass);
}
}

70 changes: 65 additions & 5 deletions src/game_loop.cxx
Expand Up @@ -20,6 +20,7 @@

#include <GL/gl.h>
#include "repeat.hxx"
#include "get_font.hxx"
#include "game_loop.hxx"
#include "event_code.hxx"
#include "step_timer.hxx"
Expand All @@ -31,9 +32,24 @@ GameLoop::GameLoop() :
limbsOff_(NULL),
running_(true),
menuP_(true),
inputP_(false),
userInput_(),
numPlayers_(1),
numCPUs_(0),
activeInput_(NUM_EVENT_CODE),
keystate_(SDL_GetKeyState(NULL)),
stepCounter_(0),
menu_() {
menu_(),
inputFieldGraphic_(NULL) {
// This is necessary for the input field
SDL_EnableUNICODE(1);
// Stores user input from the input field in menu
userInput_ = (char*) malloc(32);
userInput_[0] = '\0';
}

GameLoop::~GameLoop() {
free(userInput_);
}

bool GameLoop::handleEvents() {
Expand All @@ -59,19 +75,57 @@ bool GameLoop::handleEvents() {
continue;
// Menu
if (event.type == SDL_KEYDOWN &&
event.key.keysym.sym == SDLK_ESCAPE)
event.key.keysym.sym == SDLK_ESCAPE && !inputP_)
menuP_ = !menuP_;
if (menuP_)
// Input field
if (inputP_) {
if (!menu_.getChoice(event, userInput_)) {
// Update the logic's text
menu_.getInputField()->setText(userInput_);
// Update the graphic's text
inputFieldGraphic_->setText();
}
else {
inputP_ = false;
menuP_ = true;
switch (activeInput_) {
case CHANGE_PLAYERS:
numPlayers_ = max(strtol(userInput_, 0, 10), 1L);
break;
case CHANGE_CPUS:
numCPUs_ = max(strtol(userInput_, 0, 10), 1L);
break;
default:
;
}
userInput_[0] = '\0';
}
}
else if (menuP_)
menuP_ = !menu_.handle(event);
// Game
else
if (limbsOff_)
limbsOff_->handle(event);
// Userevents
if (event.type == SDL_USEREVENT) {
if (event.user.code == NEW_GAME) {
switch (event.user.code) {
case NEW_GAME:
if (limbsOff_)
delete limbsOff_;
limbsOff_ = new Game(screen_);
limbsOff_ = new Game(screen_, numPlayers_, numCPUs_);
break;
case CHANGE_PLAYERS:
activeInput_ = CHANGE_PLAYERS;
inputP_ = true;
break;
case CHANGE_CPUS:
activeInput_ = CHANGE_CPUS;
inputP_ = true;
inputP_ = true;
break;
default:
;
}
}
}
Expand All @@ -87,6 +141,9 @@ int GameLoop::run() {
for (int i = 0; i < j; ++i)
SDL_JoystickOpen(i);
MenuGraphic menugraphic(&menu_);
char font[256];
getFont(font, sizeof(font));
inputFieldGraphic_ = new InputFieldGraphic(font, menu_.getInputField());
Uint32 time = SDL_GetTicks();
while (running_) {
int steps = timer.getStepTime() * _STEPS_PER_SECOND;
Expand All @@ -102,6 +159,9 @@ int GameLoop::run() {
handleEvents();
// Draw
glClear(GL_COLOR_BUFFER_BIT);
// Input field
if (inputP_)
inputFieldGraphic_->draw();
// Menu
if (menuP_)
menugraphic.draw();
Expand Down
8 changes: 7 additions & 1 deletion src/game_loop.hxx
Expand Up @@ -21,6 +21,7 @@
#include <SDL/SDL.h>
#include "game.hxx"
#include "menu.hxx"
#include "event_code.hxx"

class GameLoop {
public:
Expand All @@ -31,13 +32,18 @@ public:
/** Number of simulation steps between each event update. */
static const int _EVENT_INTERVAL = 20;
GameLoop();
~GameLoop();
int run();
private:
GameLoop(const GameLoop&);
GameLoop& operator=(const GameLoop&);
bool running_, menuP_;
int numPlayers_, numCPUs_;
bool running_, menuP_, inputP_;
char* userInput_;
EventCode activeInput_;
Screen* screen_;
Game* limbsOff_;
InputFieldGraphic* inputFieldGraphic_;
Menu menu_;
int prevWidth_, prevHeight_;
Uint8* keystate_;
Expand Down
29 changes: 29 additions & 0 deletions src/graphics/game_graphics_gl.hxx
Expand Up @@ -131,6 +131,35 @@ private:
ScreenGraphic& operator=(const ScreenGraphic&);
};

class InputFieldGraphic: public ScreenGraphic {
public:
InputFieldGraphic(const char* face, ScreenElement* logic);
~InputFieldGraphic();
char* getText();
void doDraw();
void setFace(const char* face);
void setText();
private:
ScreenElement* logic_; // Why the fuck isn't this inherited?
InputFieldGraphic(const InputFieldGraphic&);
InputFieldGraphic& operator=(const InputFieldGraphic&);
/** The font name. */
char* face_;
/** The printed text. */
char* text_;
/** The size of the field. */
GLfloat width_, height_;
/* The texture created from surface_. */
GLuint texture_;
/** The font size. */
int size_;
/** The actual TTF_Font. */
TTF_Font* font_;
void drawShadow();
void make();
};


class Label: public Graphic {
public:
Label(const char* face, const char* text, int size, GLfloat width,
Expand Down

0 comments on commit 700e831

Please sign in to comment.