Jeg prøvede det du sagde, og det virkede desværre ikke...
#include <windows.h>
#include <iostream>
#include <sstream>
#include <windowsx.h>
#include <d3d9.h>
// Globals
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define SCREEN_WIDTH 1280
#define SCREEN_HEIGHT 1024
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
int i = 0;
//Funktioner
void initD3D(HWND hwnd);
void render(void);
void clean(void);
void ChangeColor(int color);
LRESULT CALLBACK WindowProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmd, int nShow)
{
WNDCLASSEX wc;
ZeroMemory(&wc,sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpszClassName = "WindowClass";
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInst;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
RegisterClassEx(&wc);
HWND MainWindow = CreateWindow("WindowClass","DirectX",WS_EX_TOPMOST | WS_POPUP,0,0,SCREEN_WIDTH,SCREEN_HEIGHT,NULL,NULL,hInst,0);
MSG msg;
ShowWindow(MainWindow,nShow);
initD3D(MainWindow);
while(TRUE)
{
DWORD renderspeed = GetTickCount();
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(KEY_UP(VK_DOWN))
{
i--;
ChangeColor(i);
}
if(KEY_UP(VK_UP))
{
i++;
ChangeColor(i);
}
render();
if(KEY_DOWN(VK_ESCAPE))
{
PostMessage(MainWindow, WM_DESTROY, 0, 0);
}
while(GetTickCount() - renderspeed < 25);
}
clean();
return msg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc (hWnd, message, wParam, lParam);
}
void initD3D(HWND hwnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3ddev);
return;
}
void render(void)
{
ChangeColor(i);
d3ddev->BeginScene();
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
return;
}
void clean(void)
{
d3ddev->Release();
d3d->Release();
return;
}
void ChangeColor(int color)
{
std::stringstream SS;
std::string color1;
SS << color;
SS >> color1;
if(color < 0)
{
MessageBox(NULL, color1.c_str(), "DirectX", NULL);
i = 0;
color = 0;
}
if(color == 0)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
}
else if(color == 1)
{
MessageBox(NULL, "1", "DirectX", NULL);
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(7, 99, 0), 1.0f, 0);
}
else if(color == 2)
{
MessageBox(NULL, "2", "DirectX", NULL);
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(120, 0, 14), 1.0f, 0);
}
else
{
MessageBox(NULL, color1.c_str(), "DirectX", NULL);
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
}
}
Jeg indsatte nogen MessageBoxe for lidt debugging og den udskriver:
-1
1
1
1
...
Jeg forstår virkelig ikke hvad jeg gør galt, for det virker stadig ikke...