Created
June 28, 2024 21:24
-
-
Save bw2012/2e64fe6e25f15604380c55c35f618950 to your computer and use it in GitHub Desktop.
Free Pascal glut minimal template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
program template_glut; | |
{$mode objfpc}{$H+} | |
uses | |
dynlibs, sysutils, GL, glu, Glut, GLext; | |
const | |
AppWidth = 300; | |
AppHeight = 300; | |
var | |
GLErrorStr: string = ''; | |
procedure glutInitPascal(ParseCmdLine: Boolean); | |
var | |
Cmd: array of PChar; | |
CmdCount, I: Integer; | |
begin | |
if ParseCmdLine then | |
CmdCount := ParamCount + 1 | |
else | |
CmdCount := 1; | |
SetLength(Cmd, CmdCount); | |
for I := 0 to CmdCount - 1 do | |
Cmd[I] := PChar(ParamStr(I)); | |
glutInit(@CmdCount, @Cmd); | |
end; | |
procedure glWrite(X, Y: GLfloat; Font: Pointer; Text: String); | |
var | |
I: Integer; | |
begin | |
glRasterPos2f(X, Y); | |
for I := 1 to Length(Text) do glutBitmapCharacter(Font, Integer(Text[I])); | |
end; | |
function glGetViewportWidth: Integer; | |
var | |
Rect: array[0..3] of Integer; | |
begin | |
glGetIntegerv(GL_VIEWPORT, @Rect); | |
Result := Rect[2] - Rect[0]; | |
end; | |
function glGetViewportHeight: Integer; | |
var | |
Rect: array[0..3] of Integer; | |
begin | |
glGetIntegerv(GL_VIEWPORT, @Rect); | |
Result := Rect[3] - Rect[1]; | |
end; | |
procedure glEnter2D; | |
begin | |
glMatrixMode(GL_PROJECTION); | |
glPushMatrix; | |
glLoadIdentity; | |
gluOrtho2D(0, glGetViewportWidth, 0, glGetViewportHeight); | |
glMatrixMode(GL_MODELVIEW); | |
glPushMatrix; | |
glLoadIdentity; | |
glDisable(GL_DEPTH_TEST); | |
end; | |
procedure glExit2D; | |
begin | |
glMatrixMode(GL_PROJECTION); | |
glPopMatrix; | |
glMatrixMode(GL_MODELVIEW); | |
glPopMatrix; | |
glEnable(GL_DEPTH_TEST); | |
end; | |
procedure DrawGLScene; cdecl; | |
begin | |
glClearColor(0, 0, 0, 0); | |
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT); | |
glEnter2D; | |
glWrite(20, 20, GLUT_BITMAP_9_BY_15, 'TEST'); | |
glExit2D; | |
glutSwapBuffers; | |
end; | |
procedure ReSizeGLScene(Width, Height: Integer); cdecl; | |
begin | |
if Height = 0 then | |
Height := 1; | |
glViewport(0, 0, Width, Height); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity; | |
gluPerspective(45, Width / Height, 0.1, 1000); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity; | |
end; | |
procedure GLKeyboard(Key: Byte; x, y: LongInt); cdecl; | |
begin | |
if Key = 27 then halt(0); | |
end; | |
procedure InitializeGL; | |
begin | |
glClearColor(0.18, 0.20, 0.66, 0); | |
end; | |
var | |
ScreenWidth, ScreenHeight: Integer; | |
begin | |
glutInitPascal(True); | |
glutInitDisplayMode(GLUT_DOUBLE or GLUT_RGB or GLUT_DEPTH); | |
glutInitWindowSize(AppWidth, AppHeight); | |
ScreenWidth := glutGet(GLUT_SCREEN_WIDTH); | |
ScreenHeight := glutGet(GLUT_SCREEN_HEIGHT); | |
glutInitWindowPosition((ScreenWidth - AppWidth) div 2, (ScreenHeight - AppHeight) div 2); | |
glutCreateWindow('TEST'); | |
glutFullScreen; | |
InitializeGL; | |
glutDisplayFunc(@DrawGLScene); | |
glutReshapeFunc(@ReSizeGLScene); | |
glutKeyboardFunc(@GLKeyboard); | |
glutMainLoop; | |
end. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment