1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-07-02 17:52:54 +03:00

Merge branch 'master' of projects.qi-hardware.com:gmenu2x into install_locations

Conflicts:
	data/platform/nanonote/sections/terminals/ash
	src/gmenu2x.cpp
	src/surface.cpp
This commit is contained in:
Ayla 2011-06-02 12:16:26 +02:00
commit ef4b012026
38 changed files with 509 additions and 654 deletions

View File

@ -2,9 +2,10 @@
source /etc/profile
setfont2 /usr/share/setfont2/un-fuzzy-6x10-font.pnm
loadunimap /usr/share/setfont2/ben_uni.trans
clear
#setfont2 /usr/share/setfont2/un-fuzzy-6x10-font.pnm
#loadunimap /usr/share/setfont2/ben_uni.trans
cd /usr/share/gmenu2x
./gmenu2x
setfont /usr/share/kbd/consolefonts/kernel-6x11-font
clear
cd /usr/share/gmenu2x && ./gmenu2x

View File

@ -1,4 +0,0 @@
title=ash(Default)
icon=skin:icons/utilities-terminal.png
exec=/bin/ash
params=--login

View File

@ -0,0 +1,4 @@
title=ash(Color)
icon=skin:icons/utilities-terminal.png
exec=/usr/bin/setfont
params=/usr/share/kbd/consolefonts/kernel-6x11-font; /bin/ash --login

View File

@ -0,0 +1,4 @@
title=ash(Default)
icon=skin:icons/utilities-terminal.png
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

View File

@ -1 +1 @@
open.png
a.png

View File

@ -8,10 +8,11 @@ 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
browsedialog.cpp buttonbox.cpp dialog.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 \
@ -21,9 +22,10 @@ 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
browsedialog.h buttonbox.h dialog.h \
imageio.h powersaver.h
AM_CFLAGS= @CFLAGS@ @SDL_CFLAGS@

View File

