sim: embedd background bmp and load from memory
Loading the file from disk introduces a slight dependency on the working directory for the simulator. Prevent that by loading the bmp from memory. Use a python script to convert the backround bmp image to a C header file. Include this header in `main.cpp` and load the bmp from memory.
This commit is contained in:
parent
1c479a2875
commit
aa34f93161
|
@ -325,6 +325,10 @@ endif()
|
||||||
|
|
||||||
target_include_directories(infinisim PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/gif-h")
|
target_include_directories(infinisim PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/gif-h")
|
||||||
|
|
||||||
|
# embedd background image for the status window to use
|
||||||
|
add_subdirectory(img)
|
||||||
|
add_dependencies(infinisim infinisim_img_background)
|
||||||
|
|
||||||
install(TARGETS infinisim DESTINATION bin)
|
install(TARGETS infinisim DESTINATION bin)
|
||||||
|
|
||||||
# helper library to manipulate littlefs raw image
|
# helper library to manipulate littlefs raw image
|
||||||
|
|
26
img/CMakeLists.txt
Normal file
26
img/CMakeLists.txt
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
message(STATUS "folder img: converting background.bmp to C file to include in binary")
|
||||||
|
|
||||||
|
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.12)
|
||||||
|
# FindPython3 module introduces with CMake 3.12
|
||||||
|
# https://cmake.org/cmake/help/latest/module/FindPython3.html
|
||||||
|
find_package(Python3 REQUIRED)
|
||||||
|
else()
|
||||||
|
set(Python3_EXECUTABLE "python")
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(bmp_file ${CMAKE_CURRENT_SOURCE_DIR}/sim_background.bmp)
|
||||||
|
set(out_file ${CMAKE_CURRENT_BINARY_DIR}/sim_background.h)
|
||||||
|
# call python script to convert image to c file
|
||||||
|
add_custom_command(
|
||||||
|
OUTPUT ${out_file}
|
||||||
|
COMMAND "${Python3_EXECUTABLE}" ${CMAKE_CURRENT_SOURCE_DIR}/convert_bmp_to_header.py
|
||||||
|
${bmp_file}
|
||||||
|
--var-name "SIM_BACKGROUND"
|
||||||
|
--output ${out_file}
|
||||||
|
DEPENDS ${bmp_file}
|
||||||
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
|
)
|
||||||
|
# add target that can be added as dependency to infinisim target to trigger creation of C-file
|
||||||
|
add_custom_target(infinisim_img_background
|
||||||
|
DEPENDS ${out_file}
|
||||||
|
)
|
21
img/convert_bmp_to_header.py
Executable file
21
img/convert_bmp_to_header.py
Executable file
|
@ -0,0 +1,21 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
|
parser.add_argument("bmp",
|
||||||
|
help="Path to bmp to convert to C header file")
|
||||||
|
parser.add_argument("--var-name",
|
||||||
|
help="name of the variable to make the bmp data available",
|
||||||
|
default="IMAGE_DATA")
|
||||||
|
parser.add_argument("-o", "--output",
|
||||||
|
help="Path where to create C header file",
|
||||||
|
required=True)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
with open(args.output, "w", encoding="utf-8") as f:
|
||||||
|
# conversion script based on:
|
||||||
|
# https://stackoverflow.com/questions/18422123/sdl-embed-image-inside-program-executable
|
||||||
|
f.write("static const unsigned char {:s}[] = {{{:s}}};".format(
|
||||||
|
args.var_name,
|
||||||
|
",".join(str(b) for b in open(args.bmp, "rb").read())))
|
8
main.cpp
8
main.cpp
|
@ -69,6 +69,8 @@
|
||||||
#endif
|
#endif
|
||||||
#include <gif.h>
|
#include <gif.h>
|
||||||
|
|
||||||
|
#include "img/sim_background.h" // provides variable SIM_BACKGROUND
|
||||||
|
|
||||||
/*********************
|
/*********************
|
||||||
* DEFINES
|
* DEFINES
|
||||||
*********************/
|
*********************/
|
||||||
|
@ -426,8 +428,10 @@ public:
|
||||||
init_NRF_WDT();
|
init_NRF_WDT();
|
||||||
init_NRF_POWER();
|
init_NRF_POWER();
|
||||||
|
|
||||||
// Attempt to load background PNG for the status display window
|
// Attempt to load background BMP from memory for the status display window
|
||||||
SDL_Surface* simDisplayBgRaw = SDL_LoadBMP("img/sim_background.bmp");
|
const size_t SIM_BACKGROUND_size = sizeof(SIM_BACKGROUND);
|
||||||
|
SDL_RWops *rw = SDL_RWFromMem((void*)SIM_BACKGROUND, SIM_BACKGROUND_size);
|
||||||
|
SDL_Surface* simDisplayBgRaw = SDL_LoadBMP_RW(rw, 1);
|
||||||
if (simDisplayBgRaw == NULL) {
|
if (simDisplayBgRaw == NULL) {
|
||||||
printf("Failed to load sim background image: %s\n", SDL_GetError());
|
printf("Failed to load sim background image: %s\n", SDL_GetError());
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user