Move background highlighting to utils

This commit is contained in:
simon.kagstrom 2009-12-20 10:21:06 +00:00
parent b94df096f0
commit f8bbe977c0
5 changed files with 48 additions and 47 deletions

View File

@ -60,41 +60,6 @@ void Menu::printText(SDL_Surface *where, const char *msg, SDL_Color clr,
free(buf);
}
void Menu::highlightBackground(SDL_Surface *where,
SDL_Surface *bg_left, SDL_Surface *bg_middle, SDL_Surface *bg_right,
int x, int y, int w, int h)
{
SDL_Rect dst;
/* Can't highlight without images */
if (!bg_left || !bg_middle || !bg_right)
return;
int font_height = this->font->getHeight("X");
int bg_y_start = y + font_height / 2 -
bg_left->h / 2;
int bg_x_start = x - bg_left->w / 3;
int bg_x_end = x + w - (2 * bg_right->w) / 3;
int n_mid = (bg_x_end - bg_x_start) / bg_middle->w;
/* Left */
dst = (SDL_Rect){bg_x_start, bg_y_start, 0,0};
SDL_BlitSurface(bg_left, NULL, where, &dst);
/* Middle */
for (int i = 1; i < n_mid; i++)
{
dst = (SDL_Rect){bg_x_start + i * bg_middle->w, bg_y_start, 0,0};
SDL_BlitSurface(bg_middle, NULL, where, &dst);
}
dst = (SDL_Rect){bg_x_end - bg_middle->w, bg_y_start, 0,0};
SDL_BlitSurface(bg_middle, NULL, where, &dst);
/* Right */
dst = (SDL_Rect){bg_x_end, bg_y_start, 0,0};
SDL_BlitSurface(bg_right, NULL, where, &dst);
}
void Menu::draw(SDL_Surface *where, int x, int y, int w, int h)
{
@ -144,7 +109,7 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h)
tw = this->font->getWidth(msg);
th = this->font->getHeight(msg);
this->highlightBackground(where,
highlight_background(where, this->font,
this->text_bg_left, this->text_bg_middle, this->text_bg_right,
x_start, cur_y, tw, th);
}
@ -185,7 +150,7 @@ void Menu::draw(SDL_Surface *where, int x, int y, int w, int h)
tw = this->font->getWidth(p);
th = this->font->getHeight(p);
this->highlightBackground(where,
highlight_background(where, this->font,
this->submenu_bg_left, this->submenu_bg_middle, this->submenu_bg_right,
x_start + tw_first, cur_y, tw, th);
free(p);

View File

@ -61,10 +61,6 @@ public:
int x, int y, int w, int h);
protected:
void highlightBackground(SDL_Surface *where,
SDL_Surface *bg_left, SDL_Surface *bg_middle, SDL_Surface *bg_right,
int x, int y, int w, int h);
void printText(SDL_Surface *where, const char *msg, SDL_Color clr,
int x, int y, int w, int h);

View File

@ -6,6 +6,7 @@
#include <SDL_ttf.h>
#include "utils.hh"
#include "font.hh"
TTF_Font *read_and_alloc_font(const char *path, int pt_size)
{
@ -238,3 +239,38 @@ void *sdl_surface_to_png(SDL_Surface *surf, size_t *out_sz)
return out.data;
}
void highlight_background(SDL_Surface *where, Font *font,
SDL_Surface *bg_left, SDL_Surface *bg_middle, SDL_Surface *bg_right,
int x, int y, int w, int h)
{
SDL_Rect dst;
/* Can't highlight without images */
if (!bg_left || !bg_middle || !bg_right)
return;
int font_height = font->getHeight("X");
int bg_y_start = y + font_height / 2 -
bg_left->h / 2;
int bg_x_start = x - bg_left->w / 3;
int bg_x_end = x + w - (2 * bg_right->w) / 3;
int n_mid = (bg_x_end - bg_x_start) / bg_middle->w;
/* Left */
dst = (SDL_Rect){bg_x_start, bg_y_start, 0,0};
SDL_BlitSurface(bg_left, NULL, where, &dst);
/* Middle */
for (int i = 1; i < n_mid; i++)
{
dst = (SDL_Rect){bg_x_start + i * bg_middle->w, bg_y_start, 0,0};
SDL_BlitSurface(bg_middle, NULL, where, &dst);
}
dst = (SDL_Rect){bg_x_end - bg_middle->w, bg_y_start, 0,0};
SDL_BlitSurface(bg_middle, NULL, where, &dst);
/* Right */
dst = (SDL_Rect){bg_x_end, bg_y_start, 0,0};
SDL_BlitSurface(bg_right, NULL, where, &dst);
}

View File

@ -8,6 +8,8 @@
#include <SDL.h>
#include <SDL_ttf.h>
class Font;
#define BUG_ON(cond)
#define panic(x...) do \
@ -67,4 +69,8 @@ const char **get_file_list(const char *base_dir, const char *exts[]);
void *sdl_surface_to_png(SDL_Surface *src, size_t *out_sz);
void highlight_background(SDL_Surface *where, Font *font,
SDL_Surface *bg_left, SDL_Surface *bg_middle, SDL_Surface *bg_right,
int x, int y, int w, int h);
#endif /* __UTILS_H__ */

View File

@ -115,12 +115,10 @@ void VirtualKeyboard::draw(SDL_Surface *where, int x, int y, int w, int h)
what = shifted_names[which];
if (this->sel_x == x && this->sel_y == y)
{
SDL_Rect dst = (SDL_Rect){x * key_w + border_x - 8,
y * key_h + border_y - 4, 0,0};
SDL_BlitSurface(Gui::gui->selected_key, NULL, where, &dst);
}
highlight_background(where, Gui::gui->small_font,
Gui::gui->bg_left, Gui::gui->bg_middle, Gui::gui->bg_right,
x * key_w + border_x, y * key_h + border_y,
this->font->getWidth(what), h);
this->font->draw(where, what,
x * key_w + border_x, y * key_h + border_y, w, h);
}