#include "core_d3d9.h" #include #pragma comment(lib,"kernel32") #pragma comment(lib,"user32") #pragma comment(lib,"d3d9") #define IDI_MAINICON 101 static WNDCLASS wincl; static int wincount=0; LRESULT APIENTRY WindowProcD3D9(HWND hwnd,UINT msg,WPARAM wp,LPARAM lp) { CoreD3D9 *thiscore=(CoreD3D9 *)GetWindowLong(hwnd,GWL_USERDATA); if ((thiscore==NULL)&&(msg!=WM_CREATE)) return DefWindowProc(hwnd,msg,wp,lp); RAVEN_KEY_CODE ratkey; RAVEN_SYS_KEY vkey; int btnnum=0; switch (msg) { case WM_CREATE: { HINSTANCE instance=(HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE); thiscore=(CoreD3D9 *)(((LPCREATESTRUCT)lp)->lpCreateParams); SetWindowLong(hwnd,GWL_USERDATA,(LONG)thiscore); thiscore->hwnd=hwnd; ShowWindow(hwnd,SW_SHOW); return 0; } break; case WM_QUIT: case WM_CLOSE: thiscore->isappterm=true; return 0; case WM_SIZE: // replace this with a D3D9 framebuffer resize // until then this does nothing /*thiscore->height=HIWORD(lp); thiscore->width=LOWORD(lp); thiscore->isresized=true; ReleaseDC(thiscore->hwnd,thiscore->device); thiscore->device=GetDC(thiscore->hwnd);*/ return 0; case WM_ERASEBKGND: case WM_ACTIVATEAPP: return 0; case WM_KEYDOWN: vkey=wp; ratkey=RAVEN_key_map_get_code(thiscore->keymap,vkey); if (ratkey) thiscore->keys[ratkey]=true; else thiscore->keys[vkey]=true; return 0; case WM_KEYUP: vkey=wp; ratkey=RAVEN_key_map_get_code(thiscore->keymap,vkey); if (ratkey) thiscore->keys[ratkey]=false; else thiscore->keys[vkey]=false; return 0; case WM_RBUTTONDOWN: btnnum++; /* Right mouse button pressed */ case WM_MBUTTONDOWN: btnnum++; /* Middle mouse button pressed */ case WM_LBUTTONDOWN: btnnum++; /* Left mouse button pressed */ thiscore->buttons[btnnum]=true; return 0; case WM_RBUTTONUP: btnnum++; /* Right mouse button released */ case WM_MBUTTONUP: btnnum++; /* Middle mouse button released */ case WM_LBUTTONUP: btnnum++; /* Left mouse button released */ thiscore->buttons[btnnum]=false; return 0; case WM_MOUSEMOVE: thiscore->mousex=(double)LOWORD(lp)/(double)thiscore->width; thiscore->mousey=(double)HIWORD(lp)/(double)thiscore->height; return 0; case WM_SYSCOMMAND: /* System call made. */ if (wp==SC_SCREENSAVE||wp==SC_MONITORPOWER) return 0; break; default: break; } return DefWindowProc(hwnd,msg,wp,lp); } CoreD3D9::CoreD3D9(char *title,unsigned int width,unsigned int height,int colorsize,int depthsize,int flags): Core(title,width,height,colorsize,depthsize,flags), dev(NULL), d3d(NULL) { instance=GetModuleHandle(NULL); char path[5000]; if (GetModuleFileName(NULL,path,sizeof(path))) { char *slash=strrchr(path,'\\'); if (slash) *slash='\0'; _chdir(path); } } CoreD3D9::~CoreD3D9() {} bool CoreD3D9::Start() { if (!keymap) { cerr<<"Keymap failed to initialize!"<GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps); memset(&d3dpp,0,sizeof(d3dpp)); d3dpp.BackBufferFormat=colorsize>16?D3DFMT_X8R8G8B8:D3DFMT_R5G6B5; d3dpp.Windowed=!(flags&RAVEN_FULLSCREEN); if (flags&RAVEN_FULLSCREEN) { bool modefound=false; unsigned int nummodes=d3d->GetAdapterModeCount(D3DADAPTER_DEFAULT,d3dpp.BackBufferFormat); for (register unsigned int i=0; iEnumAdapterModes(D3DADAPTER_DEFAULT,d3dpp.BackBufferFormat,i,&mode); if (mode.Width>=640&&mode.Height>=480) { if (mode.Width==width&&mode.Height==height) { dispmode=mode; modefound=true; } } } if (!modefound) { flags&=~RAVEN_FULLSCREEN; // unflag fullscreen, it didn't work out cout<<"Failed to go to fullscreen! Switching to windowed."<0); d3dpp.AutoDepthStencilFormat=(depthsize>16)?D3DFMT_D32:D3DFMT_D16; if (flags&RAVEN_FULLSCREEN) { RECT winrect; winrect.left=0L; winrect.right=(long)width; winrect.top=0L; winrect.bottom=(long)height; AdjustWindowRect(&winrect,style,FALSE); hwnd=CreateWindow("RAVEN wc D3D9",title,style|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, winrect.left,winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top, NULL,NULL,instance,(void*)this); } else { RECT winrect; winrect.left=(long)(screenwidth/2-width/2); winrect.right=winrect.left+(long)width; winrect.top=(long)(screenheight/2-height/2); winrect.bottom=winrect.top+(long)height; AdjustWindowRect(&winrect,style,FALSE); hwnd=CreateWindow("RAVEN wc D3D9",title,style|WS_CLIPCHILDREN|WS_CLIPSIBLINGS, winrect.left,winrect.top,winrect.right-winrect.left,winrect.bottom-winrect.top, NULL,NULL,instance,(void*)this); } if (!hwnd) { cerr<<"Failed to create window!"<CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL, hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&dev)!=D3D_OK) { cerr<<"Failed to create Direct3D device!"<Release(); dev=NULL; } if (d3d) { d3d->Release(); d3d=NULL; } SetWindowLong(hwnd,GWL_USERDATA,(LONG)NULL); if (!DestroyWindow(hwnd)) { cerr<<"Failed to destroy window!"<Present(NULL,NULL,NULL,NULL); return true; }