Initial commit
authorIvan Hernandez <iturtleman128@gmail.com>
Sat, 11 Jul 2009 20:08:39 +0000 (15:08 -0500)
committerIvan Hernandez <iturtleman128@gmail.com>
Sat, 11 Jul 2009 20:08:39 +0000 (15:08 -0500)
32 files changed:
Animation.cpp [new file with mode: 0644]
Animation.h [new file with mode: 0644]
ArtAssets/Backgrounds/bg.bmp [new file with mode: 0644]
ArtAssets/Fonts/text.bmp [new file with mode: 0644]
ArtAssets/Sprites/sun.anim [new file with mode: 0644]
ArtAssets/Sprites/sun1.bmp [new file with mode: 0644]
ArtAssets/Sprites/sun2.bmp [new file with mode: 0644]
ArtAssets/Sprites/sun3.bmp [new file with mode: 0644]
ArtAssets/Sprites/sun4.bmp [new file with mode: 0644]
ArtAssets/Sprites/viking.anim [new file with mode: 0644]
ArtAssets/Sprites/viking1.bmp [new file with mode: 0644]
ArtAssets/Sprites/viking2.bmp [new file with mode: 0644]
ArtAssets/Sprites/viking3.bmp [new file with mode: 0644]
ArtAssets/Sprites/viking4.bmp [new file with mode: 0644]
ArtAssets/Sprites/viking5.bmp [new file with mode: 0644]
ArtAssets/Sprites/viking6.bmp [new file with mode: 0644]
ArtAssets/Sprites/viking7.bmp [new file with mode: 0644]
ArtAssets/Sprites/viking8.bmp [new file with mode: 0644]
ArtAssets/Sprites/viking9.bmp [new file with mode: 0644]
ArtAssets/image.bmp [new file with mode: 0644]
Game.cpp [new file with mode: 0644]
Game.h [new file with mode: 0644]
Level.cpp [new file with mode: 0644]
Level.h [new file with mode: 0644]
SDL.dll [new file with mode: 0644]
Sprite.cpp [new file with mode: 0644]
Sprite.h [new file with mode: 0644]
Text.cpp [new file with mode: 0644]
Text.h [new file with mode: 0644]
fns.cpp [new file with mode: 0644]
fns.h [new file with mode: 0644]
main.cpp [new file with mode: 0644]

