//create a window for the async socket, this window //receives the WM_xxx messages associated with the socket bool createSocketWindow(_SOCKET s, Dispatcher d) { //create a message handling window for the async socket. msg_hwnd = CreateWindowEx(0, "STATIC", PLUGIN_NAME, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, 0, 0); if (msg_hwnd == NULL) { msg(PLUGIN_NAME": CreateWindowEx() failed.\n"); return false; } //register the callback function for our invisible window. old_proc = (WNDPROC)SetWindowLong(msg_hwnd, GWL_WNDPROC, (long) socket_callback); if (old_proc == 0) { killWindow(); msg(PLUGIN_NAME": SetWindowLong() failed.\n"); return false; } conn = s; dispatch = d; //make the socket a non-blocking asynchronous socket hooked with our socket_callback handler. if (WSAAsyncSelect(conn, msg_hwnd, SOCKET_MSG, FD_READ | FD_WRITE | FD_CLOSE) == SOCKET_ERROR) { killWindow(); dispatch = NULL; conn = INVALID_SOCKET; msg(PLUGIN_NAME": Failed to create asynchronous connection to server.\n"); return false; } //asynchronous socket properly configured #ifdef DEBUG msg(PLUGIN_NAME": Successfully configured async socket\n"); #endif return true; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdSHow) { MSG msg; int done = 0; if (!createGLWindow("OpenGLDemo", windowWidth, windowHeight, 16)) { //some error code killWindow(); return 0; } while (!done) { if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) done = TRUE; else { TranslateMessage(&msg); DispatchMessage(&msg); } } } killWindow(); return (int)(msg.wParam); }
bool term_network() { #ifdef _WIN32 killWindow(); // return WSACleanup() == 0; #endif return true; }
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { MSG msg; createWindow(); for(ever) { if(FMUSIC_IsFinished(song)) break; if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if(msg.message == WM_QUIT) break; else { TranslateMessage(&msg); DispatchMessage(&msg); } } else if(active) { if(keys[VK_ESCAPE]) break; render(); SwapBuffers(hDC); } } killWindow(); return msg.wParam; }
void Game::onExit() { for (auto tmp : _gameComponents) { tmp->onExit(); delete tmp; } delete _game; killWindow(); }
int send_all(Buffer &b) { int len = send(conn, (const char*)b.get_buf(), b.size(), 0); if (len == SOCKET_ERROR) { int error = WSAGetLastError(); if (error == WSAEWOULDBLOCK) { sendBuf << b; return 0; } else { cleanup(); killWindow(); msg(PLUGIN_NAME": Failed to send requested data. %d != %d. Error: %x, %d\n", len, b.size(), error, error); return -1; } } else if (len != b.size()) { //move the remainder into sendBuf shift(b, len); sendBuf << b; //msg(PLUGIN_NAME": Short send. %d != %d.", len, out.size()); } return len; }
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow) { HWND hWnd; MSG msg; WNDCLASS wndClass; wndClass.style=CS_HREDRAW|CS_VREDRAW; wndClass.lpfnWndProc=WndProc; wndClass.cbClsExtra=0; wndClass.cbWndExtra=0; wndClass.hInstance=hInstance; wndClass.hIcon=LoadIcon(NULL,IDI_APPLICATION); wndClass.hCursor=LoadCursor(NULL,IDC_ARROW); wndClass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH); wndClass.lpszMenuName=NULL; wndClass.lpszClassName=szName; if(!RegisterClass(&wndClass)) { MessageBox(NULL,TEXT("Can not create!"),szName,MB_ICONERROR); return 0; } hWnd=CreateWindow(szName,TEXT("Soft Render"),WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,SCREEN_WIDTH,SCREEN_HEIGHT, NULL,NULL,hInstance,NULL); hdc=GetDC(hWnd); checkDisplayMode(hdc); createPalette(hdc); dibDC=CreateCompatibleDC(hdc); swidth=SCREEN_WIDTH; sheight=SCREEN_HEIGHT; init(); ShowWindow(hWnd,iCmdShow); UpdateWindow(hWnd); willExit=false; memset(&msg,0,sizeof(MSG)); if(hPalette) { SelectPalette(hdc,hPalette,FALSE); RealizePalette(hdc); } while(!willExit) { if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { if(msg.message==WM_QUIT) willExit=true; else { TranslateMessage(&msg); DispatchMessage(&msg); } } else { runWindow(); draw(); BitBlt(hdc,0,0,swidth,sheight,dibDC,0,0,SRCCOPY); } } releasePalette(); releaseDIB(dibDC); DeleteDC(dibDC); ReleaseDC(hWnd,hdc); killWindow(hWnd,hInstance,wndClass); return msg.wParam; }
bool OpenGLDevice::initWindow() { GLuint PixelFormat; // Holds The Results After Searching For A Match WNDCLASS wc; // Windows Class Structure DWORD dwExStyle; // Window Extended Style DWORD dwStyle; // Window Style RECT WindowRect; // Grabs Rectangle Upper Left / Lower Right Values WindowRect.left =(long)0; // Set Left Value To 0 WindowRect.right =(long)screenSize.width; // Set Right Value To Requested Width WindowRect.top =(long)0; // Set Top Value To 0 WindowRect.bottom =(long)screenSize.height; // Set Bottom Value To Requested Height hInstance = GetModuleHandle(NULL); // Grab An Instance For Our Window wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Move, And Own DC For Window wc.lpfnWndProc = (WNDPROC) WndProc; // WndProc Handles Messages wc.cbClsExtra = 0; // No Extra Window Data wc.cbWndExtra = 0; // No Extra Window Data wc.hInstance = hInstance; // Set The Instance wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); // Load The Default Icon wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Load The Arrow Pointer wc.hbrBackground = NULL; // No Background Required For GL wc.lpszMenuName = NULL; // We Don't Want A Menu wc.lpszClassName = "dog";//title.getCString(); // Set The Class Name if (!RegisterClass(&wc)) // Attempt To Register The Window Class { MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Exit And Return FALSE } if (fullscreen) // Attempt Fullscreen Mode? { DEVMODE dmScreenSettings; // Device Mode memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure dmScreenSettings.dmPelsWidth = screenSize.width; // Selected Screen Width dmScreenSettings.dmPelsHeight = screenSize.height; // Selected Screen Height dmScreenSettings.dmBitsPerPel = 16; // Selected Bits Per Pixel dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; // Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar. if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) { // If The Mode Fails, Offer Two Options. Quit Or Run In A Window. if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES) { fullscreen=FALSE; // Select Windowed Mode (Fullscreen=FALSE) } else { // Pop Up A Message Box Letting User Know The Program Is Closing. MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP); return FALSE; // Exit And Return FALSE } } } if (fullscreen) // Are We Still In Fullscreen Mode? { dwExStyle=WS_EX_APPWINDOW; // Window Extended Style dwStyle=WS_POPUP; // Windows Style ShowCursor(FALSE); // Hide Mouse Pointer } else { dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style } AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); // Adjust Window To True Requested Size hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window "dog",//title.getCString(), // Class Name "dog",//title.getCString(), // Window Title WS_CLIPSIBLINGS | // Required Window Style WS_CLIPCHILDREN | // Required Window Style dwStyle, // Selected Window Style 0, 0, // Window Position WindowRect.right-WindowRect.left, // Calculate Adjusted Window Width WindowRect.bottom-WindowRect.top, // Calculate Adjusted Window Height NULL, // No Parent Window NULL, // No Menu hInstance, // Instance NULL); // Don't Pass Anything To WM_CREATE if( !hWnd ) { killWindow(); // Reset The Display MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format 16, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 16, // 16Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; hDC=GetDC(hWnd); // Did We Get A Device Context? if( !hDC ) { killWindow(); // Reset The Display MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } PixelFormat=ChoosePixelFormat(hDC,&pfd); // Did Windows Find A Matching Pixel Format? if( !PixelFormat ) { killWindow(); // Reset The Display MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format? { killWindow(); // Reset The Display MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } hRC=wglCreateContext(hDC); // Are We Able To Get A Rendering Context? if( !hRC ) { killWindow(); // Reset The Display MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context { killWindow(); // Reset The Display MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; // Return FALSE } ShowWindow(hWnd,SW_SHOW); // Show The Window SetForegroundWindow(hWnd); // Slightly Higher Priority SetFocus(hWnd); // Sets Keyboard Focus To The Window //glShadeModel(GL_SMOOTH); // Enables Smooth Shading glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background glClearDepth(1.0f); // Depth Buffer Setup glEnable(GL_DEPTH_TEST); // Enables Depth Testing glDepthFunc(GL_LEQUAL); // The Type Of Depth Test To Do //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective return TRUE; // Initialization Went OK }
void OpenGLDevice::quit() { killWindow(); running = false; }
int CALLBACK WinMain( HINSTANCE hInstance1, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { windowWidth = 800; windowHeight = 600; camZoom = 2; hInstance = hInstance1; LPCTSTR className = "GraphicsGame"; WNDCLASS wc = {}; wc.style = CS_OWNDC|CS_VREDRAW|CS_HREDRAW; wc.lpfnWndProc = (WNDPROC)WindowProc; wc.hInstance = hInstance; wc.lpszClassName = className; wc.hCursor = NULL; if(!RegisterClass(&wc)) { killWindow(className); MessageBox(NULL, "Couldnt Register Class", "Error", MB_OK); return 1; } int windowStatus = createGLWindow(hInstance, className); if(!windowStatus) { return 1; } double dx = 0; double dy = 0; double maxSpeed = 5; double accel = 0.1f; MSG message; game = (Game*)calloc(1, sizeof(Game)); initGL(); initGame(game); int counter = 0; LARGE_INTEGER cps; LARGE_INTEGER curCount; LARGE_INTEGER prevCount; LONGLONG countDifference; QueryPerformanceFrequency(&cps); double secsPassed = 0; QueryPerformanceCounter(&curCount); float pan = 0.2; float topPan = 1; float lowPan = 0.2; while(!game->done) { prevCount = curCount; QueryPerformanceCounter(&curCount); countDifference = curCount.QuadPart - prevCount.QuadPart; secsPassed = (long double)countDifference / (long double)cps.QuadPart; while(PeekMessage(&message, hWindow, 0, 0, PM_REMOVE)) { if(message.message == WM_QUIT) { game->done = true; } TranslateMessage(&message); DispatchMessage(&message); } dx = 0; dy = 0; if(game->keys[VK_SHIFT]) { pan = topPan; } else { pan = lowPan; } if(game->keys['W']) { camY+=pan; } if(game->keys['A']) { camX-=pan; } if(game->keys['S']) { camY-=pan; } if(game->keys['D']) { camX+=pan; } if(game->keys['Q']) { camZoom += 0.01; if(camZoom > 6) camZoom = 6; resize(windowWidth, windowHeight); } if(game->keys['E']) { camZoom -= 0.01; if(camZoom < 1) camZoom = 1; resize(windowWidth, windowHeight); } if(game->keys[VK_SPACE]) { saveFile(game); } updateGame(game, dx, dy, secsPassed); drawScene(game); SwapBuffers(hDeviceContext); } free(game); killWindow(className); return 0; }
int createGLWindow(HINSTANCE hInstance, LPCTSTR className) { hWindow = CreateWindow( className, "GraphicsGame", WS_OVERLAPPEDWINDOW|WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, windowWidth, windowHeight, NULL, NULL, hInstance, NULL ); if(!hWindow) { killWindow(className); MessageBox(hWindow, "Couldnt Create Window", "Error", MB_OK); return false; } hDeviceContext = GetDC(hWindow); if(!hDeviceContext) { killWindow(className); MessageBox(hWindow, "Could Create Device Context", "Error", MB_OK); return false; } PIXELFORMATDESCRIPTOR pfd = {}; pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); pfd.nVersion = 1; pfd.dwFlags = PFD_SUPPORT_OPENGL|PFD_DRAW_TO_WINDOW|PFD_DOUBLEBUFFER; pfd.iPixelType = PFD_TYPE_RGBA; pfd.cColorBits = 24; pfd.cDepthBits = 24; pfd.iLayerType = PFD_MAIN_PLANE; int pixelFormat = ChoosePixelFormat(hDeviceContext, &pfd); if(!pixelFormat) { killWindow(className); MessageBox(hWindow, "Couldnt Select Good Pixel Format", "Error", MB_OK); return false; } if(!SetPixelFormat(hDeviceContext, pixelFormat, &pfd)) { killWindow(className); MessageBox(hWindow, "Couldnt Select Good Pixel Format", "Error", MB_OK); return false; } GLRenderContext = wglCreateContext(hDeviceContext); if(!GLRenderContext) { killWindow(className); MessageBox(hWindow, "Couldnt make opengl context", "Error", MB_OK); return false; } if(!wglMakeCurrent(hDeviceContext, GLRenderContext)) { killWindow(className); MessageBox(hWindow, "Couldnt make gl context current", "Error", MB_OK); return false; } return true; }
BOOL createWindow() { GLuint PixelFormat; WNDCLASS wc; DWORD dwExStyle; DWORD dwStyle; RECT WindowRect; int width = GetSystemMetrics(SM_CXSCREEN); int height = GetSystemMetrics(SM_CYSCREEN); WindowRect.left=(long)0; WindowRect.right=(long)width; WindowRect.top=(long)0; WindowRect.bottom=(long)height; hInstance = GetModuleHandle(NULL); wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_WINLOGO); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = NULL; wc.lpszMenuName = NULL; wc.lpszClassName = "OpenGL"; if(!RegisterClass(&wc)) { MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION); return FALSE; } if(FULLSCREEN) { DEVMODE dmScreenSettings; memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); dmScreenSettings.dmSize=sizeof(dmScreenSettings); dmScreenSettings.dmPelsWidth = width; dmScreenSettings.dmPelsHeight = height; dmScreenSettings.dmBitsPerPel = 32; dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT; if(ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL) { return FALSE; } dwExStyle=WS_EX_APPWINDOW; dwStyle=WS_POPUP; ShowCursor(FALSE); } else { dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; dwStyle=WS_OVERLAPPEDWINDOW; } AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle); if( !(hWnd=CreateWindowEx( dwExStyle, "OpenGL", "Waveride by Straylight", dwStyle | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 0, 0, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, NULL, NULL, hInstance, NULL)) ) { killWindow(); return FALSE; } static PIXELFORMATDESCRIPTOR pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, 32, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0 }; if(!(hDC=GetDC(hWnd))) { killWindow(); return FALSE; } if(!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) { killWindow(); return FALSE; } if(!SetPixelFormat(hDC,PixelFormat,&pfd)) { killWindow(); return FALSE; } if(!(hRC=wglCreateContext(hDC))) { killWindow(); return FALSE; } if(!wglMakeCurrent(hDC,hRC)) { killWindow(); return FALSE; } ShowWindow(hWnd,SW_SHOW); SetForegroundWindow(hWnd); SetFocus(hWnd); resize(width, height); init(); return TRUE; }