thecocce / zengl

Automatically exported from code.google.com/p/zengl
0 stars 0 forks source link

Scissor with Render Targerts #86

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Scissor seems to still not be working with Render Targets... Please see this 
modified demo08.

PRESS R - It will take a screenshot on a RenderTarget and draw it. You can see 
the render target have the wrong scissor on it.

program demo08;
{$I zglCustomConfig.cfg}
{$R *.res}

uses
{$IFDEF USE_ZENGL_STATIC}
  zgl_main,
  zgl_screen,
  zgl_window,
  zgl_timers,
  zgl_keyboard,
  zgl_render_2d, zgl_render, zgl_direct3d_all, zgl_render_target,
  zgl_fx,
  zgl_textures,
  zgl_textures_png,
  zgl_textures_jpg,
  zgl_sprite_2d,
  zgl_sengine_2d,
  zgl_primitives_2d,
  zgl_font,
  zgl_text,
  zgl_math_2d,
  zgl_utils
{$ELSE}
  zglHeader
{$ENDIF}
  ;

type
  zglPMikuSprite = ^zglTMikuSprite;

  zglTMikuSprite = record
    // RU: Îáÿçàòåëüíàÿ ÷àñòü íîâîãî òèïà ñïðàéòà.
    // EN: New type should start with this.
    Sprite: zglTSprite2D;

    // RU: Íîâûå ïàðàìåòðû.
    // EN: New params.
    Speed: zglTPoint2D;
  end;

var
  dirRes: UTF8String {$IFNDEF MACOSX} = '../data/' {$ENDIF};
  fntMain: zglPFont;
  texLogo: zglPTexture;
  texMiku: zglPTexture;
  time: Integer;
  sengine2d: zglTSEngine2D;
  ScaleX, ScaleY: Single;
  PosX: Single;
  DesktopWidth, DesktopHeight: Integer;
  GlobalScaleX, GlobalScaleY, GlobalOffSetX: Single;
  rTargetTexture: zglPRenderTarget;
  rTargetAlpha: Single;

  // Miku
procedure MikuInit(var Miku: zglTMikuSprite);
begin
  with Miku, Miku.Sprite do
  begin
    X := 1280 + random(1280);
    Y := random(720 - 128);

    // RU: Çàäàåì ñêîðîñòü äâèæåíèÿ.
    // EN: Set the moving speed.
    Speed.X := -random(10) / 5 - 0.5;
    Speed.Y := (random(10) - 5) / 5;
  end;
end;

procedure MikuDraw(var Miku: zglTMikuSprite);
begin
  with Miku.Sprite do
    asprite2d_Draw(Texture, X, Y, W, H, Angle, Round(Frame), Alpha, FxFlags);
end;

procedure MikuProc(var Miku: zglTMikuSprite);
begin
  with Miku, Miku.Sprite do
  begin
    X := X + Speed.X;
    Y := Y + Speed.Y;
    Frame := Frame + (abs(Speed.X) + abs(Speed.Y)) / 25;
    if Frame > 8 Then
      Frame := 1;

    // RU: Åñëè ñïðàéò âûõîäèò çà ïðåäåëû ïî X, ñðàçó æå óäàëÿåì åãî.
    // EN: Delete the sprite if it goes beyond X.
    if X < -128 Then
      sengine2d_DelSprite(ID);

    // RU: Åñëè ñïðàéò âûõîäèò çà ïðåäåëû ïî Y, ñòàâèì åãî â î÷åðåäü íà óäàëåíèå.
    // EN: Add sprite to queue for delete if it goes beyond Y.
    if Y < -128 Then
      Destroy := TRUE;
    if Y > 600 Then
      Destroy := TRUE;
  end;
end;

procedure MikuFree(var Miku: zglTMikuSprite);
begin
end;

// RU: Äîáàâèòü 100 ñïðàéòîâ.
// EN: Add 100 sprites.
procedure AddMiku;
var
  i: Integer;
