2012-09-10 18:10:59 +00:00
# include "platform.h"
2013-05-13 19:53:28 +00:00
# include "Renderer.h"
2012-09-10 18:10:59 +00:00
# include GLHEADER
2012-08-29 18:53:53 +00:00
# include <iostream>
# include "Font.h"
2012-09-04 16:45:16 +00:00
# include <boost/filesystem.hpp>
2013-01-04 23:31:51 +00:00
# include "Log.h"
2012-08-29 18:53:53 +00:00
namespace Renderer {
bool loadedFonts = false ;
2012-10-17 18:21:56 +00:00
void setColor4bArray ( GLubyte * array , unsigned int color )
2012-08-29 18:53:53 +00:00
{
2012-10-17 18:21:56 +00:00
array [ 0 ] = ( color & 0xff000000 ) / 0x1000000 ;
array [ 1 ] = ( color & 0x00ff0000 ) / 0x10000 ;
array [ 2 ] = ( color & 0x0000ff00 ) / 0x100 ;
array [ 3 ] = ( color & 0x000000ff ) ;
2012-08-29 18:53:53 +00:00
}
2012-10-17 18:21:56 +00:00
void buildGLColorArray ( GLubyte * ptr , unsigned int color , unsigned int vertCount )
2012-08-29 18:53:53 +00:00
{
for ( unsigned int i = 0 ; i < vertCount ; i + + )
{
2012-10-17 18:21:56 +00:00
setColor4bArray ( ptr , color ) ;
2012-08-29 18:53:53 +00:00
ptr + = 4 ;
}
}
2013-06-02 19:34:50 +00:00
void translatef ( float x , float y )
{
glTranslatef ( x , y , 0 ) ;
}
void translate ( Vector2i offset )
{
translatef ( ( float ) offset . x , ( float ) offset . y ) ;
}
2012-10-17 18:21:56 +00:00
void drawRect ( int x , int y , int w , int h , unsigned int color )
2012-08-29 18:53:53 +00:00
{
2013-05-15 07:50:59 +00:00
# ifdef USE_OPENGL_ES
GLshort points [ 12 ] ;
# else
2013-05-13 19:53:28 +00:00
GLint points [ 12 ] ;
2013-05-15 07:50:59 +00:00
# endif
2012-08-29 18:53:53 +00:00
points [ 0 ] = x ; points [ 1 ] = y ;
points [ 2 ] = x ; points [ 3 ] = y + h ;
points [ 4 ] = x + w ; points [ 5 ] = y ;
points [ 6 ] = x + w ; points [ 7 ] = y ;
points [ 8 ] = x ; points [ 9 ] = y + h ;
points [ 10 ] = x + w ; points [ 11 ] = y + h ;
GLubyte colors [ 6 * 4 ] ;
2012-10-17 18:21:56 +00:00
buildGLColorArray ( colors , color , 6 ) ;
2012-08-29 18:53:53 +00:00
2012-10-17 18:32:01 +00:00
glEnable ( GL_BLEND ) ;
glBlendFunc ( GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA ) ;
2012-08-29 18:53:53 +00:00
glEnableClientState ( GL_VERTEX_ARRAY ) ;
glEnableClientState ( GL_COLOR_ARRAY ) ;
2012-10-17 18:32:01 +00:00
2013-05-15 07:50:59 +00:00
# ifdef USE_OPENGL_ES
glVertexPointer ( 2 , GL_SHORT , 0 , points ) ;
# else
2013-05-13 19:53:28 +00:00
glVertexPointer ( 2 , GL_INT , 0 , points ) ;
2013-05-15 07:50:59 +00:00
# endif
2012-08-29 18:53:53 +00:00
glColorPointer ( 4 , GL_UNSIGNED_BYTE , 0 , colors ) ;
glDrawArrays ( GL_TRIANGLES , 0 , 6 ) ;
2012-10-17 18:32:01 +00:00
glDisableClientState ( GL_BLEND ) ;
2012-08-29 18:53:53 +00:00
glDisableClientState ( GL_VERTEX_ARRAY ) ;
2012-10-17 18:32:01 +00:00
glDisable ( GL_COLOR_ARRAY ) ;
2012-08-29 18:53:53 +00:00
}
2012-08-29 21:52:25 +00:00
Font * fonts [ 3 ] = { NULL , NULL , NULL } ;
2012-10-24 15:28:37 +00:00
2012-10-25 23:23:26 +00:00
/*void unloadFonts()
2012-10-24 15:28:37 +00:00
{
std : : cout < < " unloading fonts... " ;
for ( unsigned int i = 0 ; i < 3 ; i + + )
{
delete fonts [ i ] ;
fonts [ i ] = NULL ;
}
loadedFonts = false ;
std : : cout < < " done. \n " ;
2012-10-25 23:23:26 +00:00
} */
2012-10-24 15:28:37 +00:00
2012-10-25 23:23:26 +00:00
//creates the default fonts (which shouldn't ever be deleted)
2012-08-29 18:53:53 +00:00
void loadFonts ( )
{
2012-10-24 15:28:37 +00:00
if ( loadedFonts )
2012-10-25 23:23:26 +00:00
return ;
2012-10-24 15:28:37 +00:00
2012-11-12 15:21:35 +00:00
std : : string fontPath = Font : : getDefaultPath ( ) ;
2012-09-04 16:45:16 +00:00
//make sure our font exists
if ( ! boost : : filesystem : : exists ( fontPath ) )
{
2013-04-13 18:19:06 +00:00
LOG ( LogError ) < < " System font wasn't found! Try installing the DejaVu truetype font (pacman -S ttf-dejavu on Arch, sudo apt-get install ttf-dejavu on Debian) " ;
2012-11-12 15:21:35 +00:00
return ;
2012-09-04 16:45:16 +00:00
}
2013-05-13 19:53:28 +00:00
float fontSizes [ ] = { 0.035f , 0.045f , 0.1f } ;
2012-08-29 21:52:25 +00:00
for ( unsigned int i = 0 ; i < 3 ; i + + )
{
2012-10-24 15:28:37 +00:00
fonts [ i ] = new Font ( fontPath , ( unsigned int ) ( fontSizes [ i ] * getScreenHeight ( ) ) ) ;
2012-08-29 21:52:25 +00:00
}
2012-08-29 18:53:53 +00:00
loadedFonts = true ;
2012-09-16 19:18:11 +00:00
2013-01-04 23:31:51 +00:00
LOG ( LogInfo ) < < " Loaded fonts successfully. " ;
2012-08-29 18:53:53 +00:00
}
2012-10-24 15:28:37 +00:00
Font * getDefaultFont ( FontSize size )
2012-08-29 18:53:53 +00:00
{
2012-08-29 21:52:25 +00:00
if ( ! loadedFonts )
loadFonts ( ) ;
return fonts [ size ] ;
2012-08-29 18:53:53 +00:00
}
2012-10-24 15:28:37 +00:00
void drawText ( std : : string text , int x , int y , unsigned int color , Font * font )
2012-08-29 18:53:53 +00:00
{
2012-10-24 15:28:37 +00:00
font - > drawText ( text , x , y , color ) ;
2012-08-29 18:53:53 +00:00
}
2012-10-24 15:28:37 +00:00
void drawCenteredText ( std : : string text , int xOffset , int y , unsigned int color , Font * font )
2012-08-29 18:53:53 +00:00
{
int w , h ;
font - > sizeText ( text , & w , & h ) ;
2013-05-13 19:53:28 +00:00
int x = getScreenWidth ( ) - w ;
x = x / 2 ;
x + = xOffset / 2 ;
2012-08-29 18:53:53 +00:00
2012-10-24 15:28:37 +00:00
drawText ( text , x , y , color , font ) ;
2012-08-29 18:53:53 +00:00
}
//this could probably be optimized
//draws text and ensures it's never longer than xLen
2012-10-24 15:28:37 +00:00
void drawWrappedText ( std : : string text , int xStart , int yStart , int xLen , unsigned int color , Font * font )
2012-08-29 18:53:53 +00:00
{
int y = yStart ;
std : : string line , word , temp ;
int w , h ;
size_t space , newline ;
while ( text . length ( ) > 0 | | ! line . empty ( ) ) //while there's text or we still have text to render
{
space = text . find ( ' ' , 0 ) ;
if ( space = = std : : string : : npos )
space = text . length ( ) - 1 ;
word = text . substr ( 0 , space + 1 ) ;
//check if the next word contains a newline
newline = word . find ( ' \n ' , 0 ) ;
if ( newline ! = std : : string : : npos )
{
word = word . substr ( 0 , newline ) ;
text . erase ( 0 , newline + 1 ) ;
} else {
text . erase ( 0 , space + 1 ) ;
}
temp = line + word ;
font - > sizeText ( temp , & w , & h ) ;
//if we're on the last word and it'll fit on the line, just add it to the line
if ( ( w < = xLen & & text . length ( ) = = 0 ) | | newline ! = std : : string : : npos )
{
line = temp ;
word = " " ;
}
//if the next line will be too long or we're on the last of the text, render it
if ( w > xLen | | text . length ( ) = = 0 | | newline ! = std : : string : : npos )
{
//render line now
if ( w > 0 ) //make sure it's not blank
2012-10-24 15:28:37 +00:00
drawText ( line , xStart , y , color , font ) ;
2012-08-29 18:53:53 +00:00
//increment y by height and some extra padding for the next line
y + = h + 4 ;
//move the word we skipped to the next line
line = word ;
} else {
//there's still space, continue building the line
line = temp ;
}
}
}
} ;