Choad
November 28th, 2006, 01:43 PM
#include <stdio.h>
#include <stdlib.h>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
////////////////
// Prototypes //
////////////////
void DrawScene(void);
bool Init(void);
SDL_Surface * LoadImage( std::string filename );
/////////////
// Globals //
/////////////
SDL_Surface * screen;
SDL_Surface * sprBackground;
SDL_Surface * sprPlayer;
SDL_Surface * sprBlock;
SDL_Rect rectBackground;
SDL_Rect rectPlayer;
SDL_Rect rectPlayerSource;
SDL_Rect rectBlock;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
bool levelLayout[19][14];
///////////////
// Functions //
///////////////
void DrawScene(void)
{
SDL_BlitSurface(sprBackground,
NULL,
screen,
&rectBackground);
SDL_BlitSurface(sprPlayer,
&rectPlayerSource,
screen,
&rectPlayer);
SDL_Flip(screen);
}
bool Init(void)
{
// set up the level
for (int i = 0; i < 20; i++)
{
for (int o = 0; o < 15; o++)
{
if ((i == 0) || (i == 19) || (o == 0) || (o == 14))
{
levelLayout[i][o] = true;
}
else
{
levelLayout[i][o] = false;
}
}
}
// load sprites
sprBackground = LoadImage("background.png");
sprPlayer = LoadImage("player.png");
sprBlock = LoadImage("block.png");
rectBackground.x = 0;
rectBackground.y = 0;
rectPlayer.x = 64;
rectPlayer.y = 64;
rectPlayerSource.x = 0;
rectPlayerSource.y = 0;
rectPlayerSource.w = 32;
rectPlayerSource.h = 32;
//Initialize SDL video subsystems
if( SDL_Init( SDL_INIT_VIDEO ) == -1 )
{
return false;
}
// create screen
screen = SDL_SetVideoMode( SCREEN_WIDTH,
SCREEN_HEIGHT,
SCREEN_BPP,
SDL_SWSURFACE );
//Register SDL_Quit() with atexit
atexit(SDL_Quit);
}
SDL_Surface *LoadImage( std::string filename )
{
SDL_Surface* loaded_image = NULL;
SDL_Surface* compatible_image = NULL;
if(filename.c_str() == NULL)
{
return NULL;
}
// load the image
loaded_image = IMG_Load(filename.c_str());
if( loaded_image == NULL )
{
// if not exit the function
return NULL;
}
// convert image to screen format
compatible_image = SDL_DisplayFormat( loaded_image );
if( compatible_image != NULL )
{
// set transparrent colour
Uint32 colorkey = SDL_MapRGB( compatible_image->format,
255,
0,
255); // choose pink
SDL_SetColorKey(compatible_image,
SDL_RLEACCEL | SDL_SRCCOLORKEY,
colorkey);
}
// Destroy the old copy
SDL_FreeSurface( loaded_image );
// return a pointer to the newly created display compatible image
return compatible_image;
}
//////////
// Main //
//////////
int main(int argc, char *argv[])
{
Init();
int done = 0;
Uint8 *keys;
SDL_Event event; //Used to point to SDL_Event
SDL_Rect dest;
/** Create a while loop that exits when the variable done = 1.
This example keeps running until Esc key is pressed. */
while (done == 0)
{
//Using polling to read input
while (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE)
{
done = 1;
}
}//end if
}//end while
DrawScene();
}//end while
return 0;
}
well, its a fair chunk of code... it compiles just fine but i cant run it. i am a bit annoyed because i kept on compiling it to see if it was working and it was, but i wasnt actually running the binary, so i cant figure out what bit of code caused the error
i am compiling it with this command
$ g++ `sdl-config --cflags --libs` -o mything mything.cpp -lSDL -lSDL_image
you need a player.png, background.png and block.png
any help appreciated :)
#include <stdlib.h>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
////////////////
// Prototypes //
////////////////
void DrawScene(void);
bool Init(void);
SDL_Surface * LoadImage( std::string filename );
/////////////
// Globals //
/////////////
SDL_Surface * screen;
SDL_Surface * sprBackground;
SDL_Surface * sprPlayer;
SDL_Surface * sprBlock;
SDL_Rect rectBackground;
SDL_Rect rectPlayer;
SDL_Rect rectPlayerSource;
SDL_Rect rectBlock;
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
bool levelLayout[19][14];
///////////////
// Functions //
///////////////
void DrawScene(void)
{
SDL_BlitSurface(sprBackground,
NULL,
screen,
&rectBackground);
SDL_BlitSurface(sprPlayer,
&rectPlayerSource,
screen,
&rectPlayer);
SDL_Flip(screen);
}
bool Init(void)
{
// set up the level
for (int i = 0; i < 20; i++)
{
for (int o = 0; o < 15; o++)
{
if ((i == 0) || (i == 19) || (o == 0) || (o == 14))
{
levelLayout[i][o] = true;
}
else
{
levelLayout[i][o] = false;
}
}
}
// load sprites
sprBackground = LoadImage("background.png");
sprPlayer = LoadImage("player.png");
sprBlock = LoadImage("block.png");
rectBackground.x = 0;
rectBackground.y = 0;
rectPlayer.x = 64;
rectPlayer.y = 64;
rectPlayerSource.x = 0;
rectPlayerSource.y = 0;
rectPlayerSource.w = 32;
rectPlayerSource.h = 32;
//Initialize SDL video subsystems
if( SDL_Init( SDL_INIT_VIDEO ) == -1 )
{
return false;
}
// create screen
screen = SDL_SetVideoMode( SCREEN_WIDTH,
SCREEN_HEIGHT,
SCREEN_BPP,
SDL_SWSURFACE );
//Register SDL_Quit() with atexit
atexit(SDL_Quit);
}
SDL_Surface *LoadImage( std::string filename )
{
SDL_Surface* loaded_image = NULL;
SDL_Surface* compatible_image = NULL;
if(filename.c_str() == NULL)
{
return NULL;
}
// load the image
loaded_image = IMG_Load(filename.c_str());
if( loaded_image == NULL )
{
// if not exit the function
return NULL;
}
// convert image to screen format
compatible_image = SDL_DisplayFormat( loaded_image );
if( compatible_image != NULL )
{
// set transparrent colour
Uint32 colorkey = SDL_MapRGB( compatible_image->format,
255,
0,
255); // choose pink
SDL_SetColorKey(compatible_image,
SDL_RLEACCEL | SDL_SRCCOLORKEY,
colorkey);
}
// Destroy the old copy
SDL_FreeSurface( loaded_image );
// return a pointer to the newly created display compatible image
return compatible_image;
}
//////////
// Main //
//////////
int main(int argc, char *argv[])
{
Init();
int done = 0;
Uint8 *keys;
SDL_Event event; //Used to point to SDL_Event
SDL_Rect dest;
/** Create a while loop that exits when the variable done = 1.
This example keeps running until Esc key is pressed. */
while (done == 0)
{
//Using polling to read input
while (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE)
{
done = 1;
}
}//end if
}//end while
DrawScene();
}//end while
return 0;
}
well, its a fair chunk of code... it compiles just fine but i cant run it. i am a bit annoyed because i kept on compiling it to see if it was working and it was, but i wasnt actually running the binary, so i cant figure out what bit of code caused the error
i am compiling it with this command
$ g++ `sdl-config --cflags --libs` -o mything mything.cpp -lSDL -lSDL_image
you need a player.png, background.png and block.png
any help appreciated :)