Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #define _USE_MATH_DEFINES
- #include <math.h>
- //#define WIN32_LEAN_AND_MEAN
- #include <windows.h>
- #include <assert.h>
- #include <gl/gl.h>
- #include <gl/glext.h>
- #include <gl/wglext.h>
- #include "resource.h"
- #pragma warning (disable : 4996)
- BOOL app_active = TRUE;
- BOOL app_focus = TRUE;
- int xPos = 0;
- int yPos = 0;
- int xSize = 0;
- int ySize = 0;
- HINSTANCE hInst;
- const char *programName = "Paranoid Graphics Demo";
- const char *windowClassname = "ParanoidWindow";
- BOOL vsyncOn;
- BOOL wireframeOn;
- int gameCurrentTime;
- int gameBaseTime;
- int gameDeltaTime;
- // - Credits: Raz0r
- //This complex horrid mess disables V-Sync.
- PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
- void SetSwapInterval( int swapInterval )
- {
- // - Credits: Raz0r
- //This complex horrid mess disables V-Sync.
- wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
- if (wglSwapIntervalEXT) wglSwapIntervalEXT(swapInterval); // disable vertical synchronisation
- }
- void SetWireframeMode( BOOL enabled )
- {
- glPolygonMode( GL_FRONT_AND_BACK, enabled ? GL_LINE : GL_FILL );
- }
- LRESULT CALLBACK ParanoidWndProc( HWND hWnd, unsigned int Msg, WPARAM wParam, LPARAM lParam )
- {
- PAINTSTRUCT ps;
- HDC hdc;
- /*char debugMsg[256];
- _snprintf( debugMsg, sizeof(debugMsg), "Message: 0x%x, wParam: %i, lParam: %i\n", Msg, wParam, lParam );
- OutputDebugString( debugMsg );*/
- switch ( Msg )
- {
- case WM_CLOSE: // case WM_DESTROY:, TODO: Research which is the best to use
- PostQuitMessage(0);
- break;
- case WM_MOVE:
- xPos = LOWORD(lParam);
- yPos = HIWORD(lParam);
- break;
- case WM_SIZE:
- xSize = LOWORD(lParam);
- ySize = HIWORD(lParam);
- break;
- case WM_ACTIVATE:
- /*if ( LOWORD(wParam) == WA_INACTIVE ) {
- app_focus = FALSE;
- } else {
- app_focus = TRUE;
- }*/
- app_focus = (LOWORD(wParam) != WA_INACTIVE);
- break;
- case WM_PAINT:
- hdc = BeginPaint( hWnd, &ps );
- //RenderDisplay(hdc,&ps);
- EndPaint( hWnd, &ps );
- break;
- case WM_CHAR:
- switch (wParam)
- {
- case 'V':
- case 'v':
- vsyncOn = !vsyncOn;
- SetSwapInterval(vsyncOn);
- break;
- case 'Z':
- case 'z':
- wireframeOn = !wireframeOn;
- SetWireframeMode(wireframeOn);
- break;
- }
- break;
- default:
- return DefWindowProc(hWnd,Msg,wParam,lParam);
- }
- return 0;
- }
- int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
- {
- WNDCLASSEX mainWndClass;
- HWND ourWindow;
- MSG msg;
- HDC hdc;
- HDC desktopHDC;
- int windowX, windowY;
- unsigned int pixelformat;
- HGLRC hglrc;
- int exitCode = 0;
- PIXELFORMATDESCRIPTOR ourPFD =
- {
- sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
- 1, // version number
- PFD_DRAW_TO_WINDOW | // support window
- PFD_SUPPORT_OPENGL | // support OpenGL
- PFD_DOUBLEBUFFER, // double buffered
- PFD_TYPE_RGBA, // RGBA type
- 24, // 24-bit 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, // accum bits ignored
- 24, // 24-bit z-buffer
- 8, // 8-bit stencil buffer
- 0, // no auxiliary buffer
- PFD_MAIN_PLANE, // main layer
- 0, // reserved
- 0, 0, 0 // layer masks ignored
- };
- hInst = hInstance;
- memset(&mainWndClass,0,sizeof(WNDCLASSEX));
- mainWndClass.cbSize = sizeof(WNDCLASSEX);
- mainWndClass.style = CS_VREDRAW|CS_HREDRAW|CS_OWNDC|CS_DBLCLKS;
- mainWndClass.lpfnWndProc = ParanoidWndProc;
- mainWndClass.hInstance = hInstance;
- mainWndClass.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
- mainWndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
- //mainWndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
- mainWndClass.lpszClassName = windowClassname;
- mainWndClass.hIconSm = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
- if ( !RegisterClassEx(&mainWndClass) ) {
- MessageBox(NULL,"Failed to register a window class for the application",programName,MB_OK|MB_ICONSTOP);
- return 1;
- }
- desktopHDC = GetDC( GetDesktopWindow() );
- windowX = (GetDeviceCaps( desktopHDC, HORZRES ) - 800) / 2;
- windowY = (GetDeviceCaps( desktopHDC, VERTRES ) - 600) / 2;
- ReleaseDC( GetDesktopWindow(), desktopHDC );
- ourWindow = CreateWindowEx( 0,
- windowClassname,
- programName,
- (WS_OVERLAPPEDWINDOW|WS_VISIBLE|WS_CLIPCHILDREN|WS_CLIPSIBLINGS)&~(WS_THICKFRAME|WS_MAXIMIZEBOX),
- windowX, windowY,
- 800, 600,
- NULL, NULL, hInstance, NULL
- );
- if ( !ourWindow ) {
- MessageBox(NULL,"Failed to create a window for the application, you might be using DOS?!",programName,MB_OK|MB_ICONSTOP);
- return 1;
- }
- //ShowWindow( ourWindow, nCmdShow );
- hdc = GetDC(ourWindow);
- if ( !hdc ) {
- MessageBox(ourWindow,"Failed to create a device context for the application",programName,MB_OK|MB_ICONSTOP);
- return 1;
- }
- ourPFD.cColorBits = 32;
- ourPFD.cDepthBits = 24;
- ourPFD.cStencilBits = 8;
- pixelformat = ChoosePixelFormat( hdc, &ourPFD );
- if( !pixelformat ) {
- MessageBox(ourWindow,"Failed to choose a pixel format for the application",programName,MB_OK|MB_ICONSTOP);
- return 1;
- }
- if( !SetPixelFormat( hdc, pixelformat, &ourPFD ) ) {
- MessageBox(ourWindow,"Failed to set the pixel format for the application",programName,MB_OK|MB_ICONSTOP);
- return 1;
- }
- hglrc = wglCreateContext(hdc);
- if ( !hglrc ) {
- MessageBox(ourWindow,"Failed to create an OpenGL context for the application",programName,MB_OK|MB_ICONSTOP);
- return 1;
- }
- wglMakeCurrent(hdc,hglrc);
- vsyncOn = TRUE;
- SetSwapInterval(vsyncOn);
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glEnable(GL_TEXTURE_2D);
- glEnable(GL_BLEND);
- glEnable(GL_CULL_FACE);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
- glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
- glEnable(GL_LINE_SMOOTH);
- glEnable(GL_POLYGON_SMOOTH);
- wireframeOn = FALSE;
- SetWireframeMode(wireframeOn);
- gameBaseTime = timeGetTime();
- gameCurrentTime = 0;
- while (app_active)
- {
- static float xAngle = 0;
- gameDeltaTime = (timeGetTime() - gameBaseTime) - gameCurrentTime;
- gameCurrentTime = timeGetTime() - gameBaseTime;
- while ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
- {
- if ( msg.message == WM_QUIT ) {
- exitCode = msg.wParam;
- goto afterMainLoop;
- }
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, 800, 600, 0.0, 0.0, 10.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- #if 1
- glClear( GL_COLOR_BUFFER_BIT );
- #else
- glBegin( GL_TRIANGLE_STRIP );
- glColor4f(0,0,0,0.5f);
- glVertex2f( 800, 0 );
- glVertex2f( 0, 0 );
- glVertex2f( 800, 600 );
- glVertex2f( 0, 600 );
- glEnd();
- #endif
- // Chain holding the crate
- glTranslatef(400,0,0);
- glRotatef( sinf(xAngle)*30, 0, 0, 1 );
- glBegin( GL_TRIANGLE_STRIP );
- glColor4f(0.5f,0.25f,0,1);
- glVertex2f( 5, 0 );
- glVertex2f( -5, 0 );
- glVertex2f( 5, 350 );
- glVertex2f( -5, 350 );
- glEnd();
- // The crate
- glTranslatef(0,400,0);
- glBegin( GL_TRIANGLE_STRIP );
- glColor4f(1,0.5f,0,1);
- glVertex2f( 50, -50 );
- glVertex2f( -50, -50 );
- glVertex2f( 50, 50 );
- glVertex2f( -50, 50 );
- glEnd();
- // The chain support at the top
- glLoadIdentity();
- glTranslatef(400,0,0);
- glBegin( GL_TRIANGLE_FAN );
- glColor4f(0.5f,0.5f,0.5f,1);
- glVertex2f( 0, 0 );
- glVertex2f( -50, 0 );
- glVertex2f( -40, 20 );
- glVertex2f( 40, 20 );
- glVertex2f( 50, 0 );
- glEnd();
- SwapBuffers(hdc);
- xAngle = (gameCurrentTime/1000.0f) * (float)(2*M_PI) * 0.5f;
- if (!vsyncOn) Sleep(1); // We don't need to slow down if V-sync is on
- //if (!vsyncOn && gameDeltaTime < 1) Sleep(1); // We don't need to slow down if V-sync is on
- }
- afterMainLoop:
- if ( !ReleaseDC(ourWindow, hdc) ) return 1;
- if ( !DestroyWindow(ourWindow) ) return 1;
- return exitCode;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement