I have started using SDL and am having trouble. Please bear in mind I AM ONLY 13.
I have this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <string>
//Screen attributes
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//The surfaces
SDL_Surface *sprite = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
//The event structure that will be used
SDL_Event event;
SDL_Surface *load_image( std::string filename )
{
//The image that's loaded
SDL_Surface* loadedImage = NULL;
//The optimized image that will be used
SDL_Surface* optimizedImage = NULL;
//Load the image
loadedImage = IMG_Load( filename.c_str() );
//If the image loaded
if( loadedImage != NULL )
{
//Create an optimized image
optimizedImage = SDL_DisplayFormat( loadedImage );
//Free the old image
SDL_FreeSurface( loadedImage );
}
//Return the optimized image
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
//Temporary rectangle to hold the offsets
SDL_Rect offset;
//Get the offsets
offset.x = x;
offset.y = y;
//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}
bool init()
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{
return false;
}
//Set the window caption
SDL_WM_SetCaption( "Platform Game", NULL );
//If everything initialized fine
return true;
}
bool load_files()
{
//Load the image
sprite = load_image( "sprite.png" );
background = load_image( "background.png" );
//If there was an error in loading the image
if( sprite == NULL )
{
return false;
}
//If everything loaded fine
return true;
}
void clean_up()
{
//Free the surface
SDL_FreeSurface( sprite );
SDL_FreeSurface( background );
//Quit SDL
SDL_Quit();
}
class Sprite
{
private:
//The initial offsets
int x, y;
//The velocity of the sprite
int xVel, yVel;
public:
//Initialize the variables
Sprite();
//Handles key presses
void handle_input();
//Move the sprite
void move();
//Show the sprite
void show();
};
Sprite::Sprite()
{
//Initialisze the offests
x = 0;
y = 470;
//Initialize velocity
xVel = 0;
yVel = 0;
}
void Sprite::handle_input()
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Adjust the velocity switch
( event.key.keysym.sym ) ;
{
case SDLK_UP: yVel -= SPRITE_HEIGHT / 2; break;
case SDLK_DOWN: yVel += SPRITE_HEIGHT / 2; break;
case SDLK_LEFT: xVel -= SPRITE_WIDTH / 2; break;
case SDLK_RIGHT: xVel += SPRITE_WIDTH / 2; break;
}
}
//If a key was released
else if( event.type == SDL_KEYUP )
{
//Adjust the velocity
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += SPRITE_HEIGHT / 2; break;
case SDLK_DOWN: yVel -= SPRITE_HEIGHT / 2;break;
case SDLK_LEFT: xVel += SPRITE_WIDTH / 2; break;
case SDLK_RIGHT: xVel -= SPRITE_WIDTH / 2; break;
}
}
}
void Sprite::move()
{
//Move the sprite left or right
x += xVel;
//If the sprite moves to far left or right
if( ( x < 0 ) || ( x + SPRITE_WIDTH > SCREEN_WIDTH ) )
{
//move back
x -= xVel;
}
//Move the sprite up or down
y += yVel;
//If the sprite went too far up or down
if( ( y < 0 ) || ( y + SPRITE_HEIGHT > SCREEN_HEIGHT ) )
{
//move back
y -= yVel;
}
}
void Sprite::show()
{
//Show the sprite
apply_surface(x, y, sprite, screen);
}
int main( int argc, char* args[] )
{
//Make sure the program waits for a quit
bool quit = false;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Apply the surface to the screen
apply_surface( 0, 0, background, screen );
//While the user hasn't quit
while( quit == false )
{
//While there's an event to handle
while( SDL_PollEvent( &event ) )
{
//Handle events for the Sprite
mySprite.handle_input();
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
//Move the sprite
mySprite.move();
//Show the dot
mySprite.show();
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
//Free the surface and quit SDL
clean_up();
return 0;
}
|
and these errors:
C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp In member function `void Sprite::handle_input()':
148 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp case label ` SDLK_UP' not within a switch statement
148 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp `SPRITE_HEIGHT' undeclared (first use this function)
148 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp break statement not within loop or switch
149 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp case label ` SDLK_DOWN' not within a switch statement
150 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp break statement not within loop or switch
151 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp case label ` SDLK_RIGHT' not within a switch statement
151 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp break statement not within loop or switch
C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp In member function `void Sprite::move()':
174 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp `SPRITE_WIDTH' undeclared (first use this function)
182 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp `SPRITE_HEIGHT' undeclared (first use this function)
C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp In function `int SDL_main(int, char**)':
222 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp `mySprite' undeclared (first use this function)
236 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp `mySprite' undeclared (first use this function)
245 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp `fps' undeclared (first use this function)
245 C:\Documents and Settings\Timbo\My Documents\C++\SDL\Platform Game\main.cpp `FRAMES_PER_SECOND' undeclared (first use this function)