Recent

Author Topic: [LAMW] Draw Rectangle using jCanvasES2  (Read 264 times)

Handoko

  • Hero Member
  • *****
  • Posts: 2900
  • My goal: build my own game engine using Lazarus
[LAMW] Draw Rectangle using jCanvasES2
« on: March 18, 2019, 06:18:42 pm »
Someone asked me how to draw rectangle using jCanvasES2. Instead to answer him privately, I share it here so more users can learn it.

Code: Pascal  [Select]
  1. {Hint: save all files to location: /home/handoko/Desktop/GLES_Rectangle/LamwGUIProject1/jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, AndroidWidget, Laz_And_Controls, seekbar,
  10.   Laz_And_GLESv2_Canvas;
  11.  
  12. type
  13.  
  14.   { TAndroidModule1 }
  15.  
  16.   TAndroidModule1 = class(jForm)
  17.     jCanvasES2_1: jCanvasES2;
  18.     jSeekBar1: jSeekBar;
  19.     jTextView1: jTextView;
  20.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  21.     procedure jCanvasES2_1GLCreate(Sender: TObject);
  22.     procedure jCanvasES2_1GLDraw(Sender: TObject);
  23.     procedure jSeekBar1ProgressChanged(Sender: TObject; progress: integer;
  24.       fromUser: boolean);
  25.   private
  26.     {private declarations}
  27.   public
  28.     {public declarations}
  29.   end;
  30.  
  31. var
  32.   AndroidModule1: TAndroidModule1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38.  
  39. { TAndroidModule1 }
  40.  
  41. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  42. begin
  43.   jCanvasES2_1.Screen_Setup(jCanvasES2_1.Width, jCanvasES2_1.Height, xp2D);
  44.   jCanvasES2_1.Refresh;
  45. end;
  46.  
  47. procedure TAndroidModule1.jCanvasES2_1GLCreate(Sender: TObject);
  48. begin
  49.   jCanvasES2_1.Shader_Compile('simon_Vert', 'simon_Frag');
  50.   jCanvasES2_1.Shader_Link;
  51. end;
  52.  
  53. procedure TAndroidModule1.jCanvasES2_1GLDraw(Sender: TObject);
  54. var
  55.   XYs  : TXYs;
  56.   Size : Single;
  57. begin
  58.  
  59.   jCanvasES2_1.MVP:= cID4x4;
  60.   jCanvasES2_1.SetMVP(jCanvasES2_1.MVP);
  61.  
  62.   // Clear screen
  63.   XYs.Cnt   := 4;
  64.   XYs.Pt[0] := _XY(-1, -1);
  65.   XYs.Pt[1] := _XY(-1,  1);
  66.   XYs.Pt[2] := _XY( 1,  1);
  67.   XYs.Pt[3] := _XY( 1, -1);
  68.   jCanvasES2_1.DrawPolyFill(XYs, 0, _RGBA(0, 0, 0, 1), _RGBA(0, 0, 0, 1), 0);
  69.  
  70.   // Draw the rectangle
  71.   Size := jSeekBar1.Progress/100;
  72.   XYs.Pt[0] := _XY(-Size, -Size);
  73.   XYs.Pt[1] := _XY(-Size,  Size);
  74.   XYs.Pt[2] := _XY( Size,  Size);
  75.   XYs.Pt[3] := _XY( Size, -Size);
  76.   jCanvasES2_1.DrawPolyFill(XYs, 0, _RGBA(0.5, 1, 0.8, 1), _RGBA(0, 0, 0, 1), 0);
  77.  
  78. end;
  79.  
  80. procedure TAndroidModule1.jSeekBar1ProgressChanged(Sender: TObject;
  81.   progress: integer; fromUser: boolean);
  82. begin
  83.   jCanvasES2_1.Refresh;
  84. end;
  85.  
  86. end.

  • jCanvasES2.DrawRect is buggy. The better alternative: DrawPoly or DrawPolyFill.
  • jCanvasES2 is buggy too. I know the secret to make it crash in less than 10 second.  :D
How to make jCanvasES2 crash?
  • Draw some visible object on jCanvasES2. (jCanvasES2 won't crash if there is no any visible object on it)
  • Use your fingers (preferable 8 fingers) to tap the jCanvasES2 very quicky repeatedly. I said very quickly.
  • It will be sure to crash, usually in less than 10 seconds (depends on how quick you tapping it).
Tested on Android 7.1.2 ARM device, binary compiled using Lazarus 1.8.4 64-bit Linux LAMW rev 754.
« Last Edit: March 18, 2019, 06:32:34 pm by Handoko »

Handoko

  • Hero Member
  • *****
  • Posts: 2900
  • My goal: build my own game engine using Lazarus
Re: [LAMW] Draw Rectangle using jCanvasES2
« Reply #1 on: March 18, 2019, 06:20:15 pm »
This is the apk:

karika

  • New member
  • *
  • Posts: 20
Re: [LAMW] Draw Rectangle using jCanvasES2
« Reply #2 on: March 21, 2019, 08:41:18 pm »
Thank you so much
I have benefited a lot from this

karika

  • New member
  • *
  • Posts: 20
Re: [LAMW] Draw Rectangle using jCanvasES2
« Reply #3 on: March 21, 2019, 08:46:27 pm »
I tried to draw a circle but the application did not work
Code: Pascal  [Select]
  1. {Hint: save all files to location: C:\Users\hamada\Desktop\New folder (5)\AppLAMWProject10\jni }
  2. unit unit1;
  3.  
  4. {$mode delphi}
  5.  
  6. interface
  7.  
  8. uses
  9.   Classes, SysUtils, AndroidWidget, Laz_And_Controls, seekbar,
  10.   Laz_And_GLESv2_Canvas;
  11.  
  12. type
  13.  
  14.   { TAndroidModule1 }
  15.  
  16.   TAndroidModule1 = class(jForm)
  17.     jCanvasES2_1: jCanvasES2;
  18.     jSeekBar1: jSeekBar;
  19.     jTextView1: jTextView;
  20.     procedure AndroidModule1JNIPrompt(Sender: TObject);
  21.     procedure jCanvasES2_1GLCreate(Sender: TObject);
  22.     procedure jCanvasES2_1GLDraw(Sender: TObject);
  23.     procedure jSeekBar1ProgressChanged(Sender: TObject; progress: integer;
  24.       fromUser: boolean);
  25.   private
  26.     {private declarations}
  27.   public
  28.     {public declarations}
  29.   end;
  30.  
  31. var
  32.   AndroidModule1: TAndroidModule1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38.  
  39. { TAndroidModule1 }
  40.  
  41. procedure TAndroidModule1.jCanvasES2_1GLDraw(Sender: TObject);
  42. var
  43.   XY:TXY;
  44.  begin
  45.  
  46.  jCanvasES2_1.MVP:= cID4x4;
  47.  jCanvasES2_1.SetMVP(jCanvasES2_1.MVP);
  48.  
  49.  // Draw the Circle
  50.   XY.X:=4;xy.Y:=4;
  51.   jCanvasES2_1.DrawCircle(XY,0,0,_RGBA(0, 0, 0, 1),0.3);
  52.  
  53.  end;
  54.  
  55. procedure TAndroidModule1.jSeekBar1ProgressChanged(Sender: TObject;
  56.   progress: integer; fromUser: boolean);
  57. begin
  58.  
  59. end;
  60.  
  61. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  62. begin
  63.     jCanvasES2_1.Screen_Setup(jCanvasES2_1.Width, jCanvasES2_1.Height, xp2D);
  64.   jCanvasES2_1.Refresh;
  65. end;
  66.  
  67. procedure TAndroidModule1.jCanvasES2_1GLCreate(Sender: TObject);
  68. begin
  69.     jCanvasES2_1.Shader_Compile('simon_Vert', 'simon_Frag');
  70.   jCanvasES2_1.Shader_Link;
  71. end;
  72.  
  73. end.
  74.  

Handoko

  • Hero Member
  • *****
  • Posts: 2900
  • My goal: build my own game engine using Lazarus
Re: [LAMW] Draw Rectangle using jCanvasES2
« Reply #4 on: March 21, 2019, 09:15:50 pm »
I'm preparing to visit a nearby town for several days so I can't write the demo for you soon. Please wait patiently.

Or maybe someone can write an example of drawing a circle using jCanvasES2.

karika

  • New member
  • *
  • Posts: 20
Re: [LAMW] Draw Rectangle using jCanvasES2
« Reply #5 on: March 21, 2019, 09:35:25 pm »
ok I wish you success