mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-11-29 09:59:41 +02:00
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.
This commit is contained in:
parent
e6be835038
commit
c54dec90f5
@ -2,10 +2,6 @@
|
|||||||
#include "surface.h"
|
#include "surface.h"
|
||||||
#include "utilities.h"
|
#include "utilities.h"
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
ASFont::ASFont(SDL_Surface* font) {
|
ASFont::ASFont(SDL_Surface* font) {
|
||||||
this->font.initFont(font);
|
this->font.initFont(font);
|
||||||
halfHeight = getHeight()/2;
|
halfHeight = getHeight()/2;
|
||||||
@ -18,7 +14,7 @@ ASFont::ASFont(Surface* font) {
|
|||||||
halfLineHeight = getLineHeight()/2;
|
halfLineHeight = getLineHeight()/2;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASFont::ASFont(const string &font) {
|
ASFont::ASFont(const std::string &font) {
|
||||||
this->font.initFont(font);
|
this->font.initFont(font);
|
||||||
halfHeight = getHeight()/2;
|
halfHeight = getHeight()/2;
|
||||||
halfLineHeight = getLineHeight()/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);
|
font.write(surface, text, x, y);
|
||||||
}
|
}
|
||||||
void ASFont::write(SDL_Surface* surface, vector<string> *text, int x, int y, const unsigned short halign, const unsigned short valign) {
|
void ASFont::write(SDL_Surface* surface, std::vector<std::string> *text, int x, int y, const unsigned short halign, const unsigned short valign) {
|
||||||
switch (valign) {
|
switch (valign) {
|
||||||
case SFontVAlignMiddle:
|
case SFontVAlignMiddle:
|
||||||
y -= getHalfHeight()*text->size();
|
y -= getHalfHeight()*text->size();
|
||||||
@ -67,7 +63,7 @@ void ASFont::write(SDL_Surface* surface, vector<string> *text, int x, int y, con
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint i=0; i<text->size(); i++) {
|
for (unsigned i=0; i<text->size(); i++) {
|
||||||
int ix = x;
|
int ix = x;
|
||||||
switch (halign) {
|
switch (halign) {
|
||||||
case SFontHAlignCenter:
|
case SFontHAlignCenter:
|
||||||
@ -83,8 +79,8 @@ void ASFont::write(SDL_Surface* surface, vector<string> *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) {
|
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) {
|
if (text.find("\n", 0) != std::string::npos) {
|
||||||
vector<string> textArr;
|
std::vector<std::string> textArr;
|
||||||
split(textArr,text,"\n");
|
split(textArr,text,"\n");
|
||||||
write(surface->raw, &textArr, x, y, halign, valign);
|
write(surface->raw, &textArr, x, y, halign, valign);
|
||||||
} else
|
} else
|
||||||
@ -109,16 +105,16 @@ int ASFont::getTextWidth(const char* text) {
|
|||||||
return font.getTextWidth(text);
|
return font.getTextWidth(text);
|
||||||
}
|
}
|
||||||
int ASFont::getTextWidth(const std::string& text) {
|
int ASFont::getTextWidth(const std::string& text) {
|
||||||
if (text.find("\n",0)!=string::npos) {
|
if (text.find("\n", 0) != std::string::npos) {
|
||||||
vector<string> textArr;
|
std::vector<std::string> textArr;
|
||||||
split(textArr,text,"\n");
|
split(textArr,text,"\n");
|
||||||
return getTextWidth(&textArr);
|
return getTextWidth(&textArr);
|
||||||
} else
|
} else
|
||||||
return getTextWidth(text.c_str());
|
return getTextWidth(text.c_str());
|
||||||
}
|
}
|
||||||
int ASFont::getTextWidth(vector<string> *text) {
|
int ASFont::getTextWidth(std::vector<std::string> *text) {
|
||||||
int w = 0;
|
int w = 0;
|
||||||
for (uint i=0; i<text->size(); i++)
|
for (unsigned i=0; i<text->size(); i++)
|
||||||
w = max( getTextWidth(text->at(i).c_str()), w );
|
w = max( getTextWidth(text->at(i).c_str()), w );
|
||||||
return w;
|
return w;
|
||||||
}
|
}
|
||||||
|
20
src/asfont.h
20
src/asfont.h
@ -3,13 +3,13 @@
|
|||||||
#ifndef ASFONT_H
|
#ifndef ASFONT_H
|
||||||
#define ASFONT_H
|
#define ASFONT_H
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <vector>
|
|
||||||
#include <SDL.h>
|
|
||||||
#include "sfontplus.h"
|
#include "sfontplus.h"
|
||||||
|
|
||||||
using std::string;
|
#include <string>
|
||||||
using std::vector;
|
#include <vector>
|
||||||
|
|
||||||
|
struct SDL_Surface;
|
||||||
|
class Surface;
|
||||||
|
|
||||||
const unsigned short SFontHAlignLeft = 0;
|
const unsigned short SFontHAlignLeft = 0;
|
||||||
const unsigned short SFontHAlignRight = 1;
|
const unsigned short SFontHAlignRight = 1;
|
||||||
@ -18,13 +18,11 @@ const unsigned short SFontVAlignTop = 0;
|
|||||||
const unsigned short SFontVAlignBottom = 1;
|
const unsigned short SFontVAlignBottom = 1;
|
||||||
const unsigned short SFontVAlignMiddle = 2;
|
const unsigned short SFontVAlignMiddle = 2;
|
||||||
|
|
||||||
class Surface;
|
|
||||||
|
|
||||||
class ASFont {
|
class ASFont {
|
||||||
public:
|
public:
|
||||||
ASFont(SDL_Surface* font);
|
ASFont(SDL_Surface* font);
|
||||||
ASFont(Surface* font);
|
ASFont(Surface* font);
|
||||||
ASFont(const string &font);
|
ASFont(const std::string &font);
|
||||||
~ASFont();
|
~ASFont();
|
||||||
|
|
||||||
bool utf8Code(unsigned char c);
|
bool utf8Code(unsigned char c);
|
||||||
@ -34,11 +32,11 @@ public:
|
|||||||
int getLineHeight();
|
int getLineHeight();
|
||||||
int getHalfLineHeight();
|
int getHalfLineHeight();
|
||||||
int getTextWidth(const char* text);
|
int getTextWidth(const char* text);
|
||||||
int getTextWidth(const string& text);
|
int getTextWidth(const std::string& text);
|
||||||
int getTextWidth(vector<string> *text);
|
int getTextWidth(std::vector<std::string> *text);
|
||||||
void write(SDL_Surface* surface, const char* text, int x, int y);
|
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, 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(SDL_Surface* surface, std::vector<std::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);
|
void write(Surface* surface, const std::string& text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -1,11 +1,7 @@
|
|||||||
#include "sfontplus.h"
|
#include "sfontplus.h"
|
||||||
|
|
||||||
#include "imageio.h"
|
#include "imageio.h"
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) {
|
Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) {
|
||||||
assert(x>=0);
|
assert(x>=0);
|
||||||
@ -49,7 +45,7 @@ SFontPlus::SFontPlus(SDL_Surface* font) {
|
|||||||
initFont(font);
|
initFont(font);
|
||||||
}
|
}
|
||||||
|
|
||||||
SFontPlus::SFontPlus(const string &font) {
|
SFontPlus::SFontPlus(const std::string &font) {
|
||||||
surface = NULL;
|
surface = NULL;
|
||||||
initFont(font);
|
initFont(font);
|
||||||
}
|
}
|
||||||
@ -63,7 +59,7 @@ bool SFontPlus::utf8Code(unsigned char c) {
|
|||||||
//return c>=194;
|
//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);
|
SDL_Surface *buf = loadPNG(font);
|
||||||
if (buf!=NULL) {
|
if (buf!=NULL) {
|
||||||
initFont( SDL_DisplayFormatAlpha(buf), characters );
|
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();
|
freeFont();
|
||||||
this->characters = characters;
|
this->characters = characters;
|
||||||
if (font==NULL) return;
|
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);
|
Uint32 pink = SDL_MapRGB(surface->format, 255,0,255);
|
||||||
#ifdef DEBUG
|
#ifdef DEBUG
|
||||||
bool utf8 = false;
|
bool utf8 = false;
|
||||||
for (uint x=0; x<characters.length(); x++) {
|
for (unsigned x=0; x<characters.length(); x++) {
|
||||||
if (!utf8) utf8 = (unsigned char)characters[x]>128;
|
if (!utf8) utf8 = (unsigned char)characters[x]>128;
|
||||||
if (utf8) DEBUG("%d\n", (unsigned char)characters[x]);
|
if (utf8) DEBUG("%d\n", (unsigned char)characters[x]);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
uint c = 0;
|
unsigned c = 0;
|
||||||
|
|
||||||
SDL_LockSurface(surface);
|
SDL_LockSurface(surface);
|
||||||
for (uint x=0; x<(uint)surface->w && c<characters.length(); x++) {
|
for (unsigned x=0; x<(unsigned)surface->w && c<characters.length(); x++) {
|
||||||
if (getPixel(x,0) == pink) {
|
if (getPixel(x,0) == pink) {
|
||||||
uint startx = x;
|
unsigned startx = x;
|
||||||
charpos.push_back(x);
|
charpos.push_back(x);
|
||||||
|
|
||||||
x++;
|
x++;
|
||||||
while (x<(uint)surface->w && getPixel(x,0) == pink) x++;
|
while (x<(unsigned)surface->w && getPixel(x,0) == pink) x++;
|
||||||
charpos.push_back(x);
|
charpos.push_back(x);
|
||||||
|
|
||||||
//utf8 characters
|
//utf8 characters
|
||||||
@ -110,12 +106,12 @@ void SFontPlus::initFont(SDL_Surface *font, const string &characters) {
|
|||||||
SDL_UnlockSurface(surface);
|
SDL_UnlockSurface(surface);
|
||||||
Uint32 colKey = getPixel(0,surface->h-1);
|
Uint32 colKey = getPixel(0,surface->h-1);
|
||||||
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey);
|
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};
|
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;
|
bool nonKeyFound = false;
|
||||||
while (y-- > 0 && !nonKeyFound) {
|
while (y-- > 0 && !nonKeyFound) {
|
||||||
uint x = srcrect.w;
|
unsigned x = srcrect.w;
|
||||||
while (x-- > 0 && !nonKeyFound)
|
while (x-- > 0 && !nonKeyFound)
|
||||||
nonKeyFound = getPixel(x+srcrect.x,y+srcrect.y) != colKey;
|
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;
|
if (text.empty()) return;
|
||||||
|
|
||||||
string::size_type pos;
|
std::string::size_type pos;
|
||||||
SDL_Rect srcrect, dstrect;
|
SDL_Rect srcrect, dstrect;
|
||||||
|
|
||||||
// these values won't change in the loop
|
// 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;
|
dstrect.y = y;
|
||||||
srcrect.h = dstrect.h = surface->h-1;
|
srcrect.h = dstrect.h = surface->h-1;
|
||||||
|
|
||||||
for(uint i=0; i<text.length() && x<surface->w; i++) {
|
for(unsigned i=0; i<text.length() && x<surface->w; i++) {
|
||||||
//Utf8 characters
|
//Utf8 characters
|
||||||
if (utf8Code(text[i]) && i+1<text.length()) {
|
if (utf8Code(text[i]) && i+1<text.length()) {
|
||||||
pos = characters.find(text.substr(i,2));
|
pos = characters.find(text.substr(i,2));
|
||||||
i++;
|
i++;
|
||||||
} else
|
} else
|
||||||
pos = characters.find(text[i]);
|
pos = characters.find(text[i]);
|
||||||
if (pos == string::npos) {
|
if (pos == std::string::npos) {
|
||||||
x += charpos[2]-charpos[1];
|
x += charpos[2]-charpos[1];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -164,18 +160,18 @@ void SFontPlus::write(SDL_Surface *s, const string &text, int x, int y) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
uint SFontPlus::getTextWidth(const string &text) {
|
unsigned SFontPlus::getTextWidth(const std::string &text) {
|
||||||
string::size_type pos;
|
std::string::size_type pos;
|
||||||
int width = 0;
|
int width = 0;
|
||||||
|
|
||||||
for(uint x=0; x<text.length(); x++) {
|
for(unsigned x=0; x<text.length(); x++) {
|
||||||
//Utf8 characters
|
//Utf8 characters
|
||||||
if (utf8Code(text[x]) && x+1<text.length()) {
|
if (utf8Code(text[x]) && x+1<text.length()) {
|
||||||
pos = characters.find(text.substr(x,2));
|
pos = characters.find(text.substr(x,2));
|
||||||
x++;
|
x++;
|
||||||
} else
|
} else
|
||||||
pos = characters.find(text[x]);
|
pos = characters.find(text[x]);
|
||||||
if (pos == string::npos) {
|
if (pos == std::string::npos) {
|
||||||
width += charpos[2]-charpos[1];
|
width += charpos[2]-charpos[1];
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -187,10 +183,10 @@ uint SFontPlus::getTextWidth(const string &text) {
|
|||||||
return width;
|
return width;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint SFontPlus::getHeight() {
|
unsigned SFontPlus::getHeight() {
|
||||||
return surface->h - 1;
|
return surface->h - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint SFontPlus::getLineHeight() {
|
unsigned SFontPlus::getLineHeight() {
|
||||||
return lineHeight;
|
return lineHeight;
|
||||||
}
|
}
|
||||||
|
@ -6,38 +6,33 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ"
|
#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ"
|
||||||
#ifdef _WIN32
|
|
||||||
typedef unsigned int uint;
|
|
||||||
#endif
|
|
||||||
using std::vector;
|
|
||||||
using std::string;
|
|
||||||
|
|
||||||
class SFontPlus {
|
class SFontPlus {
|
||||||
private:
|
private:
|
||||||
Uint32 getPixel(Sint32 x, Sint32 y);
|
Uint32 getPixel(Sint32 x, Sint32 y);
|
||||||
|
|
||||||
SDL_Surface *surface;
|
SDL_Surface *surface;
|
||||||
vector<uint> charpos;
|
std::vector<unsigned> charpos;
|
||||||
string characters;
|
std::string characters;
|
||||||
uint height, lineHeight;
|
unsigned height, lineHeight;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
SFontPlus();
|
SFontPlus();
|
||||||
SFontPlus(SDL_Surface *font);
|
SFontPlus(SDL_Surface *font);
|
||||||
SFontPlus(const string &font);
|
SFontPlus(const std::string &font);
|
||||||
~SFontPlus();
|
~SFontPlus();
|
||||||
|
|
||||||
bool utf8Code(unsigned char c);
|
bool utf8Code(unsigned char c);
|
||||||
|
|
||||||
void initFont(SDL_Surface *font, const string &characters = SFONTPLUS_CHARSET);
|
void initFont(SDL_Surface *font, const std::string &characters = SFONTPLUS_CHARSET);
|
||||||
void initFont(const string &font, const string &characters = SFONTPLUS_CHARSET);
|
void initFont(const std::string &font, const std::string &characters = SFONTPLUS_CHARSET);
|
||||||
void freeFont();
|
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);
|
unsigned getTextWidth(const std::string &text);
|
||||||
uint getHeight();
|
unsigned getHeight();
|
||||||
uint getLineHeight();
|
unsigned getLineHeight();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* SFONTPLUS_H */
|
#endif /* SFONTPLUS_H */
|
||||||
|
Loading…
Reference in New Issue
Block a user