Recent

Author Topic: Beginner graphics  (Read 11132 times)

daringly

  • Jr. Member
  • **
  • Posts: 73
Beginner graphics
« 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.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Beginner graphics
« Reply #1 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.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Beginner graphics
« Reply #2 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
« Last Edit: September 04, 2018, 07:57:06 am by Handoko »

daringly

  • Jr. Member
  • **
  • Posts: 73
Re: Beginner graphics
« Reply #3 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.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
« Last Edit: September 05, 2018, 03:58:17 pm by Handoko »

daringly

  • Jr. Member
  • **
  • Posts: 73
Re: Beginner graphics
« Reply #5 on: September 21, 2018, 06:32:41 pm »
The TCanvas documentation is still missing. Is there anywhere else to get it?

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: Beginner graphics
« Reply #6 on: September 21, 2018, 06:54:12 pm »
Google Delphi TCanvas.
Should be almost 100% the same.

Bart


eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Beginner graphics
« Reply #8 on: September 22, 2018, 12:26:12 am »
No canvas drawing required at all.
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

daringly

  • Jr. Member
  • **
  • Posts: 73
Re: Beginner graphics
« Reply #9 on: September 24, 2018, 05:42:08 pm »
Eny,

What is the code to do that? I really am dated at this.

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Beginner graphics
« Reply #10 on: September 24, 2018, 05:51:35 pm »
I guess, she used TShape and TLabel.

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Beginner graphics
« Reply #11 on: September 24, 2018, 11:22:16 pm »
I guess, she used TShape and TLabel.
Yes He used TShape and TLabel  8)
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Beginner graphics
« Reply #12 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..
The only true wisdom is knowing you know nothing

Handoko

  • Hero Member
  • *****
  • Posts: 5130
  • My goal: build my own game engine using Lazarus
Re: Beginner graphics
« Reply #13 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.
« Last Edit: September 25, 2018, 05:13:31 am by Handoko »

daringly

  • Jr. Member
  • **
  • Posts: 73
Re: Beginner graphics
« Reply #14 on: September 25, 2018, 05:25:49 pm »
Thanks! This was exactly the type of example I was looking for.

 

TinyPortal © 2005-2018