SDLDraw
This unit mainly consist of classes that helps working with surfaces. But let's go over helper functions first.
Helper fuctions
- function LightColor(Color: Cardinal;Value: Integer): Cardinal;
- This function first calculate lightness of given Color in range 0-255 and than adds Value to that lightness.
It makes sure result is in range 0-255 and returns it.
- function HueColorTo(Color,ToColor: Cardinal): Cardinal;
- This function moves hue of Color to match hue of ToColor. This is how you get same shade of ToColor but with
lightness and saturaion of Color.
- function RGBtoHSL(Color: Cardinal): Cardinal;
- You give this function Color in format $RRGGBB (Red,Green,Blue) and it returns it in format $HHSSLL
(Hue, Saturation, Lightness).
- function HSLtoRGB(Color: Cardinal): Cardinal;
- You give this function Color in format $HHSSLL (Hue, Saturation, Lightness) and it returns it in format
$RRGGBB (Red,Green,Blue).
- procedure DefaultInitGL;
- This procedure sets viewport to entire main surface, projection matrix to Ortho, ClearColor to 0,
ClearDepth to 1, BlendFunc to (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) and enabled blending.
- function SDLPoint(AX,AY: Integer): TPoint;
- Simply returns TPoint with X=AX and Y=AY.
- procedure InflateRect(var Rect: TSDL_Rect;dx,dy: Integer);
- This procedure increases or decreases the specified rectangle.
- procedure OffsetRect(var Rect: TSDL_Rect;dx,dy: Integer);
- This procedure moves the specified rectangle by dx pixels horizontaly and dy pixels verticaly.
- function PointInRect(const Point: TPoint;const Rect: TSDL_Rect): Boolean;
- Returns True if Point is inside Rect.
- function RectInRect(const Rect1, Rect2: TSDL_Rect): Boolean;
- Returns True if Rect1 is in Rect2.
- function OverlapRect(const Rect1, Rect2: TSDL_Rect): Boolean;
- Returns True if Rect1 and Rect2 overlaps.
- procedure WriteString(Stream: TStream;const Value: String);
- Writes length of Value and then value it self in the Stream. I put this here becouse I needed it everywhere.
- procedure ReadString(Stream: TStream;var Value: String);
- Reads length of Value and then value it self from the Stream. I put this here becouse I needed it everywhere.
SDLScreen global variable
No matter if you create TSDLScreen or TSDLGLScreen you should assign it to this variable so every other
unit can also use main surface.
TSDLScreen and TSDLGLScreen
TSDLScreen represents screen surface. The first thing you create in SDL application is this. TSDLGLScreen you create
if you want to use OpenGL with SDL. It has the same properties and methods but some of them are overriden to give OpenGL
functionality. Properties are:
- property ClipRect: TSDL_Rect read FClipRect;
- Returns current clip rect of main surface.
- property FPS: Integer read FFPS;
- Returns Frames Per Second.
- property OpenGLScreen: Boolean read FOpenGLScreen;
- Returns True if this is TSDLGLScreen.
- property SDLImageClass: TSDLImageClass read FSDLImageClass;
- This returns TSDLImage if this is TSDLScreen and TSDLGLImage if this is TSDLGLScreen.
- property Surface: PSDL_Surface read FSurface;
- Returns pointer to main surface.
- property SurfaceRect: TSDL_Rect read FSurfaceRect;
- Returns main surface rect: x=0, y=0, w=Surface.w, h=Surface.h.
When you call methods which has surface as a parameter if you set that parameter to nil it will be the same
as you set it to main surface if you are not in OpenGL mode. Methods are:
- constructor Create(const Caption: string;W,H,Bpp: Integer; FS: Boolean); virtual;
- Creates main surface with height = H, width = W and BitsPerPixel = Bpp. if FS is True surface is fullscreen,
else it is in a window with caption = Caption. Flags for creating are SDL_HWSURFACE and SDL_DOUBLEBUF.
- procedure Flip; virtual;
- Flips main surface to the screen.
- procedure Clear(ASurface: PSDL_Surface;Color: Cardinal); virtual;
- Clears the specified surface to Color.
- procedure SetClipRect(ASurface: PSDL_Surface; rect: PSDL_Rect); virtual;
- Sets clip region to rect on specified surface.
- function MapCardinal(Format : PSDL_PixelFormat;Color: Cardinal): Cardinal; virtual;
- Returns color in specifed format from color in $AARRGGBB (A-Alpha) format.
- function GetRGBAPixel(ASurface: Pointer;X,Y: Integer): Cardinal; virtual;
- Returns color of pixel X,Y from specified surface in $AARRGGB format.
- procedure Colorize(ASurface: PSDL_Surface; Value: Cardinal);
- Colorize the specified surcface maintaining every pixel's lightness.
- procedure ModifyHueFor(ASurface: PSDL_Surface; Value: Integer);
- Moves the color of every pixel in the specified surface in spectar from 0 to 255 for the specified value.
- procedure ModifyHueTo(ASurface: PSDL_Surface; ToColor: Cardinal);
- Moves the color of every pixel in the specified surface in spectar from 0 to 255 to match the position of ToColor
in spectar.
- procedure DrawOrthoLine(S: PSDL_Surface;Horiz: Boolean; x,y: Integer;Length,C: Cardinal); virtual;
- if Horiz is True draws horizontal, and if it is false draws vertical line on the specified surface
from x,y with length=Length and color = C. It also clips the line if it is outside surface's clip region.
- procedure FillRect(S: PSDL_Surface;const Rect: TSDL_Rect;C: Cardinal); virtual;
- Fills the specified rect of the specified surface with color C.
- procedure DrawFrame(S: PSDL_Surface;const Rect: TSDL_Rect;C: Cardinal); virtual;
- Draws frame of color C on the specified surface.
- procedure Draw3DControl(S:PSDL_Surface;const Rect: TSDL_Rect;C: Cardinal; BorderWidth: Integer;Down: Boolean=False); virtual;
- Draws 3D control on the specified surface. It first fills the Rect with color C then it takes
a bit lighter and a bit darker color then C. if Down is True it draws left and top border in light color
and right and bottom border in dark color. if Down is false those colors exchange their places. Border's width is given
with BorderWidth parameter.
- procedure DrawArrow(S: PSDL_Surface;Dir: TArrowDirection; const Rect: TSDL_Rect;C: Cardinal); virtual;
- Draws a triangle in the specified rect of the specified surface with color = C and direction given by Dir parameter
which can be adUp,adLeft,adDown or adRight.
TSDLImage and TSDLGLImage
This classes represents one sdl surface and has methods for its drawing. TSDLGLImage inherits from TSDLImage but
has overriden methods and some new properties. Properties are:
- property Alpha: Byte read FAlpha write SetAlpha;
- This property represents how much is the entire surface transparent. 0 means totaly trnasparent and
255 means totaly opaque.
- property Height: Integer read GetHeight;
- Returns Height of an image but as a hole multiple of PatternHeight. if PatternHeight is 0 then it returns
surface's height. If you need surface's height you should use Surface.h property.
- property Name: string read FName write FName;
- Name of an image in TSDLImages - image list.
- property PatternCount: Integer read GetPatternCount;
- if PatternWidth and PatternHeight are greater then 0 returns noumber of patterns, else it returns 0.
- property PatternHeight: Integer read GetPatternHeight write FPatternHeight;
- Represents Height of one pattern.
- property PatternRects[Index: Integer]: TSDL_Rect read GetPatternRect;
- Returns rect of pattern with the specified index where patterns are counted from left to right and from
top to bottom.
- property PatternWidth: Integer read GetPatternWidth write FPatternWidth;
- Represents Width of one pattern.
- property Surface: PSDL_Surface read FSurface write SetSurface;
- Returns Surface of this image.
- property Transparent: Boolean read FTransparent write SetTransparent;
- Set this to True if your image has a color you want to be transparent.
- property TransparentColor: Cardinal read FTransparentColor write SetTransparentColor;
- Represents color which will be considered transparent while drawing the image.
- property UseAlphaCh: Boolean read FUseAlphaCh write SetUseAlphaCh;
- Set this to True if your image has an alpha channel which you want to use.
- property Width: Integer read GetWidth;
- Returns Width of an image but as a hole multiple of PatternWidth. if PatternWidth is 0 then it returns
surface's width. If you need surface's width you should use Surface.w property.
TSDLGLImage also has:
- property MinFilter : GLint read FMinFilter write SetMinFilter;
- Can be GL_NEAREST, GL_LINEAR, GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR,
GL_LINEAR_MIPMAP_LINEAR. Default is GL_LINEAR.
- property MagFilter : GLint read FMagFilter write SetMagFilter;
- Can be GL_NEAREST, GL_LINEAR. Default is GL_LINEAR.
- property TexWrapS : GLint read FTexWrapS write SetTexWrapS;
- Can be GL_CLAMP, GL_REPEAT. Default is GL_REPEAT.
- property TexWrapT : GLint read FTexWrapT write SetTexWrapT;
- Can be GL_CLAMP, GL_REPEAT. Default is GL_REPEAT.
- TextureID : GLuint;
- Represents OpenGL texture ID.
TSDLImages
TSDLImages is an image collection. You should create it with SDLScreen.SDLImageClass as a parameter. It has one property:
- property Items[Index: Integer]: TSDLImage read GetItem write SetItem; default;
- Represents and item with the specified index.
And a few methods:
- function ByName(const AName: string): TSDLImage;
- Returns an image with the specified name.
- function Add(const AName: string): TSDLImage;
- Creates a new image with the specified name. Adds image type of main surface - TSDLImage or TSDLGLImage.
- function Insert(Index: Integer): TSDLImage;
- Creates a new image at the specified position.
- function IndexOfItem(const AName: string): Integer;
- Returns Index of image with the specified name.
- function LoadFromSil(const FileName: string): Integer;
- Loads an image list from sil - Sdl Image List file. It returns ESILERROR = -1 on unknown error,
ESILUNKNOWNFORMAT = -2 if format is not supported, ESILUNSUPPORTEDVER = -3 if version is not supported,
ESILCOMPRESSED = 1 if it is compressed and 0 if everything went well and it is not compressed.