Sign in to follow this  

SDL Problem

This topic is 3192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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:
		#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;
}


[Edited by - thejahooli on March 17, 2009 1:17:46 PM]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I have fixed it now. The problem was that I had accidently not done the PollEVent correctly. It should have been
if(SDL_PollEvent(&movement)) { /* code here */ }
rather than
SDL_PollEvent(&movement) { /*code here */ }

Share this post


Link to post
Share on other sites

This topic is 3192 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this