Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #define _USE_MATH_DEFINES
  3. #include <math.h>
  4. //#define WIN32_LEAN_AND_MEAN
  5. #include <windows.h>
  6. #include <assert.h>
  7. #include <gl/gl.h>
  8. #include <gl/glext.h>
  9. #include <gl/wglext.h>
  10. #include "resource.h"
  11.  
  12. #pragma warning (disable : 4996)
  13.  
  14. BOOL app_active = TRUE;
  15. BOOL app_focus = TRUE;
  16. int xPos = 0;
  17. int yPos = 0;
  18. int xSize = 0;
  19. int ySize = 0;
  20. HINSTANCE hInst;
  21. const char *programName = "Paranoid Graphics Demo";
  22. const char *windowClassname = "ParanoidWindow";
  23. BOOL vsyncOn;
  24. BOOL wireframeOn;
  25. int gameCurrentTime;
  26. int gameBaseTime;
  27. int gameDeltaTime;
  28.  
  29. // - Credits: Raz0r
  30. //This complex horrid mess disables V-Sync.
  31. PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = NULL;
  32.  
  33. void SetSwapInterval( int swapInterval )
  34. {
  35. // - Credits: Raz0r
  36. //This complex horrid mess disables V-Sync.
  37. wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT");
  38. if (wglSwapIntervalEXT) wglSwapIntervalEXT(swapInterval); // disable vertical synchronisation
  39. }
  40.  
  41. void SetWireframeMode( BOOL enabled )
  42. {
  43. glPolygonMode( GL_FRONT_AND_BACK, enabled ? GL_LINE : GL_FILL );
  44. }
  45.  
  46. LRESULT CALLBACK ParanoidWndProc( HWND hWnd, unsigned int Msg, WPARAM wParam, LPARAM lParam )
  47. {
  48. PAINTSTRUCT ps;
  49. HDC hdc;
  50. /*char debugMsg[256];
  51.  
  52. _snprintf( debugMsg, sizeof(debugMsg), "Message: 0x%x, wParam: %i, lParam: %i\n", Msg, wParam, lParam );
  53. OutputDebugString( debugMsg );*/
  54.  
  55. switch ( Msg )
  56. {
  57. case WM_CLOSE: // case WM_DESTROY:, TODO: Research which is the best to use
  58. PostQuitMessage(0);
  59. break;
  60.  
  61. case WM_MOVE:
  62. xPos = LOWORD(lParam);
  63. yPos = HIWORD(lParam);
  64. break;
  65.  
  66. case WM_SIZE:
  67. xSize = LOWORD(lParam);
  68. ySize = HIWORD(lParam);
  69. break;
  70.  
  71. case WM_ACTIVATE:
  72. /*if ( LOWORD(wParam) == WA_INACTIVE ) {
  73. app_focus = FALSE;
  74. } else {
  75. app_focus = TRUE;
  76. }*/
  77. app_focus = (LOWORD(wParam) != WA_INACTIVE);
  78. break;
  79.  
  80. case WM_PAINT:
  81. hdc = BeginPaint( hWnd, &ps );
  82. //RenderDisplay(hdc,&ps);
  83. EndPaint( hWnd, &ps );
  84. break;
  85.  
  86. case WM_CHAR:
  87. switch (wParam)
  88. {
  89. case 'V':
  90. case 'v':
  91. vsyncOn = !vsyncOn;
  92. SetSwapInterval(vsyncOn);
  93. break;
  94. case 'Z':
  95. case 'z':
  96. wireframeOn = !wireframeOn;
  97. SetWireframeMode(wireframeOn);
  98. break;
  99. }
  100. break;
  101.  
  102. default:
  103. return DefWindowProc(hWnd,Msg,wParam,lParam);
  104. }
  105.  
  106. return 0;
  107. }
  108.  
  109. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  110. {
  111. WNDCLASSEX mainWndClass;
  112. HWND ourWindow;
  113. MSG msg;
  114. HDC hdc;
  115. HDC desktopHDC;
  116. int windowX, windowY;
  117. unsigned int pixelformat;
  118. HGLRC hglrc;
  119. int exitCode = 0;
  120. PIXELFORMATDESCRIPTOR ourPFD =
  121. {
  122. sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
  123. 1, // version number
  124. PFD_DRAW_TO_WINDOW | // support window
  125. PFD_SUPPORT_OPENGL | // support OpenGL
  126. PFD_DOUBLEBUFFER, // double buffered
  127. PFD_TYPE_RGBA, // RGBA type
  128. 24, // 24-bit color depth
  129. 0, 0, 0, 0, 0, 0, // color bits ignored
  130. 0, // no alpha buffer
  131. 0, // shift bit ignored
  132. 0, // no accumulation buffer
  133. 0, 0, 0, 0, // accum bits ignored
  134. 24, // 24-bit z-buffer
  135. 8, // 8-bit stencil buffer
  136. 0, // no auxiliary buffer
  137. PFD_MAIN_PLANE, // main layer
  138. 0, // reserved
  139. 0, 0, 0 // layer masks ignored
  140. };
  141.  
  142. hInst = hInstance;
  143.  
  144. memset(&mainWndClass,0,sizeof(WNDCLASSEX));
  145. mainWndClass.cbSize = sizeof(WNDCLASSEX);
  146. mainWndClass.style = CS_VREDRAW|CS_HREDRAW|CS_OWNDC|CS_DBLCLKS;
  147. mainWndClass.lpfnWndProc = ParanoidWndProc;
  148. mainWndClass.hInstance = hInstance;
  149. mainWndClass.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
  150. mainWndClass.hCursor = LoadCursor(NULL,IDC_ARROW);
  151. //mainWndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
  152. mainWndClass.lpszClassName = windowClassname;
  153. mainWndClass.hIconSm = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
  154. if ( !RegisterClassEx(&mainWndClass) ) {
  155. MessageBox(NULL,"Failed to register a window class for the application",programName,MB_OK|MB_ICONSTOP);
  156. return 1;
  157. }
  158.  
  159. desktopHDC = GetDC( GetDesktopWindow() );
  160. windowX = (GetDeviceCaps( desktopHDC, HORZRES ) - 800) / 2;
  161. windowY = (GetDeviceCaps( desktopHDC, VERTRES ) - 600) / 2;
  162. ReleaseDC( GetDesktopWindow(), desktopHDC );
  163.  
  164. ourWindow = CreateWindowEx( 0,
  165. windowClassname,
  166. programName,
  167. (WS_OVERLAPPEDWINDOW|WS_VISIBLE|WS_CLIPCHILDREN|WS_CLIPSIBLINGS)&~(WS_THICKFRAME|WS_MAXIMIZEBOX),
  168. windowX, windowY,
  169. 800, 600,
  170. NULL, NULL, hInstance, NULL
  171. );
  172. if ( !ourWindow ) {
  173. MessageBox(NULL,"Failed to create a window for the application, you might be using DOS?!",programName,MB_OK|MB_ICONSTOP);
  174. return 1;
  175. }
  176. //ShowWindow( ourWindow, nCmdShow );
  177.  
  178. hdc = GetDC(ourWindow);
  179. if ( !hdc ) {
  180. MessageBox(ourWindow,"Failed to create a device context for the application",programName,MB_OK|MB_ICONSTOP);
  181. return 1;
  182. }
  183.  
  184. ourPFD.cColorBits = 32;
  185. ourPFD.cDepthBits = 24;
  186. ourPFD.cStencilBits = 8;
  187.  
  188. pixelformat = ChoosePixelFormat( hdc, &ourPFD );
  189. if( !pixelformat ) {
  190. MessageBox(ourWindow,"Failed to choose a pixel format for the application",programName,MB_OK|MB_ICONSTOP);
  191. return 1;
  192. }
  193.  
  194. if( !SetPixelFormat( hdc, pixelformat, &ourPFD ) ) {
  195. MessageBox(ourWindow,"Failed to set the pixel format for the application",programName,MB_OK|MB_ICONSTOP);
  196. return 1;
  197. }
  198.  
  199. hglrc = wglCreateContext(hdc);
  200. if ( !hglrc ) {
  201. MessageBox(ourWindow,"Failed to create an OpenGL context for the application",programName,MB_OK|MB_ICONSTOP);
  202. return 1;
  203. }
  204.  
  205. wglMakeCurrent(hdc,hglrc);
  206.  
  207. vsyncOn = TRUE;
  208. SetSwapInterval(vsyncOn);
  209.  
  210. glClearColor(0.0, 0.0, 0.0, 0.0);
  211. glEnable(GL_TEXTURE_2D);
  212. glEnable(GL_BLEND);
  213. glEnable(GL_CULL_FACE);
  214. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  215. glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
  216. glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
  217. glEnable(GL_LINE_SMOOTH);
  218. glEnable(GL_POLYGON_SMOOTH);
  219.  
  220. wireframeOn = FALSE;
  221. SetWireframeMode(wireframeOn);
  222.  
  223. gameBaseTime = timeGetTime();
  224. gameCurrentTime = 0;
  225. while (app_active)
  226. {
  227. static float xAngle = 0;
  228.  
  229. gameDeltaTime = (timeGetTime() - gameBaseTime) - gameCurrentTime;
  230. gameCurrentTime = timeGetTime() - gameBaseTime;
  231.  
  232. while ( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
  233. {
  234. if ( msg.message == WM_QUIT ) {
  235. exitCode = msg.wParam;
  236. goto afterMainLoop;
  237. }
  238.  
  239. TranslateMessage(&msg);
  240. DispatchMessage(&msg);
  241. }
  242.  
  243. glMatrixMode(GL_PROJECTION);
  244. glLoadIdentity();
  245. glOrtho(0.0, 800, 600, 0.0, 0.0, 10.0);
  246.  
  247. glMatrixMode(GL_MODELVIEW);
  248. glLoadIdentity();
  249. #if 1
  250. glClear( GL_COLOR_BUFFER_BIT );
  251. #else
  252. glBegin( GL_TRIANGLE_STRIP );
  253. glColor4f(0,0,0,0.5f);
  254. glVertex2f( 800, 0 );
  255. glVertex2f( 0, 0 );
  256. glVertex2f( 800, 600 );
  257. glVertex2f( 0, 600 );
  258. glEnd();
  259. #endif
  260. // Chain holding the crate
  261. glTranslatef(400,0,0);
  262. glRotatef( sinf(xAngle)*30, 0, 0, 1 );
  263. glBegin( GL_TRIANGLE_STRIP );
  264. glColor4f(0.5f,0.25f,0,1);
  265. glVertex2f( 5, 0 );
  266. glVertex2f( -5, 0 );
  267. glVertex2f( 5, 350 );
  268. glVertex2f( -5, 350 );
  269. glEnd();
  270.  
  271. // The crate
  272. glTranslatef(0,400,0);
  273. glBegin( GL_TRIANGLE_STRIP );
  274. glColor4f(1,0.5f,0,1);
  275. glVertex2f( 50, -50 );
  276. glVertex2f( -50, -50 );
  277. glVertex2f( 50, 50 );
  278. glVertex2f( -50, 50 );
  279. glEnd();
  280.  
  281. // The chain support at the top
  282. glLoadIdentity();
  283. glTranslatef(400,0,0);
  284. glBegin( GL_TRIANGLE_FAN );
  285. glColor4f(0.5f,0.5f,0.5f,1);
  286. glVertex2f( 0, 0 );
  287. glVertex2f( -50, 0 );
  288. glVertex2f( -40, 20 );
  289. glVertex2f( 40, 20 );
  290. glVertex2f( 50, 0 );
  291. glEnd();
  292.  
  293. SwapBuffers(hdc);
  294. xAngle = (gameCurrentTime/1000.0f) * (float)(2*M_PI) * 0.5f;
  295.  
  296. if (!vsyncOn) Sleep(1); // We don't need to slow down if V-sync is on
  297. //if (!vsyncOn && gameDeltaTime < 1) Sleep(1); // We don't need to slow down if V-sync is on
  298. }
  299. afterMainLoop:
  300.  
  301. if ( !ReleaseDC(ourWindow, hdc) ) return 1;
  302.  
  303. if ( !DestroyWindow(ourWindow) ) return 1;
  304.  
  305. return exitCode;
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement