Recent

Author Topic: Substitution for HDC and HGLRC in Linux  (Read 4536 times)

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Substitution for HDC and HGLRC in Linux
« on: March 11, 2018, 04:32:13 pm »
I try to convert some programs from CorpsmanDE from Delphi to Lazarus in Linux
Opensuse Linux 42.3 Lazarus 1.8.2
I have the following Code
Code: Pascal  [Select][+][-]
  1. Unit Unit1;
  2.  
  3. {$MODE Delphi}
  4.  
  5. Interface
  6.  
  7. Uses
  8.   LCLIntf, LCLType, LMessages, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  9.   ExtCtrls, dglopengl, Cubes, StdCtrls, GLU,GL;
  10.  
  11. Type
  12.   TForm1 = Class(TForm)
  13.     Panel1: TPanel;
  14.     Timer1: TTimer;
  15.     Animate_timer: TTimer;
  16.     CheckBox1: TCheckBox;
  17.     Procedure FormCreate(Sender: TObject);
  18.     Procedure FormDestroy(Sender: TObject);
  19.     Procedure FormResize(Sender: TObject);
  20.     Procedure FormKeyDown(Sender: TObject; Var Key: Word;
  21.       Shift: TShiftState);
  22.     Procedure Timer1Timer(Sender: TObject);
  23.     Procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
  24.       Y: Integer);
  25.     Procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
  26.       Shift: TShiftState; X, Y: Integer);
  27.     Procedure Animate_timerTimer(Sender: TObject);
  28.     Procedure Panel1MouseUp(Sender: TObject; Button: TMouseButton;
  29.       Shift: TShiftState; X, Y: Integer);
  30.     Procedure CheckBox1Click(Sender: TObject);
  31.   private
  32.     { Private-Deklarationen }
  33.   public
  34.     { Public-Deklarationen }
  35.     // The Render Function
  36.     Procedure Render;
  37.   End;
  38.  
  39. Var
  40.   ScreenWidth: integer = 640;
  41.   ScreenHeight: Integer = 480;
  42.  
  43.   Form1: TForm1;
  44.   // OpenGL Handles
  45.   DC: HDC; //Handle auf Zeichenfläche
  46.   RC: HGLRC;
  47.   Initialized: Boolean = False;
  48.   // The Cube that should be rotated
  49.   blub: TBasicCube;
  50.   // save the old mouse coords
  51.   oox, ooy, ox, oy: Integer;
  52.   lb, lbo: Boolean;
  53. Implementation
  54.                    

What can I use in Linux instead of HDC and HGLRC?.
As far as I understand it, those handles exist only in Windows
Thanks again

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Substitution for HDC and HGLRC in Linux
« Reply #1 on: March 11, 2018, 05:23:25 pm »
For both you can use (system.)THandle. THandle is weakly typed.
In trunk you can now use OpaquePointer, which is more correct. OpaquePointer is strongly typed.(And types that derive from it too)
In this case it does not matter too much. Old code can still use system.THandle.
« Last Edit: March 11, 2018, 05:30:59 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Substitution for HDC and HGLRC in Linux
« Reply #2 on: March 11, 2018, 05:42:18 pm »
Thanks,I will use it

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Substitution for HDC and HGLRC in Linux
« Reply #3 on: March 11, 2018, 06:40:10 pm »
The substitution works,but I still have some questions
Code: Pascal  [Select][+][-]
  1. Procedure Tform1.Render;
  2. Begin
  3.   If Initialized Then Begin
  4.     // Clearscreen
  5.     glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT);
  6.     glViewport(0, 0, panel1.width, panel1.height);
  7.     glMatrixMode(GL_MODELVIEW);
  8.     glLoadIdentity;
  9.     gluLookAt(0, 0, -10, 0, 0, 0, 0, 1, 0);
  10.     // Render the Cube
  11.     blub.render;
  12.  
  13.     SwapBuffers(dc);
  14.   End;
  15. End;
  16.  
  17. Procedure TForm1.FormCreate(Sender: TObject);
  18. Const
  19.   Lambient: Array[0..3] Of Single = (0.75, 0.75, 0.75, 1);
  20.   Lpos: Array[0..3] Of Single = (1, 10, 0.5, 1);
  21. Begin
  22.   lb := false;
  23.   lbo := False;
  24.   // Setup OpenGL
  25.   DC := GetDC(panel1.Handle);
  26.   RC := CreateRenderingContext(DC, [opDoubleBuffered], 32, 24, 0, 0, 0, 0);
  27.   ActivateRenderingContext(DC, RC);
  28.   glEnable(GL_CULL_FACE);
  29.   glCullFace(GL_Back);
  30.   // Enable Depth Test
  31.   glEnable(GL_DEPTH_TEST);
  32.   // Set Detph Function
  33.   glDepthFunc(GL_LEQUAL);
  34.   // Set Clear Color = black
  35.   glClearColor(0, 0, 0, 0);
  36.   // Disable Rendering via VCL for Panel1
  37.   Panel1.FullRepaint := false;
  38.   // Set the OpenGLpanel to Full window size.
  39.   Panel1.Align := alClient;
  40.  
  41.   // add some light
  42.   glLightfv(GL_LIGHT0, GL_AMBIENT, @Lambient);
  43.   glLightfv(GL_LIGHT0, GL_POSITION, @Lpos);
  44.   glEnable(GL_LIGHT0);
  45.   glEnable(gl_lighting);
  46.  
  47.   blub := TBasicCube.create;
  48.   // Resize Window
  49.   OnResize(Nil);
  50.   // Set OpenGL initialized
  51.   Initialized := true;
  52. End;
  53.  
  54. Procedure TForm1.FormDestroy(Sender: TObject);
  55. Begin
  56.   // Free OpenGL
  57.   Initialized := false;
  58.   blub.free;
  59.   DeactivateRenderingContext;
  60.   DestroyRenderingContext(RC);
  61.   ReleaseDC(panel1.Handle, DC);
  62. End;
  63.  
