From: Ivan Hernanez Date: Thu, 31 May 2012 07:31:38 +0000 (-0500) Subject: updated some of sprite, level, and game classes to allow for Text class. X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=261c14a068ab52b16b836ce77db26c67d20bd38f;p=IvanGame.git updated some of sprite, level, and game classes to allow for Text class. --- diff --git a/Game.cpp b/Game.cpp index 01fb4be..995167e 100644 --- a/Game.cpp +++ b/Game.cpp @@ -41,7 +41,7 @@ Game* Game::game(){ return m_instance; } -Game::Game() : mCurrentLevel(0), mScreen(0), ShowCollisions(false), ShowFPS(false), startclock(0), deltaclock(0), currentFPS(0) +Game::Game() : mCurrentLevel(0), mScreen(0), ShowCollisions(false), ShowFPS(false), startclock(0), deltaclock(0), currentFPS(0), Paused(false) { int screenwidth = 640; int screenheight = 480; diff --git a/Game.h b/Game.h index 9c7c95c..78467dc 100644 --- a/Game.h +++ b/Game.h @@ -63,6 +63,12 @@ public: /** The current Sprite selected by the game */ static Sprite* CurrentSprite; + /** State of if the game should be paused or not. */ + bool Paused; + + /** The level that will be active next (for onloadFunctions)*/ + Level* LoadingLevel; + protected: Game(); ~Game(); diff --git a/Level.cpp b/Level.cpp index d16b65e..ffe14cf 100644 --- a/Level.cpp +++ b/Level.cpp @@ -2,7 +2,6 @@ #include "Game.h" #include "fns.h" #include -#include "SDL_ttf.h" Level::Level(SDL_Surface* screen) : mScreen(screen) { @@ -10,17 +9,19 @@ Level::Level(SDL_Surface* screen) : mScreen(screen) mUpdateBehavior = DoNothing; } -Level::Level(SDL_Surface* screen, string n, Behavior loadBehave, Behavior updateBehave) : mScreen(screen), mName(n), mBackground(NULL) +Level::Level(SDL_Surface* screen, string n, Condition winCond, Behavior loadBehave, Behavior updateBehave, Behavior endBehave) : mScreen(screen), mName(n), mBackground(NULL), Loaded(false), mTextures(map()), ToAdd(vector()), ToRemove(vector()) { /// Set behavior mLoadBehavior = loadBehave; mUpdateBehavior = updateBehave; + mEndBehavior = endBehave; + mWinCondition = winCond; } Level::~Level() { - for (size_t i = 0; i < mSprites.size(); ++i) { - delete mSprites[i]; + for (size_t i = 0; i < mWorldObjects.size(); ++i) { + delete mWorldObjects[i]; } } @@ -46,68 +47,7 @@ void Level::DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2 SDL_BlitSurface(img, &src, mScreen, &dest); } -void SDL_GL_RenderText(string s, TTF_Font *font, SDL_Color color, SDL_Rect *location, bool unicode=false) -{ - const char* text = s.c_str(); - SDL_Surface *initial; - SDL_Surface *intermediary; - SDL_Rect rect; - int w,h; - Texture texture; - - /* Use SDL_TTF to render our text */ - initial = TTF_RenderUTF8_Blended(font, s.c_str(), color); - - /* Convert the rendered text to a known format */ - w = initial->w; - h = initial->h; - - intermediary = SDL_CreateRGBSurface(0, w, h, 32, - 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); - - SDL_BlitSurface(initial, 0, intermediary, 0); - - /* Tell GL about our new texture */ - glGenTextures(1, &texture); - glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, intermediary->pixels ); - - /* GL_NEAREST looks horrible, if scaled... */ - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - - /* prepare to render our texture */ - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, texture); - glColor3f(1.0f, 1.0f, 1.0f); - - /* Draw a quad at location */ - glBegin(GL_QUADS); - /* Recall that the origin is in the lower-left corner - That is why the TexCoords specify different corners - than the Vertex coors seem to. */ - 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); - 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); - glDeleteTextures(1, &texture); -} void Level::drawScene() { @@ -117,45 +57,12 @@ void Level::drawScene() /** Create a black mBackgroundground using the mScreen pixel format (32 bpp) */ glClear(GL_COLOR_BUFFER_BIT); - // Load a font - TTF_Font *font; - font = TTF_OpenFont("FreeSans.ttf", 24); - - if(font){ - // Write text to surface - SDL_Surface *text; - SDL_Color text_color = {0xFF, 0xFF, 0xFF}; - string txt="A journey of a thousand miles begins with a single step."; - SDL_Rect rect = {0,0,100,100}; - SDL_GL_RenderText(txt, font, text_color, &rect); - } - else - cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl; - - TTF_CloseFont(font); - - // Load another font - font = TTF_OpenFont("Japanese.ttf", 24); - - if(font){ - // Write text to surface - SDL_Surface *text; - SDL_Color text_color = {0xFF, 0xFF, 0xFF}; - string txt="日本語"; - SDL_Rect rect = {0,100,100,100}; - SDL_GL_RenderText(txt, font, text_color, &rect, true); - } - else - cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl; - - TTF_CloseFont(font); - /** Draw BG */ if(mBackground!=NULL) mBackground->draw(); /** Draw Sprites*/ - DrawSprites(); + DrawObjects(); /** Draw Boundary Data */ DrawCollisions(); @@ -164,10 +71,10 @@ void Level::drawScene() SDL_GL_SwapBuffers(); } -void Level::DrawSprites() +void Level::DrawObjects() { - for (size_t i=0; idraw(); + for (size_t i=0; idraw(); } } @@ -175,8 +82,8 @@ void Level::DrawCollisions() { if(Game::game()->ShowCollisions){ mBackground->drawCollisions(); - for (size_t i=0; idrawCollisions(); + for (size_t i=0; idrawCollisions(); } } } diff --git a/Level.h b/Level.h index 9533397..984c94f 100644 --- a/Level.h +++ b/Level.h @@ -2,38 +2,37 @@ #define __LEVEL_H__ #include "Sprite.h" #include "Background.h" +#include "HUD.h" #include #include - - #include using std::vector; using std::string; /** - This is a stand alone Level that will have varying jobs +This is a stand alone Level that will have varying jobs - This class is tasked with the following: - - drawing the scence and all objects in order - - loading the background - - keeping track of animations - - moving objects - - handling events - */ +This class is tasked with the following: +- drawing the scence and all objects in order +- loading the background +- keeping track of animations +- moving objects +- handling events +*/ class Level { public: Level(SDL_Surface* screen); /** - Level constructor - \param screen the Screen to be drawn to - \param name of the level - \param loadBehavior the Loading Behavior for this level (Run once). - \param updateBehavior the Updating Behavior for this level (Run every loop). + Level constructor + \param screen the Screen to be drawn to + \param name of the level + \param loadBehavior the Loading Behavior for this level (Run once). + \param updateBehavior the Updating Behavior for this level (Run every loop). */ - Level(SDL_Surface* screen, string name, Behavior loadBehave = DoNothing, Behavior updateBehave = DoNothing); + Level(SDL_Surface* screen, string name, Condition winCond, Behavior loadBehave = DoNothing, Behavior updateBehave = DoNothing, Behavior endBehave = DoNothing); ~Level(); void drawScene();/**< Draws everything that is set to draw on the screen. */ void LoadBG(string name);/**< Loads the Background. \param name Name of the background to load */ @@ -52,22 +51,29 @@ public: void setBackground(Background* b){mBackground=b;}/**< sets the current level's background \param b Background */ Background* getBackground(){return mBackground;}/**< gets the current level's name \return Level's Background */ SDL_Surface* getScreen(){return mScreen;}/**< gets the current level's name \return Level's SDL_Surface */ - 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; + protected://allow inheritance string mName; Background* mBackground;/**< Pointer to the current Background. */ SDL_Surface *mScreen;/**< Pointer to the screen. */ - vector mSprites;/**< Vector of all sprites on the level. \todo Maybe make into map? This should be a list of World objects once implemented. \todo add accessor or move to public \todo make a map by name (for collisions) \todo make a list ordered by Zorder (for drawing*/ + vector mWorldObjects;/**< Vector of all objects on the level. \todo make a list ordered by Zorder (for drawing)*/ + vector mSprites;/**< Vector of all sprites on the level */ map mActors;/**< This level's actors. */ vector mAnims;/**< This level's Animations */ + map mTextures;/**< Textures that have been loaded by the level. */ + vector ToAdd;/**< List of sprites that should be added this game loop */ + vector ToRemove;/**< List of sprites that should die after this game loop */ + 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) */ void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);/**< Draws an image at the specified location (x,y) that is blitted with width w and height h from the point (x2,y2) of the given image */ - void DrawSprites();/**< Draws all Sprites. */ + void DrawObjects();/**< Draws all Sprites. */ void DrawCollisions();/**< Draws all Collision data */ Behavior mLoadBehavior;/**< will be used to define the Load action of the level */ Behavior mUpdateBehavior;/**< will be used to define the update action of the level */ + Behavior mEndBehavior;/**< will be used to define the end action of the level as it closes*/ + Condition mWinCondition;/**< Conditions for the level to wait upon before exiting.*/ }; #endif diff --git a/LoadResources.cpp b/LoadResources.cpp index 64d02d7..5fcd495 100644 --- a/LoadResources.cpp +++ b/LoadResources.cpp @@ -5,6 +5,6 @@ ///\file this file should contain implementations for loading resources void Game::LoadResources(){ - mLevels.push_back(new Level(mScreen, "World", LevelWorldLoad, LevelWorldUpdate)); + mLevels.push_back(new Level(mScreen, "World", NeverEnd, LevelWorldLoad, LevelWorldUpdate)); } diff --git a/Text.cpp b/Text.cpp index 63ba570..0238f09 100644 --- a/Text.cpp +++ b/Text.cpp @@ -1,153 +1,89 @@ #include "Text.h" -#include -#include -#include #include -#include -#include +#include +#include "Game.h" #include "fns.h" +#include using namespace std; -void Text::draw(SDL_Surface *screen, SDL_Surface *img, int x, int y, int w, int h, int x2, int y2) +void Text::draw() { - SDL_Rect dest; - dest.x = x; - dest.y = y; - SDL_Rect src; - src.x = x2; - src.y = y2; - src.w = w; - src.h = h; - SDL_BlitSurface(img, &src, screen, &dest); -} + const char* txt = text.c_str(); + SDL_Surface *initial; + SDL_Surface *intermediary; + SDL_Rect rect; + SDL_Rect location = {mPos.x, mPos.y, width, height}; + int w,h; + Texture texture; -Text* Text::init(std::string iniFile, float r, float g, float b, float a) -{ - Text *tempFont; - int width; - string buffer, var, fontFile, datFile; - unsigned char tmp; - SDL_Surface *tempSurface; - iniFile="ArtAssets/Fonts/"+iniFile; - ifstream fin(iniFile.c_str()); - - if(!fin) - { - printf("Error opening %s\n\n",iniFile.c_str()); - } + /* Use SDL_TTF to render our txt */ + initial = TTF_RenderUTF8_Blended(font, text.c_str(), color); - while(getline(fin,buffer)) - { - if(buffer[0] != '#' && buffer[0] != '\r' && buffer[0] != '\0' && buffer[0] != '\n' && buffer.length() != 0) - { - stringstream value(buffer); - value>>width; - value>>fontFile; - value>>datFile; - } - } - fin.close(); - tempFont = new Text; - tempFont->width = width; - tempFont->data = new unsigned char[width*width*4]; - tempFont->charWidth = width/16; - FILE *input = fopen(fontFile.c_str(),"r"); - if(input) - { - for(int i=0;idata[i*4] = (unsigned char)255*(unsigned char)r; - tempFont->data[i*4+1] = (unsigned char)255*(unsigned char)g; - tempFont->data[i*4+2] = (unsigned char)255*(unsigned char)b; - tempFont->data[i*4+3] = (unsigned char)(((float)tmp)*a); - } - } - else - { - cout<<"Error loading font: "+fontFile<font = SDL_CreateRGBSurfaceFrom(tempFont->data, width, width, 32, width*4, rmask, gmask, bmask, amask); - tempFont->font = SDL_DisplayFormatAlpha(tempSurface); - SDL_FreeSurface(tempSurface); - - //hold widths of the font - tempFont->widths = new int[256]; - - //read info about width of each char - input = fopen(datFile.c_str(),"r"); - if(fin) - { - for(int i=0; i<256;++i) - { - tempFont->widths[i]=getc(input); - } - } - fin.close(); - return tempFont; -} + /* Convert the rendered txt to a known format */ + w = nextPow2(initial->w); + h = nextPow2(initial->h); -void Text::drawString(SDL_Surface *screen, Text *font, int x, int y, std::string str, ...) -{ - char string[1024]; - va_list ap; // Pointer To List Of Arguments - va_start(ap, str.c_str()); // Parses The String For Variables - vsprintf(string, str.c_str(), ap); // Converts Symbols To Actual Numbers - va_end(ap); // Results Are Stored In Text - int len = strlen(string); - int xPos=0; - for(int i=0;ifont , xPos+x, y, font->widths[string[i]]+2, font->charWidth, (string[i]%16*font->charWidth)+((font->charWidth/2)-(font->widths[string[i]])/2), (((int)string[i]/16)*font->charWidth)); - xPos+=font->widths[string[i]]; - } -} + intermediary = SDL_CreateRGBSurface(0, w, h, 32, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000); -int Text::stringWidth(Text *font,std::string str,...) -{ - char string[1024]; // Temporary string - - va_list ap; // Pointer To List Of Arguments - va_start(ap, str.c_str()); // Parses The String For Variables - vsprintf(string, str.c_str(), ap); // Converts Symbols To Actual Numbers - va_end(ap); // Results Are Stored In Text - int xPos=0; - int len=strlen(string); - for(int i=0;iwidths[string[i]]; - } - return xPos; -} + SDL_BlitSurface(initial, 0, intermediary, 0); -void Text::DeleteFont(Text *font) -{ - delete [] font->widths; - delete [] font->data; - SDL_FreeSurface(font->font); - delete font; + /* Tell GL about our new texture */ + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_BGRA, GL_UNSIGNED_BYTE, intermediary->pixels ); + + /* GL_NEAREST looks horrible, if scaled... */ + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + + /* prepare to render our texture */ + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, texture); + glColor3f(1.0f, 1.0f, 1.0f); + + /* Draw a quad at location */ + glBegin(GL_QUADS); + /* Recall that the origin is in the lower-left corner + That is why the TexCoords specify different corners + than the Vertex coors seem to. */ + 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); + 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); + glDeleteTextures(1, &texture); } -void Text::DeleteFont() -{ - delete [] this->widths; - delete [] this->data; - SDL_FreeSurface(this->font); - delete this; +Text::Text(string t, SDL_Color color){ + // Load a font + font = TTF_OpenFont("FreeSans.ttf", 24); + + if(font){ + // Write text to surface + SDL_Surface *text; + //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); } + diff --git a/Text.h b/Text.h index 0bba3ba..9ae9993 100644 --- a/Text.h +++ b/Text.h @@ -4,7 +4,9 @@ #include #include #include - +#include "SDL_ttf.h" +#include "WorldObject.h" +using namespace std; /** On-screen text @@ -12,21 +14,15 @@ - displaying text on screen. */ -class Text +class Text : public WorldObject { public: - SDL_Surface *font; + TTF_Font* font; + string text; + SDL_Color color; + void draw(); int width; - int charWidth; - int* widths; - unsigned char* data; - void draw(SDL_Surface *screen, SDL_Surface *img, int x, int y, int w, int h, int x2, int y2); - Text* init(std::string imageMap, float r, float g, float b, float a); - inline Text* initFont(std::string imageMap, float r, float g, float b){ return init(imageMap, r,g,b,1); } - inline Text* initFont(std::string imageMap){ return init(imageMap, 1,1,1,1); } - void drawString(SDL_Surface *screen, Text *font, int x, int y, std::string str, ...); - int stringWidth(Text *font,std::string str,...); - void DeleteFont(); - void DeleteFont(Text *font); + int height; + Text(string t, SDL_Color color); }; #endif diff --git a/WorldObject.cpp b/WorldObject.cpp index 3ed213f..f2b9428 100644 --- a/WorldObject.cpp +++ b/WorldObject.cpp @@ -2,6 +2,8 @@ #include "fns.h" +void WorldObject::drawCollisions(){} + void WorldObject::drawCollisions(vector &vec, const Point2D& pos){ for(unsigned int i=0; i < vec.size(); i++){ vec[i]->draw(pos); diff --git a/WorldObject.h b/WorldObject.h index b827348..27f0df0 100644 --- a/WorldObject.h +++ b/WorldObject.h @@ -14,6 +14,7 @@ public: mScale(1) {} virtual void draw() = 0;/**< Draws the Object. */ + virtual void drawCollisions();/**< Draws Collision data for the Object */ void drawCollisions(vector& vec, const Point2D& pos);/**< Draws Collision data for the Object */ void xadd(int num) {mPos.x += num;}/**< Increase x coordiante by a given amount. */ void yadd(int num) {mPos.y += num;}/**< Increase y coordinate by a given amount. */ diff --git a/fns.cpp b/fns.cpp index 2a7719a..ec94784 100644 --- a/fns.cpp +++ b/fns.cpp @@ -76,6 +76,16 @@ Point2D Point2D::div(double d){ return *this; } +unsigned int nextPow2(unsigned int i){ + i--; + i |= i>>1; //handle 2 bit numbers + i |= i>>2; //handle 4 bit numbers + i |= i>>4; //handle 8 bit numbers + i |= i>>8; //handle 16 bit numbers + i |= i>>16; //handle 32 bit numbers + i++; + return i; +} /* template string to_string(const T& t) diff --git a/fns.h b/fns.h index 2c2e65c..06a7794 100644 --- a/fns.h +++ b/fns.h @@ -10,6 +10,10 @@ using namespace std; typedef void (*Behavior) (); ///As the name denotes, this just simply does nothing static void DoNothing(){} +///A function that determines state of level completion +typedef bool (*Condition) (); +///Condition that is always false +static bool NeverEnd(){return false;} ///Texture name typedef unsigned int Texture; ///deg/rad @@ -73,4 +77,7 @@ class SizeD double h;/**< y Position */ }; //template string to_string(const T& t); + +///Rounds a number to the next power of two +unsigned int nextPow2(unsigned int i); #endif diff --git a/game.vcxproj b/game.vcxproj index c3c288b..eaa73da 100644 --- a/game.vcxproj +++ b/game.vcxproj @@ -102,12 +102,11 @@ cd ../../ + - - true - + @@ -117,13 +116,12 @@ cd ../../ + - - true - +