Permalink
Browse files

initial support for multiple fonts - possibly buggy!

  • Loading branch information...
1 parent 3348cdc commit a7ef0c1c63df12a9ea996f4413594cc79844554c chris committed Jul 25, 2012
Showing with 123 additions and 29 deletions.
  1. +12 −2 README.md
  2. +5 −3 examples/chiptest.c
  3. +12 −6 examples/invaders.c
  4. +3 −2 examples/phystest.c
  5. +11 −4 examples/simple.c
  6. +3 −1 examples/sprites.c
  7. +18 −2 include/support.h
  8. BIN resources/textures/bigfont.png
  9. +59 −9 src/support.c
View
@@ -182,9 +182,19 @@ This initialises the resources used by the glPrintf you must supply the windows
_____
-__void glPrintf(float x, float y, const char *fmt, ...);__
+__font_t* createFont(const char* tpath,uint cbase,float tHeight,float tLines, int fWidth, int fHeight);__
-this behaves exactly like a normal printf except for the first two parameters which specify the starting coordinate
+tpath is the full path and of the texture for this font, cbase is the ascii code of the first character
+tHeight is the height in pixels of the texture, tLines specifies the number of lines in total the texture
+contains (I think there is a bug with this!) fWidth and fHeight are the width and height a character
+
+TODO freeFont to release a fonts resources...
+_____
+
+__void glPrintf(float x, float y, font_t fnt, const char *fmt, ...);__
+
+this behaves exactly like a normal printf except for the first two parameters which specify the starting coordinate
+you must specify a previously created font structure to print with
_____
View
@@ -56,7 +56,7 @@ struct cloud_t {
struct cloud_t clouds[max_clouds];
-
+font_t *font1;
int main()
{
@@ -78,7 +78,9 @@ int main()
// initialises glprint's matrix, shader and texture
initGlPrint(getDisplayWidth(), getDisplayHeight());
- initSprite(getDisplayWidth(), getDisplayHeight());
+ font1=createFont("resources/textures/font.png",0,256,16,16,16);
+
+ initSprite(getDisplayWidth(), getDisplayHeight());
centreX=((float)getDisplayWidth())/2.0;
centreY=((float)getDisplayHeight())/2.0;
@@ -208,7 +210,7 @@ void render()
}
// see printf documentation for the formatting of variables...
- glPrintf(100, 240, "frame=%i", frame);
+ glPrintf(100, 240, font1,"frame=%i", frame);
// swap the front (visible) buffer for the back (offscreen) buffer
swapBuffers();
View
@@ -21,6 +21,9 @@ GLuint cubeTex, shipTex, alienTex, shotTex, expTex;
// structures holding various pointers and handles for obj shapes
struct obj_t cubeObj, shipObj, alienObj, shotObj;
+// pointers to glprint font structures
+font_t *font1,*font2;
+
// matrices and combo matrices
kmMat4 model, view, projection, mvp, vp, mv;
@@ -217,6 +220,9 @@ int main()
// initialises glprint's matrix shader and texture
initGlPrint(getDisplayWidth(), getDisplayHeight());
+ font1=createFont("resources/textures/font.png",0,256,16,16,16);
+ font2=createFont("resources/textures/bigfont.png",32,512,9.5,32,48);
+
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -294,8 +300,8 @@ int main()
glBindTexture(GL_TEXTURE_2D, shipTex);
drawObj(&shipObj, &mvp, &mv, lightDir, viewDir);
- glPrintf(100 + sinf(rad) * 16, 240 + cosf(rad) * 16,
- "frame=%i fps=%3.2f", frame, lfps);
+ glPrintf(50 + sinf(rad) * 16, 240 + cosf(rad) * 16,
+ font2,"frame=%i fps=%3.2f", frame, lfps);
kmVec3 tmp;
@@ -437,11 +443,11 @@ int main()
kmVec3Normalize(&viewDir,&viewDir);
// dump values
- glPrintf(100, 280, "eye %3.2f %3.2f %3.2f ", pEye.x, pEye.y, pEye.z);
- glPrintf(100, 296, "centre %3.2f %3.2f %3.2f ", pCenter.x, pCenter.y,
+ glPrintf(100, 280, font1,"eye %3.2f %3.2f %3.2f ", pEye.x, pEye.y, pEye.z);
+ glPrintf(100, 296, font1,"centre %3.2f %3.2f %3.2f ", pCenter.x, pCenter.y,
pCenter.z);
- glPrintf(100, 320, "mouse %i,%i %i ", mouse[0], mouse[1], mouse[2]);
- glPrintf(100, 340, "frame %i %i ", frame, frame % 20);
+ glPrintf(100, 320, font1,"mouse %i,%i %i ", mouse[0], mouse[1], mouse[2]);
+ glPrintf(100, 340, font1,"frame %i %i ", frame, frame % 20);
View
@@ -126,6 +126,7 @@ void render(); // func prototype
// obj shape textures
GLuint cubeTex, groundTex,ballTex;
+font_t *font1;
// structures holding various pointers and handles for obj shapes
struct obj_t cubeObj, groundObj,ballObj;
@@ -205,7 +206,7 @@ int main()
// initialises glprint's matrix, shader and texture
initGlPrint(getDisplayWidth(), getDisplayHeight());
-
+ font1=createFont("resources/textures/font.png",0,256,16,16,16);
// we don't want to draw the back of triangles
// the blending is set up for glprint but disabled
// while not in use
@@ -379,7 +380,7 @@ void render()
}
}
// see printf documentation for the formatting of variables...
- glPrintf(100, 240, "frame=%i", frame);
+ glPrintf(100, 240, font1,"frame=%i", frame);
// swap the front (visible) buffer for the back (offscreen) buffer
swapBuffers();
View
@@ -125,6 +125,8 @@ bool *keys;
int *mouse;
struct joystick_t *joy1;
+font_t *font1,*font2;
+
int main()
{
@@ -181,6 +183,9 @@ int main()
// initialises glprint's matrix, shader and texture
initGlPrint(getDisplayWidth(), getDisplayHeight());
+ font1=createFont("resources/textures/font.png",0,256,16,16,16);
+ font2=createFont("resources/textures/bigfont.png",32,512,9.5,32,48);
+
// we don't want to draw the back of triangles
// the blending is set up for glprint but disabled
// while not in use
@@ -305,12 +310,14 @@ void render()
// see printf documentation for the formatting of variables...
- glPrintf(100, 240, "frame=%i", frame);
+ glPrintf(24, 24, font2, "frame=%i", frame);
- glPrintf(100, 260, "mouse %i %i %i", mouse[0],mouse[1],mouse[2]);
+ glPrintf(100, 260, font1, "mouse %i %i %i", mouse[0],mouse[1],mouse[2]);
- glPrintf(100, 280, "joystick %i,%i %i",joy1->axis[0],joy1->axis[1],joy1->buttons);
-
+ glPrintf(100, 280, font1, "joystick %i,%i %i",joy1->axis[0],joy1->axis[1],joy1->buttons);
+
+
+ glPrintf(100, 320, font2, "abcABCqrsQRS123");
//rmx+=mouse[0];
//rmy+=mouse[1];
//glPrintf(100, 280, "%i %i", rmx,rmy);
View
@@ -24,6 +24,7 @@ void render(); // func prototype
GLuint cloudTex,biTex,triTex;
float centreX,centreY,cloudW,cloudH,planeW,planeH;
+font_t *font1;
// matrices and combo matrices
kmMat4 model, view, projection, mvp, vp, mv;
@@ -67,6 +68,7 @@ int main()
// initialises glprint's matrix, shader and texture
initGlPrint(getDisplayWidth(), getDisplayHeight());
+ font1=createFont("resources/textures/font.png",0,256,16,16,16);
initSprite(getDisplayWidth(), getDisplayHeight());
centreX=((float)getDisplayWidth())/2.0;
@@ -165,7 +167,7 @@ void render()
// see printf documentation for the formatting of variables...
- glPrintf(100, 240, "frame=%i", frame);
+ glPrintf(100, 240, font1,"frame=%i", frame);
// swap the front (visible) buffer for the back (offscreen) buffer
swapBuffers();
View
@@ -15,8 +15,6 @@ void print_log(GLuint object);
int loadPNG(const char *filename);
int makeContext();
void closeContext();
-void initGlPrint(int w, int h);
-void glPrintf(float x, float y, const char *fmt, ...);
void swapBuffers();
int getDisplayWidth();
int getDisplayHeight();
@@ -40,3 +38,21 @@ void drawPointCloud(struct pointCloud_t* pntC,kmMat4* m);
void freePointCloud(struct pointCloud_t* pntC);
+struct __fnt {
+ uint tex;
+ uint base;
+ uint vertBuf;
+ uint texBuf;
+ float tHeight;
+ float tLines;
+ int fWidth;
+ int fHeight;
+};
+
+typedef struct __fnt font_t;
+
+font_t* createFont(const char* tpath,uint cbase,float tHeight,float tLines, int fWidth, int fHeight);
+void initGlPrint(int w, int h);
+void glPrintf(float x, float y, font_t* fnt, const char *fmt, ...);
+
+
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
@@ -620,6 +620,7 @@ struct { // blob of globals for the glPrint routine
void initGlPrint(int w, int h)
{
+/*
const GLfloat quadVertices[] = {
0, 0, 0,
16, 16, 0,
@@ -638,7 +639,7 @@ void initGlPrint(int w, int h)
0, 0,
0, 1. / 16
};
-
+*/
kmMat4OrthographicProjection(&__glp.opm, 0, w, h, 0, -10, 10);
@@ -668,6 +669,7 @@ void initGlPrint(int w, int h)
__glp.vert_attrib = getShaderLocation(shaderAttrib, __glp.printProg, "vert_attrib");
__glp.uv_attrib = getShaderLocation(shaderAttrib, __glp.printProg, "uv_attrib");
+/*
__glp.fonttex = loadPNG("resources/textures/font.png");
glGenBuffers(1, &__glp.quadvbo);
@@ -679,10 +681,51 @@ void initGlPrint(int w, int h)
glBindBuffer(GL_ARRAY_BUFFER, __glp.texvbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * 6, texCoord,
GL_STATIC_DRAW);
+*/
+}
+
+
+
+font_t* createFont(const char* tpath,uint cbase,float tHeight,float tLines,int fWidth,int fHeight) {
+ font_t *t=malloc(sizeof(font_t));
+
+ t->tex = loadPNG(tpath);
+ t->base=cbase;
+ t->tHeight=tHeight;
+ t->tLines=tLines;
+ t->fWidth=fWidth;
+ t->fHeight=fHeight;
+
+ float *vb=malloc(sizeof(float) * 3 * 6);
+
+ vb[0]=vb[1]=vb[2]=vb[5]=vb[7]=vb[8]=vb[11]=vb[12]=vb[14]=vb[15]=vb[17]=0;
+ vb[3]=vb[6]=vb[9]=fWidth;
+ vb[4]=vb[10]=vb[16]=fHeight;
+
+ glGenBuffers(1, &t->vertBuf);
+ glBindBuffer(GL_ARRAY_BUFFER, t->vertBuf);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * 6, vb, GL_STATIC_DRAW);
+
+ free(vb);
+
+ float *tc=malloc(sizeof(float) * 2 * 6);
+ tc[0]=tc[1]=tc[5]=tc[8]=tc[9]=tc[10]=0;
+ tc[2]=tc[4]=tc[6]=1./16;
+ tc[3]=tc[7]=tc[11]=1./tLines;
+
+ glGenBuffers(1, &t->texBuf);
+ glBindBuffer(GL_ARRAY_BUFFER, t->texBuf);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 2 * 6, tc, GL_STATIC_DRAW);
+
+ free(tc);
+
+
+
+ return t;
}
-void glPrintf(float x, float y, const char *fmt, ...)
+void glPrintf(float x, float y, font_t *fnt, const char *fmt, ...)
{
char text[256];
va_list ap;
@@ -698,24 +741,31 @@ void glPrintf(float x, float y, const char *fmt, ...)
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
- glBindTexture(GL_TEXTURE_2D, __glp.fonttex);
+ //glBindTexture(GL_TEXTURE_2D, __glp.fonttex);
+ glBindTexture(GL_TEXTURE_2D, fnt->tex);
glEnableVertexAttribArray(__glp.vert_attrib);
- glBindBuffer(GL_ARRAY_BUFFER, __glp.quadvbo);
+// glBindBuffer(GL_ARRAY_BUFFER, __glp.quadvbo);
+ glBindBuffer(GL_ARRAY_BUFFER, fnt->vertBuf);
glVertexAttribPointer(__glp.vert_attrib, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(__glp.uv_attrib);
- glBindBuffer(GL_ARRAY_BUFFER, __glp.texvbo);
+// glBindBuffer(GL_ARRAY_BUFFER, __glp.texvbo);
+ glBindBuffer(GL_ARRAY_BUFFER, fnt->texBuf);
glVertexAttribPointer(__glp.uv_attrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
glUniform1i(__glp.texture_uniform, 0);
for (int n = 0; n < strlen(text); n++) {
- int c = (int)text[n];
+ int c = (int)text[n]-fnt->base;
+// float cx = c % 16;
+// float cy = (int)(c / 16.0);
+// cy = cy * (1. / 16);
+// cx = cx * (1. / 16);
float cx = c % 16;
- float cy = (int)(c / 16.0);
- cy = cy * (1. / 16);
+ float cy = (int)(c/16.0);
+ cy = cy * (1. / (fnt->tLines));
cx = cx * (1. / 16);
glUniformMatrix4fv(__glp.opm_uniform, 1, GL_FALSE, (GLfloat *) & __glp.otm);
@@ -724,7 +774,7 @@ void glPrintf(float x, float y, const char *fmt, ...)
glDrawArrays(GL_TRIANGLES, 0, 6);
- kmMat4Translation(&__glp.t, 16, 0, 0);
+ kmMat4Translation(&__glp.t, fnt->fWidth, 0, 0);
kmMat4Multiply(&__glp.otm, &__glp.otm, &__glp.t);
}

0 comments on commit a7ef0c1

Please sign in to comment.