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

Fix #83 issues #86

Merged
merged 6 commits into from
Jul 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions source/platform/include/Gwork/BaseRender.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ namespace Gwk
protected:

virtual bool EnsureFont(const Gwk::Font& font) { return false; }
virtual bool EnsureTexture(const Gwk::Texture& texture) { return false; }

float m_fScale;

Expand Down
6 changes: 6 additions & 0 deletions source/platform/include/Gwork/PlatformTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,11 @@ namespace Gwk
return facename == rhs.facename && size == rhs.size && bold == rhs.bold;
}

inline bool operator!=(const Font& rhs) const
{
return bold != rhs.bold || size != rhs.size || facename != rhs.facename;
}

String facename;
float size;
bool bold;
Expand Down Expand Up @@ -279,6 +284,7 @@ namespace Gwk
Texture& operator=(const Texture&) = default;

inline bool operator==(const Texture& rhs) const { return name == rhs.name; }
inline bool operator!=(const Texture& rhs) const { return name != rhs.name; }

String name;
bool readable;
Expand Down
11 changes: 7 additions & 4 deletions source/platform/include/Gwork/Renderers/Allegro5.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ namespace Gwk
float v1 = 0.0f, float u2 = 1.0f, float v2 = 1.0f) override;

Gwk::Color PixelColor(const Gwk::Texture& texture,
unsigned int x, unsigned int y,
const Gwk::Color& col_default) override;
unsigned int x, unsigned int y,
const Gwk::Color& col_default) override;

void RenderText(const Gwk::Font& font,
Gwk::Point pos,
const Gwk::String& text) override;
Gwk::Point pos,
const Gwk::String& text) override;

Gwk::Point MeasureText(const Gwk::Font& font, const Gwk::String& text) override;

Expand All @@ -65,6 +65,7 @@ namespace Gwk
Texture::Status LoadTexture(const Gwk::Texture& texture) override;
void FreeTexture(const Gwk::Texture& texture) override;
TextureData GetTextureData(const Gwk::Texture& texture) const override;
bool EnsureTexture(const Gwk::Texture& texture) override;
protected:// Resourses

struct ALTextureData : public Gwk::TextureData
Expand Down Expand Up @@ -111,6 +112,8 @@ namespace Gwk

std::unordered_map<Font, ALFontData> m_fonts;
std::unordered_map<Texture, ALTextureData> m_textures;
std::pair<const Font, ALFontData>* m_lastFont;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why store the last font here? Why not store an opaque handle in Font, like it was? If the font keeps changing you have to constantly look it up. The only cache you need is to look up the font by family/size, etc, i.e. style.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for avoiding the user data in Font/Texture primarily is to remove data specific to a renderer having the potential to be shared between renderers. One of my use cases for Gwork is an environment where multiple renderers exist. Some of the Gwork objects are shared between these renderers. The user data in Font/Texture does not have a way to determine the renderer that assigned the data or store multiple without recreating every time the renderers change.

I also question the lifespan of user data in Font/Texture.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiple renderers?! I'm curious, how so? Multiple instances with the same backend, or multiple backends?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It has always been assumed that an application would embed one renderer. I have thought about having fallback renderers if some features aren't available on other renderers, e.g. if more primitives were added to add a canvas (e.g. drawing of charts etc) it call fallback to software renderer if a particular renderer did not have that features. It's tempting to include something like NanoVG to do something like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One example of multiple renderers is in-game overlays. Without knowing which rendering API the game will you, multiple backends are used. However, some game when switching between lobby & battle(for lack of a better word), different rendering APIs are used. Usually different versions of Direct X. And in rare cases, the HUD is rendered in DX 10 and the 3D environment in DX 11.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, this has never been a requirement before. Interesting. I was wondering why the extensive changes and whether is was overkill! I can see your motivation now!

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added #87 so this feature can be mentioned as it is significant. Will have to mention/update docs.

