This is an automated email from the git hooks/post-receive script.
git pushed a commit to branch master
in repository Direct3D.wiki.
View the commit online.
commit 00bdcd90c6cb74a50bf3f098dc2884feb0118f2e
Author: Vincent Torri <vincent.to...@gmail.com>
AuthorDate: Thu Jul 27 10:43:48 2023 -0700
Update 'Direct3D 11 for 2D'
---
Direct3D-11-for-2D.md | 388 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 388 insertions(+)
diff --git a/Direct3D-11-for-2D.md b/Direct3D-11-for-2D.md
index 06b684f..e5cf502 100644
--- a/Direct3D-11-for-2D.md
+++ b/Direct3D-11-for-2D.md
@@ -15,3 +15,391 @@ References:
* [tutorials points](https://www.tutorialspoint.com/directx/directx_overview.htm)
* [Direct3D 11.3 Functional Specification](https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm)
* and of course MSDN !
+
+# Creating a window
+
+There are numerous tutorials and examples on Internet about creating a window on Windows. So the code is presented below, with some remarks at the end. Here we also use a header file below, `win.h`, which contains structures and interface.
+
+```c
+#ifndef WIN_H
+#define WIN_H
+
+/* comment for no debug informations */
+#define _DEBUG
+
+typedef struct Window Window;
+typedef struct D3d D3d;
+
+struct Window
+{
+ HINSTANCE instance;
+ RECT rect;
+ HWND win;
+ D3d *d3d;
+ int rotation; /* rotation (clockwise) in degrees */
+ unsigned int fullscreen: 1;
+};
+
+D3d *d3d_init(Window *win, int vsync);
+
+void d3d_shutdown(D3d *d3d);
+
+void d3d_resize(D3d *d3d, UINT width, UINT height);
+
+void d3d_render(D3d *d3d);
+
+#endif
+```
+
+And here is the corresponding `win_1.c` file below. The compilation command is at the top of the file.
+
+
+```c
+/*
+ * Windows 10:
+
+ gcc -g -O2 -Wall -Wextra -o d3d d3d_2.c win_2.c -ld3d11 -ld3dcompiler -ldxgi -luuid -D_WIN32_WINNT=0x0A00
+
+ * Windows 7:
+
+ gcc -g -O2 -Wall -Wextra -o d3d d3d_2.c win_2.c -ld3d11 -ld3dcompiler -ldxgi -luuid -D_WIN32_WINNT=0x0601
+
+ */
+
+#if defined _WIN32_WINNT && _WIN32_WINNT >= 0x0A00
+# define HAVE_WIN10
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+
+#ifndef WIN32_LEAN_AND_MEAN
+# define WIN32_LEAN_AND_MEAN
+#endif
+
+#include <windows.h>
+
+#include "win.h"
+
+void window_fullscreen_set(Window *win, unsigned int fullscreen);
+
+void window_rotation_set(Window *win, int rotation);
+
+/************************* Window *************************/
+
+LRESULT CALLBACK
+_window_procedure(HWND window,
+ UINT message,
+ WPARAM window_param,
+ LPARAM data_param)
+{
+ switch (message)
+ {
+ case WM_CLOSE:
+ PostQuitMessage(0);
+ return 0;
+ case WM_KEYUP:
+ if (window_param == 'Q')
+ {
+ PostQuitMessage(0);
+ }
+ if (window_param == 'F')
+ {
+ Window *win;
+
+#ifdef _DEBUG
+ printf("fullscreen\n");
+ fflush(stdout);
+#endif
+ win = (Window *)GetWindowLongPtr(window, GWLP_USERDATA);
+ window_fullscreen_set(win, !win->fullscreen);
+ }
+ if (window_param == 'R')
+ {
+ Window *win;
+
+#ifdef _DEBUG
+ printf("rotation\n");
+ fflush(stdout);
+#endif
+ win = (Window *)GetWindowLongPtr(window, GWLP_USERDATA);
+ window_rotation_set(win, (win->rotation + 90) % 360);
+ }
+ return 0;
+ case WM_ERASEBKGND:
+ /* no need to erase back */
+ return 1;
+ /* GDI notifications */
+ case WM_CREATE:
+#ifdef _DEBUG
+ printf(" * WM_CREATE\n");
+ fflush(stdout);
+#endif
+ return 0;
+ case WM_SIZE:
+ {
+#ifdef _DEBUG
+ printf(" * WM_SIZE\n");
+ fflush(stdout);
+#endif
+
+ /*
+ * here, we will change the size of the needed d3d objects:
+ * - the viewport
+ * - the swap chain
+ * - the render target view
+ */
+
+ return 0;
+ }
+ case WM_PAINT:
+ {
+#ifdef _DEBUG
+ printf(" * WM_PAINT\n");
+ fflush(stdout);
+#endif
+
+ if (GetUpdateRect(window, NULL, FALSE))
+ {
+ PAINTSTRUCT ps;
+ BeginPaint(window, &ps);
+
+ /* here, we will render a frame */
+
+ EndPaint(window, &ps);
+ }
+
+ return 0;
+ }
+ default:
+ return DefWindowProc(window, message, window_param, data_param);
+ }
+}
+
+Window *window_new(int x, int y, int w, int h)
+{
+ WNDCLASS wc;
+ RECT r;
+ Window *win;
+
+ win = (Window *)calloc(1, sizeof(Window));
+ if (!win)
+ return NULL;
+
+ win->instance = GetModuleHandle(NULL);
+ if (!win->instance)
+ goto free_win;
+
+ memset (&wc, 0, sizeof (WNDCLASS));
+ wc.style = CS_HREDRAW | CS_VREDRAW;
+ wc.lpfnWndProc = _window_procedure;
+ wc.cbClsExtra = 0;
+ wc.cbWndExtra = 0;
+ wc.hInstance = win->instance;
+ wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
+ wc.hCursor = LoadCursor (NULL, IDC_ARROW);
+ wc.hbrBackground = NULL;
+ wc.lpszMenuName = NULL;
+ wc.lpszClassName = "D3D";
+
+ if(!RegisterClass(&wc))
+ goto free_library;
+
+ r.left = 0;
+ r.top = 0;
+ r.right = w;
+ r.bottom = h;
+ if (!AdjustWindowRectEx(&r,
+ WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
+ FALSE,
+ 0U))
+ goto unregister_class;
+
+ win->win = CreateWindowEx(0U,
+ "D3D", "Test",
+ WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
+ x, y,
+ r.right - r.left,
+ r.bottom - r.top,
+ NULL,
+ NULL, win->instance, NULL);
+ if (!win->win)
+ goto unregister_class;
+
+ return win;
+
+ unregister_class:
+ UnregisterClass("D2D", win->instance);
+ free_library:
+ FreeLibrary(win->instance);
+ free_win:
+ free(win);
+
+ return NULL;
+}
+
+void window_del(Window *win)
+{
+ if (!win)
+ return;
+
+ DestroyWindow(win->win);
+ UnregisterClass("D2D", win->instance);
+ FreeLibrary(win->instance);
+ free(win);
+}
+
+void window_show(Window *win)
+{
+ ShowWindow(win->win, SW_SHOWNORMAL);
+}
+
+void window_fullscreen_set(Window *win, unsigned int on)
+{
+ HWND prev;
+ DWORD style;
+ DWORD exstyle;
+ UINT flags;
+ int x;
+ int y;
+ int w;
+ int h;
+
+ on = !!on;
+ if ((win->fullscreen && on) ||
+ (!win->fullscreen && !on))
+ return;
+
+ if (on)
+ {
+ MONITORINFO mi;
+ HMONITOR monitor;
+
+ if (!GetWindowRect(win->win, &win->rect))
+ {
+ printf("GetWindowRect() failed\n");
+ return;
+ }
+
+ monitor = MonitorFromWindow(win->win, MONITOR_DEFAULTTONEAREST);
+ mi.cbSize = sizeof(MONITORINFO);
+ if (!GetMonitorInfo(monitor, &mi))
+ return;
+
+ style = WS_VISIBLE | WS_POPUP;
+ exstyle = WS_EX_TOPMOST;
+ prev = HWND_TOPMOST;
+ x = 0;
+ y = 0;
+ w = mi.rcMonitor.right - mi.rcMonitor.left;
+ h = mi.rcMonitor.bottom - mi.rcMonitor.top;
+ flags = SWP_NOCOPYBITS | SWP_SHOWWINDOW;
+ }
+ else
+ {
+
+ style = WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
+ exstyle = 0U;
+ prev = HWND_NOTOPMOST;
+ x = win->rect.left;
+ y = win->rect.top;
+ w = win->rect.right - win->rect.left;
+ h = win->rect.bottom - win->rect.top;
+ flags = SWP_NOCOPYBITS | SWP_SHOWWINDOW;
+ }
+
+ SetLastError(0);
+ if (!SetWindowLongPtr(win->win, GWL_STYLE, style) &&
+ (GetLastError() != 0))
+ {
+ printf("SetWindowLongPtr() failed\n");
+ return;
+ }
+ SetLastError(0);
+ if (!SetWindowLongPtr(win->win, GWL_EXSTYLE, exstyle) &&
+ (GetLastError() != 0))
+ {
+ printf("SetWindowLongPtr() failed\n");
+ return;
+ }
+ if (!SetWindowPos(win->win, prev, x, y, w, h, flags))
+ {
+ printf("SetWindowPos() failed\n");
+ return;
+ }
+
+ win->fullscreen = on;
+}
+
+void window_rotation_set(Window *win, int rotation)
+{
+ int rdiff;
+
+ if (win->rotation == rotation)
+ return;
+
+ rdiff = win->rotation - rotation;
+ if (rdiff < 0) rdiff = -rdiff;
+
+ if (rdiff != 180)
+ {
+ RECT r;
+
+ if (!GetWindowRect(win->win, &r))
+ return;
+
+ if (!MoveWindow(win->win,
+ r.left, r.top,
+ r.bottom - r.top, r.right - r.left,
+ TRUE))
+ {
+ printf("MoveWindow() failed\n");
+ return;
+ }
+ }
+
+ win->rotation = rotation;
+}
+
+int main()
+{
+ Window *win;
+
+ /* remove scaling on HiDPI */
+#if _WIN32_WINNT >= 0x0A00
+ SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
+#endif
+
+ win = window_new(100, 100, 800, 480);
+ if (!win)
+ return 1;
+
+ SetWindowLongPtr(win->win, GWLP_USERDATA, (LONG_PTR)win);
+
+ window_show(win);
+
+ /* mesage loop */
+ while(1)
+ {
+ MSG msg;
+ BOOL ret;
+
+ ret = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
+ if (ret)
+ {
+ do
+ {
+ if (msg.message == WM_QUIT)
+ goto beach;
+ TranslateMessage(&msg);
+ DispatchMessageW(&msg);
+ } while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
+ }
+ }
+
+ beach:
+ window_del(win);
+
+ return 0;
+}
+```
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.