Font coloring; fix for resize hack

- added font coloring and changed default color to black
- fixed resize hack incorrectly changing video config,
  but font resizing will not work for now
dev-ui
Piotr Dziwinski 2012-09-30 10:56:35 +02:00
parent af4ff31b4e
commit 8ea4736a46
8 changed files with 82 additions and 67 deletions

View File

@ -873,12 +873,13 @@ Event CApplication::ProcessSystemEvent()
{ {
event.type = EVENT_QUIT; event.type = EVENT_QUIT;
} }
else if ( m_private->currentEvent.type == SDL_VIDEORESIZE ) { else if (m_private->currentEvent.type == SDL_VIDEORESIZE)
m_deviceConfig.size.x = m_private->currentEvent.resize.w; {
m_deviceConfig.size.y = m_private->currentEvent.resize.h; Gfx::GLDeviceConfig newConfig = m_deviceConfig;
CreateVideoSurface(); newConfig.size.x = m_private->currentEvent.resize.w;
// FIXME: dirty hack, will fail with device other than OpenGL newConfig.size.y = m_private->currentEvent.resize.h;
dynamic_cast<Gfx::CGLDevice*>(m_device)->ResizeViewport(m_deviceConfig.size.x, m_deviceConfig.size.y); if (newConfig.size != m_deviceConfig.size)
ChangeVideoConfig(newConfig);
} }
else if ( (m_private->currentEvent.type == SDL_KEYDOWN) || else if ( (m_private->currentEvent.type == SDL_KEYDOWN) ||
(m_private->currentEvent.type == SDL_KEYUP) ) (m_private->currentEvent.type == SDL_KEYUP) )

View File

@ -308,11 +308,13 @@ public:
virtual void SetTextureStageWrap(int index, TexWrapMode wrapS, TexWrapMode wrapT) = 0; virtual void SetTextureStageWrap(int index, TexWrapMode wrapS, TexWrapMode wrapT) = 0;
//! Renders primitive composed of vertices with single texture //! Renders primitive composed of vertices with single texture
virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount) = 0; virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
//! Renders primitive composed of vertices with color information and single texture Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;
virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) = 0;
//! Renders primitive composed of vertices with multitexturing (2 textures) //! Renders primitive composed of vertices with multitexturing (2 textures)
virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount) = 0; virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
Color color = Color(1.0f, 1.0f, 1.0f, 1.0f)) = 0;
//! Renders primitive composed of vertices with color information
virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount) = 0;
//! Tests whether a sphere intersects the 6 clipping planes of projection volume //! Tests whether a sphere intersects the 6 clipping planes of projection volume
virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) = 0; virtual int ComputeSphereVisibility(const Math::Vector &center, float radius) = 0;

View File

@ -333,6 +333,8 @@ void CEngine::Destroy()
void CEngine::ResetAfterDeviceChanged() void CEngine::ResetAfterDeviceChanged()
{ {
m_text->FlushCache();
// TODO reload textures, reset device state, etc. // TODO reload textures, reset device state, etc.
} }
@ -3891,7 +3893,7 @@ void CEngine::DrawStats()
std::string triangleText = str.str(); std::string triangleText = str.str();
float height = m_text->GetAscent(FONT_COLOBOT, 12.0f); float height = m_text->GetAscent(FONT_COLOBOT, 12.0f);
float width = 0.15f; float width = 0.2f;
Math::Point pos(0.04f, 0.04f + height); Math::Point pos(0.04f, 0.04f + height);
@ -3911,11 +3913,11 @@ void CEngine::DrawStats()
SetState(ENG_RSTATE_TEXT); SetState(ENG_RSTATE_TEXT);
m_text->DrawText(triangleText, FONT_COLOBOT, 12.0f, pos, 1.0f, TEXT_ALIGN_LEFT, 0); m_text->DrawText(triangleText, FONT_COLOBOT, 12.0f, pos, 1.0f, TEXT_ALIGN_LEFT, 0, Color(1.0f, 1.0f, 1.0f, 1.0f));
pos.y -= height; pos.y -= height;
m_text->DrawText(m_fpsText, FONT_COLOBOT, 12.0f, pos, 1.0f, TEXT_ALIGN_LEFT, 0); m_text->DrawText(m_fpsText, FONT_COLOBOT, 12.0f, pos, 1.0f, TEXT_ALIGN_LEFT, 0, Color(1.0f, 1.0f, 1.0f, 1.0f));
} }

