mirror of
https://github.com/H-uru/korman.git
synced 2025-07-14 02:27:36 -04:00
C Korlib code de-duplication
Moves all PyHSPlasma struct definitions into a private header file. Also adds move constructor and assignment support to PyObjectRef.
This commit is contained in:
@ -20,6 +20,7 @@ endif()
|
||||
set(korlib_HEADERS
|
||||
bumpmap.h
|
||||
korlib.h
|
||||
PyHSPlasma_private.h
|
||||
sound.h
|
||||
texture.h
|
||||
)
|
||||
|
40
korlib/PyHSPlasma_private.h
Normal file
40
korlib/PyHSPlasma_private.h
Normal file
@ -0,0 +1,40 @@
|
||||
/* This file is part of Korman.
|
||||
*
|
||||
* Korman is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Korman is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Korman. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef _PYHSPLASMA_PRIVATE_H
|
||||
#define _PYHSPLASMA_PRIVATE_H
|
||||
|
||||
#include "korlib.h"
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
class plMipmap* fThis;
|
||||
bool fPyOwned;
|
||||
} pyMipmap;
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
class hsStream* fThis;
|
||||
bool fPyOwned;
|
||||
} pyStream;
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
class plWAVHeader* fThis;
|
||||
bool fPyOwned;
|
||||
} pyWAVHeader;
|
||||
|
||||
#endif
|
@ -15,6 +15,8 @@
|
||||
*/
|
||||
|
||||
#include "bumpmap.h"
|
||||
#include "PyHSPlasma_private.h"
|
||||
|
||||
#include <PRP/Surface/plMipmap.h>
|
||||
|
||||
static uint32_t MakeUInt32Color(float r, float g, float b, float a) {
|
||||
@ -24,14 +26,6 @@ static uint32_t MakeUInt32Color(float r, float g, float b, float a) {
|
||||
(uint32_t(b * 255.9f) << 0);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
plMipmap* fThis;
|
||||
bool fPyOwned;
|
||||
} pyMipmap;
|
||||
|
||||
extern "C" {
|
||||
|
||||
PyObject* create_bump_LUT(PyObject*, PyObject* args) {
|
||||
static const int kLUTHeight = 16;
|
||||
static const int kLUTWidth = 16;
|
||||
@ -110,5 +104,3 @@ PyObject* create_bump_LUT(PyObject*, PyObject* args) {
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -19,11 +19,6 @@
|
||||
|
||||
#include "korlib.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
PyObject* create_bump_LUT(PyObject*, PyObject* args);
|
||||
|
||||
};
|
||||
|
||||
#endif // _KORLIB_BUMPMAP_H
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <Python.h>
|
||||
#include <utility>
|
||||
|
||||
#define _pycs(x) const_cast<char*>(x)
|
||||
#define arrsize(a) (sizeof(a) / sizeof((a)[0]))
|
||||
@ -31,14 +32,29 @@ class PyObjectRef {
|
||||
public:
|
||||
PyObjectRef() : m_object() { }
|
||||
PyObjectRef(PyObject* o) : m_object(o) { }
|
||||
PyObjectRef(const PyObjectRef& copy) = delete;
|
||||
PyObjectRef(PyObjectRef&& move) {
|
||||
m_object = move.m_object;
|
||||
move.m_object = NULL;
|
||||
}
|
||||
|
||||
~PyObjectRef() { Py_XDECREF(m_object); }
|
||||
|
||||
operator PyObject*() const { return m_object; }
|
||||
|
||||
PyObjectRef& operator =(PyObject* rhs) {
|
||||
Py_XDECREF(m_object);
|
||||
m_object = rhs;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PyObjectRef& operator =(PyObjectRef&& move) {
|
||||
m_object = move.m_object;
|
||||
move.m_object = NULL;
|
||||
return *this;
|
||||
}
|
||||
|
||||
PyObjectRef& operator =(const PyObjectRef& copy) = delete;
|
||||
};
|
||||
|
||||
#endif // _KORLIB_H
|
||||
|
@ -21,8 +21,6 @@
|
||||
// This konstant is compared against that in the Python module to prevent sneaky errors...
|
||||
#define KORLIB_API_VERSION 1
|
||||
|
||||
extern "C" {
|
||||
|
||||
static PyMethodDef korlib_Methods[] = {
|
||||
{ _pycs("create_bump_LUT"), (PyCFunction)create_bump_LUT, METH_VARARGS, NULL },
|
||||
{ _pycs("inspect_vorbisfile"), (PyCFunction)inspect_vorbisfile, METH_VARARGS, NULL },
|
||||
@ -43,6 +41,8 @@ static PyModuleDef korlib_Module = {
|
||||
NULL, /* m_free */
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
|
||||
PyMODINIT_FUNC PyInit__korlib() {
|
||||
PyObject* module = PyModule_Create(&korlib_Module);
|
||||
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
#include "sound.h"
|
||||
#include "PyHSPlasma_private.h"
|
||||
|
||||
#include <PRP/Audio/plSoundBuffer.h>
|
||||
#include <Stream/hsStream.h>
|
||||
@ -22,20 +23,6 @@
|
||||
|
||||
static const int BITS_PER_SAMPLE = 16;
|
||||
|
||||
extern "C" {
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
hsStream* fThis;
|
||||
bool fPyOwned;
|
||||
} pyStream;
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
plWAVHeader* fThis;
|
||||
bool fPyOwned;
|
||||
} pyWAVHeader;
|
||||
|
||||
static size_t _read_stream(void* ptr, size_t size, size_t nmemb, void* datasource) {
|
||||
hsStream* s = static_cast<hsStream*>(datasource);
|
||||
// hsStream is a bit overzealous protecting us against overreads, so we need to
|
||||
@ -115,5 +102,3 @@ PyObject* inspect_vorbisfile(PyObject*, PyObject* args) {
|
||||
// Therefore, we only need to return the size.
|
||||
return PyLong_FromSsize_t(size);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -19,10 +19,6 @@
|
||||
|
||||
#include "korlib.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
PyObject* inspect_vorbisfile(PyObject*, PyObject* args);
|
||||
|
||||
};
|
||||
|
||||
#endif // _KORLIB_SOUND_H
|
||||
|
@ -15,6 +15,7 @@
|
||||
*/
|
||||
|
||||
#include "texture.h"
|
||||
#include "PyHSPlasma_private.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
# define WIN32_LEAN_AND_MEAN
|
||||
@ -205,12 +206,6 @@ typedef struct {
|
||||
bool m_imageInverted;
|
||||
} pyGLTexture;
|
||||
|
||||
typedef struct {
|
||||
PyObject_HEAD
|
||||
plMipmap* fThis;
|
||||
bool fPyOwned;
|
||||
} pyMipmap;
|
||||
|
||||
// ===============================================================================================
|
||||
|
||||
static void pyGLTexture_dealloc(pyGLTexture* self) {
|
||||
|
@ -19,13 +19,9 @@
|
||||
|
||||
#include "korlib.h"
|
||||
|
||||
extern "C" {
|
||||
|
||||
PyObject* scale_image(PyObject*, PyObject*, PyObject*);
|
||||
|
||||
extern PyTypeObject pyGLTexture_Type;
|
||||
PyObject* Init_pyGLTexture_Type();
|
||||
|
||||
};
|
||||
|
||||
#endif // _KORLIB_TEXTURE_H
|
||||
|
Reference in New Issue
Block a user