Added Font class and updated other bits to make things work
authorIvan Hernanez <iturtleman128@gmail.com>
Fri, 1 Jun 2012 08:02:57 +0000 (03:02 -0500)
committerIvan Hernanez <iturtleman128@gmail.com>
Fri, 1 Jun 2012 08:02:57 +0000 (03:02 -0500)
ArtAssets/Fonts/text.bmp [deleted file]
ArtAssets/Story/intro.txt [new file with mode: 0644]
CMakeLists.txt
Game.cpp
Level.cpp
Level.h
Text.cpp
Text.h
fns.h
game.vcxproj

diff --git a/ArtAssets/Fonts/text.bmp b/ArtAssets/Fonts/text.bmp
deleted file mode 100644 (file)
index b6c1ec7..0000000
Binary files a/ArtAssets/Fonts/text.bmp and /dev/null differ
diff --git a/ArtAssets/Story/intro.txt b/ArtAssets/Story/intro.txt
new file mode 100644 (file)
index 0000000..3d72445
--- /dev/null
@@ -0,0 +1,5 @@
+#testing text
+---
+en: This is the english.
+jp: このは日本語です。
+jprub: このは日本語(にほんご)です。
\ No newline at end of file
index 9b03e2f..4aa04b2 100644 (file)
@@ -11,6 +11,6 @@ find_package(SDL_ttf REQUIRED)
 
 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})
index de2c512..49b3d6b 100644 (file)
--- a/Game.cpp
+++ b/Game.cpp
@@ -96,7 +96,7 @@ Game::~Game()
 
 Level* Game::getCurrentLevel()
 {
-       if (mCurrentLevel < (size_t)mLevels.size()) {
+       if ((size_t)mCurrentLevel < (size_t)mLevels.size()) {
                return mLevels[mCurrentLevel];
        } else {
                return NULL;
index d2fcd46..0b99d85 100644 (file)
--- a/Level.cpp
+++ b/Level.cpp
@@ -8,15 +8,19 @@ Level::Level()
 {
        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()
@@ -273,3 +277,36 @@ Sprite* Level::findSpriteByName(string name){
        }
        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
diff --git a/Level.h b/Level.h
index 27cde98..f572473 100644 (file)
--- a/Level.h
+++ b/Level.h
@@ -3,10 +3,12 @@
 #include "Sprite.h"
 #include "Background.h"
 #include "HUD.h"
+#include "Font.h"
 #include <vector>
 #include <map>
 #include <string>
 
+
 using std::vector;
 using std::string;
 
@@ -52,7 +54,9 @@ public:
        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;
@@ -64,6 +68,7 @@ protected://allow inheritance
        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) */
index 6807189..6af939c 100644 (file)
--- a/Text.cpp
+++ b/Text.cpp
@@ -1,25 +1,21 @@
 #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()
@@ -27,12 +23,11 @@ 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);
@@ -59,23 +54,19 @@ void Text::draw()
        /* 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);
diff --git a/Text.h b/Text.h
index 111a9f1..c7de0f8 100644 (file)
--- a/Text.h
+++ b/Text.h
@@ -6,24 +6,27 @@
 #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
diff --git a/fns.h b/fns.h
index c3c134b..4e3a1fa 100644 (file)
--- a/fns.h
+++ b/fns.h
@@ -22,6 +22,15 @@ const float deg2rad = (float)(3.14159/180);
 
 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*/
index eaa73da..87c83f7 100644 (file)
@@ -100,6 +100,7 @@ cd ../../</Command>
     <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" />
@@ -115,6 +116,7 @@ cd ../../</Command>
     <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" />