Skip to content

Commit

Permalink
renderer: fixed minor issues and added documentation for text renderer
Browse files Browse the repository at this point in the history
  • Loading branch information
pjonnala-eab committed Aug 22, 2015
1 parent ed6726e commit 2286e28
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 25 deletions.
6 changes: 0 additions & 6 deletions libopenage/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,28 +280,22 @@ void Engine::end_game() {
bool Engine::draw_debug_overlay() {
util::col {255, 255, 255, 255}.use();

this->text_renderer->set_color(renderer::Color{255, 0, 0, 255});
// Draw FPS counter in the lower right corner
this->render_text(
{this->engine_coord_data->window_size.x - 100, 15}, 20,
"%.1f fps", this->fps_counter.fps
);

this->text_renderer->set_color(renderer::Color{0, 255, 0, 255});
// Draw version string in the lower left corner
this->render_text(
{5, 35}, 20,
"openage %s", config::version
);

this->text_renderer->set_color(renderer::Color{0, 0, 255, 255});
this->render_text(
{5, 15}, 12,
"%s", config::config_option_string
);

this->text_renderer->set_color(renderer::Color{255, 255, 255, 255});

this->profiler.show(true);

return true;
Expand Down
8 changes: 4 additions & 4 deletions libopenage/renderer/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ Color &Color::operator=(const Color &other) {
return *this;
}

bool operator==(const Color &left, const Color &right) {
return left.r == right.r && left.g == right.g && left.b == right.b && left.a == right.a;
bool Color::operator==(const Color &other) const {
return this->r == other.r && this->g == other.g && this->b == other.b && this->a == other.a;
}

bool operator!=(const Color &left, const Color &right) {
return !(left == right);
bool Color::operator!=(const Color &other) const {
return this->r != other.r || this->g != other.g || this->b != other.b || this->a != other.a;
}

}} // openage::renderer
7 changes: 4 additions & 3 deletions libopenage/renderer/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ class Color {

Color &operator=(const Color &other);

bool operator==(const Color &other) const;

bool operator!=(const Color &other) const;

uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;

};

bool operator==(const Color &left, const Color &right);
bool operator!=(const Color &left, const Color &right);

}} // openage::renderer

#endif
13 changes: 3 additions & 10 deletions libopenage/renderer/text_renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,17 @@ void TextRenderer::draw(coord::window position, const std::string &text) {

void TextRenderer::draw(int x, int y, const std::string &text) {
if (this->is_dirty) {
TextRenderBatch batch;
batch.font = this->current_font;
batch.color = this->current_color;
render_batches.push_back(batch);
this->render_batches.emplace_back(this->current_font, this->current_color);
this->is_dirty = false;
}

TextRenderBatchPass batch_pass;
batch_pass.x = x;
batch_pass.y = y;
batch_pass.text = text;
render_batches.back().passes.push_back(batch_pass);
this->render_batches.back().passes.emplace_back(x, y, text);
}

void TextRenderer::render() {
// Sort the batches by font
std::sort(std::begin(this->render_batches), std::end(this->render_batches),
[](const TextRenderBatch &a, const TextRenderBatch &b) -> bool {
[](const text_render_batch &a, const text_render_batch &b) -> bool {
return a.font < b.font;
});

Expand Down
58 changes: 56 additions & 2 deletions libopenage/renderer/text_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,80 @@ class TextRenderer {

virtual ~TextRenderer();

/**
* Set the font to be used for the future text draw calls.
*
* @param font: the font to be used.
*/
void set_font(Font *font);

/**
* Set the color to be used for the future text draw calls.
*
* @param color: the color to be used.
*/
void set_color(const Color &color);

/**
* Draw a formatted string at the specified position.
*
* @param position: where the text should be displayed.
* @param format: the text format
*/
void draw(coord::window position, const char *format, ...);

/**
* Draw text at the specified position.
*
* @param position: where the text should be displayed.
* @param text: the text to be displayed.
*/
void draw(coord::window position, const std::string &text);

/**
* Draw text at the specified position.
*
* @param x: the position in x-direction.
* @param y: the position in y-direction.
* @param text: the text to be displayed.
*/
void draw(int x, int y, const std::string &text);

/**
* Render all the text draw requests made during the frame.
*/
void render();

private:
struct TextRenderBatchPass {
/**
* A single text draw request containing the text and position.
*/
struct text_render_batch_pass {
int x;
int y;
std::string text;

text_render_batch_pass(int x, int y, const std::string &text)
:
x{x},
y{y},
text{text} {
}
};

struct TextRenderBatch {
/**
* The set of text draw requests with the same font and color.
*/
struct text_render_batch {
openage::Font *font;
Color color;
std::vector<TextRenderBatchPass> passes;

text_render_batch(openage::Font *font, const Color &color)
:
font{font},
color{color} {
}
};

Font *current_font;
Expand Down

0 comments on commit 2286e28

Please sign in to comment.