Take back SDL_ttf again so fonts finally work

This commit is contained in:
simon.kagstrom 2009-01-02 15:07:43 +00:00
parent e0e0d8ffc0
commit 2a1f0637d6
6 changed files with 67 additions and 24 deletions

View File

@ -33,7 +33,7 @@ LDFLAGS = -L$(DEVKITPRO)/SDL/lib -g $(MACHDEP) -Wl,-Map,$(notdir $@).map
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project # any extra libraries we wish to link with the project
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
LIBS := -lSDL_image -ljpeg -lpng -lz -lSDL -lfat -lwiiuse -lbte -logc -lm LIBS := -lSDL_image -lSDL_ttf -ljpeg -lpng -lz -lSDL -lfreetype -lfat -lwiiuse -lbte -logc -lm
#--------------------------------------------------------------------------------- #---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing # list of directories containing libraries, this must be the top level containing

View File

@ -155,7 +155,7 @@ public:
#endif #endif
#ifdef HAVE_SDL #ifdef HAVE_SDL
menu_t main_menu; menu_t main_menu;
Font *menu_font; TTF_Font *menu_font;
const char *base_dir; const char *base_dir;
bool fake_key_sequence; bool fake_key_sequence;

View File

@ -68,7 +68,22 @@ void C64::c64_ctor1(void)
this->fake_key_keytime = 5; this->fake_key_keytime = 5;
this->fake_key_type = 0; this->fake_key_type = 0;
this->menu_font = new Font("/apps/frodo/fonts.bmp"); SDL_RWops *rw;
Uint8 *data = (Uint8*)malloc(1 * 1024*1024);
FILE *fp = fopen("/apps/frodo/FreeMono.ttf", "r");
if (!fp) {
fprintf(stderr, "Could not open font\n");
exit(1);
}
fread(data, 1, 1 * 1024 * 1024, fp);
rw = SDL_RWFromMem(data, 1 * 1024 * 1024);
if (!rw) {
fprintf(stderr, "Could not create RW: %s\n", SDL_GetError());
exit(1);
}
this->menu_font = TTF_OpenFontRW(rw, 1, 20);
if (!this->menu_font) if (!this->menu_font)
{ {
fprintf(stderr, "Unable to open font\n" ); fprintf(stderr, "Unable to open font\n" );
@ -372,7 +387,7 @@ uint8 C64::poll_joystick(int port)
j &= 0xef; // Button j &= 0xef; // Button
if (held & WPAD_BUTTON_HOME) if (held & WPAD_BUTTON_HOME)
this->enter_menu(); this->enter_menu();
if (held & WPAD_BUTTON_A) if (held & WPAD_BUTTON_B)
exit(1); exit(1);
if ( (held & WPAD_BUTTON_A) && ThePrefs.JoystickKeyBinding[0]) if ( (held & WPAD_BUTTON_A) && ThePrefs.JoystickKeyBinding[0])

View File

@ -40,6 +40,12 @@ extern "C" int main(int argc, char **argv)
fprintf(stderr, "Couldn't initialize SDL (%s)\n", SDL_GetError()); fprintf(stderr, "Couldn't initialize SDL (%s)\n", SDL_GetError());
return 0; return 0;
} }
if (TTF_Init() < 0)
{
fprintf(stderr, "Unable to init TTF: %s\n", TTF_GetError() );
return 0;
}
if (WPAD_Init() != WPAD_ERR_NONE) if (WPAD_Init() != WPAD_ERR_NONE)
{ {
fprintf(stderr, "Failed initializing controllers\n"); fprintf(stderr, "Failed initializing controllers\n");

View File

@ -12,8 +12,6 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "bitmap-font.h"
#if defined(GEKKO) #if defined(GEKKO)
# include <wiiuse/wpad.h> # include <wiiuse/wpad.h>
#endif #endif
@ -44,11 +42,14 @@ static submenu_t *find_submenu(menu_t *p_menu, int index)
return NULL; return NULL;
} }
static void print_font(SDL_Surface *screen, Font *font, int type, static void print_font(SDL_Surface *screen, TTF_Font *font, int r, int g, int b,
int x, int y, char *msg) int x, int y, char *msg)
{ {
SDL_Surface *font_surf;
SDL_Rect dst = {x, y, 0, 0};
SDL_Color color = {r, g, b};
char buf[255]; char buf[255];
unsigned int i; int i;
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
strncpy(buf, msg, 254); strncpy(buf, msg, 254);
@ -60,7 +61,17 @@ static void print_font(SDL_Surface *screen, Font *font, int type,
buf[i] = ' '; buf[i] = ' ';
} }
font->ShowText(buf, type, x, y, screen); font_surf = TTF_RenderText_Solid(font, buf,
color);
if (!font_surf)
{
fprintf(stderr, "%s\n", TTF_GetError());
exit(1);
}
SDL_BlitSurface(font_surf, NULL, screen, &dst);
SDL_FreeSurface(font_surf);
} }
@ -68,7 +79,7 @@ static void menu_draw(SDL_Surface *screen, menu_t *p_menu)
{ {
int x_start = p_menu->x1 + (p_menu->x2 - p_menu->x1) / 2 - p_menu->text_w / 2; int x_start = p_menu->x1 + (p_menu->x2 - p_menu->x1) / 2 - p_menu->text_w / 2;
int y_start = p_menu->y1 + (p_menu->y2 - p_menu->y1) / 2 - p_menu->text_h / 2; int y_start = p_menu->y1 + (p_menu->y2 - p_menu->y1) / 2 - p_menu->text_h / 2;
int font_height = p_menu->p_font->GetHeight(); int font_height = TTF_FontHeight(p_menu->p_font);
int line_height = (font_height + font_height / 4); int line_height = (font_height + font_height / 4);
int cur_y = p_menu->cur_sel * line_height; int cur_y = p_menu->cur_sel * line_height;
int entries_visible = p_menu->y2 / line_height; int entries_visible = p_menu->y2 / line_height;
@ -87,13 +98,13 @@ static void menu_draw(SDL_Surface *screen, menu_t *p_menu)
int y = (i - p_menu->start_entry_visible) * line_height; int y = (i - p_menu->start_entry_visible) * line_height;
if ((p_menu->available_options & (1<<i)) == 0) /* Gray (not available) */ if ((p_menu->available_options & (1<<i)) == 0) /* Gray (not available) */
print_font(screen, p_menu->p_font, 2, x_start, print_font(screen, p_menu->p_font, 128,128,128, x_start,
y_start + y, msg); y_start + y, msg);
else if (p_menu->cur_sel == i) /* Selected - color */ else if (p_menu->cur_sel == i) /* Selected - color */
print_font(screen, p_menu->p_font, 3, x_start, print_font(screen, p_menu->p_font, 255,255,0, x_start,
y_start + y, msg); y_start + y, msg);
else /* Otherwise white */ else /* Otherwise white */
print_font(screen, p_menu->p_font, 0, x_start, print_font(screen, p_menu->p_font, 255,255,255, x_start,
y_start + y, msg); y_start + y, msg);
if (IS_SUBMENU(msg)) if (IS_SUBMENU(msg))
{ {
@ -114,8 +125,14 @@ static void menu_draw(SDL_Surface *screen, menu_t *p_menu)
if (p_submenu->sel == n_pipe-1) if (p_submenu->sel == n_pipe-1)
{ {
SDL_Rect r; SDL_Rect r;
int w = p_menu->p_font->GetWidth(); int w;
int h = p_menu->p_font->GetHeight(); int h;
if (TTF_SizeText(p_menu->p_font, "X", &w, &h) < 0)
{
fprintf(stderr, "%s\n", TTF_GetError());
exit(1);
}
r = (SDL_Rect) { x_start + (n+1) * w-1, r = (SDL_Rect) { x_start + (n+1) * w-1,
y_start + (i+1 - p_menu->start_entry_visible) * ((h + h/4)-1), y_start + (i+1 - p_menu->start_entry_visible) * ((h + h/4)-1),
@ -163,7 +180,7 @@ static int is_submenu_title(menu_t *p_menu, int n)
} }
void menu_init(menu_t *p_menu, Font *p_font, char **pp_msgs, void menu_init(menu_t *p_menu, TTF_Font *p_font, char **pp_msgs,
int16_t x1, int16_t y1, int16_t x2, int16_t y2) int16_t x1, int16_t y1, int16_t x2, int16_t y2)
{ {
int i; int i;
@ -192,11 +209,15 @@ void menu_init(menu_t *p_menu, Font *p_font, char **pp_msgs,
continue; /* Length of submenus is unimportant */ continue; /* Length of submenus is unimportant */
} }
text_w_font = p_menu->p_font->GetWidth(); if (TTF_SizeText(p_font, p_menu->pp_msgs[p_menu->n_entries], &text_w_font, NULL) != 0)
{
fprintf(stderr, "%s\n", TTF_GetError());
exit(1);
}
if (text_w_font > p_menu->text_w) if (text_w_font > p_menu->text_w)
p_menu->text_w = text_w_font; p_menu->text_w = text_w_font;
} }
if ( !(p_menu->p_submenus = (submenu_t*)malloc(sizeof(submenu_t) * p_menu->n_submenus)) ) if ( !(p_menu->p_submenus = (submenu_t *)malloc(sizeof(submenu_t) * p_menu->n_submenus)) )
{ {
perror("malloc failed!\n"); perror("malloc failed!\n");
exit(1); exit(1);
@ -223,7 +244,7 @@ void menu_init(menu_t *p_menu, Font *p_font, char **pp_msgs,
} }
} }
} }
p_menu->text_h = p_menu->n_entries * (p_font->GetHeight() + p_font->GetHeight() / 4); p_menu->text_h = p_menu->n_entries * (TTF_FontHeight(p_font) + TTF_FontHeight(p_font) / 4);
} }
void menu_fini(menu_t *p_menu) void menu_fini(menu_t *p_menu)
@ -243,7 +264,7 @@ static uint32_t wait_key_press(void)
Uint32 remote_keys; Uint32 remote_keys;
WPAD_ScanPads(); WPAD_ScanPads();
remote_keys = WPAD_ButtonsDown(WPAD_CHAN_0) | WPAD_ButtonsDown(WPAD_CHAN_1); remote_keys = WPAD_ButtonsDown(WPAD_CHAN_0);
if (remote_keys & WPAD_BUTTON_DOWN) if (remote_keys & WPAD_BUTTON_DOWN)
keys |= KEY_DOWN; keys |= KEY_DOWN;
@ -306,7 +327,9 @@ static uint32_t wait_key_press(void)
} }
break; break;
} }
SDL_Delay(10); if (keys != 0)
return keys;
SDL_Delay(100);
} }
return keys; return keys;

View File

@ -14,7 +14,6 @@
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
#include "bitmap-font.h"
#include <stdint.h> #include <stdint.h>
#if defined(__cplusplus) #if defined(__cplusplus)
@ -32,7 +31,7 @@ typedef struct
typedef struct typedef struct
{ {
char **pp_msgs; char **pp_msgs;
Font *p_font; TTF_Font *p_font;
int x1,y1; int x1,y1;
int x2,y2; int x2,y2;
int text_w; int text_w;
@ -47,7 +46,7 @@ typedef struct
int n_entries; int n_entries;
} menu_t; } menu_t;
void menu_init(menu_t *p_menu, Font *p_font, char **pp_msgs, void menu_init(menu_t *p_menu, TTF_Font *p_font, char **pp_msgs,
int16_t x1, int16_t y1, int16_t x2, int16_t y2); int16_t x1, int16_t y1, int16_t x2, int16_t y2);
void menu_fini(menu_t *p_menu); void menu_fini(menu_t *p_menu);