initial release of new RasterFont without color support
this new design will once create a texture for all chars. while rendering a string, a list of polygons (position on screen + texture) for this string is generated on the fly and print at once by glDrawArrays. atm, there is no support for colors, so everything will display white. Signed-off-by: Ryan Houdek <Sonicadvance1@gmail.com>
This commit is contained in:
parent
c207422987
commit
75dd48247f
|
@ -22,7 +22,13 @@
|
|||
#include "RasterFont.h"
|
||||
// globals
|
||||
|
||||
const GLubyte rasters[][13] = {
|
||||
|
||||
static const u32 char_width = 8;
|
||||
static const u32 char_height = 13;
|
||||
static const u32 char_offset = 32;
|
||||
static const u32 char_count = 95;
|
||||
|
||||
const u8 rasters[char_count][char_height] = {
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
|
||||
{0x00, 0x00, 0x18, 0x18, 0x00, 0x00, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18},
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x36, 0x36, 0x36},
|
||||
|
@ -122,102 +128,122 @@ const GLubyte rasters[][13] = {
|
|||
|
||||
RasterFont::RasterFont()
|
||||
{
|
||||
// set GL modes
|
||||
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
||||
// create the raster font
|
||||
fontOffset = glGenLists(128);
|
||||
for (int i = 32; i < 127; i++) {
|
||||
glNewList(i + fontOffset, GL_COMPILE);
|
||||
glBitmap(8, 13, 0.0f, 2.0f, 10.0f, 0.0f, rasters[i - 32]);
|
||||
glEndList();
|
||||
// generate the texture
|
||||
glEnable(GL_TEXTURE_RECTANGLE);
|
||||
glGenTextures(1, &texture);
|
||||
glBindTexture(GL_TEXTURE_RECTANGLE, texture);
|
||||
u32* texture_data = new u32[char_width*char_count*char_height];
|
||||
for(u32 y=0; y<char_height; y++) {
|
||||
for(u32 c=0; c<char_count; c++) {
|
||||
for(u32 x=0; x<char_width; x++) {
|
||||
bool pixel = rasters[c][y] & (1<<(char_width-x-1));
|
||||
texture_data[char_width*char_count*y+char_width*c+x] = pixel ? -1 : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
temp_buffer = new char[TEMP_BUFFER_SIZE];
|
||||
glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, char_width*char_count, char_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture_data);
|
||||
delete [] texture_data;
|
||||
|
||||
// generate VBO & VAO
|
||||
glGenBuffers(1, &VBO);
|
||||
glGenVertexArrays(1, &VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBindVertexArray(VAO);
|
||||
glEnableClientState(GL_VERTEX_ARRAY);
|
||||
glVertexPointer(2, GL_FLOAT, sizeof(GLfloat)*4, NULL);
|
||||
glEnable(GL_TEXTURE_RECTANGLE);
|
||||
glClientActiveTexture(GL_TEXTURE0);
|
||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||
glTexCoordPointer(2, GL_FLOAT, sizeof(GLfloat)*4, (GLfloat*)NULL+2);
|
||||
}
|
||||
|
||||
RasterFont::~RasterFont()
|
||||
{
|
||||
glDeleteLists(fontOffset, 128);
|
||||
delete [] temp_buffer;
|
||||
glDeleteTextures(1, &texture);
|
||||
glDeleteBuffers(1, &VBO);
|
||||
glDeleteVertexArrays(1, &VAO);
|
||||
}
|
||||
|
||||
void RasterFont::printString(const char *s, double x, double y, double z)
|
||||
void RasterFont::printMultilineText(const char *text, double start_x, double start_y, double z, int bbWidth, int bbHeight, u32 color)
|
||||
{
|
||||
int length = (int)strlen(s);
|
||||
if(!(color & 0xffffff)) return; // color are not supported atm, so do not draw black shadows
|
||||
|
||||
int length = (int)strlen(text);
|
||||
if (!length)
|
||||
return;
|
||||
if (length >= TEMP_BUFFER_SIZE)
|
||||
length = TEMP_BUFFER_SIZE - 1;
|
||||
|
||||
// Sanitize string to avoid GL errors.
|
||||
char *s2 = temp_buffer;
|
||||
memcpy(s2, s, length);
|
||||
s2[length] = 0;
|
||||
for (int i = 0; i < length; i++) {
|
||||
if (s2[i] < 32 || s2[i] > 126)
|
||||
s2[i] = '!';
|
||||
}
|
||||
|
||||
// go to the right spot
|
||||
glRasterPos3d(x, y, z);
|
||||
GL_REPORT_ERRORD();
|
||||
|
||||
glPushAttrib (GL_LIST_BIT);
|
||||
glListBase(fontOffset);
|
||||
glCallLists((GLsizei)strlen(s2), GL_UNSIGNED_BYTE, (GLubyte *) s2);
|
||||
GL_REPORT_ERRORD();
|
||||
glPopAttrib();
|
||||
GL_REPORT_ERRORD();
|
||||
}
|
||||
|
||||
void RasterFont::printCenteredString(const char *s, double y, int screen_width, double z)
|
||||
{
|
||||
int length = (int)strlen(s);
|
||||
int x = (int)(screen_width/2.0 - (length/2.0)*char_width);
|
||||
printString(s, x, y, z);
|
||||
}
|
||||
|
||||
void RasterFont::printMultilineText(const char *text, double start_x, double start_y, double z, int bbWidth, int bbHeight)
|
||||
{
|
||||
double x = start_x;
|
||||
double y = start_y;
|
||||
char temp[1024];
|
||||
char *t = temp;
|
||||
while (*text)
|
||||
{
|
||||
if (*text == '\n')
|
||||
{
|
||||
*t = 0;
|
||||
printString(temp, x, y, z);
|
||||
y -= char_height * 2.0f / bbHeight;
|
||||
return; // nothing to do
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, length*4*6*sizeof(GLfloat), NULL, GL_STREAM_DRAW);
|
||||
GLfloat *vertices = (GLfloat*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
|
||||
|
||||
int usage = 0;
|
||||
GLfloat delta_x = 2*char_width/GLfloat(bbWidth);
|
||||
GLfloat delta_y = 2*char_height/GLfloat(bbHeight);
|
||||
GLfloat border_x = 1*2/GLfloat(bbWidth);
|
||||
GLfloat border_y = 2*2/GLfloat(bbHeight);
|
||||
|
||||
GLfloat x = start_x;
|
||||
GLfloat y = start_y;
|
||||
|
||||
for(int i=0; i<length; i++) {
|
||||
u8 c = text[i];
|
||||
|
||||
if(c == '\n') {
|
||||
x = start_x;
|
||||
t = temp;
|
||||
y -= delta_y + border_y;
|
||||
continue;
|
||||
}
|
||||
else if (*text == '\r')
|
||||
{
|
||||
t = temp;
|
||||
|
||||
// do not print spaces, they can be skipped easyly
|
||||
if(c == ' ') {
|
||||
x += delta_x + border_x;
|
||||
continue;
|
||||
}
|
||||
else if (*text == '\t')
|
||||
{
|
||||
//todo: add tabs every something like 4*char_width
|
||||
*t = 0;
|
||||
int cpos = (int)strlen(temp);
|
||||
int newpos = (cpos + 4) & (~3);
|
||||
printString(temp, x, y, z);
|
||||
x = start_x + (char_width*newpos) * 2.0f / bbWidth;
|
||||
t = temp;
|
||||
*t++ = ' ';
|
||||
}
|
||||
else
|
||||
*t++ = *text;
|
||||
|
||||
text++;
|
||||
|
||||
if(c < char_offset || c >= char_count+char_offset) continue;
|
||||
|
||||
vertices[usage++] = x;
|
||||
vertices[usage++] = y;
|
||||
vertices[usage++] = (c-char_offset)*char_width;
|
||||
vertices[usage++] = 0;
|
||||
|
||||
vertices[usage++] = x+delta_x;
|
||||
vertices[usage++] = y;
|
||||
vertices[usage++] = (c-char_offset+1)*char_width;
|
||||
vertices[usage++] = 0;
|
||||
|
||||
vertices[usage++] = x+delta_x;
|
||||
vertices[usage++] = y+delta_y;
|
||||
vertices[usage++] = (c-char_offset+1)*char_width;
|
||||
vertices[usage++] = char_height;
|
||||
|
||||
vertices[usage++] = x;
|
||||
vertices[usage++] = y;
|
||||
vertices[usage++] = (c-char_offset)*char_width;
|
||||
vertices[usage++] = 0;
|
||||
|
||||
vertices[usage++] = x+delta_x;
|
||||
vertices[usage++] = y+delta_y;
|
||||
vertices[usage++] = (c-char_offset+1)*char_width;
|
||||
vertices[usage++] = char_height;
|
||||
|
||||
vertices[usage++] = x;
|
||||
vertices[usage++] = y+delta_y;
|
||||
vertices[usage++] = (c-char_offset)*char_width;
|
||||
vertices[usage++] = char_height;
|
||||
|
||||
x += delta_x + border_x;
|
||||
}
|
||||
glUnmapBuffer(GL_ARRAY_BUFFER);
|
||||
|
||||
// no printable char, so also nothing to do
|
||||
if(!usage) return;
|
||||
|
||||
// ????
|
||||
if (t != text)
|
||||
{
|
||||
*t = 0;
|
||||
printString(temp, x, y, z);
|
||||
}
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_RECTANGLE, texture);
|
||||
glEnable(GL_TEXTURE_RECTANGLE);
|
||||
glDrawArrays(GL_TRIANGLES, 0, usage/4);
|
||||
}
|
||||
|
|
|
@ -23,20 +23,13 @@ public:
|
|||
RasterFont();
|
||||
~RasterFont(void);
|
||||
static int debug;
|
||||
|
||||
// some useful constants
|
||||
enum {char_width = 10};
|
||||
enum {char_height = 15};
|
||||
|
||||
// and the happy helper functions
|
||||
void printString(const char *s, double x, double y, double z=0.0);
|
||||
void printCenteredString(const char *s, double y, int screen_width, double z=0.0);
|
||||
|
||||
void printMultilineText(const char *text, double x, double y, double z, int bbWidth, int bbHeight);
|
||||
void printMultilineText(const char *text, double x, double y, double z, int bbWidth, int bbHeight, u32 color);
|
||||
private:
|
||||
int fontOffset;
|
||||
char *temp_buffer;
|
||||
enum {TEMP_BUFFER_SIZE = 64 * 1024};
|
||||
|
||||
u32 VBO;
|
||||
u32 VAO;
|
||||
u32 texture;
|
||||
};
|
||||
|
||||
#endif // _RASTERFONT_H_
|
||||
|
|
|
@ -722,14 +722,11 @@ void Renderer::RenderText(const char *text, int left, int top, u32 color)
|
|||
{
|
||||
const int nBackbufferWidth = (int)OpenGL_GetBackbufferWidth();
|
||||
const int nBackbufferHeight = (int)OpenGL_GetBackbufferHeight();
|
||||
|
||||
glColor4f(((color>>16) & 0xff)/255.0f, ((color>> 8) & 0xff)/255.0f,
|
||||
((color>> 0) & 0xff)/255.0f, ((color>>24) & 0xFF)/255.0f);
|
||||
|
||||
|
||||
s_pfont->printMultilineText(text,
|
||||
left * 2.0f / (float)nBackbufferWidth - 1,
|
||||
1 - top * 2.0f / (float)nBackbufferHeight,
|
||||
0, nBackbufferWidth, nBackbufferHeight);
|
||||
0, nBackbufferWidth, nBackbufferHeight, color);
|
||||
|
||||
GL_REPORT_ERRORD();
|
||||
}
|
||||
|
|
|
@ -101,12 +101,11 @@ void SWRenderer::RenderText(const char* pstr, int left, int top, u32 color)
|
|||
{
|
||||
int nBackbufferWidth = (int)OpenGL_GetBackbufferWidth();
|
||||
int nBackbufferHeight = (int)OpenGL_GetBackbufferHeight();
|
||||
glColor4f(((color>>16) & 0xff)/255.0f, ((color>> 8) & 0xff)/255.0f,
|
||||
((color>> 0) & 0xff)/255.0f, ((color>>24) & 0xFF)/255.0f);
|
||||
|
||||
s_pfont->printMultilineText(pstr,
|
||||
left * 2.0f / (float)nBackbufferWidth - 1,
|
||||
1 - top * 2.0f / (float)nBackbufferHeight,
|
||||
0, nBackbufferWidth, nBackbufferHeight);
|
||||
0, nBackbufferWidth, nBackbufferHeight, color);
|
||||
}
|
||||
|
||||
void SWRenderer::DrawDebugText()
|
||||
|
|
Loading…
Reference in New Issue