summaryrefslogtreecommitdiff
path: root/display/display.c
blob: 56b9d80502245ff3c505de4b575b5026ddf5c3af (plain) (blame)
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
#include "display.h"


int InitializeWindowContext(char className[], char windowName[], WNDPROC WindowProc, WindowContext *WContext){

	WContext->windowClass = (WNDCLASSEX){
		.cbSize = sizeof(WNDCLASSEX),
		.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW,
		.lpfnWndProc = WindowProc,
		.hInstance = GetModuleHandle(NULL),
		.hCursor = LoadCursor(NULL, IDC_CROSS),
		.lpszClassName = className
	};
	if(RegisterClassEx(&WContext->windowClass) == 0){
		return 1;
	}

	WContext->window = CreateWindowEx(
		0,
		WContext->windowClass.lpszClassName,
		windowName,
		WS_OVERLAPPEDWINDOW | WS_VISIBLE | WS_MAXIMIZE,
		CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
		NULL,
		NULL,
		GetModuleHandle(NULL),
		NULL
	);
	if(WContext->window == NULL){
		return 1;
	}

	WContext->windowDC = GetDC(WContext->window);
	if(WContext->windowDC == NULL){
		return 1;
	}

	return 0;
}

int InitializeOpenGL(HWND window, HGLRC *renderingContext){

	int error = 0;

	int pfdAttribs[] = {
		WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
		WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
		WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
		WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
		WGL_COLOR_BITS_ARB, 32,
		WGL_DEPTH_BITS_ARB, 24,
		WGL_STENCIL_BITS_ARB, 8,
		WGL_FRAMEBUFFER_SRGB_CAPABLE_ARB, GL_TRUE,
		0
	};

	HDC windowDC = GetDC(window);
	if(windowDC == NULL){
		error = 1;
		goto end;
	}
	UINT pixelFormat, numFormats;
	if(wglChoosePixelFormatARB(
		windowDC,
		pfdAttribs,
		NULL, 1,
		&pixelFormat,
		&numFormats
	) == FALSE){
		error = 1;
		goto end;
	}

	PIXELFORMATDESCRIPTOR pixelFormatDescriptor;
	if(SetPixelFormat(
		windowDC,
		pixelFormat,
		&pixelFormatDescriptor
	) == FALSE){
		error = 1;
		goto end;
	}

	int contextAttribs[] = {
		WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
		WGL_CONTEXT_MINOR_VERSION_ARB, 3,
		0
	};
	*renderingContext = wglCreateContextAttribsARB(windowDC, NULL, contextAttribs);
	if(renderingContext == NULL){
		error = 1;
		goto end;
	}

	if(wglMakeCurrent(windowDC, *renderingContext) == FALSE){
		error = 1;
		goto end;
	}

end:
	ReleaseDC(window, windowDC);

	return error;
}