ZenGL Tutorial/fr

From Lazarus wiki
Revision as of 08:49, 8 December 2016 by E-ric (talk | contribs) (Created page with "{{ZenGL Tutorial}} {{ZenGL Tutorial Index}} == Téléchargement == Vous pouvez obtenir ZenGL pour Linux, Windows et Mac depuis [http://zengl.org/download.html...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Deutsch (de) English (en) español (es) français (fr) русский (ru)


ZenGL | Tutorial 1 | Tutorial 2 | Tutorial 3 | Edit

Téléchargement

Vous pouvez obtenir ZenGL pour Linux, Windows et Mac depuis la page d'accueil ZenGL.

Installation

Vous pouvez extraire l'archive compressée téléchargée avec un utulitaire tel que 7-Zip dans un dossier de votre choix.

Source Path

Avant d'utiliser un des modules, assurez-vous d'avoir défini le chemin correct vers les sources de ZenGL.

Aller vers "Project > Project Options". Dans "Compiler Options > Paths" et vous pouvez les ajouter dans "Other Unit Files" :

headers
extra
src
src\Direct3D
lib\jpeg\$(TargetCPU)-$(TargetOS)
lib\msvcrt\$(TargetCPU)
lib\ogg\$(TargetCPU)-$(TargetOS)
lib\zlib\$(TargetCPU)-$(TargetOS)

Compilation

Une application peut être compilée statiquement ou avec un .so/.dll/.dylib.

Approfondissez en lisant la compilation de ZenGL de le Wiki ZenGL.

Compilation statique de ZenGL

L'avantage de la compilation statique est une taille plus réduite de votre application, mais cela demande d'inclure toutes les unités.

{$DEFINE STATIC}

ZenGL en utilisant un .so/.dll/.dylib

L'utilisation d'un .so/.dll/.dylib ne vous demande pas d'ouvrir les sources de votre application. Pour faire cela, supprimer ou mettre en commentaire la directive de compilation $DEFINE STATIC. Vous avez aussi besoin de compiler la bibliothèque ZenGL.

//{$DEFINE STATIC}

dll Windows

 * * * A  F I N I R  * * * 

Open "src\Lazarus\ZenGL.lpi" then go to "Run > Compile (Ctrl + F9)".

Then in the directory "src\" you should see the file "ZenGL.dll", copy and paste it in the folder "bin\i386" where all the demo binaries are compiled. You always must copy the libraries in your program output directory if you are using the .dll.

Now you can compile the demos, commenting out the $DEFINE STATIC.

Other .dll files in the "bin\" folder you can use are: chipmunk.dll ; libogg-0.dll ; libvorbis-0.dll ; libvorbis-3.dll

First Program

This is the first demo program included with ZenGL. First create a new "Free Pascal Program". Add the Source Path as described before.

You must also change the Syntax Mode in Project > Project Options > Compiler Options > Parsing to Delphi (-Mdelphi).

Remember, if you are using a .so/.dll/.dylib copy the library binaries to the output folder of your program.

Program title, add resources:

program demo01;

{$R *.res}

Define compilation mode (comment out to use .so/.dll/.dylib):

{$DEFINE STATIC}

This adds the ZenGL units:

uses
  {$IFNDEF STATIC}
  zglHeader
  {$ELSE}
  zgl_main,
  zgl_screen,
  zgl_window,
  zgl_timers,
  zgl_utils
  {$ENDIF}
  ;

Variables like in a standard pascal program:

var
  DirApp  : String;
  DirHome : String;

Procedures, add your code here:

procedure Init;
begin
  // Here you can load the main resources.
end;

procedure Draw;
begin
  // Here you can "draw" anything.
end;

procedure Update( dt : Double );
begin
  // This function is the best way to implement smooth moving of something, because timers are restricted by FPS.
end;

procedure Timer;
begin
  // This caption will show the frames per second.
  wnd_SetCaption( '01 - Initialization[ FPS: ' + u_IntToStr( zgl_Get( RENDER_FPS ) ) + ' ]' );
end;

procedure Quit;
begin
 //
end;

The program starts here:

Begin
  {$IFNDEF STATIC}
  zglLoad( libZenGL );
  {$ENDIF}
  // For loading/creating your own options/profiles/etc. you can get the path to the user home
  // directory, or to the executable file (does not work on GNU/Linux).
  DirApp  := u_CopyStr( PChar( zgl_Get( DIRECTORY_APPLICATION ) ) );
  DirHome := u_CopyStr( PChar( zgl_Get( DIRECTORY_HOME ) ) );

  // Create a timer with an interval of 1000ms.
  timer_Add( @Timer, 1000 );

  // Register the procedure that will be executed after ZenGL initialization.
  zgl_Reg( SYS_LOAD, @Init );
  // Register the render procedure.
  zgl_Reg( SYS_DRAW, @Draw );
  // Register the procedure that will get the delta time between the frames.
  zgl_Reg( SYS_UPDATE, @Update );
  // Register the procedure that will be called after ZenGL shuts down.
  zgl_Reg( SYS_EXIT, @Quit );
  
  // Enable usage of UTF-8, because this unit saved in UTF-8 encoding and here used
  // string variables.
  zgl_Enable( APP_USE_UTF8 );

  // Set the caption of the window.
  wnd_SetCaption( '01 - Initialization' );

  // Show the mouse cursor.
  wnd_ShowCursor( TRUE );

  // Set screen options.
  scr_SetOptions( 800, 600, REFRESH_MAXIMUM, FALSE, FALSE );

  // Initialize ZenGL.
  zgl_Init();
End.

Code résultant

Le résultat est une modèle pour les projets ZenGL :

program template;

{$DEFINE STATIC}

{$R *.res}

uses
  {$IFNDEF STATIC}
  zglHeader
  {$ELSE}
  zgl_main,
  zgl_screen,
  zgl_window,
  zgl_timers,
  zgl_utils
  {$ENDIF}
  ;

var
  DirApp  : String; DirHome : String;

procedure Init;
begin
end;

procedure Draw;
begin
end;

procedure Update( dt : Double );
begin
end;

procedure Timer;
begin
  wnd_SetCaption( '01 - Initialization[ FPS: ' + u_IntToStr( zgl_Get( RENDER_FPS ) ) + ' ]' );
end;

procedure Quit;
begin
end;

Begin
  {$IFNDEF STATIC}
  zglLoad( libZenGL );
  {$ENDIF}
  DirApp  := u_CopyStr( PChar( zgl_Get( DIRECTORY_APPLICATION ) ) );
  DirHome := u_CopyStr( PChar( zgl_Get( DIRECTORY_HOME ) ) );
  timer_Add( @Timer, 1000 );
  zgl_Reg( SYS_LOAD, @Init );
  zgl_Reg( SYS_DRAW, @Draw );
  zgl_Reg( SYS_UPDATE, @Update );
  zgl_Reg( SYS_EXIT, @Quit );
  zgl_Enable( APP_USE_UTF8 );
  wnd_SetCaption( '01 - Initialization' );
  wnd_ShowCursor( TRUE );
  scr_SetOptions( 800, 600, REFRESH_MAXIMUM, FALSE, FALSE );
  zgl_Init();
End.