1 ///Imagemapping does not comply with meatBox standards; If needed, be sure to resize width to ~1."; 2 module meatbox.textbox; 3 4 import meatbox.window; 5 import meatbox.colour; 6 import meatbox.box; 7 8 import derelict.sdl2.ttf; 9 import derelict.sdl2.sdl; 10 11 static this() 12 { 13 DerelictSDL2ttf.load(); 14 TTF_Init(); 15 } 16 static ~this() 17 { TTF_Quit(); } 18 19 alias Font =TTF_Font*; 20 alias openFont =TTF_OpenFont; 21 22 //This entire file is festering with evil. Beware. 23 class Textbox: Box 24 { 25 public: 26 TTF_Font* font; 27 string text() @property 28 { return this._text; } 29 this( string text ) 30 { this( text, TTF_OpenFont("lucon.ttf", 16)); } 31 this( string text, Font font ) 32 { 33 this.font =font; 34 setText( text ); 35 } 36 ~this() 37 { TTF_CloseFont( font ); } 38 override void render() 39 { 40 glColor3ub( colour.red, colour.green, colour.blue ); 41 glVertexPointer( 2, GL_FLOAT, 0, _verts.ptr ); 42 glBindTexture( GL_TEXTURE_2D, _texture ); 43 44 glPushMatrix(); 45 glTranslatef( x, y, 0); 46 glDrawArrays( GL_QUADS, 0, 4); 47 glPopMatrix(); 48 } 49 void setText( string text ) 50 { 51 if( text == "" ) 52 { text = " "; } 53 this._text =text; 54 import std.string: toStringz; 55 SDL_Surface* surf =TTF_RenderText_Blended( font, toStringz(text), SDL_Color(255, 255, 255) ); 56 //May be faster to render to 8bit pallet, and convert? Let's just make it nice I guess. 57 58 glGenTextures( 1, &_texture ); 59 glBindTexture( GL_TEXTURE_2D, _texture ); 60 61 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 62 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 63 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 64 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 65 glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surf.w, surf.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, cast(const(void)*)surf.pixels); 66 glBindTexture( GL_TEXTURE_2D, 0); 67 this.width =surf.w; this.height =surf.h; 68 this._verts[4] =width; 69 this._verts[6] =width; 70 this._verts[3] =height; 71 this._verts[5] =height; 72 73 SDL_FreeSurface( surf ); 74 } 75 static void startRender() 76 { 77 glEnableClientState( GL_VERTEX_ARRAY ); 78 glEnableClientState( GL_TEXTURE_COORD_ARRAY ); 79 glEnable( GL_TEXTURE_2D ); 80 glEnable( GL_BLEND ); 81 glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); 82 glColor4f( 1f, 1f, 1f, 1f ); 83 glTexCoordPointer( 2, GL_FLOAT, 0, Box.vertices.ptr ); 84 } 85 static void endRender() 86 { 87 glDisable( GL_BLEND); 88 glDisable( GL_TEXTURE_2D); 89 glDisableClientState( GL_TEXTURE_COORD_ARRAY); 90 glDisableClientState( GL_VERTEX_ARRAY); 91 } 92 private: 93 string _text; 94 uint _texture; 95 float[8] _verts = 96 [ 97 0f, 0f, 98 0f, 1f, 99 1f, 1f, 100 1f, 0f 101 ]; 102 }