コード:
#include <windows.h>
#include <stdio.h>
#include <d3dx9.h>
#include <shlobj.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#define APPNAME TEXT("3Dmodel")
#define MENUID TEXT("MENUBAR")
#include "../../directoryname/lib/libmain.h"
#include "dinput.h"
//#include "../../directoryname/lib/graphic.h"
//#include "../../directoryname/lib/input.h"
#include "../../directoryname/lib/string.h"
///////// メイン変数宣言
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
HRESULT InitD3D(HWND,bool,int,int);
BOOL InitDirectInput(HWND);
void Inputkeyrerese();
HWND hWnd=NULL;
HWND hDeskwnd;
MSG msg;
HDC hdc;
LPPAINTSTRUCT lpaint;
//int flag;
HINSTANCE hInst;
WNDCLASSEX wc;
DWORD dwID;
LPDIRECT3D9 pD3d;
LPDIRECT3DDEVICE9 pDevice;
D3DPRESENT_PARAMETERS d3dpp;
LPDIRECTINPUT8 g_pDInput;
LPDIRECTINPUT8 g_pDMInput;
LPDIRECTINPUT8 g_pDGInput;
LPDIRECTINPUTDEVICE8 g_pDIDevice;
LPDIRECTINPUTDEVICE8 g_pDIMouse;
LPDIRECTINPUTDEVICE8 g_pDIGamepad;
BOOL CALLBACK EJCB(const DIDEVICEINSTANCE*,VOID*);
BOOL CALLBACK EACB(const DIDEVICEOBJECTINSTANCE*,VOID*);
DIDEVCAPS g_diDev;
int dcflag[3];
CRITICAL_SECTION cs;
DWORD WINAPI Rendsled(void);
////////////////////////////////////
class CustomVertex;
class Pos;
class Color;
//class Vertex;
class World;
class View;
class Proj;
//class RendTex;
class String;
//class Mouseinput;
//class Keyinuput;
//class Gamepadinout;
//extern Vertex modelmain[10000];
extern World mainworld;
extern View view2D;
extern Proj proja;
extern RendTex modelmonitex;
extern CustomVertex modelmoni[4];
extern VerBuff monivb;
extern String view3Dsta;
extern String Testst;
Keyinput Key;
Mouseinput Mouse;
Gamepadinput Pad;
void Render();
extern void Vertexset();
extern void Vertexreset();
extern void SetupMatrices();
void *pVertex;
extern void makemodelmoni();
extern void modelmonireset();
extern void modelmonidraw();
extern void MakestageView();
extern void SetstageView();
extern void teststatus();
Renderc rend;
RendTex rent;
Projection projection;
RECT rect;
RECT rectc;
RECT brect;
int WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR szStr,INT iCmdShow){
wc.cbSize =sizeof (wc);
wc.style =CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
wc.lpfnWndProc =WndProc;
wc.cbClsExtra =0;
wc.cbWndExtra =0;
wc.hInstance =hInst;
wc.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor =LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName =MENUID;
wc.lpszClassName=APPNAME;
wc.hIconSm =LoadIcon(NULL,IDI_ASTERISK);
RegisterClassEx (&wc);
hWnd=CreateWindow(
APPNAME,
APPNAME,
WS_OVERLAPPEDWINDOW|WS_MAXIMIZE,
CW_USEDEFAULT,
CW_USEDEFAULT,
1382,
744,
NULL,
NULL,
hInst,
NULL);
/*manebutton(hWnd,hInst);*/
ShowWindow(hWnd,SW_SHOW);
UpdateWindow(hWnd);
GetWindowRect( hWnd, &rect );
GetClientRect( hWnd, &rectc );
if(FAILED(InitD3D(hWnd,FALSE,rect.right-rect.left,rect.bottom-rect.top)))
{
MessageBox(0,TEXT("DirectXの初期化に失敗しました。"),NULL,MB_OK);
return 0;
}
if(FAILED(InitDirectInput(hWnd))){
MessageBox(0,TEXT("DirectXの初期化に失敗しました。"),NULL,MB_OK);
return 0;
}
ZeroMemory(&msg,sizeof(msg));
//hDeskwnd = GetDesktopWindow();
brect=rect;
Vertexset();
SetupMatrices();
makemodelmoni();
Testst.Makefont(14 ,7 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,TEXT("Times New Roman"));
view3Dsta.Makefont(14 ,7 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,TEXT("Times New Roman"));
MakestageView();
//CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Rendsled,NULL,0,&dwID);
//InitializeCriticalSection(&cs);
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg,NULL,0U,0U,PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
GetWindowRect( hWnd, &rect );
GetClientRect( hWnd, &rectc );
if(brect.bottom!=rectc.bottom ||brect.left!=rectc.left ||brect.right!=rectc.right ||brect.top!=rectc.top){
brect=rectc;
Vertexreset();
}
SetupMatrices();
modelmonireset();
Render();
dcflag[0]=0;
dcflag[1]=0;
dcflag[2]=0;
}
}
/*modelmoni.Texrelease();
modelmain[0].VbRelease();
modelmain[0].TexRelease();*/
Inputkeyrerese();
pDevice->Release();
pD3d->Release();
return (INT)msg.wParam;
}
/*DWORD WINAPI Rendsled(void){
EnterCriticalSection(&cs);
MSG msg={0};
int flag;
while(1){
Sleep(0);
Render();
}
LeaveCriticalSection(&cs);
return 0;
}*/
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
//DeleteCriticalSection(&cs);
PostQuitMessage(0);
return 0;
case WM_LBUTTONDBLCLK:
dcflag[0]=1;
return 0;
case WM_RBUTTONDBLCLK:
dcflag[1]=1;
return 0;
case WM_MBUTTONDBLCLK:
dcflag[2]=1;
return 0;
case WM_PAINT:
hdc=BeginPaint(hWnd,lpaint);
if(pDevice!=0x00000000){
Render();
}
EndPaint(hWnd,lpaint);
return 0;
}
return DefWindowProc (hWnd, msg, wParam, lParam);
}
HRESULT InitD3D(HWND hWnd,bool fullScreen,int Hieght,int Width)
{
D3DDISPLAYMODE d3dMode;
if(NULL==(pD3d=Direct3DCreate9(D3D_SDK_VERSION)))
{
MessageBox(0,TEXT("Direct3Dの作成に失敗しました。"),NULL,MB_OK);
return E_FAIL;
}
if(FAILED(pD3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3dMode))){
return E_FAIL;
}
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.BackBufferCount =1;
if(fullScreen){
d3dpp.Windowed=FALSE;
d3dpp.BackBufferHeight=Hieght;
d3dpp.BackBufferWidth=Width;
}else{
d3dpp.Windowed=TRUE;
}
d3dpp.BackBufferFormat =D3DFMT_UNKNOWN;
d3dpp.SwapEffect =D3DSWAPEFFECT_DISCARD;
d3dpp.EnableAutoDepthStencil=TRUE;
d3dpp.AutoDepthStencilFormat=D3DFMT_D16;
if(FAILED(pD3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,&pDevice)))
{
MessageBox(0,TEXT("HALモードでDIRECT3Dデバイスを作成できません\nREFモードで再試行します。"),NULL, MB_OK);
if( FAILED( pD3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &pDevice ) ) )
{
if( FAILED( pD3d->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &pDevice ) ) )
{
MessageBox(0,TEXT("DIRECT3Dデバイスの作成に失敗しました。"),NULL,MB_OK);
pD3d->Release();
return E_FAIL;
}
}
}
pDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
pDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
return S_OK;
}
void Render(){
if(FAILED(pDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_ARGB(0,0,0,0),1.0f,0))){
return;
}
if(SUCCEEDED(pDevice->BeginScene())){
SetupMatrices();
modelmonidraw();
SetupMatrices();
rend.Rend(0,0,sizeof(CustomVertex),D3DFVF_CustomVertex,D3DPT_TRIANGLEFAN,0,2,modelmonitex.text.tex,monivb.vb);
/*pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
pDevice->SetTexture(0,modelmonitex.text.tex);
pDevice->SetStreamSource(0,monivb,0,sizeof(CustomVertex));
pDevice->SetFVF(D3DFVF_CustomVertex);
pDevice->DrawPrimitive(D3DPT_TRIANGLEFAN,0,2);*/
teststatus();
pDevice->EndScene();
}
//if(FAILED(pDevice->Present(NULL,NULL,hWnd,NULL))){
if(pDevice->Present(NULL,NULL,NULL,NULL)== D3DERR_DEVICELOST){
if(pDevice->TestCooperativeLevel()==D3DERR_DEVICENOTRESET){
pDevice->Reset(&d3dpp);
GetWindowRect( hWnd, &rect );
GetClientRect( hWnd, &rectc );
if(FAILED(InitD3D(hWnd,FALSE,rect.right-rect.left,rect.bottom-rect.top)))
{
MessageBox(0,TEXT("DirectXの初期化に失敗しました。"),NULL,MB_OK);
return;
}
ZeroMemory(&msg,sizeof(msg));
//hDeskwnd = GetDesktopWindow();
brect=rect;
Vertexset();
SetupMatrices();
makemodelmoni();
Testst.Makefont(14 ,7 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,TEXT("Times New Roman"));
view3Dsta.Makefont(14 ,7 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,TEXT("Times New Roman"));
MakestageView();
}
else
{
return;
}
}
//}
}
BOOL InitDirectInput(HWND hWnd)
{
HINSTANCE hInst;
HRESULT hr;
hInst = (HINSTANCE)GetWindowLong( hWnd , GWL_HINSTANCE );
hr = DirectInput8Create( hInst , DIRECTINPUT_VERSION , IID_IDirectInput8 ,
(void**)&g_pDInput , NULL );
if ( FAILED( hr ) ){
MessageBox( hWnd , TEXT("Can't create DirectInput object.") , TEXT("Error") , MB_OK );
return FALSE;
}
hr = g_pDInput->CreateDevice( GUID_SysKeyboard , &g_pDIDevice , NULL );
if ( FAILED( hr ) ){
Inputkeyrerese();
MessageBox( hWnd , TEXT("Can't create DirectInput device.") , TEXT("Error") , MB_OK );
return FALSE;
}
if(g_pDIDevice!=0x00000000){
hr = g_pDIDevice->SetDataFormat( &c_dfDIKeyboard );
if ( FAILED( hr ) ){
Inputkeyrerese();
MessageBox( hWnd , TEXT("Can't set data format.") , TEXT("Error"), MB_OK );
return FALSE;
}
Key.Makedevice(DISCL_FOREGROUND /*| DISCL_NONEXCLUSIVE*/);
}
HRESULT mhr;
mhr = DirectInput8Create( hInst , DIRECTINPUT_VERSION , IID_IDirectInput8 ,
(void**)&g_pDMInput , NULL );
if ( FAILED( mhr ) ){
MessageBox( hWnd , TEXT("Can't create DirectInput object.") , TEXT("Error") , MB_OK );
return FALSE;
}
mhr = g_pDMInput->CreateDevice( GUID_SysMouse , &g_pDIMouse , NULL );
if ( FAILED( mhr ) ){
Inputkeyrerese();
MessageBox( hWnd , TEXT("Can't create DirectInput device.") , TEXT("Error") , MB_OK );
return FALSE;
}
if(g_pDIMouse!=0x00000000){
mhr = g_pDIMouse->SetDataFormat( &c_dfDIMouse );
if ( FAILED( mhr ) ){
Inputkeyrerese();
MessageBox( hWnd , TEXT("Can't set data format.") , TEXT("Error"), MB_OK );
return FALSE;
}
/*g_pDIMouse->SetDataFormat( &c_dfDIMouse );
g_pDIMouse->SetCooperativeLevel(hWnd,DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
Testmouse.diprop.diph.dwSize=sizeof(Testmouse.diprop);
Testmouse.diprop.diph.dwHeaderSize=sizeof(Testmouse.diprop.diph);
Testmouse.diprop.diph.dwObj=0;
Testmouse.diprop.diph.dwHow=DIPH_DEVICE;
Testmouse.diprop.dwData=DIPROPAXISMODE_REL;
g_pDIMouse->SetProperty(DIPROP_AXISMODE, &Testmouse.diprop.diph);
g_pDIMouse->Acquire();*/
Mouse.Makedevice(DISCL_FOREGROUND/* | DISCL_NONEXCLUSIVE*/,sizeof(Mouse.diprop),sizeof(Mouse.diprop.diph),0,DIPH_DEVICE,DIPROPAXISMODE_REL,0);
}
/*Testmouse.AcqMouse(DIPROP_AXISMODE);*/
HINSTANCE hInstg;
HRESULT ghr;
hInstg =(HINSTANCE)GetWindowLong(hWnd,GWL_HINSTANCE);
ghr = DirectInput8Create( hInst , DIRECTINPUT_VERSION , IID_IDirectInput8 ,
(void**)&g_pDGInput , NULL );
if ( FAILED( ghr ) ){
MessageBox( hWnd , TEXT("Can't create DirectInput object.") , TEXT("Error") , MB_OK );
return FALSE;
}
ghr = g_pDGInput->EnumDevices(DI8DEVCLASS_GAMECTRL,EJCB,NULL,DIEDFL_ATTACHEDONLY);
if ( FAILED( ghr ) ){
MessageBox( hWnd , TEXT("Can't create Device.") , TEXT("Error") , MB_OK );
return FALSE;
}
if(g_pDIGamepad!=0x00000000){
ghr = g_pDIGamepad->SetDataFormat( &c_dfDIJoystick );
if ( FAILED( ghr ) ){
Inputkeyrerese();
MessageBox( hWnd , TEXT("Can't set data format.") , TEXT("Error"), MB_OK );
return FALSE;
}
Pad.Makedevice(DISCL_FOREGROUND);
g_diDev.dwSize=sizeof(DIDEVCAPS);
ghr =g_pDIGamepad->GetCapabilities(&g_diDev);
if ( FAILED( ghr ) ){
Inputkeyrerese();
MessageBox( hWnd , TEXT("Can't create device capabilities.") , TEXT("Error"), MB_OK );
return FALSE;
}
ghr =g_pDIGamepad->EnumObjects(EACB,(void*)hWnd,DIDFT_AXIS);
if ( FAILED( ghr ) ){
Inputkeyrerese();
MessageBox( hWnd , TEXT("Can't set data property.") , TEXT("Error"), MB_OK );
return FALSE;
}
ghr = g_pDIGamepad->Poll();
if ( FAILED( ghr ) ){
ghr=g_pDIGamepad->Acquire();
while(ghr==DIERR_INPUTLOST){
ghr=g_pDIGamepad->Acquire();
}
}
}
return TRUE;
}
void Inputkeyrerese(){
g_pDIDevice->Unacquire();
if ( g_pDIDevice != NULL ){
g_pDIDevice->Release();}
g_pDIMouse->Unacquire();
if ( g_pDIMouse != NULL ){
g_pDIMouse->Release();}
if ( g_pDInput != NULL ){
g_pDInput->Release();
}
if ( g_pDMInput != NULL ){
g_pDMInput->Release();
}
};
BOOL CALLBACK EJCB(const DIDEVICEINSTANCE *pdidInstance,VOID *pContext){
HRESULT hr;
hr=g_pDGInput->CreateDevice( pdidInstance->guidInstance,&g_pDIGamepad,NULL);
if(FAILED(hr)) return DIENUM_CONTINUE;
return DIENUM_STOP;
}
BOOL CALLBACK EACB(const DIDEVICEOBJECTINSTANCE *pdidoi, VOID *pContext){
HRESULT hr;
DIPROPRANGE diprg;
diprg.diph.dwSize =sizeof(DIPROPRANGE);
diprg.diph.dwHeaderSize =sizeof(DIPROPHEADER);
diprg.diph.dwHow =DIPH_BYID;
diprg.diph.dwObj =pdidoi->dwType;
diprg.lMin =0-1000;
diprg.lMax =0+1000;
hr = g_pDIDevice->SetProperty(DIPROP_RANGE,&diprg.diph);
if(FAILED(hr)) return DIENUM_STOP;
return DIENUM_CONTINUE;
}