no message
This commit is contained in:
363
cocos/platform/win32/FileUtils-win32.cpp
Normal file
363
cocos/platform/win32/FileUtils-win32.cpp
Normal file
@@ -0,0 +1,363 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2010-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/FileUtils-win32.h"
|
||||
#include <Shlobj.h>
|
||||
#include <cstdlib>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
#include "base/Log.h"
|
||||
#include "base/memory/Memory.h"
|
||||
#include "platform/win32/Utils-win32.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace cc {
|
||||
|
||||
#define CC_MAX_PATH 512
|
||||
|
||||
// The root path of resources, the character encoding is UTF-8.
|
||||
// UTF-8 is the only encoding supported by cocos2d-x API.
|
||||
static ccstd::string s_resourcePath = "";
|
||||
|
||||
// D:\aaa\bbb\ccc\ddd\abc.txt --> D:/aaa/bbb/ccc/ddd/abc.txt
|
||||
static inline ccstd::string convertPathFormatToUnixStyle(const ccstd::string &path) {
|
||||
ccstd::string ret = path;
|
||||
size_t len = ret.length();
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
if (ret[i] == '\\') {
|
||||
ret[i] = '/';
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void _checkPath() {
|
||||
if (s_resourcePath.empty()) {
|
||||
WCHAR utf16Path[CC_MAX_PATH] = {0};
|
||||
GetModuleFileNameW(NULL, utf16Path, CC_MAX_PATH - 1);
|
||||
WCHAR *pUtf16ExePath = &(utf16Path[0]);
|
||||
|
||||
// We need only directory part without exe
|
||||
WCHAR *pUtf16DirEnd = wcsrchr(pUtf16ExePath, L'\\');
|
||||
|
||||
char utf8ExeDir[CC_MAX_PATH] = {0};
|
||||
int nNum = WideCharToMultiByte(CP_UTF8, 0, pUtf16ExePath, static_cast<int>(pUtf16DirEnd - pUtf16ExePath + 1), utf8ExeDir, sizeof(utf8ExeDir), nullptr, nullptr);
|
||||
|
||||
s_resourcePath = convertPathFormatToUnixStyle(utf8ExeDir);
|
||||
}
|
||||
}
|
||||
|
||||
FileUtils *createFileUtils() {
|
||||
// TODO(qgh):In the simulator, it will be called twice. So the judgment here is to prevent memory leaks.
|
||||
// But this is equivalent to using a singleton pattern,
|
||||
// which is not consistent with the current design and will be optimized later.
|
||||
if (!FileUtils::getInstance()) {
|
||||
return ccnew FileUtilsWin32();
|
||||
}
|
||||
return FileUtils::getInstance();
|
||||
}
|
||||
|
||||
FileUtilsWin32::FileUtilsWin32() {
|
||||
init();
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::init() {
|
||||
_checkPath();
|
||||
_defaultResRootPath = s_resourcePath;
|
||||
return FileUtils::init();
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::isDirectoryExistInternal(const ccstd::string &dirPath) const {
|
||||
unsigned long fAttrib = GetFileAttributes(StringUtf8ToWideChar(dirPath).c_str());
|
||||
if (fAttrib != INVALID_FILE_ATTRIBUTES &&
|
||||
(fAttrib & FILE_ATTRIBUTE_DIRECTORY)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
ccstd::string FileUtilsWin32::getSuitableFOpen(const ccstd::string &filenameUtf8) const {
|
||||
return UTF8StringToMultiByte(filenameUtf8);
|
||||
}
|
||||
|
||||
long FileUtilsWin32::getFileSize(const ccstd::string &filepath) {
|
||||
WIN32_FILE_ATTRIBUTE_DATA fad;
|
||||
if (!GetFileAttributesEx(StringUtf8ToWideChar(filepath).c_str(), GetFileExInfoStandard, &fad)) {
|
||||
return 0; // error condition, could call GetLastError to find out more
|
||||
}
|
||||
LARGE_INTEGER size;
|
||||
size.HighPart = fad.nFileSizeHigh;
|
||||
size.LowPart = fad.nFileSizeLow;
|
||||
return (long)size.QuadPart;
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::isFileExistInternal(const ccstd::string &strFilePath) const {
|
||||
if (strFilePath.empty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ccstd::string strPath = strFilePath;
|
||||
if (!isAbsolutePath(strPath)) { // Not absolute path, add the default root path at the beginning.
|
||||
strPath.insert(0, _defaultResRootPath);
|
||||
}
|
||||
|
||||
DWORD attr = GetFileAttributesW(StringUtf8ToWideChar(strPath).c_str());
|
||||
if (attr == INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
|
||||
return false; // not a file
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::isAbsolutePath(const ccstd::string &strPath) const {
|
||||
if ((strPath.length() > 2 && ((strPath[0] >= 'a' && strPath[0] <= 'z') || (strPath[0] >= 'A' && strPath[0] <= 'Z')) && strPath[1] == ':') || (strPath[0] == '/' && strPath[1] == '/')) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
FileUtils::Status FileUtilsWin32::getContents(const ccstd::string &filename, ResizableBuffer *buffer) {
|
||||
if (filename.empty())
|
||||
return FileUtils::Status::NOT_EXISTS;
|
||||
|
||||
// read the file from hardware
|
||||
ccstd::string fullPath = FileUtils::getInstance()->fullPathForFilename(filename);
|
||||
|
||||
HANDLE fileHandle = ::CreateFile(StringUtf8ToWideChar(fullPath).c_str(), GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, NULL, nullptr);
|
||||
if (fileHandle == INVALID_HANDLE_VALUE)
|
||||
return FileUtils::Status::OPEN_FAILED;
|
||||
|
||||
DWORD hi;
|
||||
auto size = ::GetFileSize(fileHandle, &hi);
|
||||
if (hi > 0) {
|
||||
::CloseHandle(fileHandle);
|
||||
return FileUtils::Status::TOO_LARGE;
|
||||
}
|
||||
// don't read file content if it is empty
|
||||
if (size == 0) {
|
||||
::CloseHandle(fileHandle);
|
||||
return FileUtils::Status::OK;
|
||||
}
|
||||
|
||||
buffer->resize(size);
|
||||
DWORD sizeRead = 0;
|
||||
BOOL successed = ::ReadFile(fileHandle, buffer->buffer(), size, &sizeRead, nullptr);
|
||||
::CloseHandle(fileHandle);
|
||||
|
||||
if (!successed) {
|
||||
CC_LOG_DEBUG("Get data from file(%s) failed, error code is %s", filename.data(), std::to_string(::GetLastError()).data());
|
||||
buffer->resize(sizeRead);
|
||||
return FileUtils::Status::READ_FAILED;
|
||||
}
|
||||
return FileUtils::Status::OK;
|
||||
}
|
||||
|
||||
ccstd::string FileUtilsWin32::getPathForFilename(const ccstd::string &filename, const ccstd::string &searchPath) const {
|
||||
ccstd::string unixFileName = convertPathFormatToUnixStyle(filename);
|
||||
ccstd::string unixSearchPath = convertPathFormatToUnixStyle(searchPath);
|
||||
|
||||
return FileUtils::getPathForFilename(unixFileName, unixSearchPath);
|
||||
}
|
||||
|
||||
ccstd::string FileUtilsWin32::getFullPathForDirectoryAndFilename(const ccstd::string &strDirectory, const ccstd::string &strFilename) const {
|
||||
ccstd::string unixDirectory = convertPathFormatToUnixStyle(strDirectory);
|
||||
ccstd::string unixFilename = convertPathFormatToUnixStyle(strFilename);
|
||||
|
||||
return FileUtils::getFullPathForDirectoryAndFilename(unixDirectory, unixFilename);
|
||||
}
|
||||
|
||||
string FileUtilsWin32::getWritablePath() const {
|
||||
if (_writablePath.length()) {
|
||||
return _writablePath;
|
||||
}
|
||||
|
||||
// Get full path of executable, e.g. c:\Program Files (x86)\My Game Folder\MyGame.exe
|
||||
WCHAR full_path[CC_MAX_PATH + 1] = {0};
|
||||
::GetModuleFileName(nullptr, full_path, CC_MAX_PATH + 1);
|
||||
|
||||
// Debug app uses executable directory; Non-debug app uses local app data directory
|
||||
//#ifndef _DEBUG
|
||||
// Get filename of executable only, e.g. MyGame.exe
|
||||
WCHAR *base_name = wcsrchr(full_path, '\\');
|
||||
wstring retPath;
|
||||
if (base_name) {
|
||||
WCHAR app_data_path[CC_MAX_PATH + 1];
|
||||
|
||||
// Get local app data directory, e.g. C:\Documents and Settings\username\Local Settings\Application Data
|
||||
if (SUCCEEDED(SHGetFolderPath(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, app_data_path))) {
|
||||
wstring ret(app_data_path);
|
||||
|
||||
// Adding executable filename, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame.exe
|
||||
ret += base_name;
|
||||
|
||||
// Remove ".exe" extension, e.g. C:\Documents and Settings\username\Local Settings\Application Data\MyGame
|
||||
ret = ret.substr(0, ret.rfind(L"."));
|
||||
|
||||
ret += L"\\";
|
||||
|
||||
// Create directory
|
||||
if (SUCCEEDED(SHCreateDirectoryEx(nullptr, ret.c_str(), nullptr))) {
|
||||
retPath = ret;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (retPath.empty())
|
||||
//#endif // not defined _DEBUG
|
||||
{
|
||||
// If fetching of local app data directory fails, use the executable one
|
||||
retPath = full_path;
|
||||
|
||||
// remove xxx.exe
|
||||
retPath = retPath.substr(0, retPath.rfind(L"\\") + 1);
|
||||
}
|
||||
|
||||
return convertPathFormatToUnixStyle(StringWideCharToUtf8(retPath));
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::renameFile(const ccstd::string &oldfullpath, const ccstd::string &newfullpath) {
|
||||
CC_ASSERT(!oldfullpath.empty());
|
||||
CC_ASSERT(!newfullpath.empty());
|
||||
|
||||
std::wstring _wNew = StringUtf8ToWideChar(newfullpath);
|
||||
std::wstring _wOld = StringUtf8ToWideChar(oldfullpath);
|
||||
|
||||
if (FileUtils::getInstance()->isFileExist(newfullpath)) {
|
||||
if (!DeleteFile(_wNew.c_str())) {
|
||||
CC_LOG_ERROR("Fail to delete file %s !Error code is 0x%x", newfullpath.c_str(), GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
if (MoveFile(_wOld.c_str(), _wNew.c_str())) {
|
||||
return true;
|
||||
} else {
|
||||
CC_LOG_ERROR("Fail to rename file %s to %s !Error code is 0x%x", oldfullpath.c_str(), newfullpath.c_str(), GetLastError());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::renameFile(const ccstd::string &path, const ccstd::string &oldname, const ccstd::string &name) {
|
||||
CC_ASSERT(!path.empty());
|
||||
ccstd::string oldPath = path + oldname;
|
||||
ccstd::string newPath = path + name;
|
||||
|
||||
std::regex pat("\\/");
|
||||
ccstd::string _old = std::regex_replace(oldPath, pat, "\\");
|
||||
ccstd::string _new = std::regex_replace(newPath, pat, "\\");
|
||||
|
||||
return renameFile(_old, _new);
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::createDirectory(const ccstd::string &dirPath) {
|
||||
CC_ASSERT(!dirPath.empty());
|
||||
|
||||
if (isDirectoryExist(dirPath))
|
||||
return true;
|
||||
|
||||
std::wstring path = StringUtf8ToWideChar(dirPath);
|
||||
|
||||
// Split the path
|
||||
size_t start = 0;
|
||||
size_t found = path.find_first_of(L"/\\", start);
|
||||
std::wstring subpath;
|
||||
ccstd::vector<std::wstring> dirs;
|
||||
|
||||
if (found != std::wstring::npos) {
|
||||
while (true) {
|
||||
subpath = path.substr(start, found - start + 1);
|
||||
if (!subpath.empty())
|
||||
dirs.push_back(subpath);
|
||||
start = found + 1;
|
||||
found = path.find_first_of(L"/\\", start);
|
||||
if (found == std::wstring::npos) {
|
||||
if (start < path.length()) {
|
||||
dirs.push_back(path.substr(start));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ((GetFileAttributes(path.c_str())) == INVALID_FILE_ATTRIBUTES) {
|
||||
subpath = L"";
|
||||
for (unsigned int i = 0; i < dirs.size(); ++i) {
|
||||
subpath += dirs[i];
|
||||
|
||||
ccstd::string utf8Path = StringWideCharToUtf8(subpath);
|
||||
if (!isDirectoryExist(utf8Path)) {
|
||||
BOOL ret = CreateDirectory(subpath.c_str(), NULL);
|
||||
if (!ret && ERROR_ALREADY_EXISTS != GetLastError()) {
|
||||
CC_LOG_ERROR("Fail create directory %s !Error code is 0x%x", utf8Path.c_str(), GetLastError());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::removeFile(const ccstd::string &filepath) {
|
||||
std::regex pat("\\/");
|
||||
ccstd::string win32path = std::regex_replace(filepath, pat, "\\");
|
||||
|
||||
if (DeleteFile(StringUtf8ToWideChar(win32path).c_str())) {
|
||||
return true;
|
||||
} else {
|
||||
CC_LOG_ERROR("Fail remove file %s !Error code is 0x%x", filepath.c_str(), GetLastError());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool FileUtilsWin32::removeDirectory(const ccstd::string &dirPath) {
|
||||
std::wstring wpath = StringUtf8ToWideChar(dirPath);
|
||||
std::wstring files = wpath + L"*.*";
|
||||
WIN32_FIND_DATA wfd;
|
||||
HANDLE search = FindFirstFileEx(files.c_str(), FindExInfoStandard, &wfd, FindExSearchNameMatch, NULL, 0);
|
||||
bool ret = true;
|
||||
if (search != INVALID_HANDLE_VALUE) {
|
||||
BOOL find = true;
|
||||
while (find) {
|
||||
// Need check string . and .. for delete folders and files begin name.
|
||||
std::wstring fileName = wfd.cFileName;
|
||||
if (fileName != L"." && fileName != L"..") {
|
||||
std::wstring temp = wpath + wfd.cFileName;
|
||||
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
temp += '/';
|
||||
ret = ret && this->removeDirectory(StringWideCharToUtf8(temp));
|
||||
} else {
|
||||
SetFileAttributes(temp.c_str(), FILE_ATTRIBUTE_NORMAL);
|
||||
ret = ret && DeleteFile(temp.c_str());
|
||||
}
|
||||
}
|
||||
find = FindNextFile(search, &wfd);
|
||||
}
|
||||
FindClose(search);
|
||||
}
|
||||
if (ret && RemoveDirectory(wpath.c_str())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
123
cocos/platform/win32/FileUtils-win32.h
Normal file
123
cocos/platform/win32/FileUtils-win32.h
Normal file
@@ -0,0 +1,123 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2010-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base/Macros.h"
|
||||
#include "base/std/container/string.h"
|
||||
#include "platform/FileUtils.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
//! @brief Helper class to handle file operations
|
||||
class CC_DLL FileUtilsWin32 : public FileUtils {
|
||||
public:
|
||||
FileUtilsWin32();
|
||||
/* override functions */
|
||||
bool init();
|
||||
ccstd::string getWritablePath() const override;
|
||||
bool isAbsolutePath(const ccstd::string &strPath) const override;
|
||||
ccstd::string getSuitableFOpen(const ccstd::string &filenameUtf8) const override;
|
||||
long getFileSize(const ccstd::string &filepath);
|
||||
|
||||
protected:
|
||||
bool isFileExistInternal(const ccstd::string &strFilePath) const override;
|
||||
|
||||
/**
|
||||
* Renames a file under the given directory.
|
||||
*
|
||||
* @param path The parent directory path of the file, it must be an absolute path.
|
||||
* @param oldname The current name of the file.
|
||||
* @param name The new name of the file.
|
||||
* @return True if the file have been renamed successfully, false if not.
|
||||
*/
|
||||
bool renameFile(const ccstd::string &path, const ccstd::string &oldname, const ccstd::string &name) override;
|
||||
|
||||
/**
|
||||
* Renames a file under the given directory.
|
||||
*
|
||||
* @param oldfullpath The current path + name of the file.
|
||||
* @param newfullpath The new path + name of the file.
|
||||
* @return True if the file have been renamed successfully, false if not.
|
||||
*/
|
||||
bool renameFile(const ccstd::string &oldfullpath, const ccstd::string &newfullpath) override;
|
||||
|
||||
/**
|
||||
* Checks whether a directory exists without considering search paths and resolution orders.
|
||||
* @param dirPath The directory (with absolute path) to look up for
|
||||
* @return Returns true if the directory found at the given absolute path, otherwise returns false
|
||||
*/
|
||||
bool isDirectoryExistInternal(const ccstd::string &dirPath) const override;
|
||||
|
||||
/**
|
||||
* Removes a file.
|
||||
*
|
||||
* @param filepath The full path of the file, it must be an absolute path.
|
||||
* @return True if the file have been removed successfully, false if not.
|
||||
*/
|
||||
bool removeFile(const ccstd::string &filepath) override;
|
||||
|
||||
/**
|
||||
* Creates a directory.
|
||||
*
|
||||
* @param dirPath The path of the directory, it must be an absolute path.
|
||||
* @return True if the directory have been created successfully, false if not.
|
||||
*/
|
||||
bool createDirectory(const ccstd::string &dirPath) override;
|
||||
|
||||
/**
|
||||
* Removes a directory.
|
||||
*
|
||||
* @param dirPath The full path of the directory, it must be an absolute path.
|
||||
* @return True if the directory have been removed successfully, false if not.
|
||||
*/
|
||||
bool removeDirectory(const ccstd::string &dirPath) override;
|
||||
|
||||
FileUtils::Status getContents(const ccstd::string &filename, ResizableBuffer *buffer) override;
|
||||
|
||||
/**
|
||||
* Gets full path for filename, resolution directory and search path.
|
||||
*
|
||||
* @param filename The file name.
|
||||
* @param searchPath The search path.
|
||||
* @return The full path of the file. It will return an empty string if the full path of the file doesn't exist.
|
||||
*/
|
||||
ccstd::string getPathForFilename(const ccstd::string &filename, const ccstd::string &searchPath) const override;
|
||||
|
||||
/**
|
||||
* Gets full path for the directory and the filename.
|
||||
*
|
||||
* @note Only iOS and Mac need to override this method since they are using
|
||||
* `[[NSBundle mainBundle] pathForResource: ofType: inDirectory:]` to make a full path.
|
||||
* Other platforms will use the default implementation of this method.
|
||||
* @param directory The directory contains the file we are looking for.
|
||||
* @param filename The name of the file.
|
||||
* @return The full path of the file, if the file can't be found, it will return an empty string.
|
||||
*/
|
||||
ccstd::string getFullPathForDirectoryAndFilename(const ccstd::string &directory, const ccstd::string &filename) const override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
95
cocos/platform/win32/Utils-win32.cpp
Normal file
95
cocos/platform/win32/Utils-win32.cpp
Normal file
@@ -0,0 +1,95 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2010-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/Utils-win32.h"
|
||||
#include <sstream>
|
||||
#include "base/Log.h"
|
||||
#include "base/memory/Memory.h"
|
||||
#include "platform/StdC.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
std::wstring StringUtf8ToWideChar(const ccstd::string &strUtf8) {
|
||||
std::wstring ret;
|
||||
if (!strUtf8.empty()) {
|
||||
int nNum = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, nullptr, 0);
|
||||
if (nNum) {
|
||||
WCHAR *wideCharString = ccnew WCHAR[nNum + 1];
|
||||
wideCharString[0] = 0;
|
||||
|
||||
nNum = MultiByteToWideChar(CP_UTF8, 0, strUtf8.c_str(), -1, wideCharString, nNum + 1);
|
||||
|
||||
ret = wideCharString;
|
||||
delete[] wideCharString;
|
||||
} else {
|
||||
CC_LOG_DEBUG("Wrong convert to WideChar code:0x%x", GetLastError());
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
ccstd::string StringWideCharToUtf8(const std::wstring &strWideChar) {
|
||||
ccstd::string ret;
|
||||
if (!strWideChar.empty()) {
|
||||
int nNum = WideCharToMultiByte(CP_UTF8, 0, strWideChar.c_str(), -1, nullptr, 0, nullptr, FALSE);
|
||||
if (nNum) {
|
||||
char *utf8String = ccnew char[nNum + 1];
|
||||
utf8String[0] = 0;
|
||||
|
||||
nNum = WideCharToMultiByte(CP_UTF8, 0, strWideChar.c_str(), -1, utf8String, nNum + 1, nullptr, FALSE);
|
||||
|
||||
ret = utf8String;
|
||||
delete[] utf8String;
|
||||
} else {
|
||||
CC_LOG_DEBUG("Wrong convert to Utf8 code:0x%x", GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ccstd::string UTF8StringToMultiByte(const ccstd::string &strUtf8) {
|
||||
ccstd::string ret;
|
||||
if (!strUtf8.empty()) {
|
||||
std::wstring strWideChar = StringUtf8ToWideChar(strUtf8);
|
||||
int nNum = WideCharToMultiByte(CP_ACP, 0, strWideChar.c_str(), -1, nullptr, 0, nullptr, FALSE);
|
||||
if (nNum) {
|
||||
char *ansiString = ccnew char[nNum + 1];
|
||||
ansiString[0] = 0;
|
||||
|
||||
nNum = WideCharToMultiByte(CP_ACP, 0, strWideChar.c_str(), -1, ansiString, nNum + 1, nullptr, FALSE);
|
||||
|
||||
ret = ansiString;
|
||||
delete[] ansiString;
|
||||
} else {
|
||||
CC_LOG_DEBUG("Wrong convert to Ansi code:0x%x", GetLastError());
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
42
cocos/platform/win32/Utils-win32.h
Normal file
42
cocos/platform/win32/Utils-win32.h
Normal file
@@ -0,0 +1,42 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2010-2012 cocos2d-x.org
|
||||
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef __CC_UTILS_WIN32_H__
|
||||
#define __CC_UTILS_WIN32_H__
|
||||
|
||||
#include "base/Macros.h"
|
||||
|
||||
#include "base/std/container/string.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
std::wstring CC_DLL StringUtf8ToWideChar(const ccstd::string &strUtf8);
|
||||
ccstd::string CC_DLL StringWideCharToUtf8(const std::wstring &strWideChar);
|
||||
ccstd::string CC_DLL UTF8StringToMultiByte(const ccstd::string &strUtf8);
|
||||
|
||||
} // namespace cc
|
||||
|
||||
#endif // __CC_UTILS_WIN32_H__
|
||||
185
cocos/platform/win32/WindowsPlatform.cpp
Normal file
185
cocos/platform/win32/WindowsPlatform.cpp
Normal file
@@ -0,0 +1,185 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/WindowsPlatform.h"
|
||||
#include "platform/win32/modules/SystemWindowManager.h"
|
||||
|
||||
#include <Windows.h>
|
||||
#include <shellapi.h>
|
||||
#include <sstream>
|
||||
|
||||
#include "modules/Accelerometer.h"
|
||||
#include "modules/Battery.h"
|
||||
#include "modules/Network.h"
|
||||
#include "modules/System.h"
|
||||
#if defined(CC_SERVER_MODE)
|
||||
#include "platform/empty/modules/Screen.h"
|
||||
#include "platform/empty/modules/SystemWindow.h"
|
||||
#else
|
||||
#include "modules/Screen.h"
|
||||
#include "modules/SystemWindow.h"
|
||||
#endif
|
||||
#include "base/memory/Memory.h"
|
||||
#include "modules/Vibrator.h"
|
||||
|
||||
namespace {
|
||||
/**
|
||||
@brief This function changes the PVRFrame show/hide setting in register.
|
||||
@param bEnable If true show the PVRFrame window, otherwise hide.
|
||||
*/
|
||||
void PVRFrameEnableControlWindow(bool bEnable) {
|
||||
HKEY hKey = 0;
|
||||
|
||||
// Open PVRFrame control key, if not exist create it.
|
||||
if (ERROR_SUCCESS != RegCreateKeyExW(HKEY_CURRENT_USER,
|
||||
L"Software\\Imagination Technologies\\PVRVFRame\\STARTUP\\",
|
||||
0,
|
||||
0,
|
||||
REG_OPTION_NON_VOLATILE,
|
||||
KEY_ALL_ACCESS,
|
||||
0,
|
||||
&hKey,
|
||||
nullptr)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const WCHAR *wszValue = L"hide_gui";
|
||||
const WCHAR *wszNewData = (bEnable) ? L"NO" : L"YES";
|
||||
WCHAR wszOldData[256] = {0};
|
||||
DWORD dwSize = sizeof(wszOldData);
|
||||
LSTATUS status = RegQueryValueExW(hKey, wszValue, 0, nullptr, (LPBYTE)wszOldData, &dwSize);
|
||||
if (ERROR_FILE_NOT_FOUND == status // the key not exist
|
||||
|| (ERROR_SUCCESS == status // or the hide_gui value is exist
|
||||
&& 0 != wcscmp(wszNewData, wszOldData))) // but new data and old data not equal
|
||||
{
|
||||
dwSize = static_cast<DWORD>(sizeof(WCHAR) * (wcslen(wszNewData) + 1));
|
||||
RegSetValueEx(hKey, wszValue, 0, REG_SZ, (const BYTE *)wszNewData, dwSize);
|
||||
}
|
||||
|
||||
RegCloseKey(hKey);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace cc {
|
||||
WindowsPlatform::WindowsPlatform() {
|
||||
}
|
||||
WindowsPlatform::~WindowsPlatform() {
|
||||
#ifdef USE_WIN32_CONSOLE
|
||||
FreeConsole();
|
||||
#endif
|
||||
}
|
||||
|
||||
int32_t WindowsPlatform::init() {
|
||||
registerInterface(std::make_shared<Accelerometer>());
|
||||
registerInterface(std::make_shared<Battery>());
|
||||
registerInterface(std::make_shared<Network>());
|
||||
registerInterface(std::make_shared<Screen>());
|
||||
registerInterface(std::make_shared<System>());
|
||||
_windowManager = std::make_shared<SystemWindowManager>();
|
||||
registerInterface(_windowManager);
|
||||
registerInterface(std::make_shared<Vibrator>());
|
||||
|
||||
#ifdef USE_WIN32_CONSOLE
|
||||
AllocConsole();
|
||||
freopen("CONIN$", "r", stdin);
|
||||
freopen("CONOUT$", "w", stdout);
|
||||
freopen("CONOUT$", "w", stderr);
|
||||
#endif
|
||||
|
||||
PVRFrameEnableControlWindow(false);
|
||||
|
||||
return _windowManager->init();
|
||||
}
|
||||
|
||||
void WindowsPlatform::exit() {
|
||||
_quit = true;
|
||||
}
|
||||
|
||||
int32_t WindowsPlatform::loop() {
|
||||
#if CC_EDITOR
|
||||
_windowManager->processEvent();
|
||||
runTask();
|
||||
#else
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
/////////////// changing timer resolution
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
UINT TARGET_RESOLUTION = 1; // 1 millisecond target resolution
|
||||
TIMECAPS tc;
|
||||
UINT wTimerRes = 0;
|
||||
if (TIMERR_NOERROR == timeGetDevCaps(&tc, sizeof(TIMECAPS))) {
|
||||
wTimerRes = std::min(std::max(tc.wPeriodMin, TARGET_RESOLUTION), tc.wPeriodMax);
|
||||
timeBeginPeriod(wTimerRes);
|
||||
}
|
||||
|
||||
float dt = 0.f;
|
||||
const DWORD _16ms = 16;
|
||||
|
||||
// Main message loop:
|
||||
LARGE_INTEGER nFreq;
|
||||
LARGE_INTEGER nLast;
|
||||
LARGE_INTEGER nNow;
|
||||
|
||||
LONGLONG actualInterval = 0LL; // actual frame internal
|
||||
LONGLONG desiredInterval = 0LL; // desired frame internal, 1 / fps
|
||||
LONG waitMS = 0L;
|
||||
|
||||
QueryPerformanceCounter(&nLast);
|
||||
QueryPerformanceFrequency(&nFreq);
|
||||
|
||||
onResume();
|
||||
while (!_quit) {
|
||||
desiredInterval = (LONGLONG)(1.0 / getFps() * nFreq.QuadPart);
|
||||
_windowManager->processEvent();
|
||||
|
||||
QueryPerformanceCounter(&nNow);
|
||||
actualInterval = nNow.QuadPart - nLast.QuadPart;
|
||||
if (actualInterval >= desiredInterval) {
|
||||
nLast.QuadPart = nNow.QuadPart;
|
||||
runTask();
|
||||
} else {
|
||||
// The precision of timer on Windows is set to highest (1ms) by 'timeBeginPeriod' from above code,
|
||||
// but it's still not precise enough. For example, if the precision of timer is 1ms,
|
||||
// Sleep(3) may make a sleep of 2ms or 4ms. Therefore, we subtract 1ms here to make Sleep time shorter.
|
||||
// If 'waitMS' is equal or less than 1ms, don't sleep and run into next loop to
|
||||
// boost CPU to next frame accurately.
|
||||
waitMS = static_cast<LONG>((desiredInterval - actualInterval) * 1000LL / nFreq.QuadPart - 1L);
|
||||
if (waitMS > 1L)
|
||||
Sleep(waitMS);
|
||||
}
|
||||
}
|
||||
|
||||
if (wTimerRes != 0)
|
||||
timeEndPeriod(wTimerRes);
|
||||
|
||||
onDestroy();
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
ISystemWindow *WindowsPlatform::createNativeWindow(uint32_t windowId, void *externalHandle) {
|
||||
return ccnew SystemWindow(windowId, externalHandle);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
58
cocos/platform/win32/WindowsPlatform.h
Normal file
58
cocos/platform/win32/WindowsPlatform.h
Normal file
@@ -0,0 +1,58 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "platform/UniversalPlatform.h"
|
||||
|
||||
namespace cc {
|
||||
class SystemWindow;
|
||||
class SystemWindowManager;
|
||||
|
||||
class CC_DLL WindowsPlatform : public UniversalPlatform {
|
||||
public:
|
||||
WindowsPlatform();
|
||||
/**
|
||||
* Destructor of WindowPlatform.
|
||||
*/
|
||||
~WindowsPlatform() override;
|
||||
/**
|
||||
* Implementation of Windows platform initialization.
|
||||
*/
|
||||
int32_t init() override;
|
||||
|
||||
int32_t loop() override;
|
||||
|
||||
void exit() override;
|
||||
|
||||
ISystemWindow *createNativeWindow(uint32_t windowId, void *externalHandle) override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<SystemWindowManager> _windowManager{nullptr};
|
||||
std::shared_ptr<SystemWindow> _window{nullptr};
|
||||
bool _quit{false};
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
270
cocos/platform/win32/compat/stdint.h
Normal file
270
cocos/platform/win32/compat/stdint.h
Normal file
@@ -0,0 +1,270 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
// ISO C9x compliant stdint.h for Microsoft Visual Studio
|
||||
// Based on ISO/IEC 9899:TC2 Committee draft (May 6, 2005) WG14/N1124
|
||||
//
|
||||
// Copyright (c) 2006-2008 Alexander Chemeris
|
||||
//
|
||||
// Redistribution and use in source and binary forms, with or without
|
||||
// modification, are permitted provided that the following conditions are met:
|
||||
//
|
||||
// 1. Redistributions of source code must retain the above copyright notice,
|
||||
// this list of conditions and the following disclaimer.
|
||||
//
|
||||
// 2. Redistributions in binary form must reproduce the above copyright
|
||||
// notice, this list of conditions and the following disclaimer in the
|
||||
// documentation and/or other materials provided with the distribution.
|
||||
//
|
||||
// 3. The name of the author may be used to endorse or promote products
|
||||
// derived from this software without specific prior written permission.
|
||||
//
|
||||
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
// EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef _MSC_STDINT_H_ // [
|
||||
#define _MSC_STDINT_H_
|
||||
|
||||
#if CC_PLATFORM == CC_PLATFORM_WINDOWS
|
||||
|
||||
#ifndef _MSC_VER // [
|
||||
#error "Use this header only with Microsoft Visual C++ compilers!"
|
||||
#endif // _MSC_VER ]
|
||||
|
||||
#if _MSC_VER > 1000
|
||||
#pragma once
|
||||
#endif
|
||||
|
||||
#include <limits.h>
|
||||
|
||||
// For Visual Studio 6 in C++ mode and for many Visual Studio versions when
|
||||
// compiling for ARM we should wrap <wchar.h> include with 'extern "C++" {}'
|
||||
// or compiler give many errors like this:
|
||||
// error C2733: second C linkage of overloaded function 'wmemchr' not allowed
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <wchar.h>
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
// Define _W64 macros to mark types changing their size, like intptr_t.
|
||||
#ifndef _W64
|
||||
#if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300
|
||||
#define _W64 __w64
|
||||
#else
|
||||
#define _W64
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// 7.18.1 Integer types
|
||||
|
||||
// 7.18.1.1 Exact-width integer types
|
||||
|
||||
// Visual Studio 6 and Embedded Visual C++ 4 doesn't
|
||||
// realize that, e.g. char has the same size as __int8
|
||||
// so we give up on __intX for them.
|
||||
#if (_MSC_VER < 1300)
|
||||
typedef signed char int8_t;
|
||||
typedef signed short int16_t;
|
||||
typedef signed int int32_t;
|
||||
typedef unsigned char uint8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef unsigned int uint32_t;
|
||||
#else
|
||||
typedef signed __int8 int8_t;
|
||||
typedef signed __int16 int16_t;
|
||||
typedef signed __int32 int32_t;
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef unsigned __int16 uint16_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
#endif
|
||||
typedef signed __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
|
||||
// 7.18.1.2 Minimum-width integer types
|
||||
typedef int8_t int_least8_t;
|
||||
typedef int16_t int_least16_t;
|
||||
typedef int32_t int_least32_t;
|
||||
typedef int64_t int_least64_t;
|
||||
typedef uint8_t uint_least8_t;
|
||||
typedef uint16_t uint_least16_t;
|
||||
typedef uint32_t uint_least32_t;
|
||||
typedef uint64_t uint_least64_t;
|
||||
|
||||
// 7.18.1.3 Fastest minimum-width integer types
|
||||
typedef int8_t int_fast8_t;
|
||||
typedef int16_t int_fast16_t;
|
||||
typedef int32_t int_fast32_t;
|
||||
typedef int64_t int_fast64_t;
|
||||
typedef uint8_t uint_fast8_t;
|
||||
typedef uint16_t uint_fast16_t;
|
||||
typedef uint32_t uint_fast32_t;
|
||||
typedef uint64_t uint_fast64_t;
|
||||
|
||||
// 7.18.1.4 Integer types capable of holding object pointers
|
||||
#ifdef _WIN64 // [
|
||||
typedef signed __int64 intptr_t;
|
||||
typedef unsigned __int64 uintptr_t;
|
||||
#else // _WIN64 ][
|
||||
typedef _W64 signed int intptr_t;
|
||||
typedef _W64 unsigned int uintptr_t;
|
||||
#endif // _WIN64 ]
|
||||
|
||||
// 7.18.1.5 Greatest-width integer types
|
||||
typedef int64_t intmax_t;
|
||||
typedef uint64_t uintmax_t;
|
||||
|
||||
// 7.18.2 Limits of specified-width integer types
|
||||
|
||||
#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) // [ See footnote 220 at page 257 and footnote 221 at page 259
|
||||
|
||||
// 7.18.2.1 Limits of exact-width integer types
|
||||
#define INT8_MIN ((int8_t)_I8_MIN)
|
||||
#define INT8_MAX _I8_MAX
|
||||
#define INT16_MIN ((int16_t)_I16_MIN)
|
||||
#define INT16_MAX _I16_MAX
|
||||
#define INT32_MIN ((int32_t)_I32_MIN)
|
||||
#define INT32_MAX _I32_MAX
|
||||
#define INT64_MIN ((int64_t)_I64_MIN)
|
||||
#define INT64_MAX _I64_MAX
|
||||
#define UINT8_MAX _UI8_MAX
|
||||
#define UINT16_MAX _UI16_MAX
|
||||
#define UINT32_MAX _UI32_MAX
|
||||
#define UINT64_MAX _UI64_MAX
|
||||
|
||||
// 7.18.2.2 Limits of minimum-width integer types
|
||||
#define INT_LEAST8_MIN INT8_MIN
|
||||
#define INT_LEAST8_MAX INT8_MAX
|
||||
#define INT_LEAST16_MIN INT16_MIN
|
||||
#define INT_LEAST16_MAX INT16_MAX
|
||||
#define INT_LEAST32_MIN INT32_MIN
|
||||
#define INT_LEAST32_MAX INT32_MAX
|
||||
#define INT_LEAST64_MIN INT64_MIN
|
||||
#define INT_LEAST64_MAX INT64_MAX
|
||||
#define UINT_LEAST8_MAX UINT8_MAX
|
||||
#define UINT_LEAST16_MAX UINT16_MAX
|
||||
#define UINT_LEAST32_MAX UINT32_MAX
|
||||
#define UINT_LEAST64_MAX UINT64_MAX
|
||||
|
||||
// 7.18.2.3 Limits of fastest minimum-width integer types
|
||||
#define INT_FAST8_MIN INT8_MIN
|
||||
#define INT_FAST8_MAX INT8_MAX
|
||||
#define INT_FAST16_MIN INT16_MIN
|
||||
#define INT_FAST16_MAX INT16_MAX
|
||||
#define INT_FAST32_MIN INT32_MIN
|
||||
#define INT_FAST32_MAX INT32_MAX
|
||||
#define INT_FAST64_MIN INT64_MIN
|
||||
#define INT_FAST64_MAX INT64_MAX
|
||||
#define UINT_FAST8_MAX UINT8_MAX
|
||||
#define UINT_FAST16_MAX UINT16_MAX
|
||||
#define UINT_FAST32_MAX UINT32_MAX
|
||||
#define UINT_FAST64_MAX UINT64_MAX
|
||||
|
||||
// 7.18.2.4 Limits of integer types capable of holding object pointers
|
||||
#ifdef _WIN64 // [
|
||||
#define INTPTR_MIN INT64_MIN
|
||||
#define INTPTR_MAX INT64_MAX
|
||||
#define UINTPTR_MAX UINT64_MAX
|
||||
#else // _WIN64 ][
|
||||
#define INTPTR_MIN INT32_MIN
|
||||
#define INTPTR_MAX INT32_MAX
|
||||
#define UINTPTR_MAX UINT32_MAX
|
||||
#endif // _WIN64 ]
|
||||
|
||||
// 7.18.2.5 Limits of greatest-width integer types
|
||||
#define INTMAX_MIN INT64_MIN
|
||||
#define INTMAX_MAX INT64_MAX
|
||||
#define UINTMAX_MAX UINT64_MAX
|
||||
|
||||
// 7.18.3 Limits of other integer types
|
||||
|
||||
#ifdef _WIN64 // [
|
||||
#define PTRDIFF_MIN _I64_MIN
|
||||
#define PTRDIFF_MAX _I64_MAX
|
||||
#else // _WIN64 ][
|
||||
#define PTRDIFF_MIN _I32_MIN
|
||||
#define PTRDIFF_MAX _I32_MAX
|
||||
#endif // _WIN64 ]
|
||||
|
||||
#define SIG_ATOMIC_MIN INT_MIN
|
||||
#define SIG_ATOMIC_MAX INT_MAX
|
||||
|
||||
#ifndef SIZE_MAX // [
|
||||
#ifdef _WIN64 // [
|
||||
#define SIZE_MAX _UI64_MAX
|
||||
#else // _WIN64 ][
|
||||
#define SIZE_MAX _UI32_MAX
|
||||
#endif // _WIN64 ]
|
||||
#endif // SIZE_MAX ]
|
||||
|
||||
// WCHAR_MIN and WCHAR_MAX are also defined in <wchar.h>
|
||||
#ifndef WCHAR_MIN // [
|
||||
#define WCHAR_MIN 0
|
||||
#endif // WCHAR_MIN ]
|
||||
#ifndef WCHAR_MAX // [
|
||||
#define WCHAR_MAX _UI16_MAX
|
||||
#endif // WCHAR_MAX ]
|
||||
|
||||
#define WINT_MIN 0
|
||||
#define WINT_MAX _UI16_MAX
|
||||
|
||||
#endif // __STDC_LIMIT_MACROS ]
|
||||
|
||||
// 7.18.4 Limits of other integer types
|
||||
|
||||
#if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) // [ See footnote 224 at page 260
|
||||
|
||||
// 7.18.4.1 Macros for minimum-width integer constants
|
||||
|
||||
#define INT8_C(val) val##i8
|
||||
#define INT16_C(val) val##i16
|
||||
#define INT32_C(val) val##i32
|
||||
#define INT64_C(val) val##i64
|
||||
|
||||
#define UINT8_C(val) val##ui8
|
||||
#define UINT16_C(val) val##ui16
|
||||
#define UINT32_C(val) val##ui32
|
||||
#define UINT64_C(val) val##ui64
|
||||
|
||||
// 7.18.4.2 Macros for greatest-width integer constants
|
||||
#define INTMAX_C INT64_C
|
||||
#define UINTMAX_C UINT64_C
|
||||
|
||||
#endif // __STDC_CONSTANT_MACROS ]
|
||||
|
||||
#endif // CC_PLATFORM == CC_PLATFORM_WINDOWS
|
||||
|
||||
#endif // _MSC_STDINT_H_ ]
|
||||
39
cocos/platform/win32/modules/Accelerometer.cpp
Normal file
39
cocos/platform/win32/modules/Accelerometer.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/modules/Accelerometer.h"
|
||||
|
||||
namespace cc {
|
||||
void Accelerometer::setAccelerometerEnabled(bool isEnabled) {
|
||||
}
|
||||
|
||||
void Accelerometer::setAccelerometerInterval(float interval) {
|
||||
}
|
||||
|
||||
const Accelerometer::MotionValue &Accelerometer::getDeviceMotionValue() {
|
||||
static MotionValue motionValue;
|
||||
return motionValue;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
49
cocos/platform/win32/modules/Accelerometer.h
Normal file
49
cocos/platform/win32/modules/Accelerometer.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IAccelerometer.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL Accelerometer : public IAccelerometer {
|
||||
public:
|
||||
/**
|
||||
* To enable or disable accelerometer.
|
||||
*/
|
||||
void setAccelerometerEnabled(bool isEnabled) override;
|
||||
|
||||
/**
|
||||
* Sets the interval of accelerometer.
|
||||
*/
|
||||
void setAccelerometerInterval(float interval) override;
|
||||
|
||||
/**
|
||||
* Gets the motion value of current device.
|
||||
*/
|
||||
const MotionValue &getDeviceMotionValue() override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
33
cocos/platform/win32/modules/Battery.cpp
Normal file
33
cocos/platform/win32/modules/Battery.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/modules/Battery.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
float Battery::getBatteryLevel() const {
|
||||
return 1.0F;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
36
cocos/platform/win32/modules/Battery.h
Normal file
36
cocos/platform/win32/modules/Battery.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IBattery.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL Battery : public IBattery {
|
||||
public:
|
||||
float getBatteryLevel() const override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
@@ -0,0 +1,508 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2018-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/modules/CanvasRenderingContext2DDelegate.h"
|
||||
#include "base/memory/Memory.h"
|
||||
|
||||
namespace {
|
||||
void fillRectWithColor(uint8_t *buf, uint32_t totalWidth, uint32_t totalHeight, uint32_t x, uint32_t y, uint32_t width, uint32_t height, uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||
CC_ASSERT(x + width <= totalWidth);
|
||||
CC_ASSERT(y + height <= totalHeight);
|
||||
|
||||
uint32_t y0 = y;
|
||||
uint32_t y1 = y + height;
|
||||
uint8_t *p;
|
||||
for (uint32_t offsetY = y0; offsetY < y1; ++offsetY) {
|
||||
for (uint32_t offsetX = x; offsetX < (x + width); ++offsetX) {
|
||||
p = buf + (totalWidth * offsetY + offsetX) * 4;
|
||||
*p++ = r;
|
||||
*p++ = g;
|
||||
*p++ = b;
|
||||
*p++ = a;
|
||||
}
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
namespace cc {
|
||||
CanvasRenderingContext2DDelegate::CanvasRenderingContext2DDelegate() {
|
||||
HDC hdc = GetDC(_wnd);
|
||||
_DC = CreateCompatibleDC(hdc);
|
||||
ReleaseDC(_wnd, hdc);
|
||||
}
|
||||
|
||||
CanvasRenderingContext2DDelegate::~CanvasRenderingContext2DDelegate() {
|
||||
deleteBitmap();
|
||||
removeCustomFont();
|
||||
if (_DC)
|
||||
DeleteDC(_DC);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::recreateBuffer(float w, float h) {
|
||||
_bufferWidth = w;
|
||||
_bufferHeight = h;
|
||||
if (_bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
deleteBitmap();
|
||||
return;
|
||||
}
|
||||
|
||||
auto textureSize = static_cast<int>(_bufferWidth * _bufferHeight * 4);
|
||||
auto *data = static_cast<uint8_t *>(malloc(sizeof(uint8_t) * textureSize));
|
||||
memset(data, 0x00, textureSize);
|
||||
_imageData.fastSet(data, textureSize);
|
||||
|
||||
prepareBitmap(static_cast<int>(_bufferWidth), static_cast<int>(_bufferHeight));
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::beginPath() {
|
||||
// called: set_lineWidth() -> beginPath() -> moveTo() -> lineTo() -> stroke(), when draw line
|
||||
_hpen = CreatePen(PS_SOLID, static_cast<int>(_lineWidth), RGB(255, 255, 255));
|
||||
// the return value of SelectObject is a handle to the object being replaced, so we should delete them to avoid memory leak
|
||||
HGDIOBJ hOldPen = SelectObject(_DC, _hpen);
|
||||
HGDIOBJ hOldBmp = SelectObject(_DC, _bmp);
|
||||
DeleteObject(hOldPen);
|
||||
DeleteObject(hOldBmp);
|
||||
|
||||
SetBkMode(_DC, TRANSPARENT);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::closePath() {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::moveTo(float x, float y) {
|
||||
MoveToEx(_DC, static_cast<int>(x), static_cast<int>(-(y - _bufferHeight - _fontSize)), nullptr);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::lineTo(float x, float y) {
|
||||
LineTo(_DC, static_cast<int>(x), static_cast<int>(-(y - _bufferHeight - _fontSize)));
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::stroke() {
|
||||
DeleteObject(_hpen);
|
||||
if (_bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
return;
|
||||
}
|
||||
|
||||
fillTextureData();
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::saveContext() {
|
||||
_savedDC = SaveDC(_DC);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::restoreContext() {
|
||||
BOOL ret = RestoreDC(_DC, _savedDC);
|
||||
if (0 == ret) {
|
||||
SE_LOGD("CanvasRenderingContext2DImpl restore context failed.\n");
|
||||
}
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::clearRect(float /*x*/, float /*y*/, float w, float h) {
|
||||
if (_bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_imageData.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
recreateBuffer(w, h);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::fillRect(float x, float y, float w, float h) {
|
||||
if (_bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
return;
|
||||
}
|
||||
|
||||
//not filled all Bits in buffer? the buffer length is _bufferWidth * _bufferHeight * 4, but it filled _bufferWidth * _bufferHeight * 3?
|
||||
uint8_t *buffer = _imageData.getBytes();
|
||||
if (buffer) {
|
||||
uint8_t r = static_cast<uint8_t>(_fillStyle[0] * 255.0f);
|
||||
uint8_t g = static_cast<uint8_t>(_fillStyle[1] * 255.0f);
|
||||
uint8_t b = static_cast<uint8_t>(_fillStyle[2] * 255.0f);
|
||||
uint8_t a = static_cast<uint8_t>(_fillStyle[3] * 255.0f);
|
||||
fillRectWithColor(buffer,
|
||||
static_cast<uint32_t>(_bufferWidth),
|
||||
static_cast<uint32_t>(_bufferHeight),
|
||||
static_cast<uint32_t>(x),
|
||||
static_cast<uint32_t>(y),
|
||||
static_cast<uint32_t>(w),
|
||||
static_cast<uint32_t>(h), r, g, b, a);
|
||||
}
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::fillText(const ccstd::string &text, float x, float y, float /*maxWidth*/) {
|
||||
if (text.empty() || _bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
return;
|
||||
}
|
||||
|
||||
SIZE textSize = {0, 0};
|
||||
Point offsetPoint = convertDrawPoint(Point{x, y}, text);
|
||||
|
||||
drawText(text, (int)offsetPoint[0], (int)offsetPoint[1]);
|
||||
fillTextureData();
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::strokeText(const ccstd::string &text, float /*x*/, float /*y*/, float /*maxWidth*/) const {
|
||||
if (text.empty() || _bufferWidth < 1.0F || _bufferHeight < 1.0F) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
CanvasRenderingContext2DDelegate::Size CanvasRenderingContext2DDelegate::measureText(const ccstd::string &text) {
|
||||
if (text.empty())
|
||||
return ccstd::array<float, 2>{0.0f, 0.0f};
|
||||
|
||||
int bufferLen = 0;
|
||||
wchar_t *pwszBuffer = CanvasRenderingContext2DDelegate::utf8ToUtf16(text, &bufferLen);
|
||||
Size size = sizeWithText(pwszBuffer, bufferLen);
|
||||
//SE_LOGD("CanvasRenderingContext2DImpl::measureText: %s, %d, %d\n", text.c_str(), size.cx, size.cy);
|
||||
CC_SAFE_DELETE_ARRAY(pwszBuffer);
|
||||
return size;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::updateFont(const ccstd::string &fontName,
|
||||
float fontSize,
|
||||
bool bold,
|
||||
bool italic,
|
||||
bool /* oblique */,
|
||||
bool /* smallCaps */) {
|
||||
do {
|
||||
_fontName = fontName;
|
||||
_fontSize = static_cast<int>(fontSize);
|
||||
ccstd::string fontPath;
|
||||
LOGFONTA tFont = {0};
|
||||
if (!_fontName.empty()) {
|
||||
// firstly, try to create font from ttf file
|
||||
const auto &fontInfoMap = getFontFamilyNameMap();
|
||||
auto iter = fontInfoMap.find(_fontName);
|
||||
if (iter != fontInfoMap.end()) {
|
||||
fontPath = iter->second;
|
||||
ccstd::string tmpFontPath = fontPath;
|
||||
size_t nFindPos = tmpFontPath.rfind("/");
|
||||
tmpFontPath = &tmpFontPath[nFindPos + 1];
|
||||
nFindPos = tmpFontPath.rfind(".");
|
||||
// IDEA: draw ttf failed if font file name not equal font face name
|
||||
// for example: "DejaVuSansMono-Oblique" not equal "DejaVu Sans Mono" when using DejaVuSansMono-Oblique.ttf
|
||||
_fontName = tmpFontPath.substr(0, nFindPos);
|
||||
} else {
|
||||
auto nFindPos = fontName.rfind("/");
|
||||
if (nFindPos != fontName.npos) {
|
||||
if (fontName.length() == nFindPos + 1) {
|
||||
_fontName = "";
|
||||
} else {
|
||||
_fontName = &_fontName[nFindPos + 1];
|
||||
}
|
||||
}
|
||||
}
|
||||
tFont.lfCharSet = DEFAULT_CHARSET;
|
||||
strcpy_s(tFont.lfFaceName, LF_FACESIZE, _fontName.c_str());
|
||||
}
|
||||
|
||||
if (_fontSize) {
|
||||
tFont.lfHeight = -_fontSize;
|
||||
}
|
||||
|
||||
if (bold) {
|
||||
tFont.lfWeight = FW_BOLD;
|
||||
} else {
|
||||
tFont.lfWeight = FW_NORMAL;
|
||||
}
|
||||
|
||||
tFont.lfItalic = italic;
|
||||
|
||||
// disable Cleartype
|
||||
tFont.lfQuality = ANTIALIASED_QUALITY;
|
||||
|
||||
// delete old font
|
||||
removeCustomFont();
|
||||
|
||||
if (!fontPath.empty()) {
|
||||
_curFontPath = fontPath;
|
||||
wchar_t *pwszBuffer = utf8ToUtf16(_curFontPath);
|
||||
if (pwszBuffer) {
|
||||
if (AddFontResource(pwszBuffer)) {
|
||||
SendMessage(_wnd, WM_FONTCHANGE, 0, 0);
|
||||
}
|
||||
delete[] pwszBuffer;
|
||||
pwszBuffer = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// create new font
|
||||
_font = CreateFontIndirectA(&tFont);
|
||||
if (!_font) {
|
||||
// create failed, use default font
|
||||
SE_LOGE("Failed to create custom font(font name: %s, font size: %f), use default font.\n",
|
||||
_fontName.c_str(), fontSize);
|
||||
} else {
|
||||
SelectObject(_DC, _font);
|
||||
SendMessage(_wnd, WM_FONTCHANGE, 0, 0);
|
||||
}
|
||||
} while (false);
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setTextAlign(TextAlign align) {
|
||||
_textAlign = align;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setTextBaseline(TextBaseline baseline) {
|
||||
_textBaseLine = baseline;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setFillStyle(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||
_fillStyle = {r / 255.0F, g / 255.0F, b / 255.0F, a / 255.0F};
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setStrokeStyle(uint8_t r, uint8_t g, uint8_t b, uint8_t a) {
|
||||
_strokeStyle = {r / 255.0F, g / 255.0F, b / 255.0F, a / 255.0F};
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setLineWidth(float lineWidth) {
|
||||
_lineWidth = lineWidth;
|
||||
}
|
||||
|
||||
const cc::Data &CanvasRenderingContext2DDelegate::getDataRef() const {
|
||||
return _imageData;
|
||||
}
|
||||
|
||||
// change utf-8 string to utf-16, pRetLen is the string length after changing
|
||||
wchar_t *CanvasRenderingContext2DDelegate::utf8ToUtf16(const ccstd::string &str, int *pRetLen /* = nullptr*/) {
|
||||
wchar_t *pwszBuffer = nullptr;
|
||||
do {
|
||||
if (str.empty()) {
|
||||
break;
|
||||
}
|
||||
int nLen = static_cast<int>(str.size());
|
||||
int nBufLen = nLen + 1;
|
||||
pwszBuffer = ccnew wchar_t[nBufLen];
|
||||
CC_BREAK_IF(!pwszBuffer);
|
||||
memset(pwszBuffer, 0, sizeof(wchar_t) * nBufLen);
|
||||
// str.size() not equal actuallyLen for Chinese char
|
||||
int actuallyLen = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), nLen, pwszBuffer, nBufLen);
|
||||
// SE_LOGE("_utf8ToUtf16, str:%s, strLen:%d, retLen:%d\n", str.c_str(), str.size(), actuallyLen);
|
||||
if (pRetLen != nullptr) {
|
||||
*pRetLen = actuallyLen;
|
||||
}
|
||||
} while (false);
|
||||
return pwszBuffer;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::removeCustomFont() {
|
||||
HFONT hDefFont = (HFONT)GetStockObject(DEFAULT_GUI_FONT);
|
||||
if (hDefFont != _font) {
|
||||
DeleteObject(SelectObject(_DC, hDefFont));
|
||||
}
|
||||
// release temp font resource
|
||||
if (!_curFontPath.empty()) {
|
||||
wchar_t *pwszBuffer = utf8ToUtf16(_curFontPath);
|
||||
if (pwszBuffer) {
|
||||
RemoveFontResource(pwszBuffer);
|
||||
SendMessage(_wnd, WM_FONTCHANGE, 0, 0);
|
||||
delete[] pwszBuffer;
|
||||
pwszBuffer = nullptr;
|
||||
}
|
||||
_curFontPath.clear();
|
||||
}
|
||||
}
|
||||
|
||||
// x, y offset value
|
||||
int CanvasRenderingContext2DDelegate::drawText(const ccstd::string &text, int x, int y) {
|
||||
int nRet = 0;
|
||||
wchar_t *pwszBuffer = nullptr;
|
||||
do {
|
||||
CC_BREAK_IF(text.empty());
|
||||
|
||||
DWORD dwFmt = DT_SINGLELINE | DT_NOPREFIX;
|
||||
|
||||
int bufferLen = 0;
|
||||
pwszBuffer = utf8ToUtf16(text, &bufferLen);
|
||||
|
||||
Size newSize = sizeWithText(pwszBuffer, bufferLen);
|
||||
|
||||
_textSize = newSize;
|
||||
|
||||
RECT rcText = {0};
|
||||
|
||||
rcText.right = static_cast<int>(newSize[0]);
|
||||
rcText.bottom = static_cast<int>(newSize[1]);
|
||||
|
||||
LONG offsetX = x;
|
||||
LONG offsetY = y;
|
||||
if (offsetX || offsetY) {
|
||||
OffsetRect(&rcText, offsetX, offsetY);
|
||||
}
|
||||
|
||||
// SE_LOGE("_drawText text,%s size: (%d, %d) offset after convert: (%d, %d) \n", text.c_str(), newSize.cx, newSize.cy, offsetX, offsetY);
|
||||
|
||||
SetBkMode(_DC, TRANSPARENT);
|
||||
SetTextColor(_DC, RGB(255, 255, 255)); // white color
|
||||
|
||||
// draw text
|
||||
nRet = DrawTextW(_DC, pwszBuffer, bufferLen, &rcText, dwFmt);
|
||||
} while (false);
|
||||
CC_SAFE_DELETE_ARRAY(pwszBuffer);
|
||||
|
||||
return nRet;
|
||||
}
|
||||
|
||||
CanvasRenderingContext2DDelegate::Size CanvasRenderingContext2DDelegate::sizeWithText(const wchar_t *pszText, int nLen) {
|
||||
Size tRet{0, 0};
|
||||
do {
|
||||
CC_BREAK_IF(!pszText || nLen <= 0);
|
||||
|
||||
RECT rc = {0, 0, 0, 0};
|
||||
DWORD dwCalcFmt = DT_CALCRECT | DT_NOPREFIX;
|
||||
|
||||
// measure text size
|
||||
DrawTextW(_DC, pszText, nLen, &rc, dwCalcFmt);
|
||||
|
||||
tRet[0] = static_cast<float>(rc.right);
|
||||
tRet[1] = static_cast<float>(rc.bottom);
|
||||
} while (false);
|
||||
|
||||
return tRet;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::prepareBitmap(int nWidth, int nHeight) {
|
||||
// release bitmap
|
||||
deleteBitmap();
|
||||
|
||||
if (nWidth > 0 && nHeight > 0) {
|
||||
_bmp = CreateBitmap(nWidth, nHeight, 1, 32, nullptr);
|
||||
SelectObject(_DC, _bmp);
|
||||
}
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::deleteBitmap() {
|
||||
if (_bmp) {
|
||||
DeleteObject(_bmp);
|
||||
_bmp = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::fillTextureData() {
|
||||
do {
|
||||
auto dataLen = static_cast<int>(_bufferWidth * _bufferHeight * 4);
|
||||
auto *dataBuf = static_cast<unsigned char *>(malloc(sizeof(unsigned char) * dataLen));
|
||||
CC_BREAK_IF(!dataBuf);
|
||||
unsigned char *imageBuf = _imageData.getBytes();
|
||||
CC_BREAK_IF(!imageBuf);
|
||||
|
||||
struct
|
||||
{
|
||||
BITMAPINFOHEADER bmiHeader;
|
||||
int mask[4];
|
||||
} bi = {0};
|
||||
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
|
||||
CC_BREAK_IF(!GetDIBits(_DC, _bmp, 0, 0,
|
||||
nullptr, (LPBITMAPINFO)&bi, DIB_RGB_COLORS));
|
||||
|
||||
// copy pixel data
|
||||
bi.bmiHeader.biHeight = (bi.bmiHeader.biHeight > 0) ? -bi.bmiHeader.biHeight : bi.bmiHeader.biHeight;
|
||||
GetDIBits(_DC, _bmp, 0, static_cast<UINT>(_bufferHeight), dataBuf,
|
||||
(LPBITMAPINFO)&bi, DIB_RGB_COLORS);
|
||||
|
||||
uint8_t r = static_cast<uint8_t>(round(_fillStyle[0] * 255));
|
||||
uint8_t g = static_cast<uint8_t>(round(_fillStyle[1] * 255));
|
||||
uint8_t b = static_cast<uint8_t>(round(_fillStyle[2] * 255));
|
||||
COLORREF textColor = (b << 16 | g << 8 | r) & 0x00ffffff;
|
||||
COLORREF *pPixel = nullptr;
|
||||
COLORREF *pImage = nullptr;
|
||||
int bufferHeight = static_cast<int>(_bufferHeight);
|
||||
int bufferWidth = static_cast<int>(_bufferWidth);
|
||||
for (int y = 0; y < bufferHeight; ++y) {
|
||||
pPixel = (COLORREF *)dataBuf + y * bufferWidth;
|
||||
pImage = (COLORREF *)imageBuf + y * bufferWidth;
|
||||
for (int x = 0; x < bufferWidth; ++x) {
|
||||
COLORREF &clr = *pPixel;
|
||||
COLORREF &val = *pImage;
|
||||
// Because text is drawn in white color, and background color is black,
|
||||
// so the red value is equal to alpha value. And we should keep this value
|
||||
// as it includes anti-atlas information.
|
||||
uint8_t alpha = GetRValue(clr);
|
||||
if (alpha > 0) {
|
||||
val = (alpha << 24) | textColor;
|
||||
}
|
||||
++pPixel;
|
||||
++pImage;
|
||||
}
|
||||
}
|
||||
free(dataBuf);
|
||||
} while (false);
|
||||
}
|
||||
|
||||
ccstd::array<float, 2> CanvasRenderingContext2DDelegate::convertDrawPoint(Point point, const ccstd::string &text) {
|
||||
Size textSize = measureText(text);
|
||||
if (_textAlign == TextAlign::CENTER) {
|
||||
point[0] -= textSize[0] / 2.0f;
|
||||
} else if (_textAlign == TextAlign::RIGHT) {
|
||||
point[0] -= textSize[0];
|
||||
}
|
||||
|
||||
if (_textBaseLine == TextBaseline::TOP) {
|
||||
// DrawText default
|
||||
GetTextMetrics(_DC, &_tm);
|
||||
point[1] += -_tm.tmInternalLeading;
|
||||
} else if (_textBaseLine == TextBaseline::MIDDLE) {
|
||||
point[1] += -textSize[1] / 2.0f;
|
||||
} else if (_textBaseLine == TextBaseline::BOTTOM) {
|
||||
point[1] += -textSize[1];
|
||||
} else if (_textBaseLine == TextBaseline::ALPHABETIC) {
|
||||
GetTextMetrics(_DC, &_tm);
|
||||
point[1] -= _tm.tmAscent;
|
||||
}
|
||||
|
||||
return point;
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::fill() {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setLineCap(const ccstd::string & /* lineCap */) {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::setLineJoin(const ccstd::string & /* lineCap */) {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::fillImageData(const Data & /* imageData */,
|
||||
float /* imageWidth */,
|
||||
float /* imageHeight */,
|
||||
float /* offsetX */,
|
||||
float /* offsetY */) {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::strokeText(const ccstd::string & /* text */,
|
||||
float /* x */,
|
||||
float /* y */,
|
||||
float /* maxWidth */) {
|
||||
}
|
||||
|
||||
void CanvasRenderingContext2DDelegate::rect(float /* x */,
|
||||
float /* y */,
|
||||
float /* w */,
|
||||
float /* h */) {
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
121
cocos/platform/win32/modules/CanvasRenderingContext2DDelegate.h
Normal file
121
cocos/platform/win32/modules/CanvasRenderingContext2DDelegate.h
Normal file
@@ -0,0 +1,121 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2018-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/canvas/ICanvasRenderingContext2D.h"
|
||||
|
||||
#include <Windows.h>
|
||||
#include <cstdint>
|
||||
#include <regex>
|
||||
#include "base/csscolorparser.h"
|
||||
#include "base/std/container/array.h"
|
||||
#include "cocos/bindings/jswrapper/SeApi.h"
|
||||
#include "cocos/bindings/manual/jsb_platform.h"
|
||||
#include "math/Math.h"
|
||||
#include "platform/FileUtils.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL CanvasRenderingContext2DDelegate : public ICanvasRenderingContext2D::Delegate {
|
||||
public:
|
||||
using Point = ccstd::array<float, 2>;
|
||||
using Vec2 = ccstd::array<float, 2>;
|
||||
using Size = ccstd::array<float, 2>;
|
||||
using Color4F = ccstd::array<float, 4>;
|
||||
using TextAlign = ICanvasRenderingContext2D::TextAlign;
|
||||
using TextBaseline = ICanvasRenderingContext2D::TextBaseline;
|
||||
CanvasRenderingContext2DDelegate();
|
||||
~CanvasRenderingContext2DDelegate() override;
|
||||
|
||||
void recreateBuffer(float w, float h) override;
|
||||
void beginPath() override;
|
||||
void closePath() override;
|
||||
void moveTo(float x, float y) override;
|
||||
void lineTo(float x, float y) override;
|
||||
void stroke() override;
|
||||
void saveContext() override;
|
||||
void restoreContext() override;
|
||||
void clearRect(float /*x*/, float /*y*/, float w, float h) override;
|
||||
void fillRect(float x, float y, float w, float h) override;
|
||||
void fillText(const ccstd::string &text, float x, float y, float /*maxWidth*/) override;
|
||||
void strokeText(const ccstd::string &text, float /*x*/, float /*y*/, float /*maxWidth*/) const;
|
||||
Size measureText(const ccstd::string &text) override;
|
||||
void updateFont(const ccstd::string &fontName, float fontSize, bool bold, bool italic, bool oblique, bool smallCaps) override;
|
||||
void setTextAlign(TextAlign align) override;
|
||||
void setTextBaseline(TextBaseline baseline) override;
|
||||
void setFillStyle(uint8_t r, uint8_t g, uint8_t b, uint8_t a) override;
|
||||
void setStrokeStyle(uint8_t r, uint8_t g, uint8_t b, uint8_t a) override;
|
||||
void setLineWidth(float lineWidth) override;
|
||||
const cc::Data &getDataRef() const override;
|
||||
void fill() override;
|
||||
void setLineCap(const ccstd::string &lineCap) override;
|
||||
void setLineJoin(const ccstd::string &lineCap) override;
|
||||
void fillImageData(const Data &imageData, float imageWidth, float imageHeight, float offsetX, float offsetY) override;
|
||||
void strokeText(const ccstd::string &text, float /*x*/, float /*y*/, float /*maxWidth*/) override;
|
||||
void rect(float x, float y, float w, float h) override;
|
||||
void updateData() override {}
|
||||
void setShadowBlur(float blur) override {}
|
||||
void setShadowColor(uint8_t r, uint8_t g, uint8_t b, uint8_t a) override {}
|
||||
void setShadowOffsetX(float offsetX) override {}
|
||||
void setShadowOffsetY(float offsetY) override {}
|
||||
|
||||
private:
|
||||
static wchar_t *utf8ToUtf16(const ccstd::string &str, int *pRetLen = nullptr);
|
||||
void removeCustomFont();
|
||||
int drawText(const ccstd::string &text, int x, int y);
|
||||
Size sizeWithText(const wchar_t *pszText, int nLen);
|
||||
void prepareBitmap(int nWidth, int nHeight);
|
||||
void deleteBitmap();
|
||||
void fillTextureData();
|
||||
ccstd::array<float, 2> convertDrawPoint(Point point, const ccstd::string &text);
|
||||
|
||||
public:
|
||||
HDC _DC{nullptr};
|
||||
HBITMAP _bmp{nullptr};
|
||||
|
||||
private:
|
||||
cc::Data _imageData;
|
||||
HFONT _font{static_cast<HFONT>(GetStockObject(DEFAULT_GUI_FONT))};
|
||||
HWND _wnd{nullptr};
|
||||
HPEN _hpen;
|
||||
PAINTSTRUCT _paintStruct;
|
||||
ccstd::string _curFontPath;
|
||||
int _savedDC{0};
|
||||
float _lineWidth{0.0F};
|
||||
float _bufferWidth{0.0F};
|
||||
float _bufferHeight{0.0F};
|
||||
|
||||
ccstd::string _fontName;
|
||||
int _fontSize{0};
|
||||
Size _textSize;
|
||||
TextAlign _textAlign{TextAlign::CENTER};
|
||||
TextBaseline _textBaseLine{TextBaseline::TOP};
|
||||
Color4F _fillStyle;
|
||||
Color4F _strokeStyle;
|
||||
|
||||
TEXTMETRIC _tm;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
33
cocos/platform/win32/modules/Network.cpp
Normal file
33
cocos/platform/win32/modules/Network.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/modules/Network.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
INetwork::NetworkType Network::getNetworkType() const {
|
||||
return INetwork::NetworkType::LAN;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
36
cocos/platform/win32/modules/Network.h
Normal file
36
cocos/platform/win32/modules/Network.h
Normal file
@@ -0,0 +1,36 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/INetwork.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL Network : public INetwork {
|
||||
public:
|
||||
NetworkType getNetworkType() const override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
76
cocos/platform/win32/modules/Screen.cpp
Normal file
76
cocos/platform/win32/modules/Screen.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/modules/Screen.h"
|
||||
#include "base/Macros.h"
|
||||
#include "cocos/bindings/jswrapper/SeApi.h"
|
||||
|
||||
#include <Windows.h>
|
||||
|
||||
namespace cc {
|
||||
|
||||
int Screen::getDPI() const {
|
||||
static int dpi = -1;
|
||||
if (dpi == -1) {
|
||||
HDC hScreenDC = GetDC(nullptr);
|
||||
int PixelsX = GetDeviceCaps(hScreenDC, HORZRES);
|
||||
int MMX = GetDeviceCaps(hScreenDC, HORZSIZE);
|
||||
ReleaseDC(nullptr, hScreenDC);
|
||||
dpi = static_cast<int>(254.0f * PixelsX / MMX / 10);
|
||||
}
|
||||
return dpi;
|
||||
}
|
||||
|
||||
float Screen::getDevicePixelRatio() const {
|
||||
return 1;
|
||||
}
|
||||
|
||||
void Screen::setKeepScreenOn(bool value) {
|
||||
CC_UNUSED_PARAM(value);
|
||||
}
|
||||
|
||||
Screen::Orientation Screen::getDeviceOrientation() const {
|
||||
return Orientation::PORTRAIT;
|
||||
}
|
||||
|
||||
Vec4 Screen::getSafeAreaEdge() const {
|
||||
return cc::Vec4();
|
||||
}
|
||||
|
||||
bool Screen::isDisplayStats() {
|
||||
se::AutoHandleScope hs;
|
||||
se::Value ret;
|
||||
char commandBuf[100] = "cc.profiler.isShowingStats();";
|
||||
se::ScriptEngine::getInstance()->evalString(commandBuf, 100, &ret);
|
||||
return ret.toBoolean();
|
||||
}
|
||||
|
||||
void Screen::setDisplayStats(bool isShow) {
|
||||
se::AutoHandleScope hs;
|
||||
char commandBuf[100] = {0};
|
||||
sprintf(commandBuf, isShow ? "cc.profiler.showStats();" : "cc.profiler.hideStats();");
|
||||
se::ScriptEngine::getInstance()->evalString(commandBuf);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
50
cocos/platform/win32/modules/Screen.h
Normal file
50
cocos/platform/win32/modules/Screen.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IScreen.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL Screen : public IScreen {
|
||||
public:
|
||||
int getDPI() const override;
|
||||
float getDevicePixelRatio() const override;
|
||||
void setKeepScreenOn(bool value) override;
|
||||
Orientation getDeviceOrientation() const override;
|
||||
Vec4 getSafeAreaEdge() const override;
|
||||
/**
|
||||
@brief Get current display stats.
|
||||
@return bool, is displaying stats or not.
|
||||
*/
|
||||
bool isDisplayStats() override;
|
||||
|
||||
/**
|
||||
@brief set display stats information.
|
||||
*/
|
||||
void setDisplayStats(bool isShow) override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
155
cocos/platform/win32/modules/System.cpp
Normal file
155
cocos/platform/win32/modules/System.cpp
Normal file
@@ -0,0 +1,155 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/modules/System.h"
|
||||
#include <Windows.h>
|
||||
#include "SDL2/SDL_clipboard.h"
|
||||
#include "base/memory/Memory.h"
|
||||
namespace cc {
|
||||
using OSType = System::OSType;
|
||||
|
||||
OSType System::getOSType() const {
|
||||
return OSType::WINDOWS;
|
||||
}
|
||||
|
||||
ccstd::string System::getDeviceModel() const {
|
||||
return "Windows";
|
||||
}
|
||||
|
||||
System::LanguageType System::getCurrentLanguage() const {
|
||||
LanguageType ret = LanguageType::ENGLISH;
|
||||
|
||||
LCID localeID = GetUserDefaultLCID();
|
||||
unsigned short primaryLanguageID = localeID & 0xFF;
|
||||
|
||||
switch (primaryLanguageID) {
|
||||
case LANG_CHINESE:
|
||||
ret = LanguageType::CHINESE;
|
||||
break;
|
||||
case LANG_ENGLISH:
|
||||
ret = LanguageType::ENGLISH;
|
||||
break;
|
||||
case LANG_FRENCH:
|
||||
ret = LanguageType::FRENCH;
|
||||
break;
|
||||
case LANG_ITALIAN:
|
||||
ret = LanguageType::ITALIAN;
|
||||
break;
|
||||
case LANG_GERMAN:
|
||||
ret = LanguageType::GERMAN;
|
||||
break;
|
||||
case LANG_SPANISH:
|
||||
ret = LanguageType::SPANISH;
|
||||
break;
|
||||
case LANG_DUTCH:
|
||||
ret = LanguageType::DUTCH;
|
||||
break;
|
||||
case LANG_RUSSIAN:
|
||||
ret = LanguageType::RUSSIAN;
|
||||
break;
|
||||
case LANG_KOREAN:
|
||||
ret = LanguageType::KOREAN;
|
||||
break;
|
||||
case LANG_JAPANESE:
|
||||
ret = LanguageType::JAPANESE;
|
||||
break;
|
||||
case LANG_HUNGARIAN:
|
||||
ret = LanguageType::HUNGARIAN;
|
||||
break;
|
||||
case LANG_PORTUGUESE:
|
||||
ret = LanguageType::PORTUGUESE;
|
||||
break;
|
||||
case LANG_ARABIC:
|
||||
ret = LanguageType::ARABIC;
|
||||
break;
|
||||
case LANG_NORWEGIAN:
|
||||
ret = LanguageType::NORWEGIAN;
|
||||
break;
|
||||
case LANG_POLISH:
|
||||
ret = LanguageType::POLISH;
|
||||
break;
|
||||
case LANG_TURKISH:
|
||||
ret = LanguageType::TURKISH;
|
||||
break;
|
||||
case LANG_UKRAINIAN:
|
||||
ret = LanguageType::UKRAINIAN;
|
||||
break;
|
||||
case LANG_ROMANIAN:
|
||||
ret = LanguageType::ROMANIAN;
|
||||
break;
|
||||
case LANG_BULGARIAN:
|
||||
ret = LanguageType::BULGARIAN;
|
||||
break;
|
||||
case LANG_HINDI:
|
||||
ret = LanguageType::HINDI;
|
||||
break;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
ccstd::string System::getCurrentLanguageCode() const {
|
||||
LANGID lid = GetUserDefaultUILanguage();
|
||||
const LCID locale_id = MAKELCID(lid, SORT_DEFAULT);
|
||||
int length = GetLocaleInfoA(locale_id, LOCALE_SISO639LANGNAME, nullptr, 0);
|
||||
|
||||
char *tempCode = reinterpret_cast<char *>(CC_MALLOC(length));
|
||||
GetLocaleInfoA(locale_id, LOCALE_SISO639LANGNAME, tempCode, length);
|
||||
ccstd::string code(tempCode);
|
||||
CC_FREE(tempCode);
|
||||
|
||||
return code;
|
||||
}
|
||||
|
||||
ccstd::string System::getSystemVersion() const {
|
||||
char buff[256] = {0};
|
||||
HMODULE handle = GetModuleHandleW(L"ntdll.dll");
|
||||
if (handle) {
|
||||
typedef NTSTATUS(WINAPI * RtlGetVersionPtr)(PRTL_OSVERSIONINFOW);
|
||||
RtlGetVersionPtr getVersionPtr = (RtlGetVersionPtr)GetProcAddress(handle, "RtlGetVersion");
|
||||
if (getVersionPtr != NULL) {
|
||||
RTL_OSVERSIONINFOW info;
|
||||
if (getVersionPtr(&info) == 0) { /* STATUS_SUCCESS == 0 */
|
||||
snprintf(buff, sizeof(buff), "Windows version %d.%d.%d", info.dwMajorVersion, info.dwMinorVersion, info.dwBuildNumber);
|
||||
return buff;
|
||||
}
|
||||
}
|
||||
}
|
||||
return buff;
|
||||
}
|
||||
|
||||
bool System::openURL(const ccstd::string &url) {
|
||||
WCHAR *temp = ccnew WCHAR[url.size() + 1];
|
||||
int urlSize = static_cast<int>(url.size() + 1);
|
||||
MultiByteToWideChar(CP_UTF8, 0, url.c_str(), urlSize, temp, urlSize);
|
||||
HINSTANCE r = ShellExecuteW(NULL, L"open", temp, NULL, NULL, SW_SHOWNORMAL);
|
||||
delete[] temp;
|
||||
return (size_t)r > 32;
|
||||
}
|
||||
|
||||
void System::copyTextToClipboard(const ccstd::string &text) {
|
||||
SDL_SetClipboardText(text.c_str());
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
65
cocos/platform/win32/modules/System.h
Normal file
65
cocos/platform/win32/modules/System.h
Normal file
@@ -0,0 +1,65 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/ISystem.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL System : public ISystem {
|
||||
public:
|
||||
/**
|
||||
@brief Get target system type.
|
||||
*/
|
||||
OSType getOSType() const override;
|
||||
/**
|
||||
@brief Get target device model.
|
||||
*/
|
||||
ccstd::string getDeviceModel() const override;
|
||||
/**
|
||||
@brief Get current language config.
|
||||
@return Current language config.
|
||||
*/
|
||||
LanguageType getCurrentLanguage() const override;
|
||||
/**
|
||||
@brief Get current language iso 639-1 code.
|
||||
@return Current language iso 639-1 code.
|
||||
*/
|
||||
ccstd::string getCurrentLanguageCode() const override;
|
||||
/**
|
||||
@brief Get system version.
|
||||
@return system version.
|
||||
*/
|
||||
ccstd::string getSystemVersion() const override;
|
||||
/**
|
||||
@brief Open url in default browser.
|
||||
@param String with url to open.
|
||||
@return True if the resource located by the URL was successfully opened; otherwise false.
|
||||
*/
|
||||
bool openURL(const ccstd::string& url) override;
|
||||
void copyTextToClipboard(const std::string& text) override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
96
cocos/platform/win32/modules/SystemWindow.cpp
Normal file
96
cocos/platform/win32/modules/SystemWindow.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/modules/SystemWindow.h"
|
||||
#include <Windows.h>
|
||||
#include <functional>
|
||||
#include "base/Log.h"
|
||||
#include "engine/EngineEvents.h"
|
||||
#include "platform/SDLHelper.h"
|
||||
#include "platform/win32/WindowsPlatform.h"
|
||||
|
||||
namespace cc {
|
||||
SystemWindow::SystemWindow(uint32_t windowId, void *externalHandle)
|
||||
: _windowId(windowId) {
|
||||
if (externalHandle) {
|
||||
_windowHandle = reinterpret_cast<uintptr_t>(externalHandle);
|
||||
}
|
||||
}
|
||||
|
||||
SystemWindow::~SystemWindow() {
|
||||
_windowHandle = 0;
|
||||
_windowId = 0;
|
||||
}
|
||||
|
||||
bool SystemWindow::createWindow(const char *title,
|
||||
int w, int h, int flags) {
|
||||
_window = SDLHelper::createWindow(title, w, h, flags);
|
||||
if (!_window) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_width = w;
|
||||
_height = h;
|
||||
_windowHandle = SDLHelper::getWindowHandle(_window);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SystemWindow::createWindow(const char *title,
|
||||
int x, int y, int w,
|
||||
int h, int flags) {
|
||||
_window = SDLHelper::createWindow(title, x, y, w, h, flags);
|
||||
if (!_window) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_width = w;
|
||||
_height = h;
|
||||
_windowHandle = SDLHelper::getWindowHandle(_window);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void SystemWindow::closeWindow() {
|
||||
HWND windowHandle = reinterpret_cast<HWND>(getWindowHandle());
|
||||
if (windowHandle != 0) {
|
||||
::SendMessageA(windowHandle, WM_CLOSE, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t SystemWindow::getWindowId() const {
|
||||
return _windowId;
|
||||
}
|
||||
|
||||
uintptr_t SystemWindow::getWindowHandle() const {
|
||||
return _windowHandle;
|
||||
}
|
||||
|
||||
void SystemWindow::setCursorEnabled(bool value) {
|
||||
SDLHelper::setCursorEnabled(value);
|
||||
}
|
||||
|
||||
SystemWindow::Size SystemWindow::getViewSize() const {
|
||||
return Size{static_cast<float>(_width), static_cast<float>(_height)};
|
||||
}
|
||||
} // namespace cc
|
||||
73
cocos/platform/win32/modules/SystemWindow.h
Normal file
73
cocos/platform/win32/modules/SystemWindow.h
Normal file
@@ -0,0 +1,73 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include "platform/interfaces/modules/ISystemWindow.h"
|
||||
|
||||
struct SDL_Window;
|
||||
|
||||
namespace cc {
|
||||
class SDLHelper;
|
||||
|
||||
class CC_DLL SystemWindow : public ISystemWindow {
|
||||
friend class SystemWindowManager;
|
||||
|
||||
public:
|
||||
explicit SystemWindow(uint32_t windowId, void* externalHandle);
|
||||
~SystemWindow() override;
|
||||
|
||||
bool createWindow(const char* title,
|
||||
int w, int h, int flags) override;
|
||||
bool createWindow(const char* title,
|
||||
int x, int y, int w,
|
||||
int h, int flags) override;
|
||||
void closeWindow() override;
|
||||
|
||||
uint32_t getWindowId() const override;
|
||||
uintptr_t getWindowHandle() const override;
|
||||
|
||||
Size getViewSize() const override;
|
||||
void setViewSize(uint32_t width, uint32_t height) override {
|
||||
_width = width;
|
||||
_height = height;
|
||||
}
|
||||
/*
|
||||
@brief enable/disable(lock) the cursor, default is enabled
|
||||
*/
|
||||
void setCursorEnabled(bool value) override;
|
||||
|
||||
private:
|
||||
SDL_Window* getSDLWindow() const { return _window; }
|
||||
|
||||
uint32_t _width{0};
|
||||
uint32_t _height{0};
|
||||
|
||||
uint32_t _windowId{0};
|
||||
uintptr_t _windowHandle{0};
|
||||
SDL_Window* _window{nullptr};
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
88
cocos/platform/win32/modules/SystemWindowManager.cpp
Normal file
88
cocos/platform/win32/modules/SystemWindowManager.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "SystemWindowManager.h"
|
||||
#include "SDL2/SDL_events.h"
|
||||
#include "platform/BasePlatform.h"
|
||||
#include "platform/SDLHelper.h"
|
||||
#include "platform/interfaces/modules/ISystemWindowManager.h"
|
||||
#include "platform/win32/modules/SystemWindow.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
int SystemWindowManager::init() {
|
||||
return SDLHelper::init();
|
||||
}
|
||||
|
||||
void SystemWindowManager::processEvent() {
|
||||
SDL_Event sdlEvent;
|
||||
while (SDL_PollEvent(&sdlEvent) != 0) {
|
||||
SDL_Window *sdlWindow = SDL_GetWindowFromID(sdlEvent.window.windowID);
|
||||
// SDL_Event like SDL_QUIT does not associate a window
|
||||
if (!sdlWindow) {
|
||||
SDLHelper::dispatchSDLEvent(0, sdlEvent);
|
||||
} else {
|
||||
ISystemWindow *window = getWindowFromSDLWindow(sdlWindow);
|
||||
CC_ASSERT(window);
|
||||
uint32_t windowId = window->getWindowId();
|
||||
SDLHelper::dispatchSDLEvent(windowId, sdlEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ISystemWindow *SystemWindowManager::createWindow(const ISystemWindowInfo &info) {
|
||||
ISystemWindow *window = BasePlatform::getPlatform()->createNativeWindow(_nextWindowId, info.externalHandle);
|
||||
if (window) {
|
||||
if (!info.externalHandle) {
|
||||
window->createWindow(info.title.c_str(), info.x, info.y, info.width, info.height, info.flags);
|
||||
}
|
||||
_windows[_nextWindowId] = std::shared_ptr<ISystemWindow>(window);
|
||||
_nextWindowId++;
|
||||
}
|
||||
return window;
|
||||
}
|
||||
|
||||
ISystemWindow *SystemWindowManager::getWindow(uint32_t windowId) const {
|
||||
if (windowId == 0) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
auto iter = _windows.find(windowId);
|
||||
if (iter != _windows.end())
|
||||
return iter->second.get();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
cc::ISystemWindow *SystemWindowManager::getWindowFromSDLWindow(SDL_Window *window) const {
|
||||
for (const auto &iter : _windows) {
|
||||
SystemWindow *sysWindow = static_cast<SystemWindow *>(iter.second.get());
|
||||
SDL_Window *sdlWindow = sysWindow->getSDLWindow();
|
||||
if (sdlWindow == window) {
|
||||
return sysWindow;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
54
cocos/platform/win32/modules/SystemWindowManager.h
Normal file
54
cocos/platform/win32/modules/SystemWindowManager.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "base/std/container/unordered_map.h"
|
||||
#include "platform/interfaces/modules/ISystemWindowManager.h"
|
||||
|
||||
struct SDL_Window;
|
||||
|
||||
namespace cc {
|
||||
|
||||
class ISystemWindow;
|
||||
|
||||
class SystemWindowManager : public ISystemWindowManager {
|
||||
public:
|
||||
SystemWindowManager() = default;
|
||||
|
||||
int init() override;
|
||||
void processEvent() override;
|
||||
|
||||
ISystemWindow *createWindow(const ISystemWindowInfo &info) override;
|
||||
ISystemWindow *getWindow(uint32_t windowId) const override;
|
||||
const SystemWindowMap &getWindows() const override { return _windows; }
|
||||
|
||||
ISystemWindow *getWindowFromSDLWindow(SDL_Window *window) const;
|
||||
|
||||
private:
|
||||
uint32_t _nextWindowId{1}; // start from 1, 0 means an invalid ID
|
||||
|
||||
SystemWindowMap _windows;
|
||||
};
|
||||
} // namespace cc
|
||||
35
cocos/platform/win32/modules/Vibrator.cpp
Normal file
35
cocos/platform/win32/modules/Vibrator.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#include "platform/win32/modules/Vibrator.h"
|
||||
|
||||
#include "base/Macros.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
void Vibrator::vibrate(float duration) {
|
||||
CC_UNUSED_PARAM(duration);
|
||||
}
|
||||
|
||||
} // namespace cc
|
||||
43
cocos/platform/win32/modules/Vibrator.h
Normal file
43
cocos/platform/win32/modules/Vibrator.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 Xiamen Yaji Software Co., Ltd.
|
||||
|
||||
http://www.cocos.com
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "platform/interfaces/modules/IVibrator.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL Vibrator : public IVibrator {
|
||||
public:
|
||||
/**
|
||||
* Vibrate for the specified amount of time.
|
||||
* If vibrate is not supported, then invoking this method has no effect.
|
||||
* Some platforms limit to a maximum duration of 5 seconds.
|
||||
* Duration is ignored on iOS due to API limitations.
|
||||
* @param duration The duration in seconds.
|
||||
*/
|
||||
void vibrate(float duration) override;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
Reference in New Issue
Block a user