From fe1aa3c26e1b036d474740d9d6873a13314620f9 Mon Sep 17 00:00:00 2001 From: Brian Fiete Date: Fri, 28 Mar 2025 08:08:33 -0400 Subject: [PATCH] BeefySysLib point/rect updates, async pipe fixes --- BeefLibs/corlib/src/IO/Pipe.bf | 3 + BeefLibs/corlib/src/Net/Socket.bf | 5 +- BeefLibs/corlib/src/Windows.bf | 17 +++ BeefySysLib/BeefySysLib.cpp | 16 +-- BeefySysLib/CachedDataStream.cpp | 7 +- BeefySysLib/CachedDataStream.h | 4 +- BeefySysLib/DataStream.h | 4 +- BeefySysLib/FileHandleStream.cpp | 14 ++- BeefySysLib/FileHandleStream.h | 4 +- BeefySysLib/FileStream.cpp | 24 ++-- BeefySysLib/FileStream.h | 8 +- BeefySysLib/MemStream.cpp | 15 ++- BeefySysLib/MemStream.h | 10 +- BeefySysLib/gfx/RenderDevice.h | 4 +- BeefySysLib/gfx/Texture.cpp | 4 +- BeefySysLib/gfx/Texture.h | 2 +- BeefySysLib/platform/sdl/GLRenderDevice.cpp | 6 +- BeefySysLib/platform/win/DXRenderDevice.cpp | 16 +-- BeefySysLib/platform/win/DXRenderDevice.h | 2 +- BeefySysLib/platform/win/Platform.cpp | 10 ++ BeefySysLib/util/BSpline.cpp | 15 ++- BeefySysLib/util/BSpline.h | 2 +- BeefySysLib/util/CatmullRom.cpp | 8 +- BeefySysLib/util/CatmullRom.h | 2 +- BeefySysLib/util/CubicFuncSpline.cpp | 26 ++--- BeefySysLib/util/CubicFuncSpline.h | 2 +- BeefySysLib/util/CubicSpline.cpp | 10 +- BeefySysLib/util/CubicSpline.h | 4 +- BeefySysLib/util/Point.h | 27 ++++- BeefySysLib/util/PolySpline.cpp | 8 +- BeefySysLib/util/PolySpline.h | 2 +- BeefySysLib/util/Rect.h | 121 +++++++++++++++++--- 32 files changed, 282 insertions(+), 120 deletions(-) diff --git a/BeefLibs/corlib/src/IO/Pipe.bf b/BeefLibs/corlib/src/IO/Pipe.bf index 005fa00b..6cf91798 100644 --- a/BeefLibs/corlib/src/IO/Pipe.bf +++ b/BeefLibs/corlib/src/IO/Pipe.bf @@ -76,6 +76,9 @@ namespace System.IO Platform.BfpFileCreateKind createKind = .CreateAlways; Platform.BfpFileCreateFlags createFlags = .Pipe; + if (options.HasFlag(.AllowTimeouts)) + createFlags |= .AllowTimeouts; + createKind = .OpenExisting; createFlags |= .Read; createFlags |= .Write; diff --git a/BeefLibs/corlib/src/Net/Socket.bf b/BeefLibs/corlib/src/Net/Socket.bf index e3db5ace..dcc2bc75 100644 --- a/BeefLibs/corlib/src/Net/Socket.bf +++ b/BeefLibs/corlib/src/Net/Socket.bf @@ -392,8 +392,11 @@ namespace System.Net service.sin_addr = address; service.sin_port = (uint16)htons((int16)port); - if (bind(mHandle, &service, sizeof(SockAddr_in)) == SOCKET_ERROR) + int32 size = sizeof(SockAddr_in); + if (bind(mHandle, &service, size) == SOCKET_ERROR) { + int err = WSAGetLastError(); + Close(); return .Err; } diff --git a/BeefLibs/corlib/src/Windows.bf b/BeefLibs/corlib/src/Windows.bf index 716f3acb..a393399e 100644 --- a/BeefLibs/corlib/src/Windows.bf +++ b/BeefLibs/corlib/src/Windows.bf @@ -107,6 +107,11 @@ namespace System public int32 Height => bottom - top; } + public struct Point : this(int32 x, int32 y) + { + + } + [CRepr] public struct OpenFileName { @@ -1703,6 +1708,12 @@ namespace System [Import("user32.lib"), CLink, CallingConvention(.Stdcall)] public static extern HWnd SetActiveWindow(HWnd wnd); + [Import("user32.lib"), CLink, CallingConvention(.Stdcall)] + public static extern HWnd SetForegroundWindow(HWnd wnd); + + [Import("user32.lib"), CLink, CallingConvention(.Stdcall)] + public static extern HWnd GetForegroundWindow(); + [CLink, CallingConvention(.Stdcall)] public static extern int CallWindowProcA(int wndProc, HWnd hWnd, int32 msg, int wParam, int lParam); @@ -1749,6 +1760,12 @@ namespace System [Import("user32.lib"), CLink, CallingConvention(.Stdcall)] public static extern IntBool SetWindowPos(HWnd hWnd, HWnd hWndAfter, int x, int y, int cx, int cy, int flags); + [Import("user32.lib"), CLink, CallingConvention(.Stdcall)] + public static extern IntBool GetClientRect(HWnd hWnd, out Rect rect); + + [Import("user32.lib"), CLink, CallingConvention(.Stdcall)] + public static extern IntBool ClientToScreen(HWnd hWnd, ref Point rect); + [Import("user32.lib"), CLink, CallingConvention(.Stdcall)] public static extern IntBool PostMessageW(HWnd hWnd, int32 msg, int wParam, int lParam); diff --git a/BeefySysLib/BeefySysLib.cpp b/BeefySysLib/BeefySysLib.cpp index 1cba869e..bfaf3143 100644 --- a/BeefySysLib/BeefySysLib.cpp +++ b/BeefySysLib/BeefySysLib.cpp @@ -587,15 +587,15 @@ BF_EXPORT void BF_CALLTYPE Gfx_ApplyEffect(TextureSegment* destTextureSegment, T ImageData destImageData; ImageData srcImageData; - Rect srcRect = srcTextureSegment->GetRect(); - Rect destRect = destTextureSegment->GetRect(); + RectF srcRect = srcTextureSegment->GetRect(); + RectF destRect = destTextureSegment->GetRect(); destTextureSegment->GetImageData(destImageData); srcImageData.CreateNew(destImageData.mWidth, destImageData.mHeight); srcTextureSegment->GetImageData(srcImageData, - (int)(destRect.mWidth - srcRect.mWidth) / 2, - (int)(destRect.mHeight - srcRect.mHeight) / 2); + (int)(destRect.width - srcRect.width) / 2, + (int)(destRect.height - srcRect.height) / 2); effect->Apply(NULL, &srcImageData, &destImageData); if (needsPremultiply) @@ -831,10 +831,10 @@ BF_EXPORT void BF_CALLTYPE RenderState_SetWireframe(RenderState* renderState, bo BF_EXPORT void BF_CALLTYPE RenderState_SetClip(RenderState* renderState, float x, float y, float width, float height) { BF_ASSERT((width >= 0) && (height >= 0)); - renderState->mClipRect.mX = x; - renderState->mClipRect.mY = y; - renderState->mClipRect.mWidth = width; - renderState->mClipRect.mHeight = height; + renderState->mClipRect.x = x; + renderState->mClipRect.y = y; + renderState->mClipRect.width = width; + renderState->mClipRect.height = height; if (!renderState->mClipped) renderState->SetClipped(true); } diff --git a/BeefySysLib/CachedDataStream.cpp b/BeefySysLib/CachedDataStream.cpp index 66ed5ed8..c4a5dba0 100644 --- a/BeefySysLib/CachedDataStream.cpp +++ b/BeefySysLib/CachedDataStream.cpp @@ -36,13 +36,13 @@ int CachedDataStream::GetSize() return mStream->GetSize(); } -void CachedDataStream::Read(void* ptr, int size) +int CachedDataStream::Read(void* ptr, int size) { Flush(); - mStream->Read(ptr, size); + return mStream->Read(ptr, size); } -void CachedDataStream::Write(void* ptr, int size) +int CachedDataStream::Write(void* ptr, int size) { while (size > 0) { @@ -59,6 +59,7 @@ void CachedDataStream::Write(void* ptr, int size) size -= writeBytes; mDataPtr += writeBytes; } + return size; } int CachedDataStream::GetPos() diff --git a/BeefySysLib/CachedDataStream.h b/BeefySysLib/CachedDataStream.h index 6be84023..1c10c9db 100644 --- a/BeefySysLib/CachedDataStream.h +++ b/BeefySysLib/CachedDataStream.h @@ -24,8 +24,8 @@ public: virtual bool Eof() override; virtual int GetSize() override; - virtual void Read(void* ptr, int size) override; - virtual void Write(void* ptr, int size) override; + virtual int Read(void* ptr, int size) override; + virtual int Write(void* ptr, int size) override; virtual int GetPos() override; virtual void Seek(int size) override; diff --git a/BeefySysLib/DataStream.h b/BeefySysLib/DataStream.h index 26a46a84..40ef38f7 100644 --- a/BeefySysLib/DataStream.h +++ b/BeefySysLib/DataStream.h @@ -21,8 +21,8 @@ public: virtual bool Eof() = 0; virtual int GetSize() = 0; - virtual void Read(void* ptr, int size) = 0; - virtual void Write(void* ptr, int size) = 0; + virtual int Read(void* ptr, int size) = 0; + virtual int Write(void* ptr, int size) = 0; virtual void WriteZeros(int size); virtual void Align(int size); diff --git a/BeefySysLib/FileHandleStream.cpp b/BeefySysLib/FileHandleStream.cpp index dca12bf0..27d08a4c 100644 --- a/BeefySysLib/FileHandleStream.cpp +++ b/BeefySysLib/FileHandleStream.cpp @@ -62,10 +62,12 @@ int FileHandleStream::GetSize() return ::GetFileSize(mFileHandle, NULL); } -void FileHandleStream::Read(void* ptr, int size) +int FileHandleStream::Read(void* ptr, int size) { if (mCacheBuffer != NULL) { + int totalReadSize = 0; + while (true) { int buffOffset = mVFilePos - mCacheReadPos; @@ -74,7 +76,7 @@ void FileHandleStream::Read(void* ptr, int size) // If inside memcpy(ptr, mCacheBuffer + buffOffset, size); mVFilePos += size; - return; + return size; } else if ((buffOffset >= 0) && (buffOffset < mCacheSize)) { @@ -84,6 +86,7 @@ void FileHandleStream::Read(void* ptr, int size) ptr = (uint8*)ptr + subSize; size -= subSize; + totalReadSize += subSize; } mCacheReadPos = mVFilePos & ~(4096 - 1); @@ -95,7 +98,9 @@ void FileHandleStream::Read(void* ptr, int size) // Zero out underflow bytes memset((uint8*)ptr + aSize, 0, mCacheSize - aSize); } + totalReadSize += aSize; } + return totalReadSize; } else { @@ -106,12 +111,13 @@ void FileHandleStream::Read(void* ptr, int size) // Zero out underflow bytes memset((uint8*)ptr + aSize, 0, size - aSize); } + return aSize; } } -void FileHandleStream::Write(void* ptr, int size) +int FileHandleStream::Write(void* ptr, int size) { - ::WriteFile(mFileHandle, ptr, size, NULL, NULL); + return (int)::WriteFile(mFileHandle, ptr, size, NULL, NULL); } int FileHandleStream::GetPos() diff --git a/BeefySysLib/FileHandleStream.h b/BeefySysLib/FileHandleStream.h index 31576866..97408476 100644 --- a/BeefySysLib/FileHandleStream.h +++ b/BeefySysLib/FileHandleStream.h @@ -23,9 +23,9 @@ public: bool Eof() override; int GetSize() override; using DataStream::Read; - void Read(void* ptr, int size) override; + int Read(void* ptr, int size) override; using DataStream::Write; - void Write(void* ptr, int size) override; + int Write(void* ptr, int size) override; int GetPos() override; void Seek(int size) override; diff --git a/BeefySysLib/FileStream.cpp b/BeefySysLib/FileStream.cpp index 08ee6f3d..1913db68 100644 --- a/BeefySysLib/FileStream.cpp +++ b/BeefySysLib/FileStream.cpp @@ -99,10 +99,12 @@ int FileStream::GetSize() return aSize; } -void FileStream::Read(void* ptr, int size) +int FileStream::Read(void* ptr, int size) { if (mCacheBuffer != NULL) { + int totalReadSize = 0; + while (true) { int buffOffset = mVFilePos - mCacheReadPos; @@ -111,7 +113,7 @@ void FileStream::Read(void* ptr, int size) // If inside memcpy(ptr, mCacheBuffer + buffOffset, size); mVFilePos += size; - return; + return size; } else if ((buffOffset >= 0) && (buffOffset < mCacheSize)) { @@ -121,6 +123,8 @@ void FileStream::Read(void* ptr, int size) ptr = (uint8*) ptr + subSize; size -= subSize; + + totalReadSize += subSize; } mCacheReadPos = mVFilePos & ~(4096-1); @@ -131,7 +135,11 @@ void FileStream::Read(void* ptr, int size) // Zero out underflow bytes memset((uint8*) ptr + aSize, 0, mCacheSize - aSize); } + + totalReadSize += aSize; } + + return totalReadSize; } else { @@ -142,12 +150,13 @@ void FileStream::Read(void* ptr, int size) memset((uint8*) ptr + aSize, 0, size - aSize); mReadPastEnd = true; } + return aSize; } } -void FileStream::Write(void* ptr, int size) +int FileStream::Write(void* ptr, int size) { - fwrite(ptr, 1, size, mFP); + return (int)fwrite(ptr, 1, size, mFP); } int FileStream::GetPos() @@ -250,7 +259,7 @@ int SysFileStream::GetSize() return (int)BfpFile_GetFileSize(mFile); } -void SysFileStream::Read(void* ptr, int size) +int SysFileStream::Read(void* ptr, int size) { int readSize = (int)BfpFile_Read(mFile, ptr, size, -1, NULL); if (readSize != size) @@ -258,11 +267,12 @@ void SysFileStream::Read(void* ptr, int size) // Zero out underflow bytes memset((uint8*)ptr + readSize, 0, size - readSize); } + return readSize; } -void SysFileStream::Write(void* ptr, int size) +int SysFileStream::Write(void* ptr, int size) { - BfpFile_Write(mFile, ptr, size, -1, NULL); + return (int)BfpFile_Write(mFile, ptr, size, -1, NULL); } int SysFileStream::GetPos() diff --git a/BeefySysLib/FileStream.h b/BeefySysLib/FileStream.h index 9405767b..30112cc1 100644 --- a/BeefySysLib/FileStream.h +++ b/BeefySysLib/FileStream.h @@ -28,9 +28,9 @@ public: bool Eof() override; int GetSize() override; using DataStream::Read; - void Read(void* ptr, int size) override; + int Read(void* ptr, int size) override; using DataStream::Write; - void Write(void* ptr, int size) override; + int Write(void* ptr, int size) override; int GetPos() override; void Seek(int size) override; @@ -67,9 +67,9 @@ public: bool Eof() override; int GetSize() override; using DataStream::Read; - void Read(void* ptr, int size) override; + int Read(void* ptr, int size) override; using DataStream::Write; - void Write(void* ptr, int size) override; + int Write(void* ptr, int size) override; int GetPos() override; void Seek(int size) override; diff --git a/BeefySysLib/MemStream.cpp b/BeefySysLib/MemStream.cpp index c4316cb5..112160f2 100644 --- a/BeefySysLib/MemStream.cpp +++ b/BeefySysLib/MemStream.cpp @@ -44,17 +44,19 @@ int MemStream::GetSize() return mSize; } -void MemStream::Read(void* ptr, int size) +int MemStream::Read(void* ptr, int size) { memcpy(ptr, mData + mPos, size); mPos += size; + return size; } -void Beefy::MemStream::Write(void* ptr, int size) +int Beefy::MemStream::Write(void* ptr, int size) { memcpy(mData + mPos, ptr, size); mPos += size; + return size; } int MemStream::GetPos() @@ -79,7 +81,7 @@ SafeMemStream::SafeMemStream(void* data, int size, bool freeMemory) : MemStream( mFailed = false; } -void SafeMemStream::Read(void* ptr, int size) +int SafeMemStream::Read(void* ptr, int size) { if (mPos + size > mSize) { @@ -91,6 +93,7 @@ void SafeMemStream::Read(void* ptr, int size) memcpy(ptr, mData + mPos, size); } mPos += size; + return size; } ////////////////////////////////////////////////////////////////////////// @@ -110,16 +113,18 @@ int DynMemStream::GetSize() return (int)mData.size(); } -void DynMemStream::Read(void* ptr, int size) +int DynMemStream::Read(void* ptr, int size) { memcpy(ptr, (uint8*)&mData.front() + mPos, size); mPos += size; + return size; } -void DynMemStream::Write(void* ptr, int size) +int DynMemStream::Write(void* ptr, int size) { mData.Insert(mPos, (uint8*)ptr, size); mPos += size; + return size; } void DynMemStream::Write(uint8 val) diff --git a/BeefySysLib/MemStream.h b/BeefySysLib/MemStream.h index 0a9d6512..682310bc 100644 --- a/BeefySysLib/MemStream.h +++ b/BeefySysLib/MemStream.h @@ -24,9 +24,9 @@ public: bool Eof() override; int GetSize() override; using DataStream::Read; - void Read(void* ptr, int size) override; + int Read(void* ptr, int size) override; using DataStream::Write; - void Write(void* ptr, int size) override; + int Write(void* ptr, int size) override; int GetPos() override; void Seek(int size) override; @@ -41,7 +41,7 @@ public: SafeMemStream(void* data, int size, bool freeMemory); using DataStream::Read; - void Read(void* ptr, int size) override; + int Read(void* ptr, int size) override; }; class DynMemStream : public DataStream @@ -56,9 +56,9 @@ public: bool Eof() override; int GetSize() override; using DataStream::Read; - void Read(void* ptr, int size) override; + int Read(void* ptr, int size) override; using DataStream::Write; - void Write(void* ptr, int size) override; + int Write(void* ptr, int size) override; void Write(uint8 val) override; int GetPos() override; diff --git a/BeefySysLib/gfx/RenderDevice.h b/BeefySysLib/gfx/RenderDevice.h index 96a05c54..6704b7d0 100644 --- a/BeefySysLib/gfx/RenderDevice.h +++ b/BeefySysLib/gfx/RenderDevice.h @@ -201,7 +201,7 @@ public: bool mClipped; bool mTexWrap; bool mWireframe; - Rect mClipRect; + RectF mClipRect; CullMode mCullMode; Topology3D mTopology; @@ -213,7 +213,7 @@ public: virtual void SetTexWrap(bool wrap) { mTexWrap = wrap; } virtual void SetWireframe(bool wireframe) { mWireframe = wireframe; } virtual void SetClipped(bool clipped) { mClipped = clipped; } - virtual void SetClipRect(const Rect& rect) { mClipRect = rect; } + virtual void SetClipRect(const RectF& rect) { mClipRect = rect; } virtual void SetWriteDepthBuffer(bool writeDepthBuffer) { mWriteDepthBuffer = writeDepthBuffer; } virtual void SetDepthFunc(DepthFunc depthFunc) { mDepthFunc = depthFunc; } virtual void SetTopology(Topology3D topology) { mTopology = topology; } diff --git a/BeefySysLib/gfx/Texture.cpp b/BeefySysLib/gfx/Texture.cpp index a7cafa56..189c306c 100644 --- a/BeefySysLib/gfx/Texture.cpp +++ b/BeefySysLib/gfx/Texture.cpp @@ -71,11 +71,11 @@ void TextureSegment::SetImageData(ImageData& imageData) SetBits(0, 0, imageData.mWidth, imageData.mHeight, imageData.mStride, imageData.mBits); } -Rect TextureSegment::GetRect() +RectF TextureSegment::GetRect() { float x1 = mU1 * mTexture->mWidth; float x2 = mU2 * mTexture->mWidth; float y1 = mV1 * mTexture->mHeight; float y2 = mV2 * mTexture->mHeight; - return Rect(x1, y1, x2 - x1, y2 - y1); + return RectF(x1, y1, x2 - x1, y2 - y1); } diff --git a/BeefySysLib/gfx/Texture.h b/BeefySysLib/gfx/Texture.h index 277afe55..dd89db95 100644 --- a/BeefySysLib/gfx/Texture.h +++ b/BeefySysLib/gfx/Texture.h @@ -47,7 +47,7 @@ public: void GetImageData(ImageData& imageData, int destX, int destY); void SetImageData(ImageData& imageData); - Rect GetRect(); + RectF GetRect(); }; NS_BF_END; diff --git a/BeefySysLib/platform/sdl/GLRenderDevice.cpp b/BeefySysLib/platform/sdl/GLRenderDevice.cpp index 29665763..28d3a558 100644 --- a/BeefySysLib/platform/sdl/GLRenderDevice.cpp +++ b/BeefySysLib/platform/sdl/GLRenderDevice.cpp @@ -737,9 +737,9 @@ void GLRenderDevice::PhysSetRenderState(RenderState* renderState) if (renderState->mClipped) { glEnable(GL_SCISSOR_TEST); - glScissor((GLsizei)renderState->mClipRect.mX, - mPhysRenderWindow->mHeight - (GLsizei)renderState->mClipRect.mY - (GLsizei)renderState->mClipRect.mHeight, - (GLsizei)renderState->mClipRect.mWidth, (GLsizei)renderState->mClipRect.mHeight); + glScissor((GLsizei)renderState->mClipRect.x, + mPhysRenderWindow->mHeight - (GLsizei)renderState->mClipRect.y - (GLsizei)renderState->mClipRect.height, + (GLsizei)renderState->mClipRect.width, (GLsizei)renderState->mClipRect.height); } else { diff --git a/BeefySysLib/platform/win/DXRenderDevice.cpp b/BeefySysLib/platform/win/DXRenderDevice.cpp index 7a9848cc..757debfd 100644 --- a/BeefySysLib/platform/win/DXRenderDevice.cpp +++ b/BeefySysLib/platform/win/DXRenderDevice.cpp @@ -762,11 +762,11 @@ void DXDrawBatch::Render(RenderDevice* renderDevice, RenderWindow* renderWindow) return; if ((mRenderState->mClipped) && - ((mRenderState->mClipRect.mWidth == 0) || (mRenderState->mClipRect.mHeight == 0))) + ((mRenderState->mClipRect.width == 0) || (mRenderState->mClipRect.height == 0))) return; if (mRenderState->mClipped) - BF_ASSERT((mRenderState->mClipRect.mWidth > 0) && (mRenderState->mClipRect.mHeight > 0)); + BF_ASSERT((mRenderState->mClipRect.width > 0) && (mRenderState->mClipRect.height > 0)); DXRenderDevice* aRenderDevice = (DXRenderDevice*)renderDevice; /*if ((mDrawLayer->mRenderWindow != NULL) && (aRenderDevice->mPhysRenderWindow != mDrawLayer->mRenderWindow)) @@ -931,10 +931,10 @@ void DXRenderDevice::PhysSetRenderState(RenderState* renderState) if (renderState->mClipped) { D3D11_RECT rects[1]; - rects[0].left = (int)renderState->mClipRect.mX; - rects[0].right = (int) (renderState->mClipRect.mX + renderState->mClipRect.mWidth); - rects[0].top = (int) renderState->mClipRect.mY; - rects[0].bottom = (int) (renderState->mClipRect.mY + renderState->mClipRect.mHeight); + rects[0].left = (int)renderState->mClipRect.x; + rects[0].right = (int) (renderState->mClipRect.x + renderState->mClipRect.width); + rects[0].top = (int) renderState->mClipRect.y; + rects[0].bottom = (int) (renderState->mClipRect.y + renderState->mClipRect.height); mD3DDeviceContext->RSSetScissorRects(1, rects); } setRasterizerState = true; @@ -1383,9 +1383,9 @@ void DXRenderState::SetTexWrap(bool wrap) InvalidateRasterizerState(); } -void DXRenderState::SetClipRect(const Rect& rect) +void DXRenderState::SetClipRect(const RectF& rect) { - BF_ASSERT((rect.mWidth >= 0) && (rect.mHeight >= 0)); + BF_ASSERT((rect.width >= 0) && (rect.height >= 0)); mClipRect = rect; InvalidateRasterizerState(); } diff --git a/BeefySysLib/platform/win/DXRenderDevice.h b/BeefySysLib/platform/win/DXRenderDevice.h index 2dea4d9a..940615b6 100644 --- a/BeefySysLib/platform/win/DXRenderDevice.h +++ b/BeefySysLib/platform/win/DXRenderDevice.h @@ -219,7 +219,7 @@ public: virtual void SetClipped(bool clipped); virtual void SetTexWrap(bool clipped); - virtual void SetClipRect(const Rect& rect); + virtual void SetClipRect(const RectF& rect); virtual void SetWriteDepthBuffer(bool writeDepthBuffer); virtual void SetDepthFunc(DepthFunc depthFunc); }; diff --git a/BeefySysLib/platform/win/Platform.cpp b/BeefySysLib/platform/win/Platform.cpp index cd11f266..fe64d26b 100644 --- a/BeefySysLib/platform/win/Platform.cpp +++ b/BeefySysLib/platform/win/Platform.cpp @@ -3016,6 +3016,9 @@ BFP_EXPORT BfpFile* BFP_CALLTYPE BfpFile_Create(const char* path, BfpFileCreateK if ((createFlags & BfpFileCreateFlag_NoBuffering) != 0) desiredAccess |= FILE_FLAG_NO_BUFFERING; + if ((createFlags & BfpFileCreateFlag_AllowTimeouts) != 0) + attributes |= FILE_FLAG_OVERLAPPED; + HANDLE handle = ::CreateFileW(wPath.c_str(), desiredAccess, shareMode, NULL, creationDisposition, attributes, NULL); if (handle == INVALID_HANDLE_VALUE) { @@ -3043,6 +3046,13 @@ BFP_EXPORT BfpFile* BFP_CALLTYPE BfpFile_Create(const char* path, BfpFileCreateK BfpFile* bfpFile = new BfpFile(); bfpFile->mHandle = handle; + + if ((createFlags & BfpFileCreateFlag_AllowTimeouts) != 0) + bfpFile->mAsyncData = new BfpAsyncData(); + + if ((createFlags & BfpFileCreateFlag_Pipe) != 0) + bfpFile->mIsPipe = true; + return bfpFile; } diff --git a/BeefySysLib/util/BSpline.cpp b/BeefySysLib/util/BSpline.cpp index 5ca93c66..acf1a477 100644 --- a/BeefySysLib/util/BSpline.cpp +++ b/BeefySysLib/util/BSpline.cpp @@ -34,9 +34,9 @@ void BSpline2D::AddPt(float x, float y) delete mUVals; mUVals = NULL; - Point2D pt; - pt.mX = x; - pt.mY = y; + PointF pt; + pt.x = x; + pt.y = y; mInputPoints.push_back(pt); } @@ -44,9 +44,8 @@ void BSpline2D::Calculate() { int n = (int) mInputPoints.size(); int t = 1; - Point2D* control = &mInputPoints[0]; + PointF* control = &mInputPoints[0]; - mUVals=new int[n+t+1]; compute_intervals(mUVals, n, t); @@ -112,7 +111,7 @@ void BSpline2D::Evaluate(float pct, float* x, float* y) int t = (int)mInputPoints.size() - 3; // ???? t = 1; - Point2D* control = &mInputPoints[0]; + PointF* control = &mInputPoints[0]; // initialize the variables that will hold our outputted point @@ -122,8 +121,8 @@ void BSpline2D::Evaluate(float pct, float* x, float* y) for (k=0; k<=n; k++) { temp = blend(t,t,mUVals,pct); // same blend is used for each dimension coordinate - oX = oX + (control[k]).mX * temp; - oY = oY + (control[k]).mY * temp; + oX = oX + (control[k]).x * temp; + oY = oY + (control[k]).y * temp; } *x = oX; diff --git a/BeefySysLib/util/BSpline.h b/BeefySysLib/util/BSpline.h index eef0a657..5b39611f 100644 --- a/BeefySysLib/util/BSpline.h +++ b/BeefySysLib/util/BSpline.h @@ -8,7 +8,7 @@ NS_BF_BEGIN; class BSpline2D { public: - Array mInputPoints; + Array mInputPoints; int* mUVals; public: diff --git a/BeefySysLib/util/CatmullRom.cpp b/BeefySysLib/util/CatmullRom.cpp index 93e0a060..8552fc86 100644 --- a/BeefySysLib/util/CatmullRom.cpp +++ b/BeefySysLib/util/CatmullRom.cpp @@ -2,7 +2,7 @@ USING_NS_BF; -Point2D Beefy::CatmullRomEvaluate(Point2D &p0, Point2D &p1, Point2D &p2, Point2D &p3, float tension, float t) +PointF Beefy::CatmullRomEvaluate(PointF &p0, PointF &p1, PointF &p2, PointF &p3, float tension, float t) { float t2 = t * t; float t3 = t2 * t; @@ -17,8 +17,8 @@ Point2D Beefy::CatmullRomEvaluate(Point2D &p0, Point2D &p1, Point2D &p2, Point2D float b3 = s * (t3 - 2 * t2 + t) + (-2 * t3 + 3 * t2); // s(t3 - 2 t2 + t)P3 + (-2 t3 + 3 t2)P3 float b4 = s * (t3 - t2); // s(t3 - t2)P4 - float x = (p0.mX*b1 + p1.mX*b2 + p2.mX*b3 + p3.mX*b4); - float y = (p0.mY*b1 + p1.mY*b2 + p2.mY*b3 + p3.mY*b4); + float x = (p0.x*b1 + p1.x*b2 + p2.x*b3 + p3.x*b4); + float y = (p0.y*b1 + p1.y*b2 + p2.y*b3 + p3.y*b4); - return Point2D(x,y); + return PointF(x,y); } diff --git a/BeefySysLib/util/CatmullRom.h b/BeefySysLib/util/CatmullRom.h index c0842c2d..2625b7f3 100644 --- a/BeefySysLib/util/CatmullRom.h +++ b/BeefySysLib/util/CatmullRom.h @@ -5,6 +5,6 @@ NS_BF_BEGIN; -Point2D CatmullRomEvaluate(Point2D &p0, Point2D &p1, Point2D &p2, Point2D &p3, float tension, float t); +PointF CatmullRomEvaluate(PointF &p0, PointF &p1, PointF &p2, PointF &p3, float tension, float t); NS_BF_END; diff --git a/BeefySysLib/util/CubicFuncSpline.cpp b/BeefySysLib/util/CubicFuncSpline.cpp index f0fb969b..e28622c7 100644 --- a/BeefySysLib/util/CubicFuncSpline.cpp +++ b/BeefySysLib/util/CubicFuncSpline.cpp @@ -25,7 +25,7 @@ void CubicFuncSpline::AddPt(float x, float y) intpoly = NULL; slopes = NULL; - mInputPoints.push_back(Point2D(x, y)); + mInputPoints.push_back(PointF(x, y)); } int CubicFuncSpline::GetLength() @@ -42,7 +42,7 @@ void CubicFuncSpline::Lagrange() for( i = 0; i < nPts; i++ ) { lagpoly[i+0*nPts] = 1.0; - float fac = mInputPoints[i].mY; + float fac = mInputPoints[i].y; j = 0; for( k = 0; k < nPts; k++ ) { @@ -50,10 +50,10 @@ void CubicFuncSpline::Lagrange() continue; lagpoly[i+(j+1)*nPts] = lagpoly[i+j*nPts]; for( jj = j; jj > 0; jj-- ) - lagpoly[i+jj*nPts] = lagpoly[i+(jj-1)*nPts] - lagpoly[i+jj*nPts]*mInputPoints[k].mX; - lagpoly[i+0*nPts] *= -mInputPoints[k].mX; + lagpoly[i+jj*nPts] = lagpoly[i+(jj-1)*nPts] - lagpoly[i+jj*nPts]*mInputPoints[k].x; + lagpoly[i+0*nPts] *= -mInputPoints[k].x; j++; - fac /= ( mInputPoints[i].mX - mInputPoints[k].mX ); + fac /= ( mInputPoints[i].x - mInputPoints[k].x ); } for( j = 0; j < nPts; j++ ) lagpoly[i+j*nPts] *= fac; @@ -75,9 +75,9 @@ void CubicFuncSpline::ComputeSplineSlopes() for( i = 0; i < n; i++ ) { - h[i] = mInputPoints[i+1].mX - mInputPoints[i].mX; + h[i] = mInputPoints[i+1].x - mInputPoints[i].x; hinv[i] = 1.0f / h[i]; - g[i] = 3 * ( mInputPoints[i+1].mY-mInputPoints[i].mY ) * hinv[i] * hinv[i]; + g[i] = 3 * ( mInputPoints[i+1].y-mInputPoints[i].y ) * hinv[i] * hinv[i]; } a[0] = 2 * hinv[0]; b[0] = g[0]; @@ -119,7 +119,7 @@ float CubicFuncSpline::Evaluate(float x) Calculate(); int idx = (int) 0; - while ((idx < (int) mInputPoints.size()) && (x > mInputPoints[idx].mX)) + while ((idx < (int) mInputPoints.size()) && (x > mInputPoints[idx].x)) idx++; if ((idx == mInputPoints.size()) || (idx == 0)) @@ -128,13 +128,13 @@ float CubicFuncSpline::Evaluate(float x) if (idx == mInputPoints.size()) idx--; float s1 = slopes[idx]; - return mInputPoints[idx].mY + (x - mInputPoints[idx].mX) * s1; + return mInputPoints[idx].y + (x - mInputPoints[idx].x) * s1; } - float x0 = mInputPoints[idx-1].mX; - float x1 = mInputPoints[idx].mX; - float y0 = mInputPoints[idx-1].mY; - float y1 = mInputPoints[idx].mY; + float x0 = mInputPoints[idx-1].x; + float x1 = mInputPoints[idx].x; + float y0 = mInputPoints[idx-1].y; + float y1 = mInputPoints[idx].y; float s0 = slopes[idx-1]; float s1 = slopes[idx]; diff --git a/BeefySysLib/util/CubicFuncSpline.h b/BeefySysLib/util/CubicFuncSpline.h index 24b378ca..93083ea2 100644 --- a/BeefySysLib/util/CubicFuncSpline.h +++ b/BeefySysLib/util/CubicFuncSpline.h @@ -9,7 +9,7 @@ NS_BF_BEGIN; class CubicFuncSpline { public: - std::vector mInputPoints; + std::vector mInputPoints; float* lagpoly; float* intpoly; float* slopes; diff --git a/BeefySysLib/util/CubicSpline.cpp b/BeefySysLib/util/CubicSpline.cpp index 0b07c6d3..2e4dbcfe 100644 --- a/BeefySysLib/util/CubicSpline.cpp +++ b/BeefySysLib/util/CubicSpline.cpp @@ -72,7 +72,7 @@ void CubicSpline2D::AddPt(float x, float y) delete mYCubicArray; mYCubicArray = NULL; - mInputPoints.push_back(Point2D(x, y)); + mInputPoints.push_back(PointF(x, y)); } int CubicSpline2D::GetLength() @@ -86,15 +86,15 @@ void CubicSpline2D::Calculate() std::vector yVals; for (int i = 0; i < (int) mInputPoints.size(); i++) { - xVals.push_back(mInputPoints[i].mX); - yVals.push_back(mInputPoints[i].mY); + xVals.push_back(mInputPoints[i].x); + yVals.push_back(mInputPoints[i].y); } mXCubicArray = SolveCubic(xVals); mYCubicArray = SolveCubic(yVals); } -Point2D CubicSpline2D::Evaluate(float t) +PointF CubicSpline2D::Evaluate(float t) { if (mXCubicArray == NULL) Calculate(); @@ -105,5 +105,5 @@ Point2D CubicSpline2D::Evaluate(float t) if (idx >= (int) mInputPoints.size() - 1) return mInputPoints[mInputPoints.size() - 1]; - return Point2D(mXCubicArray[idx].Evaluate(frac), mYCubicArray[idx].Evaluate(frac)); + return PointF(mXCubicArray[idx].Evaluate(frac), mYCubicArray[idx].Evaluate(frac)); } \ No newline at end of file diff --git a/BeefySysLib/util/CubicSpline.h b/BeefySysLib/util/CubicSpline.h index 087cde0a..8905c151 100644 --- a/BeefySysLib/util/CubicSpline.h +++ b/BeefySysLib/util/CubicSpline.h @@ -38,7 +38,7 @@ public: class CubicSpline2D { public: - std::vector mInputPoints; + std::vector mInputPoints; CubicVal* mXCubicArray; CubicVal* mYCubicArray; @@ -53,7 +53,7 @@ public: int GetLength(); void Calculate(); - Point2D Evaluate(float t); + PointF Evaluate(float t); }; NS_BF_END; diff --git a/BeefySysLib/util/Point.h b/BeefySysLib/util/Point.h index 06528339..816a7d32 100644 --- a/BeefySysLib/util/Point.h +++ b/BeefySysLib/util/Point.h @@ -4,18 +4,33 @@ NS_BF_BEGIN; -class Point2D +template +class Point { public: - float mX; - float mY; + T x; + T y; public: - Point2D(float x = 0, float y = 0) + Point(T x = 0, T y = 0) { - mX = x; - mY = y; + this->x = x; + this->y = y; + } + + Point operator+(Point rhs) + { + return Point(x + rhs.x, y + rhs.y); + } + + Point operator-(Point rhs) + { + return Point(x - rhs.x, y - rhs.y); } }; +typedef Point PointD; +typedef Point PointF; +typedef Point PointI32; + NS_BF_END; diff --git a/BeefySysLib/util/PolySpline.cpp b/BeefySysLib/util/PolySpline.cpp index 8c862cc9..f879bf1c 100644 --- a/BeefySysLib/util/PolySpline.cpp +++ b/BeefySysLib/util/PolySpline.cpp @@ -19,7 +19,7 @@ void PolySpline2D::AddPt(float x, float y) - mInputPoints.push_back(Point2D(x, y)); + mInputPoints.push_back(PointF(x, y)); } int PolySpline2D::GetLength() @@ -34,11 +34,11 @@ void PolySpline2D::Calculate() mCoefs = new float[n]; for (int j=0; j mInputPoints; + std::vector mInputPoints; public: diff --git a/BeefySysLib/util/Rect.h b/BeefySysLib/util/Rect.h index 48753f48..4b38d47a 100644 --- a/BeefySysLib/util/Rect.h +++ b/BeefySysLib/util/Rect.h @@ -1,37 +1,130 @@ #pragma once #include "Common.h" +#include "Point.h" NS_BF_BEGIN; +template class Rect { public: - float mX; - float mY; - float mWidth; - float mHeight; + T x; + T y; + T width; + T height; public: Rect() { - mX = 0; - mY = 0; - mWidth = 0; - mHeight = 0; + x = 0; + y = 0; + width = 0; + height = 0; } - Rect(float x, float y, float width, float height) + Rect(T x, T y, T width, T height) { - mX = x; - mY = y; - mWidth = width; - mHeight = height; + this->x = x; + this->y = y; + this->width = width; + this->height = height; + } + + bool operator==(const Rect& r2) + { + return (x == r2.x) && (y == r2.y) && (width == r2.width) && (height == r2.height); } bool operator!=(const Rect& r2) { - return (mX != r2.mX) || (mY != r2.mY) || (mWidth != r2.mWidth) || (mHeight != r2.mHeight); + return (x != r2.x) || (y != r2.y) || (width != r2.width) || (height != r2.height); + } + + bool Contains(T x, T y) + { + return (x >= this->x) && (y >= this->y) && (x < this->x + width) && (y < this->y + height); + } + + bool Contains(Point pt) + { + return (pt.x >= this->x) && (y >= this->y) && (x < this->x + width) && (y < this->y + height); + } + + T GetRight() + { + return x + width; + } + + T GetBottom() + { + return y + height; + } + + Rect Intersection(Rect rect) + { + T x1 = Max(x, rect.x); + T x2 = Min(x + width, rect.x + rect.width); + T y1 = Max(y, rect.y); + T y2 = Min(y + height, rect.y + rect.height); + if (((x2 - x1) < 0) || ((y2 - y1) < 0)) + return Rect(); + else + return Rect(x1, y1, x2 - x1, y2 - y1); + } + + bool Intersects(Rect rect) + { + T x1 = BF_MAX(x, rect.x); + T x2 = BF_MIN(x + width, rect.x + rect.width); + T y1 = BF_MAX(y, rect.y); + T y2 = BF_MIN(y + height, rect.y + rect.height); + if (((x2 - x1) <= 0) || ((y2 - y1) <= 0)) + return false; + else + return true; + } + + Rect Union(Rect rect) + { + T x1 = Min(x, rect.x); + T x2 = Max(x + width, rect.x + rect.width); + T y1 = Min(y, rect.y); + T y2 = Max(y + height, rect.y + rect.height); + return Rect(x1, y1, x2 - x1, y2 - y1); + } + + void Include(Point pt) + { + T left = x; + T top = y; + T right = x + width; + T bottom = y + height; + x = BF_MIN(pt.x, left); + y = BF_MIN(pt.y, top); + width = BF_MAX(pt.x, right) - x; + height = BF_MAX(pt.y, bottom) - y; + } + + void Inflate(T x, T y) + { + this->x -= x; + this->width += x + x; + this->y -= y; + this->height += y + y; + } +}; + +typedef Rect RectD; +typedef Rect RectF; +typedef Rect RectI32; + +template <> +struct BeefHash +{ + size_t operator()(RectI32 val) + { + return (size_t)val.x * 4790557 + (size_t)val.y * 6578863 + (size_t)val.width * 6273881 + (size_t)val.height * 9501077; } };