2017-01-15 23:04:51 +03:00
|
|
|
/* nuklear - 1.32.0 - public domain */
|
2016-04-22 18:41:40 +03:00
|
|
|
#define COBJMACROS
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <windows.h>
|
|
|
|
#include <d3d11.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2016-04-30 21:24:59 +03:00
|
|
|
#include <limits.h>
|
|
|
|
#include <time.h>
|
2016-04-22 18:41:40 +03:00
|
|
|
|
|
|
|
#define WINDOW_WIDTH 800
|
|
|
|
#define WINDOW_HEIGHT 600
|
|
|
|
|
|
|
|
#define MAX_VERTEX_BUFFER 512 * 1024
|
|
|
|
#define MAX_INDEX_BUFFER 128 * 1024
|
|
|
|
|
|
|
|
#define NK_INCLUDE_FIXED_TYPES
|
|
|
|
#define NK_INCLUDE_STANDARD_IO
|
2016-08-06 18:44:00 +03:00
|
|
|
#define NK_INCLUDE_STANDARD_VARARGS
|
2016-04-22 18:41:40 +03:00
|
|
|
#define NK_INCLUDE_DEFAULT_ALLOCATOR
|
|
|
|
#define NK_INCLUDE_VERTEX_BUFFER_OUTPUT
|
|
|
|
#define NK_INCLUDE_FONT_BAKING
|
|
|
|
#define NK_INCLUDE_DEFAULT_FONT
|
2016-05-01 20:14:48 +03:00
|
|
|
#define NK_IMPLEMENTATION
|
|
|
|
#define NK_D3D11_IMPLEMENTATION
|
|
|
|
#include "../../nuklear.h"
|
2016-04-22 18:41:40 +03:00
|
|
|
#include "nuklear_d3d11.h"
|
|
|
|
|
2016-04-28 15:38:44 +03:00
|
|
|
/* ===============================================================
|
|
|
|
*
|
|
|
|
* EXAMPLE
|
|
|
|
*
|
|
|
|
* ===============================================================*/
|
|
|
|
/* This are some code examples to provide a small overview of what can be
|
2018-01-01 19:20:55 +03:00
|
|
|
* done with this library. To try out an example uncomment the defines */
|
|
|
|
/*#define INCLUDE_ALL */
|
|
|
|
/*#define INCLUDE_STYLE */
|
|
|
|
/*#define INCLUDE_CALCULATOR */
|
|
|
|
/*#define INCLUDE_OVERVIEW */
|
|
|
|
/*#define INCLUDE_NODE_EDITOR */
|
|
|
|
|
|
|
|
#ifdef INCLUDE_ALL
|
|
|
|
#define INCLUDE_STYLE
|
|
|
|
#define INCLUDE_CALCULATOR
|
|
|
|
#define INCLUDE_OVERVIEW
|
|
|
|
#define INCLUDE_NODE_EDITOR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef INCLUDE_STYLE
|
|
|
|
#include "../style.c"
|
|
|
|
#endif
|
|
|
|
#ifdef INCLUDE_CALCULATOR
|
|
|
|
#include "../calculator.c"
|
|
|
|
#endif
|
|
|
|
#ifdef INCLUDE_OVERVIEW
|
|
|
|
#include "../overview.c"
|
|
|
|
#endif
|
|
|
|
#ifdef INCLUDE_NODE_EDITOR
|
|
|
|
#include "../node_editor.c"
|
|
|
|
#endif
|
2016-04-28 15:38:44 +03:00
|
|
|
|
|
|
|
/* ===============================================================
|
|
|
|
*
|
|
|
|
* DEMO
|
|
|
|
*
|
|
|
|
* ===============================================================*/
|
2016-04-22 18:41:40 +03:00
|
|
|
static IDXGISwapChain *swap_chain;
|
|
|
|
static ID3D11Device *device;
|
|
|
|
static ID3D11DeviceContext *context;
|
|
|
|
static ID3D11RenderTargetView* rt_view;
|
|
|
|
|
|
|
|
static void
|
|
|
|
set_swap_chain_size(int width, int height)
|
|
|
|
{
|
|
|
|
ID3D11Texture2D *back_buffer;
|
|
|
|
D3D11_RENDER_TARGET_VIEW_DESC desc;
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
if (rt_view)
|
|
|
|
ID3D11RenderTargetView_Release(rt_view);
|
|
|
|
|
|
|
|
ID3D11DeviceContext_OMSetRenderTargets(context, 0, NULL, NULL);
|
|
|
|
|
|
|
|
hr = IDXGISwapChain_ResizeBuffers(swap_chain, 0, width, height, DXGI_FORMAT_UNKNOWN, 0);
|
|
|
|
if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET || hr == DXGI_ERROR_DRIVER_INTERNAL_ERROR)
|
|
|
|
{
|
|
|
|
/* to recover from this, you'll need to recreate device and all the resources */
|
|
|
|
MessageBoxW(NULL, L"DXGI device is removed or reset!", L"Error", 0);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
assert(SUCCEEDED(hr));
|
|
|
|
|
|
|
|
memset(&desc, 0, sizeof(desc));
|
|
|
|
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
|
|
|
|
|
2018-03-01 18:22:19 +03:00
|
|
|
hr = IDXGISwapChain_GetBuffer(swap_chain, 0, &IID_ID3D11Texture2D, (void **)&back_buffer);
|
2016-04-22 18:41:40 +03:00
|
|
|
assert(SUCCEEDED(hr));
|
|
|
|
|
|
|
|
hr = ID3D11Device_CreateRenderTargetView(device, (ID3D11Resource *)back_buffer, &desc, &rt_view);
|
|
|
|
assert(SUCCEEDED(hr));
|
|
|
|
|
|
|
|
ID3D11Texture2D_Release(back_buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static LRESULT CALLBACK
|
|
|
|
WindowProc(HWND wnd, UINT msg, WPARAM wparam, LPARAM lparam)
|
|
|
|
{
|
|
|
|
switch (msg)
|
|
|
|
{
|
|
|
|
case WM_DESTROY:
|
|
|
|
PostQuitMessage(0);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
case WM_SIZE:
|
|
|
|
if (swap_chain)
|
|
|
|
{
|
|
|
|
int width = LOWORD(lparam);
|
|
|
|
int height = HIWORD(lparam);
|
|
|
|
set_swap_chain_size(width, height);
|
|
|
|
nk_d3d11_resize(context, width, height);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nk_d3d11_handle_event(wnd, msg, wparam, lparam))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return DefWindowProcW(wnd, msg, wparam, lparam);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
struct nk_context *ctx;
|
2018-01-05 15:09:27 +03:00
|
|
|
struct nk_colorf bg;
|
2016-04-22 18:41:40 +03:00
|
|
|
|
|
|
|
WNDCLASSW wc;
|
|
|
|
RECT rect = { 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT };
|
|
|
|
DWORD style = WS_OVERLAPPEDWINDOW;
|
|
|
|
DWORD exstyle = WS_EX_APPWINDOW;
|
|
|
|
HWND wnd;
|
|
|
|
int running = 1;
|
|
|
|
HRESULT hr;
|
|
|
|
D3D_FEATURE_LEVEL feature_level;
|
|
|
|
DXGI_SWAP_CHAIN_DESC swap_chain_desc;
|
|
|
|
|
|
|
|
/* Win32 */
|
|
|
|
memset(&wc, 0, sizeof(wc));
|
2017-05-17 08:29:43 +03:00
|
|
|
wc.style = CS_DBLCLKS;
|
2016-04-22 18:41:40 +03:00
|
|
|
wc.lpfnWndProc = WindowProc;
|
|
|
|
wc.hInstance = GetModuleHandleW(0);
|
|
|
|
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
|
|
|
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
|
|
|
|
wc.lpszClassName = L"NuklearWindowClass";
|
|
|
|
RegisterClassW(&wc);
|
|
|
|
|
|
|
|
AdjustWindowRectEx(&rect, style, FALSE, exstyle);
|
|
|
|
|
|
|
|
wnd = CreateWindowExW(exstyle, wc.lpszClassName, L"Nuklear Demo",
|
|
|
|
style | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT,
|
|
|
|
rect.right - rect.left, rect.bottom - rect.top,
|
|
|
|
NULL, NULL, wc.hInstance, NULL);
|
|
|
|
|
|
|
|
/* D3D11 setup */
|
|
|
|
memset(&swap_chain_desc, 0, sizeof(swap_chain_desc));
|
|
|
|
swap_chain_desc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
|
|
|
|
swap_chain_desc.BufferDesc.RefreshRate.Numerator = 60;
|
|
|
|
swap_chain_desc.BufferDesc.RefreshRate.Denominator = 1;
|
|
|
|
swap_chain_desc.SampleDesc.Count = 1;
|
|
|
|
swap_chain_desc.SampleDesc.Quality = 0;
|
|
|
|
swap_chain_desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
|
|
|
|
swap_chain_desc.BufferCount = 1;
|
|
|
|
swap_chain_desc.OutputWindow = wnd;
|
|
|
|
swap_chain_desc.Windowed = TRUE;
|
|
|
|
swap_chain_desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
|
|
|
|
swap_chain_desc.Flags = 0;
|
|
|
|
if (FAILED(D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE,
|
|
|
|
NULL, 0, NULL, 0, D3D11_SDK_VERSION, &swap_chain_desc,
|
|
|
|
&swap_chain, &device, &feature_level, &context)))
|
|
|
|
{
|
|
|
|
/* if hardware device fails, then try WARP high-performance
|
|
|
|
software rasterizer, this is useful for RDP sessions */
|
|
|
|
hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_WARP,
|
|
|
|
NULL, 0, NULL, 0, D3D11_SDK_VERSION, &swap_chain_desc,
|
|
|
|
&swap_chain, &device, &feature_level, &context);
|
|
|
|
assert(SUCCEEDED(hr));
|
|
|
|
}
|
|
|
|
set_swap_chain_size(WINDOW_WIDTH, WINDOW_HEIGHT);
|
|
|
|
|
|
|
|
/* GUI */
|
|
|
|
ctx = nk_d3d11_init(device, WINDOW_WIDTH, WINDOW_HEIGHT, MAX_VERTEX_BUFFER, MAX_INDEX_BUFFER);
|
|
|
|
/* Load Fonts: if none of these are loaded a default font will be used */
|
2016-07-15 19:04:50 +03:00
|
|
|
/* Load Cursor: if you uncomment cursor loading please hide the cursor */
|
2016-04-22 18:41:40 +03:00
|
|
|
{struct nk_font_atlas *atlas;
|
|
|
|
nk_d3d11_font_stash_begin(&atlas);
|
2016-12-26 18:37:47 +03:00
|
|
|
/*struct nk_font *droid = nk_font_atlas_add_from_file(atlas, "../../extra_font/DroidSans.ttf", 14, 0);*/
|
|
|
|
/*struct nk_font *robot = nk_font_atlas_add_from_file(atlas, "../../extra_font/Roboto-Regular.ttf", 14, 0);*/
|
|
|
|
/*struct nk_font *future = nk_font_atlas_add_from_file(atlas, "../../extra_font/kenvector_future_thin.ttf", 13, 0);*/
|
|
|
|
/*struct nk_font *clean = nk_font_atlas_add_from_file(atlas, "../../extra_font/ProggyClean.ttf", 12, 0);*/
|
|
|
|
/*struct nk_font *tiny = nk_font_atlas_add_from_file(atlas, "../../extra_font/ProggyTiny.ttf", 10, 0);*/
|
|
|
|
/*struct nk_font *cousine = nk_font_atlas_add_from_file(atlas, "../../extra_font/Cousine-Regular.ttf", 13, 0);*/
|
2016-04-22 18:41:40 +03:00
|
|
|
nk_d3d11_font_stash_end();
|
2016-07-15 19:04:50 +03:00
|
|
|
/*nk_style_load_all_cursors(ctx, atlas->cursors);*/
|
2016-04-22 18:41:40 +03:00
|
|
|
/*nk_style_set_font(ctx, &droid->handle)*/;}
|
|
|
|
|
2016-04-28 15:38:44 +03:00
|
|
|
/* style.c */
|
2018-01-01 19:20:55 +03:00
|
|
|
#ifdef INCLUDE_STYLE
|
2016-04-28 15:38:44 +03:00
|
|
|
/*set_style(ctx, THEME_WHITE);*/
|
|
|
|
/*set_style(ctx, THEME_RED);*/
|
|
|
|
/*set_style(ctx, THEME_BLUE);*/
|
|
|
|
/*set_style(ctx, THEME_DARK);*/
|
2018-01-01 19:20:55 +03:00
|
|
|
#endif
|
2016-04-28 15:38:44 +03:00
|
|
|
|
2018-01-05 15:09:27 +03:00
|
|
|
bg.r = 0.10f, bg.g = 0.18f, bg.b = 0.24f, bg.a = 1.0f;
|
2016-04-22 18:41:40 +03:00
|
|
|
while (running)
|
|
|
|
{
|
|
|
|
/* Input */
|
2016-04-28 15:38:44 +03:00
|
|
|
MSG msg;
|
2016-04-22 18:41:40 +03:00
|
|
|
nk_input_begin(ctx);
|
|
|
|
while (PeekMessageW(&msg, NULL, 0, 0, PM_REMOVE))
|
|
|
|
{
|
|
|
|
if (msg.message == WM_QUIT)
|
|
|
|
running = 0;
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessageW(&msg);
|
|
|
|
}
|
|
|
|
nk_input_end(ctx);
|
|
|
|
|
|
|
|
/* GUI */
|
2016-10-30 00:23:46 +03:00
|
|
|
if (nk_begin(ctx, "Demo", nk_rect(50, 50, 230, 250),
|
2016-04-22 18:41:40 +03:00
|
|
|
NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
|
|
|
|
NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
|
|
|
|
{
|
|
|
|
enum {EASY, HARD};
|
|
|
|
static int op = EASY;
|
|
|
|
static int property = 20;
|
|
|
|
|
|
|
|
nk_layout_row_static(ctx, 30, 80, 1);
|
2016-07-15 19:04:50 +03:00
|
|
|
if (nk_button_label(ctx, "button"))
|
2016-04-22 18:41:40 +03:00
|
|
|
fprintf(stdout, "button pressed\n");
|
|
|
|
nk_layout_row_dynamic(ctx, 30, 2);
|
|
|
|
if (nk_option_label(ctx, "easy", op == EASY)) op = EASY;
|
|
|
|
if (nk_option_label(ctx, "hard", op == HARD)) op = HARD;
|
|
|
|
nk_layout_row_dynamic(ctx, 22, 1);
|
|
|
|
nk_property_int(ctx, "Compression:", 0, &property, 100, 10, 1);
|
|
|
|
|
|
|
|
nk_layout_row_dynamic(ctx, 20, 1);
|
|
|
|
nk_label(ctx, "background:", NK_TEXT_LEFT);
|
|
|
|
nk_layout_row_dynamic(ctx, 25, 1);
|
2018-01-05 15:09:27 +03:00
|
|
|
if (nk_combo_begin_color(ctx, nk_rgb_cf(bg), nk_vec2(nk_widget_width(ctx),400))) {
|
2016-04-22 18:41:40 +03:00
|
|
|
nk_layout_row_dynamic(ctx, 120, 1);
|
2018-01-05 15:09:27 +03:00
|
|
|
bg = nk_color_picker(ctx, bg, NK_RGBA);
|
2016-04-22 18:41:40 +03:00
|
|
|
nk_layout_row_dynamic(ctx, 25, 1);
|
2018-01-05 15:09:27 +03:00
|
|
|
bg.r = nk_propertyf(ctx, "#R:", 0, bg.r, 1.0f, 0.01f,0.005f);
|
|
|
|
bg.g = nk_propertyf(ctx, "#G:", 0, bg.g, 1.0f, 0.01f,0.005f);
|
|
|
|
bg.b = nk_propertyf(ctx, "#B:", 0, bg.b, 1.0f, 0.01f,0.005f);
|
|
|
|
bg.a = nk_propertyf(ctx, "#A:", 0, bg.a, 1.0f, 0.01f,0.005f);
|
2016-04-22 18:41:40 +03:00
|
|
|
nk_combo_end(ctx);
|
2016-10-30 00:23:46 +03:00
|
|
|
}
|
2016-04-22 18:41:40 +03:00
|
|
|
}
|
2016-10-30 00:23:46 +03:00
|
|
|
nk_end(ctx);
|
2016-04-22 18:41:40 +03:00
|
|
|
|
2016-04-28 15:38:44 +03:00
|
|
|
/* -------------- EXAMPLES ---------------- */
|
2018-01-01 19:20:55 +03:00
|
|
|
#ifdef INCLUDE_CALCULATOR
|
|
|
|
calculator(ctx);
|
|
|
|
#endif
|
|
|
|
#ifdef INCLUDE_OVERVIEW
|
|
|
|
overview(ctx);
|
|
|
|
#endif
|
|
|
|
#ifdef INCLUDE_NODE_EDITOR
|
|
|
|
node_editor(ctx);
|
|
|
|
#endif
|
2016-04-28 15:38:44 +03:00
|
|
|
/* ----------------------------------------- */
|
|
|
|
|
2018-01-05 15:09:27 +03:00
|
|
|
/* Draw */
|
|
|
|
ID3D11DeviceContext_ClearRenderTargetView(context, rt_view, &bg.r);
|
2016-04-28 15:38:44 +03:00
|
|
|
ID3D11DeviceContext_OMSetRenderTargets(context, 1, &rt_view, NULL);
|
|
|
|
nk_d3d11_render(context, NK_ANTI_ALIASING_ON);
|
|
|
|
hr = IDXGISwapChain_Present(swap_chain, 1, 0);
|
|
|
|
if (hr == DXGI_ERROR_DEVICE_RESET || hr == DXGI_ERROR_DEVICE_REMOVED) {
|
|
|
|
/* to recover from this, you'll need to recreate device and all the resources */
|
|
|
|
MessageBoxW(NULL, L"D3D11 device is lost or removed!", L"Error", 0);
|
|
|
|
break;
|
|
|
|
} else if (hr == DXGI_STATUS_OCCLUDED) {
|
|
|
|
/* window is not visible, so vsync won't work. Let's sleep a bit to reduce CPU usage */
|
|
|
|
Sleep(10);
|
2016-04-22 18:41:40 +03:00
|
|
|
}
|
2018-01-05 15:09:27 +03:00
|
|
|
assert(SUCCEEDED(hr));
|
2016-04-22 18:41:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ID3D11DeviceContext_ClearState(context);
|
|
|
|
nk_d3d11_shutdown();
|
2018-03-14 12:14:17 +03:00
|
|
|
ID3D11RenderTargetView_Release(rt_view);
|
2016-04-22 18:41:40 +03:00
|
|
|
ID3D11DeviceContext_Release(context);
|
|
|
|
ID3D11Device_Release(device);
|
|
|
|
IDXGISwapChain_Release(swap_chain);
|
|
|
|
UnregisterClassW(wc.lpszClassName, wc.hInstance);
|
|
|
|
return 0;
|
|
|
|
}
|