std::pair<const Texture, ALTextureData>* m_lastTexture;
public:
void DrawLinedRect(Gwk::Rect rect) override;
void DrawShavedCornerRect(Gwk::Rect rect, bool bSlight = false) override;
Expand Down
3 changes: 3 additions & 0 deletions source/platform/include/Gwork/Renderers/DirectX11.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ namespace Gwk
Texture::Status LoadTexture(const Gwk::Texture& texture) override;
void FreeTexture(const Gwk::Texture& texture) override;
TextureData GetTextureData(const Gwk::Texture& texture) const override;
bool EnsureTexture(const Gwk::Texture& texture) override;

protected:

Expand Down Expand Up @@ -179,6 +180,8 @@ namespace Gwk

std::unordered_map<Font, DxFontData> m_fonts;
std::unordered_map<Texture, DxTextureData> m_textures;
std::pair<const Font, DxFontData>* m_lastFont;
std::pair<const Texture, DxTextureData>* m_lastTexture;

void Flush();
void Present();
Expand Down
12 changes: 7 additions & 5 deletions source/platform/include/Gwork/Renderers/Irrlicht.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ namespace Gwk
void DrawPixel(int x, int y) override;

void DrawTexturedRect(const Gwk::Texture& texture, Gwk::Rect targetRect, float u1 = 0.0f,
float v1 = 0.0f, float u2 = 1.0f, float v2 = 1.0f) override;
float v1 = 0.0f, float u2 = 1.0f, float v2 = 1.0f) override;

Gwk::Color PixelColor(const Gwk::Texture& texture,
unsigned int x, unsigned int y,
const Gwk::Color& col_default) override;
unsigned int x, unsigned int y,
const Gwk::Color& col_default) override;

void RenderText(const Gwk::Font& font,
Gwk::Point pos,
const Gwk::String& text) override;
Gwk::Point pos,
const Gwk::String& text) override;

Gwk::Point MeasureText(const Gwk::Font& font, const Gwk::String& text) override;

Expand All @@ -74,6 +74,7 @@ namespace Gwk
Texture::Status LoadTexture(const Gwk::Texture& texture) override;
void FreeTexture(const Gwk::Texture& texture) override;
TextureData GetTextureData(const Gwk::Texture& texture) const override;
bool EnsureTexture(const Gwk::Texture& texture) override;

protected:// Resourses

Expand Down Expand Up @@ -122,6 +123,7 @@ namespace Gwk

std::unordered_map<Font, IRFontData> m_fonts;*/
std::unordered_map<Texture, IRTextureData> m_textures;
std::pair<const Texture, IRTextureData>* m_lastTexture;

private:

Expand Down
3 changes: 3 additions & 0 deletions source/platform/include/Gwork/Renderers/OpenGL.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace Gwk
Texture::Status LoadTexture(const Gwk::Texture& texture) override;
void FreeTexture(const Gwk::Texture& texture) override;
TextureData GetTextureData(const Gwk::Texture& texture) const override;
bool EnsureTexture(const Gwk::Texture& texture) override;
protected:// Resourses

struct GLTextureData : public Gwk::TextureData
Expand Down Expand Up @@ -130,6 +131,8 @@ namespace Gwk

std::unordered_map<Font, GLFontData> m_fonts;
std::unordered_map<Texture, GLTextureData> m_textures;
std::pair<const Font, GLFontData>* m_lastFont;
std::pair<const Texture, GLTextureData>* m_lastTexture;
protected:

Rect m_viewRect;
Expand Down
17 changes: 10 additions & 7 deletions source/platform/include/Gwork/Renderers/OpenGLCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ namespace Gwk
void EndClip() override;

void DrawTexturedRect(const Gwk::Texture& texture, Gwk::Rect targetRect, float u1 = 0.0f,
float v1 = 0.0f, float u2 = 1.0f, float v2 = 1.0f) override;
float v1 = 0.0f, float u2 = 1.0f, float v2 = 1.0f) override;

