Recent

Author Topic: Why graph unit is slow? How i can make it more fast?  (Read 7624 times)

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Why graph unit is slow? How i can make it more fast?
« on: July 19, 2016, 01:48:16 am »
I am a beginner and i start programming with units like crt, graph, sysutils, mmsystem.. I know how to play wav files, write and read data from a file ecc. But i noticed a few months ago that graph unit is too slow. For example this code:
Code: Pascal  [Select][+][-]
  1. uses graph;
  2. var s:string;
  3. x,y:integer;
  4. begin
  5.        
  6.        // here usually init graph ......
  7.        
  8.         s:='hello';
  9.         x:=320;
  10.         y:=320;
  11. Repeat
  12. Cleardevice;
  13. x:=x+1;
  14. SetColor(white);
  15. OutTextXY(x,y,s);
  16. Delay(2);
  17. Until x=1300;
  18. readkey;
  19. End.
  20.  
  21.        
  22.    
  23.        
  24.  
The simplest example program shows a string that moves to the right with an animation but it lags very much as i see the string that sometimes becomes gray or white and black together... Maybe i'm wrong at something or i should change unit? Please answer because i am developing a game but it lags and so there aren't animations or effects!!
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Why graph unit is slow? How i can make it more fast?
« Reply #1 on: July 19, 2016, 02:05:43 am »
I quote a small part of the unit description:
Quote
The graph unit will allow to recompile old programs. They will work to some extent, but if the application has heavy graphical needs, it's recommended to use another set of graphical routines, suited to the platform the program should work on.
What that means is that, depending on your needs, you would be far better off using a special (gaming and/or graphics) library, such as SDL. You could also opt for something like ptcpas.

For simple games that does not requires high graphics demand (such as snake, patience, pacman and similar) it is also possible to use lazarus components/classes and some double/triple buffering. For 3D games you'd be better of using DirectX/OpenGL or use one of the many available gaming engines.

Have a look here.
« Last Edit: July 19, 2016, 02:07:33 am by molly »

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Why graph unit is slow? How i can make it more fast?
« Reply #2 on: July 19, 2016, 04:12:47 am »
Please answer because i am developing a game but it lags and so there aren't animations or effects!!

Graph is slow and not suitable for game. For simple 2D non-performance-need games, you can try:
http://wiki.freepascal.org/BGRABitmap
http://www.crossgl.com/aggpas/index.html

Both of them can be considered as graphics engine. If you need a game engine you may try:
http://wiki.freepascal.org/Castle_Game_Engine
http://www.zgameeditor.org/

ZGameEditor is a standalone program. Although the code is write using Pascal, it is not a library and cannot be include into your Pascal program. Programmers who has some game design knowledge can write games just in minutes first time he/she try ZGameEditor. It has its own Pascal-like script language.

I personally choose OpenGL and OpenGL ES (for Android) because it can deliver the performance as fast as your GPU can give you. If you wish to try, then here is how to enable OpenGL on your Lazarus:

1. Lazarus main menu > Package > Install/Uninstall Packages
2. Search for "lazopenglcontext" on the right panel
3. Click the "Install Selection" button
4. On the component palette > "OpenGL" tab

And this is OpenGL tutorial:
http://wiki.freepascal.org/OpenGL_Tutorial

If you're serious in game programming, you should learn OpenGL. You aren't a game programmer if you can't use OpenGL.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Why graph unit is slow? How i can make it more fast?
« Reply #3 on: July 19, 2016, 05:28:55 am »
You might gain speed by using another graph backend. FPC implementation ships numerous graph backend (different for each platform, look in graph folder of your FPC installation), simply change it to the one that might work the fastest on your machine. Note that this won't change the fact that graph unit itself is not designed for heavy use. Using a real game library is a much wiser choice.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Why graph unit is slow? How i can make it more fast?
« Reply #4 on: July 19, 2016, 07:59:52 am »
Maybe i'm wrong at something or i should change unit?

After studying your code I found 2 things that cause the problem:
- Clearing and repainting the whole screen should avoided.
- Drawing text should be performed once only