@ -1,124 +1,196 @@
#include "asfont.h"
#include "imageio.h"
#include "surface.h"
#include "utilities.h"
#include <iostream>
#include <cassert>
#include <cstring>
using namespace std;
#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ"
ASFont::ASFont(SDL_Surface* font) {
this->font.initFont(font);
halfHeight = getHeight()/2;
halfLineHeight = getLineHeight()/2;
}
ASFont::ASFont(const std::string &fontImagePath)
: characters(SFONTPLUS_CHARSET)
{
surface = loadPNG(fontImagePath);
if (!surface) {
return;
}
assert(surface->format->BytesPerPixel == 4);
ASFont::ASFont(Surface* font) {
this->font.initFont(font->raw);
halfHeight = getHeight()/2;
halfLineHeight = getLineHeight()/2;
}
SDL_LockSurface(surface);
ASFont::ASFont(const string &font) {
this->font.initFont(font);
halfHeight = getHeight()/2;
halfLineHeight = getLineHeight()/2;
// Determine character widths.
Uint32 pink = SDL_MapRGB(surface->format, 255, 0, 255);
Uint32 *topLine = static_cast<Uint32 *>(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);
c++;
}
c++;
}
// 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 nonTransparentFound = false;
while (!nonTransparentFound && y-- > 0) {
Uint32 *line = reinterpret_cast<Uint32 *>(
reinterpret_cast<Uint8 *>(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;
SDL_UnlockSurface(surface);
}
ASFont::~ASFont() {
font.freeFont();
if (surface) {
SDL_FreeSurface(surface);
}
}
bool ASFont::utf8Code(unsigned char c) {
return font.utf8Code(c);
return (c>=194 && c<=198) || c==208 || c==209;
//return c>=194;
}
void ASFont::write(SDL_Surface* surface, const char* text, int x, int y) {
font.write(surface, text, x, y);
void ASFont::writeLine(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; i<text.length() && x<surface->w; i++) {
//Utf8 characters
if (utf8Code(text[i]) && i+1<text.length()) {
pos = characters.find(text.substr(i,2));
i++;
} else
pos = characters.find(text[i]);
if (pos == std::string::npos) {
x += charpos[2]-charpos[1];
continue;
}
pos *= 2;
srcrect.x = charpos[pos];
srcrect.w = charpos[pos+2] - charpos[pos];
dstrect.x = x - charpos[pos+1] + charpos[pos];
SDL_BlitSurface(surface, &srcrect, s->raw, &dstrect);
x += charpos[pos+2] - charpos[pos+1];
}
}
void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, const unsigned short halign, const unsigned short valign) {
int ASFont::getTextWidth(const char *text) {
int maxWidth = 0, width = 0;
while (char ch = *text++) {
if (ch == '\n') {
// New line.
maxWidth = max(width, maxWidth);
width = 0;
} else {
std::string::size_type pos;
if (utf8Code(ch) && *text) {
// 2-byte character.
pos = characters.find(std::string(&text[-1], 2));
text++;
} else {
// 1-byte character.
pos = characters.find(ch);
}
if (pos == std::string::npos) {
pos = 0;
}
width += charpos[pos * 2 + 2] - charpos[pos * 2 + 1];
}
}
return max(width, maxWidth);
}
int ASFont::getTextWidth(const std::string& text) {
return getTextWidth(text.c_str());
}
void ASFont::writeLine(Surface* surface, const std::string& text, int x, int y, HAlign halign) {
switch (halign) {
case SFontHAlignCenter:
x -= getTextWidth(text)/2;
case HAlignLeft:
break;
case SFontHAlignRight:
case HAlignCenter:
x -= getTextWidth(text) / 2;
break;
case HAlignRight:
x -= getTextWidth(text);
break;
}
writeLine(surface, text, x, y);
}
void ASFont::writeLine(Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) {
switch (valign) {
case SFontVAlignMiddle:
y -= getHalfHeight();
case VAlignTop:
break;
case SFontVAlignBottom:
case VAlignMiddle:
y -= getHeight() / 2;
break;
case VAlignBottom:
y -= getHeight();
break;
}
font.write(surface, text, x, y);
writeLine(surface, text, x, y, halign);
}
void ASFont::write(SDL_Surface* surface, vector<string> *text, int x, int y, const unsigned short halign, const unsigned short valign) {
void ASFont::writeLine(Surface* surface, const std::vector<std::string> &text, int x, int y, HAlign halign, VAlign valign) {
switch (valign) {
case SFontVAlignMiddle:
y -= getHalfHeight()*text->size();
case VAlignTop:
break;
case SFontVAlignBottom:
y -= getHeight()*text->size();
case VAlignMiddle:
y -= (getHeight() / 2) * text.size();
break;
case VAlignBottom:
y -= getHeight() * text.size();
break;
}
for (uint i=0; i<text->size(); i++) {
int ix = x;
switch (halign) {
case SFontHAlignCenter:
ix -= getTextWidth(text->at(i))/2;
break;
case SFontHAlignRight:
ix -= getTextWidth(text->at(i));
break;
}
font.write(surface, text->at(i), x, y+getHeight()*i);
for (std::vector<std::string>::const_iterator it = text.begin(); it != text.end(); ++it) {
write(surface, *it, x, y, halign);
y += getHeight();
}
}
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<string> textArr;
split(textArr,text,"\n");
write(surface->raw, &textArr, x, y, halign, 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<std::string> textArr;
split(textArr, text, "\n");
writeLine(surface, textArr, x, y, halign, valign);
} else
write(surface->raw, text, x, y, halign, valign);
}
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);
}
int ASFont::getTextWidth(const std::string& text) {
if (text.find("\n",0)!=string::npos) {
vector<string> textArr;
split(textArr,text,"\n");
return getTextWidth(&textArr);
} else
return getTextWidth(text.c_str());
}
int ASFont::getTextWidth(vector<string> *text) {
int w = 0;
for (uint i=0; i<text->size(); i++)
w = max( getTextWidth(text->at(i).c_str()), w );
return w;
writeLine(surface, text, x, y, halign, valign);
}

View File

@ -1,49 +1,49 @@
//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 <SDL.h>
#include <string>
#include <vector>
#include <SDL.h>
#include "sfontplus.h"
using std::string;
using std::vector;
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 Surface;
class ASFont {
public:
ASFont(SDL_Surface* font);
ASFont(Surface* font);
ASFont(const string &font);
enum HAlign { HAlignLeft, HAlignRight, HAlignCenter };
enum VAlign { VAlignTop, VAlignBottom, VAlignMiddle };
ASFont(const std::string &font);
~ASFont();
bool utf8Code(unsigned char c);
int getHeight();
int getHalfHeight();
int getLineHeight();
int getHalfLineHeight();
int getTextWidth(const char* text);
int getTextWidth(const string& text);
int getTextWidth(vector<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, const unsigned short halign = 0, const unsigned short valign = 0);
void write(SDL_Surface* surface, vector<string> *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);
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:
SFontPlus font;
int halfHeight, halfLineHeight;
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<std::string> &text, int x, int y, HAlign halign, VAlign valign);
SDL_Surface *surface;
std::vector<unsigned> charpos;
std::string characters;
int lineHeight;
};
#endif /* ASFONT_H */

