From 34a3d55d10ed814e03a767e4518ee3d728dcf5e8 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 13 Apr 2011 01:51:23 +0200 Subject: [PATCH 01/27] Added missing #include. Fixes compile error when compiling with GCC 4.5.1. --- src/debug.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/debug.h b/src/debug.h index 5ba9387..12e101b 100644 --- a/src/debug.h +++ b/src/debug.h @@ -2,6 +2,8 @@ #ifndef DEBUG_H #define DEBUG_H +#include + #define NODEBUG_L 0 #define ERROR_L 1 #define WARNING_L 2 From c22cc4d6632da0d8c2b3c82a1b7b0f8b469f66e5 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 13 Apr 2011 03:36:47 +0200 Subject: [PATCH 02/27] Cleaned up link flags. Use the flags found by "configure" and nothing more. The hardcoded "-lpng12" broke linking with libpng 1.4. --- src/Makefile.am | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index f2dbbfc..ce31093 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -31,8 +31,4 @@ AM_CXXFLAGS = @CXXFLAGS@ @SDL_CFLAGS@ -DTARGET_GP2X \ -fno-exceptions \ -Wall -Wextra -Wundef -Wunused-macros -gmenu2x_LDADD = @LIBS@ @SDL_LIBS@ -lSDL_image -lSDL_gfx -lSDL -ljpeg -lpng12 -lz -ldl -lpthread - -gmenu2x_LDFLAGS = -lSDL_image -lSDL_gfx -lSDL -ljpeg -lpng12 -lz -ldl -lpthread - -gmenu2x_LIBS = @LIBS@ @SDL_LIBS@ -lSDL_image -lSDL_gfx -lSDL -ljpeg -lpng12 -lz -ldl -lpthread +gmenu2x_LDADD = @LIBS@ @SDL_LIBS@ From 38a83dd385adc1018d11e5a9f0a2e9a30a1c1973 Mon Sep 17 00:00:00 2001 From: Ayla Date: Thu, 14 Apr 2011 19:35:50 +0200 Subject: [PATCH 03/27] On the skin menu, the ALTLEFT/ALTRIGHT buttons will now respectively decrease/increase the ARGB values by 10. --- src/menusettingrgba.cpp | 15 +++++++++++++-- src/menusettingrgba.h | 1 + 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/menusettingrgba.cpp b/src/menusettingrgba.cpp index e940d20..10ea776 100644 --- a/src/menusettingrgba.cpp +++ b/src/menusettingrgba.cpp @@ -90,6 +90,12 @@ void MenuSettingRGBA::manageInput(bevent_t *event) { case CLEAR: dec(); break; + case ALTLEFT: + update_value(-10); + break; + case ALTRIGHT: + update_value(10); + break; case LEFT: leftComponent(); break; @@ -101,14 +107,19 @@ void MenuSettingRGBA::manageInput(bevent_t *event) { } } +void MenuSettingRGBA::update_value(int value) +{ + setSelPart(constrain(getSelPart() + value, 0, 255)); +} + void MenuSettingRGBA::dec() { - setSelPart(constrain(getSelPart()-1,0,255)); + update_value(-1); } void MenuSettingRGBA::inc() { - setSelPart(constrain(getSelPart()+1,0,255)); + update_value(+1); } void MenuSettingRGBA::leftComponent() diff --git a/src/menusettingrgba.h b/src/menusettingrgba.h index 9c3d0e0..11f8814 100644 --- a/src/menusettingrgba.h +++ b/src/menusettingrgba.h @@ -34,6 +34,7 @@ private: RGBAColor originalValue; RGBAColor *_value; + void update_value(int value); void dec(); void inc(); void leftComponent(); From e6be83503838810de0f39dc169a75a9d25ae47a4 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 30 Mar 2011 03:21:31 +0200 Subject: [PATCH 04/27] Isolate all PNG loading in a separate source/header. This is in preparation of replacing SDL_image with direct use of libpng. --- src/Makefile.am | 6 ++++-- src/imageio.cpp | 8 ++++++++ src/imageio.h | 10 ++++++++++ src/sfontplus.cpp | 5 +++-- src/surface.cpp | 11 ++++++----- src/surface.h | 3 +-- src/surfacecollection.cpp | 2 ++ 7 files changed, 34 insertions(+), 11 deletions(-) create mode 100644 src/imageio.cpp create mode 100644 src/imageio.h diff --git a/src/Makefile.am b/src/Makefile.am index ce31093..d0b5716 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -11,7 +11,8 @@ gmenu2x_SOURCES = asfont.cpp button.cpp cpu.cpp dirdialog.cpp filedialog.cpp \ settingsdialog.cpp sfontplus.cpp surfacecollection.cpp surface.cpp \ textdialog.cpp textmanualdialog.cpp touchscreen.cpp translator.cpp \ utilities.cpp wallpaperdialog.cpp \ - browsedialog.cpp buttonbox.cpp dialog.cpp + browsedialog.cpp buttonbox.cpp dialog.cpp \ + imageio.cpp noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \ filedialog.h filelister.h gmenu2x.h gp2x.h iconbutton.h imagedialog.h \ @@ -23,7 +24,8 @@ noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \ messagebox.h selector.h settingsdialog.h \ sfontplus.h surfacecollection.h surface.h textdialog.h textmanualdialog.h \ touchscreen.h translator.h utilities.h wallpaperdialog.h \ - browsedialog.h buttonbox.h dialog.h + browsedialog.h buttonbox.h dialog.h \ + imageio.h AM_CFLAGS= @CFLAGS@ @SDL_CFLAGS@ diff --git a/src/imageio.cpp b/src/imageio.cpp new file mode 100644 index 0000000..aeebbcb --- /dev/null +++ b/src/imageio.cpp @@ -0,0 +1,8 @@ +#include "imageio.h" + +#include + +SDL_Surface *loadPNG(const std::string &path) +{ + return IMG_Load(path.c_str()); +} diff --git a/src/imageio.h b/src/imageio.h new file mode 100644 index 0000000..3e4e48f --- /dev/null +++ b/src/imageio.h @@ -0,0 +1,10 @@ +#ifndef IMAGEIO_H +#define IMAGEIO_H + +#include + +struct SDL_Surface; + +SDL_Surface *loadPNG(const std::string &path); + +#endif diff --git a/src/sfontplus.cpp b/src/sfontplus.cpp index 765ec99..8ac44bb 100644 --- a/src/sfontplus.cpp +++ b/src/sfontplus.cpp @@ -1,7 +1,8 @@ #include "sfontplus.h" +#include "imageio.h" + #include -#include #include using namespace std; @@ -63,7 +64,7 @@ bool SFontPlus::utf8Code(unsigned char c) { } void SFontPlus::initFont(const string &font, const string &characters) { - SDL_Surface *buf = IMG_Load(font.c_str()); + SDL_Surface *buf = loadPNG(font); if (buf!=NULL) { initFont( SDL_DisplayFormatAlpha(buf), characters ); SDL_FreeSurface(buf); diff --git a/src/surface.cpp b/src/surface.cpp index 4947f67..e5dc46e 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -18,15 +18,16 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ +#include "surface.h" +#include "imageio.h" +#include "utilities.h" +#include "debug.h" + #include #include using namespace std; -#include "surface.h" -#include "utilities.h" -#include "debug.h" - RGBAColor strtorgba(const string &strColor) { RGBAColor c = {0,0,0,255}; c.r = constrain( strtol( strColor.substr(0,2).c_str(), NULL, 16 ), 0, 255 ); @@ -124,7 +125,7 @@ void Surface::load(const string &img, bool alpha, const string &skin) { skinpath = img; } - SDL_Surface *buf = IMG_Load(skinpath.c_str()); + SDL_Surface *buf = loadPNG(skinpath); if (buf!=NULL) { if (alpha) raw = SDL_DisplayFormatAlpha(buf); diff --git a/src/surface.h b/src/surface.h index e0f3478..2fdb047 100644 --- a/src/surface.h +++ b/src/surface.h @@ -20,9 +20,8 @@ #ifndef SURFACE_H #define SURFACE_H -#include #include -#include +#include #include "asfont.h" diff --git a/src/surfacecollection.cpp b/src/surfacecollection.cpp index bcd8a51..f9c22a3 100644 --- a/src/surfacecollection.cpp +++ b/src/surfacecollection.cpp @@ -23,6 +23,8 @@ #include "utilities.h" #include "debug.h" +#include + using std::endl; using std::string; From c54dec90f5c5dc9decf6aab64ab2202c51cfa92f Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Mon, 9 May 2011 04:13:11 +0200 Subject: [PATCH 05/27] Minor cleanups in SFontPlus and ASFont. Removed unused includes. Avoid importing classes into default namespace in headers. Don't use a type alias if it does not add value. --- src/asfont.cpp | 22 +++++++++------------- src/asfont.h | 20 +++++++++----------- src/sfontplus.cpp | 46 +++++++++++++++++++++------------------------- src/sfontplus.h | 25 ++++++++++--------------- 4 files changed, 49 insertions(+), 64 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 40b3169..0518a1d 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -2,10 +2,6 @@ #include "surface.h" #include "utilities.h" -#include - -using namespace std; - ASFont::ASFont(SDL_Surface* font) { this->font.initFont(font); halfHeight = getHeight()/2; @@ -18,7 +14,7 @@ ASFont::ASFont(Surface* font) { halfLineHeight = getLineHeight()/2; } -ASFont::ASFont(const string &font) { +ASFont::ASFont(const std::string &font) { this->font.initFont(font); halfHeight = getHeight()/2; halfLineHeight = getLineHeight()/2; @@ -57,7 +53,7 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, font.write(surface, text, x, y); } -void ASFont::write(SDL_Surface* surface, vector *text, int x, int y, const unsigned short halign, const unsigned short valign) { +void ASFont::write(SDL_Surface* surface, std::vector *text, int x, int y, const unsigned short halign, const unsigned short valign) { switch (valign) { case SFontVAlignMiddle: y -= getHalfHeight()*text->size(); @@ -67,7 +63,7 @@ void ASFont::write(SDL_Surface* surface, vector *text, int x, int y, con break; } - for (uint i=0; isize(); i++) { + for (unsigned i=0; isize(); i++) { int ix = x; switch (halign) { case SFontHAlignCenter: @@ -83,8 +79,8 @@ void ASFont::write(SDL_Surface* surface, vector *text, int x, int y, con } void ASFont::write(Surface* surface, const std::string& text, int x, int y, const unsigned short halign, const unsigned short valign) { - if (text.find("\n",0)!=string::npos) { - vector textArr; + if (text.find("\n", 0) != std::string::npos) { + std::vector textArr; split(textArr,text,"\n"); write(surface->raw, &textArr, x, y, halign, valign); } else @@ -109,16 +105,16 @@ int ASFont::getTextWidth(const char* text) { return font.getTextWidth(text); } int ASFont::getTextWidth(const std::string& text) { - if (text.find("\n",0)!=string::npos) { - vector textArr; + if (text.find("\n", 0) != std::string::npos) { + std::vector textArr; split(textArr,text,"\n"); return getTextWidth(&textArr); } else return getTextWidth(text.c_str()); } -int ASFont::getTextWidth(vector *text) { +int ASFont::getTextWidth(std::vector *text) { int w = 0; - for (uint i=0; isize(); i++) + for (unsigned i=0; isize(); i++) w = max( getTextWidth(text->at(i).c_str()), w ); return w; } diff --git a/src/asfont.h b/src/asfont.h index 2ac318f..f70f0c1 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -3,13 +3,13 @@ #ifndef ASFONT_H #define ASFONT_H -#include -#include -#include #include "sfontplus.h" -using std::string; -using std::vector; +#include +#include + +struct SDL_Surface; +class Surface; const unsigned short SFontHAlignLeft = 0; const unsigned short SFontHAlignRight = 1; @@ -18,13 +18,11 @@ const unsigned short SFontVAlignTop = 0; const unsigned short SFontVAlignBottom = 1; const unsigned short SFontVAlignMiddle = 2; -class Surface; - class ASFont { public: ASFont(SDL_Surface* font); ASFont(Surface* font); - ASFont(const string &font); + ASFont(const std::string &font); ~ASFont(); bool utf8Code(unsigned char c); @@ -34,11 +32,11 @@ public: int getLineHeight(); int getHalfLineHeight(); int getTextWidth(const char* text); - int getTextWidth(const string& text); - int getTextWidth(vector *text); + int getTextWidth(const std::string& text); + int getTextWidth(std::vector *text); void write(SDL_Surface* surface, const char* text, int x, int y); void write(SDL_Surface* surface, const std::string& text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0); - void write(SDL_Surface* surface, vector *text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0); + void write(SDL_Surface* surface, std::vector *text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0); void write(Surface* surface, const std::string& text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0); private: diff --git a/src/sfontplus.cpp b/src/sfontplus.cpp index 8ac44bb..4059e6f 100644 --- a/src/sfontplus.cpp +++ b/src/sfontplus.cpp @@ -1,11 +1,7 @@ #include "sfontplus.h" - #include "imageio.h" #include -#include - -using namespace std; Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { assert(x>=0); @@ -49,7 +45,7 @@ SFontPlus::SFontPlus(SDL_Surface* font) { initFont(font); } -SFontPlus::SFontPlus(const string &font) { +SFontPlus::SFontPlus(const std::string &font) { surface = NULL; initFont(font); } @@ -63,7 +59,7 @@ bool SFontPlus::utf8Code(unsigned char c) { //return c>=194; } -void SFontPlus::initFont(const string &font, const string &characters) { +void SFontPlus::initFont(const std::string &font, const std::string &characters) { SDL_Surface *buf = loadPNG(font); if (buf!=NULL) { initFont( SDL_DisplayFormatAlpha(buf), characters ); @@ -71,7 +67,7 @@ void SFontPlus::initFont(const string &font, const string &characters) { } } -void SFontPlus::initFont(SDL_Surface *font, const string &characters) { +void SFontPlus::initFont(SDL_Surface *font, const std::string &characters) { freeFont(); this->characters = characters; if (font==NULL) return; @@ -79,22 +75,22 @@ void SFontPlus::initFont(SDL_Surface *font, const string &characters) { Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); #ifdef DEBUG bool utf8 = false; - for (uint x=0; x128; if (utf8) DEBUG("%d\n", (unsigned char)characters[x]); } #endif - uint c = 0; + unsigned c = 0; SDL_LockSurface(surface); - for (uint x=0; x<(uint)surface->w && cw && cw && getPixel(x,0) == pink) x++; + while (x<(unsigned)surface->w && getPixel(x,0) == pink) x++; charpos.push_back(x); //utf8 characters @@ -110,12 +106,12 @@ void SFontPlus::initFont(SDL_Surface *font, const string &characters) { SDL_UnlockSurface(surface); Uint32 colKey = getPixel(0,surface->h-1); SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey); - string::size_type pos = characters.find("0")*2; + std::string::size_type pos = characters.find("0")*2; SDL_Rect srcrect = {charpos[pos], 1, charpos[pos+2] - charpos[pos], surface->h - 1}; - uint y = srcrect.h; + unsigned y = srcrect.h; bool nonKeyFound = false; while (y-- > 0 && !nonKeyFound) { - uint x = srcrect.w; + unsigned x = srcrect.w; while (x-- > 0 && !nonKeyFound) nonKeyFound = getPixel(x+srcrect.x,y+srcrect.y) != colKey; } @@ -129,10 +125,10 @@ void SFontPlus::freeFont() { } } -void SFontPlus::write(SDL_Surface *s, const string &text, int x, int y) { +void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { if (text.empty()) return; - string::size_type pos; + std::string::size_type pos; SDL_Rect srcrect, dstrect; // these values won't change in the loop @@ -140,14 +136,14 @@ void SFontPlus::write(SDL_Surface *s, const string &text, int x, int y) { dstrect.y = y; srcrect.h = dstrect.h = surface->h-1; - for(uint i=0; iw; i++) { + for(unsigned i=0; iw; i++) { //Utf8 characters if (utf8Code(text[i]) && i+1h - 1; } -uint SFontPlus::getLineHeight() { +unsigned SFontPlus::getLineHeight() { return lineHeight; } diff --git a/src/sfontplus.h b/src/sfontplus.h index 9950dd6..7f6b0db 100644 --- a/src/sfontplus.h +++ b/src/sfontplus.h @@ -6,38 +6,33 @@ #include #define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" -#ifdef _WIN32 - typedef unsigned int uint; -#endif -using std::vector; -using std::string; class SFontPlus { private: Uint32 getPixel(Sint32 x, Sint32 y); SDL_Surface *surface; - vector charpos; - string characters; - uint height, lineHeight; + std::vector charpos; + std::string characters; + unsigned height, lineHeight; public: SFontPlus(); SFontPlus(SDL_Surface *font); - SFontPlus(const string &font); + SFontPlus(const std::string &font); ~SFontPlus(); bool utf8Code(unsigned char c); - void initFont(SDL_Surface *font, const string &characters = SFONTPLUS_CHARSET); - void initFont(const string &font, const string &characters = SFONTPLUS_CHARSET); + void initFont(SDL_Surface *font, const std::string &characters = SFONTPLUS_CHARSET); + void initFont(const std::string &font, const std::string &characters = SFONTPLUS_CHARSET); void freeFont(); - void write(SDL_Surface *s, const string &text, int x, int y); + void write(SDL_Surface *s, const std::string &text, int x, int y); - uint getTextWidth(const string &text); - uint getHeight(); - uint getLineHeight(); + unsigned getTextWidth(const std::string &text); + unsigned getHeight(); + unsigned getLineHeight(); }; #endif /* SFONTPLUS_H */ From a35a7e2c35274a06a7f17f12d90887161f203cf1 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Mon, 9 May 2011 05:17:25 +0200 Subject: [PATCH 06/27] Introduced enums for text alignment in the ASFont class. --- src/asfont.cpp | 30 +++++++++++++++++++----------- src/asfont.h | 15 ++++++--------- src/browsedialog.cpp | 2 +- src/dialog.cpp | 4 ++-- src/gmenu2x.cpp | 20 ++++++++++---------- src/iconbutton.cpp | 8 ++++---- src/iconbutton.h | 7 +++++-- src/inputdialog.cpp | 8 ++++---- src/link.cpp | 4 ++-- src/linkapp.cpp | 4 ++-- src/menusetting.cpp | 2 +- src/menusettingbool.cpp | 2 +- src/menusettingint.cpp | 2 +- src/menusettingrgba.cpp | 8 ++++---- src/menusettingstringbase.cpp | 2 +- src/messagebox.cpp | 2 +- src/selector.cpp | 4 ++-- src/surface.cpp | 18 +++++++++--------- src/surface.h | 6 ++++-- src/textmanualdialog.cpp | 2 +- src/wallpaperdialog.cpp | 2 +- 21 files changed, 81 insertions(+), 71 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 0518a1d..46eb776 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -32,33 +32,39 @@ void ASFont::write(SDL_Surface* surface, const char* text, int x, int y) { font.write(surface, text, x, y); } -void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, const unsigned short halign, const unsigned short valign) { +void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { switch (halign) { - case SFontHAlignCenter: + case HAlignLeft: + break; + case HAlignCenter: x -= getTextWidth(text)/2; break; - case SFontHAlignRight: + case HAlignRight: x -= getTextWidth(text); break; } switch (valign) { - case SFontVAlignMiddle: + case VAlignTop: + break; + case VAlignMiddle: y -= getHalfHeight(); break; - case SFontVAlignBottom: + case VAlignBottom: y -= getHeight(); break; } font.write(surface, text, x, y); } -void ASFont::write(SDL_Surface* surface, std::vector *text, int x, int y, const unsigned short halign, const unsigned short valign) { +void ASFont::write(SDL_Surface* surface, std::vector *text, int x, int y, HAlign halign, VAlign valign) { switch (valign) { - case SFontVAlignMiddle: + case VAlignTop: + break; + case VAlignMiddle: y -= getHalfHeight()*text->size(); break; - case SFontVAlignBottom: + case VAlignBottom: y -= getHeight()*text->size(); break; } @@ -66,10 +72,12 @@ void ASFont::write(SDL_Surface* surface, std::vector *text, int x, for (unsigned i=0; isize(); i++) { int ix = x; switch (halign) { - case SFontHAlignCenter: + case HAlignLeft: + break; + case HAlignCenter: ix -= getTextWidth(text->at(i))/2; break; - case SFontHAlignRight: + case HAlignRight: ix -= getTextWidth(text->at(i)); break; } @@ -78,7 +86,7 @@ void ASFont::write(SDL_Surface* surface, std::vector *text, int x, } } -void ASFont::write(Surface* surface, const std::string& text, int x, int y, const unsigned short halign, const unsigned short valign) { +void ASFont::write(Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { if (text.find("\n", 0) != std::string::npos) { std::vector textArr; split(textArr,text,"\n"); diff --git a/src/asfont.h b/src/asfont.h index f70f0c1..4ff54ff 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -11,15 +11,12 @@ struct SDL_Surface; class Surface; -const unsigned short SFontHAlignLeft = 0; -const unsigned short SFontHAlignRight = 1; -const unsigned short SFontHAlignCenter = 2; -const unsigned short SFontVAlignTop = 0; -const unsigned short SFontVAlignBottom = 1; -const unsigned short SFontVAlignMiddle = 2; class ASFont { public: + enum HAlign { HAlignLeft, HAlignRight, HAlignCenter }; + enum VAlign { VAlignTop, VAlignBottom, VAlignMiddle }; + ASFont(SDL_Surface* font); ASFont(Surface* font); ASFont(const std::string &font); @@ -35,9 +32,9 @@ public: int getTextWidth(const std::string& text); int getTextWidth(std::vector *text); void write(SDL_Surface* surface, const char* text, int x, int y); - void write(SDL_Surface* surface, const std::string& text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0); - void write(SDL_Surface* surface, std::vector *text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0); - void write(Surface* surface, const std::string& text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0); + void write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); + void write(SDL_Surface* surface, std::vector *text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); + void write(Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); private: SFontPlus font; diff --git a/src/browsedialog.cpp b/src/browsedialog.cpp index 539733e..09e857c 100644 --- a/src/browsedialog.cpp +++ b/src/browsedialog.cpp @@ -260,7 +260,7 @@ void BrowseDialog::paint() icon = iconFile; } icon->blit(gmenu2x->s, 5, offsetY); - gmenu2x->s->write(gmenu2x->font, (*fl)[i], 24, offsetY + 8, SFontHAlignLeft, SFontVAlignMiddle); + gmenu2x->s->write(gmenu2x->font, (*fl)[i], 24, offsetY + 8, ASFont::HAlignLeft, ASFont::VAlignMiddle); if (gmenu2x->f200 && gmenu2x->ts.pressed() && gmenu2x->ts.inRect(touchRect.x, offsetY + 3, touchRect.w, rowHeight)) { ts_pressed = true; diff --git a/src/dialog.cpp b/src/dialog.cpp index 5991574..5749532 100644 --- a/src/dialog.cpp +++ b/src/dialog.cpp @@ -31,14 +31,14 @@ void Dialog::writeTitle(const std::string &title, Surface *s) { if (s==NULL) s = gmenu2x->s; - s->write(gmenu2x->font, title, 40, gmenu2x->skinConfInt["topBarHeight"]/4, SFontHAlignLeft, SFontVAlignMiddle); + s->write(gmenu2x->font, title, 40, gmenu2x->skinConfInt["topBarHeight"]/4, ASFont::HAlignLeft, ASFont::VAlignMiddle); } void Dialog::writeSubTitle(const std::string &subtitle, Surface *s) { if (s==NULL) s = gmenu2x->s; - s->write(gmenu2x->font, subtitle, 40, gmenu2x->skinConfInt["topBarHeight"]/4*3, SFontHAlignLeft, SFontVAlignMiddle); + s->write(gmenu2x->font, subtitle, 40, gmenu2x->skinConfInt["topBarHeight"]/4*3, ASFont::HAlignLeft, ASFont::VAlignMiddle); } diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 18e0810..2609b07 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -374,7 +374,7 @@ void GMenu2X::initBG() { string df = getDiskFree(); sd.blit( sc["bgmain"], 3, bottomBarIconY ); - sc["bgmain"]->write( font, df, 22, bottomBarTextY, SFontHAlignLeft, SFontVAlignMiddle ); + sc["bgmain"]->write( font, df, 22, bottomBarTextY, ASFont::HAlignLeft, ASFont::VAlignMiddle ); volumeX = 27+font->getTextWidth(df); volume.blit( sc["bgmain"], volumeX, bottomBarIconY ); volumeX += 19; @@ -845,7 +845,7 @@ void GMenu2X::main() { sc[sectionIcon]->blit(s,x-16,sectionLinkPadding,32,32); else sc.skinRes("icons/section.png")->blit(s,x-16,sectionLinkPadding); - s->write( font, menu->getSections()[i], x, skinConfInt["topBarHeight"]-sectionLinkPadding, SFontHAlignCenter, SFontVAlignBottom ); + s->write( font, menu->getSections()[i], x, skinConfInt["topBarHeight"]-sectionLinkPadding, ASFont::HAlignCenter, ASFont::VAlignBottom ); } //Links @@ -874,10 +874,10 @@ void GMenu2X::main() { */ if (menu->selLink()!=NULL) { - s->write ( font, menu->selLink()->getDescription(), halfX, resY-19, SFontHAlignCenter, SFontVAlignBottom ); + s->write ( font, menu->selLink()->getDescription(), halfX, resY-19, ASFont::HAlignCenter, ASFont::VAlignBottom ); if (menu->selLinkApp()!=NULL) { - s->write ( font, menu->selLinkApp()->clockStr(confInt["maxClock"]), cpuX, bottomBarTextY, SFontHAlignLeft, SFontVAlignMiddle ); - s->write ( font, menu->selLinkApp()->volumeStr(), volumeX, bottomBarTextY, SFontHAlignLeft, SFontVAlignMiddle ); + s->write ( font, menu->selLinkApp()->clockStr(confInt["maxClock"]), cpuX, bottomBarTextY, ASFont::HAlignLeft, ASFont::VAlignMiddle ); + s->write ( font, menu->selLinkApp()->volumeStr(), volumeX, bottomBarTextY, ASFont::HAlignLeft, ASFont::VAlignMiddle ); //Manual indicator if (!menu->selLinkApp()->getManual().empty()) sc.skinRes("imgs/manual.png")->blit(s,manualX,bottomBarIconY); @@ -931,7 +931,7 @@ void GMenu2X::main() { tickFPS = tickNow; drawn_frames = 0; } - s->write( font, fps+" FPS", resX-1,1 ,SFontHAlignRight ); + s->write( font, fps+" FPS", resX-1,1 ,ASFont::HAlignRight ); #endif s->flip(); @@ -1371,7 +1371,7 @@ void GMenu2X::contextMenu() { //draw selection rect s->box( selbox.x, selbox.y, selbox.w, selbox.h, skinConfColors[COLOR_MESSAGE_BOX_SELECTION] ); for (i=0; iwrite( font, voices[i].text, box.x+12, box.y+h2+5+(h+2)*i, SFontHAlignLeft, SFontVAlignMiddle ); + s->write( font, voices[i].text, box.x+12, box.y+h2+5+(h+2)*i, ASFont::HAlignLeft, ASFont::VAlignMiddle ); s->flip(); //touchscreen @@ -1647,7 +1647,7 @@ void GMenu2X::scanner() { Surface scanbg(bg); drawButton(&scanbg, "x", tr["Exit"], drawButton(&scanbg, "b", "", 5)-10); - scanbg.write(font,tr["Link Scanner"],halfX,7,SFontHAlignCenter,SFontVAlignMiddle); + scanbg.write(font,tr["Link Scanner"],halfX,7,ASFont::HAlignCenter,ASFont::VAlignMiddle); uint lineY = 42; @@ -1993,7 +1993,7 @@ int GMenu2X::drawButton(Surface *s, const string &btn, const string &text, int x if (sc.skinRes("imgs/buttons/"+btn+".png") != NULL) { sc["imgs/buttons/"+btn+".png"]->blit(s, x, y-7); re.w = sc["imgs/buttons/"+btn+".png"]->raw->w+3; - s->write(font, text, x+re.w, y, SFontHAlignLeft, SFontVAlignMiddle); + s->write(font, text, x+re.w, y, ASFont::HAlignLeft, ASFont::VAlignMiddle); re.w += font->getTextWidth(text); } return x+re.w+6; @@ -2005,7 +2005,7 @@ int GMenu2X::drawButtonRight(Surface *s, const string &btn, const string &text, x -= 16; sc["imgs/buttons/"+btn+".png"]->blit(s, x, y-7); x -= 3; - s->write(font, text, x, y, SFontHAlignRight, SFontVAlignMiddle); + s->write(font, text, x, y, ASFont::HAlignRight, ASFont::VAlignMiddle); return x-6-font->getTextWidth(text); } return x-6; diff --git a/src/iconbutton.cpp b/src/iconbutton.cpp index 80b8581..972ad26 100644 --- a/src/iconbutton.cpp +++ b/src/iconbutton.cpp @@ -71,13 +71,13 @@ void IconButton::recalcSize() { if (labelPosition == IconButton::DISP_LEFT || labelPosition == IconButton::DISP_RIGHT) { w += margin + labelRect.w; //if (labelRect.h > h) h = labelRect.h; - labelHAlign = SFontHAlignLeft; - labelVAlign = SFontVAlignMiddle; + labelHAlign = ASFont::HAlignLeft; + labelVAlign = ASFont::VAlignMiddle; } else { h += margin + labelRect.h; //if (labelRect.w > w) w = labelRect.w; - labelHAlign = SFontHAlignCenter; - labelVAlign = SFontVAlignTop; + labelHAlign = ASFont::HAlignCenter; + labelVAlign = ASFont::VAlignTop; } switch (labelPosition) { diff --git a/src/iconbutton.h b/src/iconbutton.h index 4c094f7..abbea9a 100644 --- a/src/iconbutton.h +++ b/src/iconbutton.h @@ -1,8 +1,10 @@ #ifndef ICONBUTTON_H #define ICONBUTTON_H -#include #include "button.h" +#include "asfont.h" + +#include using std::string; @@ -14,7 +16,8 @@ protected: GMenu2X *gmenu2x; string icon, label; int labelPosition, labelMargin; - unsigned short labelHAlign, labelVAlign; + ASFont::HAlign labelHAlign; + ASFont::VAlign labelVAlign; void recalcSize(); SDL_Rect iconRect, labelRect; diff --git a/src/inputdialog.cpp b/src/inputdialog.cpp index e74aca6..38e451f 100644 --- a/src/inputdialog.cpp +++ b/src/inputdialog.cpp @@ -150,7 +150,7 @@ bool InputDialog::exec() { gmenu2x->skinConfColors[COLOR_SELECTION_BG]); gmenu2x->s->rectangle(box.x, box.y, box.w, box.h, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); - gmenu2x->s->write(gmenu2x->font, input, box.x+5, box.y+box.h-2, SFontHAlignLeft, SFontVAlignBottom); + gmenu2x->s->write(gmenu2x->font, input, box.x+5, box.y+box.h-2, ASFont::HAlignLeft, ASFont::VAlignBottom); curTick = SDL_GetTicks(); if (curTick-caretTick>=600) { @@ -317,7 +317,7 @@ int InputDialog::drawVirtualKeyboard() { } gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); - gmenu2x->s->write(gmenu2x->font, charX, kbLeft+xc*KEY_WIDTH+KEY_WIDTH/2-1, KB_TOP+l*KEY_HEIGHT+KEY_HEIGHT/2, SFontHAlignCenter, SFontVAlignMiddle); + gmenu2x->s->write(gmenu2x->font, charX, kbLeft+xc*KEY_WIDTH+KEY_WIDTH/2-1, KB_TOP+l*KEY_HEIGHT+KEY_HEIGHT/2, ASFont::HAlignCenter, ASFont::VAlignMiddle); xc++; } } @@ -329,7 +329,7 @@ int InputDialog::drawVirtualKeyboard() { selCol = 0; selRow = kb->size(); } - gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["Cancel"], (int)(160-kbLength*KEY_WIDTH/4), KB_TOP+kb->size()*KEY_HEIGHT+KEY_HEIGHT/2, SFontHAlignCenter, SFontVAlignMiddle); + gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["Cancel"], (int)(160-kbLength*KEY_WIDTH/4), KB_TOP+kb->size()*KEY_HEIGHT+KEY_HEIGHT/2, ASFont::HAlignCenter, ASFont::VAlignMiddle); re.x = kbLeft+kbLength*KEY_WIDTH/2-1; gmenu2x->s->rectangle(re, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); @@ -337,7 +337,7 @@ int InputDialog::drawVirtualKeyboard() { selCol = 1; selRow = kb->size(); } - gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["OK"], (int)(160+kbLength*KEY_WIDTH/4), KB_TOP+kb->size()*KEY_HEIGHT+KEY_HEIGHT/2, SFontHAlignCenter, SFontVAlignMiddle); + gmenu2x->s->write(gmenu2x->font, gmenu2x->tr["OK"], (int)(160+kbLength*KEY_WIDTH/4), KB_TOP+kb->size()*KEY_HEIGHT+KEY_HEIGHT/2, ASFont::HAlignCenter, ASFont::VAlignMiddle); //if ts released if (gmenu2x->f200 && ts.released() && ts.inRect(kbRect)) { diff --git a/src/link.cpp b/src/link.cpp index 468e4b9..309fc3b 100644 --- a/src/link.cpp +++ b/src/link.cpp @@ -43,12 +43,12 @@ void Link::run() {} void Link::paint() { iconSurface->blit(gmenu2x->s, iconX, rect.y+padding, 32,32); - gmenu2x->s->write( gmenu2x->font, getTitle(), iconX+16, rect.y+gmenu2x->skinConfInt["linkHeight"]-padding, SFontHAlignCenter, SFontVAlignBottom ); + gmenu2x->s->write( gmenu2x->font, getTitle(), iconX+16, rect.y+gmenu2x->skinConfInt["linkHeight"]-padding, ASFont::HAlignCenter, ASFont::VAlignBottom ); } bool Link::paintHover() { if (gmenu2x->useSelectionPng) - gmenu2x->sc["imgs/selection.png"]->blit(gmenu2x->s,rect,SFontHAlignCenter,SFontVAlignMiddle); + gmenu2x->sc["imgs/selection.png"]->blit(gmenu2x->s,rect,ASFont::HAlignCenter,ASFont::VAlignMiddle); else gmenu2x->s->box(rect.x, rect.y, rect.w, rect.h, gmenu2x->skinConfColors[COLOR_SELECTION_BG]); return true; diff --git a/src/linkapp.cpp b/src/linkapp.cpp index 6d8e060..95f4d28 100644 --- a/src/linkapp.cpp +++ b/src/linkapp.cpp @@ -280,7 +280,7 @@ void LinkApp::drawRun() { else gmenu2x->sc["icons/generic.png"]->blit(gmenu2x->s,x,104);*/ gmenu2x->sc[getIconPath()]->blit(gmenu2x->s,x,gmenu2x->halfY-16); - gmenu2x->s->write( gmenu2x->font, text, x+42, gmenu2x->halfY+1, SFontHAlignLeft, SFontVAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, text, x+42, gmenu2x->halfY+1, ASFont::HAlignLeft, ASFont::VAlignMiddle ); gmenu2x->s->flip(); } @@ -329,7 +329,7 @@ void LinkApp::showManual() { ss << page+1; ss >> pageStatus; pageStatus = gmenu2x->tr["Page"]+": "+pageStatus+"/"+spagecount; - gmenu2x->s->write(gmenu2x->font, pageStatus, 310, 230, SFontHAlignRight, SFontVAlignMiddle); + gmenu2x->s->write(gmenu2x->font, pageStatus, 310, 230, ASFont::HAlignRight, ASFont::VAlignMiddle); gmenu2x->s->flip(); repaint = false; diff --git a/src/menusetting.cpp b/src/menusetting.cpp index 5cdc54c..1cb490b 100644 --- a/src/menusetting.cpp +++ b/src/menusetting.cpp @@ -36,7 +36,7 @@ MenuSetting::~MenuSetting() void MenuSetting::draw(int y) { - gmenu2x->s->write( gmenu2x->font, name, 5, y+gmenu2x->font->getHalfHeight(), SFontHAlignLeft, SFontVAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, name, 5, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); } void MenuSetting::handleTS() diff --git a/src/menusettingbool.cpp b/src/menusettingbool.cpp index 5d2fdff..68ddc9b 100644 --- a/src/menusettingbool.cpp +++ b/src/menusettingbool.cpp @@ -61,7 +61,7 @@ void MenuSettingBool::initButton() void MenuSettingBool::draw(int y) { MenuSetting::draw(y); - gmenu2x->s->write( gmenu2x->font, strvalue, 155, y+gmenu2x->font->getHalfHeight(), SFontHAlignLeft, SFontVAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, strvalue, 155, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); } void MenuSettingBool::manageInput(bevent_t *event) diff --git a/src/menusettingint.cpp b/src/menusettingint.cpp index 219f581..f3c8541 100644 --- a/src/menusettingint.cpp +++ b/src/menusettingint.cpp @@ -62,7 +62,7 @@ MenuSettingInt::MenuSettingInt(GMenu2X *gmenu2x, const string &name, const strin void MenuSettingInt::draw(int y) { MenuSetting::draw(y); - gmenu2x->s->write( gmenu2x->font, strvalue, 155, y+gmenu2x->font->getHalfHeight(), SFontHAlignLeft, SFontVAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, strvalue, 155, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); } void MenuSettingInt::manageInput(bevent_t *event) diff --git a/src/menusettingrgba.cpp b/src/menusettingrgba.cpp index e940d20..477200a 100644 --- a/src/menusettingrgba.cpp +++ b/src/menusettingrgba.cpp @@ -63,10 +63,10 @@ void MenuSettingRGBA::draw(int y) { MenuSetting::draw(y); gmenu2x->s->rectangle( 153, y+1, 11, 11, 0,0,0,255 ); gmenu2x->s->box( 154, y+2, 9, 9, value() ); - gmenu2x->s->write( gmenu2x->font, "R: "+strR, 169, y+gmenu2x->font->getHalfHeight(), SFontHAlignLeft, SFontVAlignMiddle ); - gmenu2x->s->write( gmenu2x->font, "G: "+strG, 205, y+gmenu2x->font->getHalfHeight(), SFontHAlignLeft, SFontVAlignMiddle ); - gmenu2x->s->write( gmenu2x->font, "B: "+strB, 241, y+gmenu2x->font->getHalfHeight(), SFontHAlignLeft, SFontVAlignMiddle ); - gmenu2x->s->write( gmenu2x->font, "A: "+strA, 277, y+gmenu2x->font->getHalfHeight(), SFontHAlignLeft, SFontVAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, "R: "+strR, 169, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, "G: "+strG, 205, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, "B: "+strB, 241, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, "A: "+strA, 277, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); } void MenuSettingRGBA::handleTS() { diff --git a/src/menusettingstringbase.cpp b/src/menusettingstringbase.cpp index aaab5c0..fda54c1 100644 --- a/src/menusettingstringbase.cpp +++ b/src/menusettingstringbase.cpp @@ -41,7 +41,7 @@ void MenuSettingStringBase::draw(int y) gmenu2x->s->write( gmenu2x->font, value(), 155, y + gmenu2x->font->getHalfHeight(), - SFontHAlignLeft, SFontVAlignMiddle); + ASFont::HAlignLeft, ASFont::VAlignMiddle); } void MenuSettingStringBase::manageInput(bevent_t *event) diff --git a/src/messagebox.cpp b/src/messagebox.cpp index 95cebce..d6e15a4 100644 --- a/src/messagebox.cpp +++ b/src/messagebox.cpp @@ -84,7 +84,7 @@ int MessageBox::exec() { //icon+text if (gmenu2x->sc[icon] != NULL) gmenu2x->sc[icon]->blitCenter( &bg, box.x+25, box.y+gmenu2x->font->getHeight()+3 ); - bg.write( gmenu2x->font, text, box.x+(gmenu2x->sc[icon] != NULL ? 47 : 10), box.y+gmenu2x->font->getHeight()+3, SFontHAlignLeft, SFontVAlignMiddle ); + bg.write( gmenu2x->font, text, box.x+(gmenu2x->sc[icon] != NULL ? 47 : 10), box.y+gmenu2x->font->getHeight()+3, ASFont::HAlignLeft, ASFont::VAlignMiddle ); int btnX = gmenu2x->halfX+box.w/2-6; for (uint i=0; isc["imgs/folder.png"]->blit(gmenu2x->s, 4, 42+(iY*16)); - gmenu2x->s->write(gmenu2x->font, fl[i], 21, 49+(iY*16), SFontHAlignLeft, SFontVAlignMiddle); + gmenu2x->s->write(gmenu2x->font, fl[i], 21, 49+(iY*16), ASFont::HAlignLeft, ASFont::VAlignMiddle); } else - gmenu2x->s->write(gmenu2x->font, titles[i-fl.dirCount()], 4, 49+(iY*16), SFontHAlignLeft, SFontVAlignMiddle); + gmenu2x->s->write(gmenu2x->font, titles[i-fl.dirCount()], 4, 49+(iY*16), ASFont::HAlignLeft, ASFont::VAlignMiddle); } gmenu2x->s->clearClipRect(); diff --git a/src/surface.cpp b/src/surface.cpp index e5dc46e..23436c0 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -267,10 +267,6 @@ void Surface::blendAdd(Surface *target, int x, int y) { */ } -void Surface::write(ASFont *font, const string &text, int x, int y, const unsigned short halign, const unsigned short valign) { - font->write(this,text,x,y,halign,valign); -} - void Surface::operator = (SDL_Surface *s) { raw = SDL_DisplayFormat(s); halfW = raw->w/2; @@ -340,21 +336,25 @@ void Surface::setClipRect(SDL_Rect rect) { SDL_SetClipRect(raw,&rect); } -bool Surface::blit(Surface *destination, SDL_Rect container, const unsigned short halign, const unsigned short valign) { +bool Surface::blit(Surface *destination, SDL_Rect container, ASFont::HAlign halign, ASFont::VAlign valign) { switch (halign) { - case SFontHAlignCenter: + case ASFont::HAlignLeft: + break; + case ASFont::HAlignCenter: container.x += container.w/2-halfW; break; - case SFontHAlignRight: + case ASFont::HAlignRight: container.x += container.w-raw->w; break; } switch (valign) { - case SFontVAlignMiddle: + case ASFont::VAlignTop: + break; + case ASFont::VAlignMiddle: container.y += container.h/2-halfH; break; - case SFontVAlignBottom: + case ASFont::VAlignBottom: container.y += container.h-raw->h; break; } diff --git a/src/surface.h b/src/surface.h index 2fdb047..c4bd187 100644 --- a/src/surface.h +++ b/src/surface.h @@ -75,14 +75,16 @@ public: void setClipRect(SDL_Rect rect); bool blit(Surface *destination, int x, int y, int w=0, int h=0, int a=-1); - bool blit(Surface *destination, SDL_Rect container, const unsigned short halign=0, const unsigned short valign=0); + bool blit(Surface *destination, SDL_Rect container, ASFont::HAlign halign = ASFont::HAlignLeft, ASFont::VAlign valign = ASFont::VAlignTop); bool blit(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); bool blitCenter(Surface *destination, int x, int y, int w=0, int h=0, int a=-1); bool blitCenter(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); bool blitRight(Surface *destination, int x, int y, int w=0, int h=0, int a=-1); bool blitRight(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); - void write(ASFont *font, const string &text, int x, int y, const unsigned short halign=0, const unsigned short valign=0); + void write(ASFont *font, const string &text, int x, int y, ASFont::HAlign halign = ASFont::HAlignLeft, ASFont::VAlign valign = ASFont::VAlignTop) { + font->write(this, text, x, y, halign, valign); + } int box(Sint16, Sint16, Sint16, Sint16, Uint8, Uint8, Uint8, Uint8); int box(Sint16, Sint16, Sint16, Sint16, Uint8, Uint8, Uint8); diff --git a/src/textmanualdialog.cpp b/src/textmanualdialog.cpp index 2bceb4f..4fb4a3f 100644 --- a/src/textmanualdialog.cpp +++ b/src/textmanualdialog.cpp @@ -96,7 +96,7 @@ void TextManualDialog::exec() { ss << page+1; ss >> pageStatus; pageStatus = gmenu2x->tr["Page"]+": "+pageStatus+"/"+spagecount; - gmenu2x->s->write(gmenu2x->font, pageStatus, 310, 230, SFontHAlignRight, SFontVAlignMiddle); + gmenu2x->s->write(gmenu2x->font, pageStatus, 310, 230, ASFont::HAlignRight, ASFont::VAlignMiddle); gmenu2x->s->flip(); diff --git a/src/wallpaperdialog.cpp b/src/wallpaperdialog.cpp index 91d73ea..27cb6e6 100644 --- a/src/wallpaperdialog.cpp +++ b/src/wallpaperdialog.cpp @@ -80,7 +80,7 @@ bool WallpaperDialog::exec() gmenu2x->s->setClipRect(0,41,311,179); for (i=firstElement; is->write(gmenu2x->font, wallpapers[i], 5, 52+(iY*17), SFontHAlignLeft, SFontVAlignMiddle); + gmenu2x->s->write(gmenu2x->font, wallpapers[i], 5, 52+(iY*17), ASFont::HAlignLeft, ASFont::VAlignMiddle); } gmenu2x->s->clearClipRect(); From ec5d426d83eefa78f4a696cb0392d90a126b9eac Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Mon, 9 May 2011 14:25:16 +0200 Subject: [PATCH 07/27] ASFont: moved SFontPlus code into ASFont source files. This is in preparation of merging the classes. --- src/Makefile.am | 4 +- src/asfont.cpp | 191 +++++++++++++++++++++++++++++++++++++++++++++ src/asfont.h | 38 ++++++++- src/gmenu2x.cpp | 1 - src/sfontplus.cpp | 192 ---------------------------------------------- src/sfontplus.h | 38 --------- 6 files changed, 227 insertions(+), 237 deletions(-) delete mode 100644 src/sfontplus.cpp delete mode 100644 src/sfontplus.h diff --git a/src/Makefile.am b/src/Makefile.am index d0b5716..ee16c15 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,7 @@ gmenu2x_SOURCES = asfont.cpp button.cpp cpu.cpp dirdialog.cpp filedialog.cpp \ menusettingmultistring.cpp menusettingrgba.cpp menusettingstring.cpp \ menusettingstringbase.cpp \ messagebox.cpp selector.cpp \ - settingsdialog.cpp sfontplus.cpp surfacecollection.cpp surface.cpp \ + settingsdialog.cpp surfacecollection.cpp surface.cpp \ textdialog.cpp textmanualdialog.cpp touchscreen.cpp translator.cpp \ utilities.cpp wallpaperdialog.cpp \ browsedialog.cpp buttonbox.cpp dialog.cpp \ @@ -22,7 +22,7 @@ noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \ menusettingmultistring.h menusettingrgba.h menusettingstring.h \ menusettingstringbase.h \ messagebox.h selector.h settingsdialog.h \ - sfontplus.h surfacecollection.h surface.h textdialog.h textmanualdialog.h \ + surfacecollection.h surface.h textdialog.h textmanualdialog.h \ touchscreen.h translator.h utilities.h wallpaperdialog.h \ browsedialog.h buttonbox.h dialog.h \ imageio.h diff --git a/src/asfont.cpp b/src/asfont.cpp index 46eb776..7a8ff4d 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -1,7 +1,198 @@ #include "asfont.h" +#include "imageio.h" #include "surface.h" #include "utilities.h" +#include + +Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { + assert(x>=0); + assert(xw); + assert(y>=0); + assert(yh); + + Uint32 Bpp = surface->format->BytesPerPixel; + + // Get the pixel + switch(Bpp) { + case 1: + return *((Uint8 *)surface->pixels + y * surface->pitch + x); + break; + case 2: + return *((Uint16 *)surface->pixels + y * surface->pitch/2 + x); + break; + case 3: { // Format/endian independent + Uint8 *bits = ((Uint8 *)surface->pixels)+y*surface->pitch+x*Bpp; + Uint8 r, g, b; + r = *((bits)+surface->format->Rshift/8); + g = *((bits)+surface->format->Gshift/8); + b = *((bits)+surface->format->Bshift/8); + return SDL_MapRGB(surface->format, r, g, b); + } + break; + case 4: + return *((Uint32 *)surface->pixels + y * surface->pitch/4 + x); + break; + } + + return 0; +} + +SFontPlus::SFontPlus() { + surface = NULL; +} + +SFontPlus::SFontPlus(SDL_Surface* font) { + surface = NULL; + initFont(font); +} + +SFontPlus::SFontPlus(const std::string &font) { + surface = NULL; + initFont(font); +} + +SFontPlus::~SFontPlus() { + freeFont(); +} + +bool SFontPlus::utf8Code(unsigned char c) { + return (c>=194 && c<=198) || c==208 || c==209; + //return c>=194; +} + +void SFontPlus::initFont(const std::string &font, const std::string &characters) { + SDL_Surface *buf = loadPNG(font); + if (buf!=NULL) { + initFont( SDL_DisplayFormatAlpha(buf), characters ); + SDL_FreeSurface(buf); + } +} + +void SFontPlus::initFont(SDL_Surface *font, const std::string &characters) { + freeFont(); + this->characters = characters; + if (font==NULL) return; + surface = font; + Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); +#ifdef DEBUG + bool utf8 = false; + for (unsigned x=0; x128; + if (utf8) DEBUG("%d\n", (unsigned char)characters[x]); + } +#endif + + unsigned c = 0; + + SDL_LockSurface(surface); + for (unsigned x=0; x<(unsigned)surface->w && cw && getPixel(x,0) == pink) x++; + charpos.push_back(x); + + //utf8 characters + if (c>0 && utf8Code(characters[c-1])) { + charpos.push_back(startx); + charpos.push_back(x); + c++; + } + + c++; + } + } + SDL_UnlockSurface(surface); + Uint32 colKey = getPixel(0,surface->h-1); + SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey); + std::string::size_type pos = characters.find("0")*2; + SDL_Rect srcrect = {charpos[pos], 1, charpos[pos+2] - charpos[pos], surface->h - 1}; + unsigned y = srcrect.h; + bool nonKeyFound = false; + while (y-- > 0 && !nonKeyFound) { + unsigned x = srcrect.w; + while (x-- > 0 && !nonKeyFound) + nonKeyFound = getPixel(x+srcrect.x,y+srcrect.y) != colKey; + } + lineHeight = y+1; +} + +void SFontPlus::freeFont() { + if (surface!=NULL) { + SDL_FreeSurface(surface); + surface = NULL; + } +} + +void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { + if (text.empty()) return; + + std::string::size_type pos; + SDL_Rect srcrect, dstrect; + + // these values won't change in the loop + srcrect.y = 1; + dstrect.y = y; + srcrect.h = dstrect.h = surface->h-1; + + for(unsigned i=0; iw; i++) { + //Utf8 characters + if (utf8Code(text[i]) && i+1h - 1; +} + +unsigned SFontPlus::getLineHeight() { + return lineHeight; +} + ASFont::ASFont(SDL_Surface* font) { this->font.initFont(font); halfHeight = getHeight()/2; diff --git a/src/asfont.h b/src/asfont.h index 4ff54ff..ce54ffd 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -1,16 +1,46 @@ -//Advanced SFont by Massimiliano Torromeo (cpp wrapper around SFont) +// Based on SFont by Karl Bartel. +// Adapted to C++ by Massimiliano Torromeo. +// Refactored by Maarten ter Huurne and several others (see git log). +// License: GPL version 2 or later. #ifndef ASFONT_H #define ASFONT_H -#include "sfontplus.h" - +#include #include #include -struct SDL_Surface; class Surface; +#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" + +class SFontPlus { +private: + Uint32 getPixel(Sint32 x, Sint32 y); + + SDL_Surface *surface; + std::vector charpos; + std::string characters; + unsigned height, lineHeight; + +public: + SFontPlus(); + SFontPlus(SDL_Surface *font); + SFontPlus(const std::string &font); + ~SFontPlus(); + + bool utf8Code(unsigned char c); + + void initFont(SDL_Surface *font, const std::string &characters = SFONTPLUS_CHARSET); + void initFont(const std::string &font, const std::string &characters = SFONTPLUS_CHARSET); + void freeFont(); + + void write(SDL_Surface *s, const std::string &text, int x, int y); + + unsigned getTextWidth(const std::string &text); + unsigned getHeight(); + unsigned getLineHeight(); +}; class ASFont { public: diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 2609b07..0870a40 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -48,7 +48,6 @@ #include "linkaction.h" #include "menu.h" #include "asfont.h" -#include "sfontplus.h" #include "surface.h" #include "filedialog.h" #include "gmenu2x.h" diff --git a/src/sfontplus.cpp b/src/sfontplus.cpp deleted file mode 100644 index 4059e6f..0000000 --- a/src/sfontplus.cpp +++ /dev/null @@ -1,192 +0,0 @@ -#include "sfontplus.h" -#include "imageio.h" - -#include - -Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { - assert(x>=0); - assert(xw); - assert(y>=0); - assert(yh); - - Uint32 Bpp = surface->format->BytesPerPixel; - - // Get the pixel - switch(Bpp) { - case 1: - return *((Uint8 *)surface->pixels + y * surface->pitch + x); - break; - case 2: - return *((Uint16 *)surface->pixels + y * surface->pitch/2 + x); - break; - case 3: { // Format/endian independent - Uint8 *bits = ((Uint8 *)surface->pixels)+y*surface->pitch+x*Bpp; - Uint8 r, g, b; - r = *((bits)+surface->format->Rshift/8); - g = *((bits)+surface->format->Gshift/8); - b = *((bits)+surface->format->Bshift/8); - return SDL_MapRGB(surface->format, r, g, b); - } - break; - case 4: - return *((Uint32 *)surface->pixels + y * surface->pitch/4 + x); - break; - } - - return 0; -} - -SFontPlus::SFontPlus() { - surface = NULL; -} - -SFontPlus::SFontPlus(SDL_Surface* font) { - surface = NULL; - initFont(font); -} - -SFontPlus::SFontPlus(const std::string &font) { - surface = NULL; - initFont(font); -} - -SFontPlus::~SFontPlus() { - freeFont(); -} - -bool SFontPlus::utf8Code(unsigned char c) { - return (c>=194 && c<=198) || c==208 || c==209; - //return c>=194; -} - -void SFontPlus::initFont(const std::string &font, const std::string &characters) { - SDL_Surface *buf = loadPNG(font); - if (buf!=NULL) { - initFont( SDL_DisplayFormatAlpha(buf), characters ); - SDL_FreeSurface(buf); - } -} - -void SFontPlus::initFont(SDL_Surface *font, const std::string &characters) { - freeFont(); - this->characters = characters; - if (font==NULL) return; - surface = font; - Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); -#ifdef DEBUG - bool utf8 = false; - for (unsigned x=0; x128; - if (utf8) DEBUG("%d\n", (unsigned char)characters[x]); - } -#endif - - unsigned c = 0; - - SDL_LockSurface(surface); - for (unsigned x=0; x<(unsigned)surface->w && cw && getPixel(x,0) == pink) x++; - charpos.push_back(x); - - //utf8 characters - if (c>0 && utf8Code(characters[c-1])) { - charpos.push_back(startx); - charpos.push_back(x); - c++; - } - - c++; - } - } - SDL_UnlockSurface(surface); - Uint32 colKey = getPixel(0,surface->h-1); - SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey); - std::string::size_type pos = characters.find("0")*2; - SDL_Rect srcrect = {charpos[pos], 1, charpos[pos+2] - charpos[pos], surface->h - 1}; - unsigned y = srcrect.h; - bool nonKeyFound = false; - while (y-- > 0 && !nonKeyFound) { - unsigned x = srcrect.w; - while (x-- > 0 && !nonKeyFound) - nonKeyFound = getPixel(x+srcrect.x,y+srcrect.y) != colKey; - } - lineHeight = y+1; -} - -void SFontPlus::freeFont() { - if (surface!=NULL) { - SDL_FreeSurface(surface); - surface = NULL; - } -} - -void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { - if (text.empty()) return; - - std::string::size_type pos; - SDL_Rect srcrect, dstrect; - - // these values won't change in the loop - srcrect.y = 1; - dstrect.y = y; - srcrect.h = dstrect.h = surface->h-1; - - for(unsigned i=0; iw; i++) { - //Utf8 characters - if (utf8Code(text[i]) && i+1h - 1; -} - -unsigned SFontPlus::getLineHeight() { - return lineHeight; -} diff --git a/src/sfontplus.h b/src/sfontplus.h deleted file mode 100644 index 7f6b0db..0000000 --- a/src/sfontplus.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef SFONTPLUS_H -#define SFONTPLUS_H - -#include -#include -#include - -#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" - -class SFontPlus { -private: - Uint32 getPixel(Sint32 x, Sint32 y); - - SDL_Surface *surface; - std::vector charpos; - std::string characters; - unsigned height, lineHeight; - -public: - SFontPlus(); - SFontPlus(SDL_Surface *font); - SFontPlus(const std::string &font); - ~SFontPlus(); - - bool utf8Code(unsigned char c); - - void initFont(SDL_Surface *font, const std::string &characters = SFONTPLUS_CHARSET); - void initFont(const std::string &font, const std::string &characters = SFONTPLUS_CHARSET); - void freeFont(); - - void write(SDL_Surface *s, const std::string &text, int x, int y); - - unsigned getTextWidth(const std::string &text); - unsigned getHeight(); - unsigned getLineHeight(); -}; - -#endif /* SFONTPLUS_H */ From 492a36b9dfda3e005a97e35f0c0e0c091bd582af Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Mon, 9 May 2011 14:54:11 +0200 Subject: [PATCH 08/27] ASFont: simplified object initialization and cleanup. Do initialization and cleanup in constructor and destructor respectively. Removed constructors that are not used by gmenu2x. --- src/asfont.cpp | 75 +++++++++++++++----------------------------------- src/asfont.h | 34 ++++++++--------------- 2 files changed, 34 insertions(+), 75 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 7a8ff4d..8d75443 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -5,6 +5,8 @@ #include +#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" + Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { assert(x>=0); assert(xw); @@ -38,42 +40,17 @@ Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { return 0; } -SFontPlus::SFontPlus() { - surface = NULL; -} - -SFontPlus::SFontPlus(SDL_Surface* font) { - surface = NULL; - initFont(font); -} - -SFontPlus::SFontPlus(const std::string &font) { - surface = NULL; - initFont(font); -} - -SFontPlus::~SFontPlus() { - freeFont(); -} - -bool SFontPlus::utf8Code(unsigned char c) { - return (c>=194 && c<=198) || c==208 || c==209; - //return c>=194; -} - -void SFontPlus::initFont(const std::string &font, const std::string &characters) { - SDL_Surface *buf = loadPNG(font); - if (buf!=NULL) { - initFont( SDL_DisplayFormatAlpha(buf), characters ); - SDL_FreeSurface(buf); - } -} - -void SFontPlus::initFont(SDL_Surface *font, const std::string &characters) { - freeFont(); +SFontPlus::SFontPlus(const std::string &fontImagePath, const std::string &characters) { this->characters = characters; - if (font==NULL) return; - surface = font; + + SDL_Surface *buf = loadPNG(fontImagePath); + if (!buf) { + surface = NULL; + return; + } + surface = SDL_DisplayFormatAlpha(buf); + SDL_FreeSurface(buf); + Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); #ifdef DEBUG bool utf8 = false; @@ -120,13 +97,17 @@ void SFontPlus::initFont(SDL_Surface *font, const std::string &characters) { lineHeight = y+1; } -void SFontPlus::freeFont() { - if (surface!=NULL) { +SFontPlus::~SFontPlus() { + if (surface) { SDL_FreeSurface(surface); - surface = NULL; } } +bool SFontPlus::utf8Code(unsigned char c) { + return (c>=194 && c<=198) || c==208 || c==209; + //return c>=194; +} + void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { if (text.empty()) return; @@ -193,26 +174,14 @@ unsigned SFontPlus::getLineHeight() { return lineHeight; } -ASFont::ASFont(SDL_Surface* font) { - this->font.initFont(font); - halfHeight = getHeight()/2; - halfLineHeight = getLineHeight()/2; -} - -ASFont::ASFont(Surface* font) { - this->font.initFont(font->raw); - halfHeight = getHeight()/2; - halfLineHeight = getLineHeight()/2; -} - -ASFont::ASFont(const std::string &font) { - this->font.initFont(font); +ASFont::ASFont(const std::string &fontImagePath) + : font(fontImagePath, SFONTPLUS_CHARSET) +{ halfHeight = getHeight()/2; halfLineHeight = getLineHeight()/2; } ASFont::~ASFont() { - font.freeFont(); } bool ASFont::utf8Code(unsigned char c) { diff --git a/src/asfont.h b/src/asfont.h index ce54ffd..723c724 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -12,9 +12,19 @@ class Surface; -#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" - class SFontPlus { +public: + SFontPlus(const std::string &fontImagePath, const std::string &characters); + ~SFontPlus(); + + bool utf8Code(unsigned char c); + + void write(SDL_Surface *s, const std::string &text, int x, int y); + + unsigned getTextWidth(const std::string &text); + unsigned getHeight(); + unsigned getLineHeight(); + private: Uint32 getPixel(Sint32 x, Sint32 y); @@ -22,24 +32,6 @@ private: std::vector charpos; std::string characters; unsigned height, lineHeight; - -public: - SFontPlus(); - SFontPlus(SDL_Surface *font); - SFontPlus(const std::string &font); - ~SFontPlus(); - - bool utf8Code(unsigned char c); - - void initFont(SDL_Surface *font, const std::string &characters = SFONTPLUS_CHARSET); - void initFont(const std::string &font, const std::string &characters = SFONTPLUS_CHARSET); - void freeFont(); - - void write(SDL_Surface *s, const std::string &text, int x, int y); - - unsigned getTextWidth(const std::string &text); - unsigned getHeight(); - unsigned getLineHeight(); }; class ASFont { @@ -47,8 +39,6 @@ public: enum HAlign { HAlignLeft, HAlignRight, HAlignCenter }; enum VAlign { VAlignTop, VAlignBottom, VAlignMiddle }; - ASFont(SDL_Surface* font); - ASFont(Surface* font); ASFont(const std::string &font); ~ASFont(); From e4b71138b946e24de335646d2ca6c35810872970 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Tue, 10 May 2011 00:31:45 +0200 Subject: [PATCH 09/27] ASFont: removed precalculation of half heights. There is no point in precalculating something so cheap to recalculate. Also, the majority of uses was to compensate for passing the wrong alignment argument. --- src/asfont.cpp | 13 ++----------- src/asfont.h | 3 --- src/gmenu2x.cpp | 3 +-- src/menusetting.cpp | 2 +- src/menusettingbool.cpp | 2 +- src/menusettingint.cpp | 2 +- src/menusettingrgba.cpp | 8 ++++---- src/menusettingstringbase.cpp | 6 ++---- 8 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 8d75443..f976a7f 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -177,8 +177,6 @@ unsigned SFontPlus::getLineHeight() { ASFont::ASFont(const std::string &fontImagePath) : font(fontImagePath, SFONTPLUS_CHARSET) { - halfHeight = getHeight()/2; - halfLineHeight = getLineHeight()/2; } ASFont::~ASFont() { @@ -208,7 +206,7 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, case VAlignTop: break; case VAlignMiddle: - y -= getHalfHeight(); + y -= getHeight() / 2; break; case VAlignBottom: y -= getHeight(); @@ -222,7 +220,7 @@ void ASFont::write(SDL_Surface* surface, std::vector *text, int x, case VAlignTop: break; case VAlignMiddle: - y -= getHalfHeight()*text->size(); + y -= (getHeight() / 2) * text->size(); break; case VAlignBottom: y -= getHeight()*text->size(); @@ -258,16 +256,9 @@ void ASFont::write(Surface* surface, const std::string& text, int x, int y, HAli int ASFont::getHeight() { return font.getHeight(); } -int ASFont::getHalfHeight() { - return halfHeight; -} - int ASFont::getLineHeight() { return font.getLineHeight(); } -int ASFont::getHalfLineHeight() { - return halfLineHeight; -} int ASFont::getTextWidth(const char* text) { return font.getTextWidth(text); diff --git a/src/asfont.h b/src/asfont.h index 723c724..f14feb3 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -45,9 +45,7 @@ public: bool utf8Code(unsigned char c); int getHeight(); - int getHalfHeight(); int getLineHeight(); - int getHalfLineHeight(); int getTextWidth(const char* text); int getTextWidth(const std::string& text); int getTextWidth(std::vector *text); @@ -58,7 +56,6 @@ public: private: SFontPlus font; - int halfHeight, halfLineHeight; }; #endif /* ASFONT_H */ diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 0870a40..afa07f5 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -1333,7 +1333,6 @@ void GMenu2X::contextMenu() { uint i, sel=0, fadeAlpha=0; int h = font->getHeight(); - int h2 = font->getHalfHeight(); SDL_Rect box; box.h = (h+2)*voices.size()+8; box.w = 0; @@ -1370,7 +1369,7 @@ void GMenu2X::contextMenu() { //draw selection rect s->box( selbox.x, selbox.y, selbox.w, selbox.h, skinConfColors[COLOR_MESSAGE_BOX_SELECTION] ); for (i=0; iwrite( font, voices[i].text, box.x+12, box.y+h2+5+(h+2)*i, ASFont::HAlignLeft, ASFont::VAlignMiddle ); + s->write( font, voices[i].text, box.x+12, box.y+5+(h+2)*i, ASFont::HAlignLeft, ASFont::VAlignTop ); s->flip(); //touchscreen diff --git a/src/menusetting.cpp b/src/menusetting.cpp index 1cb490b..98f2fa5 100644 --- a/src/menusetting.cpp +++ b/src/menusetting.cpp @@ -36,7 +36,7 @@ MenuSetting::~MenuSetting() void MenuSetting::draw(int y) { - gmenu2x->s->write( gmenu2x->font, name, 5, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, name, 5, y, ASFont::HAlignLeft, ASFont::VAlignTop ); } void MenuSetting::handleTS() diff --git a/src/menusettingbool.cpp b/src/menusettingbool.cpp index 68ddc9b..6fccab9 100644 --- a/src/menusettingbool.cpp +++ b/src/menusettingbool.cpp @@ -61,7 +61,7 @@ void MenuSettingBool::initButton() void MenuSettingBool::draw(int y) { MenuSetting::draw(y); - gmenu2x->s->write( gmenu2x->font, strvalue, 155, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, strvalue, 155, y, ASFont::HAlignLeft, ASFont::VAlignTop ); } void MenuSettingBool::manageInput(bevent_t *event) diff --git a/src/menusettingint.cpp b/src/menusettingint.cpp index f3c8541..879fa7b 100644 --- a/src/menusettingint.cpp +++ b/src/menusettingint.cpp @@ -62,7 +62,7 @@ MenuSettingInt::MenuSettingInt(GMenu2X *gmenu2x, const string &name, const strin void MenuSettingInt::draw(int y) { MenuSetting::draw(y); - gmenu2x->s->write( gmenu2x->font, strvalue, 155, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, strvalue, 155, y, ASFont::HAlignLeft, ASFont::VAlignTop ); } void MenuSettingInt::manageInput(bevent_t *event) diff --git a/src/menusettingrgba.cpp b/src/menusettingrgba.cpp index 477200a..1972eaa 100644 --- a/src/menusettingrgba.cpp +++ b/src/menusettingrgba.cpp @@ -63,10 +63,10 @@ void MenuSettingRGBA::draw(int y) { MenuSetting::draw(y); gmenu2x->s->rectangle( 153, y+1, 11, 11, 0,0,0,255 ); gmenu2x->s->box( 154, y+2, 9, 9, value() ); - gmenu2x->s->write( gmenu2x->font, "R: "+strR, 169, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); - gmenu2x->s->write( gmenu2x->font, "G: "+strG, 205, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); - gmenu2x->s->write( gmenu2x->font, "B: "+strB, 241, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); - gmenu2x->s->write( gmenu2x->font, "A: "+strA, 277, y+gmenu2x->font->getHalfHeight(), ASFont::HAlignLeft, ASFont::VAlignMiddle ); + gmenu2x->s->write( gmenu2x->font, "R: "+strR, 169, y, ASFont::HAlignLeft, ASFont::VAlignTop ); + gmenu2x->s->write( gmenu2x->font, "G: "+strG, 205, y, ASFont::HAlignLeft, ASFont::VAlignTop ); + gmenu2x->s->write( gmenu2x->font, "B: "+strB, 241, y, ASFont::HAlignLeft, ASFont::VAlignTop ); + gmenu2x->s->write( gmenu2x->font, "A: "+strA, 277, y, ASFont::HAlignLeft, ASFont::VAlignTop ); } void MenuSettingRGBA::handleTS() { diff --git a/src/menusettingstringbase.cpp b/src/menusettingstringbase.cpp index fda54c1..d378d84 100644 --- a/src/menusettingstringbase.cpp +++ b/src/menusettingstringbase.cpp @@ -38,10 +38,8 @@ MenuSettingStringBase::~MenuSettingStringBase() void MenuSettingStringBase::draw(int y) { MenuSetting::draw(y); - gmenu2x->s->write( - gmenu2x->font, value(), - 155, y + gmenu2x->font->getHalfHeight(), - ASFont::HAlignLeft, ASFont::VAlignMiddle); + gmenu2x->s->write(gmenu2x->font, value(), 155, y, + ASFont::HAlignLeft, ASFont::VAlignTop); } void MenuSettingStringBase::manageInput(bevent_t *event) From ff546cdcb050f29f5491daf9834de6b01d35abfe Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Tue, 10 May 2011 02:08:14 +0200 Subject: [PATCH 10/27] ASFont: have a single routine for computing text width that also handles newlines. This way the text has to be scanned only once and no copying is needed. --- src/asfont.cpp | 54 +++++++++++++++++++++----------------------------- src/asfont.h | 3 +-- 2 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index f976a7f..9c22239 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -143,27 +143,30 @@ void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { } } -unsigned SFontPlus::getTextWidth(const std::string &text) { - std::string::size_type pos; - int width = 0; - - for(unsigned x=0; x textArr; - split(textArr,text,"\n"); - return getTextWidth(&textArr); - } else - return getTextWidth(text.c_str()); -} -int ASFont::getTextWidth(std::vector *text) { - int w = 0; - for (unsigned i=0; isize(); i++) - w = max( getTextWidth(text->at(i).c_str()), w ); - return w; + return font.getTextWidth(text.c_str()); } diff --git a/src/asfont.h b/src/asfont.h index f14feb3..6a9dafe 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -21,7 +21,7 @@ public: void write(SDL_Surface *s, const std::string &text, int x, int y); - unsigned getTextWidth(const std::string &text); + unsigned getTextWidth(const char *text); unsigned getHeight(); unsigned getLineHeight(); @@ -48,7 +48,6 @@ public: int getLineHeight(); int getTextWidth(const char* text); int getTextWidth(const std::string& text); - int getTextWidth(std::vector *text); void write(SDL_Surface* surface, const char* text, int x, int y); void write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); void write(SDL_Surface* surface, std::vector *text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); From 1bc55392cdabfda477e9b8a273d0c7779940d7ee Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Tue, 10 May 2011 02:23:13 +0200 Subject: [PATCH 11/27] ASFont: cleanups in text write methods. --- src/asfont.cpp | 25 +++++++++++-------------- src/asfont.h | 6 +++--- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 9c22239..34d6cfe 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -189,16 +189,12 @@ bool ASFont::utf8Code(unsigned char c) { return font.utf8Code(c); } -void ASFont::write(SDL_Surface* surface, const char* text, int x, int y) { - font.write(surface, text, x, y); -} - void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { switch (halign) { case HAlignLeft: break; case HAlignCenter: - x -= getTextWidth(text)/2; + x -= getTextWidth(text) / 2; break; case HAlignRight: x -= getTextWidth(text); @@ -218,40 +214,41 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, font.write(surface, text, x, y); } -void ASFont::write(SDL_Surface* surface, std::vector *text, int x, int y, HAlign halign, VAlign valign) { +void ASFont::write(SDL_Surface* surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign) { switch (valign) { case VAlignTop: break; case VAlignMiddle: - y -= (getHeight() / 2) * text->size(); + y -= (getHeight() / 2) * text.size(); break; case VAlignBottom: - y -= getHeight()*text->size(); + y -= getHeight() * text.size(); break; } - for (unsigned i=0; isize(); i++) { + for (std::vector::const_iterator it = text.begin(); it != text.end(); ++it) { int ix = x; switch (halign) { case HAlignLeft: break; case HAlignCenter: - ix -= getTextWidth(text->at(i))/2; + ix -= getTextWidth(*it) / 2; break; case HAlignRight: - ix -= getTextWidth(text->at(i)); + ix -= getTextWidth(*it); break; } - font.write(surface, text->at(i), x, y+getHeight()*i); + font.write(surface, *it, ix, y); + y += getHeight(); } } void ASFont::write(Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { if (text.find("\n", 0) != std::string::npos) { std::vector textArr; - split(textArr,text,"\n"); - write(surface->raw, &textArr, x, y, halign, valign); + split(textArr, text, "\n"); + write(surface->raw, textArr, x, y, halign, valign); } else write(surface->raw, text, x, y, halign, valign); } diff --git a/src/asfont.h b/src/asfont.h index 6a9dafe..49b994b 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -48,12 +48,12 @@ public: int getLineHeight(); int getTextWidth(const char* text); int getTextWidth(const std::string& text); - void write(SDL_Surface* surface, const char* text, int x, int y); - void write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); - void write(SDL_Surface* surface, std::vector *text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); void write(Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); private: + void write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign); + void write(SDL_Surface* surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign); + SFontPlus font; }; From b4f3cde5268bbff3deb942f8f6f4a00382d5ec27 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Tue, 10 May 2011 02:37:10 +0200 Subject: [PATCH 12/27] ASFont: merged SFontPlus class into ASFont class. --- src/asfont.cpp | 54 +++++++++++--------------------------------------- src/asfont.h | 45 ++++++++++++++++------------------------- 2 files changed, 29 insertions(+), 70 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 34d6cfe..e34bdf8 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -7,7 +7,7 @@ #define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" -Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { +Uint32 ASFont::getPixel(Sint32 x, Sint32 y) { assert(x>=0); assert(xw); assert(y>=0); @@ -40,8 +40,8 @@ Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { return 0; } -SFontPlus::SFontPlus(const std::string &fontImagePath, const std::string &characters) { - this->characters = characters; +ASFont::ASFont(const std::string &fontImagePath) { + this->characters = SFONTPLUS_CHARSET; SDL_Surface *buf = loadPNG(fontImagePath); if (!buf) { @@ -97,18 +97,18 @@ SFontPlus::SFontPlus(const std::string &fontImagePath, const std::string &charac lineHeight = y+1; } -SFontPlus::~SFontPlus() { +ASFont::~ASFont() { if (surface) { SDL_FreeSurface(surface); } } -bool SFontPlus::utf8Code(unsigned char c) { +bool ASFont::utf8Code(unsigned char c) { return (c>=194 && c<=198) || c==208 || c==209; //return c>=194; } -void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { +void ASFont::write(SDL_Surface *s, const std::string &text, int x, int y) { if (text.empty()) return; std::string::size_type pos; @@ -143,8 +143,8 @@ void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { } } -unsigned SFontPlus::getTextWidth(const char *text) { - unsigned maxWidth = 0, width = 0; +int ASFont::getTextWidth(const char *text) { + int maxWidth = 0, width = 0; while (char ch = *text++) { if (ch == '\n') { // New line. @@ -169,24 +169,8 @@ unsigned SFontPlus::getTextWidth(const char *text) { return max(width, maxWidth); } -unsigned SFontPlus::getHeight() { - return surface->h - 1; -} - -unsigned SFontPlus::getLineHeight() { - return lineHeight; -} - -ASFont::ASFont(const std::string &fontImagePath) - : font(fontImagePath, SFONTPLUS_CHARSET) -{ -} - -ASFont::~ASFont() { -} - -bool ASFont::utf8Code(unsigned char c) { - return font.utf8Code(c); +int ASFont::getTextWidth(const std::string& text) { + return getTextWidth(text.c_str()); } void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { @@ -212,7 +196,7 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, break; } - font.write(surface, text, x, y); + write(surface, text, x, y); } void ASFont::write(SDL_Surface* surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign) { switch (valign) { @@ -239,7 +223,7 @@ void ASFont::write(SDL_Surface* surface, const std::vector &text, i break; } - font.write(surface, *it, ix, y); + write(surface, *it, ix, y); y += getHeight(); } } @@ -252,17 +236,3 @@ void ASFont::write(Surface* surface, const std::string& text, int x, int y, HAli } else write(surface->raw, text, x, y, halign, valign); } - -int ASFont::getHeight() { - return font.getHeight(); -} -int ASFont::getLineHeight() { - return font.getLineHeight(); -} - -int ASFont::getTextWidth(const char* text) { - return font.getTextWidth(text); -} -int ASFont::getTextWidth(const std::string& text) { - return font.getTextWidth(text.c_str()); -} diff --git a/src/asfont.h b/src/asfont.h index 49b994b..bea7266 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -12,28 +12,6 @@ class Surface; -class SFontPlus { -public: - SFontPlus(const std::string &fontImagePath, const std::string &characters); - ~SFontPlus(); - - bool utf8Code(unsigned char c); - - void write(SDL_Surface *s, const std::string &text, int x, int y); - - unsigned getTextWidth(const char *text); - unsigned getHeight(); - unsigned getLineHeight(); - -private: - Uint32 getPixel(Sint32 x, Sint32 y); - - SDL_Surface *surface; - std::vector charpos; - std::string characters; - unsigned height, lineHeight; -}; - class ASFont { public: enum HAlign { HAlignLeft, HAlignRight, HAlignCenter }; @@ -44,17 +22,28 @@ public: bool utf8Code(unsigned char c); - int getHeight(); - int getLineHeight(); - int getTextWidth(const char* text); + int getTextWidth(const char *text); int getTextWidth(const std::string& text); + + int getHeight() { + return surface->h - 1; + } + int getLineHeight() { + return lineHeight; + } + void write(Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); private: - void write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign); - void write(SDL_Surface* surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign); + void write(SDL_Surface *s, const std::string &text, int x, int y); + void write(SDL_Surface *surface, const std::string& text, int x, int y, HAlign halign, VAlign valign); + void write(SDL_Surface *surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign); + Uint32 getPixel(Sint32 x, Sint32 y); - SFontPlus font; + SDL_Surface *surface; + std::vector charpos; + std::string characters; + int lineHeight; }; #endif /* ASFONT_H */ From bff04d241807d559ac21840439985bc48fa57c68 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Tue, 10 May 2011 02:43:15 +0200 Subject: [PATCH 13/27] ASFont: removed duplication of horizontal alignment code. --- src/asfont.cpp | 23 +++++++---------------- src/asfont.h | 7 ++++--- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index e34bdf8..0854d15 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -173,7 +173,7 @@ int ASFont::getTextWidth(const std::string& text) { return getTextWidth(text.c_str()); } -void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { +void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign) { switch (halign) { case HAlignLeft: break; @@ -184,7 +184,10 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, x -= getTextWidth(text); break; } + write(surface, text, x, y); +} +void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { switch (valign) { case VAlignTop: break; @@ -195,9 +198,9 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, y -= getHeight(); break; } - - write(surface, text, x, y); + write(surface, text, x, y, halign); } + void ASFont::write(SDL_Surface* surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign) { switch (valign) { case VAlignTop: @@ -211,19 +214,7 @@ void ASFont::write(SDL_Surface* surface, const std::vector &text, i } for (std::vector::const_iterator it = text.begin(); it != text.end(); ++it) { - int ix = x; - switch (halign) { - case HAlignLeft: - break; - case HAlignCenter: - ix -= getTextWidth(*it) / 2; - break; - case HAlignRight: - ix -= getTextWidth(*it); - break; - } - - write(surface, *it, ix, y); + write(surface, *it, x, y, halign); y += getHeight(); } } diff --git a/src/asfont.h b/src/asfont.h index bea7266..4e30cdc 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -35,10 +35,11 @@ public: void write(Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); private: - void write(SDL_Surface *s, const std::string &text, int x, int y); - void write(SDL_Surface *surface, const std::string& text, int x, int y, HAlign halign, VAlign valign); - void write(SDL_Surface *surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign); Uint32 getPixel(Sint32 x, Sint32 y); + void write(SDL_Surface *surface, const std::string &text, int x, int y); + void write(SDL_Surface *surface, const std::string &text, int x, int y, HAlign halign); + void write(SDL_Surface *surface, const std::string &text, int x, int y, HAlign halign, VAlign valign); + void write(SDL_Surface *surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign); SDL_Surface *surface; std::vector charpos; From db5ebff16f5d5733eb7925d3cef4f22951016d3f Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Wed, 11 May 2011 02:31:41 +0200 Subject: [PATCH 14/27] Fixed debug print. GCC 4.5.1 has stricter checking of format strings and refused to compile this. --- src/surfacecollection.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/surfacecollection.cpp b/src/surfacecollection.cpp index f9c22a3..ae1a4a2 100644 --- a/src/surfacecollection.cpp +++ b/src/surfacecollection.cpp @@ -54,7 +54,7 @@ string SurfaceCollection::getSkinFilePath(const string &file) { void SurfaceCollection::debug() { SurfaceHash::iterator end = surfaces.end(); for(SurfaceHash::iterator curr = surfaces.begin(); curr != end; curr++){ - DEBUG("key: %i\n", curr->first); + DEBUG("key: %s\n", curr->first.c_str()); } } From 41e6e4693e77bc3af677c0e34f9de4010e7a0e74 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 12 May 2011 01:20:26 +0200 Subject: [PATCH 15/27] ASFont: only convert font surface if it is not already in a 32bpp format. --- src/asfont.cpp | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 0854d15..9f0f429 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -4,6 +4,7 @@ #include "utilities.h" #include +#include #define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" @@ -40,28 +41,41 @@ Uint32 ASFont::getPixel(Sint32 x, Sint32 y) { return 0; } -ASFont::ASFont(const std::string &fontImagePath) { - this->characters = SFONTPLUS_CHARSET; - +ASFont::ASFont(const std::string &fontImagePath) + : characters(SFONTPLUS_CHARSET) +{ + // Load PNG file into an SDL surface. SDL_Surface *buf = loadPNG(fontImagePath); if (!buf) { surface = NULL; return; } - surface = SDL_DisplayFormatAlpha(buf); - SDL_FreeSurface(buf); + + // Make sure we have a surface that can be blitted using alpha blending. + if (buf->format->BytesPerPixel == 4) { + surface = buf; + SDL_SetAlpha(surface, SDL_SRCALPHA, 255); + } else { + SDL_PixelFormat format32; + memset(&format32, 0, sizeof(format32)); + format32.BitsPerPixel = 32; + format32.BytesPerPixel = 4; + format32.Rmask = + SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x00FF0000 : 0x000000FF; + format32.Gmask = 0x0000FF00; + format32.Bmask = + SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x000000FF : 0x00FF0000; + format32.Amask = 0xFF000000; + format32.Rshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 16 : 0; + format32.Gshift = 8; + format32.Bshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0 : 16; + format32.Ashift = 24; + surface = SDL_ConvertSurface(buf, &format32, SDL_SRCALPHA); + SDL_FreeSurface(buf); + } Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); -#ifdef DEBUG - bool utf8 = false; - for (unsigned x=0; x128; - if (utf8) DEBUG("%d\n", (unsigned char)characters[x]); - } -#endif - unsigned c = 0; - SDL_LockSurface(surface); for (unsigned x=0; x<(unsigned)surface->w && ch-1); - SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey); std::string::size_type pos = characters.find("0")*2; SDL_Rect srcrect = {charpos[pos], 1, charpos[pos+2] - charpos[pos], surface->h - 1}; unsigned y = srcrect.h; From a382a425a9fd6fe43d91f508ff9545b2e18821ba Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 12 May 2011 02:10:40 +0200 Subject: [PATCH 16/27] ASFont: access font pixels efficiently. Previous commit ensures that the font surface is always 32 bpp. Therefore we can read font pixels directly through a pointer. --- src/asfont.cpp | 99 ++++++++++++++++++++------------------------------ src/asfont.h | 1 - 2 files changed, 39 insertions(+), 61 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 9f0f429..25dec70 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -8,39 +8,6 @@ #define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" -Uint32 ASFont::getPixel(Sint32 x, Sint32 y) { - assert(x>=0); - assert(xw); - assert(y>=0); - assert(yh); - - Uint32 Bpp = surface->format->BytesPerPixel; - - // Get the pixel - switch(Bpp) { - case 1: - return *((Uint8 *)surface->pixels + y * surface->pitch + x); - break; - case 2: - return *((Uint16 *)surface->pixels + y * surface->pitch/2 + x); - break; - case 3: { // Format/endian independent - Uint8 *bits = ((Uint8 *)surface->pixels)+y*surface->pitch+x*Bpp; - Uint8 r, g, b; - r = *((bits)+surface->format->Rshift/8); - g = *((bits)+surface->format->Gshift/8); - b = *((bits)+surface->format->Bshift/8); - return SDL_MapRGB(surface->format, r, g, b); - } - break; - case 4: - return *((Uint32 *)surface->pixels + y * surface->pitch/4 + x); - break; - } - - return 0; -} - ASFont::ASFont(const std::string &fontImagePath) : characters(SFONTPLUS_CHARSET) { @@ -73,41 +40,53 @@ ASFont::ASFont(const std::string &fontImagePath) surface = SDL_ConvertSurface(buf, &format32, SDL_SRCALPHA); SDL_FreeSurface(buf); } + assert(surface->format->BytesPerPixel == 4); - Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); - unsigned c = 0; SDL_LockSurface(surface); - for (unsigned x=0; x<(unsigned)surface->w && cformat, 255, 0, 255); + Uint32 *topLine = static_cast(surface->pixels); + const unsigned width = surface->w; + unsigned x = 0; + unsigned c = 0; + while (c < characters.length()) { + while (x < width && topLine[x] != pink) x++; + unsigned startx = x; + x++; + while (x < width && topLine[x] == pink) x++; + + charpos.push_back(startx); + charpos.push_back(x); + if (c > 0 && utf8Code(characters[c - 1])) { + // UTF8 character + charpos.push_back(startx); charpos.push_back(x); - - x++; - while (x<(unsigned)surface->w && getPixel(x,0) == pink) x++; - charpos.push_back(x); - - //utf8 characters - if (c>0 && utf8Code(characters[c-1])) { - charpos.push_back(startx); - charpos.push_back(x); - c++; - } - c++; } + c++; } - SDL_UnlockSurface(surface); - Uint32 colKey = getPixel(0,surface->h-1); - std::string::size_type pos = characters.find("0")*2; - SDL_Rect srcrect = {charpos[pos], 1, charpos[pos+2] - charpos[pos], surface->h - 1}; + + // Scan height of "0" glyph. + std::string::size_type pos = characters.find("0") * 2; + SDL_Rect srcrect = { + charpos[pos], 1, charpos[pos + 2] - charpos[pos], surface->h - 1 + }; + const unsigned alphaMask = surface->format->Amask; unsigned y = srcrect.h; - bool nonKeyFound = false; - while (y-- > 0 && !nonKeyFound) { - unsigned x = srcrect.w; - while (x-- > 0 && !nonKeyFound) - nonKeyFound = getPixel(x+srcrect.x,y+srcrect.y) != colKey; + bool nonTransparentFound = false; + while (!nonTransparentFound && y-- > 0) { + Uint32 *line = reinterpret_cast( + reinterpret_cast(surface->pixels) + + (srcrect.y + y) * surface->pitch + ); + for (unsigned x = 0; !nonTransparentFound && x < srcrect.w; x++) { + nonTransparentFound = (line[srcrect.x + x] & alphaMask) != 0; + } } - lineHeight = y+1; + lineHeight = y + 1; + + SDL_UnlockSurface(surface); } ASFont::~ASFont() { diff --git a/src/asfont.h b/src/asfont.h index 4e30cdc..850ab20 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -35,7 +35,6 @@ public: void write(Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); private: - Uint32 getPixel(Sint32 x, Sint32 y); void write(SDL_Surface *surface, const std::string &text, int x, int y); void write(SDL_Surface *surface, const std::string &text, int x, int y, HAlign halign); void write(SDL_Surface *surface, const std::string &text, int x, int y, HAlign halign, VAlign valign); From e0831be4a3d9b22a79e63cb0b83147819603d86c Mon Sep 17 00:00:00 2001 From: Xiangfu Liu Date: Fri, 27 May 2011 21:41:12 +0800 Subject: [PATCH 17/27] add color ash shell, add the method to gmenu2x.sh as comment --- nanonote/gmenu2x.sh | 7 ++++--- nanonote/sections/terminals/ash-color | 4 ++++ nanonote/sections/terminals/{ash => ash-default} | 0 3 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 nanonote/sections/terminals/ash-color rename nanonote/sections/terminals/{ash => ash-default} (100%) diff --git a/nanonote/gmenu2x.sh b/nanonote/gmenu2x.sh index 52f75fc..6c9f4b6 100755 --- a/nanonote/gmenu2x.sh +++ b/nanonote/gmenu2x.sh @@ -4,7 +4,8 @@ source /etc/profile setfont2 /usr/share/setfont2/un-fuzzy-6x10-font.pnm loadunimap /usr/share/setfont2/ben_uni.trans -clear -cd /usr/share/gmenu2x -./gmenu2x +#setfont /usr/share/kbd/consolefonts/kernel-6x11-font + +clear +cd /usr/share/gmenu2x && ./gmenu2x diff --git a/nanonote/sections/terminals/ash-color b/nanonote/sections/terminals/ash-color new file mode 100644 index 0000000..3f980a8 --- /dev/null +++ b/nanonote/sections/terminals/ash-color @@ -0,0 +1,4 @@ +title=ash(Color) +icon=skin:icons/utilities-terminal.png +exec=setfont +params=/usr/share/kbd/consolefonts/kernel-6x11-font; /bin/ash --login diff --git a/nanonote/sections/terminals/ash b/nanonote/sections/terminals/ash-default similarity index 100% rename from nanonote/sections/terminals/ash rename to nanonote/sections/terminals/ash-default From 6087e23d4d8c15b0bb7cd3d9a4d913cc12040f19 Mon Sep 17 00:00:00 2001 From: Xiangfu Liu Date: Mon, 30 May 2011 09:53:55 +0800 Subject: [PATCH 18/27] nanonote: fix setfont path --- nanonote/sections/terminals/ash-color | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nanonote/sections/terminals/ash-color b/nanonote/sections/terminals/ash-color index 3f980a8..af87764 100644 --- a/nanonote/sections/terminals/ash-color +++ b/nanonote/sections/terminals/ash-color @@ -1,4 +1,4 @@ title=ash(Color) icon=skin:icons/utilities-terminal.png -exec=setfont +exec=/usr/bin/setfont params=/usr/share/kbd/consolefonts/kernel-6x11-font; /bin/ash --login From b068aa04d4cf7f7e0e6f68f4abe048a3b6c719ce Mon Sep 17 00:00:00 2001 From: Xiangfu Liu Date: Tue, 31 May 2011 09:01:16 +0800 Subject: [PATCH 19/27] nanonote: using color font --- nanonote/gmenu2x.sh | 6 +++--- nanonote/sections/terminals/ash-default | 4 ++-- nanonote/skins/Default/wallpapers/default.png | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/nanonote/gmenu2x.sh b/nanonote/gmenu2x.sh index 6c9f4b6..b3473c0 100755 --- a/nanonote/gmenu2x.sh +++ b/nanonote/gmenu2x.sh @@ -2,10 +2,10 @@ source /etc/profile -setfont2 /usr/share/setfont2/un-fuzzy-6x10-font.pnm -loadunimap /usr/share/setfont2/ben_uni.trans +#setfont2 /usr/share/setfont2/un-fuzzy-6x10-font.pnm +#loadunimap /usr/share/setfont2/ben_uni.trans -#setfont /usr/share/kbd/consolefonts/kernel-6x11-font +setfont /usr/share/kbd/consolefonts/kernel-6x11-font clear cd /usr/share/gmenu2x && ./gmenu2x diff --git a/nanonote/sections/terminals/ash-default b/nanonote/sections/terminals/ash-default index 0d2df89..b159c92 100644 --- a/nanonote/sections/terminals/ash-default +++ b/nanonote/sections/terminals/ash-default @@ -1,4 +1,4 @@ title=ash(Default) icon=skin:icons/utilities-terminal.png -exec=/bin/ash -params=--login +exec=/usr/sbin/setfont2 +params=/usr/share/setfont2/un-fuzzy-6x10-font.pnm; /usr/bin/loadunimap /usr/share/setfont2/ben_uni.trans; /bin/ash --login diff --git a/nanonote/skins/Default/wallpapers/default.png b/nanonote/skins/Default/wallpapers/default.png index 59b2caa..c94b0dc 120000 --- a/nanonote/skins/Default/wallpapers/default.png +++ b/nanonote/skins/Default/wallpapers/default.png @@ -1 +1 @@ -open.png \ No newline at end of file +a.png \ No newline at end of file From b6a5d89bc8426c5515b89c656f4ab48221e98b58 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 2 Jun 2011 03:39:34 +0200 Subject: [PATCH 20/27] Surface: Removed unused code and restricted access. Removed all methods that are never called. Made methods that are only called by Surface itself private. One ugly thing remaining is outside access to the "raw" field. --- src/surface.cpp | 154 +++--------------------------------------------- src/surface.h | 39 ++++-------- 2 files changed, 17 insertions(+), 176 deletions(-) diff --git a/src/surface.cpp b/src/surface.cpp index 23436c0..233b3a7 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -58,38 +58,11 @@ Surface::Surface(const string &img, const string &skin, bool alpha) { halfH = raw->h/2; } -Surface::Surface(SDL_Surface *s, SDL_PixelFormat *fmt, Uint32 flags) { - dblbuffer = NULL; - this->operator =(s); - if (fmt!=NULL || flags!=0) { - if (fmt==NULL) fmt = s->format; - if (flags==0) flags = s->flags; - raw = SDL_ConvertSurface( s, fmt, flags ); - } -} - Surface::Surface(Surface *s) { dblbuffer = NULL; - this->operator =(s->raw); -} - -Surface::Surface(int w, int h, Uint32 flags) { - dblbuffer = NULL; - Uint32 rmask, gmask, bmask, amask; -#if SDL_BYTEORDER == SDL_BIG_ENDIAN - rmask = 0xff000000; - gmask = 0x00ff0000; - bmask = 0x0000ff00; - amask = 0x000000ff; -#else - rmask = 0x000000ff; - gmask = 0x0000ff00; - bmask = 0x00ff0000; - amask = 0xff000000; -#endif - raw = SDL_CreateRGBSurface( flags, w, h, 16, rmask, gmask, bmask, amask ); - halfW = w/2; - halfH = h/2; + raw = SDL_DisplayFormat(s->raw); + halfW = raw->w/2; + halfH = raw->h/2; } Surface::~Surface() { @@ -137,23 +110,6 @@ void Surface::load(const string &img, bool alpha, const string &skin) { } } -void Surface::lock() { - if ( SDL_MUSTLOCK(raw) && !locked ) { - if ( SDL_LockSurface(raw) < 0 ) { - ERROR("Can't lock surface: '%s'\n", SDL_GetError()); - SDL_Quit(); - } - locked = true; - } -} - -void Surface::unlock() { - if ( SDL_MUSTLOCK(raw) && locked ) { - SDL_UnlockSurface(raw); - locked = false; - } -} - void Surface::flip() { if (dblbuffer!=NULL) { this->blit(dblbuffer,0,0); @@ -197,118 +153,25 @@ bool Surface::blitRight(Surface *destination, int x, int y, int w, int h, int a) return blitRight(destination->raw,x,y,w,h,a); } -void Surface::putPixel(int x, int y, SDL_Color color) { - putPixel(x,y, SDL_MapRGB( raw->format , color.r , color.g , color.b )); -} - -void Surface::putPixel(int x, int y, Uint32 color) { - //determine position - char* pPosition = ( char* ) raw->pixels ; - //offset by y - pPosition += ( raw->pitch * y ) ; - //offset by x - pPosition += ( raw->format->BytesPerPixel * x ) ; - //copy pixel data - memcpy ( pPosition , &color , raw->format->BytesPerPixel ) ; -} - -SDL_Color Surface::pixelColor(int x, int y) { - SDL_Color color; - Uint32 col = pixel(x,y); - SDL_GetRGB( col, raw->format, &color.r, &color.g, &color.b ); - return color; -} - -Uint32 Surface::pixel(int x, int y) { - //determine position - char* pPosition = ( char* ) raw->pixels ; - //offset by y - pPosition += ( raw->pitch * y ) ; - //offset by x - pPosition += ( raw->format->BytesPerPixel * x ) ; - //copy pixel data - Uint32 col = 0; - memcpy ( &col , pPosition , raw->format->BytesPerPixel ) ; - return col; -} - -void Surface::blendAdd(Surface *target, int x, int y) { - SDL_Color targetcol, blendcol; - for (int iy=0; iyh; iy++) - if (iy+y >= 0 && iy+y < target->raw->h) - for (int ix=0; ixw; ix++) { - if (ix+x >= 0 && ix+x < target->raw->w) { - blendcol = pixelColor(ix,iy); - targetcol = target->pixelColor(ix+x,iy+y); - targetcol.r = min(targetcol.r+blendcol.r, 255); - targetcol.g = min(targetcol.g+blendcol.g, 255); - targetcol.b = min(targetcol.b+blendcol.b, 255); - target->putPixel(ix+x,iy+y,targetcol); - } - } - -/* - Uint32 bcol, tcol; - char *pPos, *tpPos; - for (int iy=0; iyh; iy++) - if (iy+y >= 0 && iy+y < target->raw->h) { - pPos = (char*)raw->pixels + raw->pitch*iy; - tpPos = (char*)target->raw->pixels + target->raw->pitch*(iy+y); - - for (int ix=0; ixw; ix++) { - memcpy(&bcol, pPos, raw->format->BytesPerPixel); - memcpy(&tcol, tpPos, target->raw->format->BytesPerPixel); - //memcpy(tpPos, &bcol, target->raw->format->BytesPerPixel); - pPos += raw->format->BytesPerPixel; - tpPos += target->raw->format->BytesPerPixel; - target->putPixel(ix+x,iy+y,bcol); - } - } -*/ -} - -void Surface::operator = (SDL_Surface *s) { - raw = SDL_DisplayFormat(s); - halfW = raw->w/2; - halfH = raw->h/2; -} - -void Surface::operator = (Surface *s) { - this->operator =(s->raw); -} - int Surface::box(Sint16 x, Sint16 y, Sint16 w, Sint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { return boxRGBA(raw,x,y,x+w-1,y+h-1,r,g,b,a); } -int Surface::box(SDL_Rect re, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { - return boxRGBA(raw,re.x,re.y,re.x+re.w-1,re.y+re.h-1,r,g,b,a); -} -int Surface::box(SDL_Rect re, Uint8 r, Uint8 g, Uint8 b) { - return SDL_FillRect(raw, &re, SDL_MapRGBA(format(),r,g,b,255)); -} int Surface::box(Sint16 x, Sint16 y, Sint16 w, Sint16 h, Uint8 r, Uint8 g, Uint8 b) { SDL_Rect re = {x,y,w,h}; - return box(re,r,g,b); + return SDL_FillRect(raw, &re, SDL_MapRGBA(format(),r,g,b,255)); } int Surface::box(Sint16 x, Sint16 y, Sint16 w, Sint16 h, RGBAColor c) { return box(x,y,w,h,c.r,c.g,c.b,c.a); } int Surface::box(SDL_Rect re, RGBAColor c) { - return box(re,c.r,c.g,c.b,c.a); + return boxRGBA( + raw, re.x, re.y, re.x + re.w - 1, re.y + re.h - 1, c.r, c.g, c.b, c.a + ); } int Surface::rectangle(Sint16 x, Sint16 y, Sint16 w, Sint16 h, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { return rectangleRGBA(raw,x,y,x+w-1,y+h-1,r,g,b,a); } -int Surface::rectangle(SDL_Rect re, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { - return rectangleRGBA(raw,re.x,re.y,re.x+re.w-1,re.y+re.h-1,r,g,b,a); -} -int Surface::rectangle(Sint16 x, Sint16 y, Sint16 w, Sint16 h, Uint8 r, Uint8 g, Uint8 b) { - return rectangleColor(raw, x,y,x+w-1,y+h-1, SDL_MapRGBA(format(),r,g,b,255)); -} -int Surface::rectangle(SDL_Rect re, Uint8 r, Uint8 g, Uint8 b) { - return rectangleColor(raw, re.x,re.y,re.x+re.w-1,re.y+re.h-1, SDL_MapRGBA(format(),r,g,b,255)); -} int Surface::rectangle(Sint16 x, Sint16 y, Sint16 w, Sint16 h, RGBAColor c) { return rectangle(x,y,w,h,c.r,c.g,c.b,c.a); } @@ -319,9 +182,6 @@ int Surface::rectangle(SDL_Rect re, RGBAColor c) { int Surface::hline(Sint16 x, Sint16 y, Sint16 w, Uint8 r, Uint8 g, Uint8 b, Uint8 a) { return hlineRGBA(raw,x,x+w-1,y,r,g,b,a); } -int Surface::hline(Sint16 x, Sint16 y, Sint16 w, RGBAColor c) { - return hline(x,y,w-1,c.r,c.g,c.b,c.a); -} void Surface::clearClipRect() { SDL_SetClipRect(raw,NULL); diff --git a/src/surface.h b/src/surface.h index c4bd187..64b3df1 100644 --- a/src/surface.h +++ b/src/surface.h @@ -38,18 +38,11 @@ RGBAColor strtorgba(const string &strColor); @author Massimiliano Torromeo */ class Surface { -private: - bool locked; - int halfW, halfH; - SDL_Surface *dblbuffer; - public: Surface(); Surface(const string &img, const string &skin="", bool alpha=true); Surface(const string &img, bool alpha, const string &skin=""); - Surface(SDL_Surface *s, SDL_PixelFormat *fmt = NULL, Uint32 flags = 0); Surface(Surface *s); - Surface(int w, int h, Uint32 flags = SDL_HWSURFACE|SDL_SRCALPHA); ~Surface(); void enableVirtualDoubleBuffer(SDL_Surface *surface); @@ -57,18 +50,7 @@ public: SDL_Surface *raw; void free(); - void load(const string &img, bool alpha=true, const string &skin=""); - void lock(); - void unlock(); void flip(); - SDL_PixelFormat *format(); - - void putPixel(int,int,SDL_Color); - void putPixel(int,int,Uint32); - SDL_Color pixelColor(int,int); - Uint32 pixel(int,int); - - void blendAdd(Surface*, int,int); void clearClipRect(); void setClipRect(int x, int y, int w, int h); @@ -76,11 +58,8 @@ public: bool blit(Surface *destination, int x, int y, int w=0, int h=0, int a=-1); bool blit(Surface *destination, SDL_Rect container, ASFont::HAlign halign = ASFont::HAlignLeft, ASFont::VAlign valign = ASFont::VAlignTop); - bool blit(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); bool blitCenter(Surface *destination, int x, int y, int w=0, int h=0, int a=-1); - bool blitCenter(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); bool blitRight(Surface *destination, int x, int y, int w=0, int h=0, int a=-1); - bool blitRight(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); void write(ASFont *font, const string &text, int x, int y, ASFont::HAlign halign = ASFont::HAlignLeft, ASFont::VAlign valign = ASFont::VAlignTop) { font->write(this, text, x, y, halign, valign); @@ -89,20 +68,22 @@ public: int box(Sint16, Sint16, Sint16, Sint16, Uint8, Uint8, Uint8, Uint8); int box(Sint16, Sint16, Sint16, Sint16, Uint8, Uint8, Uint8); int box(Sint16, Sint16, Sint16, Sint16, RGBAColor); - int box(SDL_Rect, Uint8, Uint8, Uint8, Uint8); - int box(SDL_Rect, Uint8, Uint8, Uint8); int box(SDL_Rect, RGBAColor); int rectangle(Sint16, Sint16, Sint16, Sint16, Uint8, Uint8, Uint8, Uint8); - int rectangle(Sint16, Sint16, Sint16, Sint16, Uint8, Uint8, Uint8); int rectangle(Sint16, Sint16, Sint16, Sint16, RGBAColor); - int rectangle(SDL_Rect, Uint8, Uint8, Uint8, Uint8); - int rectangle(SDL_Rect, Uint8, Uint8, Uint8); int rectangle(SDL_Rect, RGBAColor); int hline(Sint16, Sint16, Sint16, Uint8, Uint8, Uint8, Uint8); - int hline(Sint16, Sint16, Sint16, RGBAColor); - void operator = (SDL_Surface*); - void operator = (Surface*); +private: + SDL_PixelFormat *format(); + void load(const string &img, bool alpha=true, const string &skin=""); + bool blit(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); + bool blitCenter(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); + bool blitRight(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); + + bool locked; + int halfW, halfH; + SDL_Surface *dblbuffer; }; #endif From b2896d6bac3d8f0180ba8c0febdbb0bbf8554458 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 2 Jun 2011 04:29:19 +0200 Subject: [PATCH 21/27] 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. --- src/asfont.cpp | 18 +++++++++--------- src/asfont.h | 8 ++++---- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index 25dec70..c74015d 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -100,7 +100,7 @@ bool ASFont::utf8Code(unsigned char c) { //return c>=194; } -void ASFont::write(SDL_Surface *s, const std::string &text, int x, int y) { +void ASFont::writeLine(Surface *s, const std::string &text, int x, int y) { if (text.empty()) return; std::string::size_type pos; @@ -129,7 +129,7 @@ void ASFont::write(SDL_Surface *s, const std::string &text, int x, int y) { srcrect.w = charpos[pos+2] - charpos[pos]; dstrect.x = x - charpos[pos+1] + charpos[pos]; - SDL_BlitSurface(surface, &srcrect, s, &dstrect); + SDL_BlitSurface(surface, &srcrect, s->raw, &dstrect); x += charpos[pos+2] - charpos[pos+1]; } @@ -165,7 +165,7 @@ int ASFont::getTextWidth(const std::string& text) { return getTextWidth(text.c_str()); } -void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign) { +void ASFont::writeLine(Surface* surface, const std::string& text, int x, int y, HAlign halign) { switch (halign) { case HAlignLeft: break; @@ -176,10 +176,10 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, x -= getTextWidth(text); break; } - write(surface, text, x, y); + writeLine(surface, text, x, y); } -void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { +void ASFont::writeLine(Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { switch (valign) { case VAlignTop: break; @@ -190,10 +190,10 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, y -= getHeight(); break; } - write(surface, text, x, y, halign); + writeLine(surface, text, x, y, halign); } -void ASFont::write(SDL_Surface* surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign) { +void ASFont::writeLine(Surface* surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign) { switch (valign) { case VAlignTop: break; @@ -215,7 +215,7 @@ void ASFont::write(Surface* surface, const std::string& text, int x, int y, HAli if (text.find("\n", 0) != std::string::npos) { std::vector textArr; split(textArr, text, "\n"); - write(surface->raw, textArr, x, y, halign, valign); + writeLine(surface, textArr, x, y, halign, valign); } else - write(surface->raw, text, x, y, halign, valign); + writeLine(surface, text, x, y, halign, valign); } diff --git a/src/asfont.h b/src/asfont.h index 850ab20..a205cca 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -35,10 +35,10 @@ public: void write(Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); private: - void write(SDL_Surface *surface, const std::string &text, int x, int y); - void write(SDL_Surface *surface, const std::string &text, int x, int y, HAlign halign); - void write(SDL_Surface *surface, const std::string &text, int x, int y, HAlign halign, VAlign valign); - void write(SDL_Surface *surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign); + void writeLine(Surface *surface, const std::string &text, int x, int y); + void writeLine(Surface *surface, const std::string &text, int x, int y, HAlign halign); + void writeLine(Surface *surface, const std::string &text, int x, int y, HAlign halign, VAlign valign); + void writeLine(Surface *surface, const std::vector &text, int x, int y, HAlign halign, VAlign valign); SDL_Surface *surface; std::vector charpos; From 52c89d60055705e34afaafa7c2f1ab2840c2364e Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 2 Jun 2011 04:57:21 +0200 Subject: [PATCH 22/27] Surface: Removed duplicate constructor. There were two constructors that took an image path, skin name and alpha flag as their arguments, but in different orders. We need only one of them. --- src/surface.cpp | 12 ++---------- src/surface.h | 3 +-- src/surfacecollection.cpp | 4 ++-- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/src/surface.cpp b/src/surface.cpp index 233b3a7..13ca2e6 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -42,10 +42,9 @@ Surface::Surface() { dblbuffer = NULL; } -Surface::Surface(const string &img, bool alpha, const string &skin) { - raw = NULL; +Surface::Surface(Surface *s) { dblbuffer = NULL; - load(img, alpha, skin); + raw = SDL_DisplayFormat(s->raw); halfW = raw->w/2; halfH = raw->h/2; } @@ -58,13 +57,6 @@ Surface::Surface(const string &img, const string &skin, bool alpha) { halfH = raw->h/2; } -Surface::Surface(Surface *s) { - dblbuffer = NULL; - raw = SDL_DisplayFormat(s->raw); - halfW = raw->w/2; - halfH = raw->h/2; -} - Surface::~Surface() { free(); } diff --git a/src/surface.h b/src/surface.h index 64b3df1..6e3bc8b 100644 --- a/src/surface.h +++ b/src/surface.h @@ -40,9 +40,8 @@ RGBAColor strtorgba(const string &strColor); class Surface { public: Surface(); - Surface(const string &img, const string &skin="", bool alpha=true); - Surface(const string &img, bool alpha, const string &skin=""); Surface(Surface *s); + Surface(const string &img, const string &skin="", bool alpha=true); ~Surface(); void enableVirtualDoubleBuffer(SDL_Surface *surface); diff --git a/src/surfacecollection.cpp b/src/surfacecollection.cpp index ae1a4a2..7434edd 100644 --- a/src/surfacecollection.cpp +++ b/src/surfacecollection.cpp @@ -80,7 +80,7 @@ Surface *SurfaceCollection::add(const string &path, bool alpha) { return NULL; } else if (!fileExists(filePath)) return NULL; - Surface *s = new Surface(filePath,alpha); + Surface *s = new Surface(filePath, "", alpha); surfaces[path] = s; return s; } @@ -94,7 +94,7 @@ Surface *SurfaceCollection::addSkinRes(const string &path, bool alpha) { string skinpath = getSkinFilePath(path); if (skinpath.empty()) return NULL; - Surface *s = new Surface(skinpath,alpha); + Surface *s = new Surface(skinpath, "", alpha); if (s != NULL) surfaces[path] = s; return s; From 240286df11ccc463edce8907a47b717caf5def16 Mon Sep 17 00:00:00 2001 From: Sergey Kukunin Date: Thu, 2 Jun 2011 06:04:35 +0300 Subject: [PATCH 23/27] Add powersaver class --- src/Makefile.am | 4 +- src/gmenu2x.cpp | 9 ++++- src/gmenu2x.h | 2 + src/inputmanager.cpp | 5 ++- src/powersaver.cpp | 90 ++++++++++++++++++++++++++++++++++++++++++++ src/powersaver.h | 25 ++++++++++++ 6 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 src/powersaver.cpp create mode 100644 src/powersaver.h diff --git a/src/Makefile.am b/src/Makefile.am index ee16c15..16e0ac5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,7 +12,7 @@ gmenu2x_SOURCES = asfont.cpp button.cpp cpu.cpp dirdialog.cpp filedialog.cpp \ textdialog.cpp textmanualdialog.cpp touchscreen.cpp translator.cpp \ utilities.cpp wallpaperdialog.cpp \ browsedialog.cpp buttonbox.cpp dialog.cpp \ - imageio.cpp + imageio.cpp powersaver.cpp noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \ filedialog.h filelister.h gmenu2x.h gp2x.h iconbutton.h imagedialog.h \ @@ -25,7 +25,7 @@ noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \ surfacecollection.h surface.h textdialog.h textmanualdialog.h \ touchscreen.h translator.h utilities.h wallpaperdialog.h \ browsedialog.h buttonbox.h dialog.h \ - imageio.h + imageio.h powersaver.h AM_CFLAGS= @CFLAGS@ @SDL_CFLAGS@ diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index afa07f5..fa1b1ed 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -194,6 +194,7 @@ void GMenu2X::gp2x_tvout_off() { #endif } + GMenu2X::GMenu2X() { //Detect firmware version and type if (fileExists("/etc/open2x")) { @@ -271,7 +272,7 @@ GMenu2X::GMenu2X() { #endif //Screen - if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK)<0 ) { + if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_JOYSTICK|SDL_INIT_TIMER)<0 ) { ERROR("Could not initialize SDL: %s\n", SDL_GetError()); quit(); } @@ -309,6 +310,7 @@ GMenu2X::GMenu2X() { initBG(); input.init(path+"input.conf"); + PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); setInputSpeed(); initServices(); setBacklight(confInt["backlight"]); @@ -319,6 +321,7 @@ GMenu2X::GMenu2X() { readTmp(); if (lastSelectorElement>-1 && menu->selLinkApp()!=NULL && (!menu->selLinkApp()->getSelectorDir().empty() || !lastSelectorDir.empty())) menu->selLinkApp()->selector(lastSelectorElement,lastSelectorDir); + } GMenu2X::~GMenu2X() { @@ -561,6 +564,7 @@ void GMenu2X::readConfig() { evalIntConf( &confInt["maxClock"], 430, 30, 500 ); evalIntConf( &confInt["menuClock"], 200, 30, 430 ); evalIntConf( &confInt["globalVolume"], 67, 0,100 ); + evalIntConf( &confInt["backlightTimeout"], 15, 0,120 ); evalIntConf( &confInt["backlight"], 100, 5,100 ); evalIntConf( &confInt["videoBpp"], 32,32,32 ); // 8,16 @@ -816,7 +820,6 @@ void GMenu2X::main() { if (!fileExists(CARD_ROOT)) CARD_ROOT = "/"; - while (!quit) { tickNow = SDL_GetTicks(); @@ -1112,6 +1115,7 @@ void GMenu2X::options() { sd.addSetting(new MenuSettingBool(this,tr["Output logs"],tr["Logs the output of the links. Use the Log Viewer to read them."],&confInt["outputLogs"])); //G sd.addSetting(new MenuSettingInt(this,tr["Lcd Backlight"],tr["Set dingoo's Lcd Backlight value (default: 100)"],&confInt["backlight"],5,100)); + sd.addSetting(new MenuSettingInt(this,tr["Screen Timeout"],tr["Set screen's backlight timeout in seconds"],&confInt["backlightTimeout"],0,120)); // sd.addSetting(new MenuSettingMultiString(this,tr["Tv-Out encoding"],tr["Encoding of the tv-out signal"],&confStr["tvoutEncoding"],&encodings)); sd.addSetting(new MenuSettingBool(this,tr["Show root"],tr["Show root folder in the file selection dialogs"],&showRootFolder)); @@ -1120,6 +1124,7 @@ void GMenu2X::options() { if (prevbacklight != confInt["backlight"]) setBacklight(confInt["backlight"]); if (curMenuClock!=confInt["menuClock"]) setClock(confInt["menuClock"]); if (curGlobalVolume!=confInt["globalVolume"]) setVolume(confInt["globalVolume"]); + PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] ); if (lang == "English") lang = ""; if (lang != tr.lang()) { tr.setLang(lang); diff --git a/src/gmenu2x.h b/src/gmenu2x.h index cf13c43..278facf 100644 --- a/src/gmenu2x.h +++ b/src/gmenu2x.h @@ -30,6 +30,7 @@ #include "inputmanager.h" #include "asfont.h" #include "surface.h" +#include "powersaver.h" #include #include @@ -122,6 +123,7 @@ private: usbnet, samba, web; + string ip, defaultgw, lastSelectorDir; int lastSelectorElement; void readConfig(); diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp index a5c5a63..abc2e82 100644 --- a/src/inputmanager.cpp +++ b/src/inputmanager.cpp @@ -21,6 +21,7 @@ #include "debug.h" #include "inputmanager.h" #include "utilities.h" +#include "powersaver.h" #include #include @@ -179,6 +180,8 @@ bool InputManager::getEvent(bevent_t *bevent, bool wait) { break; } } - + if ( wait ) { + PowerSaver::getInstance()->resetScreenTimer(); + } return true; } diff --git a/src/powersaver.cpp b/src/powersaver.cpp new file mode 100644 index 0000000..914555a --- /dev/null +++ b/src/powersaver.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include + +#include "powersaver.h" +#include "debug.h" +PowerSaver* PowerSaver::instance = NULL; +Uint32 screenTimerCallback(Uint32 interval, void *param) +{ + DEBUG("Disable Backlight Event\n"); + PowerSaver::getInstance()->disableScreen(); + return 0; +} + +PowerSaver* PowerSaver::getInstance() { + if ( instance == NULL ) { + instance = new PowerSaver(); + } + return instance; +} + +PowerSaver::PowerSaver( ) { + setScreenTimeout(0); + screenTimer = NULL; +} + +PowerSaver::~PowerSaver() { + SDL_RemoveTimer(screenTimer); +} + +void PowerSaver::setScreenTimeout( unsigned int seconds ) { + screenTimeout = seconds; + resetScreenTimer(); +} + +void PowerSaver::resetScreenTimer() { + if ( screenTimer != NULL ) { + SDL_RemoveTimer(screenTimer); + } + addScreenTimer(); + //If display is off, turn on it + if ( !screenState ) { + enableScreen(); + } +} + +void PowerSaver::addScreenTimer() { + //if timeout is zero, don't set timeout + if ( screenTimeout == 0 ) { + screenTimer = NULL; + return; + } + screenTimer = SDL_AddTimer(screenTimeout*1000, screenTimerCallback,NULL); + if ( screenTimer == NULL ) { + ERROR("Could not initialize SDLTimer: %s\n", SDL_GetError()); + } +} + +#define SCREEN_BLANK_PATH "/sys/class/graphics/fb0/blank" +void PowerSaver::setScreenBlanking( bool state ) { + const char* path = SCREEN_BLANK_PATH; + const char* blank = state ? "0" : "1"; + + int fd = open(path, O_RDWR); + if (fd == -1) { + WARNING("Failed to open '%s': %s\n", path, strerror(errno)); + } else { + ssize_t written = write(fd, blank, strlen(blank)); + if (written == -1) { + WARNING("Error writing '%s': %s\n", path, strerror(errno)); + } + close(fd); + } + screenState = state; +} + +void PowerSaver::enableScreen() { + if ( !screenState ) { + setScreenBlanking(true); + } +} +void PowerSaver::disableScreen() { + if ( screenState ) { + setScreenBlanking(false); + } +} + + diff --git a/src/powersaver.h b/src/powersaver.h new file mode 100644 index 0000000..e464853 --- /dev/null +++ b/src/powersaver.h @@ -0,0 +1,25 @@ +#ifndef POWERSAVER_H +#define POWERSAVER_H +#include +class PowerSaver { + + public: + static PowerSaver* getInstance(); + ~PowerSaver(); + void addScreenTimer(); + void resetScreenTimer(); + + void enableScreen(); + void disableScreen(); + + void setScreenTimeout( unsigned int seconds ); + private: + PowerSaver( ); + static PowerSaver* instance; + bool screenState; + unsigned int screenTimeout; + SDL_TimerID screenTimer; + + void setScreenBlanking( bool state ); +}; +#endif From 44ca4e346052c6901511fb4f0c5a96cb745d78c4 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 2 Jun 2011 05:52:47 +0200 Subject: [PATCH 24/27] SurfaceCollection: Removed alpha flag. In the existing code the alpha flag is always true, so there is no point in passing it around. --- src/surfacecollection.cpp | 18 +++++++++--------- src/surfacecollection.h | 15 +++++++-------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/surfacecollection.cpp b/src/surfacecollection.cpp index 7434edd..6d8d755 100644 --- a/src/surfacecollection.cpp +++ b/src/surfacecollection.cpp @@ -28,9 +28,9 @@ using std::endl; using std::string; -SurfaceCollection::SurfaceCollection(bool defaultAlpha, const string &skin) { - this->defaultAlpha = defaultAlpha; - setSkin(skin); +SurfaceCollection::SurfaceCollection() + : skin("default") +{ } SurfaceCollection::~SurfaceCollection() {} @@ -68,7 +68,7 @@ Surface *SurfaceCollection::add(Surface *s, const string &path) { return s; } -Surface *SurfaceCollection::add(const string &path, bool alpha) { +Surface *SurfaceCollection::add(const string &path) { DEBUG("Adding surface: '%s'\n", path.c_str()); if (exists(path)) del(path); @@ -80,12 +80,12 @@ Surface *SurfaceCollection::add(const string &path, bool alpha) { return NULL; } else if (!fileExists(filePath)) return NULL; - Surface *s = new Surface(filePath, "", alpha); + Surface *s = new Surface(filePath, ""); surfaces[path] = s; return s; } -Surface *SurfaceCollection::addSkinRes(const string &path, bool alpha) { +Surface *SurfaceCollection::addSkinRes(const string &path) { DEBUG("Adding skin surface: '%s'\n", path.c_str()); if (path.empty()) return NULL; @@ -94,7 +94,7 @@ Surface *SurfaceCollection::addSkinRes(const string &path, bool alpha) { string skinpath = getSkinFilePath(path); if (skinpath.empty()) return NULL; - Surface *s = new Surface(skinpath, "", alpha); + Surface *s = new Surface(skinpath, "", true); if (s != NULL) surfaces[path] = s; return s; @@ -125,7 +125,7 @@ void SurfaceCollection::move(const string &from, const string &to) { Surface *SurfaceCollection::operator[](const string &key) { SurfaceHash::iterator i = surfaces.find(key); if (i == surfaces.end()) - return add(key, defaultAlpha); + return add(key); else return i->second; } @@ -135,7 +135,7 @@ Surface *SurfaceCollection::skinRes(const string &key) { SurfaceHash::iterator i = surfaces.find(key); if (i == surfaces.end()) - return addSkinRes(key, defaultAlpha); + return addSkinRes(key); else return i->second; } diff --git a/src/surfacecollection.h b/src/surfacecollection.h index 48386e1..c7b0766 100644 --- a/src/surfacecollection.h +++ b/src/surfacecollection.h @@ -33,13 +33,8 @@ Hash Map of surfaces that loads surfaces not already loaded and reuses already l @author Massimiliano Torromeo */ class SurfaceCollection { -private: - SurfaceHash surfaces; - std::string skin; - public: - SurfaceCollection( - bool defaultAlpha = true, const std::string &skin = "default"); + SurfaceCollection(); ~SurfaceCollection(); void setSkin(const std::string &skin); @@ -49,8 +44,8 @@ public: void debug(); Surface *add(Surface *s, const std::string &path); - Surface *add(const std::string &path, bool alpha=true); - Surface *addSkinRes(const std::string &path, bool alpha=true); + Surface *add(const std::string &path); + Surface *addSkinRes(const std::string &path); void del(const std::string &path); void clear(); void move(const std::string &from, const std::string &to); @@ -58,6 +53,10 @@ public: Surface *operator[](const std::string &); Surface *skinRes(const std::string &); + +private: + SurfaceHash surfaces; + std::string skin; }; #endif From 7877fee3fa573d2c13cf40f5054a9011ddc4a14d Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 2 Jun 2011 06:02:45 +0200 Subject: [PATCH 25/27] Surface: Remove alpha flag. As for SurfaceCollection, the alpha flag passed to the Surface constructor was also always true. Therefore there is no need to have the flag. --- src/surface.cpp | 11 ++++------- src/surface.h | 4 ++-- src/surfacecollection.cpp | 4 ++-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/src/surface.cpp b/src/surface.cpp index 13ca2e6..264ea3c 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -49,10 +49,10 @@ Surface::Surface(Surface *s) { halfH = raw->h/2; } -Surface::Surface(const string &img, const string &skin, bool alpha) { +Surface::Surface(const string &img, const string &skin) { raw = NULL; dblbuffer = NULL; - load(img, alpha, skin); + load(img, skin); halfW = raw->w/2; halfH = raw->h/2; } @@ -78,7 +78,7 @@ SDL_PixelFormat *Surface::format() { return raw->format; } -void Surface::load(const string &img, bool alpha, const string &skin) { +void Surface::load(const string &img, const string &skin) { free(); string skinpath; @@ -92,10 +92,7 @@ void Surface::load(const string &img, bool alpha, const string &skin) { SDL_Surface *buf = loadPNG(skinpath); if (buf!=NULL) { - if (alpha) - raw = SDL_DisplayFormatAlpha(buf); - else - raw = SDL_DisplayFormat(buf); + raw = SDL_DisplayFormatAlpha(buf); SDL_FreeSurface(buf); } else { ERROR("Couldn't load surface '%s'\n", img.c_str()); diff --git a/src/surface.h b/src/surface.h index 6e3bc8b..98fbdcc 100644 --- a/src/surface.h +++ b/src/surface.h @@ -41,7 +41,7 @@ class Surface { public: Surface(); Surface(Surface *s); - Surface(const string &img, const string &skin="", bool alpha=true); + Surface(const string &img, const string &skin=""); ~Surface(); void enableVirtualDoubleBuffer(SDL_Surface *surface); @@ -75,7 +75,7 @@ public: private: SDL_PixelFormat *format(); - void load(const string &img, bool alpha=true, const string &skin=""); + void load(const string &img, const string &skin); bool blit(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); bool blitCenter(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); bool blitRight(SDL_Surface *destination, int x, int y, int w=0, int h=0, int a=-1); diff --git a/src/surfacecollection.cpp b/src/surfacecollection.cpp index 6d8d755..4774aa0 100644 --- a/src/surfacecollection.cpp +++ b/src/surfacecollection.cpp @@ -80,7 +80,7 @@ Surface *SurfaceCollection::add(const string &path) { return NULL; } else if (!fileExists(filePath)) return NULL; - Surface *s = new Surface(filePath, ""); + Surface *s = new Surface(filePath); surfaces[path] = s; return s; } @@ -94,7 +94,7 @@ Surface *SurfaceCollection::addSkinRes(const string &path) { string skinpath = getSkinFilePath(path); if (skinpath.empty()) return NULL; - Surface *s = new Surface(skinpath, "", true); + Surface *s = new Surface(skinpath); if (s != NULL) surfaces[path] = s; return s; From 8b17e2ef62bd1528c42e89e5d60bb9ba9ec350ee Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 2 Jun 2011 06:19:21 +0200 Subject: [PATCH 26/27] PNG: Make loadPNG() responsible for creating an RGBA surface. Before this commit loadPNG() could return any surface format and the caller was responsible for converting it to the desired format. However, in practice all callers want a surface with an alpha channel and SDL only supports that with 32bpp surfaces, so RGBA or a permutation thereof. So I changed the contract for loadPNG() so it is required to return an RGBA surface, and removed the conversion code in the callers. The next step is to replace IMG_Load() by a function that calls libpng directly and loads a fixed 32bpp pixel format. That way, we can drop the SDL_image dependency and avoid unnecessary pixel format conversions. --- src/asfont.cpp | 29 ++--------------------------- src/imageio.cpp | 34 +++++++++++++++++++++++++++++++--- src/imageio.h | 2 ++ src/surface.cpp | 7 ++----- 4 files changed, 37 insertions(+), 35 deletions(-) diff --git a/src/asfont.cpp b/src/asfont.cpp index c74015d..401bd12 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -11,35 +11,10 @@ ASFont::ASFont(const std::string &fontImagePath) : characters(SFONTPLUS_CHARSET) { - // Load PNG file into an SDL surface. - SDL_Surface *buf = loadPNG(fontImagePath); - if (!buf) { - surface = NULL; + surface = loadPNG(fontImagePath); + if (!surface) { return; } - - // Make sure we have a surface that can be blitted using alpha blending. - if (buf->format->BytesPerPixel == 4) { - surface = buf; - SDL_SetAlpha(surface, SDL_SRCALPHA, 255); - } else { - SDL_PixelFormat format32; - memset(&format32, 0, sizeof(format32)); - format32.BitsPerPixel = 32; - format32.BytesPerPixel = 4; - format32.Rmask = - SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x00FF0000 : 0x000000FF; - format32.Gmask = 0x0000FF00; - format32.Bmask = - SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x000000FF : 0x00FF0000; - format32.Amask = 0xFF000000; - format32.Rshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 16 : 0; - format32.Gshift = 8; - format32.Bshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0 : 16; - format32.Ashift = 24; - surface = SDL_ConvertSurface(buf, &format32, SDL_SRCALPHA); - SDL_FreeSurface(buf); - } assert(surface->format->BytesPerPixel == 4); SDL_LockSurface(surface); diff --git a/src/imageio.cpp b/src/imageio.cpp index aeebbcb..b9b8921 100644 --- a/src/imageio.cpp +++ b/src/imageio.cpp @@ -2,7 +2,35 @@ #include -SDL_Surface *loadPNG(const std::string &path) -{ - return IMG_Load(path.c_str()); +SDL_Surface *loadPNG(const std::string &path) { + // Load PNG file into an SDL surface. + SDL_Surface *surface = IMG_Load(path.c_str()); + if (!surface) { + return NULL; + } + + // Make sure we have a surface that can be blitted using alpha blending. + if (surface->format->BytesPerPixel == 4) { + SDL_SetAlpha(surface, SDL_SRCALPHA, 255); + return surface; + } else { + SDL_PixelFormat format32; + memset(&format32, 0, sizeof(format32)); + format32.BitsPerPixel = 32; + format32.BytesPerPixel = 4; + format32.Rmask = + SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x00FF0000 : 0x000000FF; + format32.Gmask = 0x0000FF00; + format32.Bmask = + SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0x000000FF : 0x00FF0000; + format32.Amask = 0xFF000000; + format32.Rshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 16 : 0; + format32.Gshift = 8; + format32.Bshift = SDL_BYTEORDER == SDL_BIG_ENDIAN ? 0 : 16; + format32.Ashift = 24; + SDL_Surface *surface32 = + SDL_ConvertSurface(surface, &format32, SDL_SRCALPHA); + SDL_FreeSurface(surface); + return surface32; + } } diff --git a/src/imageio.h b/src/imageio.h index 3e4e48f..54d4640 100644 --- a/src/imageio.h +++ b/src/imageio.h @@ -5,6 +5,8 @@ struct SDL_Surface; +/** Loads an image from a PNG file into a newly allocated 32bpp RGBA surface. + */ SDL_Surface *loadPNG(const std::string &path); #endif diff --git a/src/surface.cpp b/src/surface.cpp index 264ea3c..20808b8 100644 --- a/src/surface.cpp +++ b/src/surface.cpp @@ -90,11 +90,8 @@ void Surface::load(const string &img, const string &skin) { skinpath = img; } - SDL_Surface *buf = loadPNG(skinpath); - if (buf!=NULL) { - raw = SDL_DisplayFormatAlpha(buf); - SDL_FreeSurface(buf); - } else { + raw = loadPNG(skinpath); + if (!raw) { ERROR("Couldn't load surface '%s'\n", img.c_str()); } } From 5b6d922f111cdbf92f21e99141f382d8a0beebe5 Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Thu, 2 Jun 2011 07:13:59 +0200 Subject: [PATCH 27/27] Fixed bug: wallpaper does not use alpha channel. The unnecessary alpha channel might harm performance but is not a big deal. However, the fact that the alpha flag (bool) is somehow automatically converted by C++ into a skin name (string) causes the entire screen to stay black. The black screen was fixed by removing the alpha flag. For optimum blitting performance, the wallpaper surface is then converted to the pixel format of the frame buffer. Bug was introduced in 52c89d60055705e34afaafa7c2f1ab2840c2364e. --- src/gmenu2x.cpp | 3 ++- src/linkapp.cpp | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index fa1b1ed..06f75b5 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -361,7 +361,8 @@ void GMenu2X::initBG() { bg = new Surface(s); bg->box(0,0,resX,resY,0,0,0); } else { - bg = new Surface(confStr["wallpaper"],false); + // Note: Copy constructor converts to display format. + bg = new Surface(Surface(confStr["wallpaper"])); } drawTopBar(bg); diff --git a/src/linkapp.cpp b/src/linkapp.cpp index 95f4d28..2486899 100644 --- a/src/linkapp.cpp +++ b/src/linkapp.cpp @@ -301,7 +301,8 @@ void LinkApp::showManual() { gmenu2x->setClock(336); Surface pngman(manual); - Surface bg(gmenu2x->confStr["wallpaper"],false); + // Note: Copy constructor converts to display format. + Surface bg(Surface(gmenu2x->confStr["wallpaper"])); stringstream ss; string pageStatus;