Thema: Delphi OpenGL Pong

Einzelnen Beitrag anzeigen

acidrain

Registriert seit: 18. Mär 2012
17 Beiträge
 
#23

AW: OpenGL Pong

  Alt 24. Mär 2012, 13:53
Ich hab nichts zu verbergen
Hier ist alles:
Code:
unit uPong;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DGLOpenGL, ExtCtrls;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    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 GLDraw;
    procedure InitOpenGL;
    procedure FormKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure Timer1Timer(Sender: TObject);
  private
     StartTime, TimeCount, FrameCount : Cardinal;
     Frames, DrawTime                 : Cardinal;
  public
    DC: HDC;
    RC: HGLRC;
  end;

const
 NearClipping = 1;
 FarClipping = 1000;

var
  Form1: TForm1;
  Spieler1hoch: boolean;
  Spieler1runter: boolean;
  Spieler2hoch: boolean;
  Spieler2runter: boolean;
  s1, s2: Integer;

implementation

{$R *.dfm}

procedure TForm1.InitOpenGL;
begin
  // Gerätekontext holen
  h_DC := GetDC(_FNormal.Handle);
  // Renderkontext erstellen (32 Bit Farbtiefe, 24 Bit Tiefenpuffer, Doublebuffering)
  h_RC := CreateRenderingContext(h_DC, [opDoubleBuffered], 32, 24, 0, 0, 0, 0);
  // Erstellten Renderkontext aktivieren
  ActivateRenderingContext(h_DC, h_RC);
begin;

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; Schlaeger;
  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;

// 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
 Spieler1hoch := true;

 if Key = ord('S') then
 Spieler1runter := true;

if Key = VK_UP then
 Spieler2hoch := true;

if Key = VK_DOWN then
 Spieler2runter := true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
GLDraw;
if Spieler1hoch then
  begin
   s1:= s1+5;
   Spieler1hoch:= false;

 if Spieler1runter then
  begin
   s1:= s1-5;
   Spieler1runter:= false;
end;
end;
end;

procedure TForm1.Schlaeger;
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_PROJECTION);
//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;

procedure TForm1.GLDraw;
begin
  // Clear The Screen And The Depth Buffer
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

  // Change Matrix Mode to Projection
  glMatrixMode(GL_PROJECTION);
  // Reset View
  glLoadIdentity;
  gluPerspective(45, (320 / 240), 1, 1000.0);
  // Change Projection to Matrix Mode
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
 
  Schlaeger;

  // Update the display
  SwapBuffers(h_DC);
end;

end.
  Mit Zitat antworten Zitat