Solved

Simple Delphi Graphics

Posted on 2002-05-03
17
542 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
  • 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
 
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 33

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 45

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 33

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 33

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 45

Expert Comment

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

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 400 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now