From 75c3ceb26453f2b265c3eabdd7518d26abfeed1c Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Tue, 10 Dec 2019 09:16:20 +0100 Subject: [PATCH] Use structs with casting, without classes replication --- Makefile.am | 4 ++ examples/03.CustomSceneNode.c | 14 ++-- include/CIrrCompileConfig.h | 28 ++++++++ include/EMaterialTypes.h | 54 ++++++++++++++ include/IVideoDriver.h | 2 +- include/SColor.h | 8 +-- include/SMaterial.h | 132 +++++++++++++++++++++++++++++++++- include/SMaterialLayer.h | 45 ++++++++++++ include/aabbox3d.h | 8 +-- include/cirrlicht.h | 4 ++ include/irrMath.h | 27 +++++++ src/CIrrlicht.cpp | 7 +- src/ISceneManager.cpp | 10 +-- src/IVideoDriver.cpp | 8 +-- src/aabbox3d.cpp | 22 +++--- 15 files changed, 325 insertions(+), 48 deletions(-) create mode 100644 include/CIrrCompileConfig.h create mode 100644 include/EMaterialTypes.h create mode 100644 include/SMaterialLayer.h create mode 100644 include/irrMath.h diff --git a/Makefile.am b/Makefile.am index f0f2546..0c0f90d 100644 --- a/Makefile.am +++ b/Makefile.am @@ -16,10 +16,12 @@ libCIrrlicht_la_CPPFLAGS = -I$(top_srcdir)/include libCIrrlicht_la_LDFLAGS = -version-info 0:1 pkginclude_HEADERS = \ include/aabbox3d.h \ + include/CIrrCompileConfig.h \ include/cirrlicht.h \ include/dimension2d.h \ include/EDriverTypes.h \ include/EMaterialFlags.h \ + include/EMaterialTypes.h \ include/EPrimitiveTypes.h \ include/IAnimatedMesh.h \ include/IAnimatedMeshMD2.h \ @@ -34,6 +36,7 @@ pkginclude_HEADERS = \ include/IMeshSceneNode.h \ include/IReferenceCounted.h \ include/IrrlichtDevice.h \ + include/irrMath.h \ include/irrTypes.h \ include/ISceneManager.h \ include/ISceneNode.h \ @@ -47,6 +50,7 @@ pkginclude_HEADERS = \ include/SExposedVideoData.h \ include/SKeyMap.h \ include/SMaterial.h \ + include/SMaterialLayer.h \ include/SVertexIndex.h \ include/vector2d.h \ include/vector3d.h diff --git a/examples/03.CustomSceneNode.c b/examples/03.CustomSceneNode.c index d9a161a..9d74614 100644 --- a/examples/03.CustomSceneNode.c +++ b/examples/03.CustomSceneNode.c @@ -68,7 +68,7 @@ int main() irr_scene_addCameraSceneNode(smgr, NULL, &position, &lookat, -1, true); // create our custom scene node - irr_scene_ISceneNode *myNode; + irr_scene_ISceneNode* myNode; irr_core_aabbox3d_f32 box; irr_video_S3DVertex vertices[] = { @@ -78,18 +78,20 @@ int main() { {0, 20, 0}, {0, 1, 1}, {255, 255, 255, 0}, {1, 0} }, { {-10, 0, -10}, {0, 0, 1}, {255, 0, 255, 0}, {0, 0} } }; - irr_video_SMaterial material = {false, false}; + irr_video_SMaterial material = S_MATERIAL_DEFAULT; + material.wireframe = false; + material.lighting = false; - irr_core_aabbox3d_reset(box, vertices[0].pos); + irr_core_aabbox3d_reset(&box, &vertices[0].pos); for (int i=1; i<4; i++) { - irr_core_aabbox3d_addInternalPoint(box, vertices[i].pos); + irr_core_aabbox3d_addInternalPoint(&box, &vertices[i].pos); } void customRender() { uint16_t indices[] = {0, 2, 3, 2, 1, 3, 1, 0, 3, 2, 0, 1}; - irr_video_setMaterial(driver, material); + irr_video_setMaterial(driver, &material); irr_video_setTransform(driver, irr_video_ETS_WORLD, irr_scene_getAbsoluteTransformation(myNode)); @@ -128,6 +130,8 @@ int main() if (anim) { irr_scene_addAnimator(myNode, anim); + irr_drop(anim); + anim = NULL; } // loop diff --git a/include/CIrrCompileConfig.h b/include/CIrrCompileConfig.h new file mode 100644 index 0000000..51b6288 --- /dev/null +++ b/include/CIrrCompileConfig.h @@ -0,0 +1,28 @@ +/* c-irrlicht --- C bindings for Irrlicht Engine + + Copyright (C) 2019 Javier Sancho + + This file is part of c-irrlicht. + + c-irrlicht is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + c-irrlicht 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 Lesser General Public + License along with guile-irrlicht. If not, see + . +*/ + +#ifndef __C_IRR_COMPILE_CONFIG_H_INCLUDED__ +#define __C_IRR_COMPILE_CONFIG_H_INCLUDED__ + +//! Maximum number of texture an SMaterial can have, up to 8 are supported by Irrlicht. +#define _IRR_MATERIAL_MAX_TEXTURES_ 4 + +#endif diff --git a/include/EMaterialTypes.h b/include/EMaterialTypes.h new file mode 100644 index 0000000..09cfa11 --- /dev/null +++ b/include/EMaterialTypes.h @@ -0,0 +1,54 @@ +/* c-irrlicht --- C bindings for Irrlicht Engine + + Copyright (C) 2019 Javier Sancho + + This file is part of c-irrlicht. + + c-irrlicht is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + c-irrlicht 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 Lesser General Public + License along with guile-irrlicht. If not, see + . +*/ + +#ifndef __C_E_MATERIAL_TYPES_H_INCLUDED__ +#define __C_E_MATERIAL_TYPES_H_INCLUDED__ + +typedef enum + { + irr_video_EMT_SOLID = 0, + irr_video_EMT_SOLID_2_LAYER, + irr_video_EMT_LIGHTMAP, + irr_video_EMT_LIGHTMAP_ADD, + irr_video_EMT_LIGHTMAP_M2, + irr_video_EMT_LIGHTMAP_M4, + irr_video_EMT_LIGHTMAP_LIGHTING, + irr_video_EMT_LIGHTMAP_LIGHTING_M2, + irr_video_EMT_LIGHTMAP_LIGHTING_M4, + irr_video_EMT_DETAIL_MAP, + irr_video_EMT_SPHERE_MAP, + irr_video_EMT_REFLECTION_2_LAYER, + irr_video_EMT_TRANSPARENT_ADD_COLOR, + irr_video_EMT_TRANSPARENT_ALPHA_CHANNEL, + irr_video_EMT_TRANSPARENT_ALPHA_CHANNEL_REF, + irr_video_EMT_TRANSPARENT_VERTEX_ALPHA, + irr_video_EMT_TRANSPARENT_REFLECTION_2_LAYER, + irr_video_EMT_NORMAL_MAP_SOLID, + irr_video_EMT_NORMAL_MAP_TRANSPARENT_ADD_COLOR, + irr_video_EMT_NORMAL_MAP_TRANSPARENT_VERTEX_ALPHA, + irr_video_EMT_PARALLAX_MAP_SOLID, + irr_video_EMT_PARALLAX_MAP_TRANSPARENT_ADD_COLOR, + irr_video_EMT_PARALLAX_MAP_TRANSPARENT_VERTEX_ALPHA, + irr_video_EMT_ONETEXTURE_BLEND, + irr_video_EMT_FORCE_32BIT = 0x7fffffff + } irr_video_E_MATERIAL_TYPE; + +#endif diff --git a/include/IVideoDriver.h b/include/IVideoDriver.h index a03cbde..2febea7 100644 --- a/include/IVideoDriver.h +++ b/include/IVideoDriver.h @@ -112,7 +112,7 @@ extern "C" { void irr_video_setMaterial(irr_video_IVideoDriver* driver, - const irr_video_SMaterial material); + irr_video_SMaterial* material); void irr_video_setTransform(irr_video_IVideoDriver* driver, diff --git a/include/SColor.h b/include/SColor.h index ccd8a1e..6512315 100644 --- a/include/SColor.h +++ b/include/SColor.h @@ -26,10 +26,10 @@ typedef struct { - uint32_t a; - uint32_t r; - uint32_t g; - uint32_t b; + uint8_t a; + uint8_t r; + uint8_t g; + uint8_t b; } irr_video_SColor; #endif diff --git a/include/SMaterial.h b/include/SMaterial.h index 5a27f34..a26eb69 100644 --- a/include/SMaterial.h +++ b/include/SMaterial.h @@ -22,12 +22,140 @@ #ifndef __C_IRR_S_MATERIAL_H_INCLUDED__ #define __C_IRR_S_MATERIAL_H_INCLUDED__ +#include +#include #include +#include "EMaterialTypes.h" +#include "irrMath.h" +#include "SColor.h" +#include "SMaterialLayer.h" + +#define MATERIAL_MAX_TEXTURES _IRR_MATERIAL_MAX_TEXTURES_ + +typedef enum + { + irr_video_EBO_NONE = 0, + irr_video_EBO_ADD, + irr_video_EBO_SUBTRACT, + irr_video_EBO_REVSUBTRACT, + irr_video_EBO_MIN, + irr_video_EBO_MAX, + irr_video_EBO_MIN_FACTOR, + irr_video_EBO_MAX_FACTOR, + irr_video_EBO_MIN_ALPHA, + irr_video_EBO_MAX_ALPHA + } irr_video_E_BLEND_OPERATION; + +typedef enum + { + irr_video_ECFN_NEVER = 0, + irr_video_ECFN_LESSEQUAL = 1, + irr_video_ECFN_EQUAL = 2, + irr_video_ECFN_LESS, + irr_video_ECFN_NOTEQUAL, + irr_video_ECFN_GREATEREQUAL, + irr_video_ECFN_GREATER, + irr_video_ECFN_ALWAYS + } irr_video_E_COMPARISON_FUNC; + +typedef enum + { + irr_video_ECP_NONE = 0, + irr_video_ECP_ALPHA = 1, + irr_video_ECP_RED = 2, + irr_video_ECP_GREEN = 4, + irr_video_ECP_BLUE = 8, + irr_video_ECP_RGB = 14, + irr_video_ECP_ALL = 15 + } irr_video_E_COLOR_PLANE; + +typedef enum + { + irr_video_EAAM_OFF = 0, + irr_video_EAAM_SIMPLE = 1, + irr_video_EAAM_QUALITY = 3, + irr_video_EAAM_LINE_SMOOTH = 4, + irr_video_EAAM_POINT_SMOOTH = 8, + irr_video_EAAM_FULL_BASIC = 15, + irr_video_EAAM_ALPHA_TO_COVERAGE = 16 + } irr_video_E_ANTI_ALIASING_MODE; + +typedef enum + { + irr_video_ECM_NONE = 0, + irr_video_ECM_DIFFUSE, + irr_video_ECM_AMBIENT, + irr_video_ECM_EMISSIVE, + irr_video_ECM_SPECULAR, + irr_video_ECM_DIFFUSE_AND_AMBIENT + } irr_video_E_COLOR_MATERIAL; + +typedef enum + { + irr_video_EPO_BACK = 0, + irr_video_EPO_FRONT = 1 + } irr_video_E_POLYGON_OFFSET; + typedef struct { - bool wireframe; - bool lighting; + irr_video_SMaterialLayer textureLayer[MATERIAL_MAX_TEXTURES]; + irr_video_E_MATERIAL_TYPE materialType; + irr_video_SColor ambientColor; + irr_video_SColor diffuseColor; + irr_video_SColor emissiveColor; + irr_video_SColor specularColor; + float_t shininess; + float_t materialTypeParam; + float_t materialTypeParam2; + float_t thickness; + uint8_t zBuffer; + uint8_t antiAliasing; + uint8_t colorMask:4; + uint8_t colorMaterial:3; + irr_video_E_BLEND_OPERATION blendOperation:4; + uint8_t polygonOffsetFactor:3; + irr_video_E_POLYGON_OFFSET polygonOffsetDirection:1; + bool wireframe:1; + bool pointCloud:1; + bool gouraudShading:1; + bool lighting:1; + bool zWriteEnable:1; + bool backfaceCulling:1; + bool frontfaceCulling:1; + bool fogEnable:1; + bool normalizeNormals:1; + bool useMipMaps:1; } irr_video_SMaterial; +#define S_MATERIAL_DEFAULT \ +{ \ + .materialType = irr_video_EMT_SOLID, \ + .ambientColor = {255, 255, 255, 255}, \ + .diffuseColor = {255, 255, 255, 255}, \ + .emissiveColor = {0, 0, 0, 0}, \ + .specularColor = {255, 255, 255, 255}, \ + .shininess = 0, \ + .materialTypeParam = 0, \ + .materialTypeParam2 = 0, \ + .thickness = 1, \ + .zBuffer = irr_video_ECFN_LESSEQUAL, \ + .antiAliasing = irr_video_EAAM_SIMPLE, \ + .colorMask = irr_video_ECP_ALL, \ + .colorMaterial = irr_video_ECM_DIFFUSE, \ + .blendOperation = irr_video_EBO_NONE, \ + .polygonOffsetFactor = 0, \ + .polygonOffsetDirection = irr_video_EPO_FRONT, \ + .wireframe = false, \ + .pointCloud = false, \ + .gouraudShading = true, \ + .lighting = true, \ + .zWriteEnable = true, \ + .backfaceCulling = true, \ + .frontfaceCulling = false, \ + .fogEnable = false, \ + .normalizeNormals = false, \ + .useMipMaps = true \ +} + #endif diff --git a/include/SMaterialLayer.h b/include/SMaterialLayer.h new file mode 100644 index 0000000..5c60401 --- /dev/null +++ b/include/SMaterialLayer.h @@ -0,0 +1,45 @@ +/* c-irrlicht --- C bindings for Irrlicht Engine + + Copyright (C) 2019 Javier Sancho + + This file is part of c-irrlicht. + + c-irrlicht is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + c-irrlicht 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 Lesser General Public + License along with guile-irrlicht. If not, see + . +*/ + +#ifndef __C_IRR_S_MATERIAL_LAYER_H_INCLUDED__ +#define __C_IRR_S_MATERIAL_LAYER_H_INCLUDED__ + +#include +#include +#include "ITexture.h" +#include "matrix4.h" + +typedef struct +{ + irr_video_ITexture* texture; + uint8_t textureWrapU; + uint8_t textureWrapV; + bool bilinearFilter; + bool trilinearFilter; + uint8_t anisotropicFilter; + int8_t LODBias; + // private + // Do not access to these elements + irr_core_matrix4* matrixAllocator; + irr_core_matrix4* textureMatrix; +} irr_video_SMaterialLayer; + +#endif diff --git a/include/aabbox3d.h b/include/aabbox3d.h index 1940c20..e5d9a63 100644 --- a/include/aabbox3d.h +++ b/include/aabbox3d.h @@ -34,12 +34,12 @@ extern "C" { #endif void - irr_core_aabbox3d_addInternalPoint(irr_core_aabbox3d_f32 box, - irr_core_vector3df p); + irr_core_aabbox3d_addInternalPoint(irr_core_aabbox3d_f32* box, + irr_core_vector3df* p); void - irr_core_aabbox3d_reset(irr_core_aabbox3d_f32 box, - irr_core_vector3df initValue); + irr_core_aabbox3d_reset(irr_core_aabbox3d_f32* box, + irr_core_vector3df* initValue); #ifdef __cplusplus } diff --git a/include/cirrlicht.h b/include/cirrlicht.h index f8b8e4f..c90a4e9 100644 --- a/include/cirrlicht.h +++ b/include/cirrlicht.h @@ -26,9 +26,11 @@ #include #include "aabbox3d.h" +#include "CIrrCompileConfig.h" #include "dimension2d.h" #include "EDriverTypes.h" #include "EMaterialFlags.h" +#include "EMaterialTypes.h" #include "EPrimitiveTypes.h" #include "IAnimatedMeshMD2.h" #include "IAnimatedMeshSceneNode.h" @@ -42,6 +44,7 @@ #include "IMeshSceneNode.h" #include "IReferenceCounted.h" #include "IrrlichtDevice.h" +#include "irrMath.h" #include "irrTypes.h" #include "ISceneManager.h" #include "ISceneNode.h" @@ -55,6 +58,7 @@ #include "SExposedVideoData.h" #include "SKeyMap.h" #include "SMaterial.h" +#include "SMaterialLayer.h" #include "SVertexIndex.h" #include "vector2d.h" #include "vector3d.h" diff --git a/include/irrMath.h b/include/irrMath.h new file mode 100644 index 0000000..9029fa0 --- /dev/null +++ b/include/irrMath.h @@ -0,0 +1,27 @@ +/* c-irrlicht --- C bindings for Irrlicht Engine + + Copyright (C) 2019 Javier Sancho + + This file is part of c-irrlicht. + + c-irrlicht is free software; you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as + published by the Free Software Foundation; either version 3 of the + License, or (at your option) any later version. + + c-irrlicht 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 Lesser General Public + License along with guile-irrlicht. If not, see + . +*/ + +#ifndef __C_IRR_MATH_H_INCLUDED__ +#define __C_IRR_MATH_H_INCLUDED__ + +// TODO + +#endif diff --git a/src/CIrrlicht.cpp b/src/CIrrlicht.cpp index 0c35adb..3ff0db2 100644 --- a/src/CIrrlicht.cpp +++ b/src/CIrrlicht.cpp @@ -31,13 +31,10 @@ extern "C" { bool stencilbuffer, bool vsync) { - irr::core::dimension2d wsize = - irr::core::dimension2d(windowSize->width, - windowSize->height); irr::IrrlichtDevice *device = irr::createDevice((irr::video::E_DRIVER_TYPE)deviceType, - wsize, bits, fullscreen, stencilbuffer, - vsync); + *(irr::core::dimension2d*)windowSize, + bits, fullscreen, stencilbuffer, vsync); return (irr_IrrlichtDevice*)device; } } diff --git a/src/ISceneManager.cpp b/src/ISceneManager.cpp index e7cefb8..42bfac5 100644 --- a/src/ISceneManager.cpp +++ b/src/ISceneManager.cpp @@ -64,9 +64,7 @@ public: virtual const irr::core::aabbox3d& getBoundingBox() const { - irr_core_aabbox3d_f32* box = GetBoundingBox(); - irr::core::aabbox3d* irrBox = new irr::core::aabbox3d(); - return *irrBox; + return *(irr::core::aabbox3d*)GetBoundingBox(); } virtual irr::u32 getMaterialCount() const @@ -76,11 +74,7 @@ public: virtual irr::video::SMaterial& getMaterial(irr::u32 i) { - irr_video_SMaterial* material = GetMaterial(i); - irr::video::SMaterial* irrMaterial = new irr::video::SMaterial(); - irrMaterial->Wireframe = material->wireframe; - irrMaterial->Lighting = material->lighting; - return *irrMaterial; + return *(irr::video::SMaterial*)GetMaterial(i); } }; diff --git a/src/IVideoDriver.cpp b/src/IVideoDriver.cpp index e04dbf6..6bc1020 100644 --- a/src/IVideoDriver.cpp +++ b/src/IVideoDriver.cpp @@ -146,13 +146,9 @@ extern "C" { void irr_video_setMaterial(irr_video_IVideoDriver* driver, - const irr_video_SMaterial material) + irr_video_SMaterial* material) { - irr::video::SMaterial irrMaterial = irr::video::SMaterial(); - irrMaterial.Lighting = material.lighting; - irrMaterial.Wireframe = material.wireframe; - - ((irr::video::IVideoDriver*)driver)->setMaterial(irrMaterial); + ((irr::video::IVideoDriver*)driver)->setMaterial(*(irr::video::SMaterial*)material); } void diff --git a/src/aabbox3d.cpp b/src/aabbox3d.cpp index 4209425..69b1f78 100644 --- a/src/aabbox3d.cpp +++ b/src/aabbox3d.cpp @@ -19,27 +19,23 @@ . */ +#include #include "aabbox3d.h" extern "C" { void - irr_core_aabbox3d_addInternalPoint(irr_core_aabbox3d_f32 box, - irr_core_vector3df p) + irr_core_aabbox3d_addInternalPoint(irr_core_aabbox3d_f32* box, + irr_core_vector3df* p) { - if (p.x > box.maxEdge.x) box.maxEdge.x = p.x; - if (p.y > box.maxEdge.y) box.maxEdge.y = p.y; - if (p.z > box.maxEdge.z) box.maxEdge.z = p.z; - - if (p.x < box.minEdge.x) box.minEdge.x = p.x; - if (p.y < box.minEdge.y) box.minEdge.y = p.y; - if (p.z < box.minEdge.z) box.minEdge.z = p.z; + ((irr::core::aabbox3d*)box) + ->addInternalPoint(*(irr::core::vector3df*)p); } void - irr_core_aabbox3d_reset(irr_core_aabbox3d_f32 box, - irr_core_vector3df initValue) + irr_core_aabbox3d_reset(irr_core_aabbox3d_f32* box, + irr_core_vector3df* initValue) { - box.minEdge = initValue; - box.maxEdge = initValue; + ((irr::core::aabbox3d*)box) + ->reset(*(irr::core::vector3df*)initValue); } } -- 2.39.5