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 46c5436deb3c7eb8defd81a6ff2c06b25572a07d
Author: Vincent Torri <vincent.to...@gmail.com>
AuthorDate: Thu Jul 27 23:17:06 2023 -0700
Update 'Direct3D 11 for 2D'
---
Direct3D-11-for-2D.md | 340 +++++++++++++++++++++++++++++++++++++-------------
1 file changed, 254 insertions(+), 86 deletions(-)
diff --git a/Direct3D-11-for-2D.md b/Direct3D-11-for-2D.md
index dacceb3..d737b9f 100644
--- a/Direct3D-11-for-2D.md
+++ b/Direct3D-11-for-2D.md
@@ -3,7 +3,7 @@ This tutorial aims at presenting the usage of Direct3D 11 in 2D, without any hel
All the code in this tutorial can compile with Visual Studio, MSYS2 + mingw-w64, and even with cross-compilation on Unix provided the mingw-w64 toolchain is installed.
-The code will use the Direct3D and DXGI C API when it is possible, and will target Windows 7 or Windows 10 (or above).
+The code will use the Direct3D and DXGI C API when it is possible, and will target Windows 10 or above.
At the top of each example, a compilation of the code is added.
@@ -36,7 +36,7 @@ struct Window
RECT rect;
HWND win;
D3d *d3d;
- int rotation; /* rotation (clockwise) in degrees */
+ int rotation; /* rotation (clockwise): 0, 1, 2 3 */
unsigned int fullscreen: 1;
};
@@ -44,32 +44,29 @@ D3d *d3d_init(Window *win, int vsync);
void d3d_shutdown(D3d *d3d);
-void d3d_resize(D3d *d3d, UINT width, UINT height);
+void d3d_scene_begin(D3d *d3d);
+
+void d3d_scene_end(void);
+
+void d3d_resize(D3d *d3d, int rot, 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.
+Here is the corresponding `win.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
-
+ * compilation command (Windows 10 or above):
+ *
+ * gcc -g -O2 -Wall -Wextra -o d3d d3d_0.c win.c -ld3d11 -ld3dcompiler -ldxgi -luuid -D_WIN32_WINNT=0x0A00
+ *
+ * Note: change the d3d_*.c file for each steps of the tutorial
*/
-#if defined _WIN32_WINNT && _WIN32_WINNT >= 0x0A00
-# define HAVE_WIN10
-#endif
-
#include <stdlib.h>
#include <stdio.h>
@@ -93,86 +90,116 @@ _window_procedure(HWND window,
WPARAM window_param,
LPARAM data_param)
{
- switch (message)
+ switch (message)
{
- case WM_CLOSE:
- PostQuitMessage(0);
- return 0;
- case WM_KEYUP:
- if (window_param == 'Q')
- {
+ case WM_CLOSE:
PostQuitMessage(0);
- }
- if (window_param == 'F')
- {
- Window *win;
+ return 0;
+ case WM_KEYUP:
+ if (window_param == 'Q')
+ {
+ PostQuitMessage(0);
+ }
+ if (window_param == 'F')
+ {
+ Window *win;
#ifdef _DEBUG
- printf("fullscreen\n");
+ 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 + 1) % 4);
+ }
+ if (window_param == 'D')
+ {
+/* RECT r; */
+/* Window* win; */
+
+/* #ifdef _DEBUG */
+/* printf("draw texture\n"); */
+/* fflush(stdout); */
+/* #endif */
+/* win = (Window*)GetWindowLongPtr(window, GWLP_USERDATA); */
+ }
+ if (window_param == 'U')
+ {
+ RECT r;
+ Window* win;
+
+#ifdef _DEBUG
+ printf("update d3d\n");
+ fflush(stdout);
+#endif
+ win = (Window*)GetWindowLongPtr(window, GWLP_USERDATA);
+ GetClientRect(window, &r);
+ d3d_resize(win->d3d,
+ win->rotation,
+ r.right - r.left, r.bottom - r.top);
+ d3d_render(win->d3d);
+ }
+ 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
- win = (Window *)GetWindowLongPtr(window, GWLP_USERDATA);
- window_fullscreen_set(win, !win->fullscreen);
- }
- if (window_param == 'R')
+ return 0;
+ case WM_SIZE:
{
- Window *win;
+ Window * win;
#ifdef _DEBUG
- printf("rotation\n");
+ printf(" * WM_SIZE : %u %u\n", (UINT)LOWORD(data_param), (UINT)HIWORD(data_param));
fflush(stdout);
#endif
+
win = (Window *)GetWindowLongPtr(window, GWLP_USERDATA);
- window_rotation_set(win, (win->rotation + 90) % 360);
+ d3d_resize(win->d3d,
+ win->rotation,
+ (UINT)LOWORD(data_param), (UINT)HIWORD(data_param));
+
+ return 0;
}
- 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:
- {
+ case WM_PAINT:
+ {
#ifdef _DEBUG
- printf(" * WM_SIZE\n");
- fflush(stdout);
+ printf(" * WM_PAINT\n");
+ fflush(stdout);
#endif
- /*
- * here, we will change the size of the needed d3d objects:
- * - the viewport
- * - the swap chain
- * - the render target view
- */
+ if (GetUpdateRect(window, NULL, FALSE))
+ {
+ PAINTSTRUCT ps;
+ Window *win;
- return 0;
- }
- case WM_PAINT:
- {
-#ifdef _DEBUG
- printf(" * WM_PAINT\n");
- fflush(stdout);
-#endif
+ BeginPaint(window, &ps);
- if (GetUpdateRect(window, NULL, FALSE))
- {
- PAINTSTRUCT ps;
- BeginPaint(window, &ps);
+ win = (Window *)GetWindowLongPtr(window, GWLP_USERDATA);
+ d3d_render(win->d3d);
- /* here, we will render a frame */
+ EndPaint(window, &ps);
+ }
- EndPaint(window, &ps);
+ return 0;
}
-
- return 0;
- }
- default:
- return DefWindowProc(window, message, window_param, data_param);
+ default:
+ return DefWindowProc(window, message, window_param, data_param);
}
}
@@ -215,8 +242,11 @@ Window *window_new(int x, int y, int w, int h)
0U))
goto unregister_class;
+ printf("window new : %d %d %ld %ld", w, h, r.right - r.left, r.bottom - r.top);
+ fflush(stdout);
+
win->win = CreateWindowEx(0U,
- "D3D", "Test",
+ "D3D", "Direct3D",
WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
x, y,
r.right - r.left,
@@ -341,38 +371,84 @@ void window_rotation_set(Window *win, int rotation)
rdiff = win->rotation - rotation;
if (rdiff < 0) rdiff = -rdiff;
- if (rdiff != 180)
+ if (rdiff != 2)
{
RECT r;
+ RECT r2;
+ int x;
+ int y;
+
+ win->rotation = rotation;
if (!GetWindowRect(win->win, &r))
+ {
+ printf("GetClient failed\n");
+ return;
+ }
+
+ x = r.left;
+ y = r.top;
+
+ if (!GetClientRect(win->win, &r))
+ {
+ printf("GetClient failed\n");
return;
+ }
+
+ printf(" * win rot : %ld %ld\n", r.bottom - r.top, r.right - r.left);
+ fflush(stdout);
+
+ r2.left = 0;
+ r2.top = 0;
+ r2.right = r.bottom - r.top;
+ r2.bottom = r.right - r.left;
+ if (!AdjustWindowRectEx(&r2,
+ WS_OVERLAPPEDWINDOW | WS_SIZEBOX,
+ FALSE,
+ 0U))
+ {
+ printf("AdjustWindowRectEx failed\n");
+ return;
+ }
+
+ printf(" * win rot 2 : %ld %ld\n", r2.bottom - r2.top, r2.right - r2.left);
+ fflush(stdout);
if (!MoveWindow(win->win,
- r.left, r.top,
- r.bottom - r.top, r.right - r.left,
+ x, y,
+ r2.right - r2.left, r2.bottom - r2.top,
TRUE))
{
printf("MoveWindow() failed\n");
return;
}
}
-
- win->rotation = rotation;
}
int main()
{
Window *win;
+ D3d *d3d;
+ int ret = 1;
/* 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;
+ return ret;
+
+ d3d = d3d_init(win, 0);
+ if (!d3d)
+ {
+ printf(" * d3d_init() failed\n");
+ fflush(stdout);
+ goto del_window;
+ }
+
+ d3d_scene_begin(d3d);
+
+ ret = 0;
SetWindowLongPtr(win->win, GWLP_USERDATA, (LONG_PTR)win);
@@ -390,7 +466,7 @@ int main()
do
{
if (msg.message == WM_QUIT)
- goto beach;
+ goto beach;
TranslateMessage(&msg);
DispatchMessageW(&msg);
} while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE));
@@ -398,14 +474,106 @@ int main()
}
beach:
+ d3d_scene_end();
+ d3d_shutdown(d3d);
+ del_window:
window_del(win);
- return 0;
+ return ret;
+}
+```
+
+As you can see, there are some Direct3D function in this code. For now, they are just stub functions. The file is `d3d_0.c`. For each step of the tutorial, the name of the file in the compilation command will need to be changed.
+
+```c
+/*
+ * Tutorial part 0
+ *
+ * Only stub functions, no Direct3D functions yet
+ *
+ * Compilation:
+ *
+ * gcc -g -O2 -Wall -Wextra -o d3d_0 d3d_0.c win.c -ld3d11 -ldxgi -luuid -D_WIN32_WINNT=0x0A00
+ */
+
+#include <stdlib.h> /* calloc() free() */
+#include <stdio.h> /* printf() fflush() */
+
+#define _DEBUG
+
+/* C API for d3d11 */
+#define COBJMACROS
+
+#include <dxgi1_3.h> /* DXGI interface */
+#include <d3d11.h> /* D3D11 interface */
+#include <d3dcompiler.h> /* compilation of shader */
+
+#include "win.h"
+
+#ifdef _DEBUG
+# define FCT \
+do { printf(" * %s\n", __FUNCTION__); fflush(stdout); } while (0)
+#else
+# define FCT \
+do { } while (0)
+#endif
+
+struct D3d
+{
+ unsigned int vsync : 1;
+};
+
+/************************** D3D11 **************************/
+
+D3d *d3d_init(Window *win, int vsync)
+{
+ D3d *d3d;
+
+ d3d = (D3d *)calloc(1, sizeof(D3d));
+ if (!d3d)
+ return NULL;
+
+ return d3d;
+
+ (void)win;
+ (void)vsync;
+}
+
+void d3d_shutdown(D3d *d3d)
+{
+ if (!d3d)
+ return;
+
+ free(d3d);
+}
+
+void d3d_scene_begin(D3d *d3d)
+{
+ (void)d3d;
+}
+
+void d3d_scene_end(void)
+{
+}
+
+void d3d_resize(D3d *d3d, int rot, UINT width, UINT height)
+{
+ (void)d3d;
+ (void)rot;
+ (void)width;
+ (void) height;
+}
+
+void d3d_render(D3d *d3d)
+{
+ (void)d3d;
}
```
When the program is launched, you should obtain a window with a white background, like this:
+<img alt="tutorial 0" src="" width="30%" height="30%">
+
Some actions are implemented:
* Press the key 'q' to quit the program.
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.