-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathWin32.cpp
168 lines (147 loc) · 5.19 KB
/
Win32.cpp
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
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <cmath>
namespace
{
HWND button;
////////////////////////////////////////////////////////////
/// Function called whenever one of our windows receives a message
///
////////////////////////////////////////////////////////////
LRESULT CALLBACK onEvent(HWND handle, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
// Quit when we close the main window
case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}
// Quit when we click the "quit" button
case WM_COMMAND:
{
if (reinterpret_cast<HWND>(lParam) == button)
{
PostQuitMessage(0);
return 0;
}
}
}
return DefWindowProc(handle, message, wParam, lParam);
}
} // namespace
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \param Instance: Instance of the application
///
/// \return Error code
///
////////////////////////////////////////////////////////////
int main()
{
HINSTANCE instance = GetModuleHandle(nullptr);
// Define a class for our main window
WNDCLASS windowClass;
windowClass.style = 0;
windowClass.lpfnWndProc = &onEvent;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = 0;
windowClass.hInstance = instance;
windowClass.hIcon = nullptr;
windowClass.hCursor = nullptr;
windowClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_BACKGROUND);
windowClass.lpszMenuName = nullptr;
windowClass.lpszClassName = TEXT("SFML App");
RegisterClass(&windowClass);
// Create the main window
HWND window = CreateWindow(TEXT("SFML App"),
TEXT("SFML Win32"),
WS_SYSMENU | WS_VISIBLE,
200,
200,
660,
520,
nullptr,
nullptr,
instance,
nullptr);
// Add a button for exiting
button = CreateWindow(TEXT("BUTTON"), TEXT("Quit"), WS_CHILD | WS_VISIBLE, 560, 440, 80, 40, window, nullptr, instance, nullptr);
// Create two SFML views
HWND view1 = CreateWindow(TEXT("STATIC"),
nullptr,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
20,
20,
300,
400,
window,
nullptr,
instance,
nullptr);
HWND view2 = CreateWindow(TEXT("STATIC"),
nullptr,
WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
340,
20,
300,
400,
window,
nullptr,
instance,
nullptr);
sf::RenderWindow sfmlView1(view1);
sf::RenderWindow sfmlView2(view2);
// Load some textures to display
const sf::Texture texture1("resources/image1.jpg");
const sf::Texture texture2("resources/image2.jpg");
sf::Sprite sprite1(texture1);
sf::Sprite sprite2(texture2);
sprite1.setOrigin(sf::Vector2f(texture1.getSize()) / 2.f);
sprite1.setPosition(sprite1.getOrigin());
// Create a clock for measuring elapsed time
const sf::Clock clock;
// Loop until a WM_QUIT message is received
MSG message;
message.message = static_cast<UINT>(~WM_QUIT);
while (message.message != WM_QUIT)
{
if (PeekMessage(&message, nullptr, 0, 0, PM_REMOVE))
{
// If a message was waiting in the message queue, process it
TranslateMessage(&message);
DispatchMessage(&message);
}
else
{
const float time = clock.getElapsedTime().asSeconds();
// Clear views
sfmlView1.clear();
sfmlView2.clear();
// Draw sprite 1 on view 1
sprite1.setRotation(sf::degrees(time * 100));
sfmlView1.draw(sprite1);
// Draw sprite 2 on view 2
sprite2.setPosition({std::cos(time) * 100.f, 0.f});
sfmlView2.draw(sprite2);
// Display each view on screen
sfmlView1.display();
sfmlView2.display();
}
}
// Close our SFML views before destroying the underlying window
sfmlView1.close();
sfmlView2.close();
// Destroy the main window (all its child controls will be destroyed)
DestroyWindow(window);
// Don't forget to unregister the window class
UnregisterClass(TEXT("SFML App"), instance);
}