View File

@ -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;

View File

@ -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);
}

View File

@ -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"
@ -215,6 +214,7 @@ void GMenu2X::gp2x_tvout_off() {
#endif
}
GMenu2X::GMenu2X() {
//Detect firmware version and type
if (fileExists("/etc/open2x")) {
@ -292,7 +292,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();
}
@ -334,7 +334,7 @@ GMenu2X::GMenu2X() {
}
input.init(input_file);
PowerSaver::getInstance()->setScreenTimeout( confInt["backlightTimeout"] );
setInputSpeed();
initServices();
setBacklight(confInt["backlight"]);
@ -345,6 +345,7 @@ GMenu2X::GMenu2X() {
readTmp();
if (lastSelectorElement>-1 && menu->selLinkApp()!=NULL && (!menu->selLinkApp()->getSelectorDir().empty() || !lastSelectorDir.empty()))
menu->selLinkApp()->selector(lastSelectorElement,lastSelectorDir);
}
GMenu2X::~GMenu2X() {
@ -384,7 +385,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);
@ -399,7 +401,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;
@ -587,6 +589,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
@ -850,7 +853,6 @@ void GMenu2X::main() {
if (!fileExists(CARD_ROOT))
CARD_ROOT = "/";
while (!quit) {
tickNow = SDL_GetTicks();
@ -878,7 +880,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
@ -907,10 +909,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);
@ -964,7 +966,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();
@ -1149,6 +1151,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));
@ -1157,6 +1160,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);
@ -1377,7 +1381,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;
@ -1414,7 +1417,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; i<voices.size(); i++)
s->write( 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+5+(h+2)*i, ASFont::HAlignLeft, ASFont::VAlignTop );
s->flip();
//touchscreen
@ -1693,7 +1696,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;
@ -2039,7 +2042,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;
@ -2051,7 +2054,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;

View File

@ -30,6 +30,7 @@
#include "inputmanager.h"
#include "asfont.h"
#include "surface.h"
#include "powersaver.h"
#include <iostream>
#include <string>
@ -131,6 +132,7 @@ private:
usbnet,
samba,
web;
string ip, defaultgw, lastSelectorDir;
int lastSelectorElement;
void readConfig();

View File

@ -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) {

View File

@ -1,8 +1,10 @@
#ifndef ICONBUTTON_H
#define ICONBUTTON_H
#include <string>
#include "button.h"
#include "asfont.h"
#include <string>
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;

36
src/imageio.cpp Normal file
View File

@ -0,0 +1,36 @@
#include "imageio.h"
#include <SDL_image.h>
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;
}
}

12
src/imageio.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef IMAGEIO_H
#define IMAGEIO_H
#include <string>
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

View File

@ -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)) {

View File

@ -21,6 +21,7 @@
#include "debug.h"
#include "inputmanager.h"
#include "utilities.h"
#include "powersaver.h"
#include <iostream>
#include <fstream>
@ -179,6 +180,8 @@ bool InputManager::getEvent(bevent_t *bevent, bool wait) {
break;
}
}
if ( wait ) {
PowerSaver::getInstance()->resetScreenTimer();
}
return true;
}

View File

@ -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;

View File

@ -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();
}
@ -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;
@ -329,7 +330,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;

View File

@ -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, ASFont::HAlignLeft, ASFont::VAlignTop );
}
void MenuSetting::handleTS()

View File

@ -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, ASFont::HAlignLeft, ASFont::VAlignTop );
}
void MenuSettingBool::manageInput(bevent_t *event)

View File

