Embedded window icon (cross platform)

This commit is contained in:
Love 2024-01-29 15:16:25 +01:00
parent 998cdceb16
commit c67345268e
6 changed files with 42961 additions and 9 deletions

View File

@ -34,8 +34,11 @@ add_executable(Pong src/main.cpp
src/VisibleObjects/Side.h
src/text/TextScreen.h
src/defaultfont.h
src/icon.h
src/icon.cpp
src/text/OptionScreen.h
src/text/Score.h)
src/text/Score.h
)
# Now link the libraries to the target
target_link_libraries(Pong ${SDL2_LIBRARIES} ${SDL2_GFX_LIBRARY} ${SDL2_TTF_LIBRARY})

61
bin-to-array.py Normal file
View File

@ -0,0 +1,61 @@
"""
Use this script to write data files as c arrays. This is used to stay cross-platform for embedding.
"""
import argparse
import os.path
def format_hex_line(data):
hex_data = ', '.join(f'0x{byte:02x}' for byte in data)
return f' {hex_data},\n'
def hexdump_to_cpp_array(file_name: str, output_dir: str, variable_name: str):
path = os.path.join(output_dir, variable_name)
header_path, source_path = f"{path}.h", f"{path}.cpp"
# Header
with open(header_path, 'w') as file:
file.write("#pragma once\n\n")
file.write("#include <cstddef>\n\n")
file.write(f"extern const unsigned char {variable_name}[];\n")
file.write(f"extern const size_t {variable_name}Length;\n")
# Source
byte_count = 0
with open(file_name, 'rb') as bin_file, open(source_path, 'w') as source_file:
source_file.write(f"// This is the bytes of {file_name}\n")
source_file.write(f'#include "{variable_name}.h"\n\n')
source_file.write(f"const unsigned char {variable_name}[] = {{\n")
while True:
chunk = bin_file.read(16)
if not chunk:
break
byte_count += len(chunk)
source_file.write(format_hex_line(chunk))
source_file.write("};\n")
source_file.write(f"const size_t {variable_name}Length = {byte_count};\n")
def main():
parser = argparse.ArgumentParser(description="Hexdump a file to a cpp array.")
parser.add_argument("file", help="Path to the binary file to be dumped")
parser.add_argument("-d", "--dir", help="Output directory", metavar="OUTPUT")
parser.add_argument("-n", "--name", help="Name of the array and header/source files", default="data")
args = parser.parse_args()
if args.dir:
try:
hexdump_to_cpp_array(args.file, args.dir, args.name)
path = os.path.join(args.dir, args.name)
print(f"Hex dump written to {path}.h & {path}.cpp")
except FileNotFoundError:
print(f"File not found: {args.file}")
except IOError as e:
print(f"Error: {e}")
else:
print("No output dir specified. Exiting.")
if __name__ == "__main__":
main()

BIN
icon.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 670 KiB

View File

@ -8,12 +8,14 @@
#include <SDL_ttf.h>
#include <iostream>
#include "SDL.h"
#include "icon.h"
class SdlWrapper {
private:
const uint8_t fps = 60;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Surface *iconSurface;
protected:
SDL_Point screenSize;
bool running = true;
@ -30,15 +32,30 @@ public:
std::cerr << "Failed to initialize TTF: " << TTF_GetError() << std::endl;
exit(-1);
}
window = SDL_CreateWindow(
title,
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
screenSize.x,
screenSize.y,
0
);
window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screenSize.x, screenSize.y,
0);
if (window == nullptr) {
std::cerr << "Failed to create SDL_Window with error: " << SDL_GetError() << std::endl;
exit(-1);
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
if (renderer == nullptr) {
std::cerr << "Failed to create SDL_Renderer with error: " << SDL_GetError() << std::endl;
exit(-1);
}
SDL_RWops *ops = SDL_RWFromConstMem(icon, iconLength);
if (ops == nullptr) {
std::cerr << "Failed to load from constant memory with error: " << SDL_GetError() << std::endl;
exit(-1);
}
iconSurface = SDL_LoadBMP_RW(ops, 1);
if (iconSurface == nullptr) {
std::cerr << "Failed to load BMP from SDL_RWops with error: " << SDL_GetError() << std::endl;
exit(-1);
}
SDL_SetWindowIcon(window, iconSurface);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
@ -48,6 +65,7 @@ public:
virtual ~SdlWrapper() {
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_FreeSurface(iconSurface);
SDL_Quit();
}

42864
src/icon.cpp Normal file

File diff suppressed because it is too large Load Diff

6
src/icon.h Normal file
View File

@ -0,0 +1,6 @@
#pragma once
#include <cstddef>
extern const unsigned char icon[];
extern const size_t iconLength;