aboutsummaryrefslogtreecommitdiff
path: root/subprojects/d2tk/pugl/pugl/detail/win.h
blob: 949fa9010af8c708b18e9be044428b5748a68e13 (plain)
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
/*
  Copyright 2012-2019 David Robillard <http://drobilla.net>

  Permission to use, copy, modify, and/or distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/**
   @file win.h Shared definitions for Windows implementation.
*/

#include "pugl/detail/implementation.h"

#include <windows.h>

#include <stdbool.h>

typedef PIXELFORMATDESCRIPTOR PuglWinPFD;

struct PuglWorldInternalsImpl {
	double timerFrequency;
};

struct PuglInternalsImpl {
	PuglWinPFD   pfd;
	int          pfId;
	HWND         hwnd;
	HDC          hdc;
	PuglSurface* surface;
	DWORD        refreshRate;
	bool         flashing;
	bool         resizing;
	bool         mouseTracked;
};

static inline PuglWinPFD
puglWinGetPixelFormatDescriptor(const PuglHints hints)
{
	const int rgbBits = (hints[PUGL_RED_BITS] +   //
	                     hints[PUGL_GREEN_BITS] + //
	                     hints[PUGL_BLUE_BITS]);

	const DWORD dwFlags = hints[PUGL_DOUBLE_BUFFER] ? PFD_DOUBLEBUFFER : 0u;

	PuglWinPFD pfd;
	ZeroMemory(&pfd, sizeof(pfd));
	pfd.nSize        = sizeof(pfd);
	pfd.nVersion     = 1;
	pfd.dwFlags      = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | dwFlags;
	pfd.iPixelType   = PFD_TYPE_RGBA;
	pfd.cColorBits   = (BYTE)rgbBits;
	pfd.cRedBits     = (BYTE)hints[PUGL_RED_BITS];
	pfd.cGreenBits   = (BYTE)hints[PUGL_GREEN_BITS];
	pfd.cBlueBits    = (BYTE)hints[PUGL_BLUE_BITS];
	pfd.cAlphaBits   = (BYTE)hints[PUGL_ALPHA_BITS];
	pfd.cDepthBits   = (BYTE)hints[PUGL_DEPTH_BITS];
	pfd.cStencilBits = (BYTE)hints[PUGL_STENCIL_BITS];
	pfd.iLayerType   = PFD_MAIN_PLANE;
	return pfd;
}

static inline unsigned
puglWinGetWindowFlags(const PuglView* const view)
{
	const bool     resizable = view->hints[PUGL_RESIZABLE];
	const unsigned sizeFlags = resizable ? (WS_SIZEBOX | WS_MAXIMIZEBOX) : 0u;

	return (WS_CLIPCHILDREN | WS_CLIPSIBLINGS |
	        (view->parent
	             ? WS_CHILD
	             : (WS_POPUPWINDOW | WS_CAPTION | WS_MINIMIZEBOX | sizeFlags)));
}

static inline unsigned
puglWinGetWindowExFlags(const PuglView* const view)
{
	return WS_EX_NOINHERITLAYOUT | (view->parent ? 0u : WS_EX_APPWINDOW);
}

static inline PuglStatus
puglWinCreateWindow(const PuglView* const view,
                    const char* const     title,
                    HWND* const           hwnd,
                    HDC* const            hdc)
{
	const char*    className  = (const char*)view->world->className;
	const unsigned winFlags   = puglWinGetWindowFlags(view);
	const unsigned winExFlags = puglWinGetWindowExFlags(view);

	// Calculate total window size to accommodate requested view size
	RECT wr = { (long)view->frame.x, (long)view->frame.y,
	            (long)view->frame.width, (long)view->frame.height };
	AdjustWindowRectEx(&wr, winFlags, FALSE, winExFlags);

	// Create window and get drawing context
	if (!(*hwnd = CreateWindowEx(winExFlags, className, title, winFlags,
	                             CW_USEDEFAULT, CW_USEDEFAULT,
	                             wr.right-wr.left, wr.bottom-wr.top,
	                             (HWND)view->parent, NULL, NULL, NULL))) {
		return PUGL_REALIZE_FAILED;
	} else if (!(*hdc = GetDC(*hwnd))) {
		DestroyWindow(*hwnd);
		*hwnd = NULL;
		return PUGL_REALIZE_FAILED;
	}

	return PUGL_SUCCESS;
}

static inline PuglStatus
puglWinStubConfigure(PuglView* view)
{
	PuglInternals* const impl = view->impl;
	PuglStatus           st   = PUGL_SUCCESS;

	if ((st = puglWinCreateWindow(view, "Pugl", &impl->hwnd, &impl->hdc))) {
		return st;
	}

	impl->pfd  = puglWinGetPixelFormatDescriptor(view->hints);
	impl->pfId = ChoosePixelFormat(impl->hdc, &impl->pfd);

	if (!SetPixelFormat(impl->hdc, impl->pfId, &impl->pfd)) {
		ReleaseDC(impl->hwnd, impl->hdc);
		DestroyWindow(impl->hwnd);
		impl->hwnd = NULL;
		impl->hdc  = NULL;
		return PUGL_SET_FORMAT_FAILED;
	}

	return PUGL_SUCCESS;
}