Thema: Delphi OpenGL Pong

Einzelnen Beitrag anzeigen

EWeiss
(Gast)

n/a Beiträge
 
#30

AW: OpenGL Pong

  Alt 24. Mär 2012, 14:14
Versuch das mal..

Delphi-Quellcode:
unit uPong;
 
interface
 
uses
   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
   Dialogs, DGLOpenGL, ExtCtrls;
 
type
   TForm1 = class(TForm)
     procedure FormCreate(Sender: TObject);
     procedure SetupGL;
     procedure FormResize(Sender: TObject);
     procedure FormDestroy(Sender: TObject);
     procedure IdleHandler(Sender: TObject; var Done: Boolean);
     procedure ErrorHandler;
     procedure Render;
     procedure Schlaeger;
     procedure FormKeyDown(Sender: TObject; var Key: Word;
       Shift: TShiftState);
   private
      StartTime, TimeCount, FrameCount : Cardinal;
      Frames, DrawTime : Cardinal;
   public
     DC: HDC;
     RC: HGLRC;
   end;
 
const
  NearClipping = 1;
  FarClipping = 1000;
 
var
   Form1: TForm1;
   s1, s2: Integer;
 
implementation
 
{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
 begin
  DC:= GetDC(Handle);
   if not InitOpenGL then Application.Terminate;
   RC:= CreateRenderingContext( DC,
                                [opDoubleBuffered],
                                32,
                                24,
                                0,0,0,
                                0);
   ActivateRenderingContext(DC, RC);
   SetupGL;
 end;
 
procedure TForm1.SetupGL;
 begin
  glClearColor(0.0, 0.0, 0.0, 0.0);
  glEnable(GL_DEPTH_TEST);
  glEnable(GL_CULL_FACE);
 end;
 
procedure TForm1.FormResize(Sender: TObject);
 var tmpBool : Boolean;
 begin
  glViewport(0, 0, ClientWidth, ClientHeight);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity;
  gluPerspective(45.0, ClientWidth/ClientHeight, NearClipping, FarClipping);
 
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity;
  IdleHandler(Sender, tmpBool);
 end;

procedure TForm1.FormDestroy(Sender: TObject);
 begin
  DeactivateRenderingContext;
  DestroyRenderingContext(RC);
  ReleaseDC(Handle, DC);
 end;
 
procedure TForm1.IdleHandler(Sender: TObject; var Done: Boolean);
begin
   StartTime:= GetTickCount;
   Render;
   DrawTime:= GetTickCount - StartTime;
   Inc(TimeCount, DrawTime);
   Inc(FrameCount);
  
   if TimeCount >= 1000 then begin
     Frames:= FrameCount;
     TimeCount:= TimeCount - 1000;
     FrameCount:= 0;
     Caption:= InttoStr(Frames) + 'FPS';
     ErrorHandler;
   end;
  
   Done:= false;
end;
 
procedure TForm1.ErrorHandler;
begin
  Form1.Caption:= gluErrorString(glGetError);
end;
 

procedure TForm1.Render;
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
 
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity;
  gluPerspective(45.0, ClientWidth/ClientHeight, NearClipping, FarClipping);
 
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity;
 
  Schlaeger;

  // alter code gehört zu Ball
  {glTranslatef(30, 0, 0);

  glBegin(GL_QUADS);
    glColor3f(0, 1, 0); glVertex3f(-1,1,-1); //lo
    glColor3f(0, 1, 0); glVertex3f(-1,-1,-1); //lu
    glColor3f(0, 1, 0); glVertex3f(1,-1,-1); //ru
    glColor3f(0, 1, 0); glVertex3f(1,1,-1);  //ro
  glEnd; }

 
  SwapBuffers(DC);
end;
 
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;
   Shift: TShiftState);
begin
 if Key = ord('W') then
  s1:= s1+5;

 if Key = ord('S') then
   s1:= s1-5;

 if Key = VK_UP then
  s2:= s2+5;

 if Key = VK_DOWN then
  s2:= s2-5;

 Render;
end;

procedure TForm1.Schlaeger;
begin
 //Spieler 1
 glTranslatef(-60, 0, 0);
 glTranslated (0, s1, 0);
 glBegin(GL_QUADS);
   glColor3f(0, 1, 0); glVertex3f(-1,1,-1); //lo
   glColor3f(0, 1, 0); glVertex3f(-1,-9,-1); //lu
   glColor3f(0, 1, 0); glVertex3f(1,-9,-1); //ru
   glColor3f(0, 1, 0); glVertex3f(1,1,-1); //ro
 glEnd;
 
// Spieler 2
 glTranslatef(30, 6, -50);
 glTranslated(0, s2, 0);
 glBegin(GL_QUADS);
   glColor3f(0, 1, 0); glVertex3f(-1,1,-1); //lo
   glColor3f(0, 1, 0); glVertex3f(-1,-9,-1); //lu
   glColor3f(0, 1, 0); glVertex3f(1,-9,-1); //ru
   glColor3f(0, 1, 0); glVertex3f(1,1,-1); //ro
 glEnd;
end;

end.
Habs mal getestet!
Also da ist einiges im argen Koordinaten usw..
Der linke Schläger wird aber jetzt gezeichnet wenn du die Tasten verwendest.

gruss

Geändert von EWeiss (24. Mär 2012 um 15:18 Uhr)
  Mit Zitat antworten Zitat