From c36597dcb1fef4fe651baa49366c482388dc0f14 Mon Sep 17 00:00:00 2001 From: "simon.kagstrom" Date: Mon, 23 Nov 2009 18:52:30 +0000 Subject: [PATCH] Buildable, started an example --- Makefile | 9 +++++++-- main.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ menu.cpp | 6 +++--- menu.hh | 4 ++-- utils.cpp | 7 +++++-- utils.hh | 6 ++++-- 6 files changed, 77 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 371af18..004b763 100644 --- a/Makefile +++ b/Makefile @@ -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 $@ $+ diff --git a/main.cpp b/main.cpp index a4f9ad9..8c3b0f5 100644 --- a/main.cpp +++ b/main.cpp @@ -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; } diff --git a/menu.cpp b/menu.cpp index 75f2a14..7796f49 100644 --- a/menu.cpp +++ b/menu.cpp @@ -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); diff --git a/menu.hh b/menu.hh index bdef133..5900192 100644 --- a/menu.hh +++ b/menu.hh @@ -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); diff --git a/utils.cpp b/utils.cpp index 460bc02..cf3c40f 100644 --- a/utils.cpp +++ b/utils.cpp @@ -1,5 +1,7 @@ #include #include +#include +#include #include #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); diff --git a/utils.hh b/utils.hh index 0b8b953..8964571 100644 --- a/utils.hh +++ b/utils.hh @@ -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__ */