Hello again,
is there a way to display a pSDL_SURFACE on a form? Basically I am trying to build a level editor and since I already have a lot of code using SDL I wish to use that in combination with the convience of the components and form designer.
So basically I have been messing around using the below code. Basically I am looping through each pixel and trying to set the pixel of the TCanvas of the form. Now, I have two problems with this:
- It is extremely slow when resizing the form.
- It doesn't work, see attachment. The surface should be a black background with a red rectangle but instead displays as a collection of red, green and blue pixels.
Code:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, SDL, Unit2;
type
{ TForm1 }
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormPaint(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
MapDisplay: pSDL_SURFACE;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
var
Rect: pSDL_RECT;
begin
SDL_INIT(SDL_INIT_VIDEO);
MapDisplay:= SDL.SDL_CreateRGBSurface(SDL_SWSURFACE, 499, 500, 32, 255, 255, 255, 0);
SDL_FillRect(MapDisplay, NIL, $000000);
New(Rect);
Rect^.x:= 5;
Rect^.y:= 100;
rect^.w:= 50;
Rect^.h:= 20;
SDL_FillRect(MapDisplay, NIL, $00FF00);
Dispose(Rect);
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
SDL_FreeSurface(MapDisplay);
end;
// here I have the actual rendering code.
procedure TForm1.FormPaint(Sender: TObject);
var
I, J: integer;
Loc: ^LongWord;
begin
For I:= 0 To MapDisplay^.w -1 Do
For J:= 0 To MapDisplay^.h -1 Do
begin
Loc:= MapDisplay^.pixels + I + J*MapDisplay^.w;
Canvas.Pixels[I,J]:= Loc^;
end;
end;
end.
So I am kinda lost, and would like to know what I am doing wrong and even if there is a better more efficient way of doing this.
Thanks.
Example.jpg
Bookmarks