Clearing and drawing whole screen is painfully slow, except it is performed by graphics dedicated processors (via OpenGL or DirectX). Altenatively, you can overlay a bigger sprite to 'clean' the screen area and showing the new position in a single pass. This animation trick is often used on old games without the need of game graphics cards in DOS era.

For the second issue, it is much wiser to draw the text once, save it to memory buffer and use the buffer, no need to regenerate the text.

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Why graph unit is slow? How i can make it more fast?
« Reply #5 on: July 19, 2016, 08:18:16 am »
Clearing and drawing whole screen is painfully slow, except it is performed by graphics dedicated processors (via OpenGL or DirectX). Altenatively, you can overlay a bigger sprite to 'clean' the screen area and showing the new position in a single pass. This animation trick is often used on old games without the need of game graphics cards in DOS era.
Or
- use a (smaller, size of scroll-area) viewport.
- store background, draw text, pause, restore background (and in case of scrolling as in your example that is a small area).
- draw a rectangle with same colour as background on the position/size you previously draw the text.

Best choice depends on so many things that it's hard to say what's best.

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Why graph unit is slow? How i can make it more fast?
« Reply #6 on: July 19, 2016, 10:49:09 am »
Maybe i'm wrong at something or i should change unit?

Clearing and drawing whole screen is painfully slow, except it is performed by graphics dedicated processors (via OpenGL or DirectX). Altenatively, you can overlay a bigger sprite to 'clean' the screen area and showing the new position in a single pass. This animation trick is often used on old games without the need of game graphics cards in DOS era.
And so what i can do? I thought games consist on frames in which the program repaint all the images or texts... Where i can find a simple example code?
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Why graph unit is slow? How i can make it more fast?
« Reply #7 on: July 19, 2016, 11:42:09 am »
I'm now away from my home computer. Will be back to provide you more information.

For now I recommend you to try:
www.qb64.net

Although it doesn't use Pascal, but it is good for beginners because it uses the easy to learn BASIC language. Its graphics library is far easier to use compare to OpenGL or other engines, and is optimized for performance. So it can be used for building simple games.

On its wiki, there is page called "A Small Game Tutorial". A good start for newbies both game creator and programmers.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Why graph unit is slow? How i can make it more fast?
« Reply #8 on: July 19, 2016, 08:16:01 pm »
Hello this the better code:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses crt, Graph;
  4.  
  5. procedure DevicePreparation;
  6. var
  7.   gd, gm: SmallInt;
  8. begin
  9.   gd := VGA;
  10.   gm := VGAHi;
  11.   InitGraph(gd, gm, '');
  12.   gd := GraphResult;
  13.   if (gd <> grOK) then
  14.   begin
  15.     WriteLn('Error in initializing graphic card! The error is');
  16.     WriteLn(GraphErrorMsg(gd));
  17.     halt;
  18.   end;
  19. end;
  20.  
  21. procedure DeviceClosing;
  22. begin
  23.   Closegraph;
  24.   WriteLn('Good bye!');
  25. end;
  26.  
  27. procedure GetSprite(var Buffer: Pointer; x, y, w, h: Integer);
  28. begin
  29.   Getmem(Buffer, ImageSize(x, y, x+w, y+h));
  30.   x := x - 5; // x should be smaller if the object is moving to right
  31.   GetImage(x, y, x+w, y+h, Buffer^);
  32. end;
  33.  
  34. var
  35.   Sprite: Pointer;
  36.   s: string;
  37.   x, y: integer;
  38.  
  39. begin
  40.  
  41.   // Initial values
  42.   s := 'hello';
  43.   x := 50;
  44.   y := 320;
  45.  
  46.   // Screen preparation
  47.   DevicePreparation;
  48.  
  49.   // Draw the sprite and save it to buffer
  50.   SetColor(white);
  51.   OutTextXY(x, y, s);
  52.   GetSprite(Sprite, x, y, 50, 10); // size of the object is 50 x 10
  53.  
  54.   // Animate Sprite to right
  55.   Repeat
  56.     PutImage(x, y, Sprite^, CopyPut);
  57.     x := x + 1;
  58.     Delay(2);
  59.   Until (x = 550);
  60.   ReadKey;
  61.  
  62.   // Closing
  63.   DeviceClosing;
  64.   Freemem(Sprite);
  65.  
  66. End.

