bool COpenGL::Initialize(HWND hWnd) { int pfdIndex; RECT windowRect; this->hWnd = hWnd; this->hDC = GetDC(hWnd); PIXELFORMATDESCRIPTOR pfd= { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format 16, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 16, // 16Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; PIXELFORMATDESCRIPTOR pfdSel; if(!(pfdIndex=ChoosePixelFormat(hDC,&pfd))) { DeInitialize(); return false; } if(!SetPixelFormat(hDC,pfdIndex,&pfd)) { DeInitialize(); return false; } if(!(hRC=wglCreateContext(hDC))) { DeInitialize(); return false; } if(!wglMakeCurrent(hDC,hRC)) { DeInitialize(); return false; } LoadPBOFunctions(); wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress( "wglSwapIntervalEXT" ); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); glMatrixMode (GL_PROJECTION); glLoadIdentity (); glOrtho (0.0, 1.0, 0.0, 1.0, -1, 1); glVertexPointer(2, GL_FLOAT, 0, vertices); glTexCoordPointer(2, GL_FLOAT, 0, texcoords); cgAvailable = loadCgFunctions(); if(cgAvailable) { cgContext = cgCreateContext(); cgShader = new CGLCG(cgContext); } ApplyDisplayChanges(); glClearColor(0.0f, 0.0f, 0.0f, 0.5f); glClear(GL_COLOR_BUFFER_BIT); SwapBuffers(hDC); initDone = true; return true; }
/* CDirect3D::Initialize Initializes Direct3D (always in window mode) IN: hWnd - the HWND of the window in which we render/the focus window for fullscreen ----- returns true if successful, false otherwise */ bool CDirect3D::Initialize(HWND hWnd) { if(init_done) return true; pD3D = Direct3DCreate9(D3D_SDK_VERSION); if(pD3D == NULL) { DXTRACE_ERR_MSGBOX(TEXT("Error creating initial D3D9 object"), 0); return false; } memset(&dPresentParams, 0, sizeof(dPresentParams)); dPresentParams.hDeviceWindow = hWnd; dPresentParams.Windowed = true; dPresentParams.BackBufferCount = GUI.DoubleBuffered?2:1; dPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD; dPresentParams.BackBufferFormat = D3DFMT_UNKNOWN; HRESULT hr = pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_MIXED_VERTEXPROCESSING, &dPresentParams, &pDevice); if(FAILED(hr)) { DXTRACE_ERR_MSGBOX(TEXT("Error creating D3D9 device"), hr); return false; } hr = pDevice->CreateVertexBuffer(sizeof(vertexStream),D3DUSAGE_WRITEONLY,0,D3DPOOL_MANAGED,&vertexBuffer,NULL); if(FAILED(hr)) { DXTRACE_ERR_MSGBOX(TEXT("Error creating vertex buffer"), hr); return false; } hr = pDevice->CreateVertexDeclaration(vertexElems,&vertexDeclaration); if(FAILED(hr)) { DXTRACE_ERR_MSGBOX(TEXT("Error creating vertex declaration"), hr); return false; } cgAvailable = loadCgFunctions(); if(cgAvailable) { cgContext = cgCreateContext(); hr = cgD3D9SetDevice(pDevice); if(FAILED(hr)) { DXTRACE_ERR_MSGBOX(TEXT("Error setting cg device"), hr); } cgShader = new CD3DCG(cgContext,pDevice); } pDevice->SetRenderState(D3DRS_LIGHTING, FALSE); pDevice->SetRenderState( D3DRS_ZENABLE, FALSE); pDevice->SetSamplerState(0, D3DSAMP_ADDRESSU, D3DTADDRESS_BORDER); pDevice->SetSamplerState(0, D3DSAMP_ADDRESSV, D3DTADDRESS_BORDER); Clear(); init_done = true; ApplyDisplayChanges(); return true; }