diff --git a/Animation.cpp b/Animation.cpp
new file mode 100644 (file)
index 0000000..d85c159
--- /dev/null
@@ -0,0 +1,73 @@
+#include <string>
+#include <stdio.h>
+#include <stdlib.h>
+#include <SDL/SDL.h>
+#include <SDL/SDL_image.h>
+#include <vector>
+#include <fstream>
+#include <sstream>
+#include <iostream>
+#include "Animation.h"
+#include "fns.h"
+using namespace std;
+
+int Animation::loadAnimation(std::string animFile)
+{
+       //variables
+       string buffer,file;
+       vector<int> transparency;
+       int pause;
+       animFile="ArtAssets/Sprites/"+animFile;
+       //file
+       ifstream fin(animFile.c_str());
+
+       if(!fin)
+       {
+               printf("Error opening %s\n\n",animFile.c_str());
+               return -1;
+       }
+
+       int count=0;
+       while(getline(fin,buffer))
+       {
+               if(buffer[0] != '#' && buffer[0] != '\r' && buffer[0] != '\0' && buffer[0] != '\n' && buffer.length() != 0)
+       {
+                       stringstream value(buffer);
+                       string temp;
+                       value>>temp;
+                       if(temp=="NumFrames:")
+                       {
+                               value>>mNumFrames;
+                               mAnim = new SpriteFrame[mNumFrames];
+                               mBuilt = 1;
+                       }
+                       else
+                       {
+                               file=temp;
+                               value>>pause;
+                               for(int i=0; i<3;++i)
+                               {
+                                       int num;
+                                       value>>num;
+                                       if(num >= 0 && num<=255)
+                                               transparency.push_back(num);
+                               }
+                               SDL_Surface *temp=NULL;
+                               if((temp = LoadImage("Sprites/"+file)) == NULL) return -1;
+                       
+                               //set transparent color
+                               SDL_SetColorKey(temp, SDL_SRCCOLORKEY, SDL_MapRGB(temp->format, transparency[0], transparency[1], transparency[2]));
+                               //loads image into animation
+                               mAnim[count].image = SDL_DisplayFormat(temp);
+                           SDL_FreeSurface(temp);
+                               //sets frame delay and makes sure height and width are correct
+                               mAnim[count].pause = pause;
+                       if(!mW) mW = mAnim[count].image->w; 
+                       if(!mH) mH = mAnim[count].image->h;
+                               count++;
+                       }
+       }
+       }
+  fin.close();
+  return 0;
+}
diff --git a/Animation.h b/Animation.h
new file mode 100644 (file)
index 0000000..666a2cf
--- /dev/null
@@ -0,0 +1,28 @@
+#ifndef SPRITEBASE
+#define SPRITEBASE
+#include <string>
+#include <SDL/SDL.h>
+#include <SDL/SDL_image.h>
+
+struct SpriteFrame{
+       SDL_Surface *image;
+       int pause;
+};
+
+class Animation
+{
+       public:
+       Animation() {}
+       Animation(std::string animFile) 
+       {
+               loadAnimation(animFile);
+       }
+       int loadAnimation(std::string animFile);
+       SpriteFrame *mAnim;
+       int mBuilt, mNumFrames, mW, mH;
+
+       private:
+       std::string mname;//add name to anim file HERE
+};
+
+#endif
diff --git a/ArtAssets/Backgrounds/bg.bmp b/ArtAssets/Backgrounds/bg.bmp
new file mode 100644 (file)
index 0000000..be78c03
Binary files /dev/null and b/ArtAssets/Backgrounds/bg.bmp differ
diff --git a/ArtAssets/Fonts/text.bmp b/ArtAssets/Fonts/text.bmp
new file mode 100644 (file)
index 0000000..b6c1ec7
Binary files /dev/null and b/ArtAssets/Fonts/text.bmp differ
diff --git a/ArtAssets/Sprites/sun.anim b/ArtAssets/Sprites/sun.anim
new file mode 100644 (file)
index 0000000..81cfb53
--- /dev/null
@@ -0,0 +1,8 @@
+#Number of Frames 
+#ArtAssets/ milisec    Transparency
+#filename      pause   r       g       b 
+NumFrames: 4
+sun1.bmp 20 0 0 255
+sun2.bmp 20 0 0 255
+sun3.bmp 20 0 0 255
+sun4.bmp 20 0 0 255
diff --git a/ArtAssets/Sprites/sun1.bmp b/ArtAssets/Sprites/sun1.bmp
new file mode 100644 (file)
index 0000000..3fa16ba
Binary files /dev/null and b/ArtAssets/Sprites/sun1.bmp differ
diff --git a/ArtAssets/Sprites/sun2.bmp b/ArtAssets/Sprites/sun2.bmp
new file mode 100644 (file)
index 0000000..689b54d
Binary files /dev/null and b/ArtAssets/Sprites/sun2.bmp differ
diff --git a/ArtAssets/Sprites/sun3.bmp b/ArtAssets/Sprites/sun3.bmp
new file mode 100644 (file)
index 0000000..796873d
Binary files /dev/null and b/ArtAssets/Sprites/sun3.bmp differ
diff --git a/ArtAssets/Sprites/sun4.bmp b/ArtAssets/Sprites/sun4.bmp
new file mode 100644 (file)
index 0000000..ca29b8f
Binary files /dev/null and b/ArtAssets/Sprites/sun4.bmp differ
diff --git a/ArtAssets/Sprites/viking.anim b/ArtAssets/Sprites/viking.anim
new file mode 100644 (file)
index 0000000..f77cabf
--- /dev/null
@@ -0,0 +1,13 @@
+#Number of Frames 
+#ArtAssets/ milisec    Transparency
+#filename      pause   r       g       b 
+NumFrames: 9
+viking1.bmp 50 0 255 0
+viking2.bmp 50 0 255 0 
+viking3.bmp 50 0 255 0
+viking4.bmp 50 0 255 0
+viking5.bmp 50 0 255 0
+viking6.bmp 50 0 255 0
+viking7.bmp 50 0 255 0
+viking8.bmp 50 0 255 0
+viking9.bmp 50 0 255 0
diff --git a/ArtAssets/Sprites/viking1.bmp b/ArtAssets/Sprites/viking1.bmp
new file mode 100644 (file)
index 0000000..8dd36a3
Binary files /dev/null and b/ArtAssets/Sprites/viking1.bmp differ
diff --git a/ArtAssets/Sprites/viking2.bmp b/ArtAssets/Sprites/viking2.bmp
new file mode 100644 (file)
index 0000000..676dd47
Binary files /dev/null and b/ArtAssets/Sprites/viking2.bmp differ
diff --git a/ArtAssets/Sprites/viking3.bmp b/ArtAssets/Sprites/viking3.bmp
new file mode 100644 (file)
index 0000000..0a2342a
Binary files /dev/null and b/ArtAssets/Sprites/viking3.bmp differ
diff --git a/ArtAssets/Sprites/viking4.bmp b/ArtAssets/Sprites/viking4.bmp
new file mode 100644 (file)
index 0000000..dc2cc22
Binary files /dev/null and b/ArtAssets/Sprites/viking4.bmp differ
diff --git a/ArtAssets/Sprites/viking5.bmp b/ArtAssets/Sprites/viking5.bmp
new file mode 100644 (file)
index 0000000..8d55d29
Binary files /dev/null and b/ArtAssets/Sprites/viking5.bmp differ
diff --git a/ArtAssets/Sprites/viking6.bmp b/ArtAssets/Sprites/viking6.bmp
new file mode 100644 (file)
index 0000000..68c7bba
Binary files /dev/null and b/ArtAssets/Sprites/viking6.bmp differ
diff --git a/ArtAssets/Sprites/viking7.bmp b/ArtAssets/Sprites/viking7.bmp
new file mode 100644 (file)
index 0000000..62e1bf1
Binary files /dev/null and b/ArtAssets/Sprites/viking7.bmp differ
diff --git a/ArtAssets/Sprites/viking8.bmp b/ArtAssets/Sprites/viking8.bmp
new file mode 100644 (file)
index 0000000..751f45f
Binary files /dev/null and b/ArtAssets/Sprites/viking8.bmp differ
diff --git a/ArtAssets/Sprites/viking9.bmp b/ArtAssets/Sprites/viking9.bmp
new file mode 100644 (file)
index 0000000..92c4c9f
Binary files /dev/null and b/ArtAssets/Sprites/viking9.bmp differ
diff --git a/ArtAssets/image.bmp b/ArtAssets/image.bmp
new file mode 100644 (file)
index 0000000..aaa70a3
Binary files /dev/null and b/ArtAssets/image.bmp differ
diff --git a/Game.cpp b/Game.cpp
new file mode 100644 (file)
index 0000000..2221c33
--- /dev/null
+++ b/Game.cpp
@@ -0,0 +1,5 @@
+#include "Game.h"
+#include "fns.h"
+#include <string>
+
+
diff --git a/Game.h b/Game.h
new file mode 100644 (file)
index 0000000..48f6a66
--- /dev/null
+++ b/Game.h
@@ -0,0 +1,24 @@
+#ifndef GAME
+#define GAME
+#include "Level.h"
+#include <vector>
+#include <string>
+using namespace std;
+
+class Game
+{
+       public:
+       Game():mCurrentLevel(0) {}
+       Game(SDL_Surface *screen):mCurrentLevel(0),screen(screen) {}
+       ~Game() {}
+       void addLevel(Level level);
+       void removeLevel(string levelName);
+       void loadLevel(string levelName);
+       int currentLevel(){ return mCurrentLevel; }     bool init();
+       
+       private:
+       int mCurrentLevel;
+       std::vector<Level> mLevels;
+       SDL_Surface *screen;
+};
+#endif
diff --git a/Level.cpp b/Level.cpp
new file mode 100644 (file)
index 0000000..4bbfe31
--- /dev/null
+++ b/Level.cpp
@@ -0,0 +1,73 @@
+#include "Level.h"
+#include "fns.h"
+
+void Level::DrawIMG(SDL_Surface *img, int x, int y)
+{
+    SDL_Rect dest;
+    dest.x = x;
+    dest.y = y;
+    SDL_BlitSurface(img, NULL, screen, &dest);
+}
+
+
+void Level::DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2)
+{
+    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);
+}
+
+
+void Level::DrawBG()
+{
+  DrawIMG(back, 0, 0);
+}
+
+
+void Level::DrawScene(SDL_Surface *screen)
+{
+       Uint32 color;
+
+    // Create a black background using the screen pixel format (32 bpp)
+    color = SDL_MapRGB (screen->format, 0, 0, 0);
+    SDL_FillRect (screen, NULL, color);
+       
+       //Draw BG
+       DrawBG();
+       
+       DrawSprites();
+    
+    // Flip the working image buffer with the screen buffer
+    SDL_Flip (screen);
+    
+    // Add a little pause...
+    SDL_Delay (1);
+}
+
+void Level::DrawSprites()
+{
+       for(int i=0; i<SpriteList.size(); ++i)
+       {
+               SpriteList[i].clearBG();
+       }
+       for(int i=0; i<SpriteList.size(); ++i)
+       {
+               SpriteList[i].updateBG();
+       }
+       for(int i=0; i<SpriteList.size(); ++i)
+       {
+               SpriteList[i].draw();
+       }
+}
+
+void Level::LoadBG(std::string name)
+{
+       back=LoadImage("Background"+name);
+}
+
diff --git a/Level.h b/Level.h
new file mode 100644 (file)
index 0000000..32c7ce7
--- /dev/null
+++ b/Level.h
@@ -0,0 +1,27 @@
+#ifndef LEVEL
+#define LEVEL
+#include "Sprite.h"
+#include <vector>
+#include <string>
+using std::vector;
+using std::string;
+class Level
+{
+       public:
+       Level() {}
+       Level(SDL_Surface *screen):screen(screen) {}
+       ~Level();
+       vector<Sprite> SpriteList;
+       void DrawScene(SDL_Surface *screen);
+       void DrawBG();
+       void DrawIMG();
+       void DrawIMG(SDL_Surface *img, int x, int y);
+       void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2);
+       void DrawSprites();
+       void LoadBG(string name);
+
+       private:
+       SDL_Surface *back;
+       SDL_Surface *screen;
+};
+#endif
diff --git a/SDL.dll b/SDL.dll
new file mode 100644 (file)
index 0000000..3ce97a5
Binary files /dev/null and b/SDL.dll differ
diff --git a/Sprite.cpp b/Sprite.cpp
new file mode 100644 (file)
index 0000000..226757f
--- /dev/null
@@ -0,0 +1,66 @@
+#include "Sprite.h"
+#include <iostream>
+using std::cout;
+using std::endl;
+
+int Sprite::init(SDL_Surface *screen, Animation *anim, ...)
+{
+       mSpriteAnimation = anim;
+       if(mSpriteAnimation -> mBuilt)
+       {
+               if(mSpriteAnimation->mNumFrames>1) mAnimating =1;
+               mBackReplacement = SDL_DisplayFormat(mSpriteAnimation->mAnim[0].image);
+       }
+       mVisible=true;
+       mAnimating=true;
+       mScreen = screen;
+}
+
+void Sprite::clearBG()
+{
+       if(mDrawn ==1)
+       {
+               SDL_Rect dest;
+               dest.x = mOldX;
+               dest.y = mOldY;
+               dest.w = mSpriteAnimation->mW;
+               dest.h = mSpriteAnimation->mH;
+               SDL_BlitSurface(mBackReplacement,NULL,mScreen,&dest);
+       }
+}
+
+void Sprite::updateBG()
+{
+       SDL_Rect srcrect;
+       srcrect.w = mSpriteAnimation->mW;
+       srcrect.h = mSpriteAnimation->mH;
+       srcrect.x = mX;
+       srcrect.y = mY;
+       mOldX = mX;
+       mOldY = mY;
+       SDL_BlitSurface(mScreen,&srcrect,mBackReplacement,NULL);
+}
+
+void Sprite::draw()
+{
+       if(mAnimating ==1)
+       {
+               if(mLastUpdate+mSpriteAnimation->mAnim[mFrame].pause*mSpeed<SDL_GetTicks())
+               {
+                       mFrame++;
+                       if(mFrame > mSpriteAnimation->mNumFrames-1) mFrame=0;
+                       mLastUpdate = SDL_GetTicks();
+               }
+       }
+       if(mDrawn == 0)
+       {
+               mDrawn=1;
+               mFrame=0;
+       }
+       SDL_Rect dest;
+       dest.x = mX;
+       dest.y = mY;
+       if(mVisible == true)
+               SDL_BlitSurface(mSpriteAnimation->mAnim[mFrame].image,NULL,mScreen,&dest);
+}
+
diff --git a/Sprite.h b/Sprite.h
new file mode 100644 (file)
index 0000000..9c0be7c
--- /dev/null
+++ b/Sprite.h
@@ -0,0 +1,54 @@
+#ifndef SPRITE
+#define SPRITE
+#include "Animation.h"
+#include <SDL/SDL.h>
+
+class Sprite
+{
+       public:
+       Sprite():mFrame(0),mLastUpdate(0),mDrawn(0) {}
+       Sprite(std::string name,Animation *anim, SDL_Surface *screen):mSpriteAnimation(anim),mScreen(screen),mFrame(0),mLastUpdate(0),mDrawn(0)
+       {
+               //later add a part that adds name to list of level sprites
+               //Game.Level.SpriteList.pushback(name);
+               if(mSpriteAnimation -> mBuilt)
+               {
+                       if(mSpriteAnimation->mNumFrames>1) mAnimating =1;
+                       mBackReplacement = SDL_DisplayFormat(mSpriteAnimation->mAnim[0].image);
+               }
+       }
+       int init(SDL_Surface *screen, Animation *anim, ...);
+       void draw();
+       void clearBG();
+       void updateBG();
+       void setFrame(int frame) { mFrame = frame; }
+       int getFrame() { return mFrame; }
+       void setSpeed(float speed) {mSpeed = speed;}
+       float getSpeed() {return mSpeed;}
+       void toggleAnim(){ mAnimating = !mAnimating;}
+       void startAnim() {mAnimating = 1;}
+       void stopAnim() {mAnimating = 0;}
+       void rewind() {mFrame = 0;}
+       void xadd(int num) {mX += num;}
+       void yadd(int num) {mY += num;}
+       void xset(int x) {mX = x;}
+       void yset(int y) {mY = y;}
+       void setPosition(int x, int y) {mX = x; mY = y;}
+       std::string name;
+
+       private:
+       std::string mName;
+       int mFrame;
+       int mX,mY,mOldX, mOldY;
+       bool mAnimating;
+       bool mDrawn;
+       bool mVisible;
+       int mAnimation;
+       float mSpeed;
+       long mLastUpdate;
+       Animation *mSpriteAnimation;
+       SDL_Surface *mBackReplacement;
+       SDL_Surface *mScreen;
+       int ZOrder;
+};
+#endif
diff --git a/Text.cpp b/Text.cpp
new file mode 100644 (file)
index 0000000..63ba570
--- /dev/null
+++ b/Text.cpp
@@ -0,0 +1,153 @@
+#include "Text.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+#include <iostream>
+#include <fstream>
+#include <sstream>
+#include "fns.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)
+{
+       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);
+}
+
+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());
+       }
+
+       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;
+}
+
+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]];
+       }
+}
+
+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;
+}
+
+void Text::DeleteFont(Text *font)
+{
+  delete [] font->widths;
+  delete [] font->data;
+  SDL_FreeSurface(font->font);
+  delete font;
+}
+
+void Text::DeleteFont()
+{
+  delete [] this->widths;
+  delete [] this->data;
+  SDL_FreeSurface(this->font);
+  delete this;
+}
diff --git a/Text.h b/Text.h
new file mode 100644 (file)
index 0000000..be47bde
--- /dev/null
+++ b/Text.h
@@ -0,0 +1,25 @@
+#ifndef TEXT
+#define TEXT
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+#include <SDL/SDL.h>
+
+class Text
+{
+       public:
+       SDL_Surface *font;
+       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);
+};
+#endif
diff --git a/fns.cpp b/fns.cpp
new file mode 100644 (file)
index 0000000..b90fd11
--- /dev/null
+++ b/fns.cpp
@@ -0,0 +1,49 @@
+#include "fns.h"
+#include <SDL/SDL_image.h>
+
+#ifndef RELEASE
+#include <iostream>
+using std::cout;
+using std::endl;
+#endif
+
+SDL_Surface* LoadImage( std::string filename )
+{
+       SDL_Surface* loaded_image = NULL;
+       SDL_Surface* compatible_image = NULL;
+
+       filename="ArtAssets/"+filename;
+       if(filename.c_str() == NULL) // check to see if a filename was provided
+       {
+               printf("No Filename %s\n\n",filename.c_str());
+               return NULL;// if not exit the function
+       }
+
+
+       // load the image using our new IMG_Load function from sdl-Image1.2
+       loaded_image = IMG_Load( filename.c_str() );
+       
+       if(!loaded_image) // check to see if it loaded properly
+       {
+               printf("Error opening %s\n\n",filename.c_str());
+               return NULL;
+       }
+
+       
+       // the image loaded fine so we can now convert it to the current display depth
+       compatible_image = SDL_DisplayFormat( loaded_image );
+
+       // Destroy the old copy
+       SDL_FreeSurface( loaded_image );
+
+       // return a pointer to the newly created display compatible image
+       return compatible_image;
+}
+
+/*
+template<class T> string to_string(const T& t)
+{
+    ostringstream os;
+    os << t;
+    return os.str();
+}//*/
diff --git a/fns.h b/fns.h
new file mode 100644 (file)
index 0000000..dc9759c
--- /dev/null
+++ b/fns.h
@@ -0,0 +1,10 @@
+#ifndef FNS
+#define FNS
+#include <SDL/SDL.h>
+#include <string>
+
+SDL_Surface* LoadImage( std::string filename );
+
+
+//template<class T> string to_string(const T& t);
+#endif
diff --git a/main.cpp b/main.cpp
new file mode 100644 (file)
index 0000000..b0d23f7
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,352 @@
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <string>\r
+#include <SDL/SDL.h>\r
+#include "Sprite.h"\r
+#include <iostream>\r
+#include <sstream>\r
+#include <vector>\r
+#include "fns.h"\r
+\r
+using namespace std;\r
+\r
+template<class T> string to_string(const T& t)\r
+{\r
+    ostringstream os;\r
+    os << t;\r
+    return os.str();\r
+}\r
+\r
+// Allocate a pointer to an SDL_Surface for the screen buffer \r
+// and for the image we want to load and display on screen.\r
+SDL_Surface *back = NULL;\r
+SDL_Surface *screen = NULL;\r
+SDL_Surface *image = NULL;\r
+\r
+Animation vikingAnimation;\r
+Animation sunAnimation;\r
+//Sprite vikings1;\r
+//Sprite vikings2;\r
+//Sprite sun;\r
+\r
+vector<Sprite> SpriteList;//see about differnt containter HERE\r
+\r
+void DrawSprites()\r
+{\r
+       for(int i=0; i<SpriteList.size(); ++i)\r
+       {\r
+               SpriteList[i].clearBG();\r
+       }\r
+       for(int i=0; i<SpriteList.size(); ++i)\r
+       {\r
+               SpriteList[i].updateBG();\r
+       }\r
+       for(int i=0; i<SpriteList.size(); ++i)\r
+       {\r
+               SpriteList[i].draw();\r
+       }\r
+}\r
+\r
+//functions from other main\r
+\r
+//locks the screen for drawing pixels to the screen\r
+void Slock(SDL_Surface *screen)\r
+{   \r
+    if ( SDL_MUSTLOCK(screen) )\r
+    {   \r
+        if ( SDL_LockSurface(screen) < 0 )\r
+        {\r
+            return;\r
+        }\r
+    }\r
+}\r
+\r
+void Sulock(SDL_Surface *screen)\r
+{   \r
+    if ( SDL_MUSTLOCK(screen) )\r
+    {\r
+        SDL_UnlockSurface(screen);\r
+    }\r
+}\r
+\r
+void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B)\r
+{\r
+Uint32 color = SDL_MapRGB(screen->format, R, G, B);\r
+    switch (screen->format->BytesPerPixel)\r
+    {\r
+    case 1: // Assuming 8-bpp\r
+        {\r
+            Uint8 *bufp;\r
+            bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;\r
+            *bufp = color;\r
+        }\r
+        break;\r
+    case 2: // Probably 15-bpp or 16-bpp\r
+        {\r
+            Uint16 *bufp;\r
+            bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;\r
+            *bufp = color;\r
+        }\r
+        break;\r
+    case 3: // Slow 24-bpp mode, usually not used\r
+        {\r
+            Uint8 *bufp;\r
+            bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;\r
+            if(SDL_BYTEORDER == SDL_LIL_ENDIAN)\r
+            {\r
+bufp[0] = color;\r
+                bufp[1] = color >> 8;\r
+                bufp[2] = color >> 16;\r
+            } else {\r
+                bufp[2] = color;\r
+                bufp[1] = color >> 8;\r
+                bufp[0] = color >> 16;\r
+            }\r
+        }\r
+        break;\r
+    case 4: // Probably 32-bpp\r
+        {\r
+            Uint32 *bufp;\r
+            bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;\r
+            *bufp = color;\r
+        }\r
+        break;\r
+    }\r
+}\r
+\r
+/*void draw ()\r
+{\r
+\r
+    Uint32 color;\r
+\r
+    // Create a black background using the screen pixel format (32 bpp)\r
+    color = SDL_MapRGB (screen->format, 0, 0, 0);\r
+    SDL_FillRect (screen, NULL, color);\r
+\r
+    // Set the image offset\r
+    SDL_BlitSurface(image, NULL, screen, &rect);\r
+\r
+    // Flip the working image buffer with the screen buffer\r
+    SDL_Flip (screen);\r
+\r
+    // Add a little pause...\r
+    SDL_Delay (1);\r
+}*/\r
+\r
+//blits whole image to specified location\r
+void DrawIMG(SDL_Surface *img, int x, int y)\r
+{\r
+    SDL_Rect dest;\r
+    dest.x = x;\r
+    dest.y = y;\r
+    SDL_BlitSurface(img, NULL, screen, &dest);\r
+}\r
+\r
+//blits the part of the image defined by x y w and h to the psecified position\r
+void DrawIMG(SDL_Surface *img, int x, int y,\r
+             int w, int h, int x2, int y2)\r
+{\r
+    SDL_Rect dest;\r
+    dest.x = x;\r
+    dest.y = y;\r
+    SDL_Rect src;\r
+    src.x = x2;\r
+    src.y = y2;\r
+    src.w = w;\r
+    src.h = h;\r
+    SDL_BlitSurface(img, &src, screen, &dest);\r
+}\r
+\r
+void DrawBG()\r
+{\r
+  DrawIMG(back, 0, 0);\r
+}\r
+\r
+//-------------------------------------------------------------------//\r
+// Function : init()    - Params : none\r
+// SDL Initialisation function\r
+// Instead of having a heavy and loaded main function, all the \r
+// setting up of our application is handled here\r
+\r
+bool init()\r
+{\r
+    // Initialize SDL \r
+    if (SDL_Init (SDL_INIT_VIDEO) < 0)\r
+    {\r
+        printf ("Couldn't initialize SDL: %s\n", SDL_GetError ());\r
+        exit (1);\r
+    }\r
+    \r
+    atexit (SDL_Quit);\r
+\r
+    // Set 800x600 32-bits video mode\r
+    screen = SDL_SetVideoMode (800, 600, 32, SDL_SWSURFACE | SDL_DOUBLEBUF);\r
+    if (screen == NULL)\r
+    {\r
+        printf ("Couldn't set 800x600 32b video mode: %s\n", SDL_GetError ());\r
+        exit (2);\r
+    }\r
+    \r
+    // Set the title of our application window handler\r
+    SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);\r
+    \r
+    // We now load our first bitmap image. \r
+    back = LoadImage("Backgrounds/bg.bmp");\r
+       image = LoadImage("image.bmp");\r
+    \r
+    // This lets us set the original coordinates of our image on the screen.\r
+    // Don't forget that top left corner is the origin (0,0)\r
+    //rect.x = 200;\r
+    //rect.y = 200;\r
+\r
+    // We activate keyboard repetition. Therefore, when a key stays pressed down\r
+    // it will keep moving the image around the screen\r
+    // To see prcisely what this toggle does, just comment the line and recompile\r
+    // the code...\r
+    SDL_EnableKeyRepeat(25, 20);\r
+    \r
+    return true;\r
+}\r
+\r
+\r
+\r
+//-------------------------------------------------------------------//\r
+// Function : draw()    - Params : none\r
+// Frame rendering function\r
+// This function is called on every loop, to draw the scene\r
+// It draws then swaps the work and screen buffer to display the scene\r
+void DrawScene(SDL_Surface *screen)\r
+{\r
+       /*Tutorial 1\r
+       Slock(screen);\r
+       for(int x=0;x<640;x++)\r
+       {  \r
+               for(int y=0;y<480;y++)\r
+           {\r
+                        DrawPixel(screen, x,y,y/2,y/2,x/3);\r
+           }\r
+       }\r
+       Sulock(screen);\r
+       */\r
+                                                                                                                                  \r
+    Uint32 color;\r
+\r
+    // Create a black background using the screen pixel format (32 bpp)\r
+    color = SDL_MapRGB (screen->format, 0, 0, 0);\r
+    SDL_FillRect (screen, NULL, color);\r
+       \r
+       //Draw BG\r
+       DrawBG();\r
+       \r
+       /*\r
+       vikings1.clearBG();\r
+       vikings2.clearBG();\r
+       sun.clearBG();\r
+\r
+       vikings1.updateBG();\r
+       vikings2.updateBG();\r
+       sun.updateBG();\r
+\r
+       vikings1.draw();\r
+       vikings2.draw();\r
+       sun.draw();\r
+       \r
+       //*/\r
+       DrawSprites();\r
+\r
+    // Set the image offset (change to draw a set of passed images)\r
+    //DrawIMG(image,x,y);\r
+    \r
+    // Flip the working image buffer with the screen buffer\r
+    SDL_Flip (screen);\r
+    \r
+    // Add a little pause...\r
+    SDL_Delay (1);\r
+}\r
+\r
+\r
+\r
+\r
+//-------------------------------------------------------------------//\r
+// Function : main()    - Params : argc, argv\r
+// Main program function\r
+// This function calls the init() function then loops on draw function \r
+// until an escape condition is reached\r
+int main (int argc, char *argv[])\r
+{\r
+    // Used to loop\r
+    int done = 0;\r
+    // The keys variable is used to store the state of the keyboard at \r
+    // each frame and see if which keys are pressed down\r
+    Uint8 *keys;\r
+    \r
+    // Initialise SDL and all the rest...\r
+    init();\r
+\r
+       //load sprites\r
+       //Animation vikingAnimation = new Animation("viking.anim");\r
+       vikingAnimation.loadAnimation("viking.anim");\r
+       //Animation sunAnimation = new Animation();\r
+       sunAnimation.loadAnimation("sun.anim");\r
+       \r
+       Sprite vikings1;\r
+       vikings1.name="viking1";\r
+       vikings1.init(screen, &vikingAnimation);\r
+       vikings1.setPosition(10,30);\r
+       vikings1.setSpeed(1);\r
+\r
+       Sprite vikings2;\r
+       vikings2.name="viking2";\r
+       vikings2.init(screen, &vikingAnimation);\r
+       vikings2.setPosition(350,300);\r
+       vikings2.setSpeed(1.5);\r
+\r
+       Sprite sun;\r
+       sun.name="sun";\r
+       sun.init(screen, &sunAnimation);\r
+       sun.setPosition(480,50);\r
+       sun.setSpeed(1);\r
+       \r
+       SpriteList.push_back(vikings1);\r
+       SpriteList.push_back(vikings2);\r
+       SpriteList.push_back(sun);\r
+\r
+       //Hide Cursor\r
+       SDL_ShowCursor(0);\r
+\r
+    // Game Loop\r
+    while (!done)\r
+    {\r
+        // This will let us track events\r
+        SDL_Event event;\r
+\r
+        // We fill 'event' with the first event in the event queue\r
+        while (SDL_PollEvent (&event))\r
+        {\r
+            switch (event.type)\r
+            {\r
+            // If our event reports a key being pressed down\r
+            // we process it\r
+            case SDL_KEYDOWN:\r
+                  keys = SDL_GetKeyState(NULL);\r
+                  if ( keys[SDLK_LEFT] ) {  SpriteList[0].xadd(-1);}\r
+                  if ( keys[SDLK_RIGHT] ) { SpriteList[0].xadd(1);}\r
+                  if ( keys[SDLK_UP] ) { SpriteList[0].yadd(-1);}\r
+                  if ( keys[SDLK_DOWN] ) { SpriteList[0].yadd(1);}\r
+                  if ( keys[SDLK_ESCAPE] ) { done = 1;}\r
+                  break;\r
+            // If the event is a click on the close button in the top\r
+            // right corner of the window, we kill the application\r
+            case SDL_QUIT:\r
+                done = 1;\r
+                break;\r
+            default:\r
+                break;\r
+            }\r
+        }\r
+               // Render the scene\r
+        DrawScene (screen);\r
+    }\r
+\r
+    return 0;\r
+}\r