@@ -40,7 +40,7 @@ private: | |||
char *name; | |||
SDL_Surface *img; | |||
int width, height; | |||
int2 size; | |||
float tx, ty; | |||
GLuint texture; | |||
}; | |||
@@ -68,10 +68,9 @@ Font::Font(char const *path) | |||
exit(1); | |||
} | |||
data->width = data->img->w / 16; | |||
data->height = data->img->h / 16; | |||
data->tx = (float)data->width / PotUp(data->img->w); | |||
data->ty = (float)data->height / PotUp(data->img->h); | |||
data->size = int2(data->img->w, data->img->h) / 16; | |||
data->tx = (float)data->size.x / PotUp(data->img->w); | |||
data->ty = (float)data->size.y / PotUp(data->img->h); | |||
drawgroup = DRAWGROUP_BEFORE; | |||
} | |||
@@ -150,14 +149,14 @@ void Font::Print(int3 pos, char const *str) | |||
glTexCoord2f(tx, ty + data->ty); | |||
glVertex2f(pos.x, pos.y); | |||
glTexCoord2f(tx + data->tx, ty + data->ty); | |||
glVertex2f(pos.x + data->width, pos.y); | |||
glVertex2f(pos.x + data->size.x, pos.y); | |||
glTexCoord2f(tx + data->tx, ty); | |||
glVertex2f(pos.x + data->width, pos.y + data->height); | |||
glVertex2f(pos.x + data->size.x, pos.y + data->size.y); | |||
glTexCoord2f(tx, ty); | |||
glVertex2f(pos.x, pos.y + data->height); | |||
glVertex2f(pos.x, pos.y + data->size.y); | |||
} | |||
pos.x += data->width; | |||
pos.x += data->size.x; | |||
} | |||
glEnd(); | |||
} | |||
@@ -188,3 +187,8 @@ void Font::PrintBold(int3 pos, char const *str) | |||
glPopAttrib(); | |||
} | |||
int2 Font::GetSize() const | |||
{ | |||
return data->size; | |||
} | |||
@@ -35,6 +35,7 @@ public: | |||
/* New methods */ | |||
void Print(int3 pos, char const *str); | |||
void PrintBold(int3 pos, char const *str); | |||
int2 GetSize() const; | |||
private: | |||
FontData *data; | |||
@@ -18,15 +18,24 @@ | |||
#include "matrix.h" | |||
class WorldEntity; | |||
class Input | |||
{ | |||
public: | |||
/* These methods are general queries */ | |||
static float2 GetAxis(int axis); | |||
static void SetMousePos(int2 coord); | |||
static int2 GetMousePos(); | |||
static int3 GetMouseButtons(); | |||
/* Entities can subscribe to events */ | |||
static void ListenMouse(WorldEntity *e); | |||
static void UnlistenMouse(WorldEntity *e); | |||
/* These methods are called by the underlying input listeners */ | |||
static void SetMousePos(int2 coord); | |||
static void SetMouseButton(int index); | |||
static void UnsetMouseButton(int index); | |||
static int3 GetMouseButtons(); | |||
}; | |||
#endif // __DH_INPUT_H__ | |||
@@ -27,7 +27,7 @@ class TextData | |||
friend class Text; | |||
private: | |||
int font; | |||
int font, align, length; | |||
char *text; | |||
int3 pos; | |||
}; | |||
@@ -41,6 +41,7 @@ Text::Text(char const *text, char const *font) | |||
{ | |||
data->font = Forge::Register(font); | |||
data->text = text ? strdup(text) : NULL; | |||
data->length = text ? strlen(text) : 0; | |||
data->pos = int3(0, 0, 0); | |||
drawgroup = DRAWGROUP_HUD; | |||
@@ -51,15 +52,17 @@ void Text::SetText(char const *text) | |||
if (data->text) | |||
free(data->text); | |||
data->text = text ? strdup(text) : NULL; | |||
data->length = text ? strlen(text) : 0; | |||
} | |||
void Text::SetInt(int val) | |||
{ | |||
if (data->text) | |||
free(data->text); | |||
char buf[128]; | |||
sprintf(buf, "%i", val); | |||
data->text = strdup(buf); | |||
char text[128]; | |||
sprintf(text, "%i", val); | |||
data->text = strdup(text); | |||
data->length = strlen(text); | |||
} | |||
void Text::SetPos(int3 pos) | |||
@@ -67,6 +70,11 @@ void Text::SetPos(int3 pos) | |||
data->pos = pos; | |||
} | |||
void Text::SetAlign(int align) | |||
{ | |||
data->align = align; | |||
} | |||
void Text::TickDraw(float deltams) | |||
{ | |||
Entity::TickDraw(deltams); | |||
@@ -74,7 +82,12 @@ void Text::TickDraw(float deltams) | |||
if (data->text) | |||
{ | |||
Font *font = Forge::GetFont(data->font); | |||
font->Print(data->pos, data->text); | |||
int3 delta = 0; | |||
if (data->align == ALIGN_RIGHT) | |||
delta.x -= data->length * font->GetSize().x; | |||
else if (data->align == ALIGN_CENTER) | |||
delta.x -= data->length * font->GetSize().x / 2; | |||
font->Print(data->pos + delta, data->text); | |||
} | |||
} | |||
@@ -29,6 +29,14 @@ public: | |||
void SetText(char const *text); | |||
void SetInt(int val); | |||
void SetPos(int3 pos); | |||
void SetAlign(int align); | |||
enum | |||
{ | |||
ALIGN_LEFT, | |||
ALIGN_RIGHT, | |||
ALIGN_CENTER, | |||
}; | |||
protected: | |||
virtual void TickDraw(float deltams); | |||