Advertisement

SFont

Started by December 12, 2004 03:48 AM
4 comments, last by Joshnathan 19 years, 10 months ago
Hi all, I downloaded this example from linux games, but it doesn't work. this is the source:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <SDL/SDL.h>
#include <SDL/SFont.h>

#include <SDL/SDL_image.h>

SDL_Surface *screen;

void init_SDL()
{
	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
		fprintf(stderr,
			"Couldn't initialize SDL: %s\n", SDL_GetError());
		exit(1);
	}
	atexit(SDL_Quit);			/* Clean up on exit */

	/* Initialize the display */
	screen = SDL_SetVideoMode(640, 480, 0, 0);
	if ( screen == NULL ) {
		fprintf(stderr, "Couldn't set %dx%dx%d video mode: %s\n",
					640, 480, 16, SDL_GetError());
		exit(1);
	}
	
	/* Set the window manager title bar */
	SDL_WM_SetCaption("SFont Test", "SFont");
}

int main(int argc, char *argv[])
{
	SFont_Font* Font;

	init_SDL();

	// Load and Prepare the font - You don't have to use the IMGlib for this
	Font = SFont_InitFont(IMG_Load("24P_Copperplate_Blue.png"));
	if(!Font) {
		fprintf(stderr, "An error occured while loading the font.");
		exit(1);
	}
	
	// a simple text blit to (0/0)
	SFont_Write (screen, Font, 0,0,"Welcome to SFontTest1!");

	// Update the screen
	SDL_UpdateRect(screen, 0, 0, 0, 0);
	// Let the user time to look at the font
	SDL_Delay(4000);

	// Don't forget to free our font
	SFont_FreeFont(Font);
	
	// Bye
	exit(0);
}

and I get these errors: [Linker error] undefined reference to `SFont_InitFont' [Linker error] undefined reference to `SFont_Write' [Linker error] undefined reference to `SFont_FreeFont' Hope you can help me out, Joshua
-----------------------------Sismondi GamesStarted c++ in October 2004...
Did you remember to add SFont.c to your project?
Advertisement
nope, and thanks :D
-----------------------------Sismondi GamesStarted c++ in October 2004...
One more question. I now have this function:
void MainMenu(){    bool running = true;    int highlight = 1;        while(running)    {        SDL_Event event;                while(SDL_PollEvent(&event))        {            switch(event.type)            {                case SDL_KEYDOWN:                    switch(event.key.keysym.sym)                    {                        case SDLK_UP:                            if(highlight > 1)                            highlight--;                            break;                        case SDLK_DOWN:                            if(highlight < 3)                            highlight++;                            break;                        case SDLK_ESCAPE:                            running = false;                            break;                    }                        break;                case SDL_QUIT:                    running = false;                    break;            }        }        DrawMenu(highlight);    }} void DrawMenu(int highlight){ SFont_Font* font; font = SFont_InitFont(IMG_Load("24P_Copperplate_Blue.png")); if(!font) cout<< "Error Initiating Font\n";  if(highlight == 1) font = SFont_InitFont(IMG_Load("24P_Arial_NeonYellow.png"));      SFont_Write(screen, font, screen->w/2-SFont_TextWidth(font, "New Game")/2, 100, "New Game"); font = SFont_InitFont(IMG_Load("24P_Copperplate_Blue.png"));   if(highlight == 2) font = SFont_InitFont(IMG_Load("24P_Arial_NeonYellow.png"));      SFont_Write(screen, font, screen->w/2-SFont_TextWidth(font, "Change Screen Mode")/2, 150, "Change Screen Mode"); font = SFont_InitFont(IMG_Load("24P_Copperplate_Blue.png"));  if(highlight == 3) font = SFont_InitFont(IMG_Load("24P_Arial_NeonYellow.png"));      SFont_Write(screen, font, screen->w/2-SFont_TextWidth(font, "View Highscores")/2, 200, "View Highscores");  SDL_Flip(screen); SFont_FreeFont(font);}        


but everytime I push "up" or "down", I want the old "highlighted" font to be destroyed. How do I do that?. atm, all this function does is when I hit "down", he draws on top of the other fonts. How can I change that?
-----------------------------Sismondi GamesStarted c++ in October 2004...
As far as I can tell, you never clear the screen. One way to do that is to use SDL_FillRect to draw an opaque rectangle on the surface before doing anything else. ex:

SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); // black
That works, thanks again

Joshua
-----------------------------Sismondi GamesStarted c++ in October 2004...

This topic is closed to new replies.

Advertisement