begin
  // RU: Ïðè äîáàâëåíèè ñïðàéòà â ìåíåäæåð ñïðàéòîâ óêàçûâàåòñÿ òåêñòóðà, ñëîé(ïîëîæåíèå ïî Z) è óêàçàòåëè íà îñíîâíûå ôóíêöèè - Èíèöèàëèçàöèÿ, Ðåíäåð, Îáðàáîòêà è Óíè÷òîæåíèå.
  // EN: For adding sprite to sprite engine must be set next parameters: texture, layer(Z-coordinate) and pointers to Initialization, Render, Process and Destroy functions.
  for i := 1 to 100 do
    sengine2d_AddCustom(texMiku, SizeOf(zglTMikuSprite), random(10), @MikuInit, @MikuDraw, @MikuProc, @MikuFree);
end;

// RU: Óäàëèòü 100 ñïðàéòîâ.
// EN: Delete 100 sprites.
procedure DelMiku;
var
  i: Integer;
begin
  // RU: Óäàëèì 100 ñïðàéòîâ ñî ñëó÷àéíûì ID.
  // EN: Delete 100 sprites with random ID.
  for i := 1 to 100 do
    sengine2d_DelSprite(random(sengine2d.Count));
end;

procedure Init;
var
  i: Integer;
begin
  rTargetTexture := rtarget_Add(tex_CreateZero(1280, 720, TEX_DEFAULT_2D), RT_CLEAR_COLOR);

  texLogo := tex_LoadFromFile(dirRes + 'zengl.png');

  texMiku := tex_LoadFromFile(dirRes + 'miku.png');
  tex_SetFrameSize(texMiku, 128, 128);

  // RU: Óñòàíàâëèâàåì òåêóùèì ìåíåäæåðîì ñïðàéòîâ ñâîé.
  // EN: Set own sprite engine as current.
  sengine2d_Set(@sengine2d);

  // RU: Ñîçäàäèì 1000 ñïðàéòîâ Miku-chan :)
  // EN: Create 1000 sprites of Miku-chan :)
  for i := 0 to 9 do
    AddMiku();

  fntMain := font_LoadFromFile(dirRes + 'font.zfi');

end;

procedure Draw;
begin

  glPushMatrix;
  glScalef(GlobalScaleX, GlobalScaleY, 1);
  glTranslatef(-GlobalOffSetX, 0, 0);

  pr2d_Rect(0, 0, 1280, 720, $AAAAAA, 255, pr2d_fill);
  pr2d_Rect(0, 0, 1280, 720, $FFFFFF, 255, PR2D_SMOOTH);

  pr2d_Rect(300, 300, 500, 300, $000000, 255, pr2d_fill);
  pr2d_Rect(300, 300, 500, 300, $FF0000, 255, PR2D_SMOOTH);

  scissor_Begin(Round((300 - GlobalOffSetX) * GlobalScaleX), Round(300 * GlobalScaleY), Round(500 * GlobalScaleX), Round(300 * GlobalScaleY), False);

  batch2d_Begin();
  // RU: Ðèñóåì âñå ñïðàéòû íàõîäÿùèåñÿ â òåêóùåì ñïðàéòîâîì ìåíåäæåðå.
  // EN: Render all sprites contained in current sprite engine.
  if time > 255 Then
    sengine2d_Draw();

  if time <= 255 Then
    ssprite2d_Draw(texLogo, 640 - 256, 360 - 128, 512, 256, 0, time)
  else if time < 510 Then
  begin
    pr2d_Rect(0, 0, 1280, 720, $000000, 510 - time, pr2d_fill);
    ssprite2d_Draw(texLogo, 640 - 256, 360 - 128, 512, 256, 0, 510 - time);
  end;

  if time > 255 Then
  begin
    pr2d_Rect(0, 0, 256, 64, $000000, 200, pr2d_fill);
    text_Draw(fntMain, 0, 0, 'FPS: ' + u_IntToStr(zgl_Get(RENDER_FPS)));
    text_Draw(fntMain, 0, 20, 'Sprites: ' + u_IntToStr(sengine2d.Count));
    text_Draw(fntMain, 0, 40, 'Up/Down - Add/Delete Miku :)');
  end;
  batch2d_End();
  scissor_End;

  glPopMatrix;

  if rTargetAlpha > 0 then
  begin
    pr2d_rect(0,0,1280,720,$000000,255,PR2D_FILL);
    ssprite2d_Draw(rTargetTexture.Surface, 0, 0, 1280, 720, 0, time)
  end;

