Drawing stuff

This commit is contained in:
simon.kagstrom 2009-11-20 20:28:48 +00:00
parent 73ad55c774
commit a9d3c2f3fe
3 changed files with 64 additions and 21 deletions

View File

@ -201,6 +201,30 @@ static void menu_draw(SDL_Surface *screen, menu_t *p_menu, int sel)
}
}
void Menu::highlightBackground(const char *msg, int x, int y)
{
int tw, th;
TTF_SizeText(this->font, msg, &tw, &th);
int bg_y_start = y + font_height / 2 -
this->text_bg_left->h / 2;
int bg_x_start = x - this->text_bg_right / 2;
int bg_x_end = x + tw -
this->text_bg_right / 2;
int n_mid = this->text_bg_left->w + this->text_bg_right->w -
this->text_bg_middle->w;
SDL_BlitSurface(this->text_bg_left, NULL,
where, (SDL_Rect){bg_x_start, bg_y_start, 0,0});
for (int i = 1; i < n_mid; i++)
SDL_BlitSurface(this->text_bg_middle, NULL,
where,
(SDL_Rect){bg_x_start + i * this->text_bg_middle->w,
bg_y_start, 0,0});
SDL_BlitSurface(this->text_bg_right, NULL,
where, (SDL_Rect){bg_x_end, bg_y_start, 0,0});
}
void Menu::draw(SDL_Surface *where, int x, int y, int w, int h)
{
@ -230,30 +254,21 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h)
i <= this->start_entry_visible + entries_visible; i++)
{
const char *msg = this->pp_msgs[i];
int cur_y;
if (i >= this->n_entries)
break;
y = (i - this->start_entry_visible) * line_height;
cur_y = (i - this->start_entry_visible) * line_height;
/* Draw the background for the selected entry */
if (this->cur_sel == i)
this->highlightBackground(msg, x_start, cur_y);
/* And print the text on top */
this->printText(msg, this->text_color,
x_start, y_start + y, w);
if (sel < 0)
this->printText(msg, this->text_color,
x_start, y_start + y, w);
else if (p_menu->cur_sel == i) /* Selected - color */
menu_print_font(screen, 0,255,0,
x_start, y_start + y, msg);
else if (IS_SUBMENU(msg))
{
if (p_menu->cur_sel == i-1)
menu_print_font(screen, 0x80,0xff,0x80,
x_start, y_start + y, msg);
else
menu_print_font(screen, 0x40,0x40,0x40,
x_start, y_start + y, msg);
}
else /* Otherwise white */
menu_print_font(screen, 0x40,0x40,0x40,
x_start, y_start + y, msg);
if (IS_SUBMENU(msg))
{
submenu_t *p_submenu = this->findSubmenu(i);
@ -262,6 +277,24 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h)
for (n = 0; msg[n] != '\0'; n++)
{
if (msg[n] == '|')
{
int n_chars;
char *p;
for (n_chars = 1; msg[n+n_chars] && msg[n+n_chars] != '|'; n_chars++);
n_pipe++;
if (p_submenu->sel != n_pipe-1)
continue;
p = xmalloc(n_chars + 1);
strncpy(p, msg + n, n_chars);
/* Found it! */
this->highlightBackground(p, x, y);
free(p);
break;
}
}
}
}

13
menu.hh
View File

@ -46,7 +46,12 @@ public:
void setTextColor(SDL_Color clr);
void setSelectedColor(SDL_Color clr);
void setSelectedBackground(SDL_Surface *left, SDL_Surface *middle, SDL_Surface *right)
{
this->text_bg_left = left;
this->text_bg_middle = middle;
this->text_bg_right = right;
}
void setText(const char **messages);
@ -60,6 +65,8 @@ public:
~Menu();
protected:
void highlightBackground(const char *msg, int x, int y);
void printText(const char *msg, SDL_Color clr, int x, int y);
virtual void selectCallback(int which);
@ -84,7 +91,9 @@ protected:
const char **pp_msgs;
TTF_Font *font;
SDL_Color text_color;
SDL_Color text_selected_color;
SDL_Surface *text_bg_left;
SDL_Surface *text_bg_right;
SDL_Surface *text_bg_middle;
/* Relative to this menu */
int mouse_x, mouse_y;

View File

@ -28,6 +28,7 @@ static inline void *xmalloc(size_t sz)
void *out = malloc(sz);
panic_if(!out, "malloc failed");
memset(out, 0, sz);
return out;
}