mirror of
https://github.com/smallmain/cocos-enhance-kit.git
synced 2025-10-09 02:45:22 +00:00
[engine] [cocos2d-x] [jsb-adapter] 适配引擎 v2.4.12 版本
This commit is contained in:
113
cocos2d-x/cocos/platform/openharmony/render/egl_core.cpp
Normal file
113
cocos2d-x/cocos/platform/openharmony/render/egl_core.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
/****************************************************************************
|
||||
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 engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
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 "egl_core.h"
|
||||
#include "platform/openharmony/napi/NapiHelper.h"
|
||||
|
||||
EGLConfig getConfig(int version, EGLDisplay eglDisplay) {
|
||||
int attribList[] = {
|
||||
EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
|
||||
EGL_RED_SIZE, 8,
|
||||
EGL_GREEN_SIZE, 8,
|
||||
EGL_BLUE_SIZE, 8,
|
||||
EGL_ALPHA_SIZE, 8,
|
||||
EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
|
||||
EGL_STENCIL_SIZE, 8,
|
||||
EGL_DEPTH_SIZE, 24,
|
||||
EGL_NONE
|
||||
};
|
||||
EGLConfig configs = NULL;
|
||||
int configsNum;
|
||||
if (!eglChooseConfig(eglDisplay, attribList, &configs, 1, &configsNum)) {
|
||||
LOGE("eglChooseConfig ERROR");
|
||||
return NULL;
|
||||
}
|
||||
return configs;
|
||||
}
|
||||
|
||||
void EGLCore::GLContextInit(void* window, int w, int h)
|
||||
{
|
||||
LOGD("EGLCore::GLContextInit window = %{public}p, w = %{public}d, h = %{public}d.", window, w, h);
|
||||
width_ = w;
|
||||
height_ = h;
|
||||
mEglWindow = (EGLNativeWindowType)(window);
|
||||
|
||||
// 1. create sharedcontext
|
||||
mEGLDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
|
||||
if (mEGLDisplay == EGL_NO_DISPLAY) {
|
||||
LOGE("EGLCore::unable to get EGL display.");
|
||||
return;
|
||||
}
|
||||
|
||||
EGLint eglMajVers, eglMinVers;
|
||||
if (!eglInitialize(mEGLDisplay, &eglMajVers, &eglMinVers)) {
|
||||
mEGLDisplay = EGL_NO_DISPLAY;
|
||||
LOGE("EGLCore::unable to initialize display");
|
||||
return;
|
||||
}
|
||||
|
||||
mEGLConfig = getConfig(3, mEGLDisplay);
|
||||
if (mEGLConfig == nullptr) {
|
||||
LOGE("EGLCore::GLContextInit config ERROR");
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Create EGL Surface from Native Window
|
||||
if (mEglWindow) {
|
||||
mEGLSurface = eglCreateWindowSurface(mEGLDisplay, mEGLConfig, mEglWindow, nullptr);
|
||||
if (mEGLSurface == nullptr) {
|
||||
LOGE("EGLCore::eglCreateContext eglSurface is null");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Create EGLContext from
|
||||
int attrib3_list[] = {
|
||||
EGL_CONTEXT_CLIENT_VERSION, 2,
|
||||
EGL_NONE
|
||||
};
|
||||
|
||||
mEGLContext = eglCreateContext(mEGLDisplay, mEGLConfig, mSharedEGLContext, attrib3_list);
|
||||
|
||||
if (!eglMakeCurrent(mEGLDisplay, mEGLSurface, mEGLSurface, mEGLContext)) {
|
||||
LOGE("EGLCore::eglMakeCurrent error = %{public}d", eglGetError());
|
||||
}
|
||||
}
|
||||
|
||||
void EGLCore::Update()
|
||||
{
|
||||
eglSwapBuffers(mEGLDisplay, mEGLSurface);
|
||||
}
|
||||
|
||||
bool EGLCore::checkGlError(const char* op)
|
||||
{
|
||||
LOGE("EGL ERROR CODE = %{public}x", eglGetError());
|
||||
GLint error;
|
||||
for (error = glGetError(); error; error = glGetError()) {
|
||||
LOGE("ERROR: %{public}s, ERROR CODE = %{public}x", op, error);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
53
cocos2d-x/cocos/platform/openharmony/render/egl_core.h
Normal file
53
cocos2d-x/cocos/platform/openharmony/render/egl_core.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/****************************************************************************
|
||||
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 engine source code (the "Software"), a limited,
|
||||
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
||||
to use Cocos Creator solely to develop games on your target platforms. You shall
|
||||
not use Cocos Creator software for developing other software or tools that's
|
||||
used for developing games. You are not granted to publish, distribute,
|
||||
sublicense, and/or sell copies of Cocos Creator.
|
||||
|
||||
The software or tools in this License Agreement are licensed, not sold.
|
||||
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
||||
|
||||
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 _GL_CORE_
|
||||
#define _GL_CORE_
|
||||
|
||||
#include "platform/openharmony/CCGL-openharmony.h"
|
||||
#include <string>
|
||||
|
||||
class EGLCore {
|
||||
public:
|
||||
EGLCore() {};
|
||||
void GLContextInit(void* window, int w, int h);
|
||||
void Update();
|
||||
|
||||
public:
|
||||
int width_;
|
||||
int height_;
|
||||
|
||||
private:
|
||||
bool checkGlError(const char* op);
|
||||
|
||||
EGLNativeWindowType mEglWindow;
|
||||
EGLDisplay mEGLDisplay = EGL_NO_DISPLAY;
|
||||
EGLConfig mEGLConfig = nullptr;
|
||||
EGLContext mEGLContext = EGL_NO_CONTEXT;
|
||||
EGLContext mSharedEGLContext = EGL_NO_CONTEXT;
|
||||
EGLSurface mEGLSurface = nullptr;
|
||||
};
|
||||
|
||||
#endif // _GL_CORE_
|
Reference in New Issue
Block a user