Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************
- * original DX9 code courtesy of www.directxtutorial.com
- * modified by learn_more
- */
- #define USE_DIRECTX (USE_RENDERER == REND_DX9)
- #define USE_OPENGL (USE_RENDERER == REND_OGL)
- #define USE_GDI (USE_RENDERER == REND_GDI)
- #define GDI_TRANS_COL RGB(TRANS_R,TRANS_G,TRANS_B)
- #define GDI_TRIANGLE_COL RGB(TRI_R,TRI_G,TRI_B)
- #define DX_TRANS_COL D3DCOLOR_XRGB(TRANS_R,TRANS_G,TRANS_B)
- #define DX_TRIANGLE_COL D3DCOLOR_XRGB(TRI_R,TRI_G,TRI_B)
- #define REND_DX9 1
- #define REND_OGL 2
- #define REND_GDI 3
- #define _WIN32_WINNT 0x0501
- #define _WIN32_WINDOWS 0x0410
- #define _WIN32_IE 0x0600
- #include <windows.h>
- #define USE_RENDERER REND_GDI //REND_DX9 | REND_OGL | REND_GDI
- #define TRY_TRANSPARANT
- #define TOTAL_OPACITY 140
- #define WINDOW_WIDTH 640
- #define WINDOW_HEIGHT 480
- #define EDGE_OFF 40
- #define TRANS_R 255
- #define TRANS_G 0
- #define TRANS_B 255
- #define TRI_R 255
- #define TRI_G 0
- #define TRI_B 0
- #if USE_DIRECTX
- #include <d3d9.h>
- #pragma comment (lib, "d3d9")
- LPDIRECT3D9 d3d;
- LPDIRECT3DDEVICE9 d3ddev;
- LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;
- struct CUSTOMVERTEX {FLOAT X, Y, Z, RHW; DWORD COLOR;};
- #define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
- #elif USE_OPENGL
- #include <GL/gl.h>
- #include <GL/glu.h>
- #pragma comment( lib, "opengl32" )
- #pragma comment( lib, "glu32" )
- #endif
- #if USE_OPENGL
- HDC m_hdc;
- HGLRC m_hrc;
- HWND m_hwnd;
- #endif
- void render_frame( HDC hdc )
- {
- #if USE_DIRECTX
- d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, DX_TRANS_COL, 1.0f, 0);
- d3ddev->BeginScene();
- d3ddev->SetFVF(CUSTOMFVF);
- d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
- d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
- d3ddev->EndScene();
- d3ddev->Present(NULL, NULL, NULL, NULL);
- #elif USE_OPENGL
- glClear( GL_COLOR_BUFFER_BIT );
- glBegin( GL_TRIANGLES );
- glColor3d( TRI_R, TRI_G, TRI_B );
- glVertex2i( WINDOW_WIDTH/2, EDGE_OFF );
- glVertex2i( EDGE_OFF, WINDOW_HEIGHT-EDGE_OFF );
- glVertex2i( WINDOW_WIDTH-EDGE_OFF, WINDOW_HEIGHT-EDGE_OFF );
- glEnd();
- SwapBuffers( m_hdc );
- #elif USE_GDI
- HBRUSH hNewBrush = CreateSolidBrush( GDI_TRANS_COL ); //create a brush with our key color
- HGDIOBJ hOrBrush = SelectObject( hdc, hNewBrush ); //use the created brush
- Rectangle( hdc, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT );
- SelectObject( hdc, hOrBrush );
- DeleteObject( hNewBrush );
- hNewBrush = CreateSolidBrush( GDI_TRIANGLE_COL );
- hOrBrush = SelectObject( hdc, hNewBrush );
- POINT pt[3] = { {WINDOW_WIDTH/2,EDGE_OFF},
- {WINDOW_WIDTH-EDGE_OFF,WINDOW_HEIGHT-EDGE_OFF},
- {EDGE_OFF,WINDOW_HEIGHT-EDGE_OFF}};
- Polygon( hdc, pt, 3 );
- SelectObject( hdc, hOrBrush );
- DeleteObject( hNewBrush );
- #endif
- }
- void initGFX( HWND hWnd )
- {
- #if USE_DIRECTX
- d3d = Direct3DCreate9(D3D_SDK_VERSION);
- D3DPRESENT_PARAMETERS d3dpp = {0};
- d3dpp.Windowed = TRUE;
- d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- d3dpp.hDeviceWindow = hWnd;
- d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
- d3dpp.BackBufferWidth = WINDOW_WIDTH;
- d3dpp.BackBufferHeight = WINDOW_HEIGHT;
- d3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev );
- CUSTOMVERTEX vertices[] =
- {
- { WINDOW_WIDTH/2, EDGE_OFF, 0.5f, 1.0f, DX_TRIANGLE_COL, },
- { WINDOW_WIDTH-EDGE_OFF, WINDOW_HEIGHT-EDGE_OFF, 0.5f, 1.0f, DX_TRIANGLE_COL, },
- { EDGE_OFF, WINDOW_HEIGHT-EDGE_OFF, 0.5f, 1.0f, DX_TRIANGLE_COL, },
- };
- d3ddev->CreateVertexBuffer( sizeof(vertices), 0, CUSTOMFVF, D3DPOOL_MANAGED, &v_buffer, NULL );
- VOID* pVoid;
- v_buffer->Lock(0, 0, (void**)&pVoid, 0);
- memcpy(pVoid, vertices, sizeof(vertices));
- v_buffer->Unlock();
- #elif USE_OPENGL
- PIXELFORMATDESCRIPTOR pfd = {0};
- m_hdc = GetDC( hWnd );
- pfd.nSize = sizeof(pfd);
- pfd.nVersion = 1;
- pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
- pfd.iPixelType = PFD_TYPE_RGBA;
- pfd.cColorBits = 32;
- int pf = ChoosePixelFormat( m_hdc, &pfd);
- SetPixelFormat( m_hdc, pf, &pfd);
- m_hrc = wglCreateContext( m_hdc );
- wglMakeCurrent( m_hdc, m_hrc );
- glClearColor( TRANS_R/255.0f, TRANS_G/255.0f, TRANS_B/255.0f, 1 );
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluOrtho2D( 0, WINDOW_WIDTH, WINDOW_HEIGHT, 0 );
- glClear( GL_COLOR_BUFFER_BIT );
- SwapBuffers( m_hdc );
- m_hwnd = hWnd;
- #endif
- }
- void cleanGFX()
- {
- #if USE_DIRECTX
- v_buffer->Release();
- d3ddev->Release();
- d3d->Release();
- #elif USE_OPENGL
- wglMakeCurrent( NULL, NULL );
- ReleaseDC( m_hwnd, m_hdc );
- wglDeleteContext( m_hrc );
- #endif
- }
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- #if USE_GDI
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc;
- hdc = BeginPaint( hWnd, &ps );
- render_frame( hdc );
- EndPaint(hWnd, &ps);
- }
- break;
- #endif
- case WM_KEYUP:
- if( wParam == VK_ESCAPE )
- PostQuitMessage( 0 );
- break;
- case WM_DESTROY:
- PostQuitMessage( 0 );
- break;
- #if USE_OPENGL
- case WM_SIZE:
- glViewport( 0, 0, LOWORD(lParam), HIWORD(lParam) );
- break;
- #endif
- default:
- return DefWindowProc(hWnd, message, wParam, lParam);
- }
- return 0;
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow )
- {
- HWND hWnd;
- WNDCLASSEX wc = {0};
- TCHAR szClassName[] = TEXT("GFXTestApp");
- #if USE_DIRECTX
- TCHAR szTitle[] = TEXT("DX");
- #elif USE_OPENGL
- TCHAR szTitle[] = TEXT("OGL");
- #else
- TCHAR szTitle[] = TEXT("GDI");
- #endif
- wc.cbSize = sizeof(WNDCLASSEX);
- #if USE_OPENGL
- wc.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
- #else
- wc.style = CS_HREDRAW | CS_VREDRAW;
- #endif
- wc.lpfnWndProc = WndProc;
- wc.hInstance = hInstance;
- wc.hCursor = LoadCursor(NULL, IDC_CROSS); //hax
- wc.lpszClassName = szClassName;
- RegisterClassEx(&wc);
- DWORD dwStyle = WS_POPUPWINDOW|WS_THICKFRAME|WS_CAPTION;
- RECT rc = {0, 0, WINDOW_WIDTH, WINDOW_HEIGHT};
- AdjustWindowRect( &rc, dwStyle, NULL ); //ensure the client rectangle is screen_width*height
- hWnd = CreateWindowEx( NULL, szClassName, szTitle, dwStyle,
- 0, 0, rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, hInstance, NULL );
- if( !hWnd ) {
- MessageBox( NULL, TEXT("You fucked WndProc or the classname probably?"),
- TEXT("Fail create window"), MB_OK|MB_ICONERROR );
- ExitProcess( 0 );
- }
- initGFX(hWnd);
- ShowWindow( hWnd, nCmdShow );
- #ifdef TRY_TRANSPARANT
- LONG lOld = GetWindowLong( hWnd, GWL_EXSTYLE );
- SetWindowLong( hWnd, GWL_EXSTYLE, lOld | WS_EX_LAYERED );
- SetLayeredWindowAttributes( hWnd, GDI_TRANS_COL, TOTAL_OPACITY, LWA_COLORKEY|LWA_ALPHA );
- #endif
- MSG msg;
- while(TRUE)
- {
- while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- if( msg.message == WM_QUIT)
- break;
- #if USE_DIRECTX || USE_OPENGL
- render_frame( NULL );
- #endif
- }
- cleanGFX();
- DestroyWindow( hWnd );
- return msg.wParam;
- }
Advertisement
Add Comment
Please, Sign In to add comment