#include "SDL.h"
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
bool gamequit = false;
//set surfaces
SDL_Surface *screen;
SDL_Surface *playerimage;
//player class
class Player
{
public:
int w;
int h;
int x;
int y;
int speed;
};
//surface applying
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
//creates rect for surface
SDL_Rect offset;
//sets the offset x and y to the given x and y
offset.x = x;
offset.y = y;
//blits the surface
SDL_BlitSurface(source, NULL, destination, &offset);
}
//game end function
void gameend()
{
SDL_FreeSurface(screen);
SDL_FreeSurface(playerimage);
SDL_Quit();
}
int main(int argc, char* argv[])
{
//initialize SDL
SDL_Init(SDL_INIT_VIDEO);
//images and screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP,SDL_SWSURFACE);
playerimage = SDL_LoadBMP("player.bmp");
//game event
SDL_Event movement;
//sets player variables
Player player;
player.x = SCREEN_WIDTH / 2 - 16;
player.y = SCREEN_HEIGHT / 2 - 16;
player.speed = 8;
//main game loop
for(gamequit = false; gamequit != true;)
{
//draws the player to the screen
apply_surface(player.x,player.y,playerimage,screen);
//Polls for an event
SDL_PollEvent(&movement);
//if the event was a key being presses
if(movement.key.state == SDL_PRESSED)
{
//checks which key is being pressed
// and sets the players x and y to the necessery adjustments
if(movement.key.keysym.sym == SDLK_DOWN)
{
player.y = player.y + player.speed;
}
if(movement.key.keysym.sym == SDLK_UP)
{
player.y = player.y - player.speed;
}
if(movement.key.keysym.sym == SDLK_LEFT)
{
player.x = player.x - player.speed;
}
if(movement.key.keysym.sym == SDLK_RIGHT)
{
player.x = player.x + player.speed;
}
}
if(movement.type == SDL_QUIT)
{
gamequit = true;
gameend();
}
//check collision
if(player.x < 0)
{
player.x = 0;
}
if(player.x > SCREEN_WIDTH - player.w)
{
player.x = SCREEN_WIDTH - player.w;
}
if(player.y < 0)
{
player.y = 0;
}
if(player.y > SCREEN_HEIGHT - player.h)
{
player.y = SCREEN_HEIGHT - player.h;
}
SDL_Flip(screen);
}
SDL_Delay(10000);
return 0;
}
SDL Problem
I am having a problem with SDL.
Everytime I try to run a program using Visual C++ with SDL it stops responding after a few seconds. If anyone could help this is the code:
[Edited by - thejahooli on March 17, 2009 1:17:46 PM]
Is that the actual code? Is it edited or abridged in any way?
Perhaps you could repost the source file in its entirety, using [source] tags to preserve the formatting. Meanwhile though, it looks to me like:
1. You're not calling SDL_Init() anywhere.
2. Even if SDL_Init() were being called, SDL_SetVideoMode() would be invoked prior to this call.
Perhaps you could repost the source file in its entirety, using [source] tags to preserve the formatting. Meanwhile though, it looks to me like:
1. You're not calling SDL_Init() anywhere.
2. Even if SDL_Init() were being called, SDL_SetVideoMode() would be invoked prior to this call.
You're also not checking if you actually get an event with pollevent, or what type it is. You need to know it's a key event before you examine the 'key' member, for example.
I have edited it a bit but still not working.
And also this is the entire code. It is just a test of using SDL.
And also this is the entire code. It is just a test of using SDL.
SDL_Delay maybe?
edit: nvm, I think I misread your code without indentation. If you can repost your code inside a code block that will help people reading your code.
edit: nvm, I think I misread your code without indentation. If you can repost your code inside a code block that will help people reading your code.
Quote:I have edited it a bit but still not working.Could you do the following?
1. Click the 'edit' button.
2. Add '[source]' before the beginning of the source code in your post.
3. Add '[/source]' after the end of the source code in your post.
This will put your code in one of those nice little boxes, and will make it much easier for us to read.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement