PDA

View Full Version : Jedi SDL - how to start?



Loodziek
05-06-2007, 02:56 PM
Hi all. I'm wanna make my game. I know basics of Omega components but they are not supported for Linux. So I trying to start with SDL library. And I have a "little" problem. If found THIS (http://lazyfoo.net/SDL_tutorials/index.php) tutorial, but I don't have ANY basics for C. I have a friends who programme in this language but it's stupid ask for everything friends. Maybe someone know where I can find any tutorials, arts or other helping things to start learn SDL? And I'm sorry for my weak english :/ Thanks anyway...

jasonf
05-06-2007, 03:29 PM
What sort of game are you thinking of writing?

2D or 3D?

Both will use SDL to create the main window, but 3D uses OpenGL from that point on.

2D with SDL is really easy once you've got the hang of it. I'm sure there are many tutorials available. The good news is that the C examples can be very useful for showing you the calls you need to make. The techniques are interchangable.

In the JEDI-SDL package, there are lots of examples which show you how SDL can be used. They cover Initialising the subsystems, graphics, input, sound, music.. amongst others.

It won't take long to get into it.

At the moment everything I do uses SDL.. but I created wrapper classes long ago to hide away all of the low level calls.

I was going to do a SDL tutorial a long time ago, but I never got around to it.

savage
05-06-2007, 03:29 PM
Have a look in our articles section ( http://www.pascalgamedevelopment.com/articles.php ) and look at the article by WILL which is an introduction to using JEDI-SDL. You may also want to have a look at http://jedi-sdl.pascalgamedevelopment.com for documentation, though this is included in the latest release found on that page.

Loodziek
05-06-2007, 07:40 PM
Both will use SDL to create the main window, but 3D uses OpenGL from that point on.

I know, I know. I wanna make game with 2D graphic and possible 3D addons like explosions or fire. I'ill read documenation and examples. Thanks again guys.

WILL
05-06-2007, 08:16 PM
Well the good news is that 2D is super easy with OpenGL. Plus it'll take full advantage of your 3D hardware. And later mixing 3D and 2D would be a breeze.

Loodziek
20-08-2007, 06:43 PM
Hi again guys. In this time I know basics of using SDL, SDL_Image and SDL_TFF. But it's still only basics. So I have some questions to you:
:arrow: I know how to draw image, set it position. But how can I set an transparent color for image? You know, I mean when I have for eg. #ff00ff background color I want to set this color as Alpha.
:arrow: In Lazarus, Delphi we put Timer and we can timing. In Omega we have an OmegaTimer. But how can I set timer in JEDI-SDL?
:arrow: And more, but not everything suddenly ;)

Thanks for any responds.

And I again sorry for my weak English, tell me where I make mistakes, I will try to don't do it in future...

arthurprs
20-08-2007, 07:25 PM
Hi again guys. In this time I know basics of using SDL, SDL_Image and SDL_TFF. But it's still only basics. So I have some questions to you:
:arrow: I know how to draw image, set it position. But how can I set an transparent color for image? You know, I mean when I have for eg. #ff00ff background color I want to set this color as Alpha.
:arrow: In Lazarus, Delphi we put Timer and we can timing. In Omega we have an OmegaTimer. But how can I set timer in JEDI-SDL?
:arrow: And more, but not everything suddenly ;)

Thanks for any responds.

And I again sorry for my weak English, tell me where I make mistakes, I will try to don't do it in future...

*1-
SDL_SetColorKey(surface, // surface is the surface that want to set a transparent color
SDL_RLEACCEL or SDL_SRCCOLORKEY,
SDL_MapRGB(surface.format, r, g, b)); // this color will be transparent


*2-
I don't know i use a few lines of code and a SDL_delay to do it, but in (VRY SLOW) pcs the game will run at diferent speed ;/


sorry me weak english too ;p
im from brazil and you ?

WILL
21-08-2007, 12:33 AM
Hey Loodziek, glad to hear you're progressing with SDL. :)


:arrow: I know how to draw image, set it position. But how can I set an transparent color for image? You know, I mean when I have for eg. #ff00ff background color I want to set this color as Alpha.

Ah, this one is quite easy, you just have to set the transparent color for the surface you wish to draw, as arthurprs says.

Here is a ]SDL_SetColorKey function[/url]. The notation assumes you are using a C-type language, but it's the same thing in all other languages.


:arrow: In Lazarus, Delphi we put Timer and we can timing. In Omega we have an OmegaTimer. But how can I set timer in JEDI-SDL?

There are 2 ways you can create your game.

