Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Software-Graphics-Renderer/GraphicsRenderer/Framework.cpp at master · AdamGPrice/Software-Graphics-Renderer · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
AdamGPrice
Software-Graphics-Renderer
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Software-Graphics-Renderer
/
GraphicsRenderer
/
Framework.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
267 lines (228 loc) · 6.9 KB
master
Breadcrumbs
Software-Graphics-Renderer
/
GraphicsRenderer
/
Framework.cpp
Copy path
Top
File metadata and controls
Code
Blame
267 lines (228 loc) · 6.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#
include
"
Framework.h
"
const
unsigned
int
DEFAULT_FRAMERATE
=
30
;
//
Reference to ourselves - primarily used to access the message handler correctly
//
This is initialised in the constructor
Framework * _thisFramework =
NULL
;
//
Forward declaration of our window procedure
LRESULT
CALLBACK
WndProc
(
HWND
hWnd,
UINT
message,
WPARAM
wParam,
LPARAM
lParam);
int
APIENTRY
wWinMain
(_In_
HINSTANCE
hInstance,
_In_opt_
HINSTANCE
hPrevInstance,
_In_
LPWSTR
lpCmdLine,
_In_
int
nCmdShow)
{
UNREFERENCED_PARAMETER
(hPrevInstance);
UNREFERENCED_PARAMETER
(lpCmdLine);
//
We can only run if an instance of a class that inherits from Framework
//
has been created
if
(_thisFramework)
{
return
_thisFramework->
Run
(hInstance, nCmdShow);
}
return
-
1
;
}
Framework::Framework
() : Framework(
800
,
600
)
{
}
Framework::Framework
(
unsigned
int
width,
unsigned
int
height)
: _hInstance(
0
), _hWnd(
0
), _width(width), _height(height)
{
_thisFramework =
this
;
}
Framework::~Framework
()
{
}
int
Framework::Run
(
HINSTANCE
hInstance,
int
nCmdShow)
{
int
returnValue;
_hInstance = hInstance;
if
(!
InitialiseMainWindow
(nCmdShow))
{
return
-
1
;
}
if
(!
Initialise
())
{
return
-
1
;
}
returnValue =
MainLoop
();
Shutdown
();
return
returnValue;
}
//
Main program loop.
int
Framework::MainLoop
()
{
MSG
msg;
HACCEL
hAccelTable =
LoadAccelerators
(_hInstance,
MAKEINTRESOURCE
(
IDC_RASTERISER
));
LARGE_INTEGER
counterFrequency;
LARGE_INTEGER
nextTime;
LARGE_INTEGER
currentTime;
LARGE_INTEGER
lastTime;
bool
updateFlag =
true
;
//
Initialise timer
QueryPerformanceFrequency
(&counterFrequency);
DWORD
msPerFrame =
static_cast
<
DWORD
>(counterFrequency.
QuadPart
/
DEFAULT_FRAMERATE
);
double
timeFactor =
1.0
/ counterFrequency.
QuadPart
;
QueryPerformanceCounter
(&nextTime);
lastTime = nextTime;
//
Main message loop:
msg.
message
=
WM_NULL
;
while
(msg.
message
!=
WM_QUIT
)
{
if
(updateFlag)
{
QueryPerformanceCounter
(¤tTime);
_timeSpan = (currentTime.
QuadPart
- lastTime.
QuadPart
) * timeFactor;
lastTime = currentTime;
Update
(_bitmap);
updateFlag =
false
;
}
QueryPerformanceCounter
(¤tTime);
//
Is it time to render the frame?
if
(currentTime.
QuadPart
> nextTime.
QuadPart
)
{
Render
(_bitmap);
//
Make sure that the window gets repainted
InvalidateRect
(_hWnd,
NULL
,
FALSE
);
//
Set time for next frame
nextTime.
QuadPart
+= msPerFrame;
//
If we get more than a frame ahead, allow one to be dropped
//
Otherwise, we will never catch up if we let the error accumulate
//
and message handling will suffer
if
(nextTime.
QuadPart
< currentTime.
QuadPart
)
{
nextTime.
QuadPart
= currentTime.
QuadPart
+ msPerFrame;
}
updateFlag =
true
;
}
//
Each time we go through this loop, we look to see if there is a Windows message
//
that needs to be processed
if
(
PeekMessage
(&msg,
0
,
0
,
0
,
PM_REMOVE
))
{
if
(!
TranslateAccelerator
(msg.
hwnd
, hAccelTable, &msg))
{
TranslateMessage
(&msg);
DispatchMessage
(&msg);
}
}
}
return
static_cast
<
int
>(msg.
wParam
);
}
//
Initialise the application. Called after the window and bitmap has been
//
created, but before the main loop starts
//
//
Return false if the application cannot be initialised.
bool
Framework::Initialise
()
{
return
true
;
}
//
Perform any updates to the structures that will be used
//
to render the window (i.e. transformation matrices, etc).
//
//
This should be overridden
void
Framework::Update
(
const
Bitmap &bitmap)
{
//
Default update method does nothing
}
//
Render the window. This should be overridden
void
Framework::Render
(
const
Bitmap &bitmap)
{
//
Default render method just sets the background to the default window colour
bitmap.
Clear
(
reinterpret_cast
<
HBRUSH
>(
COLOR_WINDOW
+
1
));
}
//
Perform any application shutdown that is needed
void
Framework::Shutdown
()
{
}
//
Register the window class, create the window and
//
create the bitmap that we will use for rendering
bool
Framework::InitialiseMainWindow
(
int
nCmdShow)
{
#
define
MAX_LOADSTRING
100
WCHAR
windowTitle[
MAX_LOADSTRING
];
WCHAR
windowClass[
MAX_LOADSTRING
];
LoadStringW
(_hInstance,
IDS_APP_TITLE
, windowTitle,
MAX_LOADSTRING
);
LoadStringW
(_hInstance,
IDC_RASTERISER
, windowClass,
MAX_LOADSTRING
);
WNDCLASSEXW
wcex;
wcex.
cbSize
=
sizeof
(
WNDCLASSEX
);
wcex.
style
=
CS_HREDRAW
|
CS_VREDRAW
;
wcex.
lpfnWndProc
= WndProc;
wcex.
cbClsExtra
=
0
;
wcex.
cbWndExtra
=
0
;
wcex.
hInstance
= _hInstance;
wcex.
hIcon
=
LoadIcon
(_hInstance,
MAKEINTRESOURCE
(
IDI_RASTERISER
));
wcex.
hCursor
=
LoadCursor
(
nullptr
,
IDC_ARROW
);
wcex.
hbrBackground
=
reinterpret_cast
<
HBRUSH
>(
COLOR_WINDOW
+
1
);
wcex.
lpszMenuName
=
nullptr
;
wcex.
lpszClassName
= windowClass;
wcex.
hIconSm
=
LoadIcon
(wcex.
hInstance
,
MAKEINTRESOURCE
(
IDI_SMALL
));
if
(!
RegisterClassExW
(&wcex))
{
return
false
;
}
//
Now work out how large the window needs to be for our required client window size
RECT
windowRect = {
0
,
0
,
static_cast
<
LONG
>(_width),
static_cast
<
LONG
>(_height) };
AdjustWindowRect
(&windowRect,
WS_OVERLAPPEDWINDOW
,
FALSE
);
_width = windowRect.
right
- windowRect.
left
;
_height = windowRect.
bottom
- windowRect.
top
;
_hWnd =
CreateWindowW
(windowClass,
windowTitle,
WS_OVERLAPPEDWINDOW
,
CW_USEDEFAULT
,
CW_USEDEFAULT
, _width, _height,
nullptr
,
nullptr
, _hInstance,
nullptr
);
if
(!_hWnd)
{
return
false
;
}
ShowWindow
(_hWnd, nCmdShow);
UpdateWindow
(_hWnd);
//
Create a bitmap of the same size as the client area of the window. This is what we
//
will be drawing on
RECT
clientArea;
GetClientRect
(_hWnd, &clientArea);
_bitmap.
Create
(_hWnd, clientArea.
right
- clientArea.
left
, clientArea.
bottom
- clientArea.
top
);
return
true
;
}
//
The WndProc for the current window. This cannot be a method, but we can
//
redirect all messages to a method.
LRESULT
CALLBACK
WndProc
(
HWND
hWnd,
UINT
message,
WPARAM
wParam,
LPARAM
lParam)
{
if
(_thisFramework !=
NULL
)
{
//
If framework is started, then we can call our own message proc
return
_thisFramework->
MsgProc
(hWnd, message, wParam, lParam);
}
else
{
//
otherwise, we just pass control to the default message proc
return
DefWindowProc
(hWnd, message, wParam, lParam);
}
}
//
Our main WndProc
LRESULT
Framework::MsgProc
(
HWND
hWnd,
UINT
message,
WPARAM
wParam,
LPARAM
lParam)
{
switch
(message)
{
case
WM_PAINT
:
{
//
Copy the contents of the bitmap to the window
PAINTSTRUCT
ps;
HDC
hdc =
BeginPaint
(hWnd, &ps);
BitBlt
(hdc,
0
,
0
, _bitmap.
GetWidth
(), _bitmap.
GetHeight
(), _bitmap.
GetDC
(),
0
,
0
,
SRCCOPY
);
EndPaint
(hWnd, &ps);
}
break
;
case
WM_SIZE
:
//
Delete any existing bitmap and create a new one of the required size.
_bitmap.
Create
(hWnd,
LOWORD
(lParam),
HIWORD
(lParam));
//
Now render to the resized bitmap
Update
(_bitmap);
Render
(_bitmap);
InvalidateRect
(hWnd,
NULL
,
FALSE
);
break
;
case
WM_DESTROY
:
PostQuitMessage
(
0
);
break
;
default
:
return
DefWindowProc
(hWnd, message, wParam, lParam);
}
return
0
;
}
You can’t perform that action at this time.