commit 2b9215ca24e7ece618bc77c5dd16c087d2ac6985
parent 4b3b56b77f664f9d1c60aaa921fb8eb0480aa597
Author: Vincent Forest <vincent.forest@meso-star.com>
Date: Fri, 19 Oct 2018 12:27:33 +0200
Refactor the htrdr_ground_create function
Move the setup of the geometry in a specific function. Time this
function and report the elapsed time.
Diffstat:
| M | src/htrdr_ground.c | | | 111 | ++++++++++++++++++++++++++++++++++++++++++++++++------------------------------- |
1 file changed, 67 insertions(+), 44 deletions(-)
diff --git a/src/htrdr_ground.c b/src/htrdr_ground.c
@@ -17,6 +17,7 @@
#include "htrdr_ground.h"
#include "htrdr_slab.h"
+#include <rsys/clock_time.h>
#include <rsys/cstr.h>
#include <rsys/double2.h>
#include <rsys/double3.h>
@@ -126,50 +127,20 @@ trace_ground
return RES_OK;
}
-static void
-release_ground(ref_T* ref)
-{
- struct htrdr_ground* ground;
- ASSERT(ref);
- ground = CONTAINER_OF(ref, struct htrdr_ground, ref);
- if(ground->view) S3D(scene_view_ref_put(ground->view));
- MEM_RM(ground->htrdr->allocator, ground);
-}
-
-/*******************************************************************************
- * Local functions
- ******************************************************************************/
-res_T
-htrdr_ground_create
- (struct htrdr* htrdr,
- const char* obj_filename,
- const int repeat_ground, /* Infinitely repeat the ground in X and Y */
- struct htrdr_ground** out_ground)
+static res_T
+setup_ground(struct htrdr_ground* ground, const char* obj_filename)
{
struct s3d_scene* scn = NULL;
struct s3daw* s3daw = NULL;
- struct htrdr_ground* ground = NULL;
- size_t ishape;
size_t nshapes;
+ size_t ishape;
res_T res = RES_OK;
- ASSERT(htrdr && obj_filename && out_ground);
+ ASSERT(ground && obj_filename);
- ground = MEM_CALLOC(htrdr->allocator, 1, sizeof(*ground));
- if(!ground) {
- res = RES_MEM_ERR;
- htrdr_log_err(htrdr,
- "%s: could not allocate the ground data structure -- %s.\n",
- FUNC_NAME, res_to_cstr(res));
- goto error;
- }
- ref_init(&ground->ref);
- ground->htrdr = htrdr;
- ground->repeat = repeat_ground;
-
- res = s3daw_create(&htrdr->logger, htrdr->allocator, NULL, NULL, htrdr->s3d,
- htrdr->verbose, &s3daw);
+ res = s3daw_create(&ground->htrdr->logger, ground->htrdr->allocator, NULL,
+ NULL, ground->htrdr->s3d, ground->htrdr->verbose, &s3daw);
if(res != RES_OK) {
- htrdr_log_err(htrdr,
+ htrdr_log_err(ground->htrdr,
"%s: could not create the Star-3DAW device -- %s.\n",
FUNC_NAME, res_to_cstr(res));
goto error;
@@ -177,14 +148,15 @@ htrdr_ground_create
res = s3daw_load(s3daw, obj_filename);
if(res != RES_OK) {
- htrdr_log_err(htrdr, "%s: could not load the obj file `%s' -- %s.\n",
+ htrdr_log_err(ground->htrdr,
+ "%s: could not load the obj file `%s' -- %s.\n",
FUNC_NAME, obj_filename, res_to_cstr(res));
goto error;
}
- res = s3d_scene_create(htrdr->s3d, &scn);
+ res = s3d_scene_create(ground->htrdr->s3d, &scn);
if(res != RES_OK) {
- htrdr_log_err(htrdr,
+ htrdr_log_err(ground->htrdr,
"%s: could not create the Star-3D scene of the ground -- %s.\n",
FUNC_NAME, res_to_cstr(res));
goto error;
@@ -196,14 +168,14 @@ htrdr_ground_create
S3DAW(get_shape(s3daw, ishape, &shape));
res = s3d_mesh_set_hit_filter_function(shape, ground_filter, NULL);
if(res != RES_OK) {
- htrdr_log_err(htrdr,
+ htrdr_log_err(ground->htrdr,
"%s: could not setup the hit filter function of the ground geometry "
"-- %s.\n", FUNC_NAME, res_to_cstr(res));
goto error;
}
res = s3d_scene_attach_shape(scn, shape);
if(res != RES_OK) {
- htrdr_log_err(htrdr,
+ htrdr_log_err(ground->htrdr,
"%s: could not attach the ground geometry to its Star-3D scene -- %s.\n",
FUNC_NAME, res_to_cstr(res));
goto error;
@@ -212,7 +184,7 @@ htrdr_ground_create
res = s3d_scene_view_create(scn, S3D_TRACE, &ground->view);
if(res != RES_OK) {
- htrdr_log_err(htrdr,
+ htrdr_log_err(ground->htrdr,
"%s: could not create the Star-3D scene view of the ground geometry "
"-- %s.\n", FUNC_NAME, res_to_cstr(res));
goto error;
@@ -220,7 +192,7 @@ htrdr_ground_create
res = s3d_scene_view_get_aabb(ground->view, ground->lower, ground->upper);
if(res != RES_OK) {
- htrdr_log_err(htrdr,
+ htrdr_log_err(ground->htrdr,
"%s: could not get the ground bounding box -- %s.\n",
FUNC_NAME, res_to_cstr(res));
goto error;
@@ -229,6 +201,57 @@ htrdr_ground_create
exit:
if(s3daw) S3DAW(ref_put(s3daw));
if(scn) S3D(scene_ref_put(scn));
+ return res;
+error:
+ goto exit;
+}
+
+static void
+release_ground(ref_T* ref)
+{
+ struct htrdr_ground* ground;
+ ASSERT(ref);
+ ground = CONTAINER_OF(ref, struct htrdr_ground, ref);
+ if(ground->view) S3D(scene_view_ref_put(ground->view));
+ MEM_RM(ground->htrdr->allocator, ground);
+}
+
+/*******************************************************************************
+ * Local functions
+ ******************************************************************************/
+res_T
+htrdr_ground_create
+ (struct htrdr* htrdr,
+ const char* obj_filename,
+ const int repeat_ground, /* Infinitely repeat the ground in X and Y */
+ struct htrdr_ground** out_ground)
+{
+ char buf[128];
+ struct htrdr_ground* ground = NULL;
+ struct time t0, t1;
+ res_T res = RES_OK;
+ ASSERT(htrdr && obj_filename && out_ground);
+
+ ground = MEM_CALLOC(htrdr->allocator, 1, sizeof(*ground));
+ if(!ground) {
+ res = RES_MEM_ERR;
+ htrdr_log_err(htrdr,
+ "%s: could not allocate the ground data structure -- %s.\n",
+ FUNC_NAME, res_to_cstr(res));
+ goto error;
+ }
+ ref_init(&ground->ref);
+ ground->htrdr = htrdr;
+ ground->repeat = repeat_ground;
+
+ time_current(&t0);
+ res = setup_ground(ground, obj_filename);
+ if(res != RES_OK) goto error;
+ time_sub(&t0, time_current(&t1), &t0);
+ time_dump(&t0, TIME_ALL, NULL, buf, sizeof(buf));
+ htrdr_log(ground->htrdr, "Setup ground in %s\n", buf);
+
+exit:
*out_ground = ground;
return res;
error: