Browse Source

Test entity alignment features. Also, start working on the mouse tracking.

legacy
Sam Hocevar sam 14 years ago
parent
commit
50ed9ffecc
5 changed files with 51 additions and 16 deletions
  1. +13
    -9
      src/font.cpp
  2. +1
    -0
      src/font.h
  3. +11
    -2
      src/input.h
  4. +18
    -5
      src/text.cpp
  5. +8
    -0
      src/text.h

+ 13
- 9
src/font.cpp View File

@@ -40,7 +40,7 @@ private:
char *name; char *name;


SDL_Surface *img; SDL_Surface *img;
int width, height;
int2 size;
float tx, ty; float tx, ty;
GLuint texture; GLuint texture;
}; };
@@ -68,10 +68,9 @@ Font::Font(char const *path)
exit(1); 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; drawgroup = DRAWGROUP_BEFORE;
} }
@@ -150,14 +149,14 @@ void Font::Print(int3 pos, char const *str)
glTexCoord2f(tx, ty + data->ty); glTexCoord2f(tx, ty + data->ty);
glVertex2f(pos.x, pos.y); glVertex2f(pos.x, pos.y);
glTexCoord2f(tx + data->tx, ty + data->ty); 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); 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); 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(); glEnd();
} }
@@ -188,3 +187,8 @@ void Font::PrintBold(int3 pos, char const *str)
glPopAttrib(); glPopAttrib();
} }


int2 Font::GetSize() const
{
return data->size;
}


+ 1
- 0
src/font.h View File

@@ -35,6 +35,7 @@ public:
/* New methods */ /* New methods */
void Print(int3 pos, char const *str); void Print(int3 pos, char const *str);
void PrintBold(int3 pos, char const *str); void PrintBold(int3 pos, char const *str);
int2 GetSize() const;


private: private:
FontData *data; FontData *data;


+ 11
- 2
src/input.h View File

@@ -18,15 +18,24 @@


#include "matrix.h" #include "matrix.h"


class WorldEntity;

class Input class Input
{ {
public: public:
/* These methods are general queries */
static float2 GetAxis(int axis); static float2 GetAxis(int axis);
static void SetMousePos(int2 coord);
static int2 GetMousePos(); 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 SetMouseButton(int index);
static void UnsetMouseButton(int index); static void UnsetMouseButton(int index);
static int3 GetMouseButtons();
}; };


#endif // __DH_INPUT_H__ #endif // __DH_INPUT_H__


+ 18
- 5
src/text.cpp View File

@@ -27,7 +27,7 @@ class TextData
friend class Text; friend class Text;


private: private:
int font;
int font, align, length;
char *text; char *text;
int3 pos; int3 pos;
}; };
@@ -41,6 +41,7 @@ Text::Text(char const *text, char const *font)
{ {
data->font = Forge::Register(font); data->font = Forge::Register(font);
data->text = text ? strdup(text) : NULL; data->text = text ? strdup(text) : NULL;
data->length = text ? strlen(text) : 0;
data->pos = int3(0, 0, 0); data->pos = int3(0, 0, 0);


drawgroup = DRAWGROUP_HUD; drawgroup = DRAWGROUP_HUD;
@@ -51,15 +52,17 @@ void Text::SetText(char const *text)
if (data->text) if (data->text)
free(data->text); free(data->text);
data->text = text ? strdup(text) : NULL; data->text = text ? strdup(text) : NULL;
data->length = text ? strlen(text) : 0;
} }


void Text::SetInt(int val) void Text::SetInt(int val)
{ {
if (data->text) if (data->text)
free(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) void Text::SetPos(int3 pos)
@@ -67,6 +70,11 @@ void Text::SetPos(int3 pos)
data->pos = pos; data->pos = pos;
} }


void Text::SetAlign(int align)
{
data->align = align;
}

void Text::TickDraw(float deltams) void Text::TickDraw(float deltams)
{ {
Entity::TickDraw(deltams); Entity::TickDraw(deltams);
@@ -74,7 +82,12 @@ void Text::TickDraw(float deltams)
if (data->text) if (data->text)
{ {
Font *font = Forge::GetFont(data->font); 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);
} }
} }




+ 8
- 0
src/text.h View File

@@ -29,6 +29,14 @@ public:
void SetText(char const *text); void SetText(char const *text);
void SetInt(int val); void SetInt(int val);
void SetPos(int3 pos); void SetPos(int3 pos);
void SetAlign(int align);

enum
{
ALIGN_LEFT,
ALIGN_RIGHT,
ALIGN_CENTER,
};


protected: protected:
virtual void TickDraw(float deltams); virtual void TickDraw(float deltams);


Loading…
Cancel
Save