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;
/** 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();
#include "Game.h"
#include "fns.h"
#include <GL/gl.h>
-#include "SDL_ttf.h"
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<string, Texture>()), ToAdd(vector<WorldObject*>()), ToRemove(vector<WorldObject*>())
{
/// 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];
}
}
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()
{
/** 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();
SDL_GL_SwapBuffers();
}
-void Level::DrawSprites()
+void Level::DrawObjects()
{
- for (size_t i=0; i<mSprites.size(); ++i) {
- mSprites[i]->draw();
+ for (size_t i=0; i<mWorldObjects.size(); ++i) {
+ mWorldObjects[i]->draw();
}
}
{
if(Game::game()->ShowCollisions){
mBackground->drawCollisions();
- for (size_t i=0; i<mSprites.size(); ++i) {
- mSprites[i]->drawCollisions();
+ for (size_t i=0; i<mWorldObjects.size(); ++i) {
+ mWorldObjects[i]->drawCollisions();
}
}
}
#define __LEVEL_H__
#include "Sprite.h"
#include "Background.h"
+#include "HUD.h"
#include <vector>
#include <map>
-
-
#include <string>
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 */
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<Sprite*> 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<WorldObject*> mWorldObjects;/**< Vector of all objects on the level. \todo make a list ordered by Zorder (for drawing)*/
+ vector<Sprite*> mSprites;/**< Vector of all sprites on the level */
map<string,Actor> mActors;/**< This level's actors. */
vector<Animation> mAnims;/**< This level's Animations */
+ 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 */
+ 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
///\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));
}
#include "Text.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string>
#include <iostream>
-#include <fstream>
-#include <sstream>
+#include <string>
+#include "Game.h"
#include "fns.h"
+#include <GL/gl.h>
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;i<width*width;++i)
- {
- tmp = getc(input);
- tempFont->data[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<<endl;
- return 0;
- }
- fclose(input);
- // now let's create a SDL surface for the font
- 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
- tempFont->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;i<len;i++)// Loop through all the chars in the string
- {
- draw(screen, font->font , 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;i<len;i++)
- {
- // Add their widths together
- xPos+=font->widths[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);
}
+
#include <stdlib.h>
#include <string>
#include <SDL/SDL.h>
-
+#include "SDL_ttf.h"
+#include "WorldObject.h"
+using namespace std;
/**
On-screen text
- 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
#include "fns.h"
+void WorldObject::drawCollisions(){}
+
void WorldObject::drawCollisions(vector<Collision*> &vec, const Point2D& pos){
for(unsigned int i=0; i < vec.size(); i++){
vec[i]->draw(pos);
mScale(1)
{}
virtual void draw() = 0;/**< Draws the Object. */
+ virtual void drawCollisions();/**< Draws Collision data for the Object */
void drawCollisions(vector<Collision*>& 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. */
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<class T> string to_string(const T& t)
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
double h;/**< y Position */
};
//template<class T> string to_string(const T& t);
+
+///Rounds a number to the next power of two
+unsigned int nextPow2(unsigned int i);
#endif
<ClInclude Include="fns.h" />
<ClInclude Include="Frame.h" />
<ClInclude Include="Game.h" />
+ <ClInclude Include="HUD.h" />
<ClInclude Include="Level.h" />
<ClInclude Include="LevelFns.h" />
<ClInclude Include="Sprite.h" />
- <ClInclude Include="Text.h">
- <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
- </ClInclude>
+ <ClInclude Include="Text.h" />
<ClInclude Include="WorldObject.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Collision.cpp" />
<ClCompile Include="fns.cpp" />
<ClCompile Include="Game.cpp" />
+ <ClCompile Include="HUD.cpp" />
<ClCompile Include="Level.cpp" />
<ClCompile Include="LoadResources.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="Sprite.cpp" />
- <ClCompile Include="Text.cpp">
- <ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</ExcludedFromBuild>
- </ClCompile>
+ <ClCompile Include="Text.cpp" />
<ClCompile Include="WorldObject.cpp" />
</ItemGroup>
<ItemGroup>