Lazarus

Free Pascal => Beginners => Topic started by: daringly on September 04, 2018, 05:20:12 am

Title: Beginner graphics
Post by: daringly on September 04, 2018, 05:20:12 am
I'm working on a strategy game. The graphics don't have to be pretty, just barely functional.

If your goal was to open a window, draw a few lines, and display a line or two of text in different places, what is the easiest way to do that? There are a lot of libraries, but I remember from 30 years ago with Turbo Pascal, you could do minimalist graphics much easier.
Title: Re: Beginner graphics
Post by: taazz on September 04, 2018, 05:33:22 am
I'm working on a strategy game. The graphics don't have to be pretty, just barely functional.

If your goal was to open a window, draw a few lines, and display a line or two of text in different places, what is the easiest way to do that? There are a lot of libraries, but I remember from 30 years ago with Turbo Pascal, you could do minimalist graphics much easier.
It does not work that way any more. The modern operating system do not give you direct access to the hardware any longer most of the time an application is spend waiting to be informed by the OS of various events keyboard, screen redraw etc. The newer libraries are complicate because the try to work around the reactive model application to a more active where you control the timming with the hardware not the os and that is why they look more complicated. In any way if you are not looking for speed but for graphics only then I would suggest to start with http://wiki.freepascal.org/Developing_with_Graphics it has all the relevant information you need to make an informed decision.
Title: Re: Beginner graphics
Post by: Handoko on September 04, 2018, 06:52:06 am
I wrote a vertically scrolling space shooter game for Game Contest 2018 unfortunately I can't finish it on the deadline. It only uses TCanvas drawing feature, it has no problem running on my 10 years old Dual Core laptop. Computers nowadays run very fast, you don't need hardware-accelerated graphics library for simple games.

For simple games, you can use TCanvas:
- Very easy to use
- Zero deployment setup
- Slow, not suitable for graphics intensive games
http://wiki.lazarus.freepascal.org/TCanvas

After you understand how to use TCanvas, you may want to try BGRABitmap:
- You can switch to BGRABitmap from TCanvas easily
- CanvasBGRA is slower than TCanvas, but some features are much faster
- Has lots more features than TCanvas
- Zero deployment setup
http://wiki.freepascal.org/BGRABitmap

Graphics32 and Fast Direct Pixel Access:
- Fast
- Zero deployment setup
- A bit difficult for beginners
https://github.com/graphics32
http://wiki.freepascal.org/Fast_direct_pixel_access

Allegro.pas
- Very fast because it is hardware accelerated
- Offer more than graphics: joystick, keyboard, audio, physics, etc
- Detailed documentation provided by Allegro official website
- Relatively easy to learn because of the detailed documentation
- Need some extra work for deployment your games
http://allegro-pas.sourceforge.net/introduction.php
https://liballeg.org/api.html

nxPascal:
- Lightweight but powerful
- Very fast because it is hardware accelerated
- I can't find the documentation nor tutorials
http://wiki.freepascal.org/nxPascal

GLScene:
- Very fast because it is hardware accelerated
- Not suitable for 2D application
- Zero deployment setup
http://wiki.freepascal.org/GLScene

Castle Game Engine:
- Probable the most feature-rich
- 2D, 3D, Android, iOS, Web, physics, AI, etc
- Very fast because it is hardware accelerated
https://castle-engine.io/features.php

OpenGL and DirectX:
- The fastest and the hardest
- Don't use it unless you want to write your own graphics engine
http://wiki.freepascal.org/OpenGL_Tutorial
http://wiki.freepascal.org/FPC_and_DirectX

For beginners, here has detailed explanations how to write a simple snake game using TCanvas:
http://forum.lazarus.freepascal.org/index.php/topic,38136.msg258381.html#msg258381
Title: Re: Beginner graphics
Post by: daringly on September 05, 2018, 03:28:27 pm
Thanks for the info.