@ -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, ASFont::HAlignLeft, ASFont::VAlignTop );
}
void MenuSettingInt::manageInput(bevent_t *event)

View File

@ -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, 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() {
@ -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()

View File

@ -34,6 +34,7 @@ private:
RGBAColor originalValue;
RGBAColor *_value;
void update_value(int value);
void dec();
void inc();
void leftComponent();

View File

@ -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(),
SFontHAlignLeft, SFontVAlignMiddle);
gmenu2x->s->write(gmenu2x->font, value(), 155, y,
ASFont::HAlignLeft, ASFont::VAlignTop);
}
void MenuSettingStringBase::manageInput(bevent_t *event)

View File

@ -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; i<buttons.size(); i++) {

90
src/powersaver.cpp Normal file
View File

@ -0,0 +1,90 @@
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#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);
}
}

25
src/powersaver.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef POWERSAVER_H
#define POWERSAVER_H
#include <SDL.h>
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

View File

@ -106,9 +106,9 @@ int Selector::exec(int startSelection) {
iY = i-firstElement;
if (fl.isDirectory(i)) {
gmenu2x->sc["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();

View File

@ -1,195 +0,0 @@
#include "sfontplus.h"
#include <cassert>
#include <SDL_image.h>
#include <iostream>
using namespace std;
Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) {
assert(x>=0);
assert(x<surface->w);
assert(y>=0);
assert(y<surface->h);
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 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 string &font, const string &characters) {
SDL_Surface *buf = IMG_Load(font.c_str());
if (buf!=NULL) {
initFont( SDL_DisplayFormatAlpha(buf), characters );
SDL_FreeSurface(buf);
}
}
void SFontPlus::initFont(SDL_Surface *font, const 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 (uint x=0; x<characters.length(); x++) {
if (!utf8) utf8 = (unsigned char)characters[x]>128;
if (utf8) DEBUG("%d\n", (unsigned char)characters[x]);
}
#endif
uint c = 0;
SDL_LockSurface(surface);
for (uint x=0; x<(uint)surface->w && c<characters.length(); x++) {
if (getPixel(x,0) == pink) {
uint startx = x;
charpos.push_back(x);
x++;
while (x<(uint)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++;
}
}
SDL_UnlockSurface(surface);
Uint32 colKey = getPixel(0,surface->h-1);
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey);
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;
bool nonKeyFound = false;
while (y-- > 0 && !nonKeyFound) {
uint 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 string &text, int x, int y) {
if (text.empty()) return;
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(uint i=0; i<text.length() && x<surface->w; i++) {
//Utf8 characters
if (utf8Code(text[i]) && i+1<text.length()) {
pos = characters.find(text.substr(i,2));
i++;
} else
pos = characters.find(text[i]);
if (pos == string::npos) {
x += charpos[2]-charpos[1];
continue;
}
pos *= 2;
srcrect.x = charpos[pos];
srcrect.w = charpos[pos+2] - charpos[pos];
dstrect.x = x - charpos[pos+1] + charpos[pos];
SDL_BlitSurface(surface, &srcrect, s, &dstrect);
x += charpos[pos+2] - charpos[pos+1];
}
}
uint SFontPlus::getTextWidth(const string &text) {
string::size_type pos;
int width = 0;
for(uint x=0; x<text.length(); x++) {
//Utf8 characters
if (utf8Code(text[x]) && x+1<text.length()) {
pos = characters.find(text.substr(x,2));
x++;
} else
pos = characters.find(text[x]);
if (pos == string::npos) {
width += charpos[2]-charpos[1];
continue;
}
pos *= 2;
width += charpos[pos+2] - charpos[pos+1];
}
return width;
}
uint SFontPlus::getHeight() {
return surface->h - 1;
}
uint SFontPlus::getLineHeight() {
return lineHeight;
}

View File

@ -1,43 +0,0 @@
#ifndef SFONTPLUS_H
#define SFONTPLUS_H
#include <SDL.h>
#include <string>
#include <vector>
#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<uint> charpos;
string characters;
uint height, lineHeight;
public:
SFontPlus();
SFontPlus(SDL_Surface *font);
SFontPlus(const 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 freeFont();
void write(SDL_Surface *s, const string &text, int x, int y);
uint getTextWidth(const string &text);
uint getHeight();
uint getLineHeight();
};
#endif /* SFONTPLUS_H */

View File

@ -18,16 +18,17 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "surface.h"
#include "imageio.h"
#include "utilities.h"
#include "debug.h"
#include "surfacecollection.h"
#include <SDL_gfxPrimitives.h>
#include <iostream>
using namespace std;
#include "surface.h"
#include "utilities.h"
#include "debug.h"
#include "surfacecollection.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 );
@ -42,54 +43,19 @@ Surface::Surface() {
dblbuffer = NULL;
}
Surface::Surface(const string &img, bool alpha, const string &skin) {
raw = NULL;
dblbuffer = NULL;
load(img, alpha, skin);
halfW = raw->w/2;
halfH = raw->h/2;
}
Surface::Surface(const string &img, const string &skin, bool alpha) {
raw = NULL;
dblbuffer = NULL;
load(img, alpha, skin);
halfW = raw->w/2;
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);
raw = SDL_DisplayFormat(s->raw);
halfW = raw->w/2;
halfH = raw->h/2;
}
Surface::Surface(int w, int h, Uint32 flags) {
Surface::Surface(const string &img, const string &skin) {
raw = NULL;
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;
load(img, skin);
halfW = raw->w/2;
halfH = raw->h/2;
}
Surface::~Surface() {
@ -113,7 +79,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;
@ -122,35 +88,12 @@ void Surface::load(const string &img, bool alpha, const string &skin) {
else
skinpath = img;
SDL_Surface *buf = IMG_Load(skinpath.c_str());
if (buf!=NULL) {
if (alpha)
raw = SDL_DisplayFormatAlpha(buf);
else
raw = SDL_DisplayFormat(buf);
SDL_FreeSurface(buf);
} else {
raw = loadPNG(skinpath);
if (!raw) {
ERROR("Couldn't load surface '%s'\n", img.c_str());
}
}
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);
@ -194,122 +137,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; iy<raw->h; iy++)
if (iy+y >= 0 && iy+y < target->raw->h)
for (int ix=0; ix<raw->w; 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; iy<raw->h; 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; ix<raw->w; 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::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;
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);
}
@ -320,9 +166,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);
@ -337,21 +180,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;
}