:arrow: Threaded -- using a triggered event for your main code-block and others for drawing, networking, etc... Not the easiest way to do it at the beginning.

:arrow: Non-threaded -- using a simple 'main loop' where you'll only exit this loop when the game is done or you wish to close it.

Both ways you should build a 'game mode/state' system into your game code so that it will become easier to change from different areas within your game. ie. Game Menu, in-game action, paused game, credits screen, level loading, initial startup, etc...


:arrow: And more, but not everything suddenly ;)

Feel free to ask! :) SDL is great as it handles so much. It's pretty much the only other library besides DirectX to support all the features you'd want in your game from music to graphics to input controls and so on.

technomage
21-08-2007, 08:32 AM
Hi Loodziek

Issue one of the Delphi Gamer magazine has a Tutorial on S2DL which is a simple wrapper library for SDL. You can get the library at

http://www.cairnsgames.co.za/files/s2dl.zip

the magazine is available at

http://mag.delphigamer.com

Hope this helps

Dean

Loodziek
21-08-2007, 06:34 PM
@technomage
Yep, I read this issue when it's only appear, but I want test pure SDL power ;) But thanks for respond.

@arthurprs
Am from Poland, Arthur. I have a question. When I try to compile your source:
SDL_SetColorKey(surface, // surface is the surface that want to set a transparent color
SDL_RLEACCEL or SDL_SRCCOLORKEY,
SDL_MapRGB(surface.format, r, g, b)); // this color will be transparent

It's ok when I compile it in Delphi 7, but when I try to compile it in Lazarus it's isn't works:

