annotate EXCLUDE/GLTSF/src/Window.cpp @ 4734:0c7c67d4e6ee

Added On_Char method to Window_Listener for WM_CHAR messages. Removed a lot of TSF code because part of it was wrong and part was too complicated. Added Clear method to clear the window. IME input should work in both windowed mode and fullscreen mode with these changes. I have tested on Windows XP SP3 and Windows 7 Ultimate in VirtualBox. When you type a character (with an IME or not), the console will show the code point as U+XXXX. You use Left Alt+Shift (or whatever you have it set to) to switch input languages as usual. Hit ESC to exit (or close the window in windowed mode). The program will pause before exiting so you can review the console output (press a key to exit).
author dewyatt
date Wed, 09 Jun 2010 00:03:54 -0400
parents 6032ada8b9e5
children 0e18c79c4111
rev   line source
4730
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
1 #include "Window.hpp"
4734
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
2 #include <gl/GL.h>
4730
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
3
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
4 #pragma comment(lib, "opengl32.lib")
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
5
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
6 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
7
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
8 Window::Window() : my_Handle(0),
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
9 my_Device_Context(0),
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
10 my_GL_Context(0),
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
11 my_Class_Registered(false),
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
12 my_Listener(0)
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
17 Window::~Window()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
18 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
19 Finalize();
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
22 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
23 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
24 Finalize();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
25
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
26 my_Video_Mode = Mode;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
27 if (!my_Video_Mode.Is_Valid())
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
28 throw std::runtime_error("Invalid video mode");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
29
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
30 my_Fullscreen = Fullscreen;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
31 Register_Class();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
32 Create_Window(Title, Mode, Fullscreen);
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
35 void Window::Finalize()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
36 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
37 Destroy_Window();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
38 Unregister_Class();
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
41 void Window::Set_Listener(Window_Listener *Listener)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
42 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
43 my_Listener = Listener;
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
46 void Window::Register_Class()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
47 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
48 WNDCLASSEXW Window_Class = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
49 Window_Class.cbSize = sizeof(Window_Class);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
50 Window_Class.style = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
51 Window_Class.lpfnWndProc = &Window::Window_Procedure;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
52 Window_Class.cbClsExtra = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
53 Window_Class.cbWndExtra = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
54 Window_Class.hInstance = GetModuleHandle(NULL);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
55 Window_Class.hIcon = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
56 Window_Class.hCursor = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
57 Window_Class.hbrBackground = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
58 Window_Class.lpszMenuName = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
59 Window_Class.lpszClassName = Window_Class_Name;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
60 Window_Class.hIconSm = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
61 if (0 == RegisterClassExW(&Window_Class))
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
62 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
63
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
64 my_Class_Registered = true;
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
67 void Window::Unregister_Class()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
68 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
69 if (my_Class_Registered)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
70 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
71 if (0 == UnregisterClassW(Window_Class_Name, GetModuleHandle(NULL)))
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
72 printf("Warning: Failed to unregister window class\n");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
73
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
74 my_Class_Registered = false;
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
78 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
79 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
80 HDC Screen_DC = GetDC(NULL);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
81 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
82 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
83 int Width = my_Video_Mode.Width;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
84 int Height = my_Video_Mode.Height;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
85 ReleaseDC(NULL, Screen_DC);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
86
4734
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
87 DWORD Style = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
4730
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
88 if (!my_Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
89 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
90 RECT Rect = {0, 0, Width, Height};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
91 AdjustWindowRect(&Rect, Style, false);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
92 Width = Rect.right - Rect.left;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
93 Height = Rect.bottom - Rect.top;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
94 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
95 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
96 if (!my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
97 throw std::runtime_error("Failed to create window");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
98
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
99 if (Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
100 Switch_To_Fullscreen(Mode);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
101
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
102 Create_Context(Mode);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
103
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
104 RECT Rect = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
105 GetClientRect(my_Handle, &Rect);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
106 //TODO: ...
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
109 void Window::Destroy_Window()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
110 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
111 Destroy_Context();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
112 if (my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
113 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
114 DestroyWindow(my_Handle);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
115 my_Handle = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
116
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
117 if (my_Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
118 ChangeDisplaySettings(NULL, 0);
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
122 void Window::Create_Context(const Video_Mode &Mode)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
123 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
124 my_Device_Context = GetDC(my_Handle);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
125 if (!my_Device_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
126 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
127
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
128 PIXELFORMATDESCRIPTOR Pixel_Descriptor = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
129 Pixel_Descriptor.nSize = sizeof(Pixel_Descriptor);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
130 Pixel_Descriptor.nVersion = 1;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
131 Pixel_Descriptor.iLayerType = PFD_MAIN_PLANE;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
132 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
133 Pixel_Descriptor.iPixelType = PFD_TYPE_RGBA;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
134 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
135 Pixel_Descriptor.cDepthBits = 24;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
136 Pixel_Descriptor.cStencilBits = 8;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
137 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
138
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
139 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
140 if (0 == Best_Format)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
141 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
142
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
143 PIXELFORMATDESCRIPTOR Actual_Format = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
144 Actual_Format.nSize = sizeof(Actual_Format);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
145 Actual_Format.nVersion = 1;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
146 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
147 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
148 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
149
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
150 my_GL_Context = wglCreateContext(my_Device_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
151 if (!my_GL_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
152 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
153
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
154 wglMakeCurrent(my_Device_Context, my_GL_Context);
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
157 void Window::Destroy_Context()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
158 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
159 if (my_GL_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
160 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
161 wglDeleteContext(my_GL_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
162 my_GL_Context = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
163 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
164 if (my_Device_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
165 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
166 ReleaseDC(my_Handle, my_Device_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
167 my_Device_Context = 0;
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
171 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
172 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
173 DEVMODE Device_Mode = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
174 Device_Mode.dmSize = sizeof(Device_Mode);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
175 Device_Mode.dmPelsWidth = Mode.Width;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
176 Device_Mode.dmPelsHeight = Mode.Height;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
177 Device_Mode.dmBitsPerPel = Mode.Bits_Per_Pixel;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
178 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
179
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
180 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
181 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
182
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
183 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
184 SetWindowLong(my_Handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
185
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
186 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
187 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
188
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
189 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
190 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
191 switch (Message)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
192 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
193 case WM_CREATE:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
194 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
195 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
196 SetWindowLongPtr(Handle, GWLP_USERDATA, This);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
197 return 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
198 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
199 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
200 case WM_DESTROY:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
201 PostQuitMessage(0);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
202 return 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
203 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
204 default:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
205 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
206 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
207 if (Win)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
208 return Win->Handle_Message(Handle, Message, wParam, lParam);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
209 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
210 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
211 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
212 return DefWindowProcW(Handle, Message, wParam, lParam);
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
215 #define Call_Listener(x)\
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
216 if (my_Listener) my_Listener->x
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
217
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
218 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
219 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
220 switch (Message)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
221 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
222 case WM_CLOSE:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
223 Call_Listener(On_Close());
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
224 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
225 case WM_KEYDOWN:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
226 Call_Listener(On_Key_Down(wParam));
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
227 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
228 case WM_KEYUP:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
229 Call_Listener(On_Key_Up(wParam));
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
230 break;
4734
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
231 case WM_CHAR:
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
232 Call_Listener(On_Char(wParam));
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
233 break;
4730
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
234 default:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
235 return DefWindowProcW(Handle, Message, wParam, lParam);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
236 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
237 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
238 return 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
239 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
240
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
241 void Window::Show()
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 if (my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
244 ShowWindow(my_Handle, SW_SHOW);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
245 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
246
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
247 void Window::Hide()
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 if (my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
250 ShowWindow(my_Handle, SW_HIDE);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
251 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
252
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
253 void Window::Update()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
254 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
255 MSG Message = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
256 while (PeekMessageW(&Message, NULL, 0, 0, PM_REMOVE))
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 TranslateMessage(&Message);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
259 DispatchMessageW(&Message);
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 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
262
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
263 void Window::Display()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
264 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
265 if (my_Device_Context && my_GL_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
266 SwapBuffers(my_Device_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
267 }
4734
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
268
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
269 void Window::Clear()
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
270 {
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
271 glClear(GL_COLOR_BUFFER_BIT);
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
272 }