-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapper_pointtext.cpp
More file actions
106 lines (88 loc) · 3.53 KB
/
wrapper_pointtext.cpp
File metadata and controls
106 lines (88 loc) · 3.53 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**********************************************/
/* Turbo Turtle Wrapper - Point Text Renderer */
/* */
/* Copyright (c) 2009 by Richard Goedeken */
/* Richard@fascinationsoftware.com */
/**********************************************/
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL.h>
#include <SDL_opengl.h>
// these data are defined in wrapper_fontdata.cpp
extern const short bitmap_font10x18[96][18];
extern const short bitmap_font12x16[96][16];
extern const short bitmap_font8x12[96][12];
// these data are defined in wrapper_main.cpp
extern float fPixelsPerTurtleStep;
struct font {
const short *psBitmapData;
int nFirstChar;
int nChars;
int nCols;
int nRows;
};
#define NUM_FONTS 3
struct font my_fonts[NUM_FONTS] = { { (const short *) bitmap_font8x12, 32, 96, 8, 12 },
{ (const short *) bitmap_font10x18, 32, 96, 10, 18 },
{ (const short *) bitmap_font12x16, 32, 96, 12, 16 } };
bool DrawPointText(int iFont, int iJustifyVert, int iJustifyHorz, float fHeight, float fX, float fY, const char *string)
{
// return if font index is invalid
if (iFont < 0 || iFont > NUM_FONTS - 1)
return false;
// get parameters for this font
int iFirstChar = my_fonts[iFont].nFirstChar;
int iFontChars = my_fonts[iFont].nChars;
int iFontRows = my_fonts[iFont].nRows;
int iFontCols = my_fonts[iFont].nCols;
int iStrLength = strlen(string);
// handle vertical justification
float fTopY = fY;
if (iJustifyVert == 1) fTopY += fHeight / 2.0;
else if (iJustifyVert >= 2) fTopY += fHeight;
// handle horizontal justification
float fLeftX = fX;
float fPointSpace = fHeight / iFontRows;
if (iJustifyHorz == 1) fLeftX -= iStrLength * iFontCols * fPointSpace / 2.0;
else if (iJustifyHorz >= 2) fLeftX -= iStrLength * iFontCols * fPointSpace;
// set up openGL
float fOldPointSize;
glGetFloatv(GL_POINT_SIZE, &fOldPointSize);
glPointSize(fPointSpace * fPixelsPerTurtleStep * 1.5);
glBegin(GL_POINTS);
// dot-matrix it up
for (int i = 0; i < iStrLength; i++)
{
char ch = string[i];
if (ch >= iFirstChar && ch < iFirstChar + iFontChars)
{
const short *psLetterBitmap = my_fonts[iFont].psBitmapData + ((ch - iFirstChar) * iFontRows);
for (int j = 0; j < iFontRows; j++)
{
int iRowBitmap = psLetterBitmap[j];
int mask = (1 << (iFontCols-1));
fY = fTopY - fPointSpace * j;
for (int k = 0; mask > 0; k += 1, mask >>= 1)
{
if (iRowBitmap & mask)
glVertex2f(fLeftX + k * fPointSpace, fY);
}
}
}
fLeftX += iFontCols * fPointSpace;
}
// OpenGL cleanup
glEnd();
glPointSize(fOldPointSize);
return true;
}