Guest User

Untitled

a guest
Sep 17th, 2013
1,204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.99 KB | None | 0 0
  1. /******************************************************************
  2. *   original DX9 code courtesy of www.directxtutorial.com
  3. *   modified by learn_more
  4. */
  5.  
  6. #define USE_DIRECTX     (USE_RENDERER == REND_DX9)
  7. #define USE_OPENGL      (USE_RENDERER == REND_OGL)
  8. #define USE_GDI         (USE_RENDERER == REND_GDI)
  9.  
  10. #define GDI_TRANS_COL           RGB(TRANS_R,TRANS_G,TRANS_B)
  11. #define GDI_TRIANGLE_COL        RGB(TRI_R,TRI_G,TRI_B)
  12. #define DX_TRANS_COL            D3DCOLOR_XRGB(TRANS_R,TRANS_G,TRANS_B)
  13. #define DX_TRIANGLE_COL         D3DCOLOR_XRGB(TRI_R,TRI_G,TRI_B)
  14.  
  15. #define REND_DX9    1
  16. #define REND_OGL    2
  17. #define REND_GDI    3
  18.  
  19.  
  20.  
  21.  
  22. #define _WIN32_WINNT 0x0501
  23. #define _WIN32_WINDOWS 0x0410
  24. #define _WIN32_IE 0x0600
  25. #include <windows.h>
  26.  
  27.  
  28. #define USE_RENDERER    REND_GDI        //REND_DX9  |  REND_OGL  |  REND_GDI
  29. #define TRY_TRANSPARANT
  30.  
  31. #define TOTAL_OPACITY   140
  32.  
  33. #define WINDOW_WIDTH    640
  34. #define WINDOW_HEIGHT   480
  35. #define EDGE_OFF        40
  36.  
  37. #define TRANS_R     255
  38. #define TRANS_G     0
  39. #define TRANS_B     255
  40.  
  41. #define TRI_R       255
  42. #define TRI_G       0
  43. #define TRI_B       0
  44.  
  45.  
  46.  
  47.  
  48. #if USE_DIRECTX
  49. #include <d3d9.h>
  50. #pragma comment (lib, "d3d9")
  51.  
  52. LPDIRECT3D9 d3d;
  53. LPDIRECT3DDEVICE9 d3ddev;
  54. LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;
  55. struct CUSTOMVERTEX {FLOAT X, Y, Z, RHW; DWORD COLOR;};
  56. #define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
  57. #elif USE_OPENGL
  58. #include <GL/gl.h>
  59. #include <GL/glu.h>
  60. #pragma comment( lib, "opengl32" )
  61. #pragma comment( lib, "glu32" )
  62. #endif
  63.  
  64.  
  65. #if USE_OPENGL
  66. HDC m_hdc;
  67. HGLRC m_hrc;
  68. HWND m_hwnd;
  69. #endif
  70.  
  71. void render_frame( HDC hdc )
  72. {
  73. #if USE_DIRECTX
  74.     d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, DX_TRANS_COL, 1.0f, 0);
  75.  
  76.     d3ddev->BeginScene();
  77.  
  78.     d3ddev->SetFVF(CUSTOMFVF);
  79.     d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
  80.     d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
  81.  
  82.     d3ddev->EndScene();
  83.     d3ddev->Present(NULL, NULL, NULL, NULL);
  84. #elif USE_OPENGL
  85.     glClear( GL_COLOR_BUFFER_BIT );
  86.     glBegin( GL_TRIANGLES );
  87.         glColor3d( TRI_R, TRI_G, TRI_B );
  88.         glVertex2i( WINDOW_WIDTH/2, EDGE_OFF );
  89.         glVertex2i( EDGE_OFF, WINDOW_HEIGHT-EDGE_OFF );
  90.         glVertex2i( WINDOW_WIDTH-EDGE_OFF, WINDOW_HEIGHT-EDGE_OFF );
  91.     glEnd();
  92.  
  93.  
  94.     SwapBuffers( m_hdc );
  95. #elif USE_GDI
  96.     HBRUSH hNewBrush = CreateSolidBrush( GDI_TRANS_COL );   //create a brush with our key color
  97.     HGDIOBJ hOrBrush = SelectObject( hdc, hNewBrush );      //use the created brush
  98.     Rectangle( hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT );
  99.     SelectObject( hdc, hOrBrush );
  100.     DeleteObject( hNewBrush );
  101.  
  102.     hNewBrush = CreateSolidBrush( GDI_TRIANGLE_COL );
  103.     hOrBrush = SelectObject( hdc, hNewBrush );
  104.     POINT pt[3] = { {WINDOW_WIDTH/2,EDGE_OFF},
  105.                     {WINDOW_WIDTH-EDGE_OFF,WINDOW_HEIGHT-EDGE_OFF},
  106.                     {EDGE_OFF,WINDOW_HEIGHT-EDGE_OFF}};
  107.     Polygon( hdc, pt, 3 );
  108.     SelectObject( hdc, hOrBrush );
  109.     DeleteObject( hNewBrush );
  110. #endif
  111. }
  112.  
  113. void initGFX( HWND hWnd )
  114. {
  115. #if USE_DIRECTX
  116.     d3d = Direct3DCreate9(D3D_SDK_VERSION);
  117.  
  118.     D3DPRESENT_PARAMETERS d3dpp = {0};
  119.     d3dpp.Windowed = TRUE;
  120.     d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  121.     d3dpp.hDeviceWindow = hWnd;
  122.     d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
  123.     d3dpp.BackBufferWidth = WINDOW_WIDTH;
  124.     d3dpp.BackBufferHeight = WINDOW_HEIGHT;
  125.  
  126.     d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev );
  127.    
  128.     CUSTOMVERTEX vertices[] =
  129.     {
  130.         { WINDOW_WIDTH/2, EDGE_OFF, 0.5f, 1.0f, DX_TRIANGLE_COL, },
  131.         { WINDOW_WIDTH-EDGE_OFF, WINDOW_HEIGHT-EDGE_OFF, 0.5f, 1.0f, DX_TRIANGLE_COL, },
  132.         { EDGE_OFF, WINDOW_HEIGHT-EDGE_OFF, 0.5f, 1.0f, DX_TRIANGLE_COL, },
  133.     };
  134.  
  135.     d3ddev->CreateVertexBuffer( sizeof(vertices), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL );
  136.     VOID* pVoid;
  137.     v_buffer->Lock(0, 0, (void**)&pVoid, 0);
  138.     memcpy(pVoid, vertices, sizeof(vertices));
  139.     v_buffer->Unlock();
  140. #elif USE_OPENGL
  141.     PIXELFORMATDESCRIPTOR pfd = {0};
  142.    
  143.     m_hdc = GetDC( hWnd );
  144.     pfd.nSize        = sizeof(pfd);
  145.     pfd.nVersion     = 1;
  146.     pfd.dwFlags      = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
  147.     pfd.iPixelType   = PFD_TYPE_RGBA;
  148.     pfd.cColorBits   = 32;
  149.  
  150.     int pf = ChoosePixelFormat( m_hdc, &pfd);
  151.     SetPixelFormat( m_hdc, pf, &pfd);
  152.  
  153.     m_hrc = wglCreateContext( m_hdc );
  154.     wglMakeCurrent( m_hdc, m_hrc );
  155.  
  156.     glClearColor( TRANS_R/255.0f, TRANS_G/255.0f, TRANS_B/255.0f, 1 );
  157.  
  158.     glMatrixMode( GL_PROJECTION );
  159.     glLoadIdentity();
  160.     gluOrtho2D( 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0 );
  161.  
  162.     glClear( GL_COLOR_BUFFER_BIT );
  163.     SwapBuffers( m_hdc );
  164.  
  165.     m_hwnd = hWnd;
  166. #endif
  167. }
  168.  
  169.  
  170. void cleanGFX()
  171. {
  172. #if USE_DIRECTX
  173.     v_buffer->Release();
  174.     d3ddev->Release();
  175.     d3d->Release();
  176. #elif USE_OPENGL
  177.     wglMakeCurrent( NULL, NULL );
  178.     ReleaseDC( m_hwnd, m_hdc );
  179.     wglDeleteContext( m_hrc );
  180. #endif
  181. }
  182.  
  183.  
  184. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  185. {
  186.     switch (message)
  187.     {
  188. #if USE_GDI
  189.     case WM_PAINT:
  190.         {
  191.             PAINTSTRUCT ps;
  192.             HDC hdc;
  193.             hdc = BeginPaint( hWnd, &ps );
  194.             render_frame( hdc );
  195.             EndPaint(hWnd, &ps);
  196.         }
  197.         break;
  198. #endif
  199.     case WM_KEYUP:
  200.         if( wParam == VK_ESCAPE )
  201.             PostQuitMessage( 0 );
  202.         break;
  203.     case WM_DESTROY:
  204.         PostQuitMessage( 0 );
  205.         break;
  206. #if USE_OPENGL
  207.     case WM_SIZE:
  208.         glViewport( 0, 0, LOWORD(lParam), HIWORD(lParam) );
  209.         break;
  210. #endif
  211.     default:
  212.         return DefWindowProc(hWnd, message, wParam, lParam);
  213.     }
  214.  
  215.     return 0;
  216. }
  217.  
  218. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
  219. {
  220.     HWND hWnd;
  221.     WNDCLASSEX wc = {0};
  222.     TCHAR szClassName[] = TEXT("GFXTestApp");
  223. #if USE_DIRECTX
  224.     TCHAR szTitle[] = TEXT("DX");
  225. #elif USE_OPENGL
  226.     TCHAR szTitle[] = TEXT("OGL");
  227. #else
  228.     TCHAR szTitle[] = TEXT("GDI");
  229. #endif
  230.     wc.cbSize = sizeof(WNDCLASSEX);
  231. #if USE_OPENGL
  232.     wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
  233. #else
  234.     wc.style = CS_HREDRAW | CS_VREDRAW;
  235. #endif
  236.     wc.lpfnWndProc = WndProc;
  237.     wc.hInstance = hInstance;
  238.     wc.hCursor = LoadCursor(NULL, IDC_CROSS);   //hax
  239.     wc.lpszClassName = szClassName;
  240.  
  241.     RegisterClassEx(&wc);
  242.    
  243.     DWORD dwStyle = WS_POPUPWINDOW|WS_THICKFRAME|WS_CAPTION;
  244.  
  245.     RECT rc = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
  246.     AdjustWindowRect( &rc, dwStyle, NULL );     //ensure the client rectangle is screen_width*height
  247.  
  248.     hWnd = CreateWindowEx( NULL, szClassName, szTitle, dwStyle,
  249.         0, 0, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL );
  250.  
  251.     if( !hWnd ) {
  252.         MessageBox( NULL, TEXT("You fucked WndProc or the classname probably?"),
  253.             TEXT("Fail create window"), MB_OK|MB_ICONERROR );
  254.         ExitProcess( 0 );
  255.     }
  256.  
  257.     initGFX(hWnd);
  258.     ShowWindow( hWnd, nCmdShow );
  259.  
  260. #ifdef  TRY_TRANSPARANT
  261.     LONG lOld = GetWindowLong( hWnd, GWL_EXSTYLE );
  262.     SetWindowLong( hWnd, GWL_EXSTYLE, lOld | WS_EX_LAYERED );
  263.     SetLayeredWindowAttributes( hWnd, GDI_TRANS_COL, TOTAL_OPACITY, LWA_COLORKEY|LWA_ALPHA );
  264. #endif
  265.  
  266.     MSG msg;
  267.     while(TRUE)
  268.     {
  269.         while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  270.         {
  271.             TranslateMessage(&msg);
  272.             DispatchMessage(&msg);
  273.         }
  274.         if( msg.message == WM_QUIT)
  275.             break;
  276. #if USE_DIRECTX || USE_OPENGL
  277.         render_frame( NULL );
  278. #endif
  279.     }
  280.     cleanGFX();
  281.     DestroyWindow( hWnd );
  282.  
  283.     return msg.wParam;
  284. }
Advertisement
Add Comment
Please, Sign In to add comment