CLI scripting
							parent
							
								
									6b6c8625a0
								
							
						
					
					
						commit
						b919917b21
					
				| 
						 | 
				
			
			@ -38,6 +38,10 @@ if(UNIX)
 | 
			
		|||
	set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)
 | 
			
		||||
endif()
 | 
			
		||||
 | 
			
		||||
if (WIN32)
 | 
			
		||||
	add_definitions(-D_CRT_SECURE_NO_WARNINGS)
 | 
			
		||||
endif()
 | 
			
		||||
 | 
			
		||||
file(GLOB SOURCE_FILES
 | 
			
		||||
	#Public header
 | 
			
		||||
	include/cram.h
 | 
			
		||||
| 
						 | 
				
			
			@ -54,8 +58,8 @@ endif()
 | 
			
		|||
 | 
			
		||||
if(BUILD_CLI)
 | 
			
		||||
	add_executable(cramcli
 | 
			
		||||
		tools/cli/dirent.h
 | 
			
		||||
		tools/cli/stb_image_write.h
 | 
			
		||||
		tools/cli/lib/dirent.h
 | 
			
		||||
		tools/cli/lib/stb_image_write.h
 | 
			
		||||
		tools/cli/main.c
 | 
			
		||||
	)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -82,6 +86,7 @@ target_include_directories(Cram PUBLIC
 | 
			
		|||
	$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib>
 | 
			
		||||
	$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
 | 
			
		||||
	$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
 | 
			
		||||
	$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/tools/cli/lib>
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Soname
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -37,6 +37,28 @@
 | 
			
		|||
 | 
			
		||||
#include <stdint.h>
 | 
			
		||||
 | 
			
		||||
#ifdef _MSC_VER
 | 
			
		||||
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <search.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#endif /* _MSC_VER */
 | 
			
		||||
 | 
			
		||||
/* TODO: ifndefs here? */
 | 
			
		||||
#define Cram_assert assert
 | 
			
		||||
#define Cram_qsort qsort
 | 
			
		||||
#define Cram_malloc malloc
 | 
			
		||||
#define Cram_realloc realloc
 | 
			
		||||
#define Cram_free free
 | 
			
		||||
#define Cram_memcpy memcpy
 | 
			
		||||
#define Cram_memset memset
 | 
			
		||||
#define Cram_strdup strdup
 | 
			
		||||
#define Cram_abs abs
 | 
			
		||||
#define Cram_min min
 | 
			
		||||
#define Cram_max max
 | 
			
		||||
 | 
			
		||||
#ifdef __cplusplus
 | 
			
		||||
extern "C"
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			@ -62,7 +84,7 @@ typedef struct Cram_ContextCreateInfo
 | 
			
		|||
{
 | 
			
		||||
	const char *name;
 | 
			
		||||
	uint32_t maxDimension;
 | 
			
		||||
	uint32_t padding;
 | 
			
		||||
	int32_t padding;
 | 
			
		||||
	uint8_t trim;
 | 
			
		||||
} Cram_ContextCreateInfo;
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										22
									
								
								src/cram.c
								
								
								
								
							
							
						
						
									
										22
									
								
								src/cram.c
								
								
								
								
							| 
						 | 
				
			
			@ -26,28 +26,6 @@
 | 
			
		|||
 | 
			
		||||
#include "cram.h"
 | 
			
		||||
 | 
			
		||||
#ifdef _MSC_VER
 | 
			
		||||
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <search.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#endif /* _MSC_VER */
 | 
			
		||||
 | 
			
		||||
/* TODO: ifndefs here */
 | 
			
		||||
#define Cram_assert assert
 | 
			
		||||
#define Cram_qsort qsort
 | 
			
		||||
#define Cram_malloc malloc
 | 
			
		||||
#define Cram_realloc realloc
 | 
			
		||||
#define Cram_free free
 | 
			
		||||
#define Cram_memcpy memcpy
 | 
			
		||||
#define Cram_memset memset
 | 
			
		||||
#define Cram_strdup strdup
 | 
			
		||||
#define Cram_abs abs
 | 
			
		||||
#define Cram_min min
 | 
			
		||||
#define Cram_max max
 | 
			
		||||
 | 
			
		||||
#define STBI_ASSERT Cram_assert
 | 
			
		||||
#define STBI_MALLOC Cram_malloc
 | 
			
		||||
#define STBI_REALLOC Cram_realloc
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										109
									
								
								tools/cli/main.c
								
								
								
								
							
							
						
						
									
										109
									
								
								tools/cli/main.c
								
								
								
								
							| 
						 | 
				
			
			@ -76,34 +76,128 @@ static void dirwalk(char *dir)
 | 
			
		|||
 | 
			
		||||
/* TODO: command line options */
 | 
			
		||||
 | 
			
		||||
void print_help()
 | 
			
		||||
{
 | 
			
		||||
	fprintf(stdout, "Usage: cram input_dir output_dir atlas_name [--padding padding_value] [--trim] [--dimension max_dimension]");
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
uint8_t check_dir_exists(char *path)
 | 
			
		||||
{
 | 
			
		||||
	DIR *dir = opendir(path);
 | 
			
		||||
	if (dir) {
 | 
			
		||||
		closedir(dir);
 | 
			
		||||
		return 1;
 | 
			
		||||
	} else {
 | 
			
		||||
		return 0;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char *argv[])
 | 
			
		||||
{
 | 
			
		||||
	Cram_ContextCreateInfo createInfo;
 | 
			
		||||
	uint8_t *pixelData;
 | 
			
		||||
	uint32_t width;
 | 
			
		||||
	uint32_t height;
 | 
			
		||||
	char *arg;
 | 
			
		||||
	char *inputDirPath = NULL;
 | 
			
		||||
	char *outputDirPath = NULL;
 | 
			
		||||
	char *imageOutputFilename;
 | 
			
		||||
	int32_t i;
 | 
			
		||||
 | 
			
		||||
	/* Set defaults */
 | 
			
		||||
	createInfo.padding = 0;
 | 
			
		||||
	createInfo.trim = 1;
 | 
			
		||||
	createInfo.maxDimension = 8192;
 | 
			
		||||
	createInfo.name = NULL;
 | 
			
		||||
 | 
			
		||||
	if (argc < 2)
 | 
			
		||||
	{
 | 
			
		||||
		fprintf(stderr, "Must provide directory!\n");
 | 
			
		||||
		print_help();
 | 
			
		||||
		return 1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	createInfo.padding = 0;
 | 
			
		||||
	createInfo.trim = 1;
 | 
			
		||||
	createInfo.maxDimension = 2048;
 | 
			
		||||
	createInfo.name = "test";
 | 
			
		||||
	for (i = 1; i < argc; i += 1)
 | 
			
		||||
	{
 | 
			
		||||
		arg = argv[i];
 | 
			
		||||
 | 
			
		||||
		if (strcmp(arg, "--padding") == 0)
 | 
			
		||||
		{
 | 
			
		||||
			i += 1;
 | 
			
		||||
			createInfo.padding = atoi(argv[argc]);
 | 
			
		||||
			if (createInfo.padding < 0)
 | 
			
		||||
			{
 | 
			
		||||
				fprintf(stderr, "Padding must be equal to or greater than 0!");
 | 
			
		||||
				return 1;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		else if (strcmp(arg, "--trim") == 0)
 | 
			
		||||
		{
 | 
			
		||||
			createInfo.trim = 1;
 | 
			
		||||
		}
 | 
			
		||||
		else if (strcmp(arg, "--dimension") == 0)
 | 
			
		||||
		{
 | 
			
		||||
			createInfo.maxDimension = atoi(argv[argc]);
 | 
			
		||||
			if (createInfo.maxDimension < 0 || createInfo.maxDimension > 8192)
 | 
			
		||||
			{
 | 
			
		||||
				fprintf(stderr, "Padding must be between 0 and 8192!");
 | 
			
		||||
				return 1;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		else if (strcmp(arg, "--help") == 0)
 | 
			
		||||
		{
 | 
			
		||||
			print_help();
 | 
			
		||||
			return 0;
 | 
			
		||||
		}
 | 
			
		||||
		else
 | 
			
		||||
		{
 | 
			
		||||
			if (inputDirPath == NULL)
 | 
			
		||||
			{
 | 
			
		||||
				inputDirPath = arg;
 | 
			
		||||
			}
 | 
			
		||||
			else if (outputDirPath == NULL)
 | 
			
		||||
			{
 | 
			
		||||
				outputDirPath = arg;
 | 
			
		||||
			}
 | 
			
		||||
			else if (createInfo.name == NULL)
 | 
			
		||||
			{
 | 
			
		||||
				createInfo.name = arg;
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (inputDirPath == NULL || createInfo.name == NULL)
 | 
			
		||||
	{
 | 
			
		||||
		print_help();
 | 
			
		||||
		return 1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/* check that dirs exist */
 | 
			
		||||
	if (!check_dir_exists(inputDirPath))
 | 
			
		||||
	{
 | 
			
		||||
		fprintf(stderr, "Input directory not found!");
 | 
			
		||||
		return 1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (!check_dir_exists(outputDirPath))
 | 
			
		||||
	{
 | 
			
		||||
		fprintf(stderr, "Output directory not found!");
 | 
			
		||||
		return 1;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	context = Cram_Init(&createInfo);
 | 
			
		||||
 | 
			
		||||
	dirwalk(argv[1]);
 | 
			
		||||
	dirwalk(inputDirPath);
 | 
			
		||||
 | 
			
		||||
	Cram_Pack(context);
 | 
			
		||||
 | 
			
		||||
	Cram_GetPixelData(context, &pixelData, &width, &height);
 | 
			
		||||
 | 
			
		||||
	imageOutputFilename = Cram_malloc(strlen(createInfo.name) + 5);
 | 
			
		||||
	strcpy(imageOutputFilename, createInfo.name);
 | 
			
		||||
	strcat(imageOutputFilename, ".png");
 | 
			
		||||
 | 
			
		||||
	stbi_write_png(
 | 
			
		||||
		"output.png",
 | 
			
		||||
		imageOutputFilename,
 | 
			
		||||
		width,
 | 
			
		||||
		height,
 | 
			
		||||
		4,
 | 
			
		||||
| 
						 | 
				
			
			@ -111,6 +205,7 @@ int main(int argc, char *argv[])
 | 
			
		|||
		width * 4
 | 
			
		||||
	);
 | 
			
		||||
 | 
			
		||||
	Cram_free(imageOutputFilename);
 | 
			
		||||
	Cram_Destroy(context);
 | 
			
		||||
 | 
			
		||||
	return 0;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue