-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
426 lines (361 loc) · 14.9 KB
/
main.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#include "imgui.h"
#include "imgui_impl_win32.h"
#include "imgui_impl_dx11.h"
#include "imgui_freetype.h"
#include <d3d11.h>
#include <tchar.h>
#include "font.h"
#include "iconcpp.h"
#include <cstdint>
#include "glm.hpp"
#include <vector>
#include <string>
//#include <fstream>
#include <iostream>
#include <algorithm>
//for shit that should be in its own file lmao
#include "canvas.h"
#include "assets.h"
#include "gui_main.h"
#include "utils.h"
#include <ft2build.h>
#include <filesystem>
#include FT_FREETYPE_H
// Data
static IDXGISwapChain* g_pSwapChain = NULL;
static ID3D11RenderTargetView* g_mainRenderTargetView = NULL;
// Forward declarations of helper functions
bool CreateDeviceD3D(HWND hWnd);
void CleanupDeviceD3D();
void CreateRenderTarget();
void CleanupRenderTarget();
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void SetupTheme() {
// Disable INI file
ImGui::GetIO().IniFilename = NULL;
// Setup Dear ImGui style
constexpr auto ColorFromBytes = [](uint8_t r, uint8_t g, uint8_t b)
{
return ImVec4((float)r / 255.0f, (float)g / 255.0f, (float)b / 255.0f, 1.0f);
};
auto& style = ImGui::GetStyle();
ImVec4* colors = style.Colors;
const ImVec4 bgColor = ColorFromBytes(37, 37, 38);
const ImVec4 lightBgColor = ColorFromBytes(82, 82, 85);
const ImVec4 veryLightBgColor = ColorFromBytes(90, 90, 95);
const ImVec4 panelColor = ColorFromBytes(51, 51, 55);
const ImVec4 panelHoverColor = ColorFromBytes(29, 151, 236);
const ImVec4 panelActiveColor = ColorFromBytes(0, 119, 200);
const ImVec4 textColor = ColorFromBytes(255, 255, 255);
const ImVec4 textDisabledColor = ColorFromBytes(151, 151, 151);
const ImVec4 borderColor = ColorFromBytes(78, 78, 78);
colors[ImGuiCol_Text] = textColor;
colors[ImGuiCol_TextDisabled] = textDisabledColor;
colors[ImGuiCol_TextSelectedBg] = panelActiveColor;
colors[ImGuiCol_WindowBg] = bgColor;
colors[ImGuiCol_ChildBg] = bgColor;
colors[ImGuiCol_PopupBg] = bgColor;
colors[ImGuiCol_Border] = borderColor;
colors[ImGuiCol_BorderShadow] = borderColor;
colors[ImGuiCol_FrameBg] = panelColor;
colors[ImGuiCol_FrameBgHovered] = panelHoverColor;
colors[ImGuiCol_FrameBgActive] = panelActiveColor;
colors[ImGuiCol_TitleBg] = bgColor;
colors[ImGuiCol_TitleBgActive] = bgColor;
colors[ImGuiCol_TitleBgCollapsed] = bgColor;
colors[ImGuiCol_MenuBarBg] = panelColor;
colors[ImGuiCol_ScrollbarBg] = panelColor;
colors[ImGuiCol_ScrollbarGrab] = lightBgColor;
colors[ImGuiCol_ScrollbarGrabHovered] = veryLightBgColor;
colors[ImGuiCol_ScrollbarGrabActive] = veryLightBgColor;
colors[ImGuiCol_CheckMark] = panelActiveColor;
colors[ImGuiCol_SliderGrab] = ColorFromBytes(70, 70, 70);
colors[ImGuiCol_SliderGrabActive] = ColorFromBytes(60, 60, 60);
colors[ImGuiCol_Button] = panelColor;
colors[ImGuiCol_ButtonHovered] = panelHoverColor;
colors[ImGuiCol_ButtonActive] = panelHoverColor;
colors[ImGuiCol_Header] = panelColor;
colors[ImGuiCol_HeaderHovered] = panelHoverColor;
colors[ImGuiCol_HeaderActive] = panelActiveColor;
colors[ImGuiCol_Separator] = borderColor;
colors[ImGuiCol_SeparatorHovered] = borderColor;
colors[ImGuiCol_SeparatorActive] = borderColor;
colors[ImGuiCol_ResizeGrip] = bgColor;
colors[ImGuiCol_ResizeGripHovered] = panelColor;
colors[ImGuiCol_ResizeGripActive] = lightBgColor;
colors[ImGuiCol_PlotLines] = panelActiveColor;
colors[ImGuiCol_PlotLinesHovered] = panelHoverColor;
colors[ImGuiCol_PlotHistogram] = panelActiveColor;
colors[ImGuiCol_PlotHistogramHovered] = panelHoverColor;
colors[ImGuiCol_DragDropTarget] = bgColor;
colors[ImGuiCol_NavHighlight] = bgColor;
colors[ImGuiCol_DockingPreview] = panelActiveColor;
colors[ImGuiCol_Tab] = bgColor;
colors[ImGuiCol_TabActive] = borderColor;
colors[ImGuiCol_TabUnfocused] = panelColor;
colors[ImGuiCol_TabUnfocusedActive] = panelActiveColor;
colors[ImGuiCol_TabHovered] = panelHoverColor;
style.WindowRounding = 0.0f;
style.ChildRounding = 0.0f;
style.FrameRounding = 0.0f;
style.GrabRounding = 0.0f;
style.PopupRounding = 0.0f;
style.ScrollbarRounding = 0.0f;
style.TabRounding = 0.0f;
//style.WindowBorderSize = 0.f;
ImGui::GetStyle().AntiAliasedLines = false;
ImGui::GetStyle().AntiAliasedFill = false;
}
void CreateNearestNeighborSamplerState(ID3D11Device* device) {
D3D11_SAMPLER_DESC sampDesc = {};
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_POINT; // Use point filtering for sharpness
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_CLAMP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
HRESULT hr = device->CreateSamplerState(&sampDesc, &g_pSamplerStatePoint);
if (FAILED(hr)) printf("Failed to create nearest-neighbor sampler state: 0x%08X\n", hr);
}
// Main code
int main(int, char**)
{
// Create application window
//ImGui_ImplWin32_EnableDpiAwareness();
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, WndProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, _T("Painteroni"), NULL };
wc.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE("gfx/ns.ico")); // Set your custom icon
wc.hIconSm = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE("gfx/ns.ico")); // Small icon
::RegisterClassEx(&wc);
g_app.windowHandle = ::CreateWindow(wc.lpszClassName, _T("Nostalgia Paint"), WS_OVERLAPPEDWINDOW, 100, 100, 1000, 720, NULL, NULL, wc.hInstance, NULL);
// Initialize Direct3D
if (!CreateDeviceD3D(g_app.windowHandle))
{
CleanupDeviceD3D();
::UnregisterClass(wc.lpszClassName, wc.hInstance);
return 1;
}
else {
// Load the icon from file
HANDLE hIcon = LoadImage(GetModuleHandle(NULL), _T("gfx/ns.ico"), IMAGE_ICON, 32, 32, LR_LOADFROMFILE);
// Set the icon
SendMessage(g_app.windowHandle, WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
SendMessage(g_app.windowHandle, WM_SETICON, ICON_BIG, (LPARAM)hIcon);
}
// Show the window
::ShowWindow(g_app.windowHandle, SW_SHOWDEFAULT);
::UpdateWindow(g_app.windowHandle);
// Accept file dragging
DragAcceptFiles(g_app.windowHandle, TRUE);
// Setup Dear ImGui context
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImFontConfig config;
config.OversampleH = 1; config.OversampleV = 1;
// Store default font
io.Fonts->AddFontFromMemoryTTF(const_cast<std::uint8_t*>(Custom), sizeof(Custom), 17.0f, &config, io.Fonts->GetGlyphRangesDefault());
//io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\Tahoma.ttf", 18.0f, &config, io.Fonts->GetGlyphRangesDefault());
config.MergeMode = true;
// Chinese / Japanese fonts
if (std::filesystem::exists("c:\\Windows\\Fonts\\msyh.ttc"))
io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\msyh.ttc", 17.0f, &config, io.Fonts->GetGlyphRangesChineseSimplifiedCommon());
// Load icons!
{
//config.GlyphMinAdvanceX = 35.0f; // Use if you want to make the icon monospaced
static const ImWchar icon_ranges[] = { 0xf000, 0xf3ff, 0 };
io.Fonts->AddFontFromMemoryCompressedTTF(font_awesome_data, font_awesome_size, 17.0f, &config, icon_ranges);
}
// Build font atlas
ImGuiFreeType::BuildFontAtlas(io.Fonts, ImGuiFreeTypeBuilderFlags_ForceAutoHint);
FT_Library ft;
// Initialize FreeType
if (FT_Init_FreeType(&ft)) {
printf("Could not initialize FreeType library\n");
return 0;
}
// Load the font from memory
if (FT_New_Memory_Face(ft, Custom, sizeof(Custom), 0, &face)) {
printf("Failed to load font from memory\n");
return 0;
}
// Set the default font size
FT_Set_Pixel_Sizes(face, 0, 32);
// Setup our IMGUI theme!
SetupTheme();
// Setup Platform/Renderer backends
ImGui_ImplWin32_Init(g_app.windowHandle);
ImGui_ImplDX11_Init(g_app.g_pd3dDevice, g_app.g_pd3dDeviceContext);
CreateNearestNeighborSamplerState(g_app.g_pd3dDevice);
// Load our images!
g_assets.LoadAssets();
// Main loop
while (true)
{
// Poll and handle messages (inputs, window resize, etc.)
// See the WndProc() function below for our to dispatch events to the Win32 backend.
MSG msg;
while (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
if (msg.message == WM_QUIT)
goto shutdown_program;
}
// Start the Dear ImGui frame
ImGui_ImplDX11_NewFrame();
ImGui_ImplWin32_NewFrame();
ImGui::NewFrame();
// Update UI state / Canvas
g_canvas[g_cidx].Editor();
if (g_app.ui_state) g_app.ui_state->Update();
{
cGUI gui;
gui.Display();
}
// Rendering
ImGui::Render();
constexpr float clear_color_with_alpha[4] = { 0.1f, 0.1f, 0.1f, 1.f };
g_app.g_pd3dDeviceContext->OMSetRenderTargets(1, &g_mainRenderTargetView, NULL);
g_app.g_pd3dDeviceContext->ClearRenderTargetView(g_mainRenderTargetView, clear_color_with_alpha);
ImGui_ImplDX11_RenderDrawData(ImGui::GetDrawData());
g_pSwapChain->Present(1, 0); // Present with vsync
}
shutdown_program:
// Cleanup
if (canvasTexture) {
canvasTexture->Release();
canvasTexture = nullptr;
}
if (canvasSRV) {
canvasSRV->Release();
canvasSRV = nullptr;
}
if (face) {
FT_Done_Face(face);
face = nullptr;
}
if (ft) {
FT_Done_FreeType(ft);
ft = nullptr;
}
ImGui_ImplDX11_Shutdown();
ImGui_ImplWin32_Shutdown();
ImGui::DestroyContext();
CleanupDeviceD3D();
::DestroyWindow(g_app.windowHandle);
::UnregisterClass(wc.lpszClassName, wc.hInstance);
return 0;
}
// Helper functions
bool CreateDeviceD3D(HWND hWnd)
{
// Setup swap chain
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory(&sd, sizeof(sd));
sd.BufferCount = 2;
sd.BufferDesc.Width = 0;
sd.BufferDesc.Height = 0;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = hWnd;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
sd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
UINT createDeviceFlags = 0;
//createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
D3D_FEATURE_LEVEL featureLevel;
const D3D_FEATURE_LEVEL featureLevelArray[2] = { D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_0, };
if (D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, createDeviceFlags, featureLevelArray, 2, D3D11_SDK_VERSION, &sd, &g_pSwapChain, &g_app.g_pd3dDevice, &featureLevel, &g_app.g_pd3dDeviceContext) != S_OK)
return false;
CreateRenderTarget();
return true;
}
void CleanupDeviceD3D()
{
CleanupRenderTarget();
if (g_pSwapChain) { g_pSwapChain->Release(); g_pSwapChain = NULL; }
if (g_app.g_pd3dDeviceContext) { g_app.g_pd3dDeviceContext->Release(); g_app.g_pd3dDeviceContext = NULL; }
if (g_app.g_pd3dDevice) { g_app.g_pd3dDevice->Release(); g_app.g_pd3dDevice = NULL; }
}
void CreateRenderTarget()
{
ID3D11Texture2D* pBackBuffer;
g_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&pBackBuffer));
g_app.g_pd3dDevice->CreateRenderTargetView(pBackBuffer, NULL, &g_mainRenderTargetView);
pBackBuffer->Release();
}
void CleanupRenderTarget()
{
if (g_mainRenderTargetView) { g_mainRenderTargetView->Release(); g_mainRenderTargetView = NULL; }
}
// Forward declare message handler from imgui_impl_win32.cpp
extern IMGUI_IMPL_API LRESULT ImGui_ImplWin32_WndProcHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
// Win32 message handler
// You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
// - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
// - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
// Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
if (ImGui_ImplWin32_WndProcHandler(hWnd, msg, wParam, lParam)) return true;
switch (msg)
{
case WM_POINTERUPDATE: {
UINT32 pointerId = GET_POINTERID_WPARAM(wParam);
POINTER_INFO pointerInfo;
if (GetPointerInfo(pointerId, &pointerInfo)) {
if (pointerInfo.pointerType == PT_PEN) {
POINTER_PEN_INFO penInfo;
if (GetPointerPenInfo(pointerId, &penInfo)) {
// Get pressure between 0.0 (no pressure) and 1.0 (max pressure)
pen_pressure = penInfo.pressure / 1024.0f; // Maximum pressure value is 1024
std::cout << "Pressure: " << pen_pressure << std::endl;
}
}
}
break;
}
case WM_SIZE:
if (g_app.g_pd3dDevice != NULL && wParam != SIZE_MINIMIZED)
{
CleanupRenderTarget();
g_pSwapChain->ResizeBuffers(0, (UINT)LOWORD(lParam), (UINT)HIWORD(lParam), DXGI_FORMAT_UNKNOWN, 0);
CreateRenderTarget();
}
return 0;
case WM_SYSCOMMAND:
if ((wParam & 0xfff0) == SC_KEYMENU) return 0; // Disable ALT application menu
break;
case WM_DROPFILES:
{
HDROP hDrop = (HDROP)wParam;
UINT fileCount = DragQueryFile(hDrop, 0xFFFFFFFF, NULL, 0);
for (UINT i = 0; i < fileCount; ++i)
{
char filePath[MAX_PATH];
DragQueryFileA(hDrop, i, filePath, MAX_PATH);
// Use the file path as needed
const std::string extension = std::string(filePath).substr(std::string(filePath).find_last_of('.'));
if (extension == ".jpg" || extension == ".jpeg" || extension == ".png" || extension == ".bmp" || extension == ".tga")
{
std::cout << "Loading image file: " << filePath << std::endl;
g_util.LoadImageFileToCanvas(filePath, std::string(filePath).substr(std::string(filePath).find_last_of('\\') + 1));
g_canvas[g_cidx].UpdateCanvasHistory();
}
else
std::cerr << "Unsupported file format: " << extension << std::endl;
}
DragFinish(hDrop);
}
break;
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}
return ::DefWindowProc(hWnd, msg, wParam, lParam);
}