end;

procedure Timer;
begin
  INC(time, 2);

  if rTargetAlpha > 0 then
    rTargetAlpha := rTargetAlpha - 0.01;

  // RU: Âûïîëíÿåì îáðàáîòêó âñåõ ñïðàéòîâ â òåêóùåì ñïðàéòîâîì ìåíåäæåðå.
  // EN: Process all sprites contained in current sprite engine.
  sengine2d_Proc();

  // RU: Ïî íàæàòèþ ïðîáåëà î÷èñòèòü âñå ñïðàéòû.
  // EN: Delete all sprites if space was pressed.
  if key_Press(K_SPACE) Then
    sengine2d_ClearAll();
  if key_Press(K_UP) Then
    AddMiku();
  if key_Press(K_DOWN) Then
    DelMiku();

  if key_Press(K_ESCAPE) Then
    zgl_Exit();
  if key_Press(K_A) then
  begin
    ScaleX := ScaleX - 0.01;
    ScaleY := ScaleY - 0.01;
  end;

  if key_Press(K_S) then
  begin
    ScaleX := ScaleX + 0.01;
    ScaleY := ScaleY + 0.01;
  end;

  if key_Press(K_LEFT) then
    PosX := PosX - 10;

  if key_Press(K_RIGHT) then
    PosX := PosX + 10;

  if key_Press(K_R) then
  begin
    rtarget_Set(rTargetTexture);
    Draw;
    rtarget_Set(nil);
    rTargetAlpha := 1;
  end;

  key_ClearState();
end;

procedure Quit;
begin
  // RU: Î÷èùàåì ïàìÿòü îò ñîçäàííûõ ñïðàéòîâ.
  // EN: Free allocated memory for sprites.
  sengine2d_Set(@sengine2d);
  sengine2d_ClearAll();
end;

Begin
{$IFNDEF USE_ZENGL_STATIC}
  if not zglLoad(libZenGL) Then
    exit;
{$ENDIF}
  randomize;

  timer_Add(@Timer, 16);
  timer_Add(@AddMiku, 1000);

  zgl_Reg(SYS_LOAD, @Init);
  zgl_Reg(SYS_DRAW, @Draw);
  zgl_Reg(SYS_EXIT, @Quit);

  wnd_SetCaption('08 - Sprite Engine');

  wnd_ShowCursor(TRUE);

  // zgl_Enable( CORRECT_RESOLUTION );
  // scr_CorrectResolution( 1280, 720);

  DesktopWidth := 1024;
  DesktopHeight := 768;

  GlobalScaleY := DesktopHeight / 720;
  GlobalScaleX := GlobalScaleY;
  GlobalOffSetX := ((1280 * GlobalScaleX) - DesktopWidth) / 2;

  scr_SetOptions(DesktopWidth, DesktopHeight, REFRESH_MAXIMUM, False, False);

  // scr_SetOptions(1280, 720, REFRESH_MAXIMUM, false, false);

  zgl_Init();

End.

Original issue reported on code.google.com by wagenhei...@gmail.com on 3 Apr 2013 at 8:12

GoogleCodeExporter commented 9 years ago

Original comment by dr.andru@gmail.com on 3 Apr 2013 at 10:08