Skip to content

Commit

Permalink
[client,sdl] generate binary resources during build
Browse files Browse the repository at this point in the history
  • Loading branch information
Armin Novak authored and akallabeth committed Dec 20, 2023
1 parent 2056ffe commit 187f562
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 35,807 deletions.
2 changes: 1 addition & 1 deletion client/SDL/dialogs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set(SRCS
sdl_connection_dialog.hpp
)

set(LIBS
list(APPEND LIBS
${SDL2_LIBRARIES}
sdl_client_res
winpr
Expand Down
64 changes: 53 additions & 11 deletions client/SDL/dialogs/res/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,68 @@ set(SRCS
sdl_resource_manager.hpp
)

set(RES_SVG_FILES
${CMAKE_SOURCE_DIR}/resources/FreeRDP_Icon.svg
${CMAKE_SOURCE_DIR}/resources/error_FILL0_wght400_GRAD0_opsz24.svg
${CMAKE_SOURCE_DIR}/resources/feedback_FILL0_wght400_GRAD0_opsz24.svg
${CMAKE_SOURCE_DIR}/resources/warning_FILL0_wght400_GRAD0_opsz24.svg
)

set(RES_FONT_FILES
${CMAKE_SOURCE_DIR}/client/SDL/dialogs/font/OpenSans-VariableFont_wdth,wght.ttf
)

macro(convert_to_bin FILE FILE_TYPE)
get_filename_component(FILE_NAME ${FILE} NAME)
string(REGEX REPLACE "[^a-zA-Z0-9]" "_" TARGET_NAME ${FILE_NAME})

set(FILE_BIN_DIR ${CMAKE_CURRENT_BINARY_DIR}/bin)
set(FILE_BYPRODUCTS ${FILE_BIN_DIR}/${TARGET_NAME}.hpp ${FILE_BIN_DIR}/${TARGET_NAME}.cpp)

list(APPEND SRCS
${FILE_BYPRODUCTS}
)

add_custom_command(
OUTPUT ${FILE_BYPRODUCTS}
COMMAND ${CMAKE_COMMAND} -E make_directory ${FILE_BIN_DIR}
COMMAND $<TARGET_FILE:freerdp-res2bin> ${FILE} ${FILE_TYPE} ${TARGET_NAME} ${FILE_BIN_DIR}
COMMENT "create image resources"
DEPENDS freerdp-res2bin
DEPENDS ${FILE}
)
endmacro()

option(SDL_USE_COMPILED_RESOURCES "Compile in images/fonts" ON)

if (SDL_USE_COMPILED_RESOURCES)
list(APPEND SRCS
sdl_resource_file.cpp
sdl_resource_file.hpp
)
file(GLOB_RECURSE RES_CPP_FILES
LIST_DIRECTORIES false
"bin/*.cpp"
)
file(GLOB_RECURSE RES_HDR_FILES
LIST_DIRECTORIES false
"bin/*.hpp"

include_directories(${CMAKE_CURRENT_SOURCE_DIR})
foreach(FILE ${RES_SVG_FILES})
convert_to_bin("${FILE}" "images")
endforeach()

foreach(FILE ${RES_FONT_FILES})
convert_to_bin("${FILE}" "fonts")
endforeach()
add_definitions(-DSDL_USE_COMPILED_RESOURCES)
else()
set(SDL_RESOURCE_ROOT ${CMAKE_INSTALL_FULL_DATAROOTDIR}/FreeRDP)
add_definitions(-DSDL_RESOURCE_ROOT="${SDL_RESOURCE_ROOT}")

install(
FILES ${RES_SVG_FILES}
DESTINATION ${SDL_RESOURCE_ROOT}/images
)
list(APPEND RES_FILES
${RES_CPP_FILES}
${RES_HDR_FILES}

install(
FILES ${RES_FONT_FILES}
DESTINATION ${SDL_RESOURCE_ROOT}/fonts
)
add_definitions(-DSDL_USE_COMPILED_RESOURCES)
endif()

add_library(sdl_client_res OBJECT
Expand Down
367 changes: 0 additions & 367 deletions client/SDL/dialogs/res/bin/FreeRDP_Icon_svg.cpp

This file was deleted.

25 changes: 0 additions & 25 deletions client/SDL/dialogs/res/bin/FreeRDP_Icon_svg.hpp

This file was deleted.

35,342 changes: 0 additions & 35,342 deletions client/SDL/dialogs/res/bin/OpenSans_VariableFont_wdth_wght_ttf.cpp

This file was deleted.

25 changes: 0 additions & 25 deletions client/SDL/dialogs/res/bin/OpenSans_VariableFont_wdth_wght_ttf.hpp

This file was deleted.

41 changes: 18 additions & 23 deletions client/SDL/dialogs/res/convert_res_to_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

static void usage(const char* prg)
{
std::cerr << prg << " <file> <type> <dst path>" << std::endl;
std::cerr << prg << " <file> <type> <class name> <dst path>" << std::endl;
}

static int write_comment_header(std::ostream& out, const std::filesystem::path& prg,
Expand All @@ -41,9 +41,10 @@ static int write_comment_header(std::ostream& out, const std::filesystem::path&
}

static int write_cpp_header(std::ostream& out, const std::filesystem::path& prg,
const std::string& fname, const std::string& name,
const std::filesystem::path& file, const std::string& name,
const std::string& type)
{
auto fname = file.filename().string();
auto rc = write_comment_header(out, prg, fname);
if (rc != 0)
return rc;
Expand Down Expand Up @@ -97,8 +98,12 @@ static int write_cpp_trailer(std::ostream& out)
static int write_hpp_header(const std::filesystem::path prg, const std::filesystem::path& file,
const std::string& name, const std::string& fname)
{
std::ofstream out;
out.open(file);
std::ofstream out(file, std::ios::out);
if (!out.is_open())
{
std::cerr << "Failed to open output file '" << file << "'" << std::endl;
return -1;
}
auto rc = write_comment_header(out, prg, fname);
if (rc != 0)
return rc;
Expand All @@ -107,7 +112,7 @@ static int write_hpp_header(const std::filesystem::path prg, const std::filesyst
<< std::endl
<< "#include <vector>" << std::endl
<< "#include <string>" << std::endl
<< "#include \"../sdl_resource_file.hpp\"" << std::endl
<< "#include \"sdl_resource_file.hpp\"" << std::endl
<< std::endl
<< "class " << name << " {" << std::endl
<< "public:" << std::endl
Expand All @@ -127,30 +132,20 @@ static int write_hpp_header(const std::filesystem::path prg, const std::filesyst
int main(int argc, char* argv[])
{
std::filesystem::path prg(argv[0]);
if (argc != 4)
if (argc != 5)
{
usage(argv[0]);
return -1;
}

std::filesystem::path file(argv[1]);
std::string etype = argv[2];
std::filesystem::path dst(argv[3]);

auto name = file.filename().string();

/* replace some common symbols in file name not allowed for c++ */
std::replace(name.begin(), name.end(), '.', '_');
std::replace(name.begin(), name.end(), ';', '_');
std::replace(name.begin(), name.end(), ',', '_');
std::replace(name.begin(), name.end(), ' ', '_');
std::replace(name.begin(), name.end(), '\t', '_');
std::replace(name.begin(), name.end(), '-', '_');

dst /= name + ".cpp";
std::string cname = argv[3];
std::filesystem::path dst(argv[4]);
std::filesystem::path hdr(argv[4]);

std::filesystem::path hdr(argv[3]);
hdr /= name + ".hpp";
dst /= cname + ".cpp";
hdr /= cname + ".hpp";

std::ofstream out;
out.open(dst);
Expand All @@ -167,7 +162,7 @@ int main(int argc, char* argv[])
return -3;
}

auto rc = write_cpp_header(out, prg, file.filename(), name, etype);
auto rc = write_cpp_header(out, prg, file, cname, etype);
if (rc != 0)
return -1;

Expand All @@ -178,5 +173,5 @@ int main(int argc, char* argv[])
rc = write_cpp_trailer(out);
if (rc != 0)
return rc;
return write_hpp_header(prg, hdr, name, file.filename());
return write_hpp_header(prg, hdr, cname, file.filename().string());
}
3 changes: 2 additions & 1 deletion client/SDL/dialogs/res/sdl_resource_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ SDL_RWops* SDLResourceManager::get(const std::string& type, const std::string& i
auto& v = _resources[uuid];
return SDL_RWFromConstMem(v.data(), v.size());
#else
std::filesystem::path path(type);
std::filesystem::path path(SDL_RESOURCE_ROOT);
path /= type;
path /= id;

if (!std::filesystem::exists(path))
Expand Down
29 changes: 23 additions & 6 deletions client/SDL/dialogs/sdl_connection_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ bool SDLConnectionDialog::update()
case MSG_INFO:
case MSG_WARN:
case MSG_ERROR:
createWindow();
_type_active = _type;
createWindow();
break;
case MSG_DISCARD:
resetTimer();
Expand Down Expand Up @@ -336,11 +336,28 @@ bool SDLConnectionDialog::createWindow()
return false;
}

SdlWidget icon = {
_renderer,
{ 0, vpadding, widget_width / 4, total_height - 3 * vpadding - widget_height },
SDLResourceManager::get(SDLResourceManager::typeImages(), "FreeRDP_Icon.svg")
};
std::string res_name;
switch (_type_active)
{
case MSG_INFO:
res_name = "feedback_FILL0_wght400_GRAD0_opsz24.svg";
break;
case MSG_WARN:
res_name = "warning_FILL0_wght400_GRAD0_opsz24.svg";
break;
case MSG_ERROR:
res_name = "error_FILL0_wght400_GRAD0_opsz24.svg";
break;
case MSG_DISCARD:
default:
res_name = "FreeRDP_Icon.svg";
break;
}

SdlWidget icon = { _renderer,
{ 0, vpadding, widget_width / 4,
total_height - 3 * vpadding - widget_height },
SDLResourceManager::get(SDLResourceManager::typeImages(), res_name) };
_list.emplace_back(std::move(icon));
SDL_Rect rect = { widget_width / 4, vpadding, widget_width * 3 / 4,
total_height - 3 * vpadding - widget_height };
Expand Down
6 changes: 0 additions & 6 deletions client/SDL/dialogs/sdl_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,6 @@ SdlWidget::SdlWidget(SDL_Renderer* renderer, const SDL_Rect& rect, bool input)

SdlWidget::SdlWidget(SDL_Renderer* renderer, const SDL_Rect& rect, SDL_RWops* ops) : _rect(rect)
{
if (!ops)
{
ops = SDLResourceManager::get(SDLResourceManager::typeImages(), "FreeRDP_Icon.svg");
if (!ops)
widget_log_error(-1, "SDLResourceManager::get");
}
if (ops)
{
_image = IMG_LoadTexture_RW(renderer, ops, 1);
Expand Down

0 comments on commit 187f562

Please sign in to comment.