unit _render;

interface

uses _var, _scd, _update, exgine, openGL, SysUtils, StrUtils;

procedure render_game;
procedure render_menu;

implementation

//----------------------------------------------------------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure draw(tx:TTexture; x,y,w,h,angle:single;center:byte=5);
var wd,hd:integer;
begin
 glPushMatrix;
 tex.Enable(tx);
 glDisable(GL_COLOR_MATERIAL);
 wd:=1;
 hd:=1;
// if scale then
//     glTranslatef(x+w/2,y+h/2,0);
//   else
case center of
  1: glTranslatef(x+w/2,y-h/2,0);
  2: glTranslatef(x,y-h/2,0);
  3: glTranslatef(x-w/2,y-h/2,0);
  4: glTranslatef(x-w/2,y,0);
  5: glTranslatef(x,y,0);
  6: glTranslatef(x-w/2,y,0);
  7: glTranslatef(x+w/2,y+h/2,0);
  8: glTranslatef(x,y+h/2,0);
  9: glTranslatef(x-w/2,y+h/2,0);
end;

//     glTranslatef(x-w/2,y+h/2,0);

 glRotatef(angle,0,0,1);
 glBegin(GL_QUADS);
  glTexCoord2d(0,0); glVertex2f(-w/2,-h/2);
  glTexCoord2d(wd,0); glVertex2f(w/2,-h/2);
  glTexCoord2d(wd,hd); glVertex2f(w/2,h/2);
  glTexCoord2d(0,hd); glVertex2f(-w/2,h/2);
 glEnd;
 tex.Disable;
 glPopMatrix;
end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure render_level;
var a,b,c:integer;
begin

for a:=0 to 16 do
  if a+scr.wl<=LevelW-1 then
    for b:=0 to 12 do
      if b+scr.wt<=LevelH-1 then
        begin // + main cycle
          ogl.Blend(BT_SUB);
          glColor3ub(levelData.colR,levelData.colG,levelData.colB);

          if level[a+scr.wl,b+scr.wt].contain<>255 then
            draw(t[tube+level[a+scr.wl,b+scr.wt].tube],a*64-scr.dx, b*64-scr.dy,64,64,0,7);

          if level[a+scr.wl,b+scr.wt].pass=2 then
            draw(t[pass],a*64-scr.dx, b*64-scr.dy,64,64,180,7);
          if level[a+scr.wl,b+scr.wt].pass=4 then
            draw(t[pass],a*64-scr.dx, b*64-scr.dy,64,64,-90,7);
          if level[a+scr.wl,b+scr.wt].pass=6 then
            draw(t[pass],a*64-scr.dx, b*64-scr.dy,64,64,90,7);
          if level[a+scr.wl,b+scr.wt].pass=8 then
            draw(t[pass],a*64-scr.dx, b*64-scr.dy,64,64,0,7);

          if level[a+scr.wl,b+scr.wt].contain=255 then
            draw(t[wall],a*64-scr.dx, b*64-scr.dy,64,64,level[a+scr.wl,b+scr.wt].wall*90,7);

          //ex.log.Print(pchar('levelW:' + inttostr(a+scr.wl)+' levelH:' + inttostr(b+scr.wt)+' scr.wt:' + inttostr(scr.wt)+' scr.t:' + inttostr(scr.t)));

          if level[a+scr.wl,b+scr.wt].contain in [1,2] then
            for c:=0 to 29 do
            if level[a+scr.wl,b+scr.wt].food[c].active then
              begin
                glBegin(GL_POINTS);
                  glColor4ub(level[a+scr.wl,b+scr.wt].food[c].colR,level[a+scr.wl,b+scr.wt].food[c].colG,level[a+scr.wl,b+scr.wt].food[c].colB,200);
                  glVertex3d(level[a+scr.wl,b+scr.wt].food[c].fx+a*64-scr.dx,level[a+scr.wl,b+scr.wt].food[c].fy+b*64-scr.dy,0);
                glEnd();
//                ogl.Blend(BT_add);
//                glColor4ub(level[a+scr.wl,b+scr.wt].food[c].colR,level[a+scr.wl,b+scr.wt].food[c].colG,level[a+scr.wl,b+scr.wt].food[c].colB,255);
//                draw(t[food],level[a+scr.wl,b+scr.wt].food[c].fx+a*64-scr.dx,level[a+scr.wl,b+scr.wt].food[c].fy+b*64-scr.dy,2,2,0,5);

              end;

//          if level[a,b].pass in [2,4,6,8] then glColor3ub(255,255,255) else glColor3ub(155,155,155);

