Create an empthy surface in SDL

(I asked this before on the beginnersforum, but I didn't get an answer)

How can I create an empthy surface in SDL? Is something like the following possible?

1
2
3
4
5
6
7
8
9
10
SDL_Surface *mySurface;
//create an empthy surface, setting the width and height
mySurface = CreateSurface (400,500); 

//perform some actions with the surface:
SDL_FillRect(mySurface, &mySurface->clip_rect, SDL_MapRGB(255,0,0); //draw red
apply_surface(20,20,someSurface,mySurface); //blit another surface to it

//blit surface to the screen
apply_surface(0,0,mySurface,screen);


If my question isn't clear, please tell me.
mySurface = CreateSurface (400,500);

No.


Here is a beginners SDL tutorial:
http://gpwiki.org/index.php/SDL:Tutorials:Displaying_a_Bitmap
Thanks for the reply.

Normally I don't post several topics on the same subjects, but I didn't get an answer. Next time I shall be more patient. :)

This tutial didn't help very much. I'm pretty sure that what I want is possible, but maybe you understand me wrong. I played around a bit with some code. Can you tell me why the following program won't run? I'm trying to create a white surface. But the SDL_FillRect() command crashes the program.

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
#include "SDL/SDL.h"

//screen attributes
const int SCREEN_HEIGHT = 500;
const int SCREEN_WIDTH  = 500;
const int SCREEN_BPP    = 32;

//screen
SDL_Surface *screen  = NULL;
//the surface
SDL_Surface *surface = NULL;

//function to draw a surface to the screen
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
    SDL_Rect offsets;
    offsets.x=x;
    offsets.y=y;
    SDL_BlitSurface(source,NULL,destination,&offsets);
}

int main(int argc, char* args[])
{
    //Initialize SDL
    SDL_Init(SDL_INIT_EVERYTHING);
    
    //Set screen to video mode
    screen = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE);
    
    //==========================================================================
    //Here I'm trying to create an empthy surface:
    
    //allocate memory
    surface = new SDL_Surface;
    
    //set width and height
    surface->clip_rect.w = 300;
    surface->clip_rect.h = 300;
    
    //Draw the surface white (this comand crashes the program)
    SDL_FillRect(surface,&surface->clip_rect,SDL_MapRGB(surface->format,0xff,0xff,0xff));
    
    //apply the surface to the screen
    apply_surface( 100, 100, surface, screen);
    
    //flip screen
    SDL_Flip(screen);
    
    //whait before ending
    SDL_Delay(5000);
    
    //free surface
    SDL_FreeSurface(surface);
    
    //==========================================================================
    
    //quit SDL
    SDL_Quit();
    
    return 0;
}


(I know I should check for errors etc, but that's not really important now)

I tried the following too, and it doesn't crash the program. But the screen is still completely black:

1
2
//use the screen format
SDL_FillRect(surface,&surface->clip_rect,SDL_MapRGB(screen->format,0xff,0xff,0xff));



Thanks for your help.
There is an SDL_CreateRGBSurface that works almost exactly how you expected in your original post. I don't know why guestgulkan said "no" to that.

1
2
3
4
5
SDL_Surface* mySurface = SDL_CreateRGBSurface( flags, width, height, bitdepth, rmask, gmask, bmask, amask );

// Blit to/from mySurface at will

SDL_FreeSurface(mySurface);


performance is best when bitdepth and the masks match your output display format (that way no conversion has to take place) -- assuming this surface is going to be blitted to the display or another surface of the same format. To get that stuff you can probe the SDL_PixelFormat* of the surface created via SDL_SetVideoMode. To make this easier, you can wrap SDL_CreateRGBSurface in another function:

1
2
3
4
5
6
7
8
9
10
SDL_Surface* CreateSurface(Uint32 flags,int width,int height,const SDL_Surface* display)
{
  // 'display' is the surface whose format you want to match
  //  if this is really the display format, then use the surface returned from SDL_SetVideoMode

  const SDL_PixelFormat& fmt = *(display->format);
  return SDL_CreateRGBSurface(flags,width,height,
                  fmt.BitsPerPixel,
                  fmt.Rmask,fmt.Gmask,fmt.Bmask,fmt.Amask );
}


See SDL documentation for the 'flags' and further information. Get the downloadable version here:

http://www.libsdl.org/archives/sdldoc-html.zip

As the online docs don't seem to be reference docs, but merely tutorials and as such do not mention a lot of this. To figure out how to do something, you can just skim the function list in 'index.html' in the docs to see which function does what you want.
Last edited on
I was saying no to the function name/parameters he gave.
guestgulkan, you have understood me wrong; I knew that exact and function + parameters probably wouldn't exist, but I was looking for something similar. I'll try to be more clear next time.

Disch; great! That seems to be exactly what I need. And the wrapper makes things even more easy. Thanks!
Topic archived. No new replies allowed.