interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, dglOpenGL, Coding;
type
TForm1 =
class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormResize(Sender: TObject);
private
{ Private declarations }
procedure SetSizeOptions;
// Viewport und größen der map einstellen
procedure Render(Sender: TObject;
var Done: Boolean);
// Rendern
public
{ Public declarations }
DC: HDC;
RC: HGLRC;
end;
TcRGB =
Record // TcRGB Record um Farben zu speichern im RGB format
cRed, cGreen, cBlue: Single;
end;
const
SizeX = 640;
// Map größe X
SizeY = 480;
// Map größe Y
Unusable_Margin_X = 50;
// Linker Rand um später Punkte etc anzuzeigen
Unusable_Margin_Y = 10;
// Bisschen Platz unten muss sein ;)
BlockSizeX = 20;
// Block größe X
BlockSizeY = 20;
// Block größe Y
var
Form1: TForm1;
FormBlocks:
Array[0..Round(SizeX/BlockSizeX)]
of Array [0..Round(SizeY/BlockSizeY)]
of Integer;
// Map Farben
FormSettings:
Record // Sonstige Optionen
cRGB: TcRGB;
end;
FallingObject:
Record // Fallendes Objekt
pX, pY: Single;
pType: Integer;
end;
implementation
{$R *.dfm}
// Rot, Grün, Blau in TcRGB umwandeln
function GetRGB(cRed, cGreen, cBlue: Single): TcRGB;
begin
Result.cRed := cRed;
Result.cGreen := cGreen;
Result.cBlue := cBlue;
end;
// Block zeichnen
procedure DrawBlock(cRGB: TcRGB; pX, pY: Single);
begin
glBegin(GL_QUADS);
glColor3f(cRGB.cRed, cRGB.cGreen, cRGB.cBlue);
glVertex3f(pX-BlockSizeX/2, pY-BlockSizeY/2, 0);
glVertex3f(pX+BlockSizeX/2, pY-BlockSizeY/2, 0);
glVertex3f(pX+BlockSizeX/2, pY+BlockSizeY/2, 0);
glVertex3f(pX-BlockSizeX/2, pY+BlockSizeY/2, 0);
glEnd;
end;
// Viewport und Größer der map einstellen
procedure TForm1.SetSizeOptions;
begin
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glViewPort(Unusable_Margin_X, Unusable_Margin_Y, ClientWidth, ClientHeight);
glOrtho(0, SizeX, 0, SizeY, -128, 128);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
end;
// Rendern
procedure TForm1.Render(Sender: TObject;
var Done: Boolean);
var
i1, i2: Integer;
begin
SetSizeOptions;
For i1 := 0
to Round(SizeX/BlockSizeX)
do // Hintergrund für Tetris Färben
For i2 := 0
to Round(SizeY/BlockSizeY)
do
If FormBlocks[i1][i2] = 0
then
DrawBlock(FormSettings.cRGB, i1*BlockSizeX, i2*BlockSizeY);
SwapBuffers(
DC);
Done := False;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// OpenGL initialisieren
InitOpenGL;
DC := GetDC(
Handle);
RC := CreateRenderingContext(
DC, [opDoubleBuffered], 32, 24, 0, 0, 0, 0);
ActivateRenderingContext(
DC, RC);
// Alle Blöcke löschen
FillChar(FormBlocks, SizeOf(FormBlocks), 0);
// Hintergrundfarbe einstellen
FormSettings.cRGB := GetRGB(0.3, 0.3, 1);
// Viewport und Größe der map einstellen
SetSizeOptions;
// Dauerhaft Rendern
Application.OnIdle := Render;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
// Viewport und Größer der map einstellen
SetSizeOptions;
end;
end.