From: Ivan Hernandez Date: Sat, 11 Jul 2009 20:08:39 +0000 (-0500) Subject: Initial commit X-Git-Url: http://git.mmlx.us/?a=commitdiff_plain;h=a509c19c3a2c1623e16e4b5da5e1ee945f57ac3c;p=IvanGame.git Initial commit --- a509c19c3a2c1623e16e4b5da5e1ee945f57ac3c diff --git a/Animation.cpp b/Animation.cpp new file mode 100644 index 0000000..d85c159 --- /dev/null +++ b/Animation.cpp @@ -0,0 +1,73 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "Animation.h" +#include "fns.h" +using namespace std; + +int Animation::loadAnimation(std::string animFile) +{ + //variables + string buffer,file; + vector 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 index 0000000..666a2cf --- /dev/null +++ b/Animation.h @@ -0,0 +1,28 @@ +#ifndef SPRITEBASE +#define SPRITEBASE +#include +#include +#include + +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 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 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 index 0000000..81cfb53 --- /dev/null +++ b/ArtAssets/Sprites/sun.anim @@ -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 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 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 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 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 index 0000000..f77cabf --- /dev/null +++ b/ArtAssets/Sprites/viking.anim @@ -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 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 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 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 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 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 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 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 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 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 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 index 0000000..2221c33 --- /dev/null +++ b/Game.cpp @@ -0,0 +1,5 @@ +#include "Game.h" +#include "fns.h" +#include + + diff --git a/Game.h b/Game.h new file mode 100644 index 0000000..48f6a66 --- /dev/null +++ b/Game.h @@ -0,0 +1,24 @@ +#ifndef GAME +#define GAME +#include "Level.h" +#include +#include +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 mLevels; + SDL_Surface *screen; +}; +#endif diff --git a/Level.cpp b/Level.cpp new file mode 100644 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 +#include +using std::vector; +using std::string; +class Level +{ + public: + Level() {} + Level(SDL_Surface *screen):screen(screen) {} + ~Level(); + vector 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 index 0000000..3ce97a5 Binary files /dev/null and b/SDL.dll differ diff --git a/Sprite.cpp b/Sprite.cpp new file mode 100644 index 0000000..226757f --- /dev/null +++ b/Sprite.cpp @@ -0,0 +1,66 @@ +#include "Sprite.h" +#include +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 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 index 0000000..9c0be7c --- /dev/null +++ b/Sprite.h @@ -0,0 +1,54 @@ +#ifndef SPRITE +#define SPRITE +#include "Animation.h" +#include + +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 index 0000000..63ba570 --- /dev/null +++ b/Text.cpp @@ -0,0 +1,153 @@ +#include "Text.h" +#include +#include +#include +#include +#include +#include +#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;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; +} + +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]]; + } +} + +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; +} + +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 index 0000000..be47bde --- /dev/null +++ b/Text.h @@ -0,0 +1,25 @@ +#ifndef TEXT +#define TEXT +#include +#include +#include +#include + +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 index 0000000..b90fd11 --- /dev/null +++ b/fns.cpp @@ -0,0 +1,49 @@ +#include "fns.h" +#include + +#ifndef RELEASE +#include +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 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 index 0000000..dc9759c --- /dev/null +++ b/fns.h @@ -0,0 +1,10 @@ +#ifndef FNS +#define FNS +#include +#include + +SDL_Surface* LoadImage( std::string filename ); + + +//template string to_string(const T& t); +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b0d23f7 --- /dev/null +++ b/main.cpp @@ -0,0 +1,352 @@ +#include +#include +#include +#include +#include "Sprite.h" +#include +#include +#include +#include "fns.h" + +using namespace std; + +template string to_string(const T& t) +{ + ostringstream os; + os << t; + return os.str(); +} + +// Allocate a pointer to an SDL_Surface for the screen buffer +// and for the image we want to load and display on screen. +SDL_Surface *back = NULL; +SDL_Surface *screen = NULL; +SDL_Surface *image = NULL; + +Animation vikingAnimation; +Animation sunAnimation; +//Sprite vikings1; +//Sprite vikings2; +//Sprite sun; + +vector SpriteList;//see about differnt containter HERE + +void DrawSprites() +{ + for(int i=0; iformat, R, G, B); + switch (screen->format->BytesPerPixel) + { + case 1: // Assuming 8-bpp + { + Uint8 *bufp; + bufp = (Uint8 *)screen->pixels + y*screen->pitch + x; + *bufp = color; + } + break; + case 2: // Probably 15-bpp or 16-bpp + { + Uint16 *bufp; + bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x; + *bufp = color; + } + break; + case 3: // Slow 24-bpp mode, usually not used + { + Uint8 *bufp; + bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3; + if(SDL_BYTEORDER == SDL_LIL_ENDIAN) + { +bufp[0] = color; + bufp[1] = color >> 8; + bufp[2] = color >> 16; + } else { + bufp[2] = color; + bufp[1] = color >> 8; + bufp[0] = color >> 16; + } + } + break; + case 4: // Probably 32-bpp + { + Uint32 *bufp; + bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x; + *bufp = color; + } + break; + } +} + +/*void draw () +{ + + 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); + + // Set the image offset + SDL_BlitSurface(image, NULL, screen, &rect); + + // Flip the working image buffer with the screen buffer + SDL_Flip (screen); + + // Add a little pause... + SDL_Delay (1); +}*/ + +//blits whole image to specified location +void DrawIMG(SDL_Surface *img, int x, int y) +{ + SDL_Rect dest; + dest.x = x; + dest.y = y; + SDL_BlitSurface(img, NULL, screen, &dest); +} + +//blits the part of the image defined by x y w and h to the psecified position +void 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 DrawBG() +{ + DrawIMG(back, 0, 0); +} + +//-------------------------------------------------------------------// +// Function : init() - Params : none +// SDL Initialisation function +// Instead of having a heavy and loaded main function, all the +// setting up of our application is handled here + +bool init() +{ + // Initialize SDL + if (SDL_Init (SDL_INIT_VIDEO) < 0) + { + printf ("Couldn't initialize SDL: %s\n", SDL_GetError ()); + exit (1); + } + + atexit (SDL_Quit); + + // Set 800x600 32-bits video mode + screen = SDL_SetVideoMode (800, 600, 32, SDL_SWSURFACE | SDL_DOUBLEBUF); + if (screen == NULL) + { + printf ("Couldn't set 800x600 32b video mode: %s\n", SDL_GetError ()); + exit (2); + } + + // Set the title of our application window handler + SDL_WM_SetCaption ("SDL MultiMedia Application", NULL); + + // We now load our first bitmap image. + back = LoadImage("Backgrounds/bg.bmp"); + image = LoadImage("image.bmp"); + + // This lets us set the original coordinates of our image on the screen. + // Don't forget that top left corner is the origin (0,0) + //rect.x = 200; + //rect.y = 200; + + // We activate keyboard repetition. Therefore, when a key stays pressed down + // it will keep moving the image around the screen + // To see prcisely what this toggle does, just comment the line and recompile + // the code... + SDL_EnableKeyRepeat(25, 20); + + return true; +} + + + +//-------------------------------------------------------------------// +// Function : draw() - Params : none +// Frame rendering function +// This function is called on every loop, to draw the scene +// It draws then swaps the work and screen buffer to display the scene +void DrawScene(SDL_Surface *screen) +{ + /*Tutorial 1 + Slock(screen); + for(int x=0;x<640;x++) + { + for(int y=0;y<480;y++) + { + DrawPixel(screen, x,y,y/2,y/2,x/3); + } + } + Sulock(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(); + + /* + vikings1.clearBG(); + vikings2.clearBG(); + sun.clearBG(); + + vikings1.updateBG(); + vikings2.updateBG(); + sun.updateBG(); + + vikings1.draw(); + vikings2.draw(); + sun.draw(); + + //*/ + DrawSprites(); + + // Set the image offset (change to draw a set of passed images) + //DrawIMG(image,x,y); + + // Flip the working image buffer with the screen buffer + SDL_Flip (screen); + + // Add a little pause... + SDL_Delay (1); +} + + + + +//-------------------------------------------------------------------// +// Function : main() - Params : argc, argv +// Main program function +// This function calls the init() function then loops on draw function +// until an escape condition is reached +int main (int argc, char *argv[]) +{ + // Used to loop + int done = 0; + // The keys variable is used to store the state of the keyboard at + // each frame and see if which keys are pressed down + Uint8 *keys; + + // Initialise SDL and all the rest... + init(); + + //load sprites + //Animation vikingAnimation = new Animation("viking.anim"); + vikingAnimation.loadAnimation("viking.anim"); + //Animation sunAnimation = new Animation(); + sunAnimation.loadAnimation("sun.anim"); + + Sprite vikings1; + vikings1.name="viking1"; + vikings1.init(screen, &vikingAnimation); + vikings1.setPosition(10,30); + vikings1.setSpeed(1); + + Sprite vikings2; + vikings2.name="viking2"; + vikings2.init(screen, &vikingAnimation); + vikings2.setPosition(350,300); + vikings2.setSpeed(1.5); + + Sprite sun; + sun.name="sun"; + sun.init(screen, &sunAnimation); + sun.setPosition(480,50); + sun.setSpeed(1); + + SpriteList.push_back(vikings1); + SpriteList.push_back(vikings2); + SpriteList.push_back(sun); + + //Hide Cursor + SDL_ShowCursor(0); + + // Game Loop + while (!done) + { + // This will let us track events + SDL_Event event; + + // We fill 'event' with the first event in the event queue + while (SDL_PollEvent (&event)) + { + switch (event.type) + { + // If our event reports a key being pressed down + // we process it + case SDL_KEYDOWN: + keys = SDL_GetKeyState(NULL); + if ( keys[SDLK_LEFT] ) { SpriteList[0].xadd(-1);} + if ( keys[SDLK_RIGHT] ) { SpriteList[0].xadd(1);} + if ( keys[SDLK_UP] ) { SpriteList[0].yadd(-1);} + if ( keys[SDLK_DOWN] ) { SpriteList[0].yadd(1);} + if ( keys[SDLK_ESCAPE] ) { done = 1;} + break; + // If the event is a click on the close button in the top + // right corner of the window, we kill the application + case SDL_QUIT: + done = 1; + break; + default: + break; + } + } + // Render the scene + DrawScene (screen); + } + + return 0; +}