Is there a Tcanvas tutorial, or some basic code I can play with? I spent a bit of time searching, but didn't find anything putting it all together.
Title: Re: Beginner graphics
Post by: Handoko on September 05, 2018, 03:55:52 pm
http://wiki.lazarus.freepascal.org/TCanvas

http://forum.lazarus.freepascal.org/index.php/topic,38136.msg258381.html#msg258381

This is TCanvas documentation (unfortunately it's having problem currently):
https://lazarus-ccr.sourceforge.io/docs/lcl/graphics/tcanvas.html
Title: Re: Beginner graphics
Post by: daringly on September 21, 2018, 06:32:41 pm
The TCanvas documentation is still missing. Is there anywhere else to get it?
Title: Re: Beginner graphics
Post by: Bart on September 21, 2018, 06:54:12 pm
Google Delphi TCanvas.
Should be almost 100% the same.

Bart
Title: Re: Beginner graphics
Post by: Handoko on September 21, 2018, 06:56:13 pm
TCanvas Methods
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/!!MEMBERTYPE_Methods_Graphics_TCanvas.html

TCanvas Properties
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/!!MEMBERTYPE_Properties_Graphics_TCanvas.html
Title: Re: Beginner graphics
Post by: eny on September 22, 2018, 12:26:12 am
No canvas drawing required at all.
Title: Re: Beginner graphics
Post by: daringly on September 24, 2018, 05:42:08 pm
Eny,

What is the code to do that? I really am dated at this.
Title: Re: Beginner graphics
Post by: Handoko on September 24, 2018, 05:51:35 pm
I guess, she used TShape and TLabel.
Title: Re: Beginner graphics
Post by: eny on September 24, 2018, 11:22:16 pm
I guess, she used TShape and TLabel.
Yes He used TShape and TLabel  8)
Title: Re: Beginner graphics
Post by: jamie on September 24, 2018, 11:44:20 pm
The help file has all of the public functions listed and gives you an idea of what to do...

 I find it quite nice for a quick reference..
Title: Re: Beginner graphics
Post by: Handoko on September 25, 2018, 05:11:47 am
Simple pong game created within half hour, using Eny's suggestion.

- No need third party graphics engine
- No installation/distribution pain
- Cross platform
- Only 113 LOC
 ;D

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Graphics, ExtCtrls, LCLType, LCLIntf, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     ApplicationProperties1: TApplicationProperties;
  16.     Label1: TLabel;
  17.     RectShape: TShape;
  18.     BallShape: TShape;
  19.     Timer1: TTimer;
  20.     procedure ApplicationProperties1Activate(Sender: TObject);
  21.     procedure ApplicationProperties1Deactivate(Sender: TObject);
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure FormResize(Sender: TObject);
  24.     procedure Timer1Timer(Sender: TObject);
  25.   private
  26.     BallVelocityX: Integer;
  27.     BallVelocityY: Integer;
  28.     Hit:           Integer;
  29.     Missed:        Integer;
  30.   end;
  31.  
  32. var
  33.   Form1: TForm1;
  34.  
  35. implementation
  36.  
  37. {$R *.lfm}
  38.  
  39. { TForm1 }
  40.  
  41. procedure TForm1.Timer1Timer(Sender: TObject);
  42. begin
  43.  
  44.   if (BallShape.Top+BallShape.Height > RectShape.Top) and
  45.     (BallShape.Left > RectShape.Left) and
  46.     (BallShape.Left+BallShape.Width < RectShape.Left+RectShape.Width) then
  47.     begin
  48.       BallShape.Top := RectShape.Top - BallShape.Height;
  49.       BallVelocityY := -Random(10) -1;
  50.       Inc(Hit);
  51.       Label1.Caption := 'Hit ' + Hit.ToString + ' | Missed ' + Missed.ToString;
  52.     end;
  53.   if BallShape.Left < 0 then
  54.     BallVelocityX := Random(10) +1;
  55.   if BallShape.Left+BallShape.Width > Width then
  56.     BallVelocityX := -Random(10) -1;
  57.   if BallShape.Top < 0 then
  58.     BallVelocityY := Random(10) +1;
  59.   if BallShape.Top > Height then begin
  60.     BallShape.Top := 10;
  61.     Inc(Missed);
  62.     Label1.Caption := 'Hit ' + Hit.ToString + ' | Missed ' + Missed.ToString;
  63.   end;
  64.  
  65.   BallShape.Left := BallShape.Left + BallVelocityX;
  66.   BallShape.Top  := BallShape.Top  + BallVelocityY;
  67.  
  68.   if (GetKeyState(VK_LEFT) < 0)  then RectShape.Left := RectShape.Left - 10;
  69.   if (GetKeyState(VK_RIGHT) < 0) then RectShape.Left := RectShape.Left + 10;
  70.  
  71. end;
  72.  
  73. procedure TForm1.FormCreate(Sender: TObject);
  74. begin
  75.  
  76.   Constraints.MinWidth  := 320;
  77.   Constraints.MinHeight := 240;
  78.  
  79.   Label1.Caption := '';
  80.  
  81.   BallVelocityX := Random(20)-10;
  82.   BallVelocityY := Random(20)-10;
  83.   if BallVelocityX = 0 then
  84.     BallVelocityX := 1;
  85.   if BallVelocityY = 0 then
  86.     BallVelocityY := 1;
  87.  
  88. end;
  89.  
  90. procedure TForm1.FormResize(Sender: TObject);
  91. begin
  92.  
  93.   if BallShape.Left+BallShape.Width > Width then
  94.     BallShape.Left := Width - BallShape.Width - 10;
  95.   if BallShape.Top+BallShape.Height > Height then
  96.     BallShape.Top := Hit - BallShape.Height - 10;
  97.  
  98.   if RectShape.Left+RectShape.Width > Width then
  99.     RectShape.Left := Width - RectShape.Width -10;
  100.  
  101. end;
  102.  
  103. procedure TForm1.ApplicationProperties1Deactivate(Sender: TObject);
  104. begin
  105.   Timer1.Enabled := False;
  106. end;
  107.  
  108. procedure TForm1.ApplicationProperties1Activate(Sender: TObject);
  109. begin
  110.   Timer1.Enabled := True;
  111. end;
  112.  
  113. end.
Title: Re: Beginner graphics
Post by: daringly on September 25, 2018, 05:25:49 pm
Thanks! This was exactly the type of example I was looking for.
Title: Re: Beginner graphics
Post by: Thaddy on September 25, 2018, 05:36:54 pm
Always start with this:
Title: Re: Beginner graphics
Post by: correa.elias on September 25, 2018, 06:43:58 pm
Just adding to Handoko's answer theres PTCpas (http://ptcpas.sourceforge.net/) too.
Its bundled with freepascal/lazarus by default and you can find nice demos in 'examples/ptc' folder inside your compiler installation place. Compile the examples and have fun :).
Title: Re: Beginner graphics
Post by: Handoko on September 25, 2018, 07:14:05 pm
Hello correa.elias,
Welcome to the forum.

Glad to know PTCPas is written in Free Pascal. Hope it will support OpenGL someday. I use Linux and DirectX isn't supported.

I wonder why I can't find PTC in the example folder of my Lazarus installation. Maybe because I use Linux?
Title: Re: Beginner graphics
Post by: correa.elias on September 25, 2018, 07:28:38 pm
Thanks Handoko!
I have good news for you: it already supports opengl (theres 2 examples using opengl in ptc).
Take a look at FPC compiler installation folder (not Lazarus). Here in my windows computer its located at 'C:\FPC\3.0.2\examples\ptc\'.
Title: Re: Beginner graphics
Post by: lucamar on September 25, 2018, 07:29:58 pm
I wonder why I can't find PTC in the example folder of my Lazarus installation. Maybe because I use Linux?

In mine they are at /usr/share/doc/fpc-3.0.4/ptc/examples/
TinyPortal © 2005-2018