Sign in to follow this  
Afr0m@n

Clearscreen problem in SDL

Recommended Posts

Afr0m@n    100
I'm dabbling around in C/C++ (though most of this project seems to use C so far) while writing a network engine for my MMORPG, now currently existing in a thourough version in Blitz3D and a rather unstable (though I'm working on it) version in BlitzMax, which is the language I'm using at the moment. The reason for dabbling in C/C++? Obviously to get prepared to move in to the industry. Anyways.. in the project I'm doing now using SDL, I'm trying to clear the screen due to the spaceship that the player controls leaving bits and pieces of itself behind on the screen because of me updating it's speed a little bit yesterday. So far so good. Problem I'm having is that when using SDL_FillRect, my program just initializes and then shuts down again for no apparent reason! I don't know why, but I think the problem is with SDL_MapRBG (which I'm passing as a parameter to SDL_FillRect). I've created a palette variable to use with SDL_MapRBG, but at the moment it's empty. That might be part of the problem. I don't really know! :\ Does anyone else know, perhaps? =)
#include stdio.h
#include "stdlib.h"
#include "SDL.h"
#include "windows.h"
#include "SDLWrapper_Memory.h"
#include "list.h"

SDL_Surface *back;
SDL_Surface *image;
SDL_Surface *screen;
SDL_Event event;
SDL_PixelFormat *palette; //PALETTE USED WITH SDL_MAPRGB!

linkedList *EnemyList;
int timer;

//palette = screen -> format;

BITFIELD_CREATE(KeyboardDown, SDLK_LAST);

void DrawImage(SDL_Surface *img, int x, int y)
{
  SDL_Rect dest;
  dest.x = x;
  dest.y = y;
  SDL_BlitSurface(img, NULL, screen, &dest);
}

bool KeyDown(uint32 uiAscii)
{
	return (BITFIELD_GET(KeyboardDown, uiAscii) != 0);
}

///////////////////////////// GAME STRUCTS ///////////////////////////////////////
typedef struct _PLAYER
{
	int ypos;
	int xpos;
	SDL_Surface *img;
} player;
 
typedef struct _ENEMY
{
	int ypos;
	int xpos;
	SDL_Surface *img;
} enemy;
////////////////////////////////////////////////////////////////////////////////////

player p;
linkedList *list;

int main(int argc, char *argv[])
{
	if (SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0)
	{
		printf("Unable to init SDL: %s\n", SDL_GetError());
		exit(1);
	}

	screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
	if ( screen == NULL )
	{
		printf("Unable to set 640x480 video: %s\n", SDL_GetError());
		exit(1);
	}

	int done=0;
	p.img = SDL_LoadBMP("Graphics/Ships/Player/plyshp01a.bmp");
	p.xpos = 250;
	p.ypos = 200;

	while(done == 0)
	{ 

		while (SDL_PollEvent(&event))
		{
			if (event.type == SDL_QUIT)  {done = 1;}
			if (event.type == SDL_KEYDOWN)
			{
				BITFIELD_SET( KeyboardDown, event.key.keysym.sym);
			}
			if (event.type == SDL_KEYUP)
			{
				BITFIELD_RESET( KeyboardDown, event.key.keysym.sym);
			}
		}

		//This shit right here enables us to generate enemies every half second.
		if(timeGetTime() > timer + 500)
		{
			timer = timeGetTime();
			listNode *enemy;
		}
		
		if(KeyDown(SDLK_ESCAPE))
		{
			done = 1;
		}
		
		if(KeyDown(SDLK_UP))
		{
			if(p.ypos > 0)
			{
				p.ypos = p.ypos - 2;
			}
		}

		if(KeyDown(SDLK_DOWN))
		{
			if(p.ypos < 426)
			{
				p.ypos = p.ypos + 2;
			}
			
		}

		if(KeyDown(SDLK_LEFT))
		{
			if(p.xpos > 0)
			{
				p.xpos = p.xpos - 2;
			}
		}

		if(KeyDown(SDLK_RIGHT))
		{
			if(p.xpos < 576)
			{
				p.xpos = p.xpos + 2;
			}
		}
		
                //FILLRECT!
		SDL_FillRect(screen, NULL, SDL_MapRGB(palette, 0, 0, 0));
		DrawImage(p.img, p.xpos, p.ypos);
		SDL_Flip(screen);
    }

	return 0;
}

Share this post


Link to post
Share on other sites
Vexorian    152
Certainly, it is a null pointer, and null pointers make SDL windows to close, Solution? simply set the palette surface format variable after setting screen

Share this post


Link to post
Share on other sites
Afr0m@n    100
Thanks! :) I'm not sure what to do with palette to use it correctly with SDL_MapRGB though! Could anyone please explain that? I tried to google it and found a solution that said that I should do this:

palette=screen->format;


That caused my MSVC to spit out 4 error messages though! :\

Share this post


Link to post
Share on other sites
kodeninja    214
Try something like this:

screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
exit(1);
}
palette = screen->format;


The screen var need to be set by SDL_SetVideoMode before you can access it, also as far as I know C/C++ does not allow code outside of functions/procedures.

Of course another alternative would be instead of:
SDL_FillRect(screen, NULL, SDL_MapRGB(palette, 0, 0, 0));
try
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
(and just get rid of the palette var all together)

Either of the two should work...

Share this post


Link to post
Share on other sites

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