2014-07-25 7 views
0

윈도우 모드 윈도우를 생성하고 표시하는 간단한 DirectX 9 프로그램을 만들려고합니다. 아직 윈도우에 표시되지 않습니다. 프로그램을 컴파일 할 때 오류가 발생하지 않습니다. 프로그램을 실행하면 창이 표시되지 않고 프로그램이 종료됩니다.DirectX 9 윈도우가 표시되지 않습니다.

/* 
* A simple DirectX 9 program to create a windowed mode window with nothing drawn on it. No errors when compiled or ran, but no window * pops up and program terminates almost immediately. 
*/ 

#include <Windows.h> 
#include <d3d9.h> 
#include <time.h> 
#include <iostream> 

using namespace std; 

//program settings 
const string APPTITLE = "Direct3D_Windowed"; 
const int SCREENW = 1024; 
const int SCREENH = 768; 


//Direct3D object 
LPDIRECT3D9 d3d = NULL; 
LPDIRECT3DDEVICE9 d3ddev = NULL; 

bool gameOver = false; 

//macro to detect key presses 
#define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1:0) 

/* 
* Game initialization function 
*/ 
bool Game_Init(HWND window) 
{ 

    //init Direct3D 
    d3d = Direct3DCreate9(D3D_SDK_VERSION); 
    if (d3d == NULL) 
    { 
     return 0; 
    } 

    //set Direct3D presentation parameters 
    D3DPRESENT_PARAMETERS d3dpp; 
    ZeroMemory(&d3dpp, sizeof(d3dpp)); 
    d3dpp.Windowed = TRUE; 
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; 
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; 
    d3dpp.BackBufferCount = 1; 

    d3dpp.BackBufferWidth = SCREENW; 
    d3dpp.BackBufferHeight = SCREENH; 
    d3dpp.hDeviceWindow = window; 

    //create Direct3D device 
    d3d->CreateDevice(
     D3DADAPTER_DEFAULT, 
     D3DDEVTYPE_HAL, 
     window, 
     D3DCREATE_SOFTWARE_VERTEXPROCESSING, 
     &d3dpp, 
     &d3ddev); 

    if (d3ddev == NULL) 
    { 
     return 0; 
    } 

    return true; 
} 

/* 
* Game update function 
*/ 
void Game_Run(HWND hwnd) 
{ 
    //make sure the Direct3D device is valid 
    if (!d3ddev) 
    { 
     return; 
    } 

    //clear the backbuffer to bright green 
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0); 

    //start rendering 
    if (d3ddev->BeginScene()) 
    { 
     //do something? 

     //stop rendering 
     d3ddev->EndScene(); 

     //copy back buffer on the screen 
     d3ddev->Present(NULL, NULL, NULL, NULL); 
    } 

    //check for escape key (to exit program) 
    if (KEY_DOWN(VK_ESCAPE)) 
    { 
     PostMessage(hwnd, WM_DESTROY, 0, 0); 
    } 
} 

/* 
* Game shutdown function 
*/ 
void Game_End(HWND hwnd) 
{ 

    //free the memory 
    if (d3ddev) 
    { 
     d3ddev->Release(); 
    } 
    if (d3d) 
    { 
     d3d->Release(); 
    } 
} 

/* 
* Windows event handling function 
*/ 
LRESULT WINAPI WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch (msg) 
    { 
    case WM_DESTROY: 
     gameOver = true; 
     PostQuitMessage(0); 
     return 0; 
    } 

    return DefWindowProc(hWnd, msg, wParam, lParam); 
} 

/* 
* Main Windows entry function 
*/ 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
    //set the new window's properties 
    WNDCLASSEX wc; 
    wc.style = CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc = (WNDPROC)WinProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance; 
    wc.hIcon = NULL; 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = APPTITLE.c_str(); 
    wc.hIconSm = NULL; 
    RegisterClassEx(&wc); 

    //create a new window 
     HWND window = CreateWindow(
     APPTITLE.c_str(),  //window class 
     APPTITLE.c_str(),  //title bar 
     //fullscreen mode 
     //WS_EX_TOPMOST | WS_VISIBLE | WS_POPUP, 
     //windowed mode 
     WS_OVERLAPPEDWINDOW, //window style 
     CW_USEDEFAULT,   //x position of window 
     CW_USEDEFAULT,   //y position of window 
     640,     //width of the window 
     480,     //height of the window 
     NULL,     //parent window 
     NULL,     //menu 
     hInstance,    //appkication instance 
     NULL);     //window parameters 


    //was there an error creating the window? 
    if (window == 0) 
    { 
     return 0; 
    } 

    //display the window 
    ShowWindow(window, nCmdShow); 
    UpdateWindow(window); 

    //initialize the game 
    if (!Game_Init(window)) 
    { 
     return 0; 
    } 

    //main message loop 
    MSG message; 
    while (!gameOver) 
    { 
     if (PeekMessage(&message, NULL, 0, 0, PM_REMOVE)) 
     { 
      TranslateMessage(&message); 
      DispatchMessage(&message); 
     } 
     Game_Run(window); 
    } 

    Game_End(window); 

    return message.wParam; 
} 
+0

우선, 프로그램 종료 이유를 어떻게 알 수 있습니까? * anything *이 잘못되면 0을 반환합니다. 디버거를 사용할 수도 있지만 잘못된 점을 찾아내는 데는 유용하지 않습니다. 둘째,'RegisterClassEx'가 성공했는지 결코 확인하지 않는다. 셋째, winapi 함수의 와이드 문자열 버전을 사용하십시오. 그 라인을 따라, http://i.imgur.com/IMZe6OU.png – chris

+0

사실, 잠깐, 마지막 하나는 내가 정의를 바꾸지 않고 컴파일하기 위해 물건 끝에 A를 추가 할 때 함수가 없다는 것이었다. 또 다른 코멘트를 쓰고 있지만, 전역 변수는 불필요합니다.'gameOver'은 불필요합니다.'KEY_DOWN'은 함수 여야하고,'WndProc'는 캐스팅되어서는 안됩니다 (컴파일되지 않으면 옳지 않습니다). – chris

+0

솔직히 저는 "Jonathan S. Harbour의 Beginning Game Programming Third Edition"이라는 책을 읽었습니다. 그의 예에서는 "MessageBox (window,"Game_Init ","BREAKPOINT ", 0)," (이 함수는 Game_Init() 함수 안에 있습니다.하지만 오류가 발생하기 때문에이를 제거하거나 주석 처리해야합니다. – Desenski

답변

0

창의 속성을 설정할 때 cbSize 멤버를 설정해야합니다.

wc.cbSize = sizeof(WNDCLASSEX); 
+0

APPTITLE 오류는 유니 코드에서 멀티 바이트로 설정을 변경해야하기 때문에 발생합니다. 나는 당신이 제안한 cbSize를 시도 할 것입니다. – Desenski

+0

특히 TEXT 대신에'L'을 사용하십시오. 특히 데이터로 넓은 문자열을 초기화 할 때 그렇습니다. – chris

+0

그 트릭을 했어! 정말 고맙습니다. – Desenski