You probably wondered in early days how to add word wrap and line returns to your opengl text routines, basicly this is just what you want, this function will draw a text and wrap it by character (not words!) to fir the width you want.
Code:
Procedure DrawFont(x, y, mx, my: single; const text: string; fontn, wordwrap: integer);
var
i: integer;
b: byte;
cw: integer;
ta, tb, tc, td: single;
va, vb, vc, vd: single;
// current left edge
L: single;
// skip this much of characters from being parsed
SK: integer;
procedure wrap;
begin
cw:= 0;
L:= L + (FontGeo[32].H * my);
end;
procedure EatColor; // changes font color depending on keyword
begin
if text[i + 2] = 'W' then glcolor3f(1, 1, 1);
if text[i + 2] = 'N' then glcolor3f(0, 0, 0);
if text[i + 2] = 'R' then glcolor3f(1, 0, 0);
if text[i + 2] = 'G' then glcolor3f(0, 1, 0);
if text[i + 2] = 'B' then glcolor3f(0, 0, 1);
SK:= 2; // skip 2 chars so the color coding is like ~G~
end;
begin
if length(text) = 0 then exit; // idiot proof fuse
// ensure variables are reset
cw:= 0;
L:= 0;
SK:= 0;
glBindTexture(GL_TEXTURE_2D, Font.tex[fontn].GLID);
glBegin(GL_quads);
for i:= 0 to length(text) do begin
if SK <> 0 then begin SK:= SK -1; continue; end;
b:= byte(text[i + 1]); // we need a integer type to use for array access,
// next character would wrap
if (wordwrap <> 0) and (cw + trunc(FontGeo[b].spacing * mx) > wordwrap) then wrap;
// return character - go to new line
if b = 13 then begin wrap; continue; end;
// ~X~ color coding keyword
if b = 126 then begin eatcolor; continue; end;
// get tex coords from array
ta:= FontGeo[b].X / 256;
tb:= FontGeo[b].X / 256 + FontGeo[b].W / 256;
tc:= FontGeo[b].Y / 256;
td:= FontGeo[b].Y / 256 + FontGeo[b].H / 256;
// build vertices
va:= cw; // top left
vb:= va + (FontGeo[b].W * mx); // top right
vc:= (FontGeo[b].YOFS * my) + L; // bottom left
vd:= vc + (FontGeo[b].H * my); // bottom right
// crunch
gltexcoord2f(ta, tc); glvertex2fwin(x + va, y + vc);
gltexcoord2f(ta, td); glvertex2fwin(x + va, y + vd);
gltexcoord2f(tb, td); glvertex2fwin(x + vb, y + vd);
gltexcoord2f(tb, tc); glvertex2fwin(x + vb, y + vc);
// add character size to left edge
cw:= cw + trunc(FontGeo[b].spacing * mx);
end;
glend;
end;
character support records:
Code:
Glyph = packed record
X, Y, W, H, YOFS, SPACING: byte;
end;
FontGeo: array[0..255] of Glyph;
license: none, you can even eat it or whatever you want, but i take no responsibility for aynthing.
2d opengl routines:
Code:
//this draws a vertex in windows coordinate system
procedure glvertex2fwin(const x, y: single);
begin
glvertex2f(x, win_height - y);
end;
how to get into this 2d mode?
Code:
// Dwarf with Axe - GAMEDEV forums: 18 July 2002 6:12:57 PM
//
// There have been thousands of posts along the lines of
// "How do I do 2d in OpenGL" to "Duuuhde, I wunt too maek
// a two dee gaem in ohpun jee el; how do eye set uhp two dee???!?"
//
// I have developed a simple, nice, pretty way for all of you to have your 2D fun.
procedure GlEnable2D;
var
vport: array[0..3] of integer;
begin
glGetIntegerv(GL_VIEWPORT, @vPort);
glMatrixMode(GL_PROJECTION);
glPushMatrix;
glLoadIdentity;
glOrtho(0, vPort[2], 0, vPort[3], -1, 1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix;
glLoadIdentity;
end;
procedure GlDisable2D;
begin
glMatrixMode(GL_PROJECTION);
glPopMatrix;
glMatrixMode(GL_MODELVIEW);
glPopMatrix;
end;
Bookmarks