From 7e6e206937c54b100b45b2732edf19a7cc4a3edb Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Thu, 12 Dec 2019 09:26:09 +0100 Subject: [PATCH] Use structs with casting, without classes replication --- examples/01.HelloWorld.c | 2 +- examples/02.Quake3Map.c | 2 +- examples/03.CustomSceneNode.c | 10 +++--- include/SColor.h | 8 +++-- src/IGUIEnvironment.cpp | 9 +----- src/ISceneManager.cpp | 61 ++++++----------------------------- src/ISceneNode.cpp | 5 +-- src/IVideoDriver.cpp | 59 +++------------------------------ 8 files changed, 27 insertions(+), 129 deletions(-) diff --git a/examples/01.HelloWorld.c b/examples/01.HelloWorld.c index ed67469..a682692 100644 --- a/examples/01.HelloWorld.c +++ b/examples/01.HelloWorld.c @@ -74,7 +74,7 @@ int main() irr_scene_addCameraSceneNode(smgr, NULL, &position, &lookat, -1, true); // loop - irr_video_SColor bgcolor = {255, 100, 101, 140}; + irr_video_SColor bgcolor = MAKE_COLOR(255, 100, 101, 140); while (irr_run(device)) { irr_video_beginScene(driver, true, true, &bgcolor, NULL, NULL); diff --git a/examples/02.Quake3Map.c b/examples/02.Quake3Map.c index 8d2f279..6dcaafe 100644 --- a/examples/02.Quake3Map.c +++ b/examples/02.Quake3Map.c @@ -83,7 +83,7 @@ int main() // loop int lastFPS = -1; - irr_video_SColor bgcolor = {255, 200, 200, 200}; + irr_video_SColor bgcolor = MAKE_COLOR(255, 200, 200, 200); while (irr_run(device)) { if (irr_isWindowActive(device)) diff --git a/examples/03.CustomSceneNode.c b/examples/03.CustomSceneNode.c index 9d74614..e2715f6 100644 --- a/examples/03.CustomSceneNode.c +++ b/examples/03.CustomSceneNode.c @@ -73,10 +73,10 @@ int main() irr_video_S3DVertex vertices[] = { // pos, normal, color, tcoords - { {0, 0, 10}, {1, 1, 0}, {255, 0, 255, 255}, {0, 1} }, - { {10, 0, -10}, {1, 0, 0}, {255, 255, 0, 255}, {1, 1} }, - { {0, 20, 0}, {0, 1, 1}, {255, 255, 255, 0}, {1, 0} }, - { {-10, 0, -10}, {0, 0, 1}, {255, 0, 255, 0}, {0, 0} } + { {0, 0, 10}, {1, 1, 0}, MAKE_COLOR(255, 0, 255, 255), {0, 1} }, + { {10, 0, -10}, {1, 0, 0}, MAKE_COLOR(255, 255, 0, 255), {1, 1} }, + { {0, 20, 0}, {0, 1, 1}, MAKE_COLOR(255, 255, 255, 0), {1, 0} }, + { {-10, 0, -10}, {0, 0, 1}, MAKE_COLOR(255, 0, 255, 0), {0, 0} } }; irr_video_SMaterial material = S_MATERIAL_DEFAULT; material.wireframe = false; @@ -136,7 +136,7 @@ int main() // loop int frames = 0; - irr_video_SColor bgcolor = {0, 100, 100, 100}; + irr_video_SColor bgcolor = MAKE_COLOR(0, 100, 100, 100); while (irr_run(device)) { irr_video_beginScene(driver, true, true, &bgcolor, NULL, NULL); diff --git a/include/SColor.h b/include/SColor.h index 6512315..85f454f 100644 --- a/include/SColor.h +++ b/include/SColor.h @@ -26,10 +26,12 @@ typedef struct { - uint8_t a; - uint8_t r; - uint8_t g; uint8_t b; + uint8_t g; + uint8_t r; + uint8_t a; } irr_video_SColor; +#define MAKE_COLOR(a, r, g, b) {b, g, r, a} + #endif diff --git a/src/IGUIEnvironment.cpp b/src/IGUIEnvironment.cpp index f64f5a8..6fc3cfa 100644 --- a/src/IGUIEnvironment.cpp +++ b/src/IGUIEnvironment.cpp @@ -38,18 +38,11 @@ extern "C" { wchar_t *wtext = (wchar_t*)malloc((strlen(text) + 1) * sizeof(wchar_t)); mbstowcs(wtext, text, strlen(text) + 1); - // Make rectangle - irr::core::rect rect = - irr::core::rect(rectangle->x, - rectangle->y, - rectangle->x2, - rectangle->y2); - // Add static text irr::gui::IGUIStaticText *staticText = ((irr::gui::IGUIEnvironment*)guienv) ->addStaticText(wtext, - rect, + *(irr::core::rect*)rectangle, border, wordWrap, (irr::gui::IGUIElement*)parent, diff --git a/src/ISceneManager.cpp b/src/ISceneManager.cpp index 42bfac5..d01e785 100644 --- a/src/ISceneManager.cpp +++ b/src/ISceneManager.cpp @@ -89,29 +89,14 @@ extern "C" { irr_core_vector3df* scale, bool alsoAddIfMeshPointerZero) { - const irr::core::vector3df iPosition = - position ? - irr::core::vector3df(position->x, position->y, position->z) : - irr::core::vector3df(0, 0, 0); - - const irr::core::vector3df iRotation = - rotation ? - irr::core::vector3df(rotation->x, rotation->y, rotation->z) : - irr::core::vector3df(0, 0, 0); - - const irr::core::vector3df& iScale = - scale ? - irr::core::vector3df(scale->x, scale->y, scale->z) : - irr::core::vector3df(1, 1, 1); - return (irr_scene_IAnimatedMeshSceneNode*) ((irr::scene::ISceneManager*)smgr) ->addAnimatedMeshSceneNode((irr::scene::IAnimatedMesh*)mesh, (irr::scene::ISceneNode*)parent, id, - iPosition, - iRotation, - iScale, + position ? *(irr::core::vector3df*)position : irr::core::vector3df(0, 0, 0), + rotation ? *(irr::core::vector3df*)rotation : irr::core::vector3df(0, 0, 0), + scale ? *(irr::core::vector3df*)scale : irr::core::vector3df(1, 1, 1), alsoAddIfMeshPointerZero); } @@ -123,21 +108,11 @@ extern "C" { int32_t id, bool makeActive) { - const irr::core::vector3df& iPosition = - position ? - irr::core::vector3df(position->x, position->y, position->z) : - irr::core::vector3df(0, 0, 0); - - const irr::core::vector3df& iLookat = - lookat ? - irr::core::vector3df(lookat->x, lookat->y, lookat->z) : - irr::core::vector3df(0, 0, 100); - return (irr_scene_ICameraSceneNode*) ((irr::scene::ISceneManager*)smgr) ->addCameraSceneNode((irr::scene::ISceneNode*)parent, - iPosition, - iLookat, + position ? *(irr::core::vector3df*)position : irr::core::vector3df(0, 0, 0), + lookat ? *(irr::core::vector3df*)lookat : irr::core::vector3df(0, 0, 100), id, makeActive); } @@ -185,28 +160,13 @@ extern "C" { parent = irr_scene_getRootSceneNode(smgr); } - const irr::core::vector3df iPosition = - position ? - irr::core::vector3df(position->x, position->y, position->z) : - irr::core::vector3df(0, 0, 0); - - const irr::core::vector3df iRotation = - rotation ? - irr::core::vector3df(rotation->x, rotation->y, rotation->z) : - irr::core::vector3df(0, 0, 0); - - const irr::core::vector3df& iScale = - scale ? - irr::core::vector3df(scale->x, scale->y, scale->z) : - irr::core::vector3df(1, 1, 1); - CustomSceneNode *node = new CustomSceneNode((irr::scene::ISceneNode*)parent, (irr::scene::ISceneManager*)smgr, id, - iPosition, - iRotation, - iScale, + position ? *(irr::core::vector3df*)position : irr::core::vector3df(0, 0, 0), + rotation ? *(irr::core::vector3df*)rotation : irr::core::vector3df(0, 0, 0), + scale ? *(irr::core::vector3df*)scale : irr::core::vector3df(1, 1, 1), render, getBoundingBox, getMaterialCount, @@ -234,11 +194,8 @@ extern "C" { irr_scene_createRotationAnimator(irr_scene_ISceneManager* smgr, irr_core_vector3df* rotationSpeed) { - const irr::core::vector3df& irrRotationSpeed = - irr::core::vector3df(rotationSpeed->x, rotationSpeed->y, rotationSpeed->z); - return ((irr::scene::ISceneManager*)smgr) - ->createRotationAnimator(irrRotationSpeed); + ->createRotationAnimator(*(irr::core::vector3df*)rotationSpeed); } void diff --git a/src/ISceneNode.cpp b/src/ISceneNode.cpp index a2c90a7..ce80c97 100644 --- a/src/ISceneNode.cpp +++ b/src/ISceneNode.cpp @@ -62,11 +62,8 @@ extern "C" { irr_scene_setPosition(irr_scene_ISceneNode* node, irr_core_vector3df* newpos) { - const irr::core::vector3df newPosition = - irr::core::vector3df(newpos->x, newpos->y, newpos->z); - ((irr::scene::ISceneNode*)node) - ->setPosition(newPosition); + ->setPosition(*(irr::core::vector3df*)newpos); } } diff --git a/src/IVideoDriver.cpp b/src/IVideoDriver.cpp index 6bc1020..6133398 100644 --- a/src/IVideoDriver.cpp +++ b/src/IVideoDriver.cpp @@ -32,31 +32,17 @@ extern "C" { irr_video_SExposedVideoData* videoData, const irr_core_rect_s32* sourceRect) { - // Color - irr::video::SColor col = irr::video::SColor(color->a, color->r, - color->g, color->b); - // Video data // TODO irr::video::SExposedVideoData vdata = irr::video::SExposedVideoData(); - // Source rect - irr::core::rect rect; - if (sourceRect != NULL) - { - rect = irr::core::rect(sourceRect->x, - sourceRect->y, - sourceRect->x2, - sourceRect->y2); - } - // Begin scene return ((irr::video::IVideoDriver*)driver) ->beginScene(backBuffer, zBuffer, - col, + *(irr::video::SColor*)color, vdata, - sourceRect != NULL ? &rect : 0); + (irr::core::rect*)sourceRect); } void @@ -69,49 +55,12 @@ extern "C" { irr_scene_E_PRIMITIVE_TYPE pType, irr_video_E_INDEX_TYPE iType) { - // Convert vertices - irr::video::S3DVertex irrVertices[vertexCount]; - for (int i=0; ipos.x, vertex->pos.y, vertex->pos.z, - vertex->normal.x, vertex->normal.y, vertex->normal.z, - irr::video::SColor(vertex->color.a, - vertex->color.r, - vertex->color.g, - vertex->color.b), - vertex->tCoords.x, vertex->tCoords.y); - } - - // Convert indices - size_t indexListSize = - ((iType == irr_video_EIT_16BIT) ? sizeof(irr::u16) : sizeof(irr::u32)) * - primCount * 3; - void* irrIndexList = malloc(indexListSize); - - for (int i=0; idrawVertexPrimitiveList(&irrVertices[0], vertexCount, - irrIndexList, primCount, + ->drawVertexPrimitiveList(vertices, vertexCount, + indexList, primCount, (irr::video::E_VERTEX_TYPE)vType, (irr::scene::E_PRIMITIVE_TYPE)pType, (irr::video::E_INDEX_TYPE)iType); - - free(irrIndexList); } int -- 2.39.5