-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Text renderer with support for colors #357
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| add_sources(libopenage | ||
| color.cpp | ||
| text_renderer.cpp | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright 2015-2015 the openage authors. See copying.md for legal info. | ||
|
|
||
| #include "color.h" | ||
|
|
||
| namespace openage { | ||
| namespace renderer { | ||
|
|
||
| Color::Color() | ||
| : | ||
| r{0}, | ||
| g{0}, | ||
| b{0}, | ||
| a{255} { | ||
| // Empty | ||
| } | ||
|
|
||
| Color::Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a) | ||
| : | ||
| r{r}, | ||
| g{g}, | ||
| b{b}, | ||
| a{a} { | ||
| // Empty | ||
| } | ||
|
|
||
| Color::Color(const Color &other) | ||
| : | ||
| r{other.r}, | ||
| g{other.g}, | ||
| b{other.b}, | ||
| a{other.a} { | ||
| // Empty | ||
| } | ||
|
|
||
| Color &Color::operator=(const Color &other) { | ||
| if (this != &other) { | ||
| this->r = other.r; | ||
| this->g = other.g; | ||
| this->b = other.b; | ||
| this->a = other.a; | ||
| } | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
| 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 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright 2015-2015 the openage authors. See copying.md for legal info. | ||
|
|
||
| #ifndef OPENAGE_RENDERER_COLOR_H_ | ||
| #define OPENAGE_RENDERER_COLOR_H_ | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| namespace openage { | ||
| namespace renderer { | ||
|
|
||
| class Color { | ||
| public: | ||
| Color(); | ||
|
|
||
| Color(uint8_t r, uint8_t g, uint8_t b, uint8_t a); | ||
|
|
||
| Color(const Color &other); | ||
|
|
||
| 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; | ||
|
|
||
| }; | ||
|
|
||
| }} // openage::renderer | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // Copyright 2015-2015 the openage authors. See copying.md for legal info. | ||
|
|
||
| #include "text_renderer.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <epoxy/gl.h> | ||
|
|
||
| #include "../log/log.h" | ||
| #include "../util/strings.h" | ||
| #include "../font.h" | ||
|
|
||
| namespace openage { | ||
| namespace renderer { | ||
|
|
||
| TextRenderer::TextRenderer() | ||
| : | ||
| current_font{nullptr}, | ||
| current_color{255, 255, 255, 255}, | ||
| is_dirty{true} { | ||
| // Empty | ||
| } | ||
|
|
||
| TextRenderer::~TextRenderer() { | ||
| // Empty | ||
| } | ||
|
|
||
| void TextRenderer::set_font(openage::Font *font) { | ||
| if (this->current_font == font) { | ||
| return; | ||
| } | ||
|
|
||
| this->current_font = font; | ||
| this->is_dirty = true; | ||
| } | ||
|
|
||
| void TextRenderer::set_color(const Color &color) { | ||
| if (this->current_color == color) { | ||
| return; | ||
| } | ||
|
|
||
| this->current_color = color; | ||
| this->is_dirty = true; | ||
| } | ||
|
|
||
| void TextRenderer::draw(coord::window position, const char *format, ...) { | ||
| std::string text; | ||
| va_list vl; | ||
| va_start(vl, format); | ||
| util::vsformat(format, vl, text); | ||
| va_end(vl); | ||
|
|
||
| this->draw(position.x, position.y, text); | ||
| } | ||
|
|
||
| void TextRenderer::draw(coord::window position, const std::string &text) { | ||
| this->draw(position.x, position.y, text); | ||
| } | ||
|
|
||
| void TextRenderer::draw(int x, int y, const std::string &text) { | ||
| if (this->is_dirty) { | ||
| this->render_batches.emplace_back(this->current_font, this->current_color); | ||
| this->is_dirty = false; | ||
| } | ||
|
|
||
| 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 text_render_batch &a, const text_render_batch &b) -> bool { | ||
| return a.font < b.font; | ||
| }); | ||
|
|
||
| // Merge consecutive batches if font and color values are same | ||
| for (auto current_batch = std::begin(this->render_batches); current_batch != std::end(this->render_batches); ) { | ||
| auto next_batch = current_batch; | ||
| next_batch++; | ||
| if (next_batch != std::end(this->render_batches) && | ||
| current_batch->font == next_batch->font && | ||
| current_batch->color == next_batch->color) { | ||
| // Merge the render passes of current and next batches and remove the next batch | ||
| std::move(std::begin(next_batch->passes), | ||
| std::end(next_batch->passes), | ||
| std::back_inserter(current_batch->passes)); | ||
| this->render_batches.erase(next_batch); | ||
| } else { | ||
| current_batch++; | ||
| } | ||
| } | ||
|
|
||
| // Render all the batches | ||
| for (auto &batch : this->render_batches) { | ||
| glColor4f(batch.color.r / 255.f, | ||
| batch.color.g / 255.f, | ||
| batch.color.b / 255.f, | ||
| batch.color.a / 255.f); | ||
| for (auto &pass : batch.passes) { | ||
| batch.font->render_static(pass.x, pass.y, pass.text.c_str()); | ||
| } | ||
| } | ||
|
|
||
| // Clear the render batches for next frame | ||
| this->render_batches.clear(); | ||
| } | ||
|
|
||
| }} // openage::renderer | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // Copyright 2015-2015 the openage authors. See copying.md for legal info. | ||
|
|
||
| #ifndef OPENAGE_RENDERER_TEXT_RENDERER_H_ | ||
| #define OPENAGE_RENDERER_TEXT_RENDERER_H_ | ||
|
|
||
| #include <vector> | ||
| #include <string> | ||
|
|
||
| #include "../coord/window.h" | ||
| #include "color.h" | ||
|
|
||
| namespace openage { | ||
|
|
||
| class Font; | ||
|
|
||
| namespace renderer { | ||
|
|
||
| class TextRenderer { | ||
|
|
||
| public: | ||
| 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: | ||
| /** | ||
| * 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} { | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * The set of text draw requests with the same font and color. | ||
| */ | ||
| struct text_render_batch { | ||
| openage::Font *font; | ||
| Color color; | ||
| 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<text_render_batch> render_batches; | ||
|
|
||
| }; | ||
|
|
||
| }} // openage::renderer | ||
|
|
||
| #endif |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no opengl call in that file, so no need for the epoxy header.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
glColor4f is being used in the render method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you're right, sorry.