Commits

stqn committed ffae82d

Replaced TextureManager.getText() with the TextSurface module.

  • Participants
  • Parent commits 69773c7

Comments (0)

Files changed (6)

File RWKC_linux.cbp

 		<Unit filename="src/objectlist.d" />
 		<Unit filename="src/screen.d" />
 		<Unit filename="src/soundmanager.d" />
+		<Unit filename="src/textsurface.d" />
 		<Unit filename="src/texturemanager.d" />
 		<Unit filename="src/time.d" />
 		<Unit filename="src/view.d" />

File src/chrono.d

 
 import screen;
 import texturemanager;
+import textsurface;
 import time;
 
 public static Chrono gChrono;
 	float mSeconds = 0.f;
 	//float mPreviousSeconds;
 	bool mFrozen = false;
+	TextSurface mText;
+
 
 	this() {
 		gChrono = this;
+		mText = new TextSurface( "" );
 	}
 
 
 			text = format( "%d:%02d:%02d", h, m, s );
 		}
 
-		SDL_Surface* surf = gTextureManager.getText( text );
-
-		SDL_Rect dest;
-		dest.x = 16;
-		dest.y = 16;
-		dest.w = cast(ushort) surf.w;
-		dest.h = cast(ushort) surf.h;
-
-		SDL_BlitSurface( surf, null, gScreen.mSurface, &dest );
+		mText.setText( text );
+		mText.draw( 16, 16 );
 	}
 
 }
-
 
 module menu;
 
 import derelict.sdl.sdl;
+
+import gameconstants;
+import gamepart;
+import inputmanager;
 import map;
 import screen;
+import textsurface;
 import texturemanager;
-import gamepart;
-import inputmanager;
-import gameconstants;
 
 // ============ base Menu class ===============
