Recent

Author Topic: Allegro.pas  (Read 33441 times)

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Allegro.pas
« Reply #45 on: March 26, 2020, 12:47:40 pm »
In theory, it does, but MinGW dropped WinXP support few time ago;  so Allegro supports WinXP but MinGW doesn't.

I tried to download and install an older MinGW to compile Allegro but I wasn't able to find the latest version that support XP, and the MinGW package manager doesn't work properly (it was always buggy) so I downloaded a precompiled version available in Allegro's website.  Maybe that's the problem.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Allegro.pas
« Reply #46 on: April 16, 2020, 01:25:28 pm »
I did a bugfix release because I found a sily bug and I need the library shining this weekend (LudumDare!) and also in May (TINS!).  More details here.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Allegro.pas
« Reply #47 on: October 21, 2020, 01:11:16 pm »
Hi Ñuño_Martínez

I want to ask,
did I do a mistake or is it the normal, but the area of game is corrupted.

I followed the first tutorial of Allegro.Pas, and set
  Display := al_create_display (128, 128); 
so I expect that the area for game is a square (same width of sides)
but the result is not.

why?
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Allegro.pas
« Reply #48 on: October 24, 2020, 11:42:58 am »
That's weird.  I did a test (Xubuntu) and works properly.  I assume you're using Windows 10.  Maybe it is a limitation that prevents it to create windows (displays) smaller than 160 pixel width.  I see some duplicated pixels that tells me it is scaling the output to fit the window.

Can you post/attach you code here?  Maybe there's something somewhere...
« Last Edit: October 24, 2020, 11:46:00 am by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Allegro.pas
« Reply #49 on: October 25, 2020, 04:55:23 am »

yes, I am using Windows 10.

Can you post/attach you code here?  Maybe there's something somewhere...


Code: Pascal  [Select][+][-]
  1.  
  2. PROGRAM HelloWorld;
  3. (* First Allegro program. *)
  4.  
  5.  
  6. USES
  7.   allegro5, al5font;
  8.  
  9.  
  10. VAR
  11.   Timer: ALLEGRO_TIMERptr;
  12.   Queue: ALLEGRO_EVENT_QUEUEptr;
  13.   Event: ALLEGRO_EVENT;
  14.   Display: ALLEGRO_DISPLAYptr;
  15.   Font: ALLEGRO_FONTptr;
  16.  
  17.  
  18.   Redraw, EndProgram: BOOLEAN;
  19.  
  20.  
  21. BEGIN
  22.   al_init;
  23.   al_install_keyboard;
  24.  
  25.  
  26.   Timer := al_create_timer (1 / 30);
  27.   Queue := al_create_event_queue;
  28.   Display := al_create_display (128, 128);
  29.   Font := al_create_builtin_font;
  30.  
  31.  
  32.   al_register_event_source (Queue, al_get_keyboard_event_source);
  33.   al_register_event_source (Queue, al_get_display_event_source (Display));
  34.   al_register_event_source (Queue, al_get_timer_event_source (Timer));
  35.  
  36.  
  37.   Redraw := TRUE;
  38.   EndProgram := FALSE;
  39.  
  40.  
  41.   al_start_timer (Timer);
  42.   REPEAT
  43.     al_wait_for_event (Queue, @Event);
  44.  
  45.  
  46.     IF Event.ftype = ALLEGRO_EVENT_TIMER THEN
  47.       Redraw := TRUE
  48.     ELSE
  49.       IF (Event.ftype = ALLEGRO_EVENT_KEY_DOWN) OR (Event.ftype = ALLEGRO_EVENT_DISPLAY_CLOSE) THEN
  50.         EndProgram := TRUE;
  51.  
  52.  
  53.     IF Redraw AND al_is_event_queue_empty (Queue) THEN
  54.     BEGIN
  55.       al_clear_to_color (al_map_rgb (255, 128, 64));
  56.       al_draw_text (Font, al_map_rgb (255, 255, 255), 16, 32, 0, 'Holla, World!');
  57.       al_flip_display;
  58.  
  59.  
  60.       Redraw := FALSE
  61.     END
  62.   UNTIL EndProgram;
  63.  
  64.  
  65.   al_destroy_font (Font);
  66.   al_destroy_display (Display);
  67.   al_destroy_timer (Timer);
  68.   al_destroy_event_queue (Queue)
  69. END.
  70.  
  71.  
  72.  
« Last Edit: October 25, 2020, 05:35:35 am by x2nie »
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Allegro.pas
« Reply #50 on: October 25, 2020, 02:30:21 pm »
Hi Yes! if I change the size larger than 160, the client size is then correct.


Thanks you!
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Allegro.pas
« Reply #51 on: October 30, 2020, 07:48:15 pm »
I tested your code on Xubuntu Linux and it created the window of correct size.

I knew Windows had problems creating windows bigger than the desktop, but now I know it has small size limits.

Let me know about advances. :)
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Allegro.pas
« Reply #52 on: November 09, 2020, 12:23:00 pm »
Hi


during searching a way to work directly with allegro-bitmap,
suddenly I solve the yet unsolved problem : copy the allegro-bitmap content into byte array.
(see the below picture of the result.)


It also solve my own problem of how to directly modify the bitmap content.


here is to modified function
Code: Pascal  [Select][+][-]
  1. PROCEDURE Draw;
  2.   type
  3.     TByteArray = array [Word] of byte;
  4.     PByteArray = ^TByteArray;
  5.   VAR
  6.     x, y: SINGLE;
  7.     i,ix,iy,iw, ih , FormatSize : INTEGER;
  8.     FormatLock: ALLEGRO_PIXEL_FORMAT;
  9.     Screen, Temp: ALLEGRO_BITMAPptr;
  10.       Lock: ALLEGRO_LOCKED_REGIONptr;
  11.     Data: PByteArray;
  12.   BEGIN
  13.     iw := al_get_bitmap_width (Pattern);
  14.     ih := al_get_bitmap_height (Pattern);
  15.     al_set_blender (ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
  16.     al_clear_to_color (Background);
  17.     Screen := al_get_target_bitmap;
  18.  
  19.  
  20.     SetXY (8, 8);
  21.  
  22.  
  23.   { Test 1. }
  24.   { /* Disabled: drawing to same bitmap is not supported. }
  25.   (*
  26.     Print ('Screen -> Screen (%.1f fps)', get_fps(0));
  27.     get_xy(&x, &y);
  28.     al_draw_bitmap(ex.Pattern, x, y, 0);
  29.  
  30.  
  31.     start_timer(0);
  32.     al_draw_bitmap_region(screen, x, y, iw, ih, x + 8 + iw, y, 0);
  33.     stop_timer(0);
  34.     SetXY (x, y + ih);
  35.    *)
  36.  
  37.  
  38.   { Test 2. }
  39.     Print ('Screen -> Bitmap -> Screen (%.1f fps)', [GetFPS (1)]);
  40.     GetXY (x, y);
  41.     al_draw_bitmap (Pattern, x, y, 0);
  42.  
  43.  
  44.     Temp := al_create_bitmap (iw, ih);
  45.     al_set_target_bitmap (Temp);
  46.     al_clear_to_color (Red);
  47.     StartTimer (1);
  48.     al_draw_bitmap_region (Screen, x, y, iw, ih, 0, 0, 0);
  49.  
  50.  
  51.     al_set_target_bitmap (Screen);
  52.     al_draw_bitmap (Temp, x + 8 + iw, y, 0);
  53.     StopTimer (1);
  54.     SetXY (x, y + ih);
  55.  
  56.  
  57.     al_destroy_bitmap (Temp);
  58.  
  59.  
  60.   { Test 3. }
  61.     Print ('Screen -> Memory -> Screen (%.1f fps)', [getfps (2)]);
  62.     GetXY (x, y);
  63.     al_draw_bitmap (Pattern, x, y, 0);
  64.  
  65.  
  66.     al_set_new_bitmap_flags (ALLEGRO_MEMORY_BITMAP);
  67.     Temp := al_create_bitmap (iw, ih);
  68.     al_set_target_bitmap (Temp);
  69.     al_clear_to_color (Red);
  70.     StartTimer (2);
  71.     al_draw_bitmap_region (Screen, x, y, iw, ih, 0, 0, 0);
  72.  
  73.  
  74.     al_set_target_bitmap (Screen);
  75.     al_draw_bitmap (Temp, x + 8 + iw, y, 0);
  76.     StopTimer (2);
  77.     SetXY (x, y + ih);
  78.  
  79.  
  80.     al_destroy_bitmap (Temp);
  81.     al_set_new_bitmap_flags (ALLEGRO_VIDEO_BITMAP);
  82.  
  83.  
  84.  
  85.  
  86. (*
  87.   Disabled because the "memcpy".  I tried it but I never used 'memcpy' on C nor
  88.   pointers on Pascal so I don't know how to translate it. *)
  89.  
  90.  
  91.    { Test 4. }
  92.     Print ('Screen -> Locked -> Screen (%.1f fps)', [getfps(3)]);
  93.     getxy(x, y);
  94.     al_draw_bitmap(Pattern, x, y, 0);
  95.  
  96.  
  97.     StartTimer (3);
  98.     ix := round(x);
  99.     iy := round(y);
  100.     lock := al_lock_bitmap_region(Screen, ix, iy, iw, ih,
  101.       ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY);
  102.     FormatLock := ALLEGRO_PIXEL_FORMAT(lock.format);
  103.     FormatSize := lock.pixel_size;
  104.     data := AllocMem(FormatSize * iw * ih);
  105.     for i := 0 to ih -1 do
  106.        move(PByteArray(lock.data)[ + i * lock.pitch],
  107.        data[ + i * FormatSize * iw],
  108.  
  109.  
  110.        FormatSize * iw
  111.          );
  112.  
  113.  
  114.     al_unlock_bitmap(Screen);
  115.  
  116.  
  117.    lock := al_lock_bitmap_region(Screen, ix + 8 + iw, iy, iw, ih, FormatLock,
  118.       ALLEGRO_LOCK_WRITEONLY);
  119.    for i := 0 to  ih-1 do
  120.       move(data[ + i * FormatSize * iw],
  121.          PByteArray(lock.data)[ + i * lock.pitch],
  122.          FormatSize * iw);
  123.    al_unlock_bitmap(Screen);
  124.    freeMem(data);
  125.    StopTimer (3);
  126.     SetXY (x, y + ih);
  127.  
  128.   END;


-- I am sorry for the bad lines indentation
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Allegro.pas
« Reply #53 on: November 09, 2020, 07:27:08 pm »
Nice.  I'll take a look.

[Edit] Tested but it throws a Range check error.

Tempted to upload it as a branch at SourceForge's Subversion or at GitHub. Btw, if you use GitHub you can fork it and later I can merge (or do a pull request, I'm not sure if it's the same thing).

Anyway I see what I was doing wrong:  The move procedure have the parameters in different order than memcpy.  Thanks for the tip.
« Last Edit: November 09, 2020, 08:02:17 pm by Ñuño_Martínez »
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Allegro.pas
« Reply #54 on: November 24, 2020, 07:53:55 am »
hi! with Range (-Cr) debug option checked, the error is by type array insufficient:
Code: Pascal  [Select][+][-]
  1. TByteArray = array[Word] of byte;


now, by giving larger array range, the error has gone:
Code: Pascal  [Select][+][-]
  1. TByteArray = array [0..MaxInt-1] of byte;


Something else is same as before (except the indent lines for beautify purpose)
the complete code:
Code: Pascal  [Select][+][-]
  1. PROCEDURE Draw;
  2.   type
  3.   TByteArray = array [0..MaxInt-1] of byte;
  4.   PByteArray = ^TByteArray;
  5.   VAR
  6.     x, y: SINGLE;
  7.     i,ix,iy,iw, ih , FormatSize : INTEGER;
  8.     FormatLock: ALLEGRO_PIXEL_FORMAT;
  9.     Screen, Temp: ALLEGRO_BITMAPptr;
  10.       Lock: ALLEGRO_LOCKED_REGIONptr;
  11.     Data: PByteArray;
  12.   BEGIN
  13.     iw := al_get_bitmap_width (Pattern);
  14.     ih := al_get_bitmap_height (Pattern);
  15.     al_set_blender (ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO);
  16.     al_clear_to_color (Background);
  17.     Screen := al_get_target_bitmap;
  18.  
  19.  
  20.     SetXY (8, 8);
  21.  
  22.  
  23.   { Test 1. }
  24.   { /* Disabled: drawing to same bitmap is not supported. }
  25.   (*
  26.     Print ('Screen -> Screen (%.1f fps)', get_fps(0));
  27.     get_xy(&x, &y);
  28.     al_draw_bitmap(ex.Pattern, x, y, 0);
  29.  
  30.  
  31.     start_timer(0);
  32.     al_draw_bitmap_region(screen, x, y, iw, ih, x + 8 + iw, y, 0);
  33.     stop_timer(0);
  34.     SetXY (x, y + ih);
  35.    *)
  36.  
  37.  
  38.   { Test 2. }
  39.     Print ('Screen -> Bitmap -> Screen (%.1f fps)', [GetFPS (1)]);
  40.     GetXY (x, y);
  41.     al_draw_bitmap (Pattern, x, y, 0);
  42.  
  43.  
  44.     Temp := al_create_bitmap (iw, ih);
  45.     al_set_target_bitmap (Temp);
  46.     al_clear_to_color (Red);
  47.     StartTimer (1);
  48.     al_draw_bitmap_region (Screen, x, y, iw, ih, 0, 0, 0);
  49.  
  50.  
  51.     al_set_target_bitmap (Screen);
  52.     al_draw_bitmap (Temp, x + 8 + iw, y, 0);
  53.     StopTimer (1);
  54.     SetXY (x, y + ih);
  55.  
  56.  
  57.     al_destroy_bitmap (Temp);
  58.  
  59.  
  60.   { Test 3. }
  61.     Print ('Screen -> Memory -> Screen (%.1f fps)', [getfps (2)]);
  62.     GetXY (x, y);
  63.     al_draw_bitmap (Pattern, x, y, 0);
  64.  
  65.  
  66.     al_set_new_bitmap_flags (ALLEGRO_MEMORY_BITMAP);
  67.     Temp := al_create_bitmap (iw, ih);
  68.     al_set_target_bitmap (Temp);
  69.     al_clear_to_color (Red);
  70.     StartTimer (2);
  71.     al_draw_bitmap_region (Screen, x, y, iw, ih, 0, 0, 0);
  72.  
  73.  
  74.     al_set_target_bitmap (Screen);
  75.     al_draw_bitmap (Temp, x + 8 + iw, y, 0);
  76.     StopTimer (2);
  77.     SetXY (x, y + ih);
  78.  
  79.  
  80.     al_destroy_bitmap (Temp);
  81.     al_set_new_bitmap_flags (ALLEGRO_VIDEO_BITMAP);
  82.  
  83.  
  84.  
  85.  
  86. (*
  87.   Disabled because the "memcpy".  I tried it but I never used 'memcpy' on C nor
  88.   pointers on Pascal so I don't know how to translate it. *)
  89.  
  90.  
  91.    { Test 4. }
  92.     Print ('Screen -> Locked -> Screen (%.1f fps)', [getfps(3)]);
  93.     getxy(x, y);
  94.     al_draw_bitmap(Pattern, x, y, 0);
  95.  
  96.  
  97.     StartTimer (3);
  98.     ix := round(x);
  99.     iy := round(y);
  100.     lock := al_lock_bitmap_region(Screen, ix, iy, iw, ih,
  101.       ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY);
  102.     FormatLock := ALLEGRO_PIXEL_FORMAT(lock.format);
  103.     FormatSize := lock.pixel_size;
  104.     data := AllocMem(FormatSize * iw * ih);
  105.     for i := 0 to ih -1 do
  106.        move(
  107.           PByteArray(lock.data)[ i * lock.pitch],
  108.           data[ i * FormatSize * iw],
  109.           FormatSize * iw
  110.        );
  111.  
  112.  
  113.     al_unlock_bitmap(Screen);
  114.  
  115.  
  116.    lock := al_lock_bitmap_region(Screen, ix + 8 + iw, iy, iw, ih, FormatLock,
  117.       ALLEGRO_LOCK_WRITEONLY);
  118.    for i := 0 to  ih-1 do
  119.       move(
  120.          data[ i * FormatSize * iw],
  121.          PByteArray(lock.data)[ i * lock.pitch],
  122.          FormatSize * iw
  123.       );
  124.  
  125.  
  126.    al_unlock_bitmap(Screen);
  127.    freeMem(data);
  128.    StopTimer (3);
  129.    SetXY (x, y + ih);
  130.  
  131.  
  132.   END;
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Allegro.pas
« Reply #55 on: December 08, 2020, 01:13:05 pm »
Sorry for the time without answering, but I had a bad month.

That wasn't the problem.  The problem is that my graphics card stores the textures upside-down, so pitch is negative, so results in a negative index witch is out of range (all indexes must be positive).  Fix is to use a pointer instead of array, since the Lock.data points to the top-left corner of the bitmap (doesn't matter if it stored upside-up or -down).

I've commit the fix, including credits to your work (if you want to change it, let me know).
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

x2nie

  • Hero Member
  • *****
  • Posts: 515
  • Impossible=I don't know the way
    • impossible is nothing - www.x2nie.com
Re: Allegro.pas
« Reply #56 on: December 12, 2020, 03:06:39 pm »
Hi Nuno,
Your modification doesn't work.  >:D  (I have checkout the svn trunk repository).
at least didn't properly work on my machine.
** But, thanks for including my name in credit.  8-) Thats great, perhaps someday my son read it. :-X
--------------------------


However, after tried several scenario, the solution is quite simple....

The error was runtime "SIGSENV" without Lazarus prefer into which line of code the problem is being executed.
Well, years ago I have had bad day experience when porting C code into pascal, especially in copying array of bytes with pointer addressing.
-
I wonder how the code is working well in your side:
Code: Pascal  [Select][+][-]
  1. move (
  2.         ScreenData,
  3.         BitmapData[i * FormatSize * iw],
  4.         FormatSize * iw
  5.       )


but the only working code in my Lazarus Win10 32bit is by make the first array explicitly as the pointer address. like this:
Code: Pascal  [Select][+][-]
  1. move (
  2.         ScreenData[0],
  3.         BitmapData[i * FormatSize * iw],
  4.         FormatSize * iw
  5.       )



Now, the complete code (partially) is :
Code: Pascal  [Select][+][-]
  1. { Test 4. }
  2.     Print ('Screen -> Locked -> Screen (%.1f fps)', [GetFPS(3)]);
  3.     GetXY (x, y);
  4.     al_draw_bitmap (Pattern, x, y, 0);
  5.  
  6.  
  7.     StartTimer (3);
  8.  
  9.  
  10.     ix := Round (x); iy := Round (y);
  11.     Lock := al_lock_bitmap_region (
  12.       Screen,
  13.       ix, iy, iw, ih,
  14.       ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY
  15.     );
  16.     FormatLock := ALLEGRO_PIXEL_FORMAT (Lock.format);
  17.     FormatSize := Lock.pixel_size;
  18.     BitmapData := AllocMem (FormatSize * iw * ih);
  19.     FOR i := 0 TO ih - 1 DO
  20.     BEGIN
  21.     { Lock.pitch may be negative (i.e. graphics card stores textures upside down)
  22.       so indexes can't be used here as only positive values can be used.
  23.       So use pointers.
  24.     }
  25.       ScreenData := Lock.Data + (i * Lock.pitch);
  26.       move (
  27.         ScreenData[0],
  28.         BitmapData[i * FormatSize * iw],
  29.         FormatSize * iw
  30.       )
  31.     END;
  32.     al_unlock_bitmap (Screen);
  33.  
  34.  
  35.     Lock := al_lock_bitmap_region (
  36.       Screen,
  37.       ix + 8 + iw, iy, iw, ih,
  38.       FormatLock, ALLEGRO_LOCK_WRITEONLY
  39.     );
  40.     FOR i := 0 TO ih - 1 DO
  41.     BEGIN
  42.       ScreenData := Lock.Data + (i * Lock.pitch);
  43.       move (
  44.         BitmapData[i * FormatSize * iw],
  45.         ScreenData[0],
  46.         FormatSize * iw
  47.       )
  48.     END;
  49.     al_unlock_bitmap (Screen);
  50.     FreeMem (BitmapData);
  51.  
  52.  
  53.     StopTimer (3);    


It works fine with all valgrind,I/O, Range Check,Stack & Overflow assertions set on.
---------


Anyway, now I knew the reason of why the copying bitmap data is done in a loop (like the ex_blit.c does too), since we can also to copy the whole data at glance. That it because in some graphic card the data is upside down.
Thanks you
When you were logged in, you can see attachments.
Lazarus Github @ UbuntuCinnamon-v22.04.1 + LinuxMintDebianEdition5

Ñuño_Martínez

  • Hero Member
  • *****
  • Posts: 1186
    • Burdjia
Re: Allegro.pas
« Reply #57 on: December 13, 2020, 12:57:38 pm »
Your new fix works for me as well. :) Now I have to test it on Delphi (first I have to upgrade my license %))

I'm curious what's your system.  Mine is Xubuntu/Linux 20.04.1, fpc 3.2.0.
Are you interested in game programming? Join the Pascal Game Development community!
Also visit the Game Development Portal

 

TinyPortal © 2005-2018