PDA

View Full Version : Drawing a bitmap?



Stoney
03-07-2005, 10:15 AM
Hello,
I'm using JEDI-SDL in Freepascal with no problems. Initialisation is working, now I want to go a step further and draw bitmap on the screen. I've tried a bit, but it doesn't work. :(
When I was using the example of the documentation, there was a compiling error. The demos of JEDI-SDL are a little too complex for me as a JEDI-SDL-beginner.
Perhaps someone could help me and write a basic example how to load and display a bitmap.

savage
04-07-2005, 09:40 AM
Could you try this...

program HelloBitmap;

uses
sdl;

var
// Done flag
Done : Boolean;

// Event record
event : TSDL_Event;

// Our Screen Surface;
screen : PSDL_Surface;

// Our Background image
imgBackGround : PSDL_Surface;
begin
// Initialize SDL
if SDL_Init( SDL_INIT_VIDEO ) < 0 then
begin
// Display an error
Halt( 1 );
end;

// Set the Window Caption
SDL_WM_SetCaption( 'My first Bitmap', nil );

// Set the video Mode
screen := SDL_SetVideoMode( 800, 600, 16, SDL_DOUBLEBUF or SDL_ANYFORMAT );
if screen = nil then
begin
// Display an error
SDL_Quit;
Halt( 1 );
end;

// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
imgBackGround := SDL_LoadBMP( 'MyImage.bmp' );
if ( imgBackGround <> nil ) then
begin
// wait for user to press a key or click the X
Done := False;
while ( not Done ) do
begin
// This could go in a separate function
while ( SDL_PollEvent( @event ) = 1 ) do
begin
case event.type_ of
SDL_QUITEV :
begin
// handle click to close Window - User Clicks on X on title bar
Done := true;
end;

SDL_KEYDOWN :
begin
// handle key press - check for Escape key
if event.key.keysym.sym = SDLK_ESCAPE then
Done := true;
end;
end;
end;
// draw onto our back buffer
SDL_BlitSurface( imgBackGround, nil, screen, nil );

//Flip to our main surface
SDL_Flip( screen );
end;
end
else
begin
SDL_Quit;
// Display an error
Halt( 1 );
end;

// Free up any memory we may have used
if ( imgBackGround <> nil ) then
SDL_FreeSurface( imgBackGround );
SDL_Quit;
end.

Stoney
06-12-2005, 09:18 PM
Thanks, it works really well.
After practising more and more with SDL, I love and enjoy JEDI-SDL now. :D
Currently I'm working on an engine (and games based on this engine) with SDL and I have a new question: Is there any possibility to stretch a bitmap or an image?
I tried a bit with TSDL_Rects, but the image is drawn originally, without any stretching effects.

savage
06-12-2005, 10:31 PM
If you look within JEDI-SDL, you should find a Utility unit ( sdlutils.pas ) that supports stretching your bitmap and other things. There should be some demos showing off what the utilities can do.

SDL itself does not support this natively yet. I believe SDL will support this when the make the OpenGL back-end a permanent fixture.