Date:2010-05-02 14:52:46 (13 years 7 months ago)
Author:Lars C.
Commit:28d3fc9eceb22e7fded2ac2a7f887b0bba996e28
Message:iconbutton: Cache icon surface instead of doing serveral hashtable lookups per frame

Files: src/iconbutton.cpp (5 diffs)
src/iconbutton.h (2 diffs)

Change Details

src/iconbutton.cpp
11#include "iconbutton.h"
22#include "gmenu2x.h"
3#include "surface.h"
34
45using namespace std;
56using namespace fastdelegate;
...... 
1011    labelPosition = IconButton::DISP_RIGHT;
1112    labelMargin = 2;
1213    this->setLabel(label);
14    updateSurfaces();
15}
16
17void IconButton::updateSurfaces()
18{
19    iconSurface = gmenu2x->sc[icon];
20    recalcSize();
1321}
1422
1523void IconButton::setPosition(int x, int y) {
...... 
2129
2230void IconButton::paint() {
2331    uint margin = labelMargin;
24    if (gmenu2x->sc[icon] == NULL || label == "")
32    if (iconSurface == NULL || label == "")
2533        margin = 0;
26    if (gmenu2x->sc[icon] != NULL)
27        gmenu2x->sc[icon]->blit(gmenu2x->s,iconRect);
34    if (iconSurface != NULL)
35        iconSurface->blit(gmenu2x->s,iconRect);
2836    if (label != "")
2937        gmenu2x->s->write(gmenu2x->font, label, labelRect.x, labelRect.y, labelHAlign, labelVAlign);
3038}
...... 
3745    uint h = 0, w = 0;
3846    uint margin = labelMargin;
3947
40    if (gmenu2x->sc[icon] == NULL || label == "")
48    if (iconSurface == NULL || label == "")
4149        margin = 0;
4250
43    if (gmenu2x->sc[icon] != NULL) {
44        w += gmenu2x->sc[icon]->raw->w;
45        h += gmenu2x->sc[icon]->raw->h;
51    if (iconSurface != NULL) {
52        w += iconSurface->raw->w;
53        h += iconSurface->raw->h;
4654        iconRect.w = w;
4755        iconRect.h = h;
4856        iconRect.x = rect.x;
...... 
113121
114122void IconButton::setIcon(const string &icon) {
115123    this->icon = icon;
116    recalcSize();
124    updateSurfaces();
117125}
118126
119127void IconButton::setAction(ButtonAction action) {
src/iconbutton.h
77using std::string;
88
99class GMenu2X;
10class Surface;
1011
1112class IconButton : public Button {
1213protected:
...... 
1617    void recalcSize();
1718    SDL_Rect iconRect, labelRect;
1819
20    Surface *iconSurface;
21
22    void updateSurfaces();
23
1924public:
2025    static const int DISP_RIGHT = 0;
2126    static const int DISP_LEFT = 1;

Archive Download the corresponding diff file



interactive