Date:2011-06-02 04:29:19 (12 years 9 months ago)
Author:Maarten ter Huurne
Commit:b2896d6bac3d8f0180ba8c0febdbb0bbf8554458
Message:ASFont: Refactored string drawing methods.

Renamed methods that draw a single line from write() to writeLine().
There is now only one write() method left: the public method.

Pass surface to draw on as wrapped Surface instead of SDL_Surface.
At the end of the call chain we still use SDL directly though.
Files: src/asfont.cpp (6 diffs)
src/asfont.h (1 diff)

Change Details

src/asfont.cpp
100100    //return c>=194;
101101}
102102
103void ASFont::write(SDL_Surface *s, const std::string &text, int x, int y) {
103void ASFont::writeLine(Surface *s, const std::string &text, int x, int y) {
104104    if (text.empty()) return;
105105
106106    std::string::size_type pos;
...... 
129129        srcrect.w = charpos[pos+2] - charpos[pos];
130130        dstrect.x = x - charpos[pos+1] + charpos[pos];
131131
132        SDL_BlitSurface(surface, &srcrect, s, &dstrect);
132        SDL_BlitSurface(surface, &srcrect, s->raw, &dstrect);
133133
134134        x += charpos[pos+2] - charpos[pos+1];
135135    }
...... 
165165    return getTextWidth(text.c_str());
166166}
167167
168void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign) {
168void ASFont::writeLine(Surface* surface, const std::string& text, int x, int y, HAlign halign) {
169169    switch (halign) {
170170    case HAlignLeft:
171171        break;
...... 
176176        x -= getTextWidth(text);
177177        break;
178178    }
179    write(surface, text, x, y);
179    writeLine(surface, text, x, y);
180180}
181181
182void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) {
182void ASFont::writeLine(Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) {
183183    switch (valign) {
184184    case VAlignTop:
185185        break;
...... 
190190        y -= getHeight();
191191        break;
192192    }
193    write(surface, text, x, y, halign);
193    writeLine(surface, text, x, y, halign);
194194}
195195
196void ASFont::write(SDL_Surface* surface, const std::vector<std::string> &text, int x, int y, HAlign halign, VAlign valign) {
196void ASFont::writeLine(Surface* surface, const std::vector<std::string> &text, int x, int y, HAlign halign, VAlign valign) {
197197    switch (valign) {
198198    case VAlignTop:
199199        break;
...... 
215215    if (text.find("\n", 0) != std::string::npos) {
216216        std::vector<std::string> textArr;
217217        split(textArr, text, "\n");
218        write(surface->raw, textArr, x, y, halign, valign);
218        writeLine(surface, textArr, x, y, halign, valign);
219219    } else
220        write(surface->raw, text, x, y, halign, valign);
220        writeLine(surface, text, x, y, halign, valign);
221221}
src/asfont.h
3535    void write(Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop);
3636
3737private:
38    void write(SDL_Surface *surface, const std::string &text, int x, int y);
39    void write(SDL_Surface *surface, const std::string &text, int x, int y, HAlign halign);
40    void write(SDL_Surface *surface, const std::string &text, int x, int y, HAlign halign, VAlign valign);
41    void write(SDL_Surface *surface, const std::vector<std::string> &text, int x, int y, HAlign halign, VAlign valign);
38    void writeLine(Surface *surface, const std::string &text, int x, int y);
39    void writeLine(Surface *surface, const std::string &text, int x, int y, HAlign halign);
40    void writeLine(Surface *surface, const std::string &text, int x, int y, HAlign halign, VAlign valign);
41    void writeLine(Surface *surface, const std::vector<std::string> &text, int x, int y, HAlign halign, VAlign valign);
4242
4343    SDL_Surface *surface;
4444    std::vector<unsigned> charpos;

Archive Download the corresponding diff file



interactive