/* LICENSE ------- Copyright (C) 1999-2002 Nullsoft, Inc. This source code is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this source code or the software it produces. Permission is granted to anyone to use this source code for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this source code must not be misrepresented; you must not claim that you wrote the original source code. If you use this source code in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original source code. 3. This notice may not be removed or altered from any source distribution. */ #include "support.h" #include "utility.h" #include #include #include //--------------------------------------------------- void PrepareFor3DDrawing( IDirect3DDevice8 *pDevice, int viewport_width, int viewport_height, float fov_in_degrees, float near_clip, float far_clip, D3DXVECTOR3* pvEye, D3DXVECTOR3* pvLookat, D3DXVECTOR3* pvUp ) { // This function sets up DirectX up for 3D rendering. // Only call it once per frame, as it is VERY slow. // INPUTS: // pDevice a pointer to the D3D device // viewport_width the width of the client area of the window // viewport_height the height of the client area of the window // fov_in_degrees the field of view, in degrees // near_clip the distance to the near clip plane; should be > 0! // far_clip the distance to the far clip plane // eye the eyepoint coordinates, in world space // lookat the point toward which the eye is looking, in world space // up a vector indicating which dir. is up; usually <0,1,0> // // What this function does NOT do: // 1. set the current texture (SetTexture) // 2. set up the texture stages for texturing (SetTextureStageState) // 3. set the current vertex format (SetVertexShader) // 4. set up the world matrix (SetTransform(D3DTS_WORLD, &my_world_matrix)) // set up render state to some nice defaults: { // some defaults pDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); pDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE ); pDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_LESSEQUAL ); pDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); pDevice->SetRenderState( D3DRS_CLIPPING, TRUE ); pDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); pDevice->SetRenderState( D3DRS_COLORVERTEX, TRUE ); pDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_GOURAUD ); pDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID ); pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE ); // turn fog off pDevice->SetRenderState( D3DRS_FOGENABLE, FALSE ); pDevice->SetRenderState( D3DRS_RANGEFOGENABLE, FALSE ); // turn on high-quality bilinear interpolations pDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); pDevice->SetTextureStageState(1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR); pDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR); pDevice->SetTextureStageState(1, D3DTSS_MINFILTER, D3DTEXF_LINEAR); pDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR); pDevice->SetTextureStageState(1, D3DTSS_MIPFILTER, D3DTEXF_LINEAR); } // set up view & projection matrices (but not the world matrix!) { // if the window is not square, instead of distorting the scene, // clip it so that the longer dimension of the window has the // regular FOV, and the shorter dimension has a reduced FOV. float fov_x = fov_in_degrees * 3.1415927f/180.0f; float fov_y = fov_in_degrees * 3.1415927f/180.0f; float aspect = (float)viewport_height / (float)viewport_width; if (aspect < 1) fov_y *= aspect; else fov_x /= aspect; if (near_clip < 0.1f) near_clip = 0.1f; if (far_clip < near_clip + 1.0f) far_clip = near_clip + 1.0f; D3DXMATRIX proj; MakeProjectionMatrix(&proj, near_clip, far_clip, fov_x, fov_y); pDevice->SetTransform(D3DTS_PROJECTION, &proj); D3DXMATRIX view; D3DXMatrixLookAtLH(&view, pvEye, pvLookat, pvUp); pDevice->SetTransform(D3DTS_VIEW, &view); // Optimization note: "You can minimize the number of required calculations // by concatenating your world and view matrices into a world-view matrix // that you set as the world matrix, and then setting the view matrix // to the identity." //D3DXMatrixMultiply(&world, &world, &view); //D3DXMatrixIdentity(&view); } } void PrepareFor2DDrawing(IDirect3DDevice8 *pDevice) { // New 2D drawing area will have x,y coords in the range <-1,-1> .. <1,1> // +--------+ Y=-1 // | | // | screen | Z=0: front of scene // | | Z=1: back of scene // +--------+ Y=1 // X=-1 X=1 // NOTE: After calling this, be sure to then call (at least): // 1. SetVertexShader() // 2. SetTexture(), if you need it // before rendering primitives! // Also, be sure your sprites have a z coordinate of 0. pDevice->SetRenderState( D3DRS_ZENABLE, TRUE ); pDevice->SetRenderState( D3DRS_ZWRITEENABLE, TRUE ); pDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_LESSEQUAL ); pDevice->SetRenderState( D3DRS_SHADEMODE, D3DSHADE_FLAT ); pDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID ); pDevice->SetRenderState( D3DRS_FOGENABLE, FALSE ); pDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); pDevice->SetRenderState( D3DRS_CLIPPING, TRUE ); pDevice->SetRenderState( D3DRS_LIGHTING, FALSE ); pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE ); pDevice->SetRenderState( D3DRS_LOCALVIEWER, FALSE ); pDevice->SetTexture(0, NULL); pDevice->SetTexture(1, NULL); pDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_POINT);//D3DTEXF_LINEAR); pDevice->SetTextureStageState(1, D3DTSS_MAGFILTER, D3DTEXF_POINT);//D3DTEXF_LINEAR); pDevice->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); pDevice->SetTextureStageState(1, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE); pDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE ); pDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); pDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_CURRENT ); pDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE ); pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 ); pDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE ); pDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE ); pDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE ); // set up for 2D drawing: { D3DXMATRIX Ortho2D; D3DXMATRIX Identity; D3DXMatrixOrthoLH(&Ortho2D, 2.0f, -2.0f, 0.0f, 1.0f); D3DXMatrixIdentity(&Identity); pDevice->SetTransform(D3DTS_PROJECTION, &Ortho2D); pDevice->SetTransform(D3DTS_WORLD, &Identity); pDevice->SetTransform(D3DTS_VIEW, &Identity); } } //--------------------------------------------------- void MakeWorldMatrix( D3DXMATRIX* pOut, float xpos, float ypos, float zpos, float sx, float sy, float sz, float pitch, float yaw, float roll) { /* * The m_xPos, m_yPos, m_zPos variables contain the model's * location in world coordinates. * The m_fPitch, m_fYaw, and m_fRoll variables are floats that * contain the model's orientation in terms of pitch, yaw, and roll * angles, in radians. */ D3DXMATRIX MatTemp; D3DXMatrixIdentity(pOut); // 1. first, rotation if (pitch || yaw || roll) { D3DXMATRIX MatRot; D3DXMatrixIdentity(&MatRot); D3DXMatrixRotationX(&MatTemp, pitch); // Pitch D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); D3DXMatrixRotationY(&MatTemp, yaw); // Yaw D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); D3DXMatrixRotationZ(&MatTemp, roll); // Roll D3DXMatrixMultiply(&MatRot, &MatRot, &MatTemp); D3DXMatrixMultiply(pOut, pOut, &MatRot); } // 2. then, scaling D3DXMatrixScaling(&MatTemp, sx, sy, sz); D3DXMatrixMultiply(pOut, pOut, &MatTemp); // 3. last, translation to final world pos. D3DXMatrixTranslation(&MatTemp, xpos, ypos, zpos); D3DXMatrixMultiply(pOut, pOut, &MatTemp); } void MakeProjectionMatrix( D3DXMATRIX* pOut, const float near_plane, // Distance to near clipping plane const float far_plane, // Distance to far clipping plane const float fov_horiz, // Horizontal field of view angle, in radians const float fov_vert) // Vertical field of view angle, in radians { float w = (float)1/tanf(fov_horiz*0.5f); // 1/tan(x) == cot(x) float h = (float)1/tanf(fov_vert*0.5f); // 1/tan(x) == cot(x) float Q = far_plane/(far_plane - near_plane); ZeroMemory(pOut, sizeof(D3DXMATRIX)); pOut->_11 = w; pOut->_22 = h; pOut->_33 = Q; pOut->_43 = -Q*near_plane; pOut->_34 = 1; } //--------------------------------------------------- void GetWinampSongTitle(HWND hWndWinamp, char *szSongTitle, int nSize) { szSongTitle[0] = 0; if (::GetWindowText(hWndWinamp, szSongTitle, nSize)) { // remove ' - Winamp' if found at end if (strlen(szSongTitle) > 9) { int check_pos = strlen(szSongTitle) - 9; if (lstrcmp(" - Winamp", (char *)(szSongTitle + check_pos)) == 0) szSongTitle[check_pos] = 0; } // remove ' - Winamp [Paused]' if found at end if (strlen(szSongTitle) > 18) { int check_pos = strlen(szSongTitle) - 18; if (lstrcmp(" - Winamp [Paused]", (char *)(szSongTitle + check_pos)) == 0) szSongTitle[check_pos] = 0; } // remove song # and period from beginning char *p = szSongTitle; while (*p >= '0' && *p <= '9') p++; if (*p == '.' && *(p+1) == ' ') { p += 2; int pos = 0; while (*p != 0) { szSongTitle[pos++] = *p; p++; } szSongTitle[pos++] = 0; } // fix &'s for display // note: this is not necessary if you use the DT_NOPREFIX flag with text drawing functions! /* { int pos = 0; int len = strlen(szSongTitle); while (szSongTitle[pos]) { if (szSongTitle[pos] == '&') { for (int x=len; x>=pos; x--) szSongTitle[x+1] = szSongTitle[x]; len++; pos++; } pos++; } }*/ } } void GetWinampSongPosAsText(HWND hWndWinamp, char *szSongPos) { // note: size(szSongPos[]) must be at least 64. szSongPos[0] = 0; int nSongPosMS = SendMessage(hWndWinamp,WM_USER,0,105); if (nSongPosMS > 0) { float time_s = nSongPosMS*0.001f; int minutes = (int)(time_s/60); time_s -= minutes*60; int seconds = (int)time_s; time_s -= seconds; int dsec = (int)(time_s*100); sprintf(szSongPos, "%d:%02d.%02d", minutes, seconds, dsec); } } void GetWinampSongLenAsText(HWND hWndWinamp, char *szSongLen) { // note: size(szSongLen[]) must be at least 64. szSongLen[0] = 0; int nSongLenMS = SendMessage(hWndWinamp,WM_USER,1,105)*1000; if (nSongLenMS > 0) { int len_s = nSongLenMS/1000; int minutes = len_s/60; int seconds = len_s - minutes*60; sprintf(szSongLen, "%d:%02d", minutes, seconds); } } float GetWinampSongPos(HWND hWndWinamp) { // returns answer in seconds return (float)SendMessage(hWndWinamp,WM_USER,0,105)*0.001f; } float GetWinampSongLen(HWND hWndWinamp) { // returns answer in seconds return (float)SendMessage(hWndWinamp,WM_USER,1,105); }