//          ogl.TextOut(fnt8,a*64+30,b*64+30,pchar(inttostr(level[a,b].ret)));

{          if level[a+scr.wl,b+scr.wt].ret=2 then
            draw(t[pass],a*64-scr.dx, b*64-scr.dy,64,64,180,7);
          if level[a+scr.wl,b+scr.wt].ret=4 then
            draw(t[pass],a*64-scr.dx, b*64-scr.dy,64,64,-90,7);
          if level[a+scr.wl,b+scr.wt].ret=6 then
            draw(t[pass],a*64-scr.dx, b*64-scr.dy,64,64,90,7);
          if level[a+scr.wl,b+scr.wt].ret=8 then
            draw(t[pass],a*64-scr.dx, b*64-scr.dy,64,64,0,7);
}


        end; // - main cycle
end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure reneder_mut;
var a:integer;
begin
ogl.Blend(BT_SUB);
glColor3ub(255,255,255);
if high(a_mut)>=0 then
  for a:=0 to high(a_mut) do
    begin
      if a_mut[a].dy>0 then
        draw(t[mut],a_mut[a].X, a_mut[a].y,16*2,16*2,a_mut[a].angle,5)
        else
        if (abs(a_mut[a].x-scr.l)>8) and (abs(a_mut[a].x-scr.r)>8) and (abs(a_mut[a].y-scr.t)>8) and (abs(a_mut[a].y-scr.d)>8) then
          draw(t[mut],a_mut[a].X-scr.l, a_mut[a].y-scr.t,16,16,a_mut[a].angle,5);
    end;

end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure render_enemy;
var a,c:integer;
begin
ogl.Blend(BT_SUB);
if high(a_enemy)>=0 then
  for a:=0 to high(a_enemy) do
    begin
        glColor4ub(a_enemy[a].colR,a_enemy[a].colG,a_enemy[a].colB,255);
{          glBegin(GL_lines);
            glColor4ub(a_enemy[a].colR,a_enemy[a].colG,a_enemy[a].colB,200);
            glVertex3d(a_enemy[a].x+32-scr.l,a_enemy[a].y+32-scr.t,0);
            glVertex3d(a_enemy[a].tgt_x*64+32-scr.l,a_enemy[a].tgt_y*64+32-scr.t,0);
          glEnd();
}
        //ogl.TextOut(fnt8,a_enemy[a].X+32-scr.l-16, a_enemy[a].y+32-16-scr.t,pchar(inttostr(a_enemy[a].tgt_x)+' '+inttostr(a_enemy[a].tgt_y)));
        if a_enemy[a].hit = 1 then  glColor3ub(0,180,255);
        if a_enemy[a].hp<=0 then
          begin
            ogl.Blend(BT_ADD);
            glColor4ub(a_enemy[a].colR,a_enemy[a].colG,a_enemy[a].colB,255);
            draw(t[coreinv],a_enemy[a].X+32-scr.l, a_enemy[a].y+32-scr.t,20,20,a_enemy[a].angle,5);
            glColor3ub(255,255,255);
            ogl.Blend(BT_SUB);
            draw(t[a_enemy[a].spr+a_enemy[a].frame],a_enemy[a].X+32-scr.l, a_enemy[a].y+32-scr.t,16,16,a_enemy[a].angle,5);


          end
        else
        if a_enemy[a].frz<32 then
        begin
          glColor4ub(a_enemy[a].colR,a_enemy[a].colG,a_enemy[a].colB,255);
          glColor3ub(255,255,255);
          ogl.Blend(BT_SUB);
          draw(t[a_enemy[a].spr+a_enemy[a].frame],a_enemy[a].X+32-scr.l, a_enemy[a].y+32-scr.t,32,32,a_enemy[a].angle,5);
        end;
        if (a_enemy[a].frz>=32) and (a_enemy[a].frz<=70) then
          begin
            c:=random(256);
            glColor3ub(c and a_enemy[a].colR,c and a_enemy[a].colG,c and a_enemy[a].colB);
            draw(t[repliflash],tech_replica.X-scr.l, tech_replica.y+-scr.t-8,64,16,0,5);
          end;
      a_enemy[a].hit:=0;
    end;
end;