Error image (http://img249.imageshack.us/img249/8740/crashcq0.jpg)

What's going on?

@WILL
I'ill try ;)

Thanks a lot again.

arthurprs
22-08-2007, 03:09 AM
@technomage
Yep, I read this issue when it's only appear, but I want test pure SDL power ;) But thanks for respond.

@arthurprs
Am from Poland, Arthur. I have a question. When I try to compile your source:
SDL_SetColorKey(surface, // surface is the surface that want to set a transparent color
SDL_RLEACCEL or SDL_SRCCOLORKEY,
SDL_MapRGB(surface.format, r, g, b)); // this color will be transparent

It's ok when I compile it in Delphi 7, but when I try to compile it in Lazarus it's isn't works:

Error image (http://img249.imageshack.us/img249/8740/crashcq0.jpg)

What's going on?

@WILL
I'ill try ;)

Thanks a lot again.

:? No ideia, i don't see anything wrong at this line

WILL
22-08-2007, 02:27 PM
What version of JEDI-SDL do you have? And, you are using the JEDI-SDL headers right?

Loodziek
22-08-2007, 02:59 PM
1.0 Beta I think. Just try compile this code in Delphi, then compile it in Lazarus:


program Test;
uses SDL;

var
surface : PSDL_Surface;

begin

SDL_SetColorKey(surface, SDL_RLEACCEL or SDL_SRCCOLORKEY, SDL_MapRGB(surface.format, 255, 0, 255));

end.


Runtime error doesn't matter.


-Edit-

Ok, I read some sources and I find where be an error. Well it should be "surface^.format" not "surface.format". Now everything should works fine ;)

Loodziek
31-08-2007, 06:25 PM
Hi again. When I know basics of SDL it's time to try write something. But, at start I find a first problem. I wanna load map from file to dynamic table. So first I must do SetLength, right? I write a dwo functions to get X and Y. Here is my map file (data\maps\map1.txt):


1 1 1 2 1 1 1 1 1
2 2 2 2 2 2 2 2 1
1 1 2 1 1 1 1 2 1
1 1 2 1 1 1 1 2 1
1 1 2 2 2 2 2 2 1
1 1 1 1 1 1 1 2 1


There are two sprites. Map X is a spaces count in one line + 1, right? So there's my funciton:

function GetMapX : integer;
var
count : integer;
StrChain : string;
MapFile : TextFile;
begin
AssignFile(MapFile, MapPath); //Map path is a global cons 'data\maps\map1.txt'
Reset(MapFile);
Read(MapFile, StrChain);
Result := 0;
for count := 0 to Length(StrChain) do
if StrChain[count] = ' ' then
Result := Result + 1;
Result := Result + 1;
CloseFile(MapFile);
end;


I hope it's clear for u. This function is in "Maps.pas". And there's my project file. Everything works fine:

program SDL_proby;

{$mode objfpc}{$H+}

uses
SDL, SDL_Image, ImagesDraw, SDL_TTF, FontsDraw, Maps, Player;

const

//Wymiary okna
width = 900;
height = 900;

var
screen : PSDL_Surface;
grass : PSDL_Surface;

logo : PSDL_Surface;
road_horizontal : PSDL_Surface;

pos : TSDL_Rect;

i : integer;

loop : boolean;

procedure Init;
begin
SDL.SDL_Init(SDL_Init_Video);
screen := SDL_SetVideoMode(width, height, 32, SDL.SDL_SWSurface);
TTF_Init;
loop := true;
end;

procedure OnQuit;
begin
SDL.SDL_FreeSurface(grass);
SDL.SDL_FreeSurface(screen);
TTF_Quit;
SDL.SDL_Quit;
end;

procedure doGameInput;
var
KeyPress : TSDL_Event;
begin
while (SDL.SDL_PollEvent(@KeyPress) > 0) do
case (KeyPress.Type_) of SDL.SDL_Keydown :
if (KeyPress.Key.keysym.sym = SDL.SDLK_ESCAPE) then loop := false;
end;
end;


//*************************************************
//************ KOD G¬£?ìWNY PROGRAMU*****************
//*************************************************

begin
Init;
for i := 0 to 5 do ImagesDraw.DrawImage('C:\Documents and Settings\WINDOWS XP\Moje dokumenty\Moje obrazy\grass.jpg', i * 64, 0, 64, 64, screen);
for i := 0 to 5 do ImagesDraw.DrawImage('C:\Documents and Settings\WINDOWS XP\Moje dokumenty\Moje obrazy\road.jpg', i * 64, 128, 64, 64, screen);
ImagesDraw.DrawImage('C:\Documents and Settings\WINDOWS XP\Moje dokumenty\Moje obrazy\beczka.bmp', 0, 0, 64, 64, screen);

repeat
SDL.SDL_Delay(30);
doGameInput;
SDL.SDL_Flip(screen);
until loop = false;

OnQuit;
end.


I know, I know there's a not used vars and it's not optimized yet. Anyway, it works. But when I put "mx : integer" to var and put "mx := GetMapX" to code it's compiling but I get only black screen. Why it don't work? When I compile my GetMapX function in Delphi and do:

mx := GetMapX;
ShowMessage(IntToStr(mx));


Then I get 9 (it works). I'm trying, and tying but after some tryies I must restar coputer because something is wrong with exe file... So it's very bored :( And sorry for double posting - I just want to set topic into "New Posts" state.

arthurprs
01-09-2007, 12:21 AM
Yeah you get the function working but you drawed the map ?

I use Tstringlist in my bitmapfont unit, but in your case i think its not suitable, i don't have sure but TTF are veryyy slow on asphyre {that is lot faster than SDL} compared to prerendered fonts so i would suggest you to use bitmap fonts instead.

About the exe problem i don't know :?

Loodziek
01-09-2007, 07:47 AM
Yes, I don't draw map yet. I just want to check my function and prgram crashes. And I don't know why :/ What bitmapfonts and TTF have to mine problem? :think:

arthurprs
01-09-2007, 02:00 PM
Yes, I don't draw map yet. I just want to check my function and prgram crashes. And I don't know why :/ What bitmapfonts and TTF have to mine problem? :think:

If you don't nothing the screen will be black,

*no its only a suggestion for better speed.

Loodziek
01-09-2007, 03:50 PM
Arthur, look to my code again. There is a procedure "DrawImage" ;) I draw 4 grass sprites, 4 road sprites and one barrel :) But when I just put my function to file program don't render anything and it cannot be closed (I must use Crtl + Alt + Delete). I found a line where's program crash:
Read(MapFile, StrChain);
When I write ReadLn theres a the same problem. What do u think?

arthurprs
01-09-2007, 04:26 PM
Arthur, look to my code again. There is a procedure "DrawImage" ;) I draw 4 grass sprites, 4 road sprites and one barrel :) But when I just put my function to file program don't render anything and it cannot be closed (I must use Crtl + Alt + Delete). I found a line where's program crash:
Read(MapFile, StrChain);
When I write ReadLn theres a the same problem. What do u think?

Make use of SDL_Blitsurface insted..

So it crashes while reading the file ?

Loodziek
01-09-2007, 06:33 PM
My DrawImage is a IMG_Load, BlitSurface and others :)

And yes, program crashes when I try to read file. Is it possible to use TextFiles with JEDI-SDL?

arthurprs
01-09-2007, 07:57 PM
My DrawImage is a IMG_Load, BlitSurface and others :)

And yes, program crashes when I try to read file. Is it possible to use TextFiles with JEDI-SDL?

Ohhh everytime you draw something it loads -> draw -> free it :shock: :?:
Don't make this way!

Don't see any problems in your file read code :?