PDA

View Full Version : Help with SDL.



Nevyn
16-07-2006, 05:48 PM
Hi, Im currently trying to learn the Delphi headers for SDL.
Im not that good with general delphi programming allthough, I know enough i'd say. Now here is the thing. Im wondering if its possible to create a particle flow without using opengl? Sure there is no problem using opengl and if you know how implant it into the code im gonna post underneath i'd appriciate it.






uses SDL, SDL_Image, SDL_Mixer, smpeg, SysUtils;

Var screen,image,sprite: PSDL_surface;
rect1 : TSDL_rect;
event: TSDL_Event;
keys: PKeyStateArr;
music: PMix_Music;
//Game Variable Settings
title: PAnsiChar;
Resolution_x: integer;
Resolution_y: integer;
song: string;
color_depth : integer;
background: string;
Character: string;
//End of the game variable section
//Section for Video and Sound
begin
//Checks if everything works and if it does not, then it exits.
if ((SDL_Init(SDL_INIT_VIDEO OR SDL_INIT_AUDIO)=-1)) then
begin
exit;
end;
//Sets the sound channel quality.
if &#40;Mix_OpenAudio&#40;44100, AUDIO_U8, 1, 4048&#41; < 0&#41; then // Loads the sound channel
begin
exit;
end;
//Check end, If it reaches here, everything is good
//Loads our Game variable settings.
title&#58;=&#40;'Kwaoeoaooewo'&#41;;
Resolution_x &#58;= &#40;800&#41;;
Resolution_y &#58;= &#40;600&#41;;
color_depth &#58;= &#40;32&#41;;
song &#58;= &#40;'cerror-nobodandi.xm'&#41;;
background &#58;= &#40;'Background.jpg'&#41;;
Character &#58;= &#40;'Sprite.bmp'&#41;;
//End of loaded game variable settings.
//Window title section..
WriteLn&#40;'/////// Loading Window Params \\\\\\\'&#41;;
SDL_WM_SetCaption&#40;title,nil&#41;; //Titeln p?• f??nstret
WriteLn&#40;'Title set to '+title&#41;;
////Sets the resolution mode on the window.
screen&#58;=SDL_SetVideoMode&#40;Resolution_x,Resolution_y ,color_depth,SDL_HWSURFACE&#41;;
WriteLn&#40;'Resolution&#58; '+IntToStr&#40;Resolution_x&#41;+' x '+IntToStr&#40;Resolution_y&#41;+' set'&#41;;
//Load Images
image&#58;=IMG_Load&#40;PCHAR&#40;background&#41;&#41;; //Loads the Background
WriteLn&#40;'Image&#58; '+Background+' Loaded'&#41;;
sprite&#58;=IMG_Load&#40;PCHAR&#40;Character&#41;&#41;; //Loads the Character
WriteLn&#40;'Image&#58; '+Character+' Loaded'&#41;;
sprite&#58;=SDL_DisplayFormat&#40;sprite&#41;;
SDL_BlitSurface&#40;image,nil,screen, nil&#41;; //Puts out the background onto the window.
rect1.x&#58;=700;
rect1.y&#58;=500;
rect1.w&#58;=400;
rect1.h&#58;=240;
SDL_SetColorKey&#40;sprite,SDL_SRCCOLORKEY OR SDL_RLEACCEL,SDL_MapRGB&#40;sprite.format,255,255,255&#41; &#41;;
SDL_BlitSurface&#40;sprite,nil,screen,@rect1&#41;; //Loads the character onto the screen
SDL_UpdateRect&#40;screen,0,0,0,0&#41;; //Updates the character sprite
SDL_ShowCursor&#40;1&#41;; //Show mouse cursor 1=ja 0=nej
//Audio section
WriteLn&#40;'//////// Initializing Audio \\\\\\\\\'&#41;;
WriteLn&#40;'Loading song&#58; '+song&#41;;
music &#58;= Mix_LoadMUS&#40; PChar&#40;song&#41; &#41;; //Song to play.
WriteLn&#40;'Song loaded...'&#41;;
WriteLn&#40;'Song playing&#58; '+song&#41;;
WriteLn&#40;'//////// Section Audio end \\\\\\\\\\'&#41;;
Repeat
while &#40;SDL_PollEvent&#40;@Event&#41; > 0&#41; do
begin
case Event.type_ of
SDL_QuitEv&#58;
begin
Exit;
end;
end;
end;
keys&#58;=PKeyStateArr&#40;SDL_GetKeyState&#40;nil&#41;&#41;;
if &#40;keys&#91;SDLK_UP&#93;=SDL_PRESSED&#41; and &#40;rect1.y>0&#41; then //Move the character up
begin
SDL_BlitSurface&#40;image,@rect1,screen, @rect1&#41;; //Loads the character sprite onto the background
dec&#40;rect1.y&#41;;
SDL_BlitSurface&#40;sprite,nil,screen, @rect1&#41;; //Loads the character sprite again
SDL_UpdateRect&#40;screen,0,0,0,0&#41;; //update the character.
end;
if &#40;keys&#91;SDLK_DOWN&#93;=SDL_PRESSED&#41; and &#40;rect1.y<580&#41; then //Move the character down
begin
SDL_BlitSurface&#40;image,@rect1,screen,@rect1&#41;; //Loads the character
inc&#40;rect1.y&#41;;
SDL_BlitSurface&#40;sprite,nil,screen,@rect1&#41;; //Loads the character sprite again
SDL_UpdateRect&#40;screen,0,0,0,0&#41;; //update the character.
end;
if &#40;keys&#91;SDLK_LEFT&#93;=SDL_PRESSED&#41; and &#40;rect1.x>0&#41; then //Flytta gubbe n?•t v?§nster
begin
SDL_BlitSurface&#40;image,@rect1,screen,@rect1&#41;; //Laddar om gubben
dec&#40;rect1.x&#41;;
SDL_BlitSurface&#40;sprite,nil,screen, @rect1&#41;; //Kopierar gubben
SDL_UpdateRect&#40;screen,0,0,0,0&#41;; //updaterar gubben
end;
if &#40;keys&#91;SDLK_RIGHT&#93;=SDL_PRESSED&#41; and &#40;rect1.x<780&#41; then //flyttar gubbe ?•t h??ger
begin
SDL_BlitSurface&#40;image,@rect1,screen, @rect1&#41;; //Laddar om gubben
inc&#40;rect1.x&#41;;
SDL_BlitSurface&#40;sprite,nil,screen,@rect1&#41;; //Kopierar gubben
SDL_UpdateRect&#40;screen,0,0,0,0&#41;; //updaterar gubben
end;
if &#40;not &#40;Mix_PlayingMusic=1&#41;&#41; then //If the music aint playing, play it again
begin
Mix_PlayMusic&#40;music,0&#41;;
end;
Until &#40;keys&#91;SDLK_SPACE&#93;=SDL_PRESSED&#41;; //If the spacekey is down then this will happend
Mix_CloseAudio&#40;&#41;; //Audio channel exits
SDL_ShowCursor&#40;1&#41;; //The Mouse curous is shown, if already isnt
SDL_Quit; //Application is ended.
end.


