程式語言 - Delphi 7 - Visual Component Library (VCL) - Single Document Interface (SDI) - Draw Triangle



參考資訊:
https://github.com/gamedev-net/nehe-opengl

main.dpr

program app;
 
uses
    Forms, main in 'main.pas' {Form1};
 
begin
    Application.Initialize;
    Application.CreateForm(TForm1, Form1);
    Application.Run;
end.

main.pas

unit main;

interface

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

type
    TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormPaint(Sender: TObject);
        procedure FormDestroy(Sender: TObject);

    private
        hDC: HDC;
        hRC: HGLRC;
    end;

var
    Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
    pfd: PIXELFORMATDESCRIPTOR;
    fmt: Integer;
    rc: TRect;
    aspect: GLfloat;

begin
    hDC := GetDC(Handle);

    FillChar(pfd, SizeOf(pfd), 0);
    pfd.nSize := SizeOf(PIXELFORMATDESCRIPTOR);
    pfd.nVersion := 1;
    pfd.dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
    pfd.iPixelType := PFD_TYPE_RGBA;
    pfd.cColorBits := 16;
    pfd.cDepthBits := 16;
    pfd.iLayerType := PFD_MAIN_PLANE;

    fmt := ChoosePixelFormat(hDC, @pfd);
    SetPixelFormat(hDC, fmt, @pfd);
    hRC := wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);

    Windows.GetClientRect(Handle, rc);
    aspect := (rc.Right - rc.Left) / (rc.Bottom - rc.Top);
    glViewport(0, 0, rc.Right - rc.Left, rc.Bottom - rc.Top);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-aspect, aspect, -1.0, 1.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
    glLoadIdentity();
    glBegin(GL_TRIANGLES);
        glVertex3f(0.0, 0.5, 0.0);
        glVertex3f(-0.5, -0.5, 0.0);
        glVertex3f(0.5, -0.5, 0.0);
    glEnd();

    SwapBuffers(hDC);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
    wglMakeCurrent(0, 0);
    wglDeleteContext(hRC);
    ReleaseDC(Handle, hDC);
end;
end.

main.dfm

object Form1: TForm1
    Left = 0
    Top = 0
    Width = 320
    Height = 240
    Caption = 'main'
    Color = clBtnFace
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -11
    Font.Name = 'MS Sans Serif'
    Font.Style = []
    OldCreateOrder = False
    PixelsPerInch = 96
    TextHeight = 13
    OnCreate = FormCreate
    OnPaint = FormPaint
    OnDestroy = FormDestroy
end

編譯、執行

$ myx86 dcc32 main.dpr
$ BOX86_DYNAREC=0 myx86 main.exe

完成