View File

@ -20,9 +20,8 @@
#ifndef SURFACE_H
#define SURFACE_H
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <string>
#include "asfont.h"
@ -39,18 +38,10 @@ RGBAColor strtorgba(const string &strColor);
@author Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
*/
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(const string &img, const string &skin="");
~Surface();
void enableVirtualDoubleBuffer(SDL_Surface *surface);
@ -58,50 +49,40 @@ 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);
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(SDL_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 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);
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, 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

View File

@ -24,12 +24,14 @@
#include "debug.h"
#include "gmenu2x.h"
#include <iostream>
using std::endl;
using std::string;
SurfaceCollection::SurfaceCollection(bool defaultAlpha, const string &skin) {
this->defaultAlpha = defaultAlpha;
setSkin(skin);
SurfaceCollection::SurfaceCollection()
: skin("default")
{
}
SurfaceCollection::~SurfaceCollection() {}
@ -83,7 +85,7 @@ string SurfaceCollection::getSkinFilePath(const string &skin, 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());
}
}
@ -97,7 +99,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);
@ -109,12 +111,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;
@ -123,7 +125,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);
if (s != NULL)
surfaces[path] = s;
return s;
@ -154,7 +156,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;
}
@ -164,7 +166,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;
}

View File

@ -33,13 +33,8 @@ Hash Map of surfaces that loads surfaces not already loaded and reuses already l
@author Massimiliano Torromeo <massimiliano.torromeo@gmail.com>
*/
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);
@ -51,8 +46,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);
@ -60,6 +55,10 @@ public:
Surface *operator[](const std::string &);
Surface *skinRes(const std::string &);
private:
SurfaceHash surfaces;
std::string skin;
};
#endif

View File

@ -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();

View File

@ -95,7 +95,7 @@ bool WallpaperDialog::exec()
gmenu2x->s->setClipRect(0,41,311,179);
for (i=firstElement; i<wallpapers.size() && i<firstElement+10; i++) {
iY = i-firstElement;
gmenu2x->s->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();