Gwk::Color PixelColor(const Gwk::Texture& texture,
unsigned int x, unsigned int y,
const Gwk::Color& col_default) override;
unsigned int x, unsigned int y,
const Gwk::Color& col_default) override;

void RenderText(const Gwk::Font& font,
Gwk::Point pos,
const Gwk::String& text) override;
Gwk::Point pos,
const Gwk::String& text) override;

Gwk::Point MeasureText(const Gwk::Font& font, const Gwk::String& text) override;

Expand All @@ -62,6 +62,7 @@ namespace Gwk
Texture::Status LoadTexture(const Gwk::Texture& texture) override;
void FreeTexture(const Gwk::Texture& texture) override;
TextureData GetTextureData(const Gwk::Texture& texture) const override;
bool EnsureTexture(const Gwk::Texture& texture) override;

protected:// Resourses

Expand Down Expand Up @@ -119,13 +120,15 @@ namespace Gwk

float m_Spacing;

float width;
float height;
int width;
int height;
unsigned int texture_id;
};

std::unordered_map<Font, GLFontData> m_fonts;
std::unordered_map<Texture, GLTextureData> m_textures;
std::pair<const Font, GLFontData>* m_lastFont;
std::pair<const Texture, GLTextureData>* m_lastTexture;
public:

bool InitializeContext(Gwk::WindowProvider* window) override;
Expand Down
13 changes: 8 additions & 5 deletions source/platform/include/Gwork/Renderers/SDL2.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ namespace Gwk
void EndClip() override;

void DrawTexturedRect(const Gwk::Texture& texture, Gwk::Rect targetRect, float u1 = 0.0f,
float v1 = 0.0f, float u2 = 1.0f, float v2 = 1.0f) override;
float v1 = 0.0f, float u2 = 1.0f, float v2 = 1.0f) override;

Gwk::Color PixelColor(const Gwk::Texture& texture,
unsigned int x, unsigned int y,
const Gwk::Color& col_default) override;
unsigned int x, unsigned int y,
const Gwk::Color& col_default) override;

void RenderText(const Gwk::Font& font,
Gwk::Point pos,
const Gwk::String& text) override;
Gwk::Point pos,
const Gwk::String& text) override;

Gwk::Point MeasureText(const Gwk::Font& font, const Gwk::String& text) override;

Expand All @@ -73,6 +73,7 @@ namespace Gwk
Texture::Status LoadTexture(const Gwk::Texture& texture) override;
void FreeTexture(const Gwk::Texture& texture) override;
TextureData GetTextureData(const Gwk::Texture& texture) const override;
bool EnsureTexture(const Gwk::Texture& texture) override;

protected:// Resourses

Expand Down Expand Up @@ -122,6 +123,8 @@ namespace Gwk

std::unordered_map<Font, SDL2FontData> m_fonts;
std::unordered_map<Texture, SDL2TextureData> m_textures;
std::pair<const Font, SDL2FontData>* m_lastFont;
std::pair<const Texture, SDL2TextureData>* m_lastTexture;
public:

bool BeginContext(Gwk::WindowProvider* window) override;
Expand Down
106 changes: 82 additions & 24 deletions source/platform/include/Gwork/Renderers/SFML2.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,37 @@
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Color.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Font.hpp>
#include <SFML/Graphics/VertexArray.hpp>

#include <unordered_map>
#include <memory>
#include <vector>
#include <functional>

