Simple Delphi Graphics

I'm trying to write a simple space game in Delphi. Everything has gone fine so far until now. I'm trying to draw some very very simple graphics for when you shoot another ship. A laser beam effect kinda thing.

First I tried using a single line drawn onto the canvas from the image of your gun to the image of the ship you want to fire at. This works but looks really bad.

The next thing I thought of was to draw a triangle on the screen and have the base of the triangle at your gun and stretch the point to the ship you were firing at. To give some kind of perspective view of your laser.

Unfortunatly Delphi doesn't support drawing triangles (that I could find) and I'd have to use someone else's VCL. I tried this but the major problem here is that the triangle can't be an equilateral triangle as the ship won't always be directly in front of your gun (so the triangle won't line up)

The next method I thought of was to draw a very small bitmap like

http://asmod3.d2g.com/images/beam.bmp

and then draw that on screen. Again I'm not sure I'm going about it the correct way because I would like to have some kind of perspective view on the laser beam.

This has been bugging me for a while now.

Anyway have any idea how I could do this?

Thanks

LVL 1
Gareth_HastingsAsked:
Who is Participating?
 
CynnaConnect With a Mentor Commented:
Gareth,

Below is a pretty fast way to draw filled triangle the 'easy' way.
If you want more speed, things will get *much* more complicated.

Copy & Paste following function
-------------------------------------

procedure DrawTriangle(Point1,Point2,Point3: TPoint; LineColor, FillColor: TColor; Where: TCanvas);
var OldPenColor, OldBrushColor: TColor;
    OldPenWidth: Integer;
begin
  with Where do begin
       // Save original canvas colors
       OldPenColor:=Pen.Color;
       OldBrushColor:=Brush.Color;
       OldPenWidth:=Pen.Width;
       // Temporarily change to our colors:
       Pen.Color:=LineColor;
       Brush.Color:=FillColor;
       Pen.Width:=2;
       // Draw triangle using new colors
       Polygon([Point1,Point2,Point3]);
       // Restore original canvas colors:
       Pen.Color:=OldPenColor;
       Brush.Color:=OldBrushColor;
       Pen.Width:=OldPenWidth;
  end;
end;


Example (place Image and Button on the form):
----------------------------------------

procedure TForm1.Button1Click(Sender: TObject);
begin
  DrawTriangle(Point(1,100), Point(100,100), Point(20, 30), clBlue, clAqua, Image1.Canvas);
end;



COMMENTS:
------------

1. This is the 'general' function that illustrates the point. It can be trimmed and made a little faster, depending on the rest of your code.

2. 'Real' laser effect needs gradients and maybe even antialiasing, but this is a bit more complicated.
0
 
kretzschmarCommented:
well delphi is not a 3d graphics engine,
you've to code an engine byself, or use one

take a look to
go to http://www.lischke-online.de/Graphics.html
at the
OpenGL interface unit

maybe you fine something also at
www.torry.ru

there also some other 3d-graphics with delphi sites
available (links may follow)

good luck

meikl ;-)
0
 
Gareth_HastingsAuthor Commented:
an idea of the thing I'm trying to draw on screen

http://asmod3.d2g.com/images/laser2.jpg

without the gradient shading (or with if its not super hard)
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
kretzschmarCommented:
oops, saw not your last comment above,
all is possible, but this not with a few lines of code

take a look to the two links above
0
 
Slick812Commented:
hello Garth, here's something that's easy and looks sorta like a lazer

var
ColorAry: Array[0..2] of TColor;

ColorAry[0] := clRed;
ColorAry[1] := clYellow;
ColorAry[2] := clAqua;
PaintBox5.Canvas.Pen.Color := clWhite;
PaintBox5.Canvas.Pen.Width := 3;
PaintBox5.Canvas.MoveTo(StartPoint.x,StartPoint.y);
PaintBox5.Canvas.LineTo(EndPt.x,EndPt.y);

PaintBox5.Canvas.Pen.Width := 1;
for i := 0 to 2 do
begin
PaintBox5.Canvas.Pen.Color := ColorAry[i];
PaintBox5.Canvas.MoveTo(StartPoint.x,StartPoint.y);
PaintBox5.Canvas.LineTo(EndPt.x,EndPt.y);
Sleep(200)
end;

you might change the Pen colors or widths for effect, you can also do the Wide Pen in the for loop
0
 
freshman3kCommented:
Hello,

You know you can use DirectX or OpenGL with Delphi.

Here are some great links to get you started.

http://turbo.gamedev.net/delphix.asp
http://www.delphidev.com/data/Issue01/index.html
http://www.savagesoftware.com.au/DelphiGamer/links.php
http://www.delphigamer.com
0
 
freshman3kCommented:
P.S

Forgot to mention, try taking a look at DelphiX in one of those links.

http://turbo.gamedev.net/delphix.asp

also dont forget to check out the rest of this site,
http://turbo.gamedev.net

Goodluck.
0
 
aikimarkCommented:
I've also answered a Delphi question about making graphics methods faster.  You might find a solution (or three) at
http://www.experts-exchange.com/jsp/qShow.jsp?ta=delphi&qid=20260073
0
 
Slick812Commented:
there isn't a "Triangle" draw function, like Rectangle, but there is a Polygon Draw function, that can draw a triangle

var
ArryPoints: Array[0..3] of TPoint;

ArrowPoints[0].x := 10;
  ArrowPoints[0].y := 10;
ArrowPoints[1].x := 19;
  ArrowPoints[1].y := 190;
ArrowPoints[2].x := 180;
  ArrowPoints[2].y := 120;
ArrowPoints[3].x := 10;
  ArrowPoints[3].y := 10;
PaintBox5.Canvas.Polygon(ArryPoints);

but this may not look like a lazer to me.

better code for lazer look

ColorAry[0] := clAqua;
ColorAry[1] := $006699FF;
ColorAry[2] := $000000DD;
Application.ProcessMessages;
PaintBox5.Canvas.Pen.Color := clYellow;
PaintBox5.Canvas.Pen.Width := 4;
PaintBox5.Canvas.MoveTo(StartPt.x,StartPt.y);
PaintBox5.Canvas.LineTo(EndPt.x,EndPt.y);
PaintBox5.Canvas.Pen.Width := 2;
for i := 0 to 2 do
  begin
  PaintBox5.Canvas.Pen.Color := ColorAry[i];
  PaintBox5.Canvas.MoveTo(StartPt.x,StartPt.y);
  PaintBox5.Canvas.LineTo(EndPt.x,EndPt.y);
  Sleep(100);
  end;
Sleep(110);
PaintBox5.Canvas.Pen.Color := clBlack;
PaintBox5.Canvas.Pen.Width := 4;
PaintBox5.Canvas.MoveTo(StartPt.x,StartPt.y);
PaintBox5.Canvas.LineTo(EndPt.x,EndPt.y);


- - - - - - - - - - - - - - - - - -

using sleep(100) in a loop will stall your app, let me know if you need another way
0
 
Gareth_HastingsAuthor Commented:
So if I didn't want to use DirectX I would have to fill the beam in pixel by pixel and without some kind of double buffering it would be very slow right?
0
 
Slick812Commented:
there's many graphic drawing functions that you could use that are not slow, if you use Canvas.Pixels[W, 10] := clRed; then that is slow compared to drawing a line with LineTo, and it's not so easy to use the trig to figure out each pixel's position. You said you want a look like beam.bmp, try

PaintBox5.Canvas.Pen.Color := $00008800;
PaintBox5.Canvas.Pen.Width := 3;
PaintBox5.Canvas.MoveTo(10,10);
PaintBox5.Canvas.LineTo(110,10);
PaintBox5.Canvas.Pen.Width := 1;
PaintBox5.Canvas.Pen.Color := $0000DD00
PaintBox5.Canvas.MoveTo(10,10);
PaintBox5.Canvas.LineTo(110,10);
0
 
Gareth_HastingsAuthor Commented:
Sorry I didn't reply sooner, I've been playing around with this a little bit and not got any further really. So one last question before I award the points.

Does Delphi have a fill function? Like if I drew a triangle could I fill it in?
0
 
aikimarkCommented:
you really ought to use the APIs.  they can create and fill triangles.
0
 
Slick812Commented:
The delphi canvas has a FloodFill procedure,

Canvas.FloodFill(X, Y, clRed, fsBorder);

or the FillRect

Canvas.FillRect(Rect1);

you really should look at the delphi help for TCanvas and it's methods
0
 
Gareth_HastingsAuthor Commented:
thanks, worked a treat. By using 9 filled triangles a various sizes I get a fairly good laser effect. Also I found that TForm has a double buffer property which gets rid of all those flickers !!

form1.doublebuffered := true;

does use a little extra memory though. As for the speed, that routine that in total draws 18 triangles takes less the 0.4 seconds on a P3 - 500mhz machine with a 8mb generic gfx card.

Here's the final laser effect result

http://exo.netgs.co.uk/exodus/s10.jpg
0
 
Gareth_HastingsAuthor Commented:
thanks, worked a treat. By using 9 filled triangles a various sizes I get a fairly good laser effect. Also I found that TForm has a double buffer property which gets rid of all those flickers !!

form1.doublebuffered := true;

does use a little extra memory though. As for the speed, that routine that in total draws 18 triangles takes less the 0.4 seconds on a P3 - 500mhz machine with a 8mb generic gfx card.

Here's the final laser effect result

http://exo.netgs.co.uk/exodus/s10.jpg
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.