Skip to content

Commit

Permalink
Merge pull request #4986 from assimp/kimkulling/fix_sample_utf8_encoding
Browse files Browse the repository at this point in the history
Fix: Use C++17 compliant utf8 encoding.
  • Loading branch information
kimkulling committed Mar 7, 2023
2 parents 6ed433d + 4f48348 commit 3a73ce3
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 309 deletions.
14 changes: 8 additions & 6 deletions include/assimp/Base64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef AI_BASE64_HPP_INC
#define AI_BASE64_HPP_INC

#include <assimp/defs.h>

#include <stdint.h>
#include <vector>
#include <string>
Expand All @@ -54,35 +56,35 @@ namespace Base64 {
/// @param in The UTF-64 buffer.
/// @param inLength The size of the buffer
/// @param out The encoded ASCII string.
void Encode(const uint8_t *in, size_t inLength, std::string &out);
ASSIMP_API void Encode(const uint8_t *in, size_t inLength, std::string &out);

/// @brief Will encode the given character buffer from UTF64 to ASCII.
/// @param in A vector, which contains the buffer for encoding.
/// @param out The encoded ASCII string.
void Encode(const std::vector<uint8_t>& in, std::string &out);
ASSIMP_API void Encode(const std::vector<uint8_t> &in, std::string &out);

/// @brief Will encode the given character buffer from UTF64 to ASCII.
/// @param in A vector, which contains the buffer for encoding.
/// @return The encoded ASCII string.
std::string Encode(const std::vector<uint8_t>& in);
ASSIMP_API std::string Encode(const std::vector<uint8_t> &in);

/// @brief Will decode the given character buffer from ASCII to UTF64.
/// @param in The ASCII buffer to decode.
/// @param inLength The size of the buffer.
/// @param out The decoded buffer.
/// @return The new buffer size.
size_t Decode(const char *in, size_t inLength, uint8_t *&out);
ASSIMP_API size_t Decode(const char *in, size_t inLength, uint8_t *&out);

/// @brief Will decode the given character buffer from ASCII to UTF64.
/// @param in The ASCII buffer to decode as a std::string.
/// @param out The decoded buffer.
/// @return The new buffer size.
size_t Decode(const std::string& in, std::vector<uint8_t>& out);
ASSIMP_API size_t Decode(const std::string &in, std::vector<uint8_t> &out);

/// @brief Will decode the given character buffer from ASCII to UTF64.
/// @param in The ASCII string.
/// @return The decoded buffer in a vector.
std::vector<uint8_t> Decode(const std::string& in);
ASSIMP_API std::vector<uint8_t> Decode(const std::string &in);

} // namespace Base64
} // namespace Assimp
Expand Down
6 changes: 6 additions & 0 deletions include/assimp/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ typedef uint32_t ai_uint32;

#ifdef __cplusplus

#ifdef ASSIMP_USE_HUNTER
# include <utf8.h>
#else
# include "../contrib/utf8cpp/source/utf8.h"
#endif

#include <cstring>
#include <new> // for std::nothrow_t
#include <string> // for aiString::Set(const std::string&)
Expand Down
52 changes: 0 additions & 52 deletions samples/SharedCode/UTFConverter.cpp

This file was deleted.

92 changes: 0 additions & 92 deletions samples/SharedCode/UTFConverter.h

This file was deleted.

2 changes: 0 additions & 2 deletions samples/SimpleTexturedDirectx11/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ ADD_EXECUTABLE( assimp_simpletextureddirectx11 WIN32
#SimpleTexturedDirectx11/VertexShader.hlsl
SimpleTexturedDirectx11/main.cpp
SimpleTexturedDirectx11/SafeRelease.hpp
${SAMPLES_SHARED_CODE_DIR}/UTFConverter.cpp
${SAMPLES_SHARED_CODE_DIR}/UTFConverter.h
)

TARGET_USE_COMMON_OUTPUT_DIRECTORY(assimp_simpletextureddirectx11)
Expand Down
25 changes: 16 additions & 9 deletions samples/SimpleTexturedDirectx11/SimpleTexturedDirectx11/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
// Written by IAS. :)
// ---------------------------------------------------------------------------

#include <assimp/types.h>

#include <Windows.h>
#include <shellapi.h>
#include <stdexcept>
Expand All @@ -21,8 +23,8 @@
#include <dxgi1_2.h>
#include <DirectXMath.h>
#include <d3dcompiler.h>

#include "ModelLoader.h"
#include "UTFConverter.h"
#include "SafeRelease.hpp"

#ifdef _MSC_VER
Expand All @@ -33,7 +35,6 @@
#endif // _MSC_VER

using namespace DirectX;
using namespace AssimpSamples::SharedCode;

#define VERTEX_SHADER_FILE L"VertexShader.hlsl"
#define PIXEL_SHADER_FILE L"PixelShader.hlsl"
Expand All @@ -50,10 +51,10 @@ struct ConstantBuffer {
// ------------------------------------------------------------
// Window Variables
// ------------------------------------------------------------
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
static constexpr uint32_t SCREEN_WIDTH = 800;
static constexpr uint32_t SCREEN_HEIGHT = 600;

const char g_szClassName[] = "directxWindowClass";
constexpr char g_szClassName[] = "directxWindowClass";

static std::string g_ModelPath;

Expand Down Expand Up @@ -154,8 +155,14 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/,
}

// Retrieve the model file path.
g_ModelPath = UTFConverter(argv[1]).str();

std::wstring filename(argv[1]);

char *targetStart = new char[filename.size()+1];
memset(targetStart, '\0', filename.size() + 1);

utf8::utf16to8(filename.c_str(), filename.c_str() + filename.size(), targetStart);
g_ModelPath = targetStart;
delete[] targetStart;
free_command_line_allocated_memory();

WNDCLASSEX wc;
Expand Down Expand Up @@ -511,9 +518,9 @@ void InitPipeline()
{
ID3DBlob *VS, *PS;
if(FAILED(CompileShaderFromFile(SHADER_PATH VERTEX_SHADER_FILE, 0, "main", "vs_4_0", &VS)))
Throwanerror(UTFConverter(L"Failed to compile shader from file " VERTEX_SHADER_FILE).c_str());
Throwanerror("Failed to compile shader from file");
if(FAILED(CompileShaderFromFile(SHADER_PATH PIXEL_SHADER_FILE, 0, "main", "ps_4_0", &PS)))
Throwanerror(UTFConverter(L"Failed to compile shader from file " PIXEL_SHADER_FILE).c_str());
Throwanerror("Failed to compile shader from file ");

dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), nullptr, &pVS);
dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), nullptr, &pPS);
Expand Down
2 changes: 0 additions & 2 deletions samples/SimpleTexturedOpenGL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ LINK_DIRECTORIES(

ADD_EXECUTABLE( assimp_simpletexturedogl WIN32
SimpleTexturedOpenGL/src/model_loading.cpp
${SAMPLES_SHARED_CODE_DIR}/UTFConverter.cpp
${SAMPLES_SHARED_CODE_DIR}/UTFConverter.h
)

TARGET_USE_COMMON_OUTPUT_DIRECTORY(assimp_simpletexturedogl)
Expand Down

0 comments on commit 3a73ce3

Please sign in to comment.