More fixes

This commit is contained in:
simon.kagstrom 2009-11-19 17:59:43 +00:00
parent d1315cde5a
commit 1d385c66f0
4 changed files with 74 additions and 80 deletions

6
main.cpp Normal file
View File

@ -0,0 +1,6 @@
#include "menu.hh"
int main(int argc, char *argv[])
{
return 0;
}

View File

@ -752,44 +752,20 @@ const char *menu_select_file(const char *dir_path)
32, 32, FULL_DISPLAY_X/2, FULL_DISPLAY_Y - 32); 32, 32, FULL_DISPLAY_X/2, FULL_DISPLAY_Y - 32);
} }
static TTF_Font *read_font(const char *path) Menu::Menu(TTF_Font *font, SDL_Color clr, int w, int h)
{ {
TTF_Font *out; this->text_color = clr;
SDL_RWops *rw; this->font = font;
Uint8 *data = (Uint8*)malloc(1 * 1024*1024); this->w = w;
FILE *fp = fopen(path, "r"); this->h = h;
if (!data) { this->pp_msgs = NULL;
fprintf(stderr, "Malloc failed\n"); this->n_entries = 0;
exit(1); this->n_submenus = 0;
}
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);
}
out = TTF_OpenFontRW(rw, 1, 20);
if (!out)
{
fprintf(stderr, "Unable to open font %s\n", path);
exit(1);
}
fclose(fp);
return out; this->cur_sel = 0;
} this->hover_callback = NULL;
this->selection_callback = NULL;
this->mouse_x = -1;
void menu_init() this->mouse_y = -1;
{
Uint8 *data64 = (Uint8*)malloc(1 * 1024*1024);
menu_font = read_font(FONT_PATH);
menu_font64 = read_font(FONT64_PATH);
} }

66
menu.hh
View File

@ -15,48 +15,49 @@
#include <SDL.h> #include <SDL.h>
#include <SDL_ttf.h> #include <SDL_ttf.h>
#include <stdint.h> #include <stdint.h>
#include "sysdeps.h"
#include "Network.h"
#define KEY_UP 1 enum {
#define KEY_DOWN 2 KEY_UP,
#define KEY_LEFT 4 KEY_DOWN,
#define KEY_RIGHT 8 KEY_LEFT,
#define KEY_SELECT 16 KEY_RIGHT,
#define KEY_ESCAPE 32 KEY_SELECT,
#define KEY_PAGEDOWN 64 KEY_ESCAPE,
#define KEY_PAGEUP 128 KEY_PAGEDOWN,
#define KEY_HELP 256 KEY_PAGEUP,
KEY_HELP,
};
class Menu class Menu
{ {
public: public:
Menu(int x, int y, int w, int h); Menu(TTF_Font *font, SDL_Color clr, int w, int h);
void setTextColor(const SDL_Color clr); void setText(const char **messages);
void pushEvent(SDL_Event *ev); void pushEvent(SDL_Event *ev);
void runLogic(); void runLogic();
void draw(SDL_Surface *where); void draw(SDL_Surface *where,
int x, int y);
~Menu(); ~Menu();
private: private:
const char *title; const char *title;
const char **pp_msgs; const char **pp_msgs;
TTF_Font *p_font; TTF_Font *font;
SDL_Color text_color; SDL_Color text_color;
int (*hover_callback)(Menu *me, int index); int (*hover_callback)(Menu *me, int index);
int (*selection_callback)(Menu *me, int index); int (*selection_callback)(Menu *me, int index);
/* Start and end of the menu */ /* Width and height */
int x1,y1; int w, h;
int x2,y2;
int text_w; /* Relative to this menu */
int text_h; int mouse_x, mouse_y;
int n_submenus; int n_submenus;
submenu_t *p_submenus; submenu_t *p_submenus;
@ -64,29 +65,8 @@ private:
int cur_sel; /* Main selection */ int cur_sel; /* Main selection */
int start_entry_visible; int start_entry_visible;
int n_entries; int n_entries;
}; };
void menu_print_font(SDL_Surface *screen, int r, int g, int b, int x, int y, const char *msg);
void menu_print_font64(SDL_Surface *screen, int r, int g, int b, int x, int y, const char *msg);
/* Various option selects */
int menu_select(const char *title, const char **pp_msgs, int *p_submenus);
int menu_select(const char **pp_msgs, int *p_submenus);
int menu_select_sized(char *title, const char **msgs, int *submenus,
int x, int y, int w, int h);
const char *menu_select_file(const char *dir_path);
const char *menu_select_file_start(const char *dir_path, const char **d64_name);
int menu_select_peer(NetworkUpdatePeerInfo *peers, int n_peers);
uint32_t menu_wait_key_press(void);
extern bool msgKill(SDL_Rect *rc);
extern int msgInfo(char *text, int duration, SDL_Rect *rc);
extern bool msgYesNo(char *text, bool def,int x, int y);
void menu_init();
#endif /* !__MENU_H__ */ #endif /* !__MENU_H__ */

32
utils.cpp Normal file
View File

@ -0,0 +1,32 @@
TTF_Font *read_font(const char *path)
{
TTF_Font *out;
SDL_RWops *rw;
Uint8 *data = (Uint8*)malloc(1 * 1024*1024);
FILE *fp = fopen(path, "r");
if (!data) {
fprintf(stderr, "Malloc failed\n");
exit(1);
}
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);
}
out = TTF_OpenFontRW(rw, 1, 20);
if (!out)
{
fprintf(stderr, "Unable to open font %s\n", path);
exit(1);
}
fclose(fp);
return out;
}