-public static Menu gMenu;
 
 class Menu : public GamePart {
 
-	char[][] mText;
+	TextSurface[2][] mText;
+	TextSurface mArrow;
 	int mCurrentItem = 0;
 	GAMEPART_STATE mState = GAMEPART_STATE.GAME;
-
+
+	this() {
+		mArrow = new TextSurface( "=>" );
+	}
+
     void update() {
         overlay(false);
         draw();
     }
 
 	void overlay( bool fade ) {
-
 		SDL_Surface* surf = SDL_CreateRGBSurface(
 			SDL_HWSURFACE,
 			gScreen.WIDTH, gScreen.HEIGHT, 32,
 			0, 0, 0, 0 );
 
 		if( fade ) {
-
 			ubyte alpha;
 
 			while( alpha++ < 12 ) {
 		}
 
 		SDL_FreeSurface( surf );
-
 	}
 
 
 	void draw() {
-
 		int x,y;
 
 		x = (gScreen.WIDTH - 300) / 2;
 		y = 100;
 
-		foreach( i, s; mText ) {
-
+		foreach( i, t; mText ) {
 			y += 32;
-			if( s != "" ) {
-				if( std.string.ifind( s, '|' ) != -1 ) {
-					char[][] items = std.string.split( s, "|" );
-					drawItem( items[0], x, y, i==mCurrentItem );
-					drawItem( items[1], x+150, y, i==mCurrentItem );
-				}
-				else
-					drawItem( s, x, y, i==mCurrentItem );
+			t[0].draw( x, y );
+			if( t[1] !is null ) {
+				t[1].draw( x+150, y );
+			}
+			if( i == mCurrentItem ) {
+				mArrow.draw( x-32, y );
 			}
 		}
-
 	}
 
 
-	void drawItem( char[] s, int x, int y, bool highlight ) {
-		SDL_Surface* surf;
-
-		if( highlight ) {
-			x -= 32;
-			s = "=> " ~ s;
-		}
-		SDL_Rect rect = { cast(short)x, cast(short)y, gScreen.WIDTH, gScreen.HEIGHT };
-
-		surf = gTextureManager.getText( s );
-		SDL_BlitSurface( surf, null, gScreen.mSurface, &rect );
-	}
-
     void action() {
     }
 
 		mCurrentItem = (mCurrentItem+mText.length+direction) % mText.length;
 	}
 
-    void addItem( string s ) {
-        mText ~= s;
+    void addItem( string s ) {
+    	TextSurface[2] t;
+		if( std.string.ifind( s, '|' ) != -1 ) {
+			char[][] items = std.string.split( s, "|" );
+			t[0] = new TextSurface( items[0] );
+			t[1] = new TextSurface( items[1] );
+		}
+		else {
+			t[0] = new TextSurface( s );
+		}
+        mText ~= t;
     }
 
     GAMEPART_STATE getState() {

File src/messages.d

 import std.stdio;
 
 import screen;
+import textsurface;
 import texturemanager;
 import time;
 
 class Messages {
 
 	struct Mess {
-		SDL_Surface* surf;
+		TextSurface text;
 		float timer;
 		SDL_Rect pos;
 		float origY;
 	static Mess*[] mMessages;
 
 
-	this() {
-	}
-
-
 	static void add( char[] text ) {
 		Mess *m = new Mess;
-		m.surf = gTextureManager.getText( text );
+		m.text = new TextSurface( text );
+		SDL_Surface* surf = m.text.getSurface();
 		m.timer = 0.f;
-		m.pos.x = cast(short) ((gScreen.mSurface.w - m.surf.w) / 2);
-		m.pos.y = cast(short) ((gScreen.mSurface.h - m.surf.h) / 2) - 32;
-		m.pos.w = cast(ushort) m.surf.w;
-		m.pos.h = cast(ushort) m.surf.h;
+		m.pos.x = cast(short) ((gScreen.mSurface.w - surf.w) / 2);
+		m.pos.y = cast(short) ((gScreen.mSurface.h - surf.h) / 2) - 32;
+		m.pos.w = cast(ushort) surf.w;
+		m.pos.h = cast(ushort) surf.h;
 		m.origY = m.pos.y;
 		mMessages ~= m;
 		writefln( "add message text=%s", text );
 
 	static void draw() {
 		foreach( m ; mMessages ) {
-			SDL_BlitSurface( m.surf, null, gScreen.mSurface, &m.pos );
+			m.text.draw( m.pos.x, m.pos.y );
 		}
 	}
 

File src/textsurface.d

+/**
+ * Class for creating and drawing text surfaces.
+ */
+
+module textsurface;
+
+import derelict.sdl.sdl;
+import derelict.sdl.ttf;
+import std.stdio;
+import std.string;
+
+import screen;
+
+
+class TextSurface {
+
+	SDL_Surface* mSurface;
+	char[] mText;
+
+	static TTF_Font* mFont;
+
+
+	this( char[] text = "" ) {
+		if( !mFont ) {
+			DerelictSDLttf.load();
+			if( TTF_Init() ) {
+				writefln( "Can't init SDL_ttf." );
+				assert(0);
+			}
+			mFont = TTF_OpenFont( toStringz("data/Comfortaa Bold.ttf"), 24 );
+			if( !mFont ) {
+				writefln( "openfont error: %s", std.string.toString(TTF_GetError()) );
+				assert(0);
+			}
+		}
+
+		setText( text );
+	}
+
+
+	~this() {
+		if( mSurface ) {
+			SDL_FreeSurface( mSurface );
+		}
+	}
+
+
+	SDL_Surface* getSurface() {
+		return mSurface;
+	}
+
+
+	void setText( char[] text ) {
+		if( text == "" ) {
+			// TTF_Render...() fails for ""
+			text = " ";
+		}
+		if( /*mSurface &&*/ text == mText ) {
+			return;
+		}
+		mText = text;
+
+		if( mSurface ) {
+			SDL_FreeSurface( mSurface );
+		}
+
+		SDL_Color fg = {255, 255, 255};
+		mSurface = TTF_RenderUTF8_Blended( mFont, toStringz(text), fg );
+		assert( mSurface != null );
+	}
+
+
+	void draw( int x, int y ) {
+		SDL_Rect rect = {
+			cast(short) x,
+			cast(short) y,
+			cast(ushort) mSurface.w,
+			cast(ushort) mSurface.h
+		};
+		SDL_BlitSurface( mSurface, null, gScreen.mSurface, &rect );
+	}
+
+}

File src/texturemanager.d

 
 import derelict.sdl.sdl;
 import derelict.sdl.image;
-import derelict.sdl.ttf;
-import std.stdio;
 import std.string;
 
 import time;
 
 class TextureManager {
 
-	struct TextTexture {
-		float time;
-		SDL_Surface* surf;
-	};
-
 	SDL_Surface*[ char[] ] mTextureList;
-	TextTexture*[ char[] ] mTextTextureList;
-	TTF_Font* mFont;
 
 
 	this() {
-
 		DerelictSDLImage.load();
-		DerelictSDLttf.load();
-
-		if( TTF_Init() ) {
-			writefln( "Can't init SDL_ttf." );
-			assert(0);
-		}
-
-		mFont = TTF_OpenFont( toStringz("data/Comfortaa Bold.ttf"), 24 );
-		if( !mFont ) {
-			writefln( "openfont error: %s", std.string.toString(TTF_GetError()) );
-		}
-		assert( mFont );
-
 		gTextureManager = this;
-
 	}
 
 
 	SDL_Surface* get( char[] filename ) {
-
 		SDL_Surface** p;
 
 		p = filename in mTextureList;
 			return( *p );
 		}
 		else {
-
-			SDL_Surface* s = IMG_Load( toStringz(  "data/" ~ filename  ) );
-
+			SDL_Surface* s = IMG_Load( toStringz( "data/" ~ filename ) );
 			assert( s != null );
-
 			mTextureList[ filename ] = s;
-
-			return( s);
-
-		}
-
-	}
-
-
-	SDL_Surface* getText( char[] text ) {
-
-		TextTexture** p;
-		p = text in mTextTextureList;
-
-		if( p != null ) {
-			return (*p).surf;
-		}
-		else {
-			// First purge old textures
-			float time = gTime.getTime();
-			foreach( tt, t ; mTextTextureList ) {
-				if( t.time < time - 2.f ) {
-					mTextTextureList.remove(tt);
-					// Removing an element fucks up the foreach so we have to exit the loop
-					break;
-				}
-			}
-
-			// Create new texture
-			SDL_Color fg = {255, 255, 255};
-			SDL_Surface* s = TTF_RenderUTF8_Blended( mFont, toStringz(text), fg );
-			assert( s != null );
-
-			mTextTextureList[text] = new TextTexture;
-			mTextTextureList[text].surf = s;
-			mTextTextureList[text].time = time;
-
 			return s;
 		}
-
 	}
 
 }