SDL2是跨平台的图形库,在windows上使用的是directx的surface来实现的,surface可以用来开发视频播放之类的。
贴代码.
1 #include <windows.h> 2 #include <d3d9.h> 3 4 #pragma comment(lib, "d3d9.lib") 5 6 7 /*常量定义*/ 8 #define WINDOW_WIDTH 640 //窗口尺寸 9 #define WINDOW_HEIGHT 480 10 11 12 /*变量定义*/ 13 HWND m_hwnd; 14 15 IDirect3DDevice9* m_device; //d3d设备 16 IDirect3DSurface9* m_surface; //directx表面 17 D3DSURFACE_DESC m_surfacedesc; //表面描述 18 19 LRESULT CALLBACK sWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 20 { 21 switch (msg) 22 { 23 case WM_PAINT: 24 //draw(); 25 break; 26 case WM_KEYDOWN: 27 switch (wParam) 28 { 29 case VK_ESCAPE: 30 SendMessage(hwnd, WM_CLOSE, 0, 0); 31 break; 32 } 33 break; 34 case WM_DESTROY: 35 36 PostQuitMessage(0); 37 break; 38 } 39 return DefWindowProc(hwnd, msg, wParam, lParam); 40 } 41 42 void draw() 43 { 44 //渲染到离屏表面 45 //取得指向内存区的指针 46 D3DLOCKED_RECT lockedRect; 47 m_surface->LockRect(&lockedRect, 0, 0); 48 49 DWORD* imageData = (DWORD*)lockedRect.pBits; 50 51 for (UINT i = 0; i < m_surfacedesc.Height; i++) 52 { 53 for (UINT j = 0; j < m_surfacedesc.Width; j++) 54 { 55 UINT index = i * lockedRect.Pitch / 4 + j; 56 imageData[index] = 0xffffff00; 57 } 58 } 59 60 m_surface->UnlockRect(); 61 62 63 //渲染 64 m_device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0); 65 m_device->BeginScene(); 66 67 IDirect3DSurface9 * pBackBuffer = NULL; 68 69 m_device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer); 70 m_device->StretchRect(m_surface, NULL, pBackBuffer, NULL, D3DTEXF_LINEAR); 71 72 m_device->EndScene(); 73 m_device->Present(0, 0, 0, 0); 74 75 pBackBuffer->Release(); 76 } 77 78 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) 79 { 80 WNDCLASSEX winClass; 81 82 winClass.lpszClassName = "DirectSurface"; 83 winClass.cbSize = sizeof(WNDCLASSEX); 84 winClass.style = CS_HREDRAW | CS_VREDRAW; 85 winClass.lpfnWndProc = sWndProc; 86 winClass.hInstance = hInstance; 87 winClass.hIcon = NULL; 88 winClass.hIconSm = NULL; 89 winClass.hCursor = LoadCursor(NULL, IDC_ARROW); 90 winClass.hbrBackground = NULL; 91 winClass.lpszMenuName = NULL; 92 winClass.cbClsExtra = 0; 93 winClass.cbWndExtra = 0; 94 95 if (!RegisterClassEx(&winClass)) 96 { 97 MessageBox(NULL, TEXT("This program requires Windows NT!"), "error", MB_ICONERROR); 98 return 1; 99 } 100 101 m_hwnd = CreateWindowEx(NULL, 102 "DirectSurface", // window class name 103 "Draw Surface", // window caption 104 WS_OVERLAPPEDWINDOW, // window style 105 CW_USEDEFAULT, // initial x position 106 CW_USEDEFAULT, // initial y position 107 WINDOW_WIDTH, // initial x size 108 WINDOW_HEIGHT, // initial y size 109 NULL, // parent window handle 110 NULL, // window menu handle 111 hInstance, // program instance handle 112 NULL); // creation parameters 113 114 //初始化directx 115 D3DDEVTYPE deviceType = D3DDEVTYPE_HAL; 116 117 HRESULT hr = 0; 118 119 // Step 1: Create the IDirect3D9 object. 120 121 IDirect3D9* d3d9 = 0; 122 d3d9 = Direct3DCreate9(D3D_SDK_VERSION); 123 124 if (!d3d9) 125 { 126 ::MessageBox(0, "Direct3DCreate9() - FAILED", 0, 0); 127 return 0; 128 } 129 130 // Step 2: Check for hardware vp. 131 132 D3DCAPS9 caps; 133 d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps); 134 135 int vp = 0; 136 if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) 137 vp = D3DCREATE_HARDWARE_VERTEXPROCESSING; 138 else 139 vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING; 140 141 // Step 3: Fill out the D3DPRESENT_PARAMETERS structure. 142 143 D3DPRESENT_PARAMETERS d3dpp; 144 d3dpp.BackBufferWidth = WINDOW_WIDTH; 145 d3dpp.BackBufferHeight = WINDOW_HEIGHT; 146 d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8; 147 d3dpp.BackBufferCount = 1; 148 d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; 149 d3dpp.MultiSampleQuality = 0; 150 d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 151 d3dpp.hDeviceWindow = m_hwnd; 152 d3dpp.Windowed = true; 153 d3dpp.EnableAutoDepthStencil = true; 154 d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8; 155 d3dpp.Flags = 0; 156 d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; 157 d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE; 158 159 // Step 4: Create the device. 160 161 hr = d3d9->CreateDevice( 162 D3DADAPTER_DEFAULT, // primary adapter 163 deviceType, // device type 164 m_hwnd, // window associated with device 165 vp, // vertex processing 166 &d3dpp, // present parameters 167 &m_device); // return created device 168 169 if (FAILED(hr)) 170 { 171 // try again using a 16-bit depth buffer 172 d3dpp.AutoDepthStencilFormat = D3DFMT_D16; 173 174 hr = d3d9->CreateDevice( 175 D3DADAPTER_DEFAULT, 176 deviceType, 177 m_hwnd, 178 vp, 179 &d3dpp, 180 &m_device); 181 182 if (FAILED(hr)) 183 { 184 d3d9->Release(); // done with d3d9 object 185 ::MessageBox(0, "CreateDevice() - FAILED", 0, 0); 186 return 0; 187 } 188 } 189 190 d3d9->Release(); // done with d3d9 object 191 192 m_device->CreateOffscreenPlainSurface(WINDOW_WIDTH, WINDOW_HEIGHT, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &m_surface, NULL); 193 m_surface->GetDesc(&m_surfacedesc); 194 195 196 ShowWindow(m_hwnd, SW_SHOW); 197 UpdateWindow(m_hwnd); 198 199 MSG msg; 200 ZeroMemory(&msg, sizeof(msg)); 201 202 while (TRUE) 203 { 204 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 205 { 206 207 if (msg.message == WM_QUIT) 208 break; 209 210 // translate any accelerator keys 211 TranslateMessage(&msg); 212 213 // send the message to the window proc 214 DispatchMessage(&msg); 215 } // end while 216 217 draw(); 218 } 219 220 //释放资源 221 m_device->Release(); 222 if (m_surface) 223 m_surface->Release(); 224 225 return 0; 226 }
时间: 2024-11-10 15:24:06