//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure render_sparker;
var a:integer;
begin
glColor3ub(255,255,255);
if high(a_sparker)>=0 then
  for a:=0 to high(a_sparker) do
    begin
        ogl.Blend(BT_SUB);
        if  a_sparker[a].tp=0 then
          begin
            glColor3ub(levelData.colR,levelData.colG,levelData.colB);
            draw(t[sparker],a_sparker[a].X-scr.l-32-5, a_sparker[a].y-scr.t,32,32,0,5);
            draw(t[sparker],a_sparker[a].X-scr.l+32+5, a_sparker[a].y-scr.t,-32,32,0,5);
            glColor3ub(255,255,255);
            draw(t[sparker+1],a_sparker[a].X-scr.l+32+5, a_sparker[a].y-scr.t,-32,32,0,5);
            draw(t[sparker+1],a_sparker[a].X-scr.l-32-5, a_sparker[a].y-scr.t,32,32,0,5);
            if a_sparker[a].delay<frate div 3 then
              begin
                ogl.Blend(BT_ADD);
                draw(t[spark+a_sparker[a].sparkFr],a_sparker[a].X-scr.l, a_sparker[a].y-scr.t,a_sparker[a].sparkW,a_sparker[a].sparkH,0,5);
              end;
          end
          else
          begin
            glColor3ub(levelData.colR,levelData.colG,levelData.colB);
            draw(t[sparker],a_sparker[a].X-scr.l, a_sparker[a].y-scr.t-32-5,32,32,90,5);
            draw(t[sparker],a_sparker[a].X-scr.l, a_sparker[a].y-scr.t+32+5,32,-32,-90,5);
            glColor3ub(255,255,255);
            draw(t[sparker+1],a_sparker[a].X-scr.l, a_sparker[a].y-scr.t+32+5,32,-32,-90,5);
            draw(t[sparker+1],a_sparker[a].X-scr.l, a_sparker[a].y-scr.t-32-5,32,32,90,5);
            if a_sparker[a].delay<frate div 3 then
              begin
                ogl.Blend(BT_ADD);
                draw(t[spark+a_sparker[a].sparkFr],a_sparker[a].X-scr.l, a_sparker[a].y-scr.t,a_sparker[a].sparkW,a_sparker[a].sparkH,90,5);
              end;
          end
    end;
end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure render_player;
var s:string;
begin
if player.dead=0 then
begin
ogl.Blend(BT_ADD);
glColor4ub(255,255,255,255);

if player.hit = 1 then  glColor3ub(0,180,255);
if player.hit = 2 then  glColor3ub(255,180,0);

draw(t[core],player.X+32-scr.l, player.y+32-scr.t,16,16,0,5);

draw(t[pg],player.X+32-scr.l, player.y+32-scr.t,64,64,player.shellangle,5);
ogl.Blend(BT_SUB);
draw(t[player.oral],player.X+32-scr.l, player.y+32-scr.t,32,32,player.angle,5);
player.hit:=0;

if player.Charged>0 then
  begin
    glColor4ub(100,255,100,255);
    s:='0:'+rightstr('0'+inttostr(player.Charged div frate + 1),2);
    ogl.TextOut(fnt8,player.X+32+24-scr.l, player.y+32-24-scr.t,pchar(s));


    ogl.Blend(BT_ADD);
    draw(t[thunder+player.tbn],player.X+32-scr.l, player.y+32-scr.t,58,58,player.tba,5);
  end;
end;

end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure render_tech;
var a,b,c,x,y:integer;
begin

glColor3ub(255,255,255);
ogl.Blend(BT_SUB);
draw(t[enter],tech_enter.x-scr.l,tech_enter.y-scr.t,64,64,tech_enter.angle,5);

if leveldata.Keys=player.mut then a:=1 else a:=0;
draw(t[exit+a],tech_exit.x-scr.l,tech_exit.y-scr.t,64,64,tech_exit.angle,5);


glColor3ub(levelData.colR,levelData.colG,levelData.colB);
draw(t[faketube],tech_replica.x-scr.l-31,tech_replica.y-scr.t-6,16,32,0,4);
draw(t[faketube],tech_replica.x-scr.l-31,tech_replica.y-scr.t-17,16,32,0,4);
draw(t[faketube],tech_replica.x-scr.l-31,tech_replica.y-scr.t+17,16,-32,0,4);

draw(t[faketube],tech_replica.x-scr.l+31,tech_replica.y-scr.t-6,-16,32,0,6);
draw(t[faketube],tech_replica.x-scr.l+31,tech_replica.y-scr.t-17,-16,32,0,6);
draw(t[faketube],tech_replica.x-scr.l+31,tech_replica.y-scr.t+17,-16,-32,0,6);

glColor3ub(255,255,255);
draw(t[replicator],tech_replica.x-scr.l,tech_replica.y-scr.t,128,64,0,5);


