1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
bool CreateGLWindow(LPCWSTR title, int width, int height, int bitsperpel, bool fullscreen)
{
GLuint PixelFormat; //Pixel Format
WNDCLASS GLWindowClass; //Window Extended Class
DWORD dwExStyle; //Extended Style
DWORD dwStyle; //Window Style
RECT GLWindowRect;
GLWindowRect.left = (long)0; //Left boudary is set to 0
GLWindowRect.right = (long)width; //Right Boundary is set to requested width
GLWindowRect.top = (long)0; //Top boundary is set to 0 - this way the top left is coordinate 0
GLWindowRect.bottom = (long)height; //bottom boundary is set to requested height
GLWINDOW.Fullscreen = fullscreen; //sets teh requested fullscreen mode
GLWINDOW.hInstance = GetModuleHandle(NULL); //set the window hInstance to the Module Handle
GLWindowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; //redraw horizontally vertically and has own DX
GLWindowClass.lpfnWndProc = (WNDPROC)WndProc; //This is the LRESULT CALLBACK WndProc function
GLWindowClass.cbClsExtra = 0; //no extra (console)?
GLWindowClass.cbWndExtra = 0; //no extra windot data..?
GLWindowClass.hInstance = GLWINDOW.hInstance; //Sets the Window's hInstance to the one in the GLWindow object
GLWindowClass.hIcon = LoadIcon(NULL, IDI_WINLOGO); //CHANGE LATER
GLWindowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
GLWindowClass.hbrBackground = NULL; //No Background
GLWindowClass.lpszMenuName = NULL; //No Menu;
GLWindowClass.lpszClassName = L"SEWnd";
if(!RegisterClass(&GLWindowClass)) //no longer Extended
{
MessageBox(NULL,ERROR_REGISTER_WINDOW_CLASS,ERROR_HEADER,MB_INFORM);
return false;
}
if(GLWINDOW.Fullscreen)
{
DEVMODE dmScreenSettings; //Device Mode
memset(&dmScreenSettings, NULL, sizeof(dmScreenSettings)); //Clear Memory for Variable
dmScreenSettings.dmSize = sizeof(dmScreenSettings); //sets the size to that of the devmode structure
dmScreenSettings.dmPelsWidth = width; //set fullscreen width
dmScreenSettings.dmPelsHeight = height; //sets fullscreen height
dmScreenSettings.dmBitsPerPel = bitsperpel; //sets bits per pixel
dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT; //sets the fields
if(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL) //attemtp to change display settings
{
if(MessageBox(NULL,ERROR_FULLSCREEN_MODE_GPU,NULL, MB_YESNO | MB_ICONEXCLAMATION) == IDYES) //add a definition for this
{
GLWINDOW.Fullscreen = false;
}
else
{
return false; //quit
}
}
}
if(GLWINDOW.Fullscreen) //If Fullscreen is supported according to previous if statement
{
dwExStyle = WS_EX_APPWINDOW; //modify the extended style for fullscreen
dwStyle = WS_POPUP; //modify window style for fullscreen;
ShowCursor(false); //optional - but recomended for fullscreen
}
else
{
dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // defines the window Extended style //AD WS_DLGFRAME HERE TO FLIP LEFT AND RIGHT
dwStyle = WS_OVERLAPPEDWINDOW; //defines the window style //CHANGE TO WS_BORDER TO REMOVE "_ [] X" buttons
}
AdjustWindowRectEx(&GLWindowRect, dwStyle, false, dwExStyle); //adjust the window to desired size
if(!( GLWINDOW.hWnd = CreateWindowEx(dwExStyle, L"SEWnd", title, WS_CLIPSIBLINGS | WS_CLIPCHILDREN | dwStyle, 0/*loc x*/, 0/*loc y*/, GLWindowRect.right - GLWindowRect.left, GLWindowRect.bottom - GLWindowRect.top, NULL, NULL, GLWINDOW.hInstance, NULL)))
{
CloseGLWindow();
MessageBox(NULL,ERROR_CREATE_WINDOW,ERROR_HEADER,MB_INFORM);
return false;
}
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, NULL, sizeof(PIXELFORMATDESCRIPTOR)); //clear the pfd
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); //adjust pfd size
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.iLayerType = PFD_MAIN_PLANE;
if(!GL_Init()) //initialize opengl or opengl scene
{
CloseGLWindow();
MessageBox(NULL,ERROR_INIT_OPENGL,ERROR_HEADER,MB_INFORM);
return false;
}
Shader hiya("h.vert","h.frag"); //FIX LATER
if(!(GLWINDOW.hDC = GetDC(GLWINDOW.hWnd)))
{
CloseGLWindow();
MessageBox(NULL,ERROR_CREATE_DEVICE_CONTEXT,ERROR_HEADER,MB_INFORM);
return false;
}
if(!(PixelFormat = ChoosePixelFormat(GLWINDOW.hDC, &pfd))) //attempt to choose the matching pixel format
{
CloseGLWindow();
MessageBox(NULL,ERROR_MATCHING_PIXEL_FORMAT,ERROR_HEADER,MB_INFORM);
return false;
}
if(!SetPixelFormat(GLWINDOW.hDC, PixelFormat, &pfd)) //attempt to set pixel format
{
CloseGLWindow();
MessageBox(NULL,ERROR_SET_PIXEL_FORMAT,ERROR_HEADER,MB_INFORM);
return false;
}
if(!(GLWINDOW.hRC = wgl_CreateContext(GLWINDOW.hDC))) //attempt to create the context
{
CloseGLWindow();
MessageBox(NULL,ERROR_CREATE_RENDER_CONTEXT,ERROR_HEADER,MB_INFORM);
return false;
}
if(!wgl_MakeCurrent(GLWINDOW.hDC, GLWINDOW.hRC)) //attempt to activate rendering context
{
CloseGLWindow();
MessageBox(NULL,ERROR_ACTIVATE_OPENGL_CONTEXT,ERROR_HEADER,MB_INFORM);
return false;
}
ShowWindow( GLWINDOW.hWnd, SW_SHOW); //show the window
SetForegroundWindow(GLWINDOW.hWnd); //set the window to forground which inclreasses priority slightly
SetFocus(GLWINDOW.hWnd); //select the window for keyboard focus
//resize the gl scene - glResize(width, height);
//initglscene() or something like that
return true;
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_ACTIVATE:
{
if(!HIWORD(wParam)) //if not minimized
{
GLWINDOW.ActiveWindow = true;//FIX WHEN CLASS IS ADDED
}
else
{
GLWINDOW.ActiveWindow = false;
}
}
case WM_SYSCOMMAND:
{
switch(wParam)
{
case SC_SCREENSAVE: //Screen Saver
case SC_MONITORPOWER: //Monitor Power Save Mode
return false; //prevent both
}
break;
}
case WM_CLOSE:
{
PostQuitMessage(NULL);
return false;
}
case WM_KEYDOWN: //Is a Key Pressed
{
GLWINDOW.KeyStates[wParam] = true; //change that keystate to pressed
return false;
}
case WM_KEYUP: //Is a Key Released
{
GLWINDOW.KeyStates[wParam] = false; //change the keystate to unpressed
return false;
}
case WM_SIZE:
{
//resize(LOWORD(lParam),HIWORD(lParam));
return false;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) //Entry Point for Thread 1
{
GLWINDOW.Alive = true; //Possibly declare class here
MSG Message;
//POSSIBLE ASK IF FULLSCREEN IF FIRST RUN
if(!CreateGLWindow(L"SETV", 640, 480, 32, false)) //should be 16bits per pel?
{
return false;
}
while(GLWINDOW.Alive)
{
if(PeekMessage(&Message, NULL, 0, 0, PM_REMOVE)) //If there is a message
{
if(Message.message == WM_QUIT)
{
GLWINDOW.Alive = false;
}
else
{
TranslateMessage(&Message);
DispatchMessage(&Message);
}
}
else //No Messages
{
if(GLWINDOW.ActiveWindow)
{
USE_ESCAPE_TO_QUIT
else //RENDER
{
//Physics
//Sound?
RenderGLWindow();
//Sound
}
}
//F1 RESIZE THING MAYBE
}
}
CloseGLWindow();
return(Message.wParam); //Exit
}
|