I reckon the followig procedures are connected to Open Gl commands

SwapBuffers(dc);   
RC := CreateRenderingContext(DC, [opDoubleBuffered], 32, 24, 0, 0, 0, 0);
  ActivateRenderingContext(DC, RC); 
DeactivateRenderingContext;
  DestroyRenderingContext(RC);
  ReleaseDC(panel1.Handle, DC); 
Where can I find out what they do?
What can I use in Linux to substitute them?
Thanks again

soerensen3

  • Full Member
  • ***
  • Posts: 213
Re: Substitution for HDC and HGLRC in Linux
« Reply #4 on: March 12, 2018, 12:18:19 am »
I have no idea (I use SDL2 for a context and not the LCL), but please note the laz_opengl package which you can get for example through the online package manager. It contains the dglHeader and it has an OpenGL Panel. This could spare you some work or you could look how they did the context creation. As far as I know there is no cross platform way to get a context. You need to use ifdef's and treat each platform separately. This is why I would suggest using a ready lcl control. I have to add that I didn't test it myself, though.
Lazarus 1.9 with FPC 3.0.4
Target: Manjaro Linux 64 Bit (4.9.68-1-MANJARO)

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Substitution for HDC and HGLRC in Linux
« Reply #5 on: March 12, 2018, 08:10:04 am »
Thanks again

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: Substitution for HDC and HGLRC in Linux
« Reply #6 on: March 12, 2018, 10:48:50 am »
As far as I know there is no cross platform way to get a context.
That's not true. In trunk there is a cross platform way. I explained that before (as did Maciej a.k.a. HNB).
The case is the driver has a record that it maintains by itself. For FPC it is sufficient to know that is it is a record being passed around.
This was usually solved by using an untyped pointer (or type "pointer"). I fixed that to point to a structure, so the compiler knows it is a structure (record, class, object).
Introduced is:
OpaquePointer = type ^TEmptyRecord;
TEmpyRecord = record end;

Note the record is not really empty, it is opaque, because you do not access it directly. You pass it around.
Hence you can now obtain a context in a cross-platform way.
The context is solely handled by the underlying library, but by using OpaquePointer as pointer type it is now type safe.
In C(++) this is resolved in a much less elegant way. using macro's.

I can elaborate on this if you do not fully understand this. Basically OpaquePointer is a new root type (like untyped pointer, but now fully types) which you can use to create things like so:
Code: Pascal  [Select][+][-]
  1.  type TContext = type OpaquePointer; // compiler now knows....It is not just a raw pointer

Which takes most difficulties out of making such a pointer to a structure cross-platform. After all: it is the library that needs the structure, not your own code....
Examples are still rare, but expect them to be multiple soon. E.g. upcoming new headers for OpenGL, EGL, OpenVG, loads of library interfaces. And mOrmOt uses it already! And the latest rtl-generics updates from this week.
In the case of OpenGLES2, I could remove the current dependency on X in seconds..... And unify code for Unix and Windows platforms by removing a lot of ifdef's.
« Last Edit: March 12, 2018, 11:55:14 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

jcaser1948

  • Jr. Member
  • **
  • Posts: 68
Re: Substitution for HDC and HGLRC in Linux
« Reply #7 on: March 14, 2018, 08:27:58 am »
A very interesting contribution.Thanks,Thaddy

 

TinyPortal © 2005-2018