-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnit1.cpp
More file actions
112 lines (111 loc) · 3.27 KB
/
Unit1.cpp
File metadata and controls
112 lines (111 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HDC hOpenGLDC;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
nRedColor = 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
if(!InitializeOpenGL())
{
Application->MessageBox(_T("Failed initialize OpenGL"), _T("Error"));
Application->Terminate();
return;
}
Application->OnIdle = ApplicationIdle;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
DestroyOpenGL();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormPaint(TObject *Sender)
{
RenderScene();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WMEraseBkgnd(Winapi::Messages::TWMEraseBkgnd &Message)
{
// Option A: Simply prevent flicker (do nothing, return true)
Message.Result = 1;
// Option B: Custom background drawing
// HDC dc = Message.DC;
// // Perform GDI drawing here using dc...
// Message.Result = 1; // Tell Windows we handled it
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ApplicationIdle(TObject *Sender, bool &bFlag)
{
nRedColor++;
Invalidate();
}
//---------------------------------------------------------------------------
BOOL TForm1::InitializeOpenGL()
{
HGLRC hrc;
hOpenGLDC = GetDC(Handle);
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
int pixelformat = ChoosePixelFormat(hOpenGLDC, &pfd);
if (pixelformat == 0)
{
return FALSE;
}
if (SetPixelFormat(hOpenGLDC, pixelformat, &pfd) == FALSE)
{
return FALSE;
}
hrc = wglCreateContext(hOpenGLDC);
if (hrc == NULL) return FALSE;
return wglMakeCurrent(hOpenGLDC, hrc);
}
//---------------------------------------------------------------------------
void TForm1::DestroyOpenGL()
{
HGLRC hrc;
hrc = wglGetCurrentContext();
wglMakeCurrent(NULL, NULL);
if (hrc != NULL)
wglDeleteContext(hrc);
ReleaseDC(Handle, hOpenGLDC);
DeleteDC(hOpenGLDC);
}
//---------------------------------------------------------------------------
void TForm1::RenderScene()
{
float fltRedColor = static_cast<float>(nRedColor) / 255.0f;
glClearColor(fltRedColor, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearDepth(1.0f);
SwapBuffers(hOpenGLDC);
}