summaryrefslogtreecommitdiffstats
path: root/menu_demo/font.c
blob: 03321742442a59b3241798efc00418a8a2afdc33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#ifdef G_OS_WIN32
  #define WIN32_LEAN_AND_MEAN 1
  #include <windows.h>
#else
  #define FALSE 0
  #define TRUE !FALSE
#endif

#include <GL/gl.h>
#include <string.h>

#include "font.h"
#include "bitmap.h"

struct BitmapStruct g_sFont;

int g_iWinWidth;
int g_iWinHeight;

GLuint g_iBase;


void FontInit(int f_iWinWidth, int f_iWinHeight)
{
	float	cx;
	float	cy;
  int l_iImageId;
  int i = 0;

  float x = (float)(1.0f/FONT_XCHAR);

  g_iWinWidth  = f_iWinWidth;
  g_iWinHeight = f_iWinHeight;

  g_sFont = BitmapLoad("img/continuum_textured_alpha.bmp");
  l_iImageId = g_sFont.m_iImageId;

	g_iBase = glGenLists(FONT_COUNT);
	glBindTexture(GL_TEXTURE_2D, l_iImageId);

	for (i = 0; i < FONT_COUNT; i++)
	{
		cx = (float)(i % 16) / 16.0f;
		cy = (float)(i / 16) / 16.0f;

		glNewList(g_iBase + i, GL_COMPILE);

      glBegin(GL_TRIANGLE_STRIP);
				glTexCoord2f(cx,     1 - cy);     glVertex2i(0,          0);
				glTexCoord2f(cx + x, 1 - cy);     glVertex2i(FONT_WIDTH, 0);
				glTexCoord2f(cx,     1 - cy - x); glVertex2i(0,          FONT_HEIGHT);
				glTexCoord2f(cx + x, 1 - cy - x); glVertex2i(FONT_WIDTH, FONT_HEIGHT);
			glEnd();

			glTranslated(FONT_SPACING, 0, 0);
		glEndList();
	}

} // BuildFont


void FontDelete(void)
{
	glDeleteLists(g_iBase, FONT_COUNT);

} // FontDelete


void glPrint(GLint x, GLint y, char *string, struct ColorStruct f_sColor)
{
  glColor3d(f_sColor.m_dRed, f_sColor.m_dGreen, f_sColor.m_dBlue);
  glBindTexture(GL_TEXTURE_2D, g_sFont.m_iImageId);
	glDisable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);

	glPushMatrix();
	  glLoadIdentity();
	  glOrtho(0, g_iWinWidth, g_iWinHeight, 0, 0, 1);
	  glMatrixMode(GL_MODELVIEW);
	  glPushMatrix();
	    glLoadIdentity();
	    glTranslated(x, y, 0);
	    glListBase(g_iBase - 32);
	    glCallLists((GLsizei)strlen(string), GL_UNSIGNED_BYTE, string);
	    glMatrixMode(GL_PROJECTION);
	  glPopMatrix();
	  glMatrixMode(GL_MODELVIEW);
	glPopMatrix();

} // glPrint