Permalink
Browse files

Implement mass indicators

Implement MassIndicator and MassIndicatorGraphic. The former holds the
logic, the latter the graphic.

Add the GuiElement and HudElement subclasses of ScreenElement to
properly distinguish the two.

Create Character::getMass(). Use this when updating the correlated mass
indicator text.

Implement horizontal functionality and perpendicular offset for
PositionModifier. Use this to distribute the MassIndicatorGraphics
horizontally at the bottom of the screen.

Implement CharacterGraphic::getColourModifier(). Use CharacterGraphic's
ColorModifier as the correlated MassIndicatorGraphic's ColorModifier.

Free Label's surface so that we don't leak.

Handle OpenGL colours correctly in Label::doDraw().
  • Loading branch information...
1 parent e500ec2 commit c0906fd0130178f05b4f10551aa3214026d9d3c0 @alexander-b alexander-b committed May 15, 2012
View
@@ -50,6 +50,7 @@ bin_PROGRAMS = limbs-off
limbs_off_SOURCES = \
src/config_parser.hxx \
src/config_parser.cxx \
+ src/get_font.hxx \
src/step_timer.hxx \
src/step_timer.cxx \
src/template_math.hxx \
View
@@ -75,6 +75,11 @@ double Character::getVel() {
return vel_;
}
+phys_t Character::getMass() {
+ return body_.getMass();
+}
+
+
state2p Character::getState() {
return body_.getState();
}
@@ -274,3 +279,7 @@ CharacterGraphic::CharacterGraphic(Character* c) :
addGraphic(&footFront_);
addGraphic(&handFront_);
}
+
+ColorModifier* CharacterGraphic::getColourModifier() {
+ return &bodyColor_;
+}
View
@@ -40,6 +40,7 @@ public:
Character(state2p state, phys_t orientation);
void addToUniverse(GameUniverse* u);
bool isDead();
+ phys_t getMass();
double getVel();
state2p getState();
void crouch(bool state);
@@ -74,6 +75,7 @@ private:
class CharacterGraphic: public StackGraphic {
public:
CharacterGraphic(Character* c);
+ ColorModifier* getColourModifier();
private:
Character* c_;
GraphicFixture bodyFixture_, headFixture_, footBackFixture_,
View
@@ -24,6 +24,7 @@
#include "graphics/game_graphics_gl.hxx"
#include "physics/game_physics.hxx"
#include "player.hxx"
+#include "screen_element.hxx"
class Game : public EventHandler {
public:
@@ -64,8 +65,12 @@ private:
GameUniverse* universe_;
GLuint tex_;
GraphicFixture* planetFixture_;
+ std::vector<Label*> massIndicatorLabels_;
+ std::vector<MassIndicator*> massIndicators_;
+ std::vector<MassIndicatorGraphic*> massIndicatorGfx_;
Circle<phys_t>* planetCircle_;
std::vector<Player*> players_;
+ std::vector<PositionModifier*> massIndicatorPosMods_;
Screen* screen_;
StackGraphic* scene_, * foreground_;
Sprite* backgroundSprite_;
View
@@ -24,6 +24,7 @@
#include <SDL/SDL.h>
#include "game.hxx"
+#include "get_font.hxx"
#include "menu.hxx"
#include "config_parser.hxx"
@@ -97,6 +98,10 @@ Game::Game(Screen* screen) :
foreground_->addGraphic(*i);
foreground_->addModifier(camera_);
scene_->addGraphic(foreground_);
+ for (std::vector<MassIndicatorGraphic*>::const_iterator i =
+ massIndicatorGfx_.begin();
+ i != massIndicatorGfx_.end(); ++i)
+ scene_->addGraphic(*i);
}
Game::~Game() {
@@ -109,6 +114,19 @@ Game::~Game() {
for (std::vector<CharacterGraphic*>::const_iterator i =
characterGraphics_.begin(); i != characterGraphics_.end(); ++i)
delete (*i);
+ for (std::vector<Label*>::const_iterator i = massIndicatorLabels_.begin();
+ i != massIndicatorLabels_.end(); ++i)
+ delete (*i);
+ for (std::vector<MassIndicator*>::const_iterator i =
+ massIndicators_.begin(); i != massIndicators_.end(); ++i)
+ delete (*i);
+ for (std::vector<MassIndicatorGraphic*>::const_iterator i =
+ massIndicatorGfx_.begin(); i != massIndicatorGfx_.end(); ++i)
+ delete (*i);
+ for (std::vector<PositionModifier*>::iterator i =
+ massIndicatorPosMods_.begin(); i != massIndicatorPosMods_.end();
+ ++i)
+ delete (*i);
delete planetCircle_;
for (std::vector<AstroBody*>::const_iterator i = planets_.begin();
i != planets_.end(); ++i)
@@ -133,6 +151,20 @@ void Game::conceive() {
characters_.push_back(new Character(state2p()(pos, vel), i * angle));
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,
+ .02));
+ massIndicators_.push_back(new MassIndicator(i));
+ 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));
+ massIndicatorGfx_[i]->addModifier(massIndicatorPosMods_[i]);
+ massIndicatorGfx_[i]->addModifier(
+ characterGraphics_[i]->getColourModifier());
pos.rotate(a);
vel.rotate(a);
}
@@ -207,5 +239,12 @@ void Game::updateCamera(GLfloat dt) {
void Game::draw() {
scene_->draw();
+ int i = 0;
+ char mass [4];
+ for (std::vector<Character*>::const_iterator it = characters_.begin();
+ it != characters_.end(); ++it, ++i) {
+ snprintf(mass, sizeof(mass), "%.0f", (*it)->getMass());
+ massIndicatorLabels_[i]->setText(mass);
+ }
}
View
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2012 Alexander Berntsen <alexander@plaimi.net>
+ *
+ * This file is part of Limbs Off.
+ *
+ * Limbs Off is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Limbs Off is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Limbs Off. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GET_FONT_HXX_
+#define GET_FONT_HXX_
+
+#include <fontconfig/fontconfig.h>
+#include <string.h>
+
+void getFont(char* result, size_t size,
+ const char* fontFamily = "Anonymous Pro",
+ const char* fontStyle = "Bold", const char* fontSpacing = "Monospace");
+
+inline void getFont(char* result, size_t size, const char* fontFamily,
+ const char* fontStyle, const char* fontSpacing) {
+ char* font;
+ FcPattern* fontPattern = FcPatternCreate();
+ FcResult fontResult = FcResultMatch;
+ FcPatternAddString(fontPattern, FC_FAMILY,
+ (const FcChar8*) fontFamily);
+ FcPatternAddDouble(fontPattern, FC_SIZE, 74);
+ FcPatternAddString(fontPattern, FC_SPACING,
+ (const FcChar8*) fontSpacing);
+ FcPatternAddString(fontPattern, FC_STYLE,
+ (const FcChar8*) fontStyle);
+ FcDefaultSubstitute(fontPattern);
+ FcPattern* fontMatch = FcFontMatch(NULL, fontPattern, &fontResult);
+ FcPatternGetString(fontMatch, FC_FILE, 0, (FcChar8**) &font);
+ strncpy(result, font, size);
+ FcPatternDestroy(fontMatch);
+ FcPatternDestroy(fontPattern);
+}
+
+#endif /*GET_FONT_HXX_*/
@@ -64,11 +64,14 @@ private:
class PositionModifier: public GraphicModifier {
public:
- PositionModifier(int position, int num);
+ PositionModifier(int position, int num, bool horizontalP,
+ float offset = 0.0);
void begin();
void end();
private:
+ bool horizontalP_;
int position_, num_;
+ float offset_;
};
class SizeModifier: public GraphicModifier {
@@ -137,8 +140,6 @@ private:
int size_;
/** The font colour. */
SDL_Color colour_;
- /** The surface the text is rendered to. */
- SDL_Surface* surface_;
/** The actual TTF_Font. */
TTF_Font* font_;
};
@@ -163,6 +164,14 @@ private:
Disk disk_, square_;
};
+class MassIndicatorGraphic: public ScreenGraphic {
+public:
+ MassIndicatorGraphic(GLfloat width, GLfloat height, MassIndicator* logic,
+ Label* label = NULL);
+ Label* const label;
+ void doDraw();
+};
+
class ButtonGraphic: public ScreenGraphic {
public:
ButtonGraphic(GLfloat width, GLfloat height, Button* logic,
Oops, something went wrong.

0 comments on commit c0906fd

Please sign in to comment.