#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???