Hiya
I copied the tutorial at
http://cone3d.gamedev.net/cgi-bin/index.pl?page=tutorials/gfxsdl/tut3
and found that for some reason when I try to run it all that happens is that the whole screen goes black (like when you press Alt + F5) and then comes out of it with the command prompt showing but no screen, also I tried to output some text to the command prompt via "std::cout" at the start of "int main()" and it wasn't displayed.
Here is my code:
Main.cpp
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
#include "Sprite.h"
#include <iostream>
SDL_Surface *screen, *back;
CSpriteBase gaurdBase;
CSprite gaurd;
SDL_Surface * ImageLoad(char *file){
SDL_Surface *temp1, *temp2;
temp1 = SDL_LoadBMP(file);
temp2 = SDL_DisplayFormat(temp1);
SDL_FreeSurface(temp1);
return temp2;
}
int InitImages(){
back = ImageLoad("data/bg.bmp");
return 0;
}
void DrawIMG(SDL_Surface *img, int x, int y){
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}
void DrawBG(){
DrawIMG(back, 0, 0);
}
void DrawScene(){
gaurd.clearBG();
gaurd.updateBG();
gaurd.draw();
SDL_Flip(screen);
}
int main(int argc, char *argv[])
{
std::cout << "Hello";
Uint8* keys;
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
screen=SDL_SetVideoMode(640,480,32,
SDL_SWSURFACE|SDL_FULLSCREEN|SDL_HWPALETTE);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
gaurdBase.init("Images/Person");
gaurd.init(&gaurdBase,screen);
gaurd.set(350,300);
gaurd.Speed() = 1.5;
SDL_ShowCursor(0);
InitImages();
DrawBG();
int done=0;
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
//if ( event.key.keysym.sym ==SDLK_SPACE){sun.toggleAnim();}
}
}
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) { gaurd.Y() += -1; }
if ( keys[SDLK_DOWN] ) { gaurd.Y() += 1; }
if ( keys[SDLK_LEFT] ) { gaurd.X() += -1; }
if ( keys[SDLK_RIGHT] ) { gaurd.X() +=1 ; }
DrawScene();
}
return 0;
}
/*
int main ( int argc, char** argv )
{
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// load an image
SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
if (!bmp)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
// center the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;
// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(bmp);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}*/
Sprite.h
#ifndef SPRITE_H
#define SPRITE_H
struct CSpriteFrame
{
SDL_Surface *image;
int pause;
};
class CSpriteBase
{
public:
int init(char *dir);
~CSpriteBase();
CSpriteFrame *mAnim;
int mBuilt, mNumframes, mW, mH;
};
class CSprite
{
public:
int init(CSpriteBase *base, SDL_Surface *screen);
void draw();
void clearBG();
void updateBG();
int &Frame() { return mFrame; }
float &Speed() { return mSpeed; }
void toggleAnim() { mAnimating = !mAnimating; }
void startAnim() { mAnimating = 1; }
void stopAnim() { mAnimating = 0; }
void rewind() { mFrame = 0; }
int &X() { return mX; }
int &Y() { return mY; }
void set(int xx, int yy) { mX=xx; mY=yy; }
private:
int mFrame;
int mX, mY, mOldX, mOldY;
int mAnimating;
int mDrawn;
float mSpeed;
long mLastupdate;
CSpriteBase *mSpriteBase;
SDL_Surface *mBackreplacement;
SDL_Surface *mScreen;
};
#endif
Sprite.cpp
#ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
#include <String>
#include "Sprite.h"
int CSpriteBase::init(char *dir)
{
char buffer[255];
char filename[255];
char name[255];
int pause=0, r=0, g=0, b=0;
FILE *fp;
sprintf(filename, "%s/info", dir);
if((fp=fopen(filename, "r")) == NULL)
{
printf("ERROR opening file %s\n\n", filename);
return -1;
}
fgets(buffer, 255, fp);
sscanf(buffer, "FILES: %d", &mNumframes);
mAnim = new CSpriteFrame[mNumframes];
mBuilt = 1;
int count = 0;
while(!feof(fp) && count<mNumframes)
{
fgets(buffer, 255, fp);
if(buffer[0] != '#' && buffer[0] != '\r' && buffer[0] != '\0'
&& buffer[0] != '\n' && strlen(buffer) != 0)
{
sscanf(buffer, "%s %d %d %d %d", name, &pause, &r, &g, &b);
sprintf(filename, "%s/%s", dir, name);
SDL_Surface *temp;
if((temp = SDL_LoadBMP(filename)) == NULL) return -1;
if(r >= 0) SDL_SetColorKey(temp, SDL_SRCCOLORKEY,
SDL_MapRGB(temp->format, r, g, b));
mAnim[count].image = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
mAnim[count].pause = pause;
if(!mW) mW = mAnim[count].image->w;
if(!mH) mH = mAnim[count].image->h;
count++;
}
}
fclose(fp);
return 0;
}
CSpriteBase::~CSpriteBase()
{
delete []mAnim;
}
int CSprite::init(CSpriteBase *base, SDL_Surface *screen)
{
mSpriteBase = base;
if(mSpriteBase->mBuilt)
{
if(mSpriteBase->mNumframes>1) mAnimating=1;
mBackreplacement =
SDL_DisplayFormat(mSpriteBase->mAnim[0].image);
}
mScreen = screen;
}
void CSprite::clearBG()
{
if(mDrawn==1)
{
SDL_Rect dest;
dest.x = mOldX;
dest.y = mOldY;
dest.w = mSpriteBase->mW;
dest.h = mSpriteBase->mH;
SDL_BlitSurface(mBackreplacement, NULL, mScreen, &dest);
}
}
void CSprite::updateBG()
{
SDL_Rect srcrect;
srcrect.w = mSpriteBase->mW;
srcrect.h = mSpriteBase->mH;
srcrect.x = mX;
srcrect.y = mY;
mOldX=mX;mOldY=mY;
SDL_BlitSurface(mScreen, &srcrect, mBackreplacement, NULL);
}
void CSprite::draw()
{
if(mAnimating == 1)
{
if(mLastupdate+mSpriteBase->mAnim[mFrame].pause*mSpeed<
SDL_GetTicks())
{
mFrame++;
if(mFrame>mSpriteBase->mNumframes-1) mFrame=0;
mLastupdate = SDL_GetTicks();
}
}
if(mDrawn==0) mDrawn=1;
SDL_Rect dest;
dest.x = mX; dest.y = mY;
SDL_BlitSurface(mSpriteBase->mAnim[mFrame].image, NULL, mScreen,
&dest);
}
Any help would be much appreciated.
On a different note I found a useful trick on www.functionx.com - returning a reference, therefore you don't need get and set functions but still can (if required) to make sure the variable isn't set outside a range. I use this for my Sprite class for Speed, Frame, X, Y, are there any disadvantages to this approach?
[Edited by - Peter Conn on September 15, 2006 3:06:35 PM]
Whether you think you can or think you can’t, you’re probably right – Henry Ford