source.x = bgSamplePos.X;
source.y = bgSamplePos.Y;
- MaxTexWidth = 512;
- MaxTexHeight = 512;
-
load(filename);
- // Number of sections each dimension, rounded up
- _sections.width = (_imageSize.width - 1) / MaxTexWidth + 1;
- _sections.height = (_imageSize.height - 1) / MaxTexHeight + 1;
cout<<"background \""<<_name<<"\" created"<<endl;
}
void Background::draw(){
- /** \todo make the drawing for everything be sectioned if that'd make it faster otherwise create tiling for backgrounds smaller than the screen size*/
/** \todo decide if wrapping should be a good default.*/
///destination rectangle only position is used and describes where the output is drawn.
SDL_Rect dest;///<the place where the sample will be drawn (can be reused)
- double x = mPos.X, y = mPos.Y;
- while (x > mScreenSize.width) // move it to the left til it's on the screen
- x -= mScreenSize.width;
- while (y > mScreenSize.height) // move it up til it's on the screen
- y -= mScreenSize.height;
+ double x = mPos.X;
+ double y = mPos.Y;
+ const double X = mScreenSize.width;
+ const double Y = mScreenSize.height;
+ const double W = _imageSize.width;
+ const double H = _imageSize.height;
+ /*
+ while (x > X) // move it to the left til it's on the screen
+ x -= X;
+ while (y > Y) // move it up til it's on the screen
+ y -= Y;
while (x < 0) // move it to the right until it's in the drawable area
- x += mScreenSize.width;
+ x += X;
while (y < 0) // move it down til it's on the screen
- y += mScreenSize.height;
-
- if (wrapping) {
+ y += Y;
+ //*/
+ /*if (wrapping) {
SDL_Rect source;
+ if (x>=0 && y>=0){
+ // Top-left portion of image
+ dest.x = x;
+ dest.y = y;
+ source.x = 0;
+ source.y = 0;
+ source.w = X - x;
+ source.h = Y - y;
+ SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+
+ // Lower-left portion of image
+ dest.x = x;
+ dest.y = 0;
+ source.x = 0;
+ source.y = H - y;
+ source.w = X - x;
+ source.h = y;
+ SDL_BlitSurface(mBackground, &source, mScreen, &dest);
- // Top-left portion of image
- dest.x = x;
- dest.y = y;
- source.x = 0;
- source.y = 0;
- source.w = mScreenSize.width - x;
- source.h = mScreenSize.height - y;
- SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+ // Lower-right portion of image
+ dest.x = 0;
+ dest.y = 0;
+ source.x = W - x;///< \todo this needs to look right. it's grabbing the wrong part of the image but when it's getting the right part it freaks out and flashes when the bg moves off to the negative. need a check to make sure there is not more background to go before it just draws a section.
+ source.y = H - y;
+ source.w = x;
+ source.h = y;
+ SDL_BlitSurface(mBackground, &source, mScreen, &dest);
- // Top-right portion of image
- dest.x = 0;
- dest.y = y;
- source.x = mScreenSize.width - x;
- source.y = 0;
- source.w = x;
- source.h = mScreenSize.height - y;
- SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+ // Top-right portion of image
+ dest.x = 0;
+ dest.y = y;
+ source.x = W - x;
+ source.y = 0;
+ source.w = x;
+ source.h = Y - y;
+ SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+ }
+ if (x<0 || y<0){
+ x = -x;
+ y = -y;
+ // Top-left portion of image
+ dest.x = 0;
+ dest.y = 0;
+ source.x = x;
+ source.y = y;
+ source.w = X - x;
+ source.h = Y - y;
+ SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+
+ /// Lower-left portion of image \todo this be's broken
+ dest.x = X - x;
+ dest.y = 0;
+ source.x = 0;
+ source.y = H - y;
+ source.w = x;
+ source.h = Y - y;
+ SDL_BlitSurface(mBackground, &source, mScreen, &dest);
- // Lower-left portion of image
- dest.x = 0;
- dest.y = 0;
- source.x = mScreenSize.width - x;
- source.y = mScreenSize.height - y;
- source.w = x;
- source.h = y;
- SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+ // Lower-right portion of image
+ dest.x = X - x;
+ dest.y = Y - y;
+ source.x = 0;
+ source.y = 0;
+ source.w = x;
+ source.h = y;
+ SDL_BlitSurface(mBackground, &source, mScreen, &dest);
- // Lower-right portion of image
- dest.x = x;
- dest.y = 0;
- source.x = 0;
- source.y = mScreenSize.height - y;
- source.w = mScreenSize.width - x;
- source.h = y;
- SDL_BlitSurface(mBackground, &source, mScreen, &dest);
- } else {
+ // Top-right portion of image
+ dest.x = 0;
+ dest.y = Y - y;
+ source.x = x;
+ source.y = 0;
+ source.w = X - x;
+ source.h = y;
+ SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+ }
+ }
+ else */if(wrapping){
+ while (x < 0) // move it to the left til it's on the screen
+ x += W;
+ while (y < 0) // move it up til it's on the screen
+ y += H;
+ while(x > 0)//move back til we are at the spot to the left of the current background
+ x -= W;
+ int initx = x;
+ while(y > 0)
+ y -= H;
+ for(y; y < Y; y += H)
+ for(x = initx; x < X; x += W){
+ dest.x = x;
+ dest.y = y;
+ SDL_BlitSurface(mBackground, NULL, mScreen, &dest);
+ }
+ }
+ else {
dest.x = mPos.X;
dest.y = mPos.Y;
- // SDL_BlitSurface(mBackground, &source, mScreen, &dest);
+ SDL_BlitSurface(mBackground, &source, mScreen, &dest);
}
}
cout<<"Setting Size...";
_imageSize=SizeD(mBackground->w,mBackground->h);///<set the size of the background.
cout<<"done!"<<endl;
- /// \todo set values of size etc.
return mBackground;
}
-// for size of game window
+#ifndef BACKGROUND
+#define BACKGROUND
#include "fns.h"
#include <string>
#include <iostream>
private:
std::string _name;///<The background's name
SizeD _imageSize;///<The actual background image's full size.
- SizeD _sections;
- int MaxTexWidth;
- int MaxTexHeight;
SDL_Surface *mBackground;///<pointer to the actual screen
/** \todo _sections;//if needed */
public:
- Background(SDL_Surface *screen,std::string name="default", std::string filename="garden.jpg", bool wrap=true, Point2D ScreenPosition=Point2D(50,0), SizeD screenSize=SizeD(800,600),Point2D bgSamplePos=Point2D(0,0), SizeD bgSize=SizeD(800,600));
+ Background( SDL_Surface *screen,
+ std::string name="default",
+ std::string filename="bg.bmp",
+ bool wrap=true,
+ Point2D ScreenPosition=Point2D(0,0),
+ SizeD screenSize=SizeD(800,600),
+ Point2D bgSamplePos=Point2D(0,0),
+ SizeD bgSize=SizeD(800,600)
+ );
void draw();/**< Draw background. */
std::string getName(){ return _name; }/** Returns name. */
void setName(std::string name){ _name = name; }/** Set Name. */
void yset(int y) {mPos.Y = y;}/**< Sets the Background's Y coordinate. */
void setPosition(int x, int y) {mPos.X = x; mPos.Y = y;}/**< Sets the Background's X an Y coordinate. */
};
-
+#endif
find_package(SDL REQUIRED)
find_package(SDL_image REQUIRED)
-add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp)
+add_executable(game Animation.cpp fns.cpp Game.cpp Level.cpp main.cpp Sprite.cpp Text.cpp Actor.cpp Background.cpp LevelWorld.cpp)
target_link_libraries(game ${SDL_LIBRARY} ${SDLIMAGE_LIBRARY})
#include "fns.h"
#include <string>
#include <iostream>
+#include "LoadLevels.h"
const Uint32 Game::waitTime = 1000/60; /* ms */
SDL_WM_SetCaption("Ivan's Game", NULL);
SDL_ShowCursor(0);
- /** Create the first level */
- mLevels.push_back(new Level(mScreen));
+ /** Create the first level if there are none */
+ LoadLevels l(mScreen);
+ if(l.levels.size() == 0)
+ mLevels.push_back(new Level(mScreen));
+ else
+ mLevels = l.levels;
+
}
Game::~Game()
for (size_t i = 0; i < mLevels.size(); ++i) {
delete mLevels[i];
}
+ delete mScreen;
}
Level* Game::getCurrentLevel()
SDL_RemoveTimer(timer);
}
+void Game::loadLevel(std::string levelName){
+ for(int i=0; i< mLevels.size(); ++i)
+ if(mLevels[i]->getName() == levelName)
+ mCurrentLevel = i;
+ else
+ std::cout<<"Level "<<levelName<<" not found."<<std::endl;
+}
+
Uint32 Game::timerCallback(Uint32 interval, void* data)
{
SDL_Event event;
#define __GAME_H__
#include "Level.h"
-
#include <vector>
#include <string>
std::vector<Level*> mLevels;/**< Vector of pointers to all of the levels. \todo Make into map? */
SDL_Surface *mScreen;/**< The surface to draw to. */
- void addLevel(Level level);/**< Adds a level to the vector. \todo Implement! */
- void removeLevel(std::string levelName);/**< Removes the level with given name from the vector. \todo Implement! */
- void loadLevel(std::string levelName);/**< Loads a level by name. \todo Implement! */
+ void loadLevel(std::string levelName);/**< Loads a level by name. */
static Uint32 timerCallback(Uint32 interval, void* data);
};
#include "Level.h"
#include "fns.h"
-Level::Level(SDL_Surface* screen) :
- //mBackground(),
- mScreen(screen),
- mVikingAnimation("viking.anim"),
- mSunAnimation("sun.anim")
+Level::Level(SDL_Surface* screen) : mScreen(screen)
{
/** load background */
mBackground= new Background(screen);//mBackground = LoadImage("Backgrounds/bg.bmp");
-
- /** load sprites */
- Sprite* vikings1 = new Sprite(screen, "viking1", &mVikingAnimation);
- vikings1->setPosition(10,30);
- vikings1->setSpeed(1);
-
- Sprite* vikings2 = new Sprite(screen, "viking2", &mVikingAnimation);
- vikings2->setPosition(350,300);
- vikings2->setSpeed(1.5);
-
- Sprite* sun = new Sprite(screen, "sun", &mSunAnimation);
- sun->setPosition(480,50);
- sun->setSpeed(1);
-
- mSprites.push_back(vikings1);
- mSprites.push_back(vikings2);
- mSprites.push_back(sun);
}
+Level::Level(SDL_Surface* screen,string n) : mScreen(screen), mName(n)
+{
+ /** load background */
+ mBackground= new Background(screen, n);//mBackground = LoadImage("Backgrounds/bg.bmp");
+}
Level::~Level()
{
for (size_t i = 0; i < mSprites.size(); ++i) {
}
-void Level::DrawBG()
-{
- //DrawIMG(mBackground, 0, 0);
- mBackground->draw();
-}
-
-
void Level::drawScene()
{
Uint32 color;
SDL_FillRect(mScreen, NULL, color);
/** Draw BG */
- DrawBG();
+ mBackground->draw();
+ /** Draw Sprites*/
DrawSprites();
/** Flip the working image buffer with the mScreen buffer */
}
/** Will handle all movement and dynamic triggers. */
-void Level::update()
-{
- Uint8 *keys = SDL_GetKeyState(NULL);
- if ( keys[SDLK_LEFT] ) { mSprites[0]->xadd(-1); }
- if ( keys[SDLK_RIGHT] ) { mSprites[0]->xadd(1); }
- if ( keys[SDLK_UP] ) { mSprites[0]->yadd(-1); }
- if ( keys[SDLK_DOWN] ) { mSprites[0]->yadd(1); }
- if ( keys[SDLK_a] ) { mBackground->xadd(-1); }
- if ( keys[SDLK_d] ) { mBackground->xadd(1); }
- if ( keys[SDLK_w] ) { mBackground->yadd(-1); }
- if ( keys[SDLK_s] ) { mBackground->yadd(1); }
+void Level::update(){
+
+}
+
+
+void Level::addSprite(Sprite* sp){
+ mSprites.push_back(sp);
+}
+
+void Level::removeSprite(Sprite* sp){
+ for(int i=0; i<mSprites.size();++i){
+ if(mSprites[i] == sp)///< \todo test
+ return;///< \todo find the vector.remove
+ cout<<mSprites[i]<<endl;
+ }
+}
+
+void Level::addActor(string name, Actor actor){
+ mActors.insert(make_pair(name, actor));
+}
+void Level::removeActor(string name){
+ mActors.erase(name);
}
#include "Sprite.h"
#include "Background.h"
#include <vector>
+#include <map>
#include <string>
using std::vector;
{
public:
Level(SDL_Surface *screen);
+ Level(SDL_Surface *screen, string name);
~Level();
- void drawScene();/**< Draws everything that is set to draw on the screen. */
+ virtual void drawScene();/**< Draws everything that is set to draw on the screen. */
void LoadBG(string name);/**< Loads the Background. */
virtual void postEvent(SDL_Event event);/**< Passes along SDL events */
- void update();/**< Loop that runs every game frame that calculates movement, placement, etc. */
+ virtual void update();/**< Loop that runs every game frame that calculates movement, placement, etc. \todo make this be abstract so that levels are based off this class.*/
+ void addSprite(Sprite* sp);///< add a Sprite to the list of sprites
+ void removeSprite(Sprite* sp);///< remove the Sprite sp from the list of sprites
+ void addActor(string name, Actor actor);///< add a Actor to the list of sprites
+ void removeActor(string name);///< remove the Actor sp from the list of sprites
+ string getName(){ return mName; }///< returns the current level's name
-private:
- Background* mBackground;/**< Pointer to the current Background. \todo change this to a Background\current background also should be a vector or list of backgroundsfor the current level.*/
+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? */
- void DrawBG();/**< Draws the Background. \todo make this draw the current level*/
- void DrawIMG();/**< Draws an image to the screen. (Not implemented) */
+ 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.*/
+ 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. */
- Animation mVikingAnimation,/**< Viking animation. \todo make a storage location for Animations. */
- mSunAnimation;/**< Sun animation. */
+ map<string,Actor> mActors;/**< This level's actors. */
+ vector<Animation> mAnim;/**< This level's Animations */
};
#endif
--- /dev/null
+#include "LevelWorld.h"
+#include "fns.h"
+
+/// \todo set up a level to be poplulated from something.
+LevelWorld::LevelWorld(SDL_Surface* screen) :
+ Level(screen, "World")
+{
+ /** load animations */
+ mAnim.push_back(Animation("viking.anim"));
+ mAnim.push_back(Animation("sun.anim"));
+
+ /** load sprites */
+ Sprite* vikings1 = new Sprite(screen, "viking1", &mAnim[0]);
+ vikings1->setPosition(10,30);
+ vikings1->setSpeed(1);
+
+ Sprite* vikings2 = new Sprite(screen, "viking2", &mAnim[0]);
+ vikings2->setPosition(350,300);
+ vikings2->setSpeed(1.5);
+
+ Sprite* sun = new Sprite(screen, "sun", &mAnim[1]);
+ sun->setPosition(480,50);
+ sun->setSpeed(1);
+
+ mSprites.push_back(vikings1);
+ mSprites.push_back(vikings2);
+ mSprites.push_back(sun);
+}
+
+void LevelWorld::drawScene()
+{
+ Uint32 color;
+
+ /** Create a black mBackgroundground using the mScreen pixel format (32 bpp) */
+ color = SDL_MapRGB(mScreen->format, 0, 0, 0);
+ SDL_FillRect(mScreen, NULL, color);
+
+ /** Draw BG */
+ mBackground->draw();
+
+ /** Draw Sprites*/
+ DrawSprites();
+
+ /** Flip the working image buffer with the mScreen buffer */
+ SDL_Flip (mScreen);
+}
+
+/** Will handle all movement and dynamic triggers. */
+void LevelWorld::update()
+{
+ Uint8 *keys = SDL_GetKeyState(NULL);
+ if ( keys[SDLK_LEFT] ) { mSprites[0]->xadd(-1); }
+ if ( keys[SDLK_RIGHT] ) { mSprites[0]->xadd(1); }
+ if ( keys[SDLK_UP] ) { mSprites[0]->yadd(-1); }
+ if ( keys[SDLK_DOWN] ) { mSprites[0]->yadd(1); }
+ if ( keys[SDLK_a] ) { mBackground->xadd(-1); }
+ if ( keys[SDLK_d] ) { mBackground->xadd(1); }
+ if ( keys[SDLK_w] ) { mBackground->yadd(-1); }
+ if ( keys[SDLK_s] ) { mBackground->yadd(1); }
+
+}
+
--- /dev/null
+#ifndef LEVELWORLD
+#define LEVELWORLD
+#include "Level.h"
+
+/**
+ This is a stand alone Level that will have varying jobs in respect to the LevelWorld map
+
+ 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 for the Level
+ */
+
+class LevelWorld : public Level
+{
+public:
+ LevelWorld(SDL_Surface *screen);
+ void drawScene();/**< Draws everything that is set to draw on the screen. */
+ void update();/**< Loop that runs every game frame that calculates movement, placement, etc.*/
+
+};
+#endif
#include <iostream>
using std::cout;
using std::endl;
-Sprite::Sprite(SDL_Surface *screen, std::string name, Animation *anim) :
+Sprite::Sprite(SDL_Surface *screen, std::string name, Actor actor) :
mDrawn(0),
mVisible(true),
mLastUpdate(0),
- mActor(anim),
+ mActor(actor),
mScreen(screen)
/** \todo implement name and mName seee if I even need both of them. */
{
if(mVisible == true)
SDL_BlitSurface(mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame].image,NULL,mScreen,&dest);
}
-
#include "Actor.h"
/**
- Characters and other mobile objects displayed on screen.
+ Characters and other mobile objects displayed on screen with animations.
This class is tasked with the following:
- loading and controlling an Animation
- - movement of objects
- - check collision with other objects
*/
class Sprite
{
public:
- Sprite(SDL_Surface *screen, std::string name, Animation *anim);
- void draw();/**< draws Sprite. */
+ Sprite(SDL_Surface *screen, std::string name, Actor actor);
void setAnimation(int animation){ mActor.mCurrentAnimation = animation; }/**< changes to the specified animation beginning at 0. */
int getAnimation(){ return mActor.mCurrentAnimation; }/**< returns active animation. */
void setFrame(int frame) { mActor.mFrame = frame; }/**< cahnges to the specified frame of the animation beginning at 0. */
void startAnim() {mAnimating = 1;}/**< Causes the animation to play. */
void stopAnim() {mAnimating = 0;}/**< Causes the animation to stop. */
void rewind() {mActor.mFrame = 0;}/**< Resets the Sprite's animation to the first frame. */
+ std::string name;
+ void draw();/**< draws Sprite. */
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. */
void xset(int x) {mPos.X = x;}/**< Sets the Sprite's X Coordinate. */
void yset(int y) {mPos.Y = y;}/**< Sets the Sprite's Y coordinate. */
void setPosition(int x, int y) {mPos.X = x; mPos.Y = y;}/**< Sets the Sprite's X an Y coordinate. */
- std::string name;
+
private:
std::string mName;/**< Sprite's name */
- Point2D mPos;/**< Sprite's position */
bool mAnimating;/**< Tells whether to animate or not */
bool mDrawn;/**< Tells if the object has been drawn the first time */
- bool mVisible;/**< Determine if Sprite should be visible */
float mSpeed;/**< Movement speed of the Sprite. */
long mLastUpdate;/**< Number that indicates when the Sprite has last updated. Overflows in about 24 days so no worries. */
Actor mActor;/**< This Sprite's Actor. */
SDL_Surface *mScreen;/**< Screen to be drawn to. */
+ Point2D mPos;/**< Object's position */
+ bool mVisible;/**< Determine if Object should be visible */
int ZOrder;/**< Stacking order. Determines what draws on top. \todo implement. */
};
#endif
#include <SDL/SDL.h>
#include <string>
+/// \todo remove this this is just to make testing easy w/o gdb
+#include <iostream>
+using namespace std;
+
SDL_Surface* LoadImage( std::string filename );/**< Loads supported images. */
//class SizeD;
Game g;\r
\r
g.run();\r
-#if 0\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(screen, "viking1", &vikingAnimation);\r
- vikings1.setPosition(10,30);\r
- vikings1.setSpeed(1);\r
-\r
- Sprite vikings2(screen, "viking2", &vikingAnimation);\r
- vikings2.setPosition(350,300);\r
- vikings2.setSpeed(1.5);\r
-\r
- Sprite sun(screen, "sun", &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
-#endif\r
-\r
\r
return 0;\r
}\r