#include <iostream>
#include "Animation.h"
#include "fns.h"
+#include <GL/gl.h>
using namespace std;
Animation::Animation(std::string animFile):mName(animFile)
if(temp=="NumFrames:")
{
value>>mNumFrames;
- mAnim = new SpriteFrame[mNumFrames];
+ mFrames = new SpriteFrame[mNumFrames];
mBuilt = 1;
}
else
value>>yOffset;
double radius;
value>>radius;
- mAnim[count].collisionData.push_back(new CollisionCircle(Point2D(xOffset, yOffset), radius));
+ mFrames[count].collisionData.push_back(new CollisionCircle(Point2D(xOffset, yOffset), radius));
}
else if(c == 'r')
{
double width = 0.0 , height = 0.0;
value>>width;
value>>height;
- mAnim[count].collisionData.push_back(new CollisionRectangle(Point2D(xOffset, yOffset), width, height));
+ mFrames[count].collisionData.push_back(new CollisionRectangle(Point2D(xOffset, yOffset), width, height));
}
value>>c;
}
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_Surface* surface = SDL_DisplayFormat(temp);
+ mFrames[count].width=surface->w;
+ mFrames[count].height=surface->h;
SDL_FreeSurface(temp);
+
+ // Check that the image\92s width is a power of 2
+ if ( (surface->w & (surface->w - 1)) != 0 ) {
+ cout<<"warning: image.bmp\92s width is not a power of 2"<<endl;
+ }
+
+ // Also check if the height is a power of 2
+ if ( (surface->h & (surface->h - 1)) != 0 ) {
+ cout<<"warning: image.bmp\92s height is not a power of 2"<<endl;
+ }
+
+ //get number of channels in the SDL surface
+ GLint nofcolors=surface->format->BytesPerPixel;
+
+ GLenum texture_format=NULL;
+
+ //contains an alpha channel
+ if(nofcolors==4)
+ {
+ if(surface->format->Rmask==0x000000ff)
+ texture_format=GL_RGBA;
+ else
+ texture_format=GL_BGRA;
+ }
+ else if(nofcolors==3) //no alpha channel
+ {
+ if(surface->format->Rmask==0x000000ff)
+ texture_format=GL_RGB;
+ else
+ texture_format=GL_BGR;
+ }
+ else
+ {
+ cout<<"warning: the image is not truecolor...this will break "<<endl;
+ }
+
+ // allocate a texture name
+ glGenTextures( 1, &mFrames[count].image );
+ // select our current texture
+ glBindTexture( GL_TEXTURE_2D, mFrames[count].image );
+
+ // select modulate to mix texture with color for shading
+ glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
+
+ // when texture area is small, bilinear filter the closest mipmap
+ glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
+ GL_LINEAR_MIPMAP_NEAREST );
+ // when texture area is large, bilinear filter the original
+ glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
+
+ // the texture wraps over at the edges (repeat)
+ glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
+ glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
+
+ glTexImage2D( GL_TEXTURE_2D, 0, nofcolors, surface->w, surface->h, 0,
+ texture_format, GL_UNSIGNED_BYTE, surface->pixels );
+
/** sets frame delay and makes sure height and width are correct */
- mAnim[count].pause = pause;
- mW = mAnim[count].image->w;
- mH = mAnim[count].image->h;
+ mFrames[count].pause = pause;
+ mW = mFrames[count].width;
+ mH = mFrames[count].height;
/** Set the animation Peg*/
- mAnim[count].animationPeg = Point2D(x + double(mW)/2, y + double(mH)/2);
+ mFrames[count].animationPeg = Point2D(x + double(mW)/2, y + double(mH)/2);
count++;
}
Animation(std::string animFile);
Animation(std::string animFile, string name);
int loadAnimation(std::string animFile);/**< Loads the Animations from a file in the specified format. */
- SpriteFrame *mAnim;/**< Pointer to the current animation. As an array of SpriteFrames */
+ SpriteFrame *mFrames;/**< Pointer to the current animation. As an array of SpriteFrames */
int mBuilt,/**< Using as a bool */
mNumFrames,/**< Number of frames in this Animation */
mW,/**< Animation's current width */
cout<<"background \""<<_name<<"\" created"<<endl;
}
void Background::draw(){
- /** \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;
const double W = _imageSize.w;
const double H = _imageSize.h;
/*
- 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 += X;
- while (y < 0) // move it down til it's on the screen
- 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);
-
- // 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 = 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-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);
-
- // 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){
+ 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
dest.y = (int)mPos.y;
SDL_BlitSurface(mBackground, &source, mScreen, &dest);
}
+ //*/
+
}
SDL_Surface* Background::load(std::string filename){
- mBackground = LoadImage("Backgrounds/"+filename);
+ /*mBackground = LoadImage("Backgrounds/"+filename);
cout<<"Loaded file"<<endl;
cout<<"Setting Size...";
_imageSize=SizeD(mBackground->w,mBackground->h);///<set the size of the background.
cout<<"done!"<<endl;
- return mBackground;
+ return mBackground;*/
+ return NULL;
}
void Background::setWrap(bool tf){
string _name;///<The background's name
SizeD _imageSize;///<The actual background image's full size.
- SDL_Surface *mBackground;///<pointer to the actual screen
+ Texture mBackground;///<Background Texture
SDL_Surface *mScreen;///< The screen to be drawn to.
SizeD mScreenSize;///<This is the size of the screen to be drawn to. used to get the sections of the background when moving and for screen-at-a-time scrolling.
SDL_Rect source;///< This is the rectangle that gets taken from the actual background image (h,w) determine the size of the rectangle to sample from the background image. (x,y) determine the position of the rectangle's top left corner on the actual background image.
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <vector>
+#include "fns.h"
struct SpriteFrame{
- SDL_Surface *image;/**< Pointer to an image. */
+ Texture image;/**< Image Texture */
int pause;/**< Tells the amount of time to pause between this frame and the next. */
int width;/**< Base width of the frame's image. \todo make this and animation's match or at least sync or delete*/
int height;/**< Base height of the frame's image. \todo make this and animation's match or at least sync or delete*/
#include "fns.h"
#include <string>
#include <iostream>
+#include <GL/gl.h>
const Uint32 Game::waitTime = 1000/60; /* ms */
+using namespace std;
+
+void init_GL(int w = 640, int h = 480)
+{
+ // Set the OpenGL state after creating the context with SDL_SetVideoMode
+
+ glClearColor( 0, 0, 0, 0 );
+
+ glEnable( GL_TEXTURE_2D ); // Need this to display a texture
+
+ glViewport( 0, 0, w, h );
+
+ glMatrixMode( GL_PROJECTION );
+ glLoadIdentity();
+
+ glOrtho( 0, w, h, 0, -1, 1 );
+
+ glMatrixMode( GL_MODELVIEW );
+ glLoadIdentity();
+}
+
+
/** Global static pointer used to ensure a single instance of the class. */
Game* Game::m_instance = NULL;
Game::Game() : mCurrentLevel(0), mScreen(0), ShowCollisions(false), ShowFPS(false), startclock(0), deltaclock(0), currentFPS(0)
{
+ int screenwidth = 640;
+ int screenheight = 480;
+
/** Initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) {
printf ("Couldn't initialize SDL: %s\n", SDL_GetError ());
atexit (SDL_Quit);
/** Set 800x600 video mode */
- mScreen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
+ mScreen = SDL_SetVideoMode(screenwidth, screenheight, 32, SDL_OPENGL);
if (!mScreen) {
printf ("Couldn't set video mode: %s\n", SDL_GetError ());
exit (2);
SDL_ShowCursor(0);
+ init_GL(screenwidth, screenheight);
+
/** Load Resources */
LoadResources();
#include "Level.h"
#include "Game.h"
#include "fns.h"
+#include <GL/gl.h>
Level::Level(SDL_Surface* screen) : mScreen(screen)
{
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);
+ glClear(GL_COLOR_BUFFER_BIT);
/** Draw BG */
if(mBackground!=NULL)
DrawCollisions();
/** Flip the working image buffer with the mScreen buffer */
- SDL_Flip (mScreen);
+ SDL_GL_SwapBuffers();
}
void Level::DrawSprites()
Sprite* getSprite(string name);/**< gets the first sprite with the given name \param name Name of the sprite \return Pointer to the requested Sprite */
void removeSprite(Sprite* sp);/**< remove the Sprite sp from the list of sprites \param ps Sprite to remove */
void addAnimation(Animation anim);/**< add a Actor to the list of sprites \param anim The actor to add */
- Animation* Level::getAnimation(string name);/**< get a Actor* to the list of sprites \param name Name of the actor \return Pointer to the actor we requested or null */
+ Animation* getAnimation(string name);/**< get a Actor* to the list of sprites \param name Name of the actor \return Pointer to the actor we requested or null */
void removeAnimation(Animation anim);/**< remove an Actor to the list of sprites \param anim The actor to add */
void addActor(string name, Actor actor);/**< add a Actor to the list of sprites \param name Name of the actor \param actor The actor to add */
void removeActor(string name);/**< remove the Actor sp from the list of sprites \param name Name of the actor to remove */
#include "Sprite.h"
#include "Game.h"
#include <iostream>
+#include <GL/gl.h>
+
using std::cout;
using std::endl;
Sprite::Sprite(SDL_Surface *screen, std::string name, Actor actor) :
if(mActor.mFrame > mActor.mAnimations[mActor.mCurrentAnimation]->mNumFrames-1)
mActor.mFrame=0;
//update frame so we don't need to worry
- frame = &mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame];
+ frame = &mActor.mAnimations[mActor.mCurrentAnimation]->mFrames[mActor.mFrame];
//obtain next peg
Point2D npos = frame->animationPeg;
//move current position to difference of two
}
}
SDL_Rect dest;
- dest.x = (int)mPos.x;
- dest.y = (int)mPos.y;
- if(mVisible == true)
- SDL_BlitSurface(frame->image,NULL,mScreen,&dest);
+ if(mVisible == true){
+ glLoadIdentity();
+ glBindTexture(GL_TEXTURE_2D, frame->image);
+ glBegin( GL_QUADS );
+ // Top-left vertex (corner)
+ glTexCoord2i( 0, 0 );
+ glVertex3f( mPos.x, mPos.y, 0 );
+
+ // Bottom-left vertex (corner)
+ glTexCoord2i( 1, 0 );
+ glVertex3f( mPos.x+frame->width, mPos.y, 0 );
+
+ // Bottom-right vertex (corner)
+ glTexCoord2i( 1, 1 );
+ glVertex3f( mPos.x+frame->width, mPos.y+frame->height, 0 );
+
+ // Top-right vertex (corner)
+ glTexCoord2i( 0, 1 );
+ glVertex3f( mPos.x, mPos.y+frame->height, 0 );
+ glEnd();
+ glLoadIdentity();
+ }
}
vector<Collision*>& Sprite::getCollisionData(){
- return mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame].collisionData;
+ return mActor.mAnimations[mActor.mCurrentAnimation]->mFrames[mActor.mFrame].collisionData;
}
void Sprite::drawCollisions(){
//get the frame for readability
- SpriteFrame frame = mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame];
+ SpriteFrame frame = mActor.mAnimations[mActor.mCurrentAnimation]->mFrames[mActor.mFrame];
//center the location
Point2D pt = mPos + frame.animationPeg;
WorldObject::drawCollisions(frame.collisionData, pt);
public:
Sprite(SDL_Surface *screen, std::string name, Actor actor);
void setAnimation(int animation){ mActor.mCurrentAnimation = animation; }/**< changes to the specified animation beginning at 0. */
- SpriteFrame* getAnimation(){ return &mActor.mAnimations[mActor.mCurrentAnimation]->mAnim[mActor.mFrame]; }/**< returns active animation. (actually the current frame within the animation) \todo see if this is slower maybe undo */
+ SpriteFrame* getAnimation(){ return &mActor.mAnimations[mActor.mCurrentAnimation]->mFrames[mActor.mFrame]; }/**< returns active animation. (actually the current frame within the animation) \todo see if this is slower maybe undo */
void setFrame(int frame) { mActor.mFrame = frame; }/**< cahnges to the specified frame of the animation beginning at 0. */
int getFrame() { return mActor.mFrame; }/**< returns active frame. */
void setSpeed(float speed) {mSpeed = speed;}/**< sets the Sprite's speed. */
typedef void (*Behavior) ();
///As the name denotes, this just simply does nothing
static void DoNothing(){}
+///Texture name
+typedef unsigned int Texture;
SDL_Surface* LoadImage( std::string filename );/**< Loads supported images. */
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>C:\SDL\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
- <AdditionalDependencies>SDL.lib;SDL_image.lib;SDLmain.lib;SDL_draw.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>SDL.lib;SDL_image.lib;SDLmain.lib;SDL_draw.lib;glu32.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<IgnoreAllDefaultLibraries>false</IgnoreAllDefaultLibraries>
</Link>
<PostBuildEvent>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>C:\SDL\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
- <AdditionalDependencies>SDL.lib;SDL_image.lib;SDLmain.lib;SDL_draw.lib;%(AdditionalDependencies)</AdditionalDependencies>
+ <AdditionalDependencies>SDL.lib;SDL_image.lib;SDLmain.lib;SDL_draw.lib;glu32.lib;opengl32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
<PostBuildEvent>
<Command>copy "C:\SDL\lib\*.dll" "$(OutputPath)\"</Command>