loader_aw

Load OBJ/MTL file formats
git clone git://git.meso-star.fr/loader_aw.git
Log | Files | Refs | README | LICENSE

commit 2c70a8e7a998f0c036ed2abe2c67e868537cb424
Author: vaplv <vaplv@free.fr>
Date:   Wed, 25 Jun 2014 00:05:40 +0200

First commit

Diffstat:
A.gitignore | 11+++++++++++
ALICENSE | 24++++++++++++++++++++++++
Acmake/CMakeLists.txt | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/obj.c | 120+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/obj.h | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 255 insertions(+), 0 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,11 @@ +.gitignore +CMakeCache.txt +CMakeFiles +Makefile +tmp +[Bb]uild +*.sw[po] +*.[ao] +*~ +tags + diff --git a/LICENSE b/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2013-2014 Vincent Forest +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. +3. The name of the author may not be used to endorse or promote products + derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO +EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT +OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING +IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY +OF SUCH DAMAGE. diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt @@ -0,0 +1,47 @@ +cmake_minimum_required(VERSION 2.6) +project(loader_obj C) +cmake_policy(SET CMP0011 NEW) +enable_testing() + +set(OBJ_SOURCE_DIR ${PROJECT_SOURCE_DIR}/../src) + +################################################################################ +# Dependencies +################################################################################ +find_package(RCMake REQUIRED) +find_package(RSys REQUIRED) +include_directories(${RCMake_INCLUDE_DIR} ${RSys_INCLUDE_DIR}) + +include(rcmake) + +################################################################################ +# Define targets +################################################################################ +set(OBJ_FILES_SRC obj.c) +set(OBJ_FILES_INC obj.h) +rcmake_prepend_path(OBJ_FILES_SRC ${OBJ_SOURCE_DIR}) +rcmake_prepend_path(OBJ_FILES_INC ${OBJ_SOURCE_DIR}) + +add_library(loader-obj SHARED ${OBJ_FILES_SRC} ${OBJ_FILES_INC}) +target_link_libraries(loader-obj RSys) + +set(VERSION_MAJOR 0) +set(VERSION_MINOR 0) +set(VERSION_PATCH 0) +set(VERSION ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}) +set_target_properties(loader-obj PROPERTIES + DEFINE_SYMBOL OBJ_SHARED_BUILD + VERSION ${VERSION} + SOVERSION ${VERSION_MAJOR}) + +rcmake_setup_devel(LoaderObj loader-obj ${VERSION} obj/obj_version.h) + +################################################################################ +# Install directories +################################################################################ +install(TARGETS loader-obj + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + RUNTIME DESTINATION bin) +install(FILES ${OBJ_FILES_INC} DESTINATION include/obj) + diff --git a/src/obj.c b/src/obj.c @@ -0,0 +1,120 @@ +#include "obj.h" +#include <rsys/mem_allocator.h> +#include <rsys/ref_count.h> + +struct obj { + ref_T ref; + struct mem_allocator* allocator; +}; + +/******************************************************************************* + * Helper functions + ******************************************************************************/ +static void +obj_release(ref_T* ref) +{ + struct obj* obj = CONTAINER_OF(ref, struct obj, ref); + ASSERT(ref); + MEM_FREE(obj->allocator, obj); +} + +/******************************************************************************* + * Exported functions + ******************************************************************************/ +enum obj_result +obj_create + (struct mem_allocator* mem_allocator, + struct obj** obj_out) +{ + struct mem_allocator* allocator; + struct obj* obj = NULL; + enum obj_result res = OBJ_OK; + + if(!obj_out) { + res = OBJ_BAD_ARGUMENT; + goto error; + } + allocator = mem_allocator ? mem_allocator : &mem_default_allocator; + obj = MEM_CALLOC(allocator, 1, sizeof(struct obj)); + if(!obj) { + res = OBJ_MEMORY_ERROR; + goto error; + } + obj->allocator = allocator; + ref_init(&obj->ref); + +exit: + return res; +error: + if(obj) { + OBJ(ref_put(obj)); + obj = NULL; + } + goto exit; +} + +enum obj_result +obj_ref_get(struct obj* obj) +{ + if(!obj) + return OBJ_BAD_ARGUMENT; + ref_get(&obj->ref); + return OBJ_OK; +} + +enum obj_result +obj_ref_put(struct obj* obj) +{ + if(!obj) + return OBJ_BAD_ARGUMENT; + ref_put(&obj->ref, obj_release); + return OBJ_OK; +} + +enum obj_result +obj_load(struct obj* obj, const char* filename) +{ + FILE* file = NULL; + char* file_content = NULL; + long file_size = 0; + size_t len = 0; + enum obj_result res = OBJ_OK; + + if(!obj || !filename) { + res = OBJ_BAD_ARGUMENT; + goto error; + } + + file = fopen(filename, "r"); + if(!file) { + res = OBJ_IO_ERROR; + goto error; + } + + fseek(file, 0, SEEK_END); + file_size = ftell(file); + fseek(file, 0, SEEK_SET); + + file_content = MEM_ALLOC(obj->allocator, (size_t)file_size + 1); + if(!file_content) { + res = OBJ_MEMORY_ERROR; + goto error; + } + len = fread(file_content, 1, (size_t)file_size, file); + file_content[file_size] = '\0'; + ASSERT(len == (size_t)file_size); + fclose(file); + file = NULL; + + /* TODO parse file content */ + +exit: + if(file) + fclose(file); + if(file_content) + MEM_FREE(obj->allocator, file_content); + return res; +error: + goto exit; +} + diff --git a/src/obj.h b/src/obj.h @@ -0,0 +1,53 @@ +#ifndef OBJ_H +#define OBJ_H + +#include <rsys/rsys.h> + +#ifdef OBJ_SHARED_BUILD + #define OBJ_API extern EXPORT_SYM +#elif defined(OBJ_STATIC_BUILD) + #define OBJ_API extern LOCAL_SYM +#else + #define OBJ_API extern IMPORT_SYM +#endif + +#ifndef NDEBUG + #define OBJ(Func) ASSERT(obj_##Func == OBJ_OK) +#else + #define OBJ(Func) obj_##Func +#endif + +enum obj_result { + OBJ_BAD_ARGUMENT, + OBJ_IO_ERROR, + OBJ_MEMORY_ERROR, + OBJ_OK +}; + +struct obj; +struct mem_allocator; + +BEGIN_DECLS + +OBJ_API enum obj_result +obj_create + (struct mem_allocator* allocator, /* NULL <=> use default allocator */ + struct obj** obj); + +OBJ_API enum obj_result +obj_ref_get + (struct obj* obj); + +OBJ_API enum obj_result +obj_ref_put + (struct obj* obj); + +OBJ_API enum obj_result +obj_load + (struct obj* obj, + const char* filename); + +END_DECLS + +#endif /* OBJ_H */ +