Linux Game Programming SDL blit example problem

Hello everyone. I know the book says it is for linux programming but since
SDL is a cross platform API I figured most of the examples would work with
little adjustment. Well on the first bliting example I have a problem. First
I am using, WinXP, MSVC-6, SDL 1.2.4. Here is the code:

// Example of simple blitting with SDL

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

int main( int argc, char* argv[] ) // added the (int argc, char* argv[]) so
it would compile on Win32
{
SDL_Surface *screen;
SDL_Surface *image;
SDL_Rect src, dest;

// Initialize SDL's video system and check for errors.
if (SDL_Init(SDL_INIT_VIDEO) != 0 ) {
	printf("Unable to initialize SDL: %s\n", SDL_GetError());
	return 1;
}


// Make sure SDL_Quit gets called when the program exits
atexit(SDL_Quit);

// Attempt to set a 256 x 256 hicolor (16-bit) video mode. 
// Since that is rarely a valid mode SDL with emulate
screen = SDL_SetVideoMode(256, 256, 16, 0);
if (screen == NULL) {
	printf("Unable to set video mode: %s\n", SDL_GetError());
	return 1;
}


// Load the bitmap file. SDL_LoadBMP returns a pointer to a
// new surface containing the loaded BMP
image = SDL_LoadBMP("test.bmp");
if (image == NULL) {
	printf("Unable to load bitmap.\n");
	return 1;
}

// The SDL blitting function needs to know how much data to copy. 
// We provide this with SDL_Rect structures, which
// define the source and destination rectangles. The areas
// should be the same; SDL does not currntly handle image
// stretching.
src.x = 0;
src.y = 0;
src.w = image->w;
src.y = image->h;

dest.x = 0;
dest.y = 0;
dest.w = image->w;
dest.h = image->h;

// draw the bitmap to the screen. We are using a hicolor video
// mode, so we don't have to worry about colormap silliness.
// It is not necessary to lock surfaces before blitting; SDL
// will handle that.
SDL_BlitSurface(image, &src, screen, &dest);
		
// Ask SDL to update the entire screen.
SDL_UpdateRect(screen, 0,0,0,0);

// Pause for a few seconds
SDL_Delay(3000);

// Free the memory that was allocated to the bitmap
SDL_FreeSurface(image);

return 0;

}

Ok, the problem I have is this; the program runs without errors (initializes
the video system and loads the bmp). The screen is displayed in a 256 x 256
window but the bmp image does not display. I am using a 256 x 256 bmp image
created with GIMP. I am stumped as to why it wouldn’t be displaying. Any
suggestions out there are welcome. Thanks.

Steven Bradley
"Now that I have wasted this much of your time, you will have to code faster
to catch up!"

AIM3 BRADLEY wrote:

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

try changing this to:

SDL_Flip(screen);

if that works, then apparently the screen is double buffered…which can happen, even when you don’t ask for it.–
-==-
Jon Atkins
http://jonatkins.org/

Thanks for the reply. I change the SDL_UpdateRect(screen, 0,0,0,0) to
SDL_Flip(screen); but no change.

Steven Bradley> -----Original Message-----

From: Jonathan Atkins [SMTP:jcatki at jcatki.no-ip.org]
Sent: Tuesday, October 29, 2002 4:45 AM
To: sdl at libsdl.org
Subject: Re: [SDL] Linux Game Programming SDL blit example problem

AIM3 BRADLEY wrote:

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

try changing this to:

SDL_Flip(screen);

if that works, then apparently the screen is double buffered…which can
happen, even when you don’t ask for it.


-==-
Jon Atkins
http://jonatkins.org/


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Had a quick scan, this section looks wrong:

src.x = 0;
src.y = 0;
src.w = image->w;
src.y = image->h;

Where src.y is set twice. The last line should read

src.h = image->h;__________________________________________________
Do you Yahoo!?
HotJobs - Search new jobs daily now
http://hotjobs.yahoo.com/

Hey Nathan, thanks. I am a dolt, walked right over that. It works fine now.
Thanks for the catch.

Steven

I just wanted to point out that you can download a tarball of all the
examples in the book. The source code here is fully functional.

http://www.nostarchpress.com/plg-listings-08-07-2001.tar.gz

Peace,
=Pete=

Thanks for the info but I get more from it by doing it the hard way :slight_smile:

Steven> I just wanted to point out that you can download a tarball of all the

examples in the book. The source code here is fully functional.

http://www.nostarchpress.com/plg-listings-08-07-2001.tar.gz

Peace,
=Pete=


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl