Italian community of Lazarus and Free Pascal

Programmazione => Graphics => Topic aperto da: Stilgar - Maggio 06, 2012, 08:16:35 pm

Titolo: ZenGL: Astronave Kamikaze ;)
Inserito da: Stilgar - Maggio 06, 2012, 08:16:35 pm
L'astronave usata come arma ;)

Codice: [Seleziona]
program demo05;
{$D+}
{$IFDEF WINDOWS}
  {$R *.res}
{$ENDIF}
{$DEFINE STATIC}

uses
  zgl_main,
  zgl_screen,
  zgl_window,
  zgl_timers,
  zgl_keyboard,
  zgl_joystick,
  zgl_camera_2d,
  zgl_render_2d,
  zgl_fx,
  zgl_textures,
  zgl_textures_png,
  zgl_textures_jpg,
  zgl_sprite_2d,
  zgl_primitives_2d,
  zgl_font,
  zgl_text,
  zgl_math_2d,
  zgl_utils,
  zgl_collision_2d;

type
  TTux = record
    Texture : zglPTexture;
    Frame   : Integer;
    Pos     : zglTPoint2D;
    DeltaX  : single;
    DeltaY  : single;
    Killed  : Boolean;
end;

var
  dirRes      : String {$IFNDEF DARWIN} = '../data/' {$ENDIF};
  fntMain     : zglPFont;
  texLogo     : zglPTexture;
  texBack     : zglPTexture;
  texBack1    : zglPTexture;
  texGround   : zglPTexture;
  texTuxWalk  : zglPTexture;
  texTuxStand : zglPTexture;
  tux         : array[ 0..20 ] of TTux;
  nave : TTux;
  time        : Integer;
  camMain     : zglTCamera2D;
  back0_offset: Integer;
  back1_offset: Integer;
  fnt        : zglPFont;

