--- /dev/null
+#testing text
+---
+en: This is the english.
+jp: このは日本語です。
+jprub: このは日本語(にほんご)です。
\ No newline at end of file
find_library(GL GL)
-add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp WorldObject.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp Collision.cpp LoadResources.cpp HUD.cpp)
+add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp WorldObject.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp Collision.cpp LoadResources.cpp HUD.cpp Font.cpp)
target_link_libraries(game yaml-cpp ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY} ${SDLTTF_LIBRARY} ${GL})
Level* Game::getCurrentLevel()
{
- if (mCurrentLevel < (size_t)mLevels.size()) {
+ if ((size_t)mCurrentLevel < (size_t)mLevels.size()) {
return mLevels[mCurrentLevel];
} else {
return NULL;
{
mLoadBehavior = DoNothing;
mUpdateBehavior = DoNothing;
+ mFonts.push_back(map<int,Font*>());
+ mFonts.push_back(map<int,Font*>());
}
-Level::Level(string n, Condition winCond, Behavior loadBehave, Behavior updateBehave, Behavior endBehave) : mName(n), mBackground(NULL), Loaded(false), mTextures(map<string, Texture>()), ToAdd(vector<WorldObject*>()), ToRemove(vector<WorldObject*>())
+Level::Level(string n, Condition winCond, Behavior loadBehave, Behavior updateBehave, Behavior endBehave) : mName(n), mBackground(NULL), Loaded(false), mTextures(map<string, Texture>()), ToAdd(vector<WorldObject*>()), ToRemove(vector<WorldObject*>()), mFonts(vector< map<int,Font*> >())
{
/// Set behavior
mLoadBehavior = loadBehave;
mUpdateBehavior = updateBehave;
mUnloadBehavior = endBehave;
mWinCondition = winCond;
+ mFonts.push_back(map<int,Font*>());
+ mFonts.push_back(map<int,Font*>());
}
Level::~Level()
}
return NULL;//if a sprite wasn't found return null
}
+
+Font* Level::getFont( int s = 24, FontType t = English){
+ map<int, Font*> font = t == English ? mFonts[0] : mFonts[1];
+ map<int, Font*>::iterator it = font.find(s);
+ //if the font doesn't exist
+ if(it!=font.end()){
+ //create it
+ Font* f = new Font(t,s);
+ font[s] = f;
+ return f;
+ } else{
+ Font* f = it->second;
+ f->count++;
+ return f;
+ }
+}
+
+void Level::closeFont( Font* f){
+ map<int, Font*> font = f->type == English ? mFonts[0] : mFonts[1];
+ map<int, Font*>::iterator it = font.find(f->size);
+
+ if(it != font.end()){
+ //if we're here we have a font
+ Font* f = it->second;
+ //decrement the count of texts using the font
+ f->count--;
+ //if there are none left
+ if(f->count==0){
+ //delete it
+ font.erase(it);
+ }
+ }
+}
\ No newline at end of file
#include "Sprite.h"
#include "Background.h"
#include "HUD.h"
+#include "Font.h"
#include <vector>
#include <map>
#include <string>
+
using std::vector;
using std::string;
Background* getBackground(){return mBackground;}/**< gets the current level's name \return Level's Background */
Sprite* findSpriteByName(string name);/**< returns the first Sprite with a given name \param name Name of the sprite to return \return Pointer to the requested sprite */
bool Loaded;/**< Shows if the level has loaded or not (level changing) */
- void Unload();/** What should be done when this level is left (not deleted) */
+ void Unload();/**< What should be done when this level is left (not deleted) */
+ Font* getFont( int s, FontType t);/**< Grabs the font if it exists and creates it if it does not */
+ void closeFont(Font *);/**< Closes the font if not more texts are using it. */
protected://allow inheritance
string mName;
map<string, Texture> mTextures;/**< Textures that have been loaded by the level. */
vector<WorldObject*> ToAdd;/**< List of sprites that should be added this game loop */
vector<WorldObject*> ToRemove;/**< List of sprites that should die after this game loop */
+ vector< map<int,Font*> > mFonts;/** List of all the fonts we have */
HUD mHUD;/**< Display to draw on everything else */
void DrawIMG();/**< Draws an image to the screen. (Not used) */
void DrawIMG(SDL_Surface *img, int x, int y);/**< Draws the specified image to the screen at location (x,y) */
#include "Text.h"
#include <iostream>
#include <string>
-//#include "Game.h"
+#include "Game.h"
#include "fns.h"
#include <GL/gl.h>
using namespace std;
-Text::Text(string name, string t, SDL_Color color) : WorldObject(name){
- // Load a font
- font = TTF_OpenFont("FreeSans.ttf", 24);
+Text::Text(string filename, FontType t, int s, SDL_Color c, int w, int h ) : WorldObject(filename), mFont(Game::game()->getCurrentLevel()->getFont(s, t)), color(c), width(w), height(h){
+
+}
+
+Text::Text(string name, string t, int s, SDL_Color c, int w, int h) : WorldObject(name), text(t), mFont(Game::game()->getCurrentLevel()->getFont(s, (FontType)0)), color(c), width(w), height(h){
- if(font){
- //SDL_Color text_color = {r,g, b};
- string txt="A journey of a thousand miles begins with a single step.";
- SDL_Rect rect = {0,0,100,100};
- draw();
- }
- else
- cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl;
+}
- TTF_CloseFont(font);
+Text::~Text(){
+ Game::game()->getCurrentLevel()->closeFont(mFont);
}
void Text::draw()
const char* txt = text.c_str();
SDL_Surface *initial;
SDL_Surface *intermediary;
- SDL_Rect location = {mPos.x, mPos.y, width, height};
int w,h;
Texture texture;
/* Use SDL_TTF to render our txt */
- initial = TTF_RenderUTF8_Blended(font, text.c_str(), color);
+ initial = TTF_RenderUTF8_Blended(mFont->font, text.c_str(), color);
/* Convert the rendered txt to a known format */
w = nextPow2(initial->w);
/* Draw a quad at location */
glBegin(GL_QUADS);
- glTexCoord2f(0.0f, 0.0f);
- glVertex2f(location.x , location.y);
- glTexCoord2f(1.0f, 0.0f);
- glVertex2f(location.x + w, location.y);
- glTexCoord2f(1.0f, 1.0f);
- glVertex2f(location.x + w, location.y + h);
- glTexCoord2f(0.0f, 1.0f);
- glVertex2f(location.x , location.y + h);
+ glTexCoord2d(0.0f, 0.0f);
+ glVertex2d(mPos.x , mPos.y);
+ glTexCoord2d(1.0f, 0.0f);
+ glVertex2d(mPos.x + w, mPos.y);
+ glTexCoord2d(1.0f, 1.0f);
+ glVertex2d(mPos.x + w, mPos.y + h);
+ glTexCoord2d(0.0f, 1.0f);
+ glVertex2d(mPos.x , mPos.y + h);
glEnd();
/* Bad things happen if we delete the texture before it finishes */
glFinish();
-
- /* return the deltas in the unused w,h part of the rect */
- location.w = initial->w;
- location.h = initial->h;
-
+
/* Clean up */
SDL_FreeSurface(initial);
SDL_FreeSurface(intermediary);
#include <SDL/SDL.h>
#include "SDL_ttf.h"
#include "WorldObject.h"
+#include "Font.h"
using namespace std;
+
/**
On-screen text
This class is tasked with the following:
- displaying text on screen.
*/
-
class Text : public WorldObject
{
public:
- TTF_Font* font;
+ Font* mFont;
string text;
SDL_Color color;
void draw();
void update();
int width;
int height;
- Text(string name, string t, SDL_Color color);
+ Text(string name, string t, int size = 24, SDL_Color c = WHITE, int w = 600, int h = 150);
+ Text(string filename, FontType t, int size = 24, SDL_Color c = WHITE, int w = 600, int h = 150);
+ ~Text();
};
#endif
SDL_Surface* LoadImage( std::string filename );/**< Loads supported images. */
+/** The types that our fonts can be */
+enum FontType{
+ English=0,
+ Japanese,
+ JapaneseWithRuby
+};
+
+const SDL_Color WHITE = {0xFF, 0xFF, 0xFF};
+
class SizeD;
/** 2D position or vector placed here because of general access*/
<ClInclude Include="Behaviors.h" />
<ClInclude Include="Collision.h" />
<ClInclude Include="fns.h" />
+ <ClInclude Include="Font.h" />
<ClInclude Include="Frame.h" />
<ClInclude Include="Game.h" />
<ClInclude Include="HUD.h" />
<ClCompile Include="Background.cpp" />
<ClCompile Include="Collision.cpp" />
<ClCompile Include="fns.cpp" />
+ <ClCompile Include="Font.cpp" />
<ClCompile Include="Game.cpp" />
<ClCompile Include="HUD.cpp" />
<ClCompile Include="Level.cpp" />