annotate EXCLUDE/GLTSF/src/Window.cpp @ 4737:0e18c79c4111

Renamed Window::Update to Window::Handle_Events. Removed Window::Clear. Added Window::Show_Cursor/Hide_Cursor. Added On_Resized event.
author dewyatt
date Sat, 12 Jun 2010 03:19:43 -0400
parents 0c7c67d4e6ee
children
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();
4737
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
20 Show_Cursor();
4730
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
23 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
24 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
25 Finalize();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
26
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
27 my_Video_Mode = Mode;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
28 if (!my_Video_Mode.Is_Valid())
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
29 throw std::runtime_error("Invalid video mode");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
30
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
31 my_Fullscreen = Fullscreen;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
32 Register_Class();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
33 Create_Window(Title, Mode, Fullscreen);
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
36 void Window::Finalize()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
37 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
38 Destroy_Window();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
39 Unregister_Class();
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
42 void Window::Set_Listener(Window_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 my_Listener = Listener;
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
47 void Window::Register_Class()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
48 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
49 WNDCLASSEXW Window_Class = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
50 Window_Class.cbSize = sizeof(Window_Class);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
51 Window_Class.style = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
52 Window_Class.lpfnWndProc = &Window::Window_Procedure;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
53 Window_Class.cbClsExtra = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
54 Window_Class.cbWndExtra = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
55 Window_Class.hInstance = GetModuleHandle(NULL);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
56 Window_Class.hIcon = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
57 Window_Class.hCursor = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
58 Window_Class.hbrBackground = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
59 Window_Class.lpszMenuName = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
60 Window_Class.lpszClassName = Window_Class_Name;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
61 Window_Class.hIconSm = NULL;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
62 if (0 == RegisterClassExW(&Window_Class))
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
63 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
64
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
65 my_Class_Registered = true;
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
68 void Window::Unregister_Class()
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 (my_Class_Registered)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
71 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
72 if (0 == UnregisterClassW(Window_Class_Name, GetModuleHandle(NULL)))
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
73 printf("Warning: Failed to unregister window class\n");
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 my_Class_Registered = false;
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
79 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
80 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
81 HDC Screen_DC = GetDC(NULL);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
82 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
83 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
84 int Width = my_Video_Mode.Width;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
85 int Height = my_Video_Mode.Height;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
86 ReleaseDC(NULL, Screen_DC);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
87
4734
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
88 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
89 if (!my_Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
90 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
91 RECT Rect = {0, 0, Width, Height};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
92 AdjustWindowRect(&Rect, Style, false);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
93 Width = Rect.right - Rect.left;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
94 Height = Rect.bottom - Rect.top;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
95 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
96 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
97 if (!my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
98 throw std::runtime_error("Failed to create window");
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
99
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
100 if (Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
101 Switch_To_Fullscreen(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 Create_Context(Mode);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
104
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
105 RECT Rect = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
106 GetClientRect(my_Handle, &Rect);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
107 //TODO: ...
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
110 void Window::Destroy_Window()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
111 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
112 Destroy_Context();
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
113 if (my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
114 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
115 DestroyWindow(my_Handle);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
116 my_Handle = 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
117
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
118 if (my_Fullscreen)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
119 ChangeDisplaySettings(NULL, 0);
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
123 void Window::Create_Context(const Video_Mode &Mode)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
124 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
125 my_Device_Context = GetDC(my_Handle);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
126 if (!my_Device_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
127 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
128
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
129 PIXELFORMATDESCRIPTOR Pixel_Descriptor = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
130 Pixel_Descriptor.nSize = sizeof(Pixel_Descriptor);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
131 Pixel_Descriptor.nVersion = 1;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
132 Pixel_Descriptor.iLayerType = PFD_MAIN_PLANE;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
133 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
134 Pixel_Descriptor.iPixelType = PFD_TYPE_RGBA;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
135 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
136 Pixel_Descriptor.cDepthBits = 24;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
137 Pixel_Descriptor.cStencilBits = 8;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
138 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
139
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
140 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
141 if (0 == Best_Format)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
142 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
143
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
144 PIXELFORMATDESCRIPTOR Actual_Format = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
145 Actual_Format.nSize = sizeof(Actual_Format);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
146 Actual_Format.nVersion = 1;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
147 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
148 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
149 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
150
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
151 my_GL_Context = wglCreateContext(my_Device_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
152 if (!my_GL_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
153 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
154
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
155 wglMakeCurrent(my_Device_Context, my_GL_Context);
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
158 void Window::Destroy_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 if (my_GL_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
161 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
162 wglDeleteContext(my_GL_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
163 my_GL_Context = 0;
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 if (my_Device_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
166 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
167 ReleaseDC(my_Handle, my_Device_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
168 my_Device_Context = 0;
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
172 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
173 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
174 DEVMODE Device_Mode = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
175 Device_Mode.dmSize = sizeof(Device_Mode);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
176 Device_Mode.dmPelsWidth = Mode.Width;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
177 Device_Mode.dmPelsHeight = Mode.Height;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
178 Device_Mode.dmBitsPerPel = Mode.Bits_Per_Pixel;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
179 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
180
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
181 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
182 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
183
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
184 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
185 SetWindowLong(my_Handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
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 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
188 }
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 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
191 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
192 switch (Message)
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 case WM_CREATE:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
195 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
196 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
197 SetWindowLongPtr(Handle, GWLP_USERDATA, This);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
198 return 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
199 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
200 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
201 case WM_DESTROY:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
202 PostQuitMessage(0);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
203 return 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
204 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
205 default:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
206 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
207 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
208 if (Win)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
209 return Win->Handle_Message(Handle, Message, wParam, lParam);
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 break;
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 return DefWindowProcW(Handle, Message, wParam, lParam);
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
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
216 #define Call_Listener(x)\
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
217 if (my_Listener) my_Listener->x
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 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
220 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
221 switch (Message)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
222 {
4737
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
223 case WM_SIZE:
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
224 Call_Listener(On_Resized(LOWORD(lParam), HIWORD(lParam)));
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
225 break;
4730
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
226 case WM_CLOSE:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
227 Call_Listener(On_Close());
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
228 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
229 case WM_KEYDOWN:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
230 Call_Listener(On_Key_Down(wParam));
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
231 break;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
232 case WM_KEYUP:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
233 Call_Listener(On_Key_Up(wParam));
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
234 break;
4734
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
235 case WM_CHAR:
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
236 Call_Listener(On_Char(wParam));
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
237 break;
4730
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
238 default:
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
239 return DefWindowProcW(Handle, Message, wParam, lParam);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
240 break;
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 return 0;
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
243 }
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 void Window::Show()
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 if (my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
248 ShowWindow(my_Handle, SW_SHOW);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
249 }
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 void Window::Hide()
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 if (my_Handle)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
254 ShowWindow(my_Handle, SW_HIDE);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
255 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
256
4737
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
257 void Window::Handle_Events()
4730
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 MSG Message = {0};
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
260 while (PeekMessageW(&Message, NULL, 0, 0, PM_REMOVE))
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 TranslateMessage(&Message);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
263 DispatchMessageW(&Message);
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 }
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
266
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
267 void Window::Display()
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
268 {
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
269 if (my_Device_Context && my_GL_Context)
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
270 SwapBuffers(my_Device_Context);
6032ada8b9e5 Adding GLTSF (somewhat based on SFML, no actual TSF code yet)
dewyatt
parents:
diff changeset
271 }
4734
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
272
4737
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
273 void Window::Show_Cursor()
4734
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
274 {
4737
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
275 ShowCursor(TRUE);
4734
0c7c67d4e6ee Added On_Char method to Window_Listener for WM_CHAR messages.
dewyatt
parents: 4730
diff changeset
276 }
4737
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
277
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
278 void Window::Hide_Cursor()
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
279 {
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
280 ShowCursor(FALSE);
0e18c79c4111 Renamed Window::Update to Window::Handle_Events.
dewyatt
parents: 4734
diff changeset
281 }