// File: /workspace/github_c_makefile_star2_O3/vlenhart[P]q3bsp/vformat.cpp
// Function: CheckTexture(char const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void CheckTexture(const char *baseName, String &textureFileName)
{

	const char *ext = strrchr(baseName,'.');

	// check if jpg exists
	textureFileName=baseName;
	if (!ext) {
		textureFileName+=".jpg";

		if (ExistsFile(textureFileName.c_str()))
			return;
	}


	// check if png exists
	textureFileName=baseName;
	if (ext)  // erase ext 
		textureFileName.erase(textureFileName.size()-strlen(ext),strlen(ext));

	textureFileName+=".png";

	if (ExistsFile(textureFileName.c_str()))
		return;

	// check if tga exists
	textureFileName=baseName;
	if (ext)  // erase ext 
		textureFileName.erase(textureFileName.size()-strlen(ext),strlen(ext));

	textureFileName+=".tga";

	if (ExistsFile(textureFileName.c_str())) {
		// convert tga to png
		String outFileName=baseName;
		if (ext)  // erase ext 
			outFileName.erase(outFileName.size()-strlen(ext),strlen(ext));

		outFileName+=".png";

		// bmp.TGAToPNG(textureFileName.c_str(),outFileName.c_str());
		int width, height, comp;
		unsigned char *data = stbi_load(textureFileName.c_str(), &width, &height, &comp	, 0);
		stbi_write_png(outFileName.c_str(), width, height, comp, data, width*comp);
		stbi_image_free(data);

		textureFileName = outFileName;
		return;
	
	}

	// check if jpg exists
	textureFileName=baseName;
	if (ext)  // erase ext 
		textureFileName.erase(textureFileName.size()-strlen(ext),strlen(ext));

	textureFileName+=".jpg";

	if (ExistsFile(textureFileName.c_str()))
		return;

	printf("Texture file not found:'%s'\n",baseName);

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x30, %rsp
movq %rsi, %rbx
movq %rdi, %r14
movl $0x2e, %esi
callq 0x2260
movq %rax, %r15
movq 0x8(%rbx), %r12
movq %r14, %rdi
callq 0x2120
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2360
testq %r15, %r15
je 0xe97b
movq 0x8(%rbx), %r12
movq %r14, %rdi
callq 0x2120
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2360
movq 0x8(%rbx), %r12
movq %r15, %rdi
callq 0x2120
subq %rax, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
jmp 0xe9b9
leaq 0x4e22(%rip), %rsi      # 0x137a4
movq %rbx, %rdi
callq 0x2400
movq (%rbx), %rdi
callq 0xe8c8
testb %al, %al
jne 0xeb3f
movq 0x8(%rbx), %r12
movq %r14, %rdi
callq 0x2120
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2360
leaq 0x451a(%rip), %rsi      # 0x12eda
movq %rbx, %rdi
callq 0x2400
movq (%rbx), %rdi
callq 0xe8c8
testb %al, %al
jne 0xeb3f
movq 0x8(%rbx), %r12
movq %r14, %rdi
callq 0x2120
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2360
testq %r15, %r15
je 0xea19
movq 0x8(%rbx), %r12
movq %r15, %rdi
callq 0x2120
subq %rax, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
leaq 0x4d89(%rip), %rsi      # 0x137a9
movq %rbx, %rdi
callq 0x2400
movq (%rbx), %rdi
callq 0xe8c8
testb %al, %al
je 0xeae3
leaq -0x50(%rbp), %rdi
leaq -0x24(%rbp), %rdx
movq %r14, %rsi
callq 0x69f2
testq %r15, %r15
je 0xea6e
leaq -0x50(%rbp), %r14
movq 0x8(%r14), %r12
movq %r15, %rdi
callq 0x2120
subq %rax, %r12
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
leaq 0x4465(%rip), %rsi      # 0x12eda
leaq -0x50(%rbp), %rdi
callq 0x2400
movq (%rbx), %rdi
leaq -0x24(%rbp), %rsi
leaq -0x2c(%rbp), %rdx
leaq -0x28(%rbp), %rcx
xorl %r8d, %r8d
callq 0x281f
movq %rax, %r14
movq -0x50(%rbp), %rdi
movl -0x24(%rbp), %esi
movl -0x2c(%rbp), %edx
movl -0x28(%rbp), %ecx
movl %ecx, %r9d
imull %esi, %r9d
movq %rax, %r8
callq 0x5d3c
movq %r14, %rdi
callq 0x2809
leaq -0x50(%rbp), %rsi
movq %rbx, %rdi
callq 0x2140
leaq -0x40(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xeb3f
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
jmp 0xeb3f
movq 0x8(%rbx), %r12
movq %r14, %rdi
callq 0x2120
movq %rbx, %rdi
xorl %esi, %esi
movq %r12, %rdx
movq %r14, %rcx
movq %rax, %r8
callq 0x2360
testq %r15, %r15
je 0xeb24
movq 0x8(%rbx), %r12
movq %r15, %rdi
callq 0x2120
subq %rax, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
leaq 0x4c79(%rip), %rsi      # 0x137a4
movq %rbx, %rdi
callq 0x2400
movq (%rbx), %rdi
callq 0xe8c8
testb %al, %al
je 0xeb4c
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4c5b(%rip), %rdi      # 0x137ae
movq %r14, %rsi
xorl %eax, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2050
jmp 0xeb6b
movq %rax, %rbx
leaq -0x40(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xeb87
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
movq %rbx, %rdi
callq 0x23c0
nop