SDL_ttf problem help me

#include <sdl.h>
#include <sdl_ttf.h>

#define IMAGE_FILE_NAME “Image.bmp”
#define FONT_FILE_NAME “2002l.ttf”

SDL_Surface* LoadImage()
{
FILE* fp;
fopen_s(&fp, IMAGE_FILE_NAME, “rb”);

fseek(fp, 0L, SEEK_END);
size_t filesize = ftell(fp);

fseek(fp, 0L, SEEK_SET);
unsigned char* filedata = new unsigned char[filesize];
fread(filedata, sizeof(unsigned char), filesize, fp);

fclose(fp);

SDL_RWops* rw = SDL_RWFromConstMem(filedata, (int)filesize);
SDL_Surface* img = SDL_LoadBMP_RW(rw, 1);

delete [] filedata;

return img;

}

TTF_Font* LoadFont()
{
FILE* fp;
fopen_s(&fp, FONT_FILE_NAME, “rb”);

fseek(fp, 0L, SEEK_END);
size_t filesize = ftell(fp);

fseek(fp, 0L, SEEK_SET);
unsigned char* filedata = new unsigned char[filesize];
fread(filedata, sizeof(unsigned char), filesize, fp);

fclose(fp);

SDL_RWops* rw = SDL_RWFromConstMem(filedata, (int)filesize);
TTF_Font* font = TTF_OpenFontRW(rw, 1, 40);

delete [] filedata;

return font;

}

int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);

SDL_Surface* video = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE | 

SDL_HWPALETTE | SDL_DOUBLEBUF);
TTF_Init();

bool bQuit = false;

SDL_Surface* Image = LoadImage();
SDL_Rect ImageRect = { 0, 0, Image->w, Image->h };

TTF_Font* Font = LoadFont();
SDL_Color FontColor = { 255, 255, 255 };
SDL_Surface* Text = TTF_RenderText_Blended(Font, "TTF Test", 

FontColor);
SDL_Rect TextRect = { 0, 0, Text->w, Text->h };

SDL_Event sdl_event;
while(true)
{
	SDL_FillRect(video, 0, 0);

	while(SDL_PollEvent(&sdl_event))
	{
		switch(sdl_event.type)
		{
		case SDL_QUIT:
			bQuit = true;
			break;
		}
	}

	SDL_BlitSurface(Image, 0, video, &ImageRect);
	SDL_BlitSurface(Text, 0, video, &TextRect);

	SDL_Flip(video);

	if(bQuit) break;
}

TTF_CloseFont(Font);
SDL_FreeSurface(Text);
SDL_FreeSurface(Image);

TTF_Quit();
SDL_Quit();

return 0;

}--------------------------------------

I have a problem with SDL_ttf
There are two functions - LoadImage() and LoadFont()
Those functions are almost same except few lines
but LoadImage() works very well and LoadFont() doesn’t work
LoadFont() makes access violation in runtime
How do I make LoadFont() work very well???
Please help me

p.s.) why does this group ignore my private mail address???
but gmail is accepted.
Why???

[…]

I have a problem with SDL_ttf
There are two functions - LoadImage() and LoadFont()
Those functions are almost same except few lines
but LoadImage() works very well and LoadFont() doesn’t work
LoadFont() makes access violation in runtime

Where exactly do you get the access violation?

[…]

p.s.) why does this group ignore my private mail address???
but gmail is accepted.
Why???

The list will only accept messages from subscribed addresses, in order
to make it a little harder to post spam.

//David Olofson - Programmer, Composer, Open Source Advocate

.------- http://olofson.net - Games, SDL examples -------.
| http://zeespace.net - 2.5D rendering engine |
| http://audiality.org - Music/audio engine |
| http://eel.olofson.net - Real time scripting |
’-- http://www.reologica.se - Rheology instrumentation --'On Tuesday 09 January 2007 19:47, lycanthrope wrote:

David Olofson <david olofson.net> writes:

[…]

I have a problem with SDL_ttf
There are two functions - LoadImage() and LoadFont()
Those functions are almost same except few lines
but LoadImage() works very well and LoadFont() doesn’t work
LoadFont() makes access violation in runtime

Where exactly do you get the access violation?

TTF_Font* Font = LoadFont();
SDL_Color FontColor = { 255, 255, 255 };
SDL_Surface* Text = TTF_RenderText_Blended(Font, “TTF Test”,FontColor); <- here

LoadFont() returns vaild pointer, not NULL
I don’t know why access violation occur> On Tuesday 09 January 2007 19:47, lycanthrope wrote:

LoadFont() returns vaild pointer, not NULL
I don’t know why access violation occur

Don’t delete[] filedata in LoadFont(). The “freesrc” argument to
TTF_OpenFontRW() means “close the rwops during TTF_CloseFont()”, but it
needs the rwops to remain for the duration of the TTF_Font’s lifetime;
deleting the buffer frees memory that SDL_ttf expects to touch later on.

Either use a file for the rwops, which will be closed during
TTF_CloseFont(), or don’t delete that buffer until after TTF_CloseFont()
returns.

–ryan.

Ryan C. Gordon <icculus icculus.org> writes:

Don’t delete[] filedata in LoadFont(). The “freesrc” argument to
TTF_OpenFontRW() means “close the rwops during TTF_CloseFont()”, but it
needs the rwops to remain for the duration of the TTF_Font’s lifetime;
deleting the buffer frees memory that SDL_ttf expects to touch later on.

Either use a file for the rwops, which will be closed during
TTF_CloseFont(), or don’t delete that buffer until after TTF_CloseFont()
returns.

thank you.
but Why does LoadImage() make any problem?
LoadImage() also deletes buffer

but Why does LoadImage() make any problem?
LoadImage() also deletes buffer

SDL_LoadBMP_RW() closes the rwops before returning (it’s done with it,
and made a copy of the relevant data in the SDL_Surface* it returned),
so it’s safe to free the copy you passed in…thus, LoadImage() doesn’t
crash.

TTF_OpenFontRW() needs the original font data for the life of the
TTF_Font* it returns, because there may be thousands of glyphs it never
needs to render on which it doesn’t want to waste CPU time and RAM…not
to mention it will need the original data again if it needs to render
the same glyph at a different point size.

–ryan.