Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <windowsx.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "MathHelper.h"
- #include "GL.h"
- #include "GLEnums.h"
- #include "GameColorPalette.h"
- #include <string>
- #include "Point.h"
- #include "Size.h"
- #include "GameMouse.h"
- #include "GameConsole.h"
- HINSTANCE hInstance;
- HWND hwnd;
- HDC hdc;
- HGLRC hglrc;
- Size windowSize;
- GameMouse mouse;
- std::string windowText;
- bool isGameRunning = true;
- /*
- Frames Per Second Variables
- */
- int frameCount = 0;
- float fps = 0;
- int currentTime = 0;
- int previousTime = 0;
- //
- LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow );
- void draw();
- void setTitle( const std::string& text );
- void initializeGL() {
- GL::clearColor( GameColorPalette::Cornflowerblue );
- }
- void calculateFPS() {
- frameCount++;
- currentTime = timeGetTime();
- int timeInterval = currentTime - previousTime;
- if( timeInterval > 1000 ) {
- fps = frameCount / ( timeInterval / 1000.0f );
- previousTime = currentTime;
- frameCount = 0;
- }
- }
- void toggleVSync( bool toggle ) {
- typedef BOOL( WINAPI *PFNWGLSWAPINTERVALEXTPROC )( int interval );
- PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
- wglSwapIntervalEXT = ( PFNWGLSWAPINTERVALEXTPROC )wglGetProcAddress( "wglSwapIntervalEXT" );
- if( wglSwapIntervalEXT )
- wglSwapIntervalEXT( toggle );
- }
- int WINAPI WinMain( HINSTANCE h_Instance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow ) {
- hInstance = h_Instance;
- WNDCLASS wc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hbrBackground = ( HBRUSH )GetStockObject( BLACK_BRUSH );
- wc.hCursor = LoadCursor( NULL, IDC_ARROW );
- wc.hIcon = LoadIcon( NULL, IDI_APPLICATION );
- wc.hInstance = hInstance;
- wc.lpfnWndProc = WndProc;
- wc.lpszClassName = TEXT( "Client" );
- wc.lpszMenuName = 0;
- wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
- RegisterClass( &wc );
- RECT rect;
- SetRect( &rect, 50,
- 50,
- 850,
- 650 );
- windowSize.setWidth( rect.right - rect.left );
- windowSize.setHeight( rect.bottom - rect.top );
- AdjustWindowRect( &rect, WS_OVERLAPPEDWINDOW, false );
- hwnd = CreateWindow( TEXT( "Client" ),
- TEXT( "Game Window" ),
- WS_OVERLAPPEDWINDOW,
- rect.left, rect.top,
- rect.right - rect.left, rect.bottom - rect.top,
- NULL, NULL,
- hInstance, NULL );
- if( hwnd == NULL ) {
- FatalAppExit( NULL, TEXT( "CreateWindow() failed!" ) );
- }
- ShowWindow( hwnd, iCmdShow );
- hdc = GetDC( hwnd );
- PIXELFORMATDESCRIPTOR pfd = { 0 };
- pfd.nSize = sizeof( PIXELFORMATDESCRIPTOR );
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_DRAW_TO_WINDOW;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 24;
- pfd.cDepthBits = 32;
- int chosenPixelFormat = ChoosePixelFormat( hdc, &pfd );
- if( chosenPixelFormat == 0 ) {
- FatalAppExit( NULL, TEXT( "ChoosePixelFormat() failed!" ) );
- }
- int result = SetPixelFormat( hdc, chosenPixelFormat, &pfd );
- if( result == NULL ) {
- FatalAppExit( NULL, TEXT( "SetPixelFormat() failed!" ) );
- }
- hglrc = wglCreateContext( hdc );
- wglMakeCurrent( hdc, hglrc );
- MSG msg;
- GameConsole::create();
- toggleVSync( false );
- //
- initializeGL();
- while( isGameRunning ) {
- if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) {
- if( msg.message == WM_QUIT ) {
- break;
- }
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- } else {
- draw();
- }
- }
- /*
- Destroy Game Client
- */
- wglMakeCurrent( NULL, NULL );
- wglDeleteContext( hglrc );
- ReleaseDC( hwnd, hdc );
- AnimateWindow( hwnd, 200, AW_HIDE | AW_BLEND );
- GameConsole::destroy();
- return msg.wParam;
- }
- void draw() {
- GL::viewport( 0, 0, windowSize.getWidth(), windowSize.getHeight() );
- GL::matrixMode( MatrixMode::Projection );
- GL::loadIdentity();
- GL::perspective( 45.0, ( float )windowSize.getWidth() / ( float )windowSize.getHeight(), 0.1f, 1000.0f );
- GL::matrixMode( MatrixMode::Modelview );
- GL::loadIdentity();
- GL::lookAt(
- 0, 0, 10,
- 0, 0, 0,
- 0, 1, 0 );
- GL::clear( ClearBufferMask::ColorBufferBit | ClearBufferMask::DepthBufferBit );
- GL::begin( PrimitiveType::Triangles );
- GL::color3( GameColorPalette::Red );
- GL::vertex3( 1, 0, 0 );
- GL::color3( GameColorPalette::Green );
- GL::vertex3( 0, 1, 0 );
- GL::color3( GameColorPalette::Blue );
- GL::vertex3( -1, 0, 0 );
- GL::end();
- SwapBuffers( hdc );
- calculateFPS();
- windowText = std::string( "Game Window" ) + " FPS: " + std::to_string( fps );
- setTitle( windowText );
- }
- LRESULT CALLBACK WndProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam ) {
- switch( message ) {
- case WM_CREATE:
- return 0;
- break;
- case WM_LBUTTONDOWN:
- return 0;
- break;
- case WM_LBUTTONUP:
- return 0;
- break;
- case WM_RBUTTONDOWN:
- return 0;
- break;
- case WM_RBUTTONUP:
- return 0;
- break;
- case WM_SIZE:
- windowSize.width = LOWORD( lparam );
- windowSize.height = HIWORD( lparam );
- GameConsole::writeLine( "Game Window Resized" );
- GameConsole::writeLine( std::string( "Width: " ) + std::to_string( windowSize.width ) );
- GameConsole::writeLine( std::string( "Height: " ) + std::to_string( windowSize.height ) );
- return 0;
- break;
- case WM_MOUSEMOVE:
- mouse.location.x = LOWORD( lparam );
- mouse.location.y = HIWORD( lparam );
- return 0;
- break;
- case WM_PAINT:
- HDC hdc;
- PAINTSTRUCT ps;
- hdc = BeginPaint( hwnd, &ps );
- EndPaint( hwnd, &ps );
- return 0;
- break;
- case WM_KEYDOWN:
- switch( wparam ) {
- case VK_ESCAPE:
- isGameRunning = false;
- break;
- default:
- break;
- }
- return 0;
- case WM_DESTROY:
- PostQuitMessage( 0 );
- return 0;
- break;
- }
- return DefWindowProc( hwnd, message, wparam, lparam );
- }
- void setTitle( const std::string& text ) {
- SetWindowText( hwnd, ( LPSTR )text.c_str() );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement