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.