Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text renderer with support for colors #357

Merged
merged 2 commits into from
Aug 23, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
renderer: fixed minor issues and added documentation for text renderer
  • Loading branch information
pjonnala-eab committed Aug 22, 2015
commit c1511db17e62095732e9dbd73c53c3e40cd4fe49
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
62 changes: 58 additions & 4 deletions libopenage/renderer/text_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,86 @@ 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;
std::vector<text_render_batch_pass> passes;

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

Font *current_font;
Color current_color;
bool is_dirty;
std::vector<TextRenderBatch> render_batches;
std::vector<text_render_batch> render_batches;

};

Expand Down