?
Solved

Simple Delphi Graphics

Posted on 2002-05-03
17
Medium Priority
?
582 Views
Last Modified: 2010-04-04
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

0
Comment
Question by:Gareth_Hastings
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +3
17 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6989126
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
 
LVL 1

Author Comment

by:Gareth_Hastings
ID: 6989127
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 6989132
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 27

Expert Comment

by:kretzschmar
ID: 6989143
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
 
LVL 34

Expert Comment

by:Slick812
ID: 6989178
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
 
LVL 2

Expert Comment

by:freshman3k
ID: 6989248
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
 
LVL 2

Expert Comment

by:freshman3k
ID: 6989250
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
 
LVL 46

Expert Comment

by:aikimark
ID: 6989300
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
 
LVL 34

Expert Comment

by:Slick812
ID: 6989368
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
 
LVL 1

Author Comment

by:Gareth_Hastings
ID: 6994562
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
 
LVL 34

Expert Comment

by:Slick812
ID: 6995297
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
 
LVL 1

Author Comment

by:Gareth_Hastings
ID: 7053221
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
 
LVL 46

Expert Comment

by:aikimark
ID: 7053563
you really ought to use the APIs.  they can create and fill triangles.
0
 
LVL 34

Expert Comment

by:Slick812
ID: 7055400
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
 
LVL 7

Accepted Solution

by:
Cynna earned 1600 total points
ID: 7062469
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
 
LVL 1

Author Comment

by:Gareth_Hastings
ID: 7098585
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
 
LVL 1

Author Comment

by:Gareth_Hastings
ID: 7098587
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question