From dc801d859074bd57496af7652f4366391eeb9581 Mon Sep 17 00:00:00 2001 From: bjorn Date: Thu, 25 Jan 2018 18:51:07 -0800 Subject: [PATCH] luax_pushvertex; --- src/api/types/mesh.c | 19 ++----------------- src/luax.c | 19 +++++++++++++++++++ src/luax.h | 2 ++ 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/api/types/mesh.c b/src/api/types/mesh.c index 5b55d547..466dd8a8 100644 --- a/src/api/types/mesh.c +++ b/src/api/types/mesh.c @@ -62,24 +62,9 @@ int l_lovrMeshGetVertexCount(lua_State* L) { int l_lovrMeshGetVertex(lua_State* L) { Mesh* mesh = luax_checktype(L, 1, Mesh); int index = luaL_checkint(L, 2) - 1; - char* vertex = lovrMeshMap(mesh, index, 1, true, false); + uint8_t* vertex = lovrMeshMap(mesh, index, 1, true, false); VertexFormat format = lovrMeshGetVertexFormat(mesh); - - int total = 0; - for (int i = 0; i < format.length; i++) { - Attribute attribute = format.data[i]; - total += attribute.count; - for (int j = 0; j < attribute.count; j++) { - switch (attribute.type) { - case ATTR_FLOAT: lua_pushnumber(L, *((float*) vertex)); break; - case ATTR_BYTE: lua_pushnumber(L, *((uint8_t*) vertex)); break; - case ATTR_INT: lua_pushnumber(L, *((int*) vertex)); break; - } - vertex += AttributeSizes[attribute.type]; - } - } - - return total; + return luax_pushvertex(L, vertex, format); } int l_lovrMeshSetVertex(lua_State* L) { diff --git a/src/luax.c b/src/luax.c index 81aee94f..f8ed74c1 100644 --- a/src/luax.c +++ b/src/luax.c @@ -196,3 +196,22 @@ Color luax_checkcolor(lua_State* L, int index) { return color; } + +int luax_pushvertex(lua_State* L, uint8_t* vertex, VertexFormat format) { + int count = 0; + + for (int i = 0; i < format.length; i++) { + Attribute attribute = format.data[i]; + count += attribute.count; + for (int j = 0; j < attribute.count; j++) { + switch (attribute.type) { + case ATTR_FLOAT: lua_pushnumber(L, *((float*) vertex)); break; + case ATTR_BYTE: lua_pushnumber(L, *((uint8_t*) vertex)); break; + case ATTR_INT: lua_pushnumber(L, *((int*) vertex)); break; + } + vertex += AttributeSizes[attribute.type]; + } + } + + return count; +} diff --git a/src/luax.h b/src/luax.h index 529f1d3b..67579fef 100644 --- a/src/luax.h +++ b/src/luax.h @@ -2,6 +2,7 @@ #include #include #include "lib/map/map.h" +#include "lib/vertex.h" #include "util.h" #pragma once @@ -42,3 +43,4 @@ void luax_pushenum(lua_State* L, map_int_t* map, int value); void* luax_checkenum(lua_State* L, int index, map_int_t* map, const char* typeName); void* luax_optenum(lua_State* L, int index, const char* fallback, map_int_t* map, const char* typeName); Color luax_checkcolor(lua_State* L, int index); +int luax_pushvertex(lua_State* L, uint8_t* vertex, VertexFormat format);