namespace Gwk
{
namespace Renderer
{
//! Default resource loader for SFML2.
class SFML2ResourceLoader : public ResourceLoader
{
ResourcePaths& m_paths;
public:
SFML2ResourceLoader(ResourcePaths& paths)
: m_paths(paths)
{}

Font::Status LoadFont(Font& font) override;
void FreeFont(Font& font) override;

Texture::Status LoadTexture(Texture& texture) override;
void FreeTexture(Texture& texture) override;
};

//
//! Renderer for [SFML2](https://www.sfml-dev.org/).
//
class GWK_EXPORT SFML2 : public Gwk::Renderer::Base
{
template<typename T>
using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;

public:

//! Constructor for SFML2 renderer.
//! \param loader : ResourceLoader for renderer.
//! \param target : application render target.
SFML2(ResourceLoader& loader, sf::RenderTarget& target);
SFML2(ResourcePaths& paths, sf::RenderTarget& target);

virtual ~SFML2();

inline void EnsurePrimitiveType(sf::PrimitiveType type)
inline void SetPrimitiveType(sf::PrimitiveType type)
{
if (m_buffer.getPrimitiveType() != type)
{
Expand All @@ -59,7 +53,7 @@ namespace Gwk
}
}

inline void EnsureTexture(const sf::Texture *texture)
inline void SetTexture(const sf::Texture *texture)
{
if (m_renderStates.texture != texture)
{
Expand Down Expand Up @@ -99,17 +93,81 @@ namespace Gwk
void DrawLinedRect(Gwk::Rect rect) override;
void DrawFilledRect(Gwk::Rect rect) override;
void DrawShavedCornerRect(Gwk::Rect rect, bool bSlight = false) override;
void DrawTexturedRect(Gwk::Texture* texture,
Gwk::Rect rect,
float u1, float v1, float u2, float v2) override;

void RenderText(Gwk::Font* font, Gwk::Point pos, const Gwk::String& text) override;
Gwk::Point MeasureText(Gwk::Font* font, const Gwk::String& text) override;
void DrawTexturedRect(const Gwk::Texture& texture, Gwk::Rect targetRect, float u1 = 0.0f,
float v1 = 0.0f, float u2 = 1.0f, float v2 = 1.0f) override;

Gwk::Color PixelColor(Gwk::Texture* texture,
Gwk::Color PixelColor(const Gwk::Texture& texture,
unsigned int x, unsigned int y,
const Gwk::Color& col_default) override;

void RenderText(const Gwk::Font& font,
Gwk::Point pos,
const Gwk::String& text) override;

Gwk::Point MeasureText(const Gwk::Font& font, const Gwk::String& text) override;

// Resource Loader
Gwk::Font::Status LoadFont(const Gwk::Font& font) override;
void FreeFont(const Gwk::Font& font) override;
bool EnsureFont(const Gwk::Font& font) override;

Texture::Status LoadTexture(const Gwk::Texture& texture) override;
void FreeTexture(const Gwk::Texture& texture) override;
TextureData GetTextureData(const Gwk::Texture& texture) const override;
bool EnsureTexture(const Gwk::Texture& texture) override;
protected:// Resourses

struct SFMLTextureData : public Gwk::TextureData
{
SFMLTextureData()
{
}
SFMLTextureData(const SFMLTextureData&) = delete;
SFMLTextureData(SFMLTextureData&& other)
: SFMLTextureData()
{
std::swap(width, other.width);
std::swap(height, other.height);
std::swap(readable, other.readable);

texture.swap(other.texture);
image.swap(other.image);
}

~SFMLTextureData()
{
}

std::unique_ptr<sf::Texture> texture;
std::unique_ptr<sf::Image> image;
};

struct SFMLFontData
{
SFMLFontData()
{
}

SFMLFontData(const SFMLFontData&) = delete;
SFMLFontData(SFMLFontData&& other)
: SFMLFontData()
{
font.swap(other.font);
}

~SFMLFontData()
{
}

std::unique_ptr<sf::Font> font;
};

std::unordered_map<Font, SFMLFontData> m_fonts;
std::unordered_map<Texture, SFMLTextureData> m_textures;
std::pair<const Font, SFMLFontData>* m_lastFont;
std::pair<const Texture, SFMLTextureData>* m_lastTexture;

protected:

sf::RenderTarget& m_target;
Expand Down
Loading