It is better because it avoid to clear the whole screen and only generate the sprite once (using OutTextXY). The generated drawing is store in sprite buffer.

One of the side effect of this technique is flicker free.
« Last Edit: July 19, 2016, 08:21:18 pm by Handoko »

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Why graph unit is slow? How i can make it more fast?
« Reply #9 on: July 19, 2016, 08:22:00 pm »
Hello this the better code:

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses crt, Graph;
  4.  
  5. procedure DevicePreparation;
  6. var
  7.   gd, gm: SmallInt;
  8. begin
  9.   gd := VGA;
  10.   gm := VGAHi;
  11.   InitGraph(gd, gm, '');
  12.   gd := GraphResult;
  13.   if (gd <> grOK) then
  14.   begin
  15.     WriteLn('Error in initializing graphic card! The error is');
  16.     WriteLn(GraphErrorMsg(gd));
  17.     halt;
  18.   end;
  19. end;
  20.  
  21. procedure DeviceClosing;
  22. begin
  23.   Closegraph;
  24.   WriteLn('Good bye!');
  25. end;
  26.  
  27. procedure GetSprite(var Buffer: Pointer; x, y, w, h: Integer);
  28. begin
  29.   Getmem(Buffer, ImageSize(x, y, x+w, y+h));
  30.   x := x - 5; // x should be smaller if the object is moving to right
  31.   GetImage(x, y, x+w, y+h, Buffer^);
  32. end;
  33.  
  34. var
  35.   Sprite: Pointer;
  36.   s: string;
  37.   x, y: integer;
  38.  
  39. begin
  40.  
  41.   // Initial values
  42.   s := 'hello';
  43.   x := 50;
  44.   y := 320;
  45.  
  46.   // Screen preparation
  47.   DevicePreparation;
  48.  
  49.   // Draw the sprite and save it to buffer
  50.   SetColor(white);
  51.   OutTextXY(x, y, s);
  52.   GetSprite(Sprite, x, y, 50, 10); // size of the object is 50 x 10
  53.  
  54.   // Animate Sprite to right
  55.   Repeat
  56.     PutImage(x, y, Sprite^, CopyPut);
  57.     x := x + 1;
  58.     Delay(2);
  59.   Until (x = 550);
  60.   ReadKey;
  61.  
  62.   // Closing
  63.   DeviceClosing;
  64.   Freemem(Sprite);
  65.  
  66. End.

It is better because it avoid to clear the whole screen and only generate the sprite once (using OutTextXY). The generated drawing is store in sprite buffer.
thanks! now i am starting to learn SDL 2.0 and i see it is very simple than that i thought.. I want to create a game but i have no ideas.. what game can i create?.... no reply XD
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Why graph unit is slow? How i can make it more fast?
« Reply #10 on: July 19, 2016, 08:33:57 pm »
.. I want to create a game but i have no ideas.. what game can i create?.... no reply XD

You may search some old DOS games and try to create something similar to them, this kind of classic games usually not too hard to create. The hardest part of game development for single person developer are music and graphics creations.

Only your imagination can limit what you can create.
Have fun.

DiCri

  • Full Member
  • ***
  • Posts: 151
  • My goal : Build a game
    • http://manueldicriscito.altervista.org/DinoLand.zip
Re: Why graph unit is slow? How i can make it more fast?
« Reply #11 on: July 19, 2016, 09:15:11 pm »
.. I want to create a game but i have no ideas.. what game can i create?.... no reply XD

You may search some old DOS games and try to create something similar to them, this kind of classic games usually not too hard to create. The hardest part of game development for single person developer are music and graphics creations.

Only your imagination can limit what you can create.
Have fun.
I CREATE MUSIC TOO :P!! i learned how to view image on screen, scale, rotate and fade animation ecc. Thank SDL Unit
I'm a game developer.. Now studying..
Go download my game:
http://manueldicriscito.altervista.org/DinoLand.zip

 

TinyPortal © 2005-2018