Bitmap loading/viewing difficulties with SDL

I have NO idea what-so-ever why this code doesn’t work
correctly.

I compile it like this:

[gdog at localhost sdl]$ gcc sdlbitmap.c -lSDL -ldl
[gdog at localhost sdl]$ ./a.out
Unable to map surface! :
SDL Warning: 1 SDL surfaces extant
[gdog at localhost sdl]$

What does that “SDL Warning: 1 SDL surfaces extant” mean?

Anyhow…I compile the source and run the app.

All I get is a blank window that is black and it complains about not
being able to map the surface. From what I can see I can’t notice any
errors in my code.

The image that I am loading in my program can be obtained here:

http://pknet.com/~gdog/fool.bmp.gz

I know that it’s a 256x256 BMP at 8 bits per pixel.

Other than that I have no clue at why all I see is just a black screen.
(black)

I am running in a 16 bit video mode under X windows.

Here is the source:

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

main() {

SDL_Surface *bitmap;
SDL_Surface *screen;

atexit(SDL_Quit);

if(SDL_Init(SDL_INIT_VIDEO) < 0)
printf(“Can’t init SDL!\n”), exit(1);

bitmap = SDL_LoadBMP(“fool.bmp”);
if(bitmap == NULL)
printf(“Can’t load the damn bitmap!\n”), exit(1);

screen = SDL_SetVideoMode(bitmap->w + 5, bitmap->h + 5, 8,
SDL_SWSURFACE);
if(screen ==NULL)
printf(“Unable to Set the freaking video mode! %s\n”, SDL_GetError());

/* Set palette according to BMP file palette */
if(SDL_SetColors(screen, bitmap->format->palette->colors, 1,
bitmap->format->palette->ncolors) == 0)
printf(“Couldn’t set all the damn colors! : %s\n”, SDL_GetError());

if(SDL_MapSurface(bitmap, screen->format) == 0)
printf(“Unable to map surface! : %s\n”, SDL_GetError());

if(SDL_BlitSurface(bitmap, NULL, screen, NULL) == 0)
printf(“Unable to perform blit! : %s\n”, SDL_GetError());

SDL_UpdateRect(screen, 0, 0, 0, 0);

SDL_Delay(5000);

}

Oh yes. One more question.

Concerning the development of a Win32 version of SDL applications.

Do I need to get the Microsoft DirectX SDK and compile it under a Linux
environment. (assuming that I get the Win32 development
libraries…Where can I get those by the way?) or can I just have a
friend with MS Visual C++ download and compile SDL/ install it and then
I send him the source for my program and he compiles it with Visual C++
under Windows? Is the conceivable?

The reason I’m asking this question is because I noticed that you
included a PERL script that modifies parts of the DirectX SDK…and I
was curious if that would also be necessary if the development was done
under a Win32 environment…

Thanks,

Paul Lowe
xpaull at ultraviolet.org

[SDL list: hints on successfully display a bitmap + VC++ thoughts]

I have NO idea what-so-ever why this code doesn’t work
correctly.

It’s easy. Check the documentation… I’ll point out in your code.

What does that “SDL Warning: 1 SDL surfaces extant” mean?

It means you didn’t free the BMP surface you loaded.

screen = SDL_SetVideoMode(bitmap->w + 5, bitmap->h + 5, 8, SDL_SWSURFACE);

This won’t work on DirectX or DGA displays. You should choose a common
video mode or find an available video mode with SDL_GetVideoMode().

if(SDL_MapSurface(bitmap, screen->format) == 0)
printf(“Unable to map surface! : %s\n”, SDL_GetError());

This should be:
if(SDL_MapSurface(bitmap, screen->format) < 0)
printf(“Unable to map surface! : %s\n”, SDL_GetError());

if(SDL_BlitSurface(bitmap, NULL, screen, NULL) == 0)
printf(“Unable to perform blit! : %s\n”, SDL_GetError());

This should be:
if(SDL_BlitSurface(bitmap, NULL, screen, NULL) < 0)
printf(“Unable to perform blit! : %s\n”, SDL_GetError());

You are in effect asking the blit function to blit the entire bitmap surface
to the rectangle formed by the entire display surface. Since they are not
the same size, and stretching is not supported, this fails.

Do I need to get the Microsoft DirectX SDK and compile it under a Linux
environment.

No. You can use the precompiled SDL library binaries, or you can build
the GDI version (WinDIB) of the SDL library without the DirectX headers.
In no case do you need the entire DirectX SDK. All you need are the
headers, processed a bit so GCC can compile them. See directx/README
for more information.

I send him the source for my program and he compiles it with Visual C++
under Windows? Is the conceivable?

This currently doesn’t work, though if anyone has access to Visual C++ and
can make it work… please do!

SDL is designed, at the moment, to be compiled using the mingw32 gcc
cross-compiler for Linux. I recently got a Windows NT native version
of the compiler working, and will announce it soon.

The reason I’m asking this question is because I noticed that you
included a PERL script that modifies parts of the DirectX SDK…and I
was curious if that would also be necessary if the development was done
under a Win32 environment…

No, it wouldn’t.

Again, I’d really like to get Visual C++ working with SDL, but I don’t
have a copy at the moment.

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/