procedure DrawDebugInfo_Joy;
//  var w : Single;
begin
  text_Draw( fnt, 100, 400, 'Axis X: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_X ) ) );
  text_Draw( fnt, 100, 420, 'Axis Y: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_Y ) ) );
  text_Draw( fnt, 100, 440, 'Axis Z: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_Z ) ) );
  text_Draw( fnt, 100, 460, 'Axis R: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_R ) ) );
  text_Draw( fnt, 100, 480, 'Axis U: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_U ) ) );
  text_Draw( fnt, 100, 500, 'Axis V: ' + u_FloatToStr( joy_AxisPos( 0, JOY_AXIS_V ) ) );
  text_Draw( fnt, 100, 520, 'POVX: ' + u_FloatToStr( joy_AxisPos( 0, JOY_POVX ) ) );
  text_Draw( fnt, 100, 540, 'POVY: ' + u_FloatToStr( joy_AxisPos( 0, JOY_POVY ) ) );

  text_Draw( fnt, 400, 400, 'Button1: ' + u_BoolToStr( joy_Down( 0, 0 ) ) );
  text_Draw( fnt, 400, 420, 'Button2: ' + u_BoolToStr( joy_Down( 0, 1 ) ) );
  text_Draw( fnt, 400, 440, 'Button3: ' + u_BoolToStr( joy_Down( 0, 2 ) ) );
  text_Draw( fnt, 400, 460, 'Button4: ' + u_BoolToStr( joy_Down( 0, 3 ) ) );
  text_Draw( fnt, 400, 480, 'Button5: ' + u_BoolToStr( joy_Down( 0, 4 ) ) );
  text_Draw( fnt, 400, 500, 'Button6: ' + u_BoolToStr( joy_Down( 0, 5 ) ) );
  text_Draw( fnt, 400, 520, 'Button7: ' + u_BoolToStr( joy_Down( 0, 6 ) ) );
  text_Draw( fnt, 400, 540, 'Button8: ' + u_BoolToStr( joy_Down( 0, 7 ) ) );
  text_Draw( fnt, 550, 400, 'Button9: ' + u_BoolToStr( joy_Down( 0, 8 ) ) );
  text_Draw( fnt, 550, 420, 'Button10: ' + u_BoolToStr( joy_Down( 0, 9 ) ) );
  text_Draw( fnt, 550, 440, 'Button11: ' + u_BoolToStr( joy_Down( 0, 10 ) ) );
  text_Draw( fnt, 550, 460, 'Button12: ' + u_BoolToStr( joy_Down( 0, 11 ) ) );
  text_Draw( fnt, 550, 480, 'Button13: ' + u_BoolToStr( joy_Down( 0, 12 ) ) );
  text_Draw( fnt, 550, 500, 'Button14: ' + u_BoolToStr( joy_Down( 0, 13 ) ) );
  text_Draw( fnt, 550, 520, 'Button15: ' + u_BoolToStr( joy_Down( 0, 14 ) ) );
  text_Draw( fnt, 550, 540, 'Button16: ' + u_BoolToStr( joy_Down( 0, 15 ) ) );
end;


procedure Init;
  var
    i : Integer;
begin
  camMain.Zoom.X := 1;
  camMain.Zoom.Y := 1;
  texLogo := tex_LoadFromFile( dirRes + 'LFIC.png', $FF000000, TEX_DEFAULT_2D );

  texBack  := tex_LoadFromFile( dirRes + 'Sfondo0.jpg' );
  texBack1 := tex_LoadFromFile( dirRes + 'Sfondo1.png' );
  texGround := tex_LoadFromFile( dirRes + 'ground.png' );
  tex_SetFrameSize( texGround, 32, 32 );
  texTuxWalk := tex_LoadFromFile( dirRes + 'Astronave1_move.png' );
  tex_SetFrameSize( texTuxWalk, 64, 64 );
  texTuxStand := tex_LoadFromFile( dirRes + 'Astronave4x4.png' );
  tex_SetFrameSize( texTuxStand, 64, 64 );

  for i := 0 to 9 do
    begin
      tux[ i ].Texture := texTuxWalk;
      tux[ i ].Frame   := random( 19 ) + 2;
      tux[ i ].Pos.X   := i * 96;
      tux[ i ].Pos.Y   := 130;
      tux[ i ].Killed:= false;
    end;
  for i := 10 to 20 do
    begin
      tux[ i ].Texture := texTuxWalk;
      tux[ i ].Frame   := random( 19 ) + 2;
      tux[ i ].Pos.X   := ( i - 9 ) * 96;
      tux[ i ].Pos.Y   := 325;
      tux[ i ].Killed:= false;
    end;
  nave.Texture := texTuxStand;
  nave.Frame   := random( 19 ) + 2;
  nave.Pos.X   := 400 - 32;
  nave.Pos.Y   := 300 - 64 - 4;
  nave.DeltaX  := 5;
  nave.DeltaY  := 5;
  nave.Killed:= false;
  fntMain := font_LoadFromFile( dirRes + 'font.zfi' );
  joy_Init();
  fnt := font_LoadFromFile( dirRes + 'font.zfi' );
end;

procedure Draw;
  var
    i : Integer;
    t : Single;
begin
  batch2d_Begin();
  if time > 255 Then
    begin
      zgl_Disable( COLOR_BUFFER_CLEAR );
      back0_offset:= (time div 10) mod 800;
      back1_offset:= (time div 3) mod 1600;

      ssprite2d_Draw( texBack, -back0_offset, 0, 1600, 600, 0 );
      ssprite2d_Draw( texBack1, -back1_offset, 400, 1600, 200, 0 );

      if back1_offset > 800 then
        ssprite2d_Draw( texBack1, -(back1_offset mod 800-800), 400, 1600, 200, 0 );
      text_Draw( fntMain, 0, 25, 'time: ' + u_IntToStr( time ) );
      text_Draw( fntMain, 0, 50, 'sfondo 0 offset: ' + u_IntToStr( back0_offset ) );
      text_Draw( fntMain, 0, 75, 'sfondo 1 offset: ' + u_IntToStr( back1_offset ) );
      cam2d_Set( @camMain );
      for i := 0 to 20 do
        begin
          if not tux[ i ].Killed then
          asprite2d_Draw( tux[ i ].Texture, tux[ i ].Pos.X, tux[ i ].Pos.Y, 64, 64, 0, tux[ i ].Frame div 2, 255, FX_BLEND );
        end;
        asprite2d_Draw( nave.Texture, nave.Pos.X, nave.Pos.Y, 64, 64, 0, nave.Frame div 2 );
    end;

  if time <= 255 Then
    ssprite2d_Draw( texLogo, 400 - 256, 300 - 128, 512, 256, 0, time )
  else
    if time < 510 Then
      begin
        ssprite2d_Draw( texLogo, 400 - 256, 300 - 128, 512, 256, 0, 510 - time );
      end;

  if time > 255 Then
    text_Draw( fntMain, 0, 0, 'FPS: ' + u_IntToStr( zgl_Get( RENDER_FPS ) ) );


  DrawDebugInfo_Joy;

  batch2d_End();
end;

function Intersect( const Rect1, Rect2 : zglTRect ) : Boolean;
var
  tw,th, rw,rh, tx,ty,rx,ry : double;
begin
  result:= false;
  tw := Rect1.H;
  th := Rect1.H;
  rw := Rect2.W;
  rh := Rect2.H;
       // controllo parametri errati
  if (rw <= 0) or (rh <= 0) or (tw <= 0) or (th <= 0) then
  begin
    exit;
  end;
  tx := Rect1.x;
  ty := Rect1.y;
  rx := Rect2.x;
  ry := Rect2.y;
  rw += rx;
  rh += ry;
  tw += tx;
  th += ty;
  result := ((rw < rx) or (rw > tx)) and
               ((rh < ry) or (rh > ty)) and
               ((tw < tx) or (tw > rx)) and
               ((th < ty) or (th > ry));
end;
  procedure Timer;
    var
      i : Integer;
      c1, c2:zglTRect;
  begin
    INC( time, 2 );
    for i := 0 to 20 do
      begin
        INC( tux[ i ].Frame );
        if tux[ i ].Frame > 20 Then
          tux[ i ].Frame := 2;
      end;
    inc(nave.Frame);
    if (nave.Frame > 20) then nave.Frame:= 2;
    for i := 0 to 20 do
      begin
        tux[ i ].Pos.X := tux[ i ].Pos.X + 1.5;
        if tux[ i ].Pos.X > 864 Then
          tux[ i ].Pos.X := -96;
      end;
    if key_Press( K_ESCAPE ) Then zgl_Exit();
    if key_Down( K_UP ) Then
       nave.Pos.Y := nave.Pos.Y - nave.DeltaY;
    if key_Down( K_DOWN ) Then
       nave.Pos.Y := nave.Pos.Y + nave.DeltaY;
    if key_Down( K_LEFT ) Then
       nave.Pos.X := nave.Pos.X - nave.DeltaX;
    if key_Down( K_RIGHT ) Then
       nave.Pos.X := nave.Pos.X + nave.DeltaX;

    // nomorelogic: joypad movimento "analogico"
  nave.Pos.X := nave.Pos.X + (nave.DeltaX * joy_AxisPos( 0, JOY_AXIS_X ));
  nave.Pos.Y := nave.Pos.Y + (nave.DeltaY * joy_AxisPos( 0, JOY_AXIS_Y ));

    // nomorelogic: joypad movimento "digitale"
  nave.Pos.X := nave.Pos.X + (nave.DeltaX * joy_AxisPos( 0, JOY_POVX ));
  nave.Pos.Y := nave.Pos.Y + (nave.DeltaY * joy_AxisPos( 0, JOY_POVY ));

  key_ClearState();
  joy_ClearState();

  c1.Y:=nave.Pos.Y;
  c1.X:=nave.Pos.X;
  c1.W:=64;
  c1.H:=64;
  for i := 0 to 20 do
    begin
     c2.Y:=tux[i].Pos.Y;
     c2.X:=tux[i].Pos.X;
     c2.W:=64;
     c2.H:=64;
     if (Intersect(C1,C2)) then
       tux[i].Killed:= true;
  end;
end;


Begin
  randomize();
  time := 255; // nomorelogic: con questo si salta lo splash screen ;)
  back0_offset := 0;
  back1_offset := 0;
  timer_Add( @Timer, 16 );
  zgl_Reg( SYS_LOAD, @Init );
  zgl_Reg( SYS_DRAW, @Draw );
  zgl_Enable( APP_USE_UTF8 );
  wnd_SetCaption( '05 - Sprites 2D' );
  wnd_ShowCursor( TRUE );
  scr_SetOptions( 800, 600, REFRESH_MAXIMUM, FALSE, FALSE );
  zgl_Init();
End.
Titolo: Re:ZenGL: Astronave Kamikaze ;)
Inserito da: nomorelogic - Maggio 06, 2012, 11:02:52 pm
si vede che ha gli scudi alzati  8)
Titolo: Re:ZenGL: Astronave Kamikaze ;)
Inserito da: sergio - Maggio 07, 2012, 11:43:46 pm
State andando avanti come dei treni , purtroppo io ho un sacco di lavoro e non riesco a starvi dietro , ma mi sembra che si incominci a intravvedere già una buona base per il gioco ! :D Complimenti.
Per Castle , non sono ancora riuscito a vedere  ,nomorelogic , ma appena riesco ti dico .
Titolo: Re:ZenGL: Astronave Kamikaze ;)
Inserito da: nomorelogic - Maggio 08, 2012, 09:18:28 am
anche io ho un sacco di lavoro, come tutti penso e solo nel weekend riesco a fare qualcosina di concreto
però dandoci una mano a vicenda qualcosa viene fuori sicuramente.

Ad esempio avevo intenzione di "trasformare" il sorgente che abbiamo ora su zengl in qualcosa di più vicino all'OOP. Questo dovrebbe aiutare a chiarire meglio le idee a tutti su come si andrà a programmare se si userà questa liberia. Che poi penso che nonostante la programmazione OOP sia meno performante nei videogame in realtà, quando si lavora in più persone, aiuta molto in quanto le modifiche vengono apportate alle classi ed è molto più semplice così piuttosto che andare a spasso per il codice a modificare record, funzioni e procedure varie.

Però penso che oramai... tocca aspettare il prossimo weekend :)
Titolo: Re:ZenGL: Astronave Kamikaze ;)
Inserito da: Stilgar - Maggio 08, 2012, 09:24:28 am
Oddio, la OOP non è più lenta a priori ... basta stare attenti a come si fanno le cose ;)
Se vuoi ti posso scrivere un pezzo di codice in C che è più lento di VB o Java ;)
Titolo: Re:ZenGL: Astronave Kamikaze ;)
Inserito da: nomorelogic - Maggio 08, 2012, 09:29:47 am
non ho detto più lenta ma meno performante ;)
mi riferivo alla ridondanza del codice che è presente 1 volta per ogni istanza di una stessa classe ed alla gestione dinamica della memoria che vuoi o non vuoi è un'altra cosa rispetto all'uso degli array :)
Titolo: Re:ZenGL: Astronave Kamikaze ;)
Inserito da: Stilgar - Maggio 08, 2012, 09:37:50 am
Certo, stavo proprio pensando alla gestione della memoria quanto ti ho risposto.
Tieni conto che i derivati del TurboPascal, per evitare questo problema, introducono un loro gestore della memoria che si svincola dal sistema operativo.
Come in Java esiste il garbage collector, che gestisce l'uso della memoria, anche in in FreePascal e Delphi esiste il concetto.
Tu chiedi al gestore della memoria (un pezzo del tuo programma) di allocarti una certa quantitià di ram, ma questa al sistema operativo è già stata chiesta.
Se fai richiesta al sistema operativo, rallenti non di poco. Il sistema operativo gestisce la ram multiprocess, multithread, multicazzieemazzi, il tuo programma solo mono processo. In questo modo introducono il problema del multithread. Dovresti modificare il gestore della memoria per risolvere questo problema. Ma perdi qualche ciclo macchina ;)
Se poi in un loop allorchi memoria a manetta, uccidi le prestazioni del tuo programma.
Per questo fare le cose con un array è più veloce che una lista che alloca dinamicamente la ram.

Non a caso le implementazioni di TList puntano a un array di interi ;)
Titolo: Re:ZenGL: Astronave Kamikaze ;)
Inserito da: nomorelogic - Maggio 08, 2012, 10:17:03 am
il garbage collector credo esista solo nella versione .NET di delphi, mi sbaglio? :O
per i compilatori (veri) ero rimasto al gestore della memoria... :P
Titolo: Re:ZenGL: Astronave Kamikaze ;)
Inserito da: Stilgar - Maggio 08, 2012, 10:56:56 am
Il riferimento al garbage è per far capire che esiste uno strato di codice tra il sistema operativo e il nostro codice.
Se usi le interfacce con "ref count" allora hai che il "life time" delle istanze è "automatizato". Per il resto get/freemem a manetta ;)
In FreePascal non lo chiedi al sistema operativo, ma al gestore della memoria integrato. Come in Delphi.
Se poi in Delphi compili per .Net, allora non stai scrivendo codice macchina (i386 per intenderci) ma PCode ;)
I compilatori, sono compilatori, è il target che può essere di plastica o di ferro ;) (Silicio nel nostro caso :D)