Files
AVA2/src/python_grp.cpp
T
shenandshen 281418f351 Fix cross-platform rendering and optimize frame performance
Correct selection orientation, game-entry lifecycle, mounted NPCs, labels and skill panels. Add the native GPU scene path, resident geometry, bounded texture and Python caches, and collision broad phase.

Pin the independently maintained Metin2 SDK through a submodule and document reproducible setup. Include rendering regressions and macOS packaging improvements.

Validation: client Debug build and 12 targeted client tests passed; SDK build and all 14 SDK tests passed.
2026-10-11 17:55:53 -07:00

1414 lines
56 KiB
C++

#include "python_grp.h"
#include "eter_image/GrpDevice.h"
#include "eter_image/GrpImageInstance.h"
#include "eter_image/GrpExpandedImageInstance.h"
#include "eter_resource/ResourceManager.h"
#include "eter_ui/PythonWindow.h"
#include "eter_ui/PythonWindowManager.h"
#include "eter_render/TextTailManager.h"
#include "eter_render/Gr2ModelRenderer.h"
#include <SDL3/SDL.h>
#include <Python.h>
#include <string>
#include <vector>
#include <algorithm>
#include <mutex>
#include <unordered_map>
namespace ava2 {
namespace {
static uint32_t s_currentColor = 0xFFFFFFFF;
static uint32_t s_diffuseColor = 0xFFFFFFFF;
static uint32_t s_clearColor = 0xFF000000;
static float s_alpha = 1.0f;
// ---------------------------------------------------------------------
// grp Color & Primitive Rendering
// ---------------------------------------------------------------------
static PyObject* grp_GenerateColor(PyObject*, PyObject* args) {
float r = 0.0f, g = 0.0f, b = 0.0f, a = 1.0f;
if (!PyArg_ParseTuple(args, "ffff", &r, &g, &b, &a)) {
PyErr_Clear();
int ir = 0, ig = 0, ib = 0, ia = 255;
if (PyArg_ParseTuple(args, "iiii", &ir, &ig, &ib, &ia)) {
uint32_t color = ((uint32_t)(ia & 0xFF) << 24) |
((uint32_t)(ir & 0xFF) << 16) |
((uint32_t)(ig & 0xFF) << 8) |
(uint32_t)(ib & 0xFF);
return PyLong_FromUnsignedLong(color);
}
return PyLong_FromUnsignedLong(0xFFFFFFFF);
}
uint8_t ur = (uint8_t)std::clamp((int)(r * 255.0f), 0, 255);
uint8_t ug = (uint8_t)std::clamp((int)(g * 255.0f), 0, 255);
uint8_t ub = (uint8_t)std::clamp((int)(b * 255.0f), 0, 255);
uint8_t ua = (uint8_t)std::clamp((int)(a * 255.0f), 0, 255);
uint32_t color = ((uint32_t)ua << 24) | ((uint32_t)ur << 16) | ((uint32_t)ug << 8) | (uint32_t)ub;
return PyLong_FromUnsignedLong(color);
}
static PyObject* grp_SetColor(PyObject*, PyObject* args) {
unsigned long color = 0;
if (PyArg_ParseTuple(args, "k", &color)) {
s_currentColor = (uint32_t)color;
} else {
PyErr_Clear();
int icolor = 0;
if (PyArg_ParseTuple(args, "i", &icolor)) {
s_currentColor = (uint32_t)icolor;
}
}
Py_RETURN_NONE;
}
static PyObject* grp_SetAlpha(PyObject*, PyObject* args) {
float a = 1.0f;
if (PyArg_ParseTuple(args, "f", &a)) {
s_alpha = std::clamp(a, 0.0f, 1.0f);
uint8_t ua = (uint8_t)(s_alpha * 255.0f);
s_currentColor = (s_currentColor & 0x00FFFFFF) | ((uint32_t)ua << 24);
}
Py_RETURN_NONE;
}
static PyObject* grp_SetDiffuseColor(PyObject*, PyObject* args) {
float r = 1.0f, g = 1.0f, b = 1.0f, a = 1.0f;
if (PyArg_ParseTuple(args, "ffff", &r, &g, &b, &a)) {
uint8_t ur = (uint8_t)std::clamp((int)(r * 255.0f), 0, 255);
uint8_t ug = (uint8_t)std::clamp((int)(g * 255.0f), 0, 255);
uint8_t ub = (uint8_t)std::clamp((int)(b * 255.0f), 0, 255);
uint8_t ua = (uint8_t)std::clamp((int)(a * 255.0f), 0, 255);
s_diffuseColor = ((uint32_t)ua << 24) | ((uint32_t)ur << 16) | ((uint32_t)ug << 8) | (uint32_t)ub;
}
Py_RETURN_NONE;
}
static PyObject* grp_SetClearColor(PyObject*, PyObject* args) {
float r = 0.0f, g = 0.0f, b = 0.0f;
if (PyArg_ParseTuple(args, "fff", &r, &g, &b)) {
uint8_t ur = (uint8_t)std::clamp((int)(r * 255.0f), 0, 255);
uint8_t ug = (uint8_t)std::clamp((int)(g * 255.0f), 0, 255);
uint8_t ub = (uint8_t)std::clamp((int)(b * 255.0f), 0, 255);
s_clearColor = 0xFF000000 | ((uint32_t)ur << 16) | ((uint32_t)ug << 8) | (uint32_t)ub;
}
Py_RETURN_NONE;
}
static PyObject* grp_RenderBar(PyObject*, PyObject* args) {
int x = 0, y = 0, w = 0, h = 0;
if (PyArg_ParseTuple(args, "iiii", &x, &y, &w, &h)) {
auto* renderer = CGraphicDevice::Instance().GetRenderer();
if (renderer && w > 0 && h > 0) {
uint8_t a = (s_currentColor >> 24) & 0xFF;
uint8_t r = (s_currentColor >> 16) & 0xFF;
uint8_t g = (s_currentColor >> 8) & 0xFF;
uint8_t b = s_currentColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FRect rect{ (float)x, (float)y, (float)w, (float)h };
SDL_RenderFillRect(renderer, &rect);
}
}
Py_RETURN_NONE;
}
static PyObject* grp_RenderLine(PyObject*, PyObject* args) {
int x = 0, y = 0, dx = 0, dy = 0;
if (PyArg_ParseTuple(args, "iiii", &x, &y, &dx, &dy)) {
auto* renderer = CGraphicDevice::Instance().GetRenderer();
if (renderer) {
uint8_t a = (s_currentColor >> 24) & 0xFF;
uint8_t r = (s_currentColor >> 16) & 0xFF;
uint8_t g = (s_currentColor >> 8) & 0xFF;
uint8_t b = s_currentColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_RenderLine(renderer, (float)x, (float)y, (float)(x + dx), (float)(y + dy));
}
}
Py_RETURN_NONE;
}
static PyObject* grp_RenderBox(PyObject*, PyObject* args) {
int x = 0, y = 0, w = 0, h = 0;
if (PyArg_ParseTuple(args, "iiii", &x, &y, &w, &h)) {
auto* renderer = CGraphicDevice::Instance().GetRenderer();
if (renderer && w > 0 && h > 0) {
uint8_t a = (s_currentColor >> 24) & 0xFF;
uint8_t r = (s_currentColor >> 16) & 0xFF;
uint8_t g = (s_currentColor >> 8) & 0xFF;
uint8_t b = s_currentColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FRect rect{ (float)x, (float)y, (float)w, (float)h };
SDL_RenderRect(renderer, &rect);
}
}
Py_RETURN_NONE;
}
static PyObject* grp_RenderRoundBox(PyObject*, PyObject* args) {
int x = 0, y = 0, w = 0, h = 0;
if (PyArg_ParseTuple(args, "iiii", &x, &y, &w, &h)) {
auto* renderer = CGraphicDevice::Instance().GetRenderer();
if (renderer && w > 0 && h > 0) {
uint8_t a = (s_currentColor >> 24) & 0xFF;
uint8_t r = (s_currentColor >> 16) & 0xFF;
uint8_t g = (s_currentColor >> 8) & 0xFF;
uint8_t b = s_currentColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FRect rect{ (float)x, (float)y, (float)w, (float)h };
SDL_RenderRect(renderer, &rect);
}
}
Py_RETURN_NONE;
}
static PyObject* grp_RenderGradationBar(PyObject*, PyObject* args) {
int x = 0, y = 0, w = 0, h = 0;
unsigned long startCol = 0, endCol = 0;
if (PyArg_ParseTuple(args, "iiiikk", &x, &y, &w, &h, &startCol, &endCol)) {
auto* renderer = CGraphicDevice::Instance().GetRenderer();
if (renderer && w > 0 && h > 0) {
uint8_t sa = (startCol >> 24) & 0xFF, sr = (startCol >> 16) & 0xFF, sg = (startCol >> 8) & 0xFF, sb = startCol & 0xFF;
uint8_t ea = (endCol >> 24) & 0xFF, er = (endCol >> 16) & 0xFF, eg = (endCol >> 8) & 0xFF, eb = endCol & 0xFF;
SDL_FColor c1{ sr/255.0f, sg/255.0f, sb/255.0f, sa/255.0f };
SDL_FColor c2{ er/255.0f, eg/255.0f, eb/255.0f, ea/255.0f };
SDL_Vertex verts[4];
verts[0] = { { (float)x, (float)y }, c1, {0, 0} };
verts[1] = { { (float)(x+w), (float)y }, c1, {0, 0} };
verts[2] = { { (float)(x+w), (float)(y+h) }, c2, {0, 0} };
verts[3] = { { (float)x, (float)(y+h) }, c2, {0, 0} };
int indices[6] = { 0, 1, 2, 0, 2, 3 };
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_RenderGeometry(renderer, nullptr, verts, 4, indices, 6);
}
}
Py_RETURN_NONE;
}
// ---------------------------------------------------------------------
// CTextBar Implementation
// ---------------------------------------------------------------------
class CTextBar {
public:
CTextBar(int fontSize, bool isBig) : m_fontSize(fontSize), m_isBig(isBig) {}
bool Create(void*, int w, int h) { m_width = w; m_height = h; return true; }
void Render(int, int) {}
void SetTextColor(int r, int g, int b) { m_textColor = (0xff << 24) | (r << 16) | (g << 8) | b; }
void GetTextExtent(const char* text, SIZE* pSize) {
if (pSize) {
pSize->cx = text ? (int)strlen(text) * (m_isBig ? 8 : 6) : 0;
pSize->cy = m_fontSize;
}
}
void TextOut(int, int, const char* text) { if (text) m_text = text; }
void ClearBar() { m_text.clear(); }
void SetClipRect(RECT r) { m_clipRect = r; }
private:
int m_fontSize;
bool m_isBig;
int m_width = 0;
int m_height = 0;
DWORD m_textColor = 0xffffffff;
std::string m_text;
RECT m_clipRect{};
};
static uint32_t s_nextTextBarHandle = 1;
static std::unordered_map<uint32_t, CTextBar*> s_textBarHandles;
static std::mutex s_textBarMutex;
static CTextBar* GetTextBarPointer(uint32_t handle) {
std::lock_guard<std::mutex> lock(s_textBarMutex);
auto it = s_textBarHandles.find(handle);
return it == s_textBarHandles.end() ? nullptr : it->second;
}
static PyObject* grpCreateTextBar(PyObject*, PyObject* poArgs) {
int iWidth = 0, iHeight = 0;
if (!PyTuple_GetInteger(poArgs, 0, &iWidth) || !PyTuple_GetInteger(poArgs, 1, &iHeight))
return Py_BuildException();
auto* bar = new CTextBar(12, false);
bar->Create(nullptr, iWidth, iHeight);
std::lock_guard<std::mutex> lock(s_textBarMutex);
uint32_t h = s_nextTextBarHandle++;
s_textBarHandles[h] = bar;
return Py_BuildValue("i", h);
}
static PyObject* grpCreateBigTextBar(PyObject*, PyObject* poArgs) {
int iWidth = 0, iHeight = 0, iFontSize = 0;
if (!PyTuple_GetInteger(poArgs, 0, &iWidth) || !PyTuple_GetInteger(poArgs, 1, &iHeight) || !PyTuple_GetInteger(poArgs, 2, &iFontSize))
return Py_BuildException();
auto* bar = new CTextBar(iFontSize, true);
bar->Create(nullptr, iWidth, iHeight);
std::lock_guard<std::mutex> lock(s_textBarMutex);
uint32_t h = s_nextTextBarHandle++;
s_textBarHandles[h] = bar;
return Py_BuildValue("i", h);
}
static PyObject* grpDestroyTextBar(PyObject*, PyObject* poArgs) {
int iHandle = 0;
if (!PyTuple_GetInteger(poArgs, 0, &iHandle))
return Py_BuildException();
std::lock_guard<std::mutex> lock(s_textBarMutex);
auto it = s_textBarHandles.find(static_cast<uint32_t>(iHandle));
if (it != s_textBarHandles.end()) {
delete it->second;
s_textBarHandles.erase(it);
}
return Py_BuildNone();
}
static PyObject* grpRenderTextBar(PyObject*, PyObject* poArgs) {
int iHandle = 0, ix = 0, iy = 0;
if (!PyTuple_GetInteger(poArgs, 0, &iHandle) || !PyTuple_GetInteger(poArgs, 1, &ix) || !PyTuple_GetInteger(poArgs, 2, &iy))
return Py_BuildException();
auto* bar = GetTextBarPointer(static_cast<uint32_t>(iHandle));
if (bar) bar->Render(ix, iy);
return Py_BuildNone();
}
static PyObject* grpTextBarSetTextColor(PyObject*, PyObject* poArgs) {
int iHandle = 0, r = 0, g = 0, b = 0;
if (!PyTuple_GetInteger(poArgs, 0, &iHandle) || !PyTuple_GetInteger(poArgs, 1, &r) || !PyTuple_GetInteger(poArgs, 2, &g) || !PyTuple_GetInteger(poArgs, 3, &b))
return Py_BuildException();
auto* bar = GetTextBarPointer(static_cast<uint32_t>(iHandle));
if (bar) bar->SetTextColor(r, g, b);
return Py_BuildNone();
}
static PyObject* grpTextBarGetTextExtent(PyObject*, PyObject* poArgs) {
int iHandle = 0;
char* szText = nullptr;
if (!PyTuple_GetInteger(poArgs, 0, &iHandle) || !PyTuple_GetString(poArgs, 1, &szText))
return Py_BuildException();
SIZE size{0, 0};
auto* bar = GetTextBarPointer(static_cast<uint32_t>(iHandle));
if (bar) bar->GetTextExtent(szText, &size);
return Py_BuildValue("ii", size.cx, size.cy);
}
static PyObject* grpTextBarTextOut(PyObject*, PyObject* poArgs) {
int iHandle = 0, ix = 0, iy = 0;
char* szText = nullptr;
if (!PyTuple_GetInteger(poArgs, 0, &iHandle) || !PyTuple_GetInteger(poArgs, 1, &ix) || !PyTuple_GetInteger(poArgs, 2, &iy) || !PyTuple_GetString(poArgs, 3, &szText))
return Py_BuildException();
auto* bar = GetTextBarPointer(static_cast<uint32_t>(iHandle));
if (bar) bar->TextOut(ix, iy, szText);
return Py_BuildNone();
}
static PyObject* grpClearTextBar(PyObject*, PyObject* poArgs) {
int iHandle = 0;
if (!PyTuple_GetInteger(poArgs, 0, &iHandle))
return Py_BuildException();
auto* bar = GetTextBarPointer(static_cast<uint32_t>(iHandle));
if (bar) bar->ClearBar();
return Py_BuildNone();
}
static PyObject* grpSetTextBarClipRect(PyObject*, PyObject* poArgs) {
int iHandle = 0, isx = 0, isy = 0, iex = 0, iey = 0;
if (!PyTuple_GetInteger(poArgs, 0, &iHandle) ||
!PyTuple_GetInteger(poArgs, 1, &isx) ||
!PyTuple_GetInteger(poArgs, 2, &isy) ||
!PyTuple_GetInteger(poArgs, 3, &iex) ||
!PyTuple_GetInteger(poArgs, 4, &iey))
return Py_BuildException();
auto* bar = GetTextBarPointer(static_cast<uint32_t>(iHandle));
if (bar) {
RECT rect;
rect.left = isx; rect.top = isy; rect.right = iex; rect.bottom = iey;
bar->SetClipRect(rect);
}
return Py_BuildNone();
}
// ---------------------------------------------------------------------
// grp Pipeline / Camera / Transform APIs
// ---------------------------------------------------------------------
static PyObject* grpCulling(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpInitScreenEffect(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpClearDepthBuffer(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpPushState(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpPopState(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpPushMatrix(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpPopMatrix(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpIdentity(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpTranslate(PyObject*, PyObject* poArgs) {
float x = 0.0f, y = 0.0f, z = 0.0f;
if (!PyTuple_GetFloat(poArgs, 0, &x) || !PyTuple_GetFloat(poArgs, 1, &y) || !PyTuple_GetFloat(poArgs, 2, &z))
return Py_BuildException("grp.Translate:Wrong coordinates");
return Py_BuildNone();
}
static PyObject* grpRotate(PyObject*, PyObject* poArgs) {
float Degree = 0.0f, x = 0.0f, y = 0.0f, z = 0.0f;
if (!PyTuple_GetFloat(poArgs, 0, &Degree) || !PyTuple_GetFloat(poArgs, 1, &x) || !PyTuple_GetFloat(poArgs, 2, &y) || !PyTuple_GetFloat(poArgs, 3, &z))
return Py_BuildException();
return Py_BuildNone();
}
static PyObject* grpSetColorRenderState(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpSetAroundCamera(PyObject*, PyObject* poArgs) {
float distance = 0.0f, pitch = 0.0f, roll = 0.0f, lookAtZ = 0.0f;
if (!PyTuple_GetFloat(poArgs, 0, &distance) || !PyTuple_GetFloat(poArgs, 1, &pitch) || !PyTuple_GetFloat(poArgs, 2, &roll) || !PyTuple_GetFloat(poArgs, 3, &lookAtZ))
return Py_BuildException();
return Py_BuildNone();
}
static PyObject* grpSetPositionCamera(PyObject*, PyObject* poArgs) {
float fx = 0, fy = 0, fz = 0, distance = 0, pitch = 0, roll = 0;
if (!PyTuple_GetFloat(poArgs, 0, &fx) || !PyTuple_GetFloat(poArgs, 1, &fy) || !PyTuple_GetFloat(poArgs, 2, &fz) ||
!PyTuple_GetFloat(poArgs, 3, &distance) || !PyTuple_GetFloat(poArgs, 4, &pitch) || !PyTuple_GetFloat(poArgs, 5, &roll))
return Py_BuildException();
return Py_BuildNone();
}
static PyObject* grpSetEyeCamera(PyObject*, PyObject* poArgs) {
float xEye = 0, yEye = 0, zEye = 0, xCenter = 0, yCenter = 0, zCenter = 0, xUp = 0, yUp = 0, zUp = 0;
if (!PyTuple_GetFloat(poArgs, 0, &xEye) || !PyTuple_GetFloat(poArgs, 1, &yEye) || !PyTuple_GetFloat(poArgs, 2, &zEye) ||
!PyTuple_GetFloat(poArgs, 3, &xCenter) || !PyTuple_GetFloat(poArgs, 4, &yCenter) || !PyTuple_GetFloat(poArgs, 5, &zCenter) ||
!PyTuple_GetFloat(poArgs, 6, &xUp) || !PyTuple_GetFloat(poArgs, 7, &yUp) || !PyTuple_GetFloat(poArgs, 8, &zUp))
return Py_BuildException();
return Py_BuildNone();
}
static PyObject* grpSetPerspective(PyObject*, PyObject* poArgs) {
float fov = 0, aspect = 0, zNear = 0, zFar = 0;
if (!PyTuple_GetFloat(poArgs, 0, &fov) || !PyTuple_GetFloat(poArgs, 1, &aspect) ||
!PyTuple_GetFloat(poArgs, 2, &zNear) || !PyTuple_GetFloat(poArgs, 3, &zFar))
return Py_BuildException();
auto* renderer3d = TextTailManager::Instance().GetCurrentSceneRenderer();
if (renderer3d) renderer3d->SetPerspective(fov, aspect, zNear, zFar);
return Py_BuildNone();
}
static PyObject* grpSetOrtho2D(PyObject*, PyObject* poArgs) {
float width = 0, height = 0, length = 0;
if (!PyTuple_GetFloat(poArgs, 0, &width) || !PyTuple_GetFloat(poArgs, 1, &height) || !PyTuple_GetFloat(poArgs, 2, &length))
return Py_BuildException();
return Py_BuildNone();
}
static PyObject* grpSetOrtho3D(PyObject*, PyObject* poArgs) {
float width = 0, height = 0, zmin = 0, zmax = 0;
if (!PyTuple_GetFloat(poArgs, 0, &width) || !PyTuple_GetFloat(poArgs, 1, &height) || !PyTuple_GetFloat(poArgs, 2, &zmin) || !PyTuple_GetFloat(poArgs, 3, &zmax))
return Py_BuildException();
return Py_BuildNone();
}
static PyObject* grpGetCursorPosition3d(PyObject*, PyObject*) {
return Py_BuildValue("(fff)", 0.0f, 0.0f, 0.0f);
}
static PyObject* grpSetCursorPosition(PyObject*, PyObject* poArgs) {
int ix = 0, iy = 0;
if (!PyTuple_GetInteger(poArgs, 0, &ix) || !PyTuple_GetInteger(poArgs, 1, &iy))
return Py_BuildException();
return Py_BuildNone();
}
static PyObject* grpRenderBox3d(PyObject*, PyObject* poArgs) {
float sx = 0, sy = 0, sz = 0, ex = 0, ey = 0, ez = 0;
if (!PyTuple_GetFloat(poArgs, 0, &sx) || !PyTuple_GetFloat(poArgs, 1, &sy) || !PyTuple_GetFloat(poArgs, 2, &sz) ||
!PyTuple_GetFloat(poArgs, 3, &ex) || !PyTuple_GetFloat(poArgs, 4, &ey) || !PyTuple_GetFloat(poArgs, 5, &ez))
return Py_BuildException();
auto* renderer = CGraphicDevice::Instance().GetRenderer();
if (renderer) {
uint8_t a = (s_currentColor >> 24) & 0xFF;
uint8_t r = (s_currentColor >> 16) & 0xFF;
uint8_t g = (s_currentColor >> 8) & 0xFF;
uint8_t b = s_currentColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FRect rect{ sx, sy, ex - sx, ey - sy };
SDL_RenderRect(renderer, &rect);
}
return Py_BuildNone();
}
static PyObject* grpRenderBar3d(PyObject*, PyObject* poArgs) {
int sx = 0, sy = 0, sz = 0, ex = 0, ey = 0, ez = 0;
if (!PyTuple_GetInteger(poArgs, 0, &sx) || !PyTuple_GetInteger(poArgs, 1, &sy) || !PyTuple_GetInteger(poArgs, 2, &sz) ||
!PyTuple_GetInteger(poArgs, 3, &ex) || !PyTuple_GetInteger(poArgs, 4, &ey) || !PyTuple_GetInteger(poArgs, 5, &ez))
return Py_BuildException();
auto* renderer = CGraphicDevice::Instance().GetRenderer();
if (renderer) {
uint8_t a = (s_currentColor >> 24) & 0xFF;
uint8_t r = (s_currentColor >> 16) & 0xFF;
uint8_t g = (s_currentColor >> 8) & 0xFF;
uint8_t b = s_currentColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FRect rect{ (float)sx, (float)sy, (float)(ex - sx), (float)(ey - sy) };
SDL_RenderFillRect(renderer, &rect);
}
return Py_BuildNone();
}
static PyObject* grpRenderCube(PyObject*, PyObject* poArgs) {
float sx = 0, sy = 0, sz = 0, ex = 0, ey = 0, ez = 0;
if (!PyTuple_GetFloat(poArgs, 0, &sx) || !PyTuple_GetFloat(poArgs, 1, &sy) || !PyTuple_GetFloat(poArgs, 2, &sz) ||
!PyTuple_GetFloat(poArgs, 3, &ex) || !PyTuple_GetFloat(poArgs, 4, &ey) || !PyTuple_GetFloat(poArgs, 5, &ez))
return Py_BuildException();
auto* renderer = CGraphicDevice::Instance().GetRenderer();
if (renderer) {
uint8_t a = (s_currentColor >> 24) & 0xFF;
uint8_t r = (s_currentColor >> 16) & 0xFF;
uint8_t g = (s_currentColor >> 8) & 0xFF;
uint8_t b = s_currentColor & 0xFF;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FRect rect{ sx, sy, ex - sx, ey - sy };
SDL_RenderRect(renderer, &rect);
}
return Py_BuildNone();
}
static PyObject* grpRenderDownButton(PyObject*, PyObject* poArgs) {
int x = 0, y = 0, width = 0, height = 0;
if (!PyTuple_GetInteger(poArgs, 0, &x) || !PyTuple_GetInteger(poArgs, 1, &y) || !PyTuple_GetInteger(poArgs, 2, &width) || !PyTuple_GetInteger(poArgs, 3, &height))
return Py_BuildException();
auto* renderer = CGraphicDevice::Instance().GetRenderer();
if (renderer && width > 0 && height > 0) {
float sx = (float)x;
float sy = (float)y;
float ex = (float)(x + width);
float ey = (float)(y + height);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
// Box
uint8_t a = (s_currentColor >> 24) & 0xFF;
uint8_t r = (s_currentColor >> 16) & 0xFF;
uint8_t g = (s_currentColor >> 8) & 0xFF;
uint8_t b = s_currentColor & 0xFF;
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FRect rect{ sx, sy, (float)width, (float)height };
SDL_RenderRect(renderer, &rect);
// Dark color (top and left)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderLine(renderer, sx, sy, ex, sy);
SDL_RenderLine(renderer, sx, sy, sx, ey);
// Light color (bottom and right)
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderLine(renderer, sx, ey, ex, ey);
SDL_RenderLine(renderer, ex, sy, ex, ey);
}
return Py_BuildNone();
}
static PyObject* grpRenderUpButton(PyObject*, PyObject* poArgs) {
int x = 0, y = 0, width = 0, height = 0;
if (!PyTuple_GetInteger(poArgs, 0, &x) || !PyTuple_GetInteger(poArgs, 1, &y) || !PyTuple_GetInteger(poArgs, 2, &width) || !PyTuple_GetInteger(poArgs, 3, &height))
return Py_BuildException();
auto* renderer = CGraphicDevice::Instance().GetRenderer();
if (renderer && width > 0 && height > 0) {
float sx = (float)x;
float sy = (float)y;
float ex = (float)(x + width);
float ey = (float)(y + height);
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
// Box
uint8_t a = (s_currentColor >> 24) & 0xFF;
uint8_t r = (s_currentColor >> 16) & 0xFF;
uint8_t g = (s_currentColor >> 8) & 0xFF;
uint8_t b = s_currentColor & 0xFF;
SDL_SetRenderDrawColor(renderer, r, g, b, a);
SDL_FRect rect{ sx, sy, (float)width, (float)height };
SDL_RenderRect(renderer, &rect);
// Light color (top and left)
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderLine(renderer, sx, sy, ex, sy);
SDL_RenderLine(renderer, sx, sy, sx, ey);
// Dark color (bottom and right)
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderLine(renderer, sx, ey, ex, ey);
SDL_RenderLine(renderer, ex, sy, ex, ey);
}
return Py_BuildNone();
}
static PyObject* grpGetAvailableMemory(PyObject*, PyObject*) {
return Py_BuildValue("i", 256 * 1024 * 1024);
}
static PyObject* grpSaveScreenShot(PyObject*, PyObject*) {
return Py_BuildValue("(is)", 1, "screenshot.jpg");
}
static PyObject* grpSaveScreenShotToPath(PyObject*, PyObject* poArgs) {
char* szBasePath = nullptr;
if (!PyTuple_GetString(poArgs, 0, &szBasePath))
return Py_BuildException();
return Py_BuildValue("(is)", 1, (std::string(szBasePath ? szBasePath : "") + ".jpg").c_str());
}
static PyObject* grpSetGamma(PyObject*, PyObject* poArgs) {
float gamma = 0.0f;
if (!PyTuple_GetFloat(poArgs, 0, &gamma))
return Py_BuildException();
return Py_BuildNone();
}
static PyObject* grpSetInterfaceRenderState(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpSetGameRenderState(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpSetViewport(PyObject*, PyObject* poArgs) {
float fx = 0, fy = 0, fWidth = 0, fHeight = 0;
if (!PyTuple_GetFloat(poArgs, 0, &fx) || !PyTuple_GetFloat(poArgs, 1, &fy) ||
!PyTuple_GetFloat(poArgs, 2, &fWidth) || !PyTuple_GetFloat(poArgs, 3, &fHeight))
return Py_BuildException();
long sw = UI::CWindowManager::Instance().GetScreenWidth();
long sh = UI::CWindowManager::Instance().GetScreenHeight();
if (sw <= 0) sw = 1024;
if (sh <= 0) sh = 768;
int vx = static_cast<int>(fx * sw);
int vy = static_cast<int>(fy * sh);
int vw = static_cast<int>(fWidth * sw);
int vh = static_cast<int>(fHeight * sh);
auto* renderer3d = TextTailManager::Instance().GetCurrentSceneRenderer();
if (renderer3d) renderer3d->SetViewport(vx, vy, vw, vh);
return Py_BuildNone();
}
static PyObject* grpRestoreViewport(PyObject*, PyObject*) {
long sw = UI::CWindowManager::Instance().GetScreenWidth();
long sh = UI::CWindowManager::Instance().GetScreenHeight();
if (sw <= 0) sw = 1024;
if (sh <= 0) sh = 768;
auto* renderer3d = TextTailManager::Instance().GetCurrentSceneRenderer();
if (renderer3d) renderer3d->SetViewport(0, 0, static_cast<int>(sw), static_cast<int>(sh));
return Py_BuildNone();
}
static PyObject* grpSetOmniLight(PyObject*, PyObject*) { return Py_BuildNone(); }
static PyObject* grpGetCameraPosition(PyObject*, PyObject*) {
return Py_BuildValue("fff", 0.0f, 0.0f, 180.0f);
}
static PyObject* grpGetTargetPosition(PyObject*, PyObject*) {
return Py_BuildValue("fff", 0.0f, 0.0f, 0.0f);
}
// ---------------------------------------------------------------------
// grpImage Implementation
// ---------------------------------------------------------------------
static bool PyTuple_GetImageInstance(PyObject* poArgs, int pos, CGraphicImageInstance** ppRetImageInstance) {
int handle = 0;
if (!PyTuple_GetInteger(poArgs, pos, &handle))
return false;
if (!handle)
return false;
*ppRetImageInstance = GrpGetImageInstancePointer(static_cast<uint32_t>(handle));
return *ppRetImageInstance != nullptr;
}
static bool PyTuple_GetExpandedImageInstance(PyObject* poArgs, int pos, CGraphicExpandedImageInstance** ppRetImageInstance) {
int handle = 0;
if (!PyTuple_GetInteger(poArgs, pos, &handle))
return false;
if (!handle)
return false;
auto* pInstance = GrpGetImageInstancePointer(static_cast<uint32_t>(handle));
if (!pInstance || !pInstance->IsType(CGraphicExpandedImageInstance::Type()))
return false;
*ppRetImageInstance = static_cast<CGraphicExpandedImageInstance*>(pInstance);
return true;
}
static PyObject* grpImageGenerate(PyObject*, PyObject* poArgs) {
char* szFileName = nullptr;
if (!PyTuple_GetString(poArgs, 0, &szFileName))
return Py_BadArgument();
if (!szFileName || !*szFileName)
return Py_BuildValue("i", 0);
auto* pResource = CResourceManager::Instance().GetResourcePointer(szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return Py_BuildException("Resource is not an image (filename: %s)", szFileName);
auto* pImageInstance = CGraphicImageInstance::New();
pImageInstance->SetImagePointer(static_cast<CGraphicImage*>(pResource));
if (pImageInstance->IsEmpty())
return Py_BuildException("Cannot load image (filename: %s)", szFileName);
return Py_BuildValue("i", GrpRegisterImageInstanceHandle(pImageInstance));
}
static PyObject* grpImageGenerateExpanded(PyObject*, PyObject* poArgs) {
char* szFileName = nullptr;
if (!PyTuple_GetString(poArgs, 0, &szFileName))
return Py_BadArgument();
if (!szFileName || !*szFileName)
return Py_BuildValue("i", 0);
auto* pResource = CResourceManager::Instance().GetResourcePointer(szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return Py_BuildException("Resource is not an image (filename: %s)", szFileName);
auto* pImageInstance = CGraphicExpandedImageInstance::New();
pImageInstance->SetImagePointer(static_cast<CGraphicImage*>(pResource));
if (pImageInstance->IsEmpty())
return Py_BuildException("Cannot load image (filename: %s)", szFileName);
return Py_BuildValue("i", GrpRegisterImageInstanceHandle(pImageInstance));
}
static PyObject* grpImageGenerateFromHandle(PyObject*, PyObject* poArgs) {
int iHandle = 0;
if (!PyTuple_GetInteger(poArgs, 0, &iHandle))
return Py_BadArgument();
auto* pImageInstance = CGraphicImageInstance::New();
auto* pImage = GrpGetImagePointer(static_cast<uint32_t>(iHandle));
if (pImage) {
pImageInstance->SetImagePointer(pImage);
}
return Py_BuildValue("i", GrpRegisterImageInstanceHandle(pImageInstance));
}
static PyObject* grpImageDelete(PyObject*, PyObject* poArgs) {
int handle = 0;
if (!PyTuple_GetInteger(poArgs, 0, &handle))
return Py_BadArgument();
auto* pInstance = GrpReleaseImageInstanceHandle(static_cast<uint32_t>(handle));
if (pInstance) {
CGraphicImageInstance::Delete(pInstance);
}
return Py_BuildNone();
}
static PyObject* grpImageDeleteExpanded(PyObject*, PyObject* poArgs) {
int handle = 0;
if (!PyTuple_GetInteger(poArgs, 0, &handle))
return Py_BadArgument();
auto* pInstance = GrpReleaseImageInstanceHandle(static_cast<uint32_t>(handle));
if (pInstance) {
if (pInstance->IsType(CGraphicExpandedImageInstance::Type())) {
CGraphicExpandedImageInstance::Delete(static_cast<CGraphicExpandedImageInstance*>(pInstance));
} else {
CGraphicImageInstance::Delete(pInstance);
}
}
return Py_BuildNone();
}
static PyObject* grpImageSetFileName(PyObject*, PyObject* poArgs) {
CGraphicImageInstance* pImageInstance = nullptr;
if (!PyTuple_GetImageInstance(poArgs, 0, &pImageInstance))
return Py_BadArgument();
char* szFileName = nullptr;
if (!PyTuple_GetString(poArgs, 1, &szFileName))
return Py_BadArgument();
auto* pResource = CResourceManager::Instance().GetResourcePointer(szFileName);
if (!pResource || !pResource->IsType(CGraphicImage::Type()))
return Py_BuildException("Resource is not an image (filename: %s)", szFileName);
pImageInstance->SetImagePointer(static_cast<CGraphicImage*>(pResource));
return Py_BuildNone();
}
static PyObject* grpImageRender(PyObject*, PyObject* poArgs) {
CGraphicImageInstance* pImageInstance = nullptr;
if (!PyTuple_GetImageInstance(poArgs, 0, &pImageInstance))
return Py_BadArgument();
pImageInstance->Render();
return Py_BuildNone();
}
static PyObject* grpSetImagePosition(PyObject*, PyObject* poArgs) {
CGraphicImageInstance* pImageInstance = nullptr;
if (!PyTuple_GetImageInstance(poArgs, 0, &pImageInstance))
return Py_BadArgument();
float x = 0.0f, y = 0.0f;
if (!PyTuple_GetFloat(poArgs, 1, &x) || !PyTuple_GetFloat(poArgs, 2, &y))
return Py_BadArgument();
pImageInstance->SetPosition(x, y);
return Py_BuildNone();
}
static PyObject* grpSetImageOrigin(PyObject*, PyObject* poArgs) {
CGraphicExpandedImageInstance* pImageInstance = nullptr;
if (!PyTuple_GetExpandedImageInstance(poArgs, 0, &pImageInstance))
return Py_BadArgument();
float x = 0.0f, y = 0.0f;
if (!PyTuple_GetFloat(poArgs, 1, &x) || !PyTuple_GetFloat(poArgs, 2, &y))
return Py_BadArgument();
pImageInstance->SetOrigin(x, y);
return Py_BuildNone();
}
static PyObject* grpSetImageRotation(PyObject*, PyObject* poArgs) {
CGraphicExpandedImageInstance* pImageInstance = nullptr;
if (!PyTuple_GetExpandedImageInstance(poArgs, 0, &pImageInstance))
return Py_BadArgument();
float Degree = 0.0f;
if (!PyTuple_GetFloat(poArgs, 1, &Degree))
return Py_BadArgument();
pImageInstance->SetRotation(Degree);
return Py_BuildNone();
}
static PyObject* grpSetImageScale(PyObject*, PyObject* poArgs) {
CGraphicExpandedImageInstance* pImageInstance = nullptr;
if (!PyTuple_GetExpandedImageInstance(poArgs, 0, &pImageInstance))
return Py_BadArgument();
float x = 0.0f, y = 0.0f;
if (!PyTuple_GetFloat(poArgs, 1, &x) || !PyTuple_GetFloat(poArgs, 2, &y))
return Py_BadArgument();
pImageInstance->SetScale(x, y);
return Py_BuildNone();
}
static PyObject* grpSetRenderingRect(PyObject*, PyObject* poArgs) {
CGraphicExpandedImageInstance* pImageInstance = nullptr;
if (!PyTuple_GetExpandedImageInstance(poArgs, 0, &pImageInstance))
return Py_BadArgument();
float fLeft = 0.0f, fTop = 0.0f, fRight = 0.0f, fBottom = 0.0f;
if (!PyTuple_GetFloat(poArgs, 1, &fLeft) ||
!PyTuple_GetFloat(poArgs, 2, &fTop) ||
!PyTuple_GetFloat(poArgs, 3, &fRight) ||
!PyTuple_GetFloat(poArgs, 4, &fBottom))
return Py_BadArgument();
pImageInstance->SetRenderingRect(fLeft, fTop, fRight, fBottom);
return Py_BuildNone();
}
static PyObject* grpSetImageDiffuseColor(PyObject*, PyObject* poArgs) {
CGraphicImageInstance* pImageInstance = nullptr;
if (!PyTuple_GetImageInstance(poArgs, 0, &pImageInstance))
return Py_BadArgument();
float r = 1.0f, g = 1.0f, b = 1.0f, a = 1.0f;
if (!PyTuple_GetFloat(poArgs, 1, &r) ||
!PyTuple_GetFloat(poArgs, 2, &g) ||
!PyTuple_GetFloat(poArgs, 3, &b) ||
!PyTuple_GetFloat(poArgs, 4, &a))
return Py_BadArgument();
pImageInstance->SetDiffuseColor(r, g, b, a);
return Py_BuildNone();
}
static PyObject* grpGetWidth(PyObject*, PyObject* poArgs) {
CGraphicImageInstance* pImageInstance = nullptr;
if (!PyTuple_GetImageInstance(poArgs, 0, &pImageInstance))
return Py_BadArgument();
if (pImageInstance->IsEmpty())
return Py_BuildException("Image is empty");
return Py_BuildValue("i", pImageInstance->GetWidth());
}
static PyObject* grpGetHeight(PyObject*, PyObject* poArgs) {
CGraphicImageInstance* pImageInstance = nullptr;
if (!PyTuple_GetImageInstance(poArgs, 0, &pImageInstance))
return Py_BadArgument();
if (pImageInstance->IsEmpty())
return Py_BuildException("Image is empty");
return Py_BuildValue("i", pImageInstance->GetHeight());
}
// ---------------------------------------------------------------------
// grpText Implementation
// ---------------------------------------------------------------------
static uint32_t s_nextTextHandle = 1;
static std::unordered_map<uint32_t, UI::CGraphicTextInstance*> s_textHandles;
static std::mutex s_textHandleMutex;
static uint32_t GrpRegisterTextInstanceHandle(UI::CGraphicTextInstance* pInstance) {
if (!pInstance) return 0;
std::lock_guard<std::mutex> lock(s_textHandleMutex);
uint32_t handle = s_nextTextHandle++;
s_textHandles[handle] = pInstance;
return handle;
}
static UI::CGraphicTextInstance* GrpGetTextInstancePointer(uint32_t handle) {
std::lock_guard<std::mutex> lock(s_textHandleMutex);
auto it = s_textHandles.find(handle);
return it == s_textHandles.end() ? nullptr : it->second;
}
static UI::CGraphicTextInstance* GrpReleaseTextInstanceHandle(uint32_t handle) {
std::lock_guard<std::mutex> lock(s_textHandleMutex);
auto it = s_textHandles.find(handle);
if (it == s_textHandles.end()) return nullptr;
auto* p = it->second;
s_textHandles.erase(it);
return p;
}
static bool PyTuple_GetTextInstance(PyObject* poArgs, int pos, UI::CGraphicTextInstance** ppTextInstance) {
int handle = 0;
if (!PyTuple_GetInteger(poArgs, pos, &handle))
return false;
if (!handle)
return false;
*ppTextInstance = GrpGetTextInstancePointer(static_cast<uint32_t>(handle));
return *ppTextInstance != nullptr;
}
static PyObject* grpTextGenerate(PyObject*, PyObject*) {
auto* pTextInstance = new UI::CGraphicTextInstance();
return Py_BuildValue("i", GrpRegisterTextInstanceHandle(pTextInstance));
}
static PyObject* grpTextDestroy(PyObject*, PyObject* poArgs) {
int handle = 0;
if (!PyTuple_GetInteger(poArgs, 0, &handle))
return Py_BuildException();
auto* pText = GrpReleaseTextInstanceHandle(static_cast<uint32_t>(handle));
delete pText;
return Py_BuildNone();
}
static PyObject* grpTextSetFontName(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
char* szFontName = nullptr;
if (!PyTuple_GetString(poArgs, 1, &szFontName))
return Py_BuildException();
pTextInstance->SetFontName(szFontName);
return Py_BuildNone();
}
static PyObject* grpTextSetFontColor(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
if (2 == PyTuple_Size(poArgs)) {
int iColor = 0;
if (!PyTuple_GetInteger(poArgs, 1, &iColor))
return Py_BuildException();
pTextInstance->SetFontColor(DWORD(iColor));
} else if (4 == PyTuple_Size(poArgs)) {
float fRed = 0.0f, fGreen = 0.0f, fBlue = 0.0f;
if (!PyTuple_GetFloat(poArgs, 1, &fRed) ||
!PyTuple_GetFloat(poArgs, 2, &fGreen) ||
!PyTuple_GetFloat(poArgs, 3, &fBlue))
return Py_BuildException();
pTextInstance->SetColor(fRed, fGreen, fBlue);
} else {
return Py_BuildException();
}
return Py_BuildNone();
}
static PyObject* grpTextSetOutLineColor(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
float fRed = 0.0f, fGreen = 0.0f, fBlue = 0.0f, fAlpha = 0.0f;
if (!PyTuple_GetFloat(poArgs, 1, &fRed) ||
!PyTuple_GetFloat(poArgs, 2, &fGreen) ||
!PyTuple_GetFloat(poArgs, 3, &fBlue) ||
!PyTuple_GetFloat(poArgs, 4, &fAlpha))
return Py_BuildException();
pTextInstance->SetOutLineColor(fRed, fGreen, fBlue, fAlpha);
return Py_BuildNone();
}
static PyObject* grpTextSetSecret(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
bool bSecret = false;
if (!PyTuple_GetBoolean(poArgs, 1, &bSecret))
return Py_BuildException();
pTextInstance->SetSecret(bSecret ? TRUE : FALSE);
return Py_BuildNone();
}
static PyObject* grpTextOutline(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
bool bOutline = false;
if (!PyTuple_GetBoolean(poArgs, 1, &bOutline))
return Py_BuildException();
pTextInstance->SetOutline(bOutline ? TRUE : FALSE);
return Py_BuildNone();
}
static PyObject* grpTextSetText(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
char* szText = nullptr;
if (!PyTuple_GetString(poArgs, 1, &szText))
return Py_BuildException();
pTextInstance->SetText(szText);
return Py_BuildNone();
}
static PyObject* grpTextGetText(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
return Py_BuildValue("s", pTextInstance->GetText());
}
static PyObject* grpTextGetSize(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
int width = 0, height = 0;
pTextInstance->GetTextSize(&width, &height);
return Py_BuildValue("(i, i)", width, height);
}
static PyObject* grpTextSetPosition(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
int ix = 0, iy = 0;
if (!PyTuple_GetInteger(poArgs, 1, &ix) || !PyTuple_GetInteger(poArgs, 2, &iy))
return Py_BuildException();
pTextInstance->SetPosition((float)ix, (float)iy);
return Py_BuildNone();
}
static PyObject* grpTextShowCursor(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
pTextInstance->ShowCursor();
return Py_BuildNone();
}
static PyObject* grpTextHideCursor(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
pTextInstance->HideCursor();
return Py_BuildNone();
}
static PyObject* grpSetHorizontalAlign(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
int iAlign = 0;
if (!PyTuple_GetInteger(poArgs, 1, &iAlign))
return Py_BuildException();
pTextInstance->SetHorizonalAlign(iAlign);
return Py_BuildNone();
}
static PyObject* grpSetVerticalAlign(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
int iAlign = 0;
if (!PyTuple_GetInteger(poArgs, 1, &iAlign))
return Py_BuildException();
pTextInstance->SetVerticalAlign(iAlign);
return Py_BuildNone();
}
static PyObject* grpSetMax(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
int iValue = 0;
if (!PyTuple_GetInteger(poArgs, 1, &iValue))
return Py_BuildException();
pTextInstance->SetMax(iValue);
return Py_BuildNone();
}
static PyObject* grpGetSplitingTextLineCount(PyObject*, PyObject* poArgs) {
char* szText = nullptr;
if (!PyTuple_GetString(poArgs, 0, &szText))
return Py_BuildException();
int iLineLimitation = 0;
if (!PyTuple_GetInteger(poArgs, 1, &iLineLimitation))
return Py_BuildException();
int iPosition = 0;
int iLineCount = 0;
size_t len = szText ? strlen(szText) : 0;
for (size_t i = 0; i < len;) {
if ('|' == szText[i]) {
i += 1;
if (iPosition > 0)
++iLineCount;
iPosition = 0;
continue;
}
if (szText[i] & 0x80) {
i += 2;
iPosition += 2;
} else {
i += 1;
iPosition += 1;
}
if (iPosition >= iLineLimitation) {
++iLineCount;
iPosition = 0;
}
}
if (iPosition > 0)
++iLineCount;
return Py_BuildValue("i", iLineCount);
}
static PyObject* grpGetSplitingTextLine(PyObject*, PyObject* poArgs) {
char* szText = nullptr;
if (!PyTuple_GetString(poArgs, 0, &szText))
return Py_BuildException();
int iLineLimitation = 0;
if (!PyTuple_GetInteger(poArgs, 1, &iLineLimitation))
return Py_BuildException();
int iGettingLine = 0;
if (!PyTuple_GetInteger(poArgs, 2, &iGettingLine))
return Py_BuildException();
std::string strLine;
int iPosition = 0;
int iLineCount = 0;
size_t len = szText ? strlen(szText) : 0;
for (size_t i = 0; i < len;) {
if ('|' == szText[i]) {
i += 1;
if (iPosition > 0)
++iLineCount;
iPosition = 0;
continue;
}
if (szText[i] & 0x80) {
if (iLineCount == iGettingLine)
strLine.append(szText + i, 2);
i += 2;
iPosition += 2;
} else {
if (iLineCount == iGettingLine)
strLine += szText[i];
i += 1;
iPosition += 1;
}
if (iPosition >= iLineLimitation) {
++iLineCount;
iPosition = 0;
}
}
return Py_BuildValue("s", strLine.c_str());
}
static PyObject* grpTextPixelPositionToCharacterPosition(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
int iPixelPosition = 0;
if (!PyTuple_GetInteger(poArgs, 1, &iPixelPosition))
return Py_BuildException();
int iCharacterPosition = pTextInstance->PixelPositionToCharacterPosition(iPixelPosition);
return Py_BuildValue("i", iCharacterPosition);
}
static PyObject* grpTextRender(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
pTextInstance->Render();
return Py_BuildNone();
}
static PyObject* grpTextUpdate(PyObject*, PyObject* poArgs) {
UI::CGraphicTextInstance* pTextInstance = nullptr;
if (!PyTuple_GetTextInstance(poArgs, 0, &pTextInstance))
return Py_BuildException();
pTextInstance->Update();
return Py_BuildNone();
}
// ---------------------------------------------------------------------
// grpThing Implementation
// ---------------------------------------------------------------------
struct SGraphicThingHolder {
std::string fileName;
struct Vec3 { float x = 0.0f, y = 0.0f, z = 0.0f; };
Vec3 position{0.0f, 0.0f, 0.0f};
float rotation = 0.0f;
Vec3 scale{1.0f, 1.0f, 1.0f};
};
static uint32_t s_nextThingHandle = 1;
static std::unordered_map<uint32_t, SGraphicThingHolder*> s_thingHandles;
static std::mutex s_thingHandleMutex;
static bool PyTuple_GetThingInstance(PyObject* poArgs, int pos, SGraphicThingHolder** ppThing) {
int handle = 0;
if (!PyTuple_GetInteger(poArgs, pos, &handle))
return false;
if (!handle)
return false;
std::lock_guard<std::mutex> lock(s_thingHandleMutex);
auto it = s_thingHandles.find(static_cast<uint32_t>(handle));
if (it == s_thingHandles.end()) return false;
*ppThing = it->second;
return true;
}
static PyObject* grpThingGenerate(PyObject*, PyObject* poArgs) {
char* szFileName = nullptr;
if (!PyTuple_GetString(poArgs, 0, &szFileName))
return Py_BuildException();
if (!szFileName || !*szFileName)
return Py_BuildValue("i", 0);
auto* holder = new SGraphicThingHolder();
holder->fileName = szFileName;
std::lock_guard<std::mutex> lock(s_thingHandleMutex);
uint32_t handle = s_nextThingHandle++;
s_thingHandles[handle] = holder;
return Py_BuildValue("i", handle);
}
static PyObject* grpThingDelete(PyObject*, PyObject* poArgs) {
int handle = 0;
if (!PyTuple_GetInteger(poArgs, 0, &handle))
return Py_BuildException();
std::lock_guard<std::mutex> lock(s_thingHandleMutex);
auto it = s_thingHandles.find(static_cast<uint32_t>(handle));
if (it != s_thingHandles.end()) {
delete it->second;
s_thingHandles.erase(it);
}
return Py_BuildNone();
}
static PyObject* grpThingSetFileName(PyObject*, PyObject* poArgs) {
SGraphicThingHolder* pThing = nullptr;
if (!PyTuple_GetThingInstance(poArgs, 0, &pThing))
return Py_BuildException();
char* szFileName = nullptr;
if (!PyTuple_GetString(poArgs, 1, &szFileName))
return Py_BuildException();
pThing->fileName = szFileName ? szFileName : "";
return Py_BuildNone();
}
static PyObject* grpThingRender(PyObject*, PyObject* poArgs) {
SGraphicThingHolder* pThing = nullptr;
if (!PyTuple_GetThingInstance(poArgs, 0, &pThing))
return Py_BuildException();
return Py_BuildNone();
}
static PyObject* grpThingUpdate(PyObject*, PyObject* poArgs) {
SGraphicThingHolder* pThing = nullptr;
if (!PyTuple_GetThingInstance(poArgs, 0, &pThing))
return Py_BuildException();
return Py_BuildNone();
}
static PyObject* grpSetThingPosition(PyObject*, PyObject* poArgs) {
SGraphicThingHolder* pThing = nullptr;
if (!PyTuple_GetThingInstance(poArgs, 0, &pThing))
return Py_BuildException();
float x = 0.0f, y = 0.0f, z = 0.0f;
if (!PyTuple_GetFloat(poArgs, 1, &x) || !PyTuple_GetFloat(poArgs, 2, &y) || !PyTuple_GetFloat(poArgs, 3, &z))
return Py_BuildException();
pThing->position = {x, y, z};
return Py_BuildNone();
}
static PyObject* grpSetThingRotation(PyObject*, PyObject* poArgs) {
SGraphicThingHolder* pThing = nullptr;
if (!PyTuple_GetThingInstance(poArgs, 0, &pThing))
return Py_BuildException();
float fDegree = 0.0f;
if (!PyTuple_GetFloat(poArgs, 1, &fDegree))
return Py_BuildException();
pThing->rotation = fDegree;
return Py_BuildNone();
}
static PyObject* grpSetThingScale(PyObject*, PyObject* poArgs) {
SGraphicThingHolder* pThing = nullptr;
if (!PyTuple_GetThingInstance(poArgs, 0, &pThing))
return Py_BuildException();
float x = 0.0f, y = 0.0f, z = 0.0f;
if (!PyTuple_GetFloat(poArgs, 1, &x) || !PyTuple_GetFloat(poArgs, 2, &y) || !PyTuple_GetFloat(poArgs, 3, &z))
return Py_BuildException();
pThing->scale = {x, y, z};
return Py_BuildNone();
}
} // namespace
void initgrp() {
static PyMethodDef methods[] = {
{"InitScreenEffect", grpInitScreenEffect, METH_VARARGS, nullptr},
{"Culling", grpCulling, METH_VARARGS, nullptr},
{"ClearDepthBuffer", grpClearDepthBuffer, METH_VARARGS, nullptr},
{"Identity", grpIdentity, METH_VARARGS, nullptr},
{"GenerateColor", grp_GenerateColor, METH_VARARGS, nullptr},
{"PopState", grpPopState, METH_VARARGS, nullptr},
{"PushState", grpPushState, METH_VARARGS, nullptr},
{"PushMatrix", grpPushMatrix, METH_VARARGS, nullptr},
{"PopMatrix", grpPopMatrix, METH_VARARGS, nullptr},
{"Translate", grpTranslate, METH_VARARGS, nullptr},
{"Rotate", grpRotate, METH_VARARGS, nullptr},
{"SetColorRenderState", grpSetColorRenderState, METH_VARARGS, nullptr},
{"SetAroundCamera", grpSetAroundCamera, METH_VARARGS, nullptr},
{"SetPositionCamera", grpSetPositionCamera, METH_VARARGS, nullptr},
{"SetEyeCamera", grpSetEyeCamera, METH_VARARGS, nullptr},
{"SetPerspective", grpSetPerspective, METH_VARARGS, nullptr},
{"SetOrtho2d", grpSetOrtho2D, METH_VARARGS, nullptr},
{"SetOrtho3d", grpSetOrtho3D, METH_VARARGS, nullptr},
{"SetColor", grp_SetColor, METH_VARARGS, nullptr},
{"SetAlpha", grp_SetAlpha, METH_VARARGS, nullptr},
{"SetDiffuseColor", grp_SetDiffuseColor, METH_VARARGS, nullptr},
{"SetClearColor", grp_SetClearColor, METH_VARARGS, nullptr},
{"GetCursorPosition3d", grpGetCursorPosition3d, METH_VARARGS, nullptr},
{"SetCursorPosition", grpSetCursorPosition, METH_VARARGS, nullptr},
{"RenderLine", grp_RenderLine, METH_VARARGS, nullptr},
{"RenderBox", grp_RenderBox, METH_VARARGS, nullptr},
{"RenderRoundBox", grp_RenderRoundBox, METH_VARARGS, nullptr},
{"RenderBox3d", grpRenderBox3d, METH_VARARGS, nullptr},
{"RenderBar", grp_RenderBar, METH_VARARGS, nullptr},
{"RenderBar3d", grpRenderBar3d, METH_VARARGS, nullptr},
{"RenderGradationBar", grp_RenderGradationBar, METH_VARARGS, nullptr},
{"RenderCube", grpRenderCube, METH_VARARGS, nullptr},
{"RenderDownButton", grpRenderDownButton, METH_VARARGS, nullptr},
{"RenderUpButton", grpRenderUpButton, METH_VARARGS, nullptr},
{"GetAvailableMemory", grpGetAvailableMemory, METH_VARARGS, nullptr},
{"SaveScreenShot", grpSaveScreenShot, METH_VARARGS, nullptr},
{"SaveScreenShotToPath", grpSaveScreenShotToPath, METH_VARARGS, nullptr},
{"SetGamma", grpSetGamma, METH_VARARGS, nullptr},
{"SetInterfaceRenderState", grpSetInterfaceRenderState, METH_VARARGS, nullptr},
{"SetGameRenderState", grpSetGameRenderState, METH_VARARGS, nullptr},
{"SetViewport", grpSetViewport, METH_VARARGS, nullptr},
{"RestoreViewport", grpRestoreViewport, METH_VARARGS, nullptr},
{"SetOmniLight", grpSetOmniLight, METH_VARARGS, nullptr},
{"GetCameraPosition", grpGetCameraPosition, METH_VARARGS, nullptr},
{"GetTargetPosition", grpGetTargetPosition, METH_VARARGS, nullptr},
{"CreateTextBar", grpCreateTextBar, METH_VARARGS, nullptr},
{"CreateBigTextBar", grpCreateBigTextBar, METH_VARARGS, nullptr},
{"DestroyTextBar", grpDestroyTextBar, METH_VARARGS, nullptr},
{"RenderTextBar", grpRenderTextBar, METH_VARARGS, nullptr},
{"TextBarTextOut", grpTextBarTextOut, METH_VARARGS, nullptr},
{"TextBarSetTextColor", grpTextBarSetTextColor, METH_VARARGS, nullptr},
{"TextBarGetTextExtent", grpTextBarGetTextExtent, METH_VARARGS, nullptr},
{"ClearTextBar", grpClearTextBar, METH_VARARGS, nullptr},
{"SetTextBarClipRect", grpSetTextBarClipRect, METH_VARARGS, nullptr},
{nullptr, nullptr, 0, nullptr}
};
Py_InitModule("grp", methods);
}
void initgrpImage() {
static PyMethodDef methods[] = {
{"Render", grpImageRender, METH_VARARGS, nullptr},
{"SetPosition", grpSetImagePosition, METH_VARARGS, nullptr},
{"Generate", grpImageGenerate, METH_VARARGS, nullptr},
{"GenerateExpanded", grpImageGenerateExpanded, METH_VARARGS, nullptr},
{"GenerateFromHandle", grpImageGenerateFromHandle, METH_VARARGS, nullptr},
{"Delete", grpImageDelete, METH_VARARGS, nullptr},
{"DeleteExpanded", grpImageDeleteExpanded, METH_VARARGS, nullptr},
{"SetFileName", grpImageSetFileName, METH_VARARGS, nullptr},
{"SetOrigin", grpSetImageOrigin, METH_VARARGS, nullptr},
{"SetRotation", grpSetImageRotation, METH_VARARGS, nullptr},
{"SetScale", grpSetImageScale, METH_VARARGS, nullptr},
{"SetRenderingRect", grpSetRenderingRect, METH_VARARGS, nullptr},
{"SetDiffuseColor", grpSetImageDiffuseColor, METH_VARARGS, nullptr},
{"GetWidth", grpGetWidth, METH_VARARGS, nullptr},
{"GetHeight", grpGetHeight, METH_VARARGS, nullptr},
{nullptr, nullptr, 0, nullptr}
};
Py_InitModule("grpImage", methods);
}
void initgrpText() {
static PyMethodDef methods[] = {
{"Update", grpTextUpdate, METH_VARARGS, nullptr},
{"Render", grpTextRender, METH_VARARGS, nullptr},
{"Generate", grpTextGenerate, METH_VARARGS, nullptr},
{"Destroy", grpTextDestroy, METH_VARARGS, nullptr},
{"SetFontName", grpTextSetFontName, METH_VARARGS, nullptr},
{"SetFontColor", grpTextSetFontColor, METH_VARARGS, nullptr},
{"SetOutLineColor", grpTextSetOutLineColor, METH_VARARGS, nullptr},
{"SetSecret", grpTextSetSecret, METH_VARARGS, nullptr},
{"SetOutline", grpTextOutline, METH_VARARGS, nullptr},
{"SetText", grpTextSetText, METH_VARARGS, nullptr},
{"GetText", grpTextGetText, METH_VARARGS, nullptr},
{"GetSize", grpTextGetSize, METH_VARARGS, nullptr},
{"SetPosition", grpTextSetPosition, METH_VARARGS, nullptr},
{"ShowCursor", grpTextShowCursor, METH_VARARGS, nullptr},
{"HideCursor", grpTextHideCursor, METH_VARARGS, nullptr},
{"SetHorizontalAlign", grpSetHorizontalAlign, METH_VARARGS, nullptr},
{"SetVerticalAlign", grpSetVerticalAlign, METH_VARARGS, nullptr},
{"SetMax", grpSetMax, METH_VARARGS, nullptr},
{"GetSplitingTextLineCount", grpGetSplitingTextLineCount, METH_VARARGS, nullptr},
{"GetSplitingTextLine", grpGetSplitingTextLine, METH_VARARGS, nullptr},
{"PixelPositionToCharacterPosition", grpTextPixelPositionToCharacterPosition, METH_VARARGS, nullptr},
{nullptr, nullptr, 0, nullptr}
};
Py_InitModule("grpText", methods);
}
void initgrpThing() {
static PyMethodDef methods[] = {
{"Update", grpThingUpdate, METH_VARARGS, nullptr},
{"Render", grpThingRender, METH_VARARGS, nullptr},
{"SetPosition", grpSetThingPosition, METH_VARARGS, nullptr},
{"Generate", grpThingGenerate, METH_VARARGS, nullptr},
{"Delete", grpThingDelete, METH_VARARGS, nullptr},
{"SetFileName", grpThingSetFileName, METH_VARARGS, nullptr},
{"SetRotation", grpSetThingRotation, METH_VARARGS, nullptr},
{"SetScale", grpSetThingScale, METH_VARARGS, nullptr},
{nullptr, nullptr, 0, nullptr}
};
Py_InitModule("grpThing", methods);
}
} // namespace ava2