Jump to content

Quick SDL question -- SDL_CreateRGBSurface


1veedo

Recommended Posts

Hi I recently started learning some SDL but I cant seem to get SDL_CreateRGBSurface to work.

 

http://linux.die.net/man/3/sdl_creatergbsurface

http://docs.mandragor.org/files/Common_libs_documentation/SDL/SDL_Documentation_project_en/sdlcreatergbsurface.html

 

I have some basic code which displays a moveable image, and I want a solid color generated with createRGBSurface for my background. So when you move the image you cover up the old image w/ that part of the background and redraw the image. For some reason using 8 bit will generate an all black background (regardless of rgba values), and anything else seems to be completely null or something (the image leaves a trail).

#include <stdio.h>
#include <stdlib.h>

#include "SDL.h"

void Slock(SDL_Surface *screen);
void Sulock(SDL_Surface *screen);

int main(int argc, char *argv[])
{
if( SDL_Init(SDL_INIT_VIDEO | SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
return -1;
}

SDL_Surface *screen;
screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF);

if(screen == NULL)
{
	fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError());
	return 0;
}

SDL_Surface *image;

image = SDL_LoadBMP("doggy.bmp");

if(image == NULL)
{
	fprintf(stderr, "Unable to load image: %s\n", SDL_GetError());
	//return 0;
}

SDL_Surface *back; //background
SDL_Event event;
SDL_Rect rect;
SDL_Rect rback;
SDL_Rect rback2;

rect.x = 50;
rect.y = 50;

rback2.x = 50;
rback2.y = 50;
rback2.w = image->w;
rback2.h = image->h;	

rback.x = 50;
rback.y = 50;

back = SDL_CreateRGBSurface(SDL_HWSURFACE, 639, 479, 8, 0x70, 0x80, 0x90, 0x64); //639 -- minus one from 640 just in case until it starts working

if (back == NULL) {
               fprintf(stderr, "Unable to create SDL Surface for background"
                ": %s\n", SDL_GetError());
        //       exit(1);
       }

Slock(screen);
SDL_BlitSurface(back,NULL,screen,NULL);
Sulock(screen);
SDL_Flip(screen);

while(1)
{
	while(SDL_PollEvent(&event))
	{
		switch(event.type)
		{
			case SDL_KEYDOWN:
			switch(event.key.keysym.sym)
			{
				case SDLK_ESCAPE:
				exit(0); break;

				case SDLK_RIGHT:
				rect.x +=5;
				break;

				case SDLK_LEFT:
				rect.x -= 5;
				break;

				case SDLK_UP:
				rect.y -=5;
				break;

				case SDLK_DOWN:
				rect.y += 5;
				break;

				default:
				break;
			}
			break;

			case SDL_QUIT:
			exit(0);
			break;

			default:
			break;
		}

		Slock(screen);
		SDL_BlitSurface(back, &rback2, screen, &rback);
		SDL_BlitSurface(image, NULL, screen, &rect);
		Sulock(screen);
		SDL_Flip(screen);

		rback.x = rect.x;
		rback.y = rect.y;
		rback2.x = rect.x;
		rback2.y=rect.y;

	}
}
}

void Slock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
	if ( SDL_LockSurface(screen) < 0 )
	{
		return;
	}
}
}

void Sulock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
	SDL_UnlockSurface(screen);
}
}

Here's the part of code that's giving me problems:

SDL_Surface *back;
...
back = SDL_CreateRGBSurface(SDL_HWSURFACE, 639, 479, 8, 0x70, 0x80, 0x90, 0x64);

I've tried all different values for Uinit32 from full string 0x0000ff00 etc to integers and different bits but cant seem to get it working right. Any help is appreciated, thanks :)

Link to comment
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
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.