Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

Whats wrong with my font loader?

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Ive got a bmp with all chars in the ascii chart (255 of them), I have a function that loads them and stores them in a std::map <int, SDL_Surface*>. So you can acess the map with the ascii value of the char and get a sdl surface with that char on. But somehow they arent loaded. In the first loop I take that little 16*16 square of the bitmap and store it in the map, then print them in the same (ascii) order. But nothing shows. Then I print the original loaded fontbmp next to it (to compare) and it shows up. Whats wrong?
#define CHAR_WIDTH	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

bool FontSystem::InitFont() {
	SDL_Surface* FontBmp = SDL_LoadBMP( "Bmp\\Font.bmp" );	// the font bmp

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

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

	int XChars = BMP_WIDTH / CHAR_WIDTH;	// 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

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

	for(int y = 0; y < YChars; y++) {
		for(int x = 0; x < XChars; x++) {

			// update the source area
			source.x = x * CHAR_WIDTH;
			source.y = y * CHAR_WIDTH;

			SDL_BlitSurface(FontBmp,									// source surface, the font bitmap 
							&source,									
							AsciiChar[ ( y*16 ) + x ],					// destination surface, the corresponding element in the AsciiChar map
							NULL );									// we want to fill the target surface
		}
	}
		
	for(y = 0; y < YChars; y++) {			// draw everything so we can se its ok
		for(int x = 0; x < XChars; x++) {
			Sdl->DrawImg( AsciiChar[ ( y*16 ) + x ], x * CHAR_WIDTH, y * CHAR_WIDTH );
		}
	}

	// and the same bmp lined up next to it, if everythings ok they should look identical
	Sdl->DrawImg( FontBmp, 256, 0);

    // 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;
}



Share this post


Link to post
Share on other sites
Advertisement
It looks like you're trying to blit to a surface that hasn't been created yet. Use SDL_CreateRGBSurface to create the new surface first, then blit to it.

Edit:
To clarify what I mean:
	
SDL_PixelFormat* pf = FontBmp->format;

for(int y = 0; y < YChars; y++) {
for(int x = 0; x < XChars; x++) {

// update the source area
source.x = x * CHAR_WIDTH;
source.y = y * CHAR_WIDTH;

AsciiChar[ (y*16) + x] = SDL_CreateRGBSurface(SDL_HWSURFACE, CHAR_WIDTH, CHAR_WIDTH, pf->BitsPerPixel,
pf->Rmask, pf->Gmask, pf->Bmask, pf->Amask);

SDL_BlitSurface(FontBmp, &source, AsciiChar[ ( y*16 ) + x ], NULL );
}
}



You need to call SDL_FreeSurface on FontBmp when you're done blitting from it. Don't forget to free the surfaces you create in AsciiChar in the class destructor either.

[Edited by - MrP on July 8, 2005 1:15:26 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!