annotate EXCLUDE/GLTSF/src/Window.cpp @ 4730:6032ada8b9e5

Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
author dewyatt
date Tue, 25 May 2010 18:53:09 -0400
parents
children 0c7c67d4e6ee
rev   line source
4730
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
1 #include "Window.hpp"
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
2
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
3 #pragma comment(lib, "opengl32.lib")
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
4
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
5 const wchar_t *Window::Window_Class_Name = L"GLTSF";
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
6
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
7 Window::Window() : my_Handle(0),
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
8 my_Device_Context(0),
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
9 my_GL_Context(0),
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
10 my_Class_Registered(false),
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
11 my_Listener(0)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
12 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
13
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
14 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
15
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
16 Window::~Window()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
17 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
18 Finalize();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
19 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
20
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
21 void Window::Initialize(const std::wstring &Title, const Video_Mode &Mode, bool Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
22 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
23 Finalize();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
24
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
25 my_Video_Mode = Mode;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
26 if (!my_Video_Mode.Is_Valid())
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
27 throw std::runtime_error("Invalid video mode");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
28
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
29 my_Fullscreen = Fullscreen;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
30 Register_Class();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
31 Create_Window(Title, Mode, Fullscreen);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
32 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
33
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
34 void Window::Finalize()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
35 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
36 Destroy_Window();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
37 Unregister_Class();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
38 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
39
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
40 void Window::Set_Listener(Window_Listener *Listener)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
41 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
42 my_Listener = Listener;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
43 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
44
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
45 void Window::Register_Class()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
46 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
47 WNDCLASSEXW Window_Class = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
48 Window_Class.cbSize = sizeof(Window_Class);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
49 Window_Class.style = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
50 Window_Class.lpfnWndProc = &Window::Window_Procedure;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
51 Window_Class.cbClsExtra = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
52 Window_Class.cbWndExtra = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
53 Window_Class.hInstance = GetModuleHandle(NULL);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
54 Window_Class.hIcon = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
55 Window_Class.hCursor = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
56 Window_Class.hbrBackground = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
57 Window_Class.lpszMenuName = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
58 Window_Class.lpszClassName = Window_Class_Name;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
59 Window_Class.hIconSm = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
60 if (0 == RegisterClassExW(&Window_Class))
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
61 throw std::runtime_error("Failed to register window class");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
62
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
63 my_Class_Registered = true;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
64 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
65
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
66 void Window::Unregister_Class()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
67 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
68 if (my_Class_Registered)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
69 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
70 if (0 == UnregisterClassW(Window_Class_Name, GetModuleHandle(NULL)))
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
71 printf("Warning: Failed to unregister window class\n");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
72
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
73 my_Class_Registered = false;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
74 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
75 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
76
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
77 void Window::Create_Window(const std::wstring &Title, const Video_Mode &Mode, bool Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
78 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
79 HDC Screen_DC = GetDC(NULL);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
80 int Left = (GetDeviceCaps(Screen_DC, HORZRES) - my_Video_Mode.Width) / 2;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
81 int Top = (GetDeviceCaps(Screen_DC, VERTRES) - my_Video_Mode.Height) / 2;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
82 int Width = my_Video_Mode.Width;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
83 int Height = my_Video_Mode.Height;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
84 ReleaseDC(NULL, Screen_DC);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
85
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
86 DWORD Style = WS_OVERLAPPEDWINDOW;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
87 if (!my_Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
88 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
89 RECT Rect = {0, 0, Width, Height};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
90 AdjustWindowRect(&Rect, Style, false);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
91 Width = Rect.right - Rect.left;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
92 Height = Rect.bottom - Rect.top;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
93 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
94 my_Handle = CreateWindowW(Window_Class_Name, Title.c_str(), Style, Left, Top, Width, Height, NULL, NULL, GetModuleHandle(NULL), this);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
95 if (!my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
96 throw std::runtime_error("Failed to create window");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
97
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
98 if (Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
99 Switch_To_Fullscreen(Mode);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
100
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
101 Create_Context(Mode);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
102
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
103 RECT Rect = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
104 GetClientRect(my_Handle, &Rect);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
105 //TODO: ...
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
106 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
107
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
108 void Window::Destroy_Window()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
109 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
110 Destroy_Context();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
111 if (my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
112 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
113 DestroyWindow(my_Handle);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
114 my_Handle = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
115
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
116 if (my_Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
117 ChangeDisplaySettings(NULL, 0);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
118 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
119 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
120
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
121 void Window::Create_Context(const Video_Mode &Mode)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
122 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
123 my_Device_Context = GetDC(my_Handle);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
124 if (!my_Device_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
125 throw std::runtime_error("Failed to get device context");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
126
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
127 PIXELFORMATDESCRIPTOR Pixel_Descriptor = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
128 Pixel_Descriptor.nSize = sizeof(Pixel_Descriptor);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
129 Pixel_Descriptor.nVersion = 1;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
130 Pixel_Descriptor.iLayerType = PFD_MAIN_PLANE;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
131 Pixel_Descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
132 Pixel_Descriptor.iPixelType = PFD_TYPE_RGBA;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
133 Pixel_Descriptor.cColorBits = static_cast<BYTE>(Mode.Bits_Per_Pixel);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
134 Pixel_Descriptor.cDepthBits = 24;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
135 Pixel_Descriptor.cStencilBits = 8;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
136 Pixel_Descriptor.cAlphaBits = Mode.Bits_Per_Pixel == 32 ? 8 : 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
137
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
138 int Best_Format = ChoosePixelFormat(my_Device_Context, &Pixel_Descriptor);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
139 if (0 == Best_Format)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
140 throw std::runtime_error("Failed to find suitable pixel format");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
141
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
142 PIXELFORMATDESCRIPTOR Actual_Format = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
143 Actual_Format.nSize = sizeof(Actual_Format);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
144 Actual_Format.nVersion = 1;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
145 DescribePixelFormat(my_Device_Context, Best_Format, sizeof(Actual_Format), &Actual_Format);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
146 if (!SetPixelFormat(my_Device_Context, Best_Format, &Actual_Format))
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
147 throw std::runtime_error("Failed to set device pixel format");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
148
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
149 my_GL_Context = wglCreateContext(my_Device_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
150 if (!my_GL_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
151 throw std::runtime_error("Failed to create OpenGL context");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
152
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
153 wglMakeCurrent(my_Device_Context, my_GL_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
154 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
155
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
156 void Window::Destroy_Context()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
157 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
158 if (my_GL_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
159 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
160 wglDeleteContext(my_GL_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
161 my_GL_Context = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
162 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
163 if (my_Device_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
164 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
165 ReleaseDC(my_Handle, my_Device_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
166 my_Device_Context = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
167 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
168 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
169
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
170 void Window::Switch_To_Fullscreen(const Video_Mode &Mode)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
171 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
172 DEVMODE Device_Mode = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
173 Device_Mode.dmSize = sizeof(Device_Mode);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
174 Device_Mode.dmPelsWidth = Mode.Width;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
175 Device_Mode.dmPelsHeight = Mode.Height;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
176 Device_Mode.dmBitsPerPel = Mode.Bits_Per_Pixel;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
177 Device_Mode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
178
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
179 if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettings(&Device_Mode, CDS_FULLSCREEN))
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
180 throw std::runtime_error("Failed to change to fullscreen mode");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
181
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
182 SetWindowLong(my_Handle, GWL_STYLE, WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
183 SetWindowLong(my_Handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
184
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
185 SetWindowPos(my_Handle, HWND_TOP, 0, 0, Mode.Width, Mode.Height, SWP_FRAMECHANGED);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
186 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
187
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
188 LRESULT CALLBACK Window::Window_Procedure(HWND Handle, UINT Message, WPARAM wParam, LPARAM lParam)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
189 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
190 switch (Message)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
191 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
192 case WM_CREATE:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
193 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
194 LONG This = reinterpret_cast<LONG>(reinterpret_cast<CREATESTRUCT *>(lParam)->lpCreateParams);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
195 SetWindowLongPtr(Handle, GWLP_USERDATA, This);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
196 return 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
197 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
198 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
199 case WM_DESTROY:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
200 PostQuitMessage(0);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
201 return 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
202 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
203 default:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
204 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
205 Window* Win = reinterpret_cast<Window *>(GetWindowLongPtr(Handle, GWLP_USERDATA));
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
206 if (Win)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
207 return Win->Handle_Message(Handle, Message, wParam, lParam);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
208 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
209 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
210 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
211 return DefWindowProcW(Handle, Message, wParam, lParam);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
212 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
213
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
214 #define Call_Listener(x)\
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
215 if (my_Listener) my_Listener->x
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
216
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
217 LRESULT Window::Handle_Message(HWND Handle, UINT Message, WPARAM wParam, LPARAM lParam)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
218 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
219 switch (Message)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
220 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
221 case WM_CLOSE:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
222 Call_Listener(On_Close());
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
223 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
224 case WM_KEYDOWN:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
225 Call_Listener(On_Key_Down(wParam));
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
226 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
227 case WM_KEYUP:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
228 Call_Listener(On_Key_Up(wParam));
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
229 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
230 default:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
231 return DefWindowProcW(Handle, Message, wParam, lParam);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
232 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
233 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
234 return 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
235 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
236
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
237 void Window::Show()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
238 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
239 if (my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
240 ShowWindow(my_Handle, SW_SHOW);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
241 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
242
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
243 void Window::Hide()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
244 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
245 if (my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
246 ShowWindow(my_Handle, SW_HIDE);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
247 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
248
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
249 void Window::Update()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
250 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
251 MSG Message = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
252 while (PeekMessageW(&Message, NULL, 0, 0, PM_REMOVE))
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
253 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
254 TranslateMessage(&Message);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
255 DispatchMessageW(&Message);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
256 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
257 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
258
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
259 void Window::Display()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
260 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
261 if (my_Device_Context && my_GL_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
262 SwapBuffers(my_Device_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
263 }