if leveldata.repenemy>0 then

  begin
    glColor3ub(0,255,0);
    if leveldata.repenemy>7 then b:=7 else b:= leveldata.repenemy;

    for a:=0 to b-1 do
        draw(t[replilight],tech_replica.x-scr.l,tech_replica.y-scr.t+12+a*2,16,1,0,5);
  end;
end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure render_bonus;
begin
  //ogl.TextOut(fnt8,10,35,pchar(inttostr(bonus.TTL)));
  if bonus.active then
    begin
      ogl.Blend(BT_ADD);
      case bonus.tp of
        0: glColor3ub(100,255,100);
        1: glColor3ub(100,100,255);
      end;
      draw(t[t_bonus],bonus.X+32-scr.l, bonus.y+32-scr.t,24,24,player.shellangle,5);
      draw(t[coreinv],bonus.X+32-scr.l, bonus.y+32-scr.t,8,8,0,5);

    end;
end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure render_HUD;
var a,b,c,d,mx:integer;
    r:single;
begin
mx:=50;
glColor3ub(255,255,255);
ogl.Blend(BT_SUB);
draw(t[t_hud],0,768,512,64,0,1);

c:=mx*player.energy div player.max_energy;
d:=player.max_energy div mx;
for a:=0 to mx-1 do
  begin

    if player.energy-a*d>d then
      glColor4ub(255,255,255,255);

    if player.energy-a*d<=0 then
      glColor4ub(255,255,255,100);

//    if (player.energy-a*d>0) and (player.energy-a*d<d)  then
//        begin
//          b:=(player.energy-a*d)*(225 div d);
//          b:=player.energy div 50;
          //b:=255-b*4;
//          ogl.TextOut(fnt8,10,35,pchar(inttostr(b)));
//          glColor4ub(255,255,255,b)
//        end;
    draw(t[t_energy],hud.energy_x+a*2+2, hud.y-64+hud.energy_y,2,16,0,4);
//    ogl.Blend(BT_ADD);
//    if odd(a) then b:=-a*2 else b:=a*2;
//    draw(t[t_energy1],player.X+32-scr.l, player.y+32-scr.t,64,64,b,5);
//    draw(t[t_energy1],player.X+32-scr.l, player.y+32-scr.t,64,64,-a*4,5);

  end;


if leveldata.Keys>0 then
  begin
    for a:=0 to leveldata.Keys-1 do
      begin
        if a>=player.mut then glColor4ub(255,255,255,50) else glColor4ub(255,255,255,255);
        if odd(a) then b:=t_dna0 else b:=t_dna1;
        draw(t[b],hud.dna_x+a*8+4, hud.y-64+hud.dna_y,8,8,0,5);
      end;
  end;


glColor4ub(255,255,255,255);
if player.mut<leveldata.Keys then
  draw(t[bbox],hud.x+78, hud.y-64+51,64,8,0,7);

if player.energy<player.max_energy then
  draw(t[bbox],hud.x+68, hud.y-64+16,68,8,0,7);


end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure render_game;
var a,b,c:integer;
begin

render_level;

reneder_mut;

render_enemy;

render_sparker;

render_bonus;

render_player;

render_tech;

render_HUD;

{
ogl.TextOut(fnt8,10,5,pchar(inttostr(player.x)+' '+inttostr(player.y)));
ogl.TextOut(fnt8,10,15,pchar(inttostr(tech_exit.x)+' '+inttostr(tech_exit.y)));
ogl.TextOut(fnt8,10,25,pchar(inttostr(player.lvl) + ' '+ inttostr(player.lvl mod 2) ));
}
end;

//----------------------------------------------------------------------------------------------------------------------------------------------------

procedure render_menu;
var a,b,c:integer;
begin

render_level;

render_enemy;

render_player;

glColor3ub(255,255,255);
ogl.Blend(BT_SUB);
draw(t[enter],tech_enter.x-scr.l,tech_enter.y-scr.t,64,64,tech_enter.angle,5);

draw(t[exit+1],64,704,64,64,0,7);    // exit

draw(t[exit],320,704,64,64,0,7);   // continue

draw(t[exit+1],448,704,64,64,0,7);   // new

ogl.TextOut(fnt8,64+22,680,pchar('EXIT'));
ogl.TextOut(fnt8,320+7,680,pchar('CONTINUE'));
ogl.TextOut(fnt8,448+4,680,pchar('NEW GAME'));

ogl.Blend(BT_ADD);
glColor4ub(255,255,255,255);
draw(t[t_logo],512,257,1024,64,0,5);
ogl.Blend(BT_SUB);
draw(t[t_copyr],704,512,128,32,0,7);

end;


//----------------------------------------------------------------------------------------------------------------------------------------------------


end.




