Buildable, started an example

This commit is contained in:
simon.kagstrom 2009-11-23 18:52:30 +00:00
parent e12f2f77d1
commit c36597dcb1
6 changed files with 77 additions and 11 deletions

View File

@ -1,10 +1,15 @@
OBJS=menu.oo main.oo
OBJS=menu.oo main.oo utils.oo
all: menu
%.oo: %.cpp
g++ -c `sdl-config --cflags` -o $@ $<
g++ -Wall -g -c `sdl-config --cflags` -o $@ $<
menu.oo: menu.cpp menu.hh utils.hh Makefile
utils.oo: utils.cpp utils.hh Makefile
main.oo: menu.hh utils.hh Makefile
menu: $(OBJS)
g++ `sdl-config --libs` -lSDL -lSDL_image -lSDL_ttf -o $@ $+

View File

@ -1,6 +1,62 @@
#include "menu.hh"
#include "utils.hh"
class PrintMenu : public Menu
{
public:
PrintMenu(TTF_Font *font) : Menu(font)
{
}
virtual void selectCallback(int which)
{
printf("entry %d selected: %s\n", which, this->pp_msgs[which]);
}
virtual void escapeCallback(int which)
{
printf("entry %d escaped: %s\n", which, this->pp_msgs[which]);
}
};
SDL_Surface *screen;
PrintMenu *g_menu;
static void run(void)
{
while(1)
{
SDL_Event ev;
while (SDL_PollEvent(&ev)) {
if (ev.type == SDL_QUIT)
exit(1); break;
g_menu->pushEvent(&ev); break;
}
g_menu->draw(screen, 0, 80, 400, 400);
SDL_Delay(50);
}
}
static void init(void)
{
TTF_Font *fnt;
screen = SDL_SetVideoMode(640, 480, 16,
SDL_DOUBLEBUF);
panic_if(!screen, "Cannot initialize video: %s\n", SDL_GetError());
TTF_Init();
fnt = read_and_alloc_font("font.ttf");
g_menu = new PrintMenu(fnt);
}
int main(int argc, char *argv[])
{
init();
run();
return 0;
}

View File

@ -39,7 +39,7 @@ void Menu::printText(SDL_Surface *where, const char *msg, SDL_Color clr,
int last_char = w / pixels_per_char;
/* FIXME! Handle some corner cases here (short strings etc) */
panic_if(last_char > strlen(msg),
panic_if((unsigned)last_char > strlen(msg),
"last character (%d) is after the string length (%d)\n",
last_char, strlen(msg));
if (last_char > 3)
@ -106,9 +106,10 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h)
int line_height = (font_height + font_height / 4);
int x_start = x;
int y_start = y + line_height;
SDL_Rect r;
int entries_visible = h / line_height - 2;
panic_if(!this->pp_msgs, "Set the messages before drawing, thank you\n");
if (this->cur_sel - this->start_entry_visible > entries_visible)
{
while (this->cur_sel - this->start_entry_visible > entries_visible)
@ -226,7 +227,6 @@ void Menu::selectOne(int which)
void Menu::selectNext(int dx, int dy)
{
int next;
char buffer[256];
this->cur_sel = this->getNextEntry(dy);
next = this->getNextEntry(dy + 1);

View File

@ -70,9 +70,9 @@ protected:
void printText(SDL_Surface *where, const char *msg, SDL_Color clr,
int x, int y, int w, int h);
virtual void selectCallback(int which);
virtual void selectCallback(int which) = 0;
virtual void escapeCallback(int which);
virtual void escapeCallback(int which) = 0;
submenu_t *findSubmenu(int index);

View File

@ -1,5 +1,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <SDL_ttf.h>
#include "utils.hh"
@ -16,7 +18,7 @@ TTF_Font *read_and_alloc_font(const char *path)
exit(1);
}
if (!fp) {
fprintf(stderr, "Could not open font\n");
fprintf(stderr, "Could not open font %s\n", path);
exit(1);
}
fread(data, 1, 1 * 1024 * 1024, fp);
@ -29,7 +31,8 @@ TTF_Font *read_and_alloc_font(const char *path)
out = TTF_OpenFontRW(rw, 1, 20);
if (!out)
{
fprintf(stderr, "Unable to open font %s\n", path);
fprintf(stderr, "TTF: Unable to create font %s (%s)\n",
path, TTF_GetError());
exit(1);
}
fclose(fp);

View File

@ -5,9 +5,9 @@
#define panic(x...) do \
{ \
fprintf(stderr, "============Translator panic===========\n"); \
fprintf(stderr, "=============PANIC PANIC PANIC===========\n"); \
fprintf(stderr, x); \
fprintf(stderr, "=======================================\n"); \
fprintf(stderr, "=========================================\n"); \
exit(1); \
} while(0)
@ -39,4 +39,6 @@ static inline void *xmalloc(size_t sz)
panic_if(r < 0 || r >= (int)(size), "snprintf failed for %s with %d\n", fmt, r); \
} while(0)
TTF_Font *read_and_alloc_font(const char *path);
#endif /* __UTILS_H__ */