Sign in to follow this  
Mizipzor

Nothing shows when I blit

Recommended Posts

This problem has stolen many of my hours today. This function takes a string. It renders it on the class's own SDL_Surface, which I then retrieve and blit to the screen. I store the bitmapchars in a std::map. The part of how I load and store the chars works. I know that since I, in this function, (for debugging purposes) blit directly to the screen. So Ive concluded that, for some reason, I cannot blit to/from FontSystem::TextSurface! Anyone have any ideas why? Font.cpp (only Drawstring function)
void FontSystem::DrawString(std::string message) 
{
	int CharsDrawn = 0;			// We want to know how many chars we have 

	SDL_Rect dest;

	cout << message.size() << endl; // debug

	TextSurface = SDL_CreateRGBSurface(SDL_HWSURFACE, CharSize*message.size(), CharSize, pf->BitsPerPixel, 
						pf->Rmask, pf->Gmask, pf->Bmask, pf->Amask);	// here why we need to know pixelformat

	for(unsigned int i = 0; i < message.size(); i++) 
	{
		dest.x = i * CharSize;
		dest.y = 0;
		SDL_BlitSurface( AsciiChar[ (int)message[i] ], NULL, TextSurface, &dest);	// dont know if this is blitted

		dest.y = dest.x;
		SDL_BlitSurface( TextSurface, NULL, SdlScreen, &dest); // nothing is shown on screen

		dest.y = dest.x;
		SDL_BlitSurface( AsciiChar[ (int)message[i] ], NULL, SdlScreen, &dest);	// this one works!
		
		// debug
		//cout << "Drawing char: " << message[i] << "\t" << (int)message[i] << " " << std::endl;
	}

	dest.x = 128;
	dest.y = 128;
	SDL_BlitSurface( TextSurface, NULL, SdlScreen, &dest);	// nothing shows on screen
}

Below is source Im quite sure isnt responsible for this error. Dont look if you dont really want to. ;) Font.cpp
bool FontSystem::InitFont(SDL_Surface* screen) 
{
	SDL_Surface* FontBmp;	// make a temp surface

	SdlScreen = screen;

	CharSize = CHAR_SIZE;

	//// which size if defined in the header?
	//if( CHAR_SIZE == 8 ) {
	//	CharSize = 8;
	//	FontBmp = SDL_LoadBMP( "Bmp\\Font_8.bmp" );	// the font bmp
	//}
	//else if( CHAR_SIZE == 12 ) {
	//	CharSize = 12;
	//	FontBmp = SDL_LoadBMP( "Bmp\\Font_12.bmp" );	// the font bmp
	//}
	//else if( CHAR_SIZE == 16 ) {
	//	CharSize = 16;
	//	FontBmp = SDL_LoadBMP( "Bmp\\Font_16.bmp" );	// the font bmp
	//}
	//else {
	//	std::stringstream str;
	//	str << "Font: ERROR: Bad CHARSIZE: " << CHAR_SIZE;
	//	Log::Write( str.str().c_str() );
	//	return false;
	//}

	FontBmp = SDL_LoadBMP( "font.bmp" );	// the font bmp

	if( FontBmp == NULL ) {	// we failed to load the font bmp, report to log and quit
		throw "Font: ERROR: Unable to load font bitmap";
		//return false;
	}

	//SDL_BlitSurface(FontBmp, NULL, SdlScreen, NULL);

	// if it was loaded we continue and store the data

	// sort up all the variables -----------

	int XChars = BMP_WIDTH / CharSize;	// how many chars are there on a single row/line, x/y
	int YChars = XChars;				// there is an equal amount of chars on x and y

	//std::cout << XChars << " " << YChars << " " << CharSize;

	SDL_Rect source, dest;		
	dest.x = 0;
	dest.y = 0;
	source.x = 0;				// from where on the font bmp?
	source.y = 0;
	source.w = CharSize;		// the tiles we want are always going to be of the same size
	source.h = CharSize;

	// we must know the pixelformat so we can create our surfaces in the right format ----

	// make so the bmp surface is of the same kind as the screen
	FontBmp = SDL_ConvertSurface(FontBmp, SdlScreen->format, SdlScreen->flags);
	pf = FontBmp->format;	// the pixelformat

	for(int y = 0; y < YChars; y++) 
	{
		for(int x = 0; x < XChars; x++) 
		{
			// update the source area
			source.x = x * CharSize;
			source.y = y * CharSize;

			AsciiChar[ (y*CharSize) + x] = SDL_CreateRGBSurface(SDL_HWSURFACE, CharSize, CharSize, pf->BitsPerPixel, 
						pf->Rmask, pf->Gmask, pf->Bmask, pf->Amask);	// here why we need to know pixelformat

			SDL_BlitSurface(FontBmp, &source, AsciiChar[ ( y*CharSize ) + x ], NULL );	// blit it into the map

			//SDL_BlitSurface(AsciiChar[ ( y*CharSize ) + x ], NULL, SdlScreen, &source);

			// set transperency
			SDL_SetColorKey( AsciiChar[ ( y*CharSize ) + x ], SDL_SRCCOLORKEY, SDL_MapRGB( AsciiChar[ ( y*CharSize ) + x ]->format, 255, 0, 255));
		}
	}

	// were done using FontBmp
	SDL_FreeSurface(FontBmp);

    // Success, report to log and exit
	//std::stringstream str;
	//str << "Font: Font bitmap loaded, AsciiChar size: " << (int)AsciiChar.size();
	//Log::Write(str.str().c_str());
	return true;
}

Font.h
#ifndef _FONT
#define _FONT

#include <string>
#include <map>
#include <vector>
#include <SDL.h>

//#include "Log.h"
//#include "SdlWrapper.h"

#define CHAR_SIZE	16					// the pixel size of on char, also = heigth
#define BMP_WIDTH	256					// the pixel size of the entire bmp file with the fonts, also = heigth
#define FILE_LOC_STRING	"font.bmp"		// where the image is

using namespace std;

class FontSystem {

//	// SINGLETON ---
//
//protected:
//	static FontSystem* m_pInstance;
//	FontSystem( void ) { };
//
//public:
//	static FontSystem* GetInstance( void ) {
//		if ( !m_pInstance )
//			m_pInstance = new FontSystem();
//		return m_pInstance;
//	}
//
//	static void DeleteInstance(void) {
//		delete m_pInstance;
//		m_pInstance = 0;
//	}
//
//	// --- ---


private:
	//SdlWrapper*	Sdl;						// so we can draw chars
	SDL_Surface* TextSurface;
	SDL_Surface* SdlScreen;
	int CharSize;							// the size of a char

	SDL_PixelFormat*    pf;

	std::map<int, SDL_Surface*>	AsciiChar;	// call it with the ascii value of the char you want and you get a surface with the char on it!

public:
	// Setup all the data
	bool	InitFont(SDL_Surface* screen);
	
	// We need to free up all the surfaces
			FontSystem( void ) { };
			~FontSystem();

	// This function draws a string at the specified coords
	void	DrawString(std::string message);

	// Return the size in pixels of the font, same on height and width
	int		GetCharSize()	{return CharSize;};
	SDL_Surface* GetTextSurface()	{return TextSurface;};
};

#endif

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this