// File: /workspace/github_c_makefile_star2_O1/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 0xea6b
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 0xeaa9
leaq 0x4d56(%rip), %rsi      # 0x137c8
movq %rbx, %rdi
callq 0x2400
movq (%rbx), %rdi
callq 0xe9b8
testb %al, %al
jne 0xec2f
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 0x443a(%rip), %rsi      # 0x12eea
movq %rbx, %rdi
callq 0x2400
movq (%rbx), %rdi
callq 0xe9b8
testb %al, %al
jne 0xec2f
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 0xeb09
movq 0x8(%rbx), %r12
movq %r15, %rdi
callq 0x2120
subq %rax, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
leaq 0x4cbd(%rip), %rsi      # 0x137cd
movq %rbx, %rdi
callq 0x2400
movq (%rbx), %rdi
callq 0xe9b8
testb %al, %al
je 0xebd3
leaq -0x50(%rbp), %rdi
leaq -0x24(%rbp), %rdx
movq %r14, %rsi
callq 0x69c6
testq %r15, %r15
je 0xeb5e
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 0x4385(%rip), %rsi      # 0x12eea
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 0x5ccc
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 0xec2f
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
jmp 0xec2f
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 0xec14
movq 0x8(%rbx), %r12
movq %r15, %rdi
callq 0x2120
subq %rax, %r12
movq %rbx, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x20b0
leaq 0x4bad(%rip), %rsi      # 0x137c8
movq %rbx, %rdi
callq 0x2400
movq (%rbx), %rdi
callq 0xe9b8
testb %al, %al
je 0xec3c
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
leaq 0x4b8f(%rip), %rdi      # 0x137d2
movq %r14, %rsi
xorl %eax, %eax
addq $0x30, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
jmp 0x2050
jmp 0xec5b
movq %rax, %rbx
leaq -0x40(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xec77
movq -0x40(%rbp), %rsi
incq %rsi
callq 0x2230
movq %rbx, %rdi
callq 0x23c0
nop