And sorry for the swedish you see on the char movement, I was to lazy to translate that too.
You might notice this is from the Delphi SDL example. And thats because it is :>

WILL
17-07-2006, 02:16 AM
Well as you probably already concluded, there are many ways do to this with just SDL.

Barring 3rd party article engine, (do any Pascal ones exist for SDL?) you can create a rather simple one from scratch.

You'll need to become familiar with alpha masking your texture data and your blit functions OR an alphadraw function like what is offered in sdlutils (JEDI-SDL specific). That is about it as far as SDL is concerned...

Provided you have the physics down, it shouldn't be a problem for you...

Creating an alpha mask is rather simple if you understand the concept of RGB pixel data. R, G and B are channels. A is the channel for Alpha. (or transparency) RGB channels control the color and A controls the transparency. All in hardware! :D (provided your card supports this)

Your data will normally either look like; [ARGB] or [RGBA]. Each letter shown being a byte out of the 32-bit texture pixel.

Great, now how do you alter this? Well SDL as you may have guessed has such functions... just look 'em up in the sdl header. :) (if you are using JEDI-SDL's headers then it's easy to find and all grouped with the graphics/video functions)

And how does this help me create particles? Easy... particles fade out after a specific time, right?

Now you want to make a texture to use for the particles. Well you can do this in a few different ways. Create a series of frames each with it's own animation frame and alpha level and place that into a texture OR just create one particle frame and change the alpha level in the texture each time you draw. The latter could be a bit cumbersome depending on the system CPU juice available, other things going on graphics-wise and how big your particles are. It's a balance you have to determin yourself. But with ever increasingly faster cards and processors, this may be a moot point soon enough.

Once you have just picked one of these and provided that you've set the alpha channels in your texture properly... it should be rather easy to setup the drawing portion of your physics engine.


Sorry, no code to show right now. Too much else going on. :?