//-------------------------------------------------------------------------- VeJobSystem::VeJobSystem(size_t stFGNum, size_t stBGNum) noexcept { if (stFGNum) { m_i32FGState.store(0, std::memory_order_relaxed); m_kFGThreads.resize(stFGNum); for (size_t i(0); i < m_kFGThreads.size(); ++i) { fore_thread& t = m_kFGThreads[i]; t.index = (uint32_t)i; t.cond_val = 0; t.handle = VeCreateThread(&FGThreadCallback, &t, VE_JOB_FG_PRIORITY, VE_JOB_FG_STACK_SIZE); } } if (stBGNum) { VE_ASSERT(stBGNum <= (VE_JOB_BG_BUFFER_MASK + 1)); m_i32BGState.store(0, std::memory_order_relaxed); m_i32BGAvailableNum.store(0, std::memory_order_relaxed); m_kBGThreads.resize(stBGNum); for (size_t i(0); i < m_kBGThreads.size(); ++i) { back_thread& t = m_kBGThreads[i]; t.handle = VeCreateThread(&BGThreadCallback, &t, VE_JOB_BG_PRIORITY, VE_JOB_BG_STACK_SIZE); t.index = (uint32_t)i; } } m_i32BGJobNum.store(0, std::memory_order_relaxed); m_u32FrameCount.store(0, std::memory_order_relaxed); }
//-------------------------------------------------------------------------- bool VeJob::Start() noexcept { IncRefCount(); { std::lock_guard<vtd::spin_lock> lock(m_kLock); if (m_u32Pointer == 0 && m_u32Wait == 0 && m_kTaskArray.size()) { VE_ASSERT(m_kRelatedJobs.empty()); m_u32Pointer = 1; } else { return false; } } auto& kAtom = m_kTaskArray.front(); if (kAtom.m_eExecute == VeJob::EXE_BACKGROUND) { ve_job_sys.m_akBGQueues[kAtom.m_ePriority].push(this); ve_job_sys.m_i32BGJobNum.fetch_add(1, std::memory_order_relaxed); auto pt = ve_job_sys.m_kWaitingThreads.pop(); if (pt) { pt->loop.set(); } } else { size_t stFrame = ve_job_sys.m_u32FrameCount.load(std::memory_order_relaxed) & 1; stFrame = kAtom.m_eExecute == VeJob::EXE_FG_CURRENT ? stFrame : (!stFrame); ve_job_sys.m_akFGQueues[stFrame][kAtom.m_ePriority].push(this); } return true; }
//-------------------------------------------------------------------------- bool WindowsVideoDevice::AddDisplay(const VeChar8* pcDeviceName) noexcept { VeDebugOutputCore("Display: %s\n", pcDeviceName); VeDisplayMode kMode; if (!GetDisplayMode(pcDeviceName, ENUM_CURRENT_SETTINGS, &kMode)) { return false; } m_kDisplayList.resize(m_kDisplayList.size() + 1); VeVideoDisplay& kDisplay = m_kDisplayList.back(); VeDisplayData* pkDisplayData = VE_NEW VeDisplayData; VE_ASSERT(pkDisplayData); DISPLAY_DEVICEA device; device.cb = sizeof(device); if (EnumDisplayDevicesA(pcDeviceName, 0, &device, 0)) { kDisplay.m_kName = device.DeviceString; } kDisplay.m_kDesktopMode = kMode; kDisplay.m_kCurrentMode = kMode; kDisplay.m_spDriverData = pkDisplayData; return true; }
//-------------------------------------------------------------------------- void VeJob::End(bool bSuccess) noexcept { VeVector<VeJobPtr> kRelatedJobs; { std::lock_guard<vtd::spin_lock> lock(m_kLock); VE_ASSERT(!m_u32Wait); m_u32Pointer = 0; kRelatedJobs = std::move(m_kRelatedJobs); } if (bSuccess) { for (auto& job : kRelatedJobs) { job->Resume(); } } else { for (auto& job : kRelatedJobs) { job->End(false); } } OnEnd(bSuccess); DecRefCount(); }
//-------------------------------------------------------------------------- VeThread::~VeThread() { VE_ASSERT(m_u32State == 0); m_pkParams->m_pkThis = NULL; m_pkParams->m_kEventLoop.Set(); m_pkParams = NULL; }
InputDevice::InputDevice(uint8_t numKeys) : m_numButtons(numKeys) { VE_ASSERT(numKeys < MaxKeys, "Too many keys"); memset(m_state, 0, sizeof(m_state)); memset(m_lastState, 0, sizeof(m_lastState)); }
bool InputDevice::isReleased(uint8_t key) const { VE_ASSERT(key < m_numButtons, "Key out of bounds"); // ~0 = 1, ~1 = 0 // if m_lastState == 1 && m_state == 0 return (m_lastState[key] & ~m_state[key]) != 0; }
void UIScrollView::AddChild(UIWidget* pkWidget, VeInt32 i32ZOrder) { VE_ASSERT(pkWidget); pkWidget->SetParent(this); m_kChildren.AttachBack(*pkWidget); m_pkInnerNode->addChild(pkWidget, i32ZOrder); UpdateBounding(); }
//-------------------------------------------------------------------------- void Connection::_Disconnect() { VE_ASSERT(m_pkPeer); m_pkPeer->Shutdown(300); VENet::VEPeerInterface::DestroyInstance(m_pkPeer); m_pkPeer = NULL; g_pClient->m_kConnectOffList.AttachBack(m_kNode); }
//-------------------------------------------------------------------------- VeRenderWindowPtr D3D12Renderer::CreateRenderWindow( const VeWindowPtr& spWindow) noexcept { D3D12RenderWindow* pkRenderWindow = VE_NEW D3D12RenderWindow(spWindow); VE_ASSERT(pkRenderWindow); pkRenderWindow->Init(*this); return pkRenderWindow; }
//-------------------------------------------------------------------------- VeIAVertexBuffer::VeIAVertexBuffer(const VeChar8* pcName, VeUInt32 u32Slot, VeUInt32 u32Stride, VeUInt32 u32Offset) : VeRenderTick(VeRenderTick::IA_VERTEX_BUFFER), m_u32Slot(u32Slot) , m_u32Stride(u32Stride), m_u32Offset(u32Offset) { g_pRenderer->GetRenderObject(pcName, m_spBuffer); VE_ASSERT(m_spBuffer); }
//-------------------------------------------------------------------------- void VeBinaryOStream::Transfer(VeBinaryIStream& kFrom, VeInt32 i32Length) { VE_ASSERT(i32Length > 0); void* pvBuffer = VeMalloc(i32Length); VE_ASSERT_EQ(kFrom.Read(pvBuffer, i32Length), i32Length); AddBlob(pvBuffer, i32Length); VeFree(pvBuffer); }
//-------------------------------------------------------------------------- void WindowsWindow::SetTitle(const char* pcTitle) noexcept { int32_t i32Num = MultiByteToWideChar(CP_UTF8, 0, pcTitle, -1, nullptr, 0); VE_ASSERT(i32Num >= 0); VeDyanmicStack<WCHAR> kBuf(i32Num + 1); kBuf[size_t(i32Num)] = 0; MultiByteToWideChar(CP_UTF8, 0, pcTitle, -1, kBuf, i32Num); SetWindowTextW(m_hHandle, kBuf); }
//-------------------------------------------------------------------------- VeThread::VeThread(int32_t i32Priority, size_t stStackSize) noexcept { m_u32State.store(0, std::memory_order_relaxed); m_pkParams = VE_NEW ThreadParams(); m_pkParams->m_kEvent.set(); m_pkParams->m_pkThis = this; m_hThread = VeCreateThread(Callback, m_pkParams, i32Priority, stStackSize); VE_ASSERT(m_hThread); }
//-------------------------------------------------------------------------- void WindowsVideoDevice::_RestoreWindow(VeWindow::Data* pkWindow) noexcept { VE_ASSERT(pkWindow); VeWindowData* pkData = (VeWindowData*)pkWindow->m_spDriverdata; HWND hWnd = pkData->m_hWnd; pkData->m_bExpectedResize = TRUE; ShowWindow(hWnd, SW_RESTORE); pkData->m_bExpectedResize = FALSE; }
//-------------------------------------------------------------------------- void Connection::Connect(const VeChar8* pcSrvIp, VeUInt32 u32Port, const VeChar8* pcSrvPass) { VE_AUTO_LOCK_MUTEX(g_pClient->m_kMutex); if(!m_pkServer) { m_pkServer = NewAgent(); VE_ASSERT(m_pkServer); } m_kServerIp = pcSrvIp; m_u32Port = u32Port; m_kServerPass = pcSrvPass; VE_ASSERT(m_kNode.IsAttach(g_pClient->m_kConnectOffList)); g_pClient->m_kConnectOnList.AttachBack(m_kNode); m_kCommandList.PushBack(COM_CONNECT); }
//-------------------------------------------------------------------------- void VeSystem::InitVideo() noexcept { if (!m_spVideo) { m_spVideo = CreateVideoDevice(); VE_ASSERT(m_spVideo); m_spVideo->_Init(); } }
//-------------------------------------------------------------------------- bool VeRenderWindowD3D12::IsNeighbor(RecordBarrierMap& kBarriers, BarrierPath& kPath, VeSizeT i, VeSizeT j) noexcept { VE_ASSERT(kPath.i != i || kPath.j != j); if (kPath.i == i && kPath.j + 1 == j) return true; if (j == 0 && kPath.i + 1 == i && (kBarriers[kPath.i].size() - 1 == kPath.j)) return true; return false; }
//-------------------------------------------------------------------------- void VeTimer::Detach(const VeTimeEventPtr& spEvent) { VE_ASSERT(spEvent); VE_ASSERT(spEvent->m_pkParent == this); VE_ASSERT(spEvent->m_kNode.IsAttach(m_kEventList)); std::multimap<VeUInt64,VeTimeEvent*>::iterator it = m_kEventMap.lower_bound(spEvent->m_u64ExecuteTime); while(it != m_kEventMap.upper_bound(spEvent->m_u64ExecuteTime)) { if(it->second == spEvent) { m_kEventMap.erase(it); break; } ++it; } spEvent->m_pkParent = NULL; spEvent->m_kNode.Detach(); spEvent->DecRefCount(); }
//-------------------------------------------------------------------------- void VeJobSystem::ExecuteBackground(uint32_t u32Index) noexcept { VeJob* pkJob = FetchBackground(); while (pkJob) { VE_ASSERT(pkJob->GetType() & 0xF0); Execute(pkJob, u32Index); pkJob = FetchBackground(); } }
//-------------------------------------------------------------------------- void VeJobSystem::ProcessForegroundJobs() noexcept { VE_ASSERT(!m_pkParallel); int32_t check(0); if (m_i32FGState.compare_exchange_weak(check, 1, std::memory_order_relaxed)) { RunForeground(); m_u32FrameCount.fetch_add(1, std::memory_order_relaxed); } }
//-------------------------------------------------------------------------- VeSurfacePtr VeSurface::CreateRGBSurface(VeUInt32 u32Flags, VeInt32 i32Width, VeInt32 i32Height, VeInt32 i32Depth, VeUInt32 u32Rmask, VeUInt32 u32Gmask, VeUInt32 u32Bmask, VeUInt32 u32Amask) noexcept { VeUInt32 u32Format = VeMasksToPixelFormatEnum(i32Depth, u32Rmask, u32Gmask, u32Bmask, u32Amask); if (u32Format == VE_PIXELFORMAT_UNKNOWN) return nullptr; VeSurfacePtr spSurface = VE_NEW VeSurface(); VE_ASSERT(spSurface); spSurface->m_spFormat = VePixelFormat::Create(u32Format); if (!spSurface->m_spFormat) return nullptr; spSurface->m_i32Width = i32Width; spSurface->m_i32Height = i32Height; spSurface->m_i32Pitch = spSurface->CalculatePitch(); if (VE_ISPIXELFORMAT_INDEXED(spSurface->m_spFormat->m_u32Format)) { VePalettePtr spPattle = VE_NEW VePalette((1 << spSurface->m_spFormat->m_u8BitsPerPixel)); VE_ASSERT(spPattle); if (spPattle->m_kColors.size() == 2) { spPattle->m_kColors[0].r() = 0xFF; spPattle->m_kColors[0].g() = 0xFF; spPattle->m_kColors[0].b() = 0xFF; spPattle->m_kColors[1].r() = 0x00; spPattle->m_kColors[1].g() = 0x00; spPattle->m_kColors[1].b() = 0x00; } spSurface->SetPalette(spPattle); } if (spSurface->m_i32Width && spSurface->m_i32Height) { VeSizeT stSize = spSurface->m_i32Height * spSurface->m_i32Pitch; spSurface->m_spPixels = VE_NEW VeBlob(stSize); VeZeroMemory(*spSurface->m_spPixels, stSize); } return spSurface; }
void UILayerManager::PopTopLayer() { if(m_kTopLayerStack.Size()) { Layer& kLayer = m_kTopLayerStack.Back(); VE_ASSERT(kLayer.m_tFirst); removeChild(kLayer.m_tFirst, false); m_kTopLayerStack.PopBack(); } }
//-------------------------------------------------------------------------- void Connection::UpdateForeground() { for(VeUInt32 i(0); i < m_kPacketCache.Size(); ++i) { Packet* pkPacket = m_kPacketCache[i]; BitStream kStream((unsigned char*)pkPacket->data, pkPacket->length, false); VeUInt8 u8Event; kStream >> u8Event; switch(u8Event) { case ID_DISCONNECTION_NOTIFICATION: ConnectFailed(CONNECT_FAIL_NORMAL); break; case ID_REMOTE_CONNECTION_LOST: ConnectFailed(CONNECT_FAIL_NORMAL); break; case ID_CONNECTION_BANNED: ConnectFailed(CONNECT_FAIL_SERVER_FULL); break; case ID_NO_FREE_INCOMING_CONNECTIONS: ConnectFailed(CONNECT_FAIL_SERVER_FULL); break; case ID_CONNECTION_LOST: ConnectFailed(CONNECT_FAIL_NORMAL); break; case ID_CONNECTION_REQUEST_ACCEPTED: VE_ASSERT(!(m_pkServer->IsServerConnected())); m_pkServer->SetServerID(pkPacket->systemAddress); m_pkServer->OnConnect(); break; case ID_CONNECTION_ATTEMPT_FAILED: ConnectFailed(CONNECT_FAIL_NORMAL); break; case ID_CLIENT_GLOBAL_RPC: m_pkServer->OnGlobalRPCCallback(kStream); break; case ID_CLIENT_ENTITY_RPC: m_pkServer->OnEntityRPCCallback(kStream); break; case ID_CLIENT_ENTITY_UPDATE: m_pkServer->EntityUpdate(kStream); break; case ID_CLIENT_ENTITY_DESTORY: m_pkServer->EntityDestory(kStream); break; case ID_CLIENT_FORCE_DISCONNECT: ForceDisconnect(CONNECT_FORCE_OFF); break; default: break; } m_pkPeer->DeallocatePacket(pkPacket); } m_kPacketCache.Clear(); }
//-------------------------------------------------------------------------- void WindowsVideoDevice::_SetWindowFullscreen(VeWindow::Data* pkWindow, VeVideoDisplay* pkDisplay, VE_BOOL bFullscreen) noexcept { VE_ASSERT(pkWindow); VeWindowData* pkData = (VeWindowData*)pkWindow->m_spDriverdata; HWND hWnd = pkData->m_hWnd; RECT kRect; VeRect kBounds; DWORD dwStyle; HWND hTop; BOOL bMenu; VeInt32 x, y; VeInt32 w, h; if ((pkWindow->m_u32Flags & (VE_WINDOW_FULLSCREEN | VE_WINDOW_INPUT_FOCUS)) == (VE_WINDOW_FULLSCREEN | VE_WINDOW_INPUT_FOCUS)) { hTop = HWND_TOPMOST; } else { hTop = HWND_NOTOPMOST; } dwStyle = GetWindowLong(hWnd, GWL_STYLE); dwStyle &= ~STYLE_MASK; dwStyle |= GetWindowStyle(pkWindow); _GetDisplayBounds(pkDisplay, &kBounds); if (bFullscreen) { x = kBounds.x; y = kBounds.y; w = kBounds.w; h = kBounds.h; } else { kRect.left = 0; kRect.top = 0; kRect.right = pkWindow->m_kWindowed.w; kRect.bottom = pkWindow->m_kWindowed.h; bMenu = (dwStyle & WS_CHILDWINDOW) ? FALSE : (GetMenu(hWnd) != nullptr); AdjustWindowRectEx(&kRect, dwStyle, bMenu, 0); w = (kRect.right - kRect.left); h = (kRect.bottom - kRect.top); x = pkWindow->m_kWindowed.x + kRect.left; y = pkWindow->m_kWindowed.y + kRect.top; } SetWindowLong(hWnd, GWL_STYLE, dwStyle); pkData->m_bExpectedResize = TRUE; SetWindowPos(hWnd, hTop, x, y, w, h, SWP_NOCOPYBITS | SWP_NOACTIVATE); pkData->m_bExpectedResize = FALSE; }
//-------------------------------------------------------------------------- void EntityS::SyncAllToClient() { if(m_pkAgent) { BitStream kStream; kStream.Reset(); kStream << VeUInt8(ID_CLIENT_ENTITY_UPDATE); kStream << (const VeChar8*)m_kName; VE_ASSERT(m_kSyncChildren.Size() <= VE_UINT16_MAX); kStream << VeUInt16(m_kSyncChildren.Size()); for(VeUInt32 i(0); i < m_kSyncChildren.Size(); ++i) { PropertySValue* pkValue = m_kSyncChildren[i]; kStream << pkValue->GetField(); VE_ASSERT(pkValue->GetSize() <= 0xff); kStream.WriteAlignedBytesSafe((const VeChar8*)pkValue->GetClientData(), pkValue->GetClientSize(), VE_UINT8_MAX); } m_pkAgent->SendPacket(kStream); } }
//-------------------------------------------------------------------------- VeRenderWindowPtr VeRenderer::CreateRenderWindow(const char* pcTitle, int32_t w, int32_t h, int32_t x, int32_t y, uint32_t u32Flags) noexcept { VeDesktopVideoPtr spVideo = VeDynamicCast(VeDesktopVideo, venus3d.GetVideo()); if (!spVideo) return nullptr; VeDesktopWindowPtr spWindow = spVideo->Create(pcTitle, w, h, x, y, u32Flags); VE_ASSERT(spWindow); VeRenderWindowPtr spRes = CreateRenderWindow(spWindow); spWindow = nullptr; return spRes; }
//-------------------------------------------------------------------------- bool WindowsMouse::_CreateSystemCursor(VeCursor::Data* pkCur, VeSystemCursor eCursor) noexcept { VE_ASSERT(pkCur); LPCTSTR name; switch (eCursor) { case VE_SYSTEM_CURSOR_ARROW: name = IDC_ARROW; break; case VE_SYSTEM_CURSOR_IBEAM: name = IDC_IBEAM; break; case VE_SYSTEM_CURSOR_WAIT: name = IDC_WAIT; break; case VE_SYSTEM_CURSOR_CROSSHAIR: name = IDC_CROSS; break; case VE_SYSTEM_CURSOR_WAITARROW: name = IDC_WAIT; break; case VE_SYSTEM_CURSOR_SIZENWSE: name = IDC_SIZENWSE; break; case VE_SYSTEM_CURSOR_SIZENESW: name = IDC_SIZENESW; break; case VE_SYSTEM_CURSOR_SIZEWE: name = IDC_SIZEWE; break; case VE_SYSTEM_CURSOR_SIZENS: name = IDC_SIZENS; break; case VE_SYSTEM_CURSOR_SIZEALL: name = IDC_SIZEALL; break; case VE_SYSTEM_CURSOR_NO: name = IDC_NO; break; case VE_SYSTEM_CURSOR_HAND: name = IDC_HAND; break; default: return false; } HICON hicon; hicon = LoadCursor(nullptr, name); pkCur->m_pvDriverdata = hicon; return true; }
void UILayerManager::PopTopLayerAndDeavtive(const UIUserDataPtr& spUserData) { if(m_kTopLayerStack.Size()) { Layer& kLayer = m_kTopLayerStack.Back(); VE_ASSERT(kLayer.m_tFirst); removeChild(kLayer.m_tFirst, false); m_kTopLayerStack.PopBack(); kLayer.m_tFirst->SetActive(false, spUserData); } }
void UILayerManager::PopLayer() { if(m_kLayerStack.Size()) { Layer& kLayer = m_kLayerStack.Back(); VE_ASSERT(kLayer.m_tFirst); removeChild(kLayer.m_tFirst, false); m_kLayerStack.PopBack(); kLayer.m_tFirst->SetActive(false, NULL); } }