void nFile::Close() { nAssert(m_Handle); CloseHandle(m_Handle); m_Handle = NULL; }
bool nFile::Open(const nString& path,unsigned long flags) { nAssert(!m_Handle); unsigned long access = 0; unsigned long share = 0; unsigned long create = 0; if(flags & OpenFlagShareRead) share |= FILE_SHARE_READ; if(flags & OpenFlagShareWrite) share |= FILE_SHARE_WRITE; if(flags & OpenFlagCreate) create |= CREATE_ALWAYS; if(flags & OpenFlagExisting) create |= OPEN_EXISTING; if(flags & OpenFlagRead) access |= GENERIC_READ; if(flags & OpenFlagWrite) access |= GENERIC_WRITE; m_Handle = CreateFile(path.c_str(),access,share,NULL,create,NULL,NULL); if(m_Handle == INVALID_HANDLE_VALUE) { m_Handle = NULL; return false; } return true; }
void nGraphics::GetTextSize(ID3DXFont* font,const char* text,nSize* size,unsigned long flags) { UNREFERENCED_PARAMETER(flags); nAssert(font); GetTextExtentPoint(font->GetDC(),text,strlen(text),(LPSIZE)size); }
void nGraphics::SpriteEnd() { nAssert(m_pSprite); m_pSprite->End(); m_pStateBlocks[0]->Apply(); }
unsigned long nFile::Write(const void* buffer,unsigned long size) { nAssert(m_Handle); unsigned long written = 0; WriteFile(m_Handle,buffer,size,&written,NULL); return written; }
unsigned long nFile::Read(void* buffer,unsigned long size) { nAssert(m_Handle); unsigned long read = 0; ReadFile(m_Handle,buffer,size,&read,NULL); return read; }
void nGraphics::DrawText(ID3DXFont* font,const nRect& rect,const char* text,unsigned long flags,const nColor& color) { nAssert(font); // Don't draw if fully transparent if(color.a <= 0.0f) return; // Draw the text font->DrawText(m_pSprite,text,-1,(LPRECT)&rect,flags,color); }
void nGraphics::SetClipper(const nRect* rect) { nAssert(m_pDevice); if(rect) { m_pDevice->SetRenderState(D3DRS_SCISSORTESTENABLE,TRUE); m_pDevice->SetScissorRect((LPRECT)rect); } else m_pDevice->SetRenderState(D3DRS_SCISSORTESTENABLE,FALSE); }
bool nGraphics::GetClipper(nRect* rect) { nAssert(m_pDevice); unsigned long val = 0; m_pDevice->GetRenderState(D3DRS_SCISSORTESTENABLE,&val); if(rect) m_pDevice->GetScissorRect((LPRECT)rect); return val ? true : false; }
nString nKeyboard::GetKeyName(unsigned long key) { nAssert(m_pDevice); DIDEVICEOBJECTINSTANCE doi; ZeroMemory(&doi,sizeof(DIDEVICEOBJECTINSTANCE)); doi.dwSize = sizeof(DIDEVICEOBJECTINSTANCE); unsigned long dwOfs = key; m_pDevice->GetObjectInfo(&doi,dwOfs,DIPH_BYOFFSET); return doi.tszName; }
void nGraphics::DrawCircle(const nPoint& point,float radius,unsigned long elements,const nColor& color) { nAssert(elements); nVector2* lines = new nVector2[elements]; for(unsigned long i = 0; i < elements; i++) lines[i] = nVector2(point.x + radius * sinf(D3DX_PI*2/(elements-1) * i),point.y + radius * cosf(D3DX_PI*2/(elements-1) * i)); DrawLines(lines,elements,color); nSafeFree(lines); }
unsigned long nFile::Seek(unsigned long position,SeekFrom from) { nAssert(m_Handle); unsigned long fr = 0; if(from == SeekFromStart) fr = FILE_BEGIN; else if(from == SeekFromCurrent) fr = FILE_CURRENT; else if(from == SeekFromEnd) fr = FILE_END; return SetFilePointer(m_Handle,position,NULL,fr); }
void nOrtho(float left, float top, float right, float bottom, float near, float far, float* m) { nAssert(far != near); const float rightMinusLeft = right - left; const float topMinusBottom = top - bottom; const float farMinusNear = far - near; m[0] = 2 / rightMinusLeft; m[1] = m[2] = m[3] = m[4] = 0.0f; m[5] = 2 / topMinusBottom; m[6] = m[7] = m[8] = m[9] = 0.0f; m[10] = -2 / farMinusNear; m[11] = 0; m[12] = -(right + left) / rightMinusLeft; m[13] = -(top + bottom) / topMinusBottom; m[14] = -(far + near) / farMinusNear; m[15] = 1.0f; }
bool nKeyboard::Update() { nAssert(m_pDevice); CopyMemory(&m_Keys[0],&m_Keys[1],sizeof(m_Keys[0])); // Get the keystates from the device HRESULT hr = m_pDevice->GetDeviceState(sizeof(m_Keys[1]),&m_Keys[1]); if(hr != S_OK) { // If input is lost then acquire and keep trying hr = m_pDevice->Acquire(); while(hr == DIERR_INPUTLOST) hr = m_pDevice->Acquire(); return false; } return true; }
void nGraphics::DrawShadowText(ID3DXFont* font,const nRect& rect,const char* text,unsigned long flags,const nColor& color) { nAssert(font); // Don't draw if fully transparent if(color.a <= 0.0f) return; // Draw the text static nRect rect2; static nColor color2; rect2 = rect; rect2.Offset(1,1); color2.r = color2.g = color2.b = 0.0f; color2.a = color.a * 0.1f; DrawText(font,rect2,text,flags,color2); DrawText(font,rect,text,flags,color); }
unsigned long nFile::Read(nString* value) { unsigned long size = 0; char* buffer = NULL; unsigned long read = Read(&size); if(!size) return read; buffer = nMalloc(char[size + 1]); nAssert(buffer); read += Read(buffer,size); buffer[size] = NULL; *value = buffer; nSafeFree(buffer); return read; }
void nGraphics::DrawPrintShadowLine(ID3DXFont* font,const nRect& rect,const char* text,float delta,unsigned long flags,const nColor& color) { nAssert(font); // Don't draw if fully transparent if(color.a <= 0.0f) return; nString textString(text); nColor color2(color); if(delta < 0.0f) color2.a = 0.0f; else if(delta < 1.0f) color2.a *= cubicf(0.0f,1.0f,delta); else if(delta > textString.size()) color2.a = 0.0f; else if(delta > textString.size() - 1.0f) color2.a *= cubicf(0.0f,1.0f,textString.size() - min(delta,textString.size())); unsigned long numChars = min(max(0.0f,delta),textString.size()); numChars = textString.size() * cubicf(0.0f,1.0f,(float)numChars/(float)textString.size()); // Comment-out for no cubic interpolation textString.erase(textString.end() - textString.size() + numChars,textString.end()); nSize size; GetTextSize(font,textString.c_str(),&size,NULL); nSize space; GetTextSize(font," ",&space,NULL); space.cx = 8; // Draw the text DrawShadowText(font,rect,textString.c_str(),flags,color); // Draw block DrawShadowFillRect(nRect(rect.left + size.cx,rect.top,rect.left + size.cx + space.cx,rect.top + space.cy),color2); }
void nGraphics::DrawLines(const nVector2* lines,unsigned long count,const nColor& color) { nAssert(m_pLine); m_pLine->Draw((LPD3DXVECTOR2)lines,count,color); }
void nGraphics::SpriteFlush() { nAssert(m_pSprite); m_pSprite->Flush(); }
bool nFile::Flush() { nAssert(m_Handle); return FlushFileBuffers(m_Handle) ? true : false; }
unsigned long nFile::GetSize() { nAssert(m_Handle); return GetFileSize(m_Handle,NULL); // TODO Won't work correctly with 64-bit file sizes }
unsigned long nFile::Tell() { nAssert(m_Handle); return this->Seek(0,SeekFromCurrent); }
void nGraphics::GetTextRect(ID3DXFont* font,const char* text,nRect* rect,unsigned long flags) { nAssert(font); font->DrawText(m_pSprite,text,-1,(LPRECT)rect,flags|DT_CALCRECT,0xFFFFFFFF); }