From 9a9f74cb52d0a9ef9a51e572ee72332a3f4e8eb1 Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Thu, 10 Mar 2022 17:26:11 -0800 Subject: [PATCH] Improve packing --- build/packer.py | 31 ++++++++++++++++++++++-------- include/.gitignore | 2 +- res/{texture.png => blocks.png} | Bin src/Main.cpp | 33 ++++++++++++++++++++------------ 4 files changed, 45 insertions(+), 21 deletions(-) rename res/{texture.png => blocks.png} (100%) diff --git a/build/packer.py b/build/packer.py index 2515818..729b29e 100644 --- a/build/packer.py +++ b/build/packer.py @@ -1,14 +1,31 @@ import os RES_DIR = "../res/" -RES_FILE = "../include/res.hpp" +PACKED_DIR = "../include/packed/" resources = os.fsencode(RES_DIR) -res = "" +os.makedirs(PACKED_DIR, exist_ok=True) +packed_dir = os.fsencode(PACKED_DIR) + +for file in os.listdir(packed_dir): + if os.path.splitext(file)[1] == b".hpp": + os.unlink(os.path.join(packed_dir, file)) for resource in os.listdir(resources): - encoded = f"const unsigned char {os.path.splitext(resource)[0].decode().upper()}[] = {'{'}" - file = open(RES_DIR + resource.decode(), "rb") # read binary + res = "" + + splitext = os.path.splitext(resource) + name = splitext[0].decode() + ext = splitext[1].decode() + + if ext == ".png": + name += "_texture" + elif ext == ".wav": + name += "_audio" + name += "_data" + + encoded = f"const unsigned char {name.upper()}[] = {'{'}" + file = open(os.path.join(resources, resource), "rb") # read binary bytes_processed = 0 for byte in file.read(): if bytes_processed % 16 == 0: @@ -17,7 +34,5 @@ for resource in os.listdir(resources): bytes_processed += 1 encoded = encoded[:-2] + "\n};" res += encoded + "\n" - -res_file = open(RES_FILE, "w") -res_file.write(res) -res_file.close() \ No newline at end of file + with open(os.path.join(PACKED_DIR, f"{name}.hpp"), "w") as f: + f.write(res) \ No newline at end of file diff --git a/include/.gitignore b/include/.gitignore index caec75c..ea1bb8d 100644 --- a/include/.gitignore +++ b/include/.gitignore @@ -1 +1 @@ -res.hpp \ No newline at end of file +packed \ No newline at end of file diff --git a/res/texture.png b/res/blocks.png similarity index 100% rename from res/texture.png rename to res/blocks.png diff --git a/src/Main.cpp b/src/Main.cpp index 52f2b58..a753d63 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -27,7 +27,16 @@ #include #include #include -#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include #define TILE_SIZE 20 @@ -289,18 +298,18 @@ int main() TileType* grid[GRID_HEIGHT][GRID_WIDTH] = { nullptr }; - sf::Texture texture; - texture.loadFromMemory(&TEXTURE, sizeof(TEXTURE)); + sf::Texture blocks_texture; + blocks_texture.loadFromMemory(&BLOCKS_TEXTURE_DATA, sizeof(BLOCKS_TEXTURE_DATA)); sf::Sprite sprite; - sprite.setTexture(texture); + sprite.setTexture(blocks_texture); sf::Texture background_texture; - background_texture.loadFromMemory(&BACKGROUND, sizeof(BACKGROUND)); + background_texture.loadFromMemory(&BACKGROUND_TEXTURE_DATA, sizeof(BACKGROUND_TEXTURE_DATA)); sf::Sprite background; background.setTexture(background_texture); sf::Texture numeral_texture; - numeral_texture.loadFromMemory(&NUMERALS, sizeof(NUMERALS)); + numeral_texture.loadFromMemory(&NUMERALS_TEXTURE_DATA, sizeof(NUMERALS_TEXTURE_DATA)); NumberRenderer number_renderer(numeral_texture, sf::IntRect(134, 0, 10, 16), { sf::IntRect(0, 0, 14, 16), sf::IntRect(14, 0, 8, 16), @@ -351,42 +360,42 @@ int main() // https://sfxr.me/#57uBnWWZeyDTsBRrJsAp2Vwd76cMVrdeRQ7DirNQW5XekKxcrCUNx47Zggh7Uqw4R5FdeUpyk362uhjWmpNHmqxE7JBp3EkxDxfJ1VjzMRpuSHieW6B5iyVFM sf::SoundBuffer rotate_buffer; - rotate_buffer.loadFromMemory(&ROTATE, sizeof(ROTATE)); + rotate_buffer.loadFromMemory(&ROTATE_AUDIO_DATA, sizeof(ROTATE_AUDIO_DATA)); sf::Sound rotate_sound; rotate_sound.setBuffer(rotate_buffer); // https://sfxr.me/#57uBnWTMa2LUtaPa3P8xWZekiRxNwCPFWpRoPDVXDJM9KHkiGJcs6J62FRcjMY5oVNdT73MtmUf5rXCPvSZWL7AZuTRWWjKbPKTpZjT85AcZ6htUqTswkjksZ sf::SoundBuffer snap_buffer; - snap_buffer.loadFromMemory(&SNAP, sizeof(SNAP)); + snap_buffer.loadFromMemory(&SNAP_AUDIO_DATA, sizeof(SNAP_AUDIO_DATA)); sf::Sound snap_sound; snap_sound.setBuffer(snap_buffer); // https://sfxr.me/#57uBnWbareN7MJJsWGD8eFCrqjikS9f8JXg8jvmKzMdVtqmRsb81eToSUpnkqgFhvxD2QoAjpw4SmGZHZjbhEiPQKetRSHCHXYFZzD7Q6RVVS9CRSeRAb6bZp sf::SoundBuffer game_over_buffer; - game_over_buffer.loadFromMemory(&GAME_OVER, sizeof(GAME_OVER)); + game_over_buffer.loadFromMemory(&GAME_OVER_AUDIO_DATA, sizeof(GAME_OVER_AUDIO_DATA)); sf::Sound game_over_sound; game_over_sound.setBuffer(game_over_buffer); // https://sfxr.me/#7BMHBGMfGk8EHV8czJkUucUm8EMAnMNxiqYyTfKkMpHFJu44GEdD7xP6E8NM3K7RKRExTpagPBAiWf7BLtC52CEWJVGHh8hwDLygoEG86tcPth2UtmfdrXLoh sf::SoundBuffer row_clear_buffer; - row_clear_buffer.loadFromMemory(&ROW_CLEAR, sizeof(ROW_CLEAR)); + row_clear_buffer.loadFromMemory(&ROW_CLEAR_AUDIO_DATA, sizeof(ROW_CLEAR_AUDIO_DATA)); sf::Sound row_clear_sound; row_clear_sound.setBuffer(row_clear_buffer); // https://sfxr.me/#57uBnWg8448kTPqWAxeDvZ5CP5JWbrfJGWuRcTjva5uX3vvBnEAZ6SfiH9oLKMXgsusuJwGWx6KPfvLfHtqnhLxr476ptGv4jPbfNhQaFMYeMHFdHk9SotQ4X sf::SoundBuffer level_up_buffer; - level_up_buffer.loadFromMemory(&LEVEL_UP, sizeof(LEVEL_UP)); + level_up_buffer.loadFromMemory(&LEVEL_UP_AUDIO_DATA, sizeof(LEVEL_UP_AUDIO_DATA)); sf::Sound level_up_sound; level_up_sound.setBuffer(level_up_buffer); // https://sfxr.me/#34T6PkzvrkfdahGDBAh1uYGXTwZ8rG54kxfHpgdVCPxqG7yyK5UuqgiK9Z8Q5177itxbkSNfLSHm4zTkemT4iyxJpW89VJx82feaq8qxZeA5AJR2nWZZR59hq sf::SoundBuffer new_highscore_buffer; - new_highscore_buffer.loadFromMemory(&NEW_HIGHSCORE, sizeof(NEW_HIGHSCORE)); + new_highscore_buffer.loadFromMemory(&NEW_HIGHSCORE_AUDIO_DATA, sizeof(NEW_HIGHSCORE_AUDIO_DATA)); sf::Sound new_highscore_sound; new_highscore_sound.setBuffer(new_highscore_buffer);