View File

@ -151,7 +151,7 @@ void CText::FlushCache()
void CText::DrawText(const std::string &text, std::map<unsigned int, FontMetaChar> &format, void CText::DrawText(const std::string &text, std::map<unsigned int, FontMetaChar> &format,
float size, Math::Point pos, float width, TextAlign align, float size, Math::Point pos, float width, TextAlign align,
int eol) int eol, Color color)
{ {
float sw = 0.0f; float sw = 0.0f;
@ -168,12 +168,12 @@ void CText::DrawText(const std::string &text, std::map<unsigned int, FontMetaCha
pos.x -= sw; pos.x -= sw;
} }
DrawString(text, format, size, pos, width, eol); DrawString(text, format, size, pos, width, eol, color);
} }
void CText::DrawText(const std::string &text, FontType font, void CText::DrawText(const std::string &text, FontType font,
float size, Math::Point pos, float width, TextAlign align, float size, Math::Point pos, float width, TextAlign align,
int eol) int eol, Color color)
{ {
float sw = 0.0f; float sw = 0.0f;
@ -190,7 +190,7 @@ void CText::DrawText(const std::string &text, FontType font,
pos.x -= sw; pos.x -= sw;
} }
DrawString(text, font, size, pos, width, eol); DrawString(text, font, size, pos, width, eol, color);
} }
void CText::SizeText(const std::string &text, std::map<unsigned int, FontMetaChar> &format, void CText::SizeText(const std::string &text, std::map<unsigned int, FontMetaChar> &format,
@ -500,7 +500,7 @@ int CText::Detect(const std::string &text, FontType font, float size, float offs
} }
void CText::DrawString(const std::string &text, std::map<unsigned int, FontMetaChar> &format, void CText::DrawString(const std::string &text, std::map<unsigned int, FontMetaChar> &format,
float size, Math::Point pos, float width, int eol) float size, Math::Point pos, float width, int eol, Color color)
{ {
m_engine->SetState(ENG_RSTATE_TEXT); m_engine->SetState(ENG_RSTATE_TEXT);
@ -538,7 +538,7 @@ void CText::DrawString(const std::string &text, std::map<unsigned int, FontMetaC
DrawHighlight(hl, pos, charSize); DrawHighlight(hl, pos, charSize);
} }
DrawCharAndAdjustPos(ch, font, size, pos); DrawCharAndAdjustPos(ch, font, size, pos, color);
fmtIndex++; fmtIndex++;
} }
@ -569,7 +569,7 @@ void CText::StringToUTFCharList(const std::string &text, std::vector<UTF8Char> &
} }
void CText::DrawString(const std::string &text, FontType font, void CText::DrawString(const std::string &text, FontType font,
float size, Math::Point pos, float width, int eol) float size, Math::Point pos, float width, int eol, Color color)
{ {
assert(font != FONT_BUTTON); assert(font != FONT_BUTTON);
@ -579,7 +579,7 @@ void CText::DrawString(const std::string &text, FontType font,
StringToUTFCharList(text, chars); StringToUTFCharList(text, chars);
for (auto it = chars.begin(); it != chars.end(); ++it) for (auto it = chars.begin(); it != chars.end(); ++it)
{ {
DrawCharAndAdjustPos(*it, font, size, pos); DrawCharAndAdjustPos(*it, font, size, pos, color);
} }
} }
@ -663,7 +663,7 @@ void CText::DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size)
m_device->SetTextureEnabled(0, true); m_device->SetTextureEnabled(0, true);
} }
void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos) void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos, Color color)
{ {
// TODO: if (font == FONT_BUTTON) // TODO: if (font == FONT_BUTTON)
if (font == FONT_BUTTON) return; if (font == FONT_BUTTON) return;
@ -674,14 +674,14 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::P
if (cf == nullptr) if (cf == nullptr)
return; return;
int width = 1; int width = 1;
if (ch.c1 < 32) { // FIXME add support for chars with code 9 10 23 if (ch.c1 < 32) { // FIXME add support for chars with code 9 10 23
ch.c1 = ' '; ch.c1 = ' ';
ch.c2 = 0; ch.c2 = 0;
ch.c3 = 0; ch.c3 = 0;
if (ch.c1 == '\t') if (ch.c1 == '\t')
width = 4; width = 4;
} }
auto it = cf->cache.find(ch); auto it = cf->cache.find(ch);
@ -714,7 +714,7 @@ void CText::DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::P
}; };
m_device->SetTexture(0, tex.id); m_device->SetTexture(0, tex.id);
m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, quad, 4); m_device->DrawPrimitive(PRIMITIVE_TRIANGLE_STRIP, quad, 4, color);
m_engine->AddStatisticTriangle(2); m_engine->AddStatisticTriangle(2);
pos.x += tex.charSize.x * width; pos.x += tex.charSize.x * width;

View File

@ -23,6 +23,8 @@
#pragma once #pragma once
#include "graphics/core/color.h"
#include "math/point.h" #include "math/point.h"
#include <vector> #include <vector>
@ -39,9 +41,9 @@ class CEngine;
class CDevice; class CDevice;
//! Standard small font size //! Standard small font size
const float FONT_SIZE_SMALL = 10.0f; const float FONT_SIZE_SMALL = 12.0f;
//! Standard big font size //! Standard big font size
const float FONT_SIZE_BIG = 15.0f; const float FONT_SIZE_BIG = 18.0f;
/** /**
* \enum TextAlign * \enum TextAlign
@ -244,11 +246,11 @@ public:
//! Draws text (multi-format) //! Draws text (multi-format)
void DrawText(const std::string &text, std::map<unsigned int, FontMetaChar> &format, void DrawText(const std::string &text, std::map<unsigned int, FontMetaChar> &format,
float size, Math::Point pos, float width, TextAlign align, float size, Math::Point pos, float width, TextAlign align,
int eol); int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f));
//! Draws text (one font) //! Draws text (one font)
void DrawText(const std::string &text, FontType font, void DrawText(const std::string &text, FontType font,
float size, Math::Point pos, float width, TextAlign align, float size, Math::Point pos, float width, TextAlign align,
int eol); int eol, Color color = Color(0.0f, 0.0f, 0.0f, 1.0f));
//! Calculates dimensions for text (multi-format) //! Calculates dimensions for text (multi-format)
void SizeText(const std::string &text, std::map<unsigned int, FontMetaChar> &format, void SizeText(const std::string &text, std::map<unsigned int, FontMetaChar> &format,
@ -291,11 +293,11 @@ protected:
CharTexture CreateCharTexture(UTF8Char ch, CachedFont* font); CharTexture CreateCharTexture(UTF8Char ch, CachedFont* font);
void DrawString(const std::string &text, std::map<unsigned int, FontMetaChar> &format, void DrawString(const std::string &text, std::map<unsigned int, FontMetaChar> &format,
float size, Math::Point pos, float width, int eol); float size, Math::Point pos, float width, int eol, Color color);
void DrawString(const std::string &text, FontType font, void DrawString(const std::string &text, FontType font,
float size, Math::Point pos, float width, int eol); float size, Math::Point pos, float width, int eol, Color color);
void DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size); void DrawHighlight(FontHighlight hl, Math::Point pos, Math::Point size);
void DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos); void DrawCharAndAdjustPos(UTF8Char ch, FontType font, float size, Math::Point &pos, Color color);
void StringToUTFCharList(const std::string &text, std::vector<UTF8Char> &chars); void StringToUTFCharList(const std::string &text, std::vector<UTF8Char> &chars);
protected: protected:

View File

@ -170,13 +170,6 @@ void CGLDevice::ConfigChanged(const GLDeviceConfig& newConfig)
Create(); Create();
} }
void CGLDevice::ResizeViewport(const unsigned int width, const unsigned int height)
{
m_config.size.x = width;
m_config.size.y = height;
glViewport(0, 0, m_config.size.x, m_config.size.y);
}
void CGLDevice::BeginScene() void CGLDevice::BeginScene()
{ {
Clear(); Clear();
@ -860,7 +853,8 @@ GLenum TranslateGfxPrimitive(PrimitiveType type)
return flag; return flag;
} }
void CGLDevice::DrawPrimitive(PrimitiveType type, const Vertex *vertices, int vertexCount) void CGLDevice::DrawPrimitive(PrimitiveType type, const Vertex *vertices, int vertexCount,
Color color)
{ {
Vertex* vs = const_cast<Vertex*>(vertices); Vertex* vs = const_cast<Vertex*>(vertices);
@ -874,7 +868,7 @@ void CGLDevice::DrawPrimitive(PrimitiveType type, const Vertex *vertices, int ve
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), reinterpret_cast<GLfloat*>(&vs[0].texCoord)); glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), reinterpret_cast<GLfloat*>(&vs[0].texCoord));
glColor3f(1.0f, 1.0f, 1.0f); glColor4fv(color.Array());
glDrawArrays(TranslateGfxPrimitive(type), 0, vertexCount); glDrawArrays(TranslateGfxPrimitive(type), 0, vertexCount);
@ -883,23 +877,8 @@ void CGLDevice::DrawPrimitive(PrimitiveType type, const Vertex *vertices, int ve
glDisableClientState(GL_TEXTURE_COORD_ARRAY); // GL_TEXTURE0 glDisableClientState(GL_TEXTURE_COORD_ARRAY); // GL_TEXTURE0
} }
void CGLDevice::DrawPrimitive(PrimitiveType type, const VertexCol *vertices, int vertexCount) void CGLDevice::DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
{ Color color)
VertexCol* vs = const_cast<VertexCol*>(vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(VertexCol), reinterpret_cast<GLfloat*>(&vs[0].coord));
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, sizeof(VertexCol), reinterpret_cast<GLfloat*>(&vs[0].color));
glDrawArrays(TranslateGfxPrimitive(type), 0, vertexCount);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
void CGLDevice::DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount)
{ {
VertexTex2* vs = const_cast<VertexTex2*>(vertices); VertexTex2* vs = const_cast<VertexTex2*>(vertices);
@ -917,7 +896,7 @@ void CGLDevice::DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, in
glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(VertexTex2), reinterpret_cast<GLfloat*>(&vs[0].texCoord2)); glTexCoordPointer(2, GL_FLOAT, sizeof(VertexTex2), reinterpret_cast<GLfloat*>(&vs[0].texCoord2));
glColor3f(1.0f, 1.0f, 1.0f); glColor4fv(color.Array());
glDrawArrays(TranslateGfxPrimitive(type), 0, vertexCount); glDrawArrays(TranslateGfxPrimitive(type), 0, vertexCount);
@ -928,6 +907,22 @@ void CGLDevice::DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, in
glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY);
} }
void CGLDevice::DrawPrimitive(PrimitiveType type, const VertexCol *vertices, int vertexCount)
{
VertexCol* vs = const_cast<VertexCol*>(vertices);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(VertexCol), reinterpret_cast<GLfloat*>(&vs[0].coord));
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, sizeof(VertexCol), reinterpret_cast<GLfloat*>(&vs[0].color));
glDrawArrays(TranslateGfxPrimitive(type), 0, vertexCount);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}
bool InPlane(Math::Vector normal, float originPlane, Math::Vector center, float radius) bool InPlane(Math::Vector normal, float originPlane, Math::Vector center, float radius)
{ {
float distance = (originPlane + Math::DotProduct(normal, center)) / normal.Length(); float distance = (originPlane + Math::DotProduct(normal, center)) / normal.Length();

View File

@ -86,7 +86,6 @@ public:
virtual void BeginScene(); virtual void BeginScene();
virtual void EndScene(); virtual void EndScene();
virtual void ResizeViewport(const unsigned int width, const unsigned int height);
virtual void Clear(); virtual void Clear();
@ -120,9 +119,13 @@ public:
virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT); virtual void SetTextureStageWrap(int index, Gfx::TexWrapMode wrapS, Gfx::TexWrapMode wrapT);
virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices, int vertexCount); //! Renders primitive composed of vertices with single texture
virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices, int vertexCount); virtual void DrawPrimitive(PrimitiveType type, const Vertex *vertices , int vertexCount,
virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount); Color color = Color(1.0f, 1.0f, 1.0f, 1.0f));
//! Renders primitive composed of vertices with multitexturing (2 textures)
virtual void DrawPrimitive(PrimitiveType type, const VertexTex2 *vertices, int vertexCount,
Color color = Color(1.0f, 1.0f, 1.0f, 1.0f));
virtual void DrawPrimitive(PrimitiveType type, const VertexCol *vertices , int vertexCount);
virtual int ComputeSphereVisibility(const Math::Vector &center, float radius); virtual int ComputeSphereVisibility(const Math::Vector &center, float radius);

View File

@ -39,6 +39,16 @@ struct IntPoint
int y; int y;
IntPoint(int aX = 0, int aY = 0) : x(aX), y(aY) {} IntPoint(int aX = 0, int aY = 0) : x(aX), y(aY) {}
inline bool operator==(const IntPoint& p) const
{
return x == p.x && y == p.y;
}
inline bool operator!=(const IntPoint& p) const
{
return !operator==(p);
}
}; };