Improve packing

cpp
Elnu 3 years ago
parent 874d9b3819
commit 9a9f74cb52

@ -1,14 +1,31 @@
import os import os
RES_DIR = "../res/" RES_DIR = "../res/"
RES_FILE = "../include/res.hpp" PACKED_DIR = "../include/packed/"
resources = os.fsencode(RES_DIR) 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): for resource in os.listdir(resources):
encoded = f"const unsigned char {os.path.splitext(resource)[0].decode().upper()}[] = {'{'}" res = ""
file = open(RES_DIR + resource.decode(), "rb") # read binary
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 bytes_processed = 0
for byte in file.read(): for byte in file.read():
if bytes_processed % 16 == 0: if bytes_processed % 16 == 0:
@ -17,7 +34,5 @@ for resource in os.listdir(resources):
bytes_processed += 1 bytes_processed += 1
encoded = encoded[:-2] + "\n};" encoded = encoded[:-2] + "\n};"
res += encoded + "\n" res += encoded + "\n"
with open(os.path.join(PACKED_DIR, f"{name}.hpp"), "w") as f:
res_file = open(RES_FILE, "w") f.write(res)
res_file.write(res)
res_file.close()

@ -1 +1 @@
res.hpp packed

Before

Width:  |  Height:  |  Size: 8.8 KiB

After

Width:  |  Height:  |  Size: 8.8 KiB

@ -27,7 +27,16 @@
#include <unistd.h> #include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <pwd.h> #include <pwd.h>
#include <res.hpp>
#include <packed/background_texture_data.hpp>
#include <packed/blocks_texture_data.hpp>
#include <packed/game_over_audio_data.hpp>
#include <packed/level_up_audio_data.hpp>
#include <packed/new_highscore_audio_data.hpp>
#include <packed/numerals_texture_data.hpp>
#include <packed/rotate_audio_data.hpp>
#include <packed/row_clear_audio_data.hpp>
#include <packed/snap_audio_data.hpp>
#define TILE_SIZE 20 #define TILE_SIZE 20
@ -289,18 +298,18 @@ int main()
TileType* grid[GRID_HEIGHT][GRID_WIDTH] = { nullptr }; TileType* grid[GRID_HEIGHT][GRID_WIDTH] = { nullptr };
sf::Texture texture; sf::Texture blocks_texture;
texture.loadFromMemory(&TEXTURE, sizeof(TEXTURE)); blocks_texture.loadFromMemory(&BLOCKS_TEXTURE_DATA, sizeof(BLOCKS_TEXTURE_DATA));
sf::Sprite sprite; sf::Sprite sprite;
sprite.setTexture(texture); sprite.setTexture(blocks_texture);
sf::Texture background_texture; sf::Texture background_texture;
background_texture.loadFromMemory(&BACKGROUND, sizeof(BACKGROUND)); background_texture.loadFromMemory(&BACKGROUND_TEXTURE_DATA, sizeof(BACKGROUND_TEXTURE_DATA));
sf::Sprite background; sf::Sprite background;
background.setTexture(background_texture); background.setTexture(background_texture);
sf::Texture numeral_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), { NumberRenderer number_renderer(numeral_texture, sf::IntRect(134, 0, 10, 16), {
sf::IntRect(0, 0, 14, 16), sf::IntRect(0, 0, 14, 16),
sf::IntRect(14, 0, 8, 16), sf::IntRect(14, 0, 8, 16),
@ -351,42 +360,42 @@ int main()
// https://sfxr.me/#57uBnWWZeyDTsBRrJsAp2Vwd76cMVrdeRQ7DirNQW5XekKxcrCUNx47Zggh7Uqw4R5FdeUpyk362uhjWmpNHmqxE7JBp3EkxDxfJ1VjzMRpuSHieW6B5iyVFM // https://sfxr.me/#57uBnWWZeyDTsBRrJsAp2Vwd76cMVrdeRQ7DirNQW5XekKxcrCUNx47Zggh7Uqw4R5FdeUpyk362uhjWmpNHmqxE7JBp3EkxDxfJ1VjzMRpuSHieW6B5iyVFM
sf::SoundBuffer rotate_buffer; sf::SoundBuffer rotate_buffer;
rotate_buffer.loadFromMemory(&ROTATE, sizeof(ROTATE)); rotate_buffer.loadFromMemory(&ROTATE_AUDIO_DATA, sizeof(ROTATE_AUDIO_DATA));
sf::Sound rotate_sound; sf::Sound rotate_sound;
rotate_sound.setBuffer(rotate_buffer); rotate_sound.setBuffer(rotate_buffer);
// https://sfxr.me/#57uBnWTMa2LUtaPa3P8xWZekiRxNwCPFWpRoPDVXDJM9KHkiGJcs6J62FRcjMY5oVNdT73MtmUf5rXCPvSZWL7AZuTRWWjKbPKTpZjT85AcZ6htUqTswkjksZ // https://sfxr.me/#57uBnWTMa2LUtaPa3P8xWZekiRxNwCPFWpRoPDVXDJM9KHkiGJcs6J62FRcjMY5oVNdT73MtmUf5rXCPvSZWL7AZuTRWWjKbPKTpZjT85AcZ6htUqTswkjksZ
sf::SoundBuffer snap_buffer; sf::SoundBuffer snap_buffer;
snap_buffer.loadFromMemory(&SNAP, sizeof(SNAP)); snap_buffer.loadFromMemory(&SNAP_AUDIO_DATA, sizeof(SNAP_AUDIO_DATA));
sf::Sound snap_sound; sf::Sound snap_sound;
snap_sound.setBuffer(snap_buffer); snap_sound.setBuffer(snap_buffer);
// https://sfxr.me/#57uBnWbareN7MJJsWGD8eFCrqjikS9f8JXg8jvmKzMdVtqmRsb81eToSUpnkqgFhvxD2QoAjpw4SmGZHZjbhEiPQKetRSHCHXYFZzD7Q6RVVS9CRSeRAb6bZp // https://sfxr.me/#57uBnWbareN7MJJsWGD8eFCrqjikS9f8JXg8jvmKzMdVtqmRsb81eToSUpnkqgFhvxD2QoAjpw4SmGZHZjbhEiPQKetRSHCHXYFZzD7Q6RVVS9CRSeRAb6bZp
sf::SoundBuffer game_over_buffer; 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; sf::Sound game_over_sound;
game_over_sound.setBuffer(game_over_buffer); game_over_sound.setBuffer(game_over_buffer);
// https://sfxr.me/#7BMHBGMfGk8EHV8czJkUucUm8EMAnMNxiqYyTfKkMpHFJu44GEdD7xP6E8NM3K7RKRExTpagPBAiWf7BLtC52CEWJVGHh8hwDLygoEG86tcPth2UtmfdrXLoh // https://sfxr.me/#7BMHBGMfGk8EHV8czJkUucUm8EMAnMNxiqYyTfKkMpHFJu44GEdD7xP6E8NM3K7RKRExTpagPBAiWf7BLtC52CEWJVGHh8hwDLygoEG86tcPth2UtmfdrXLoh
sf::SoundBuffer row_clear_buffer; 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; sf::Sound row_clear_sound;
row_clear_sound.setBuffer(row_clear_buffer); row_clear_sound.setBuffer(row_clear_buffer);
// https://sfxr.me/#57uBnWg8448kTPqWAxeDvZ5CP5JWbrfJGWuRcTjva5uX3vvBnEAZ6SfiH9oLKMXgsusuJwGWx6KPfvLfHtqnhLxr476ptGv4jPbfNhQaFMYeMHFdHk9SotQ4X // https://sfxr.me/#57uBnWg8448kTPqWAxeDvZ5CP5JWbrfJGWuRcTjva5uX3vvBnEAZ6SfiH9oLKMXgsusuJwGWx6KPfvLfHtqnhLxr476ptGv4jPbfNhQaFMYeMHFdHk9SotQ4X
sf::SoundBuffer level_up_buffer; 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; sf::Sound level_up_sound;
level_up_sound.setBuffer(level_up_buffer); level_up_sound.setBuffer(level_up_buffer);
// https://sfxr.me/#34T6PkzvrkfdahGDBAh1uYGXTwZ8rG54kxfHpgdVCPxqG7yyK5UuqgiK9Z8Q5177itxbkSNfLSHm4zTkemT4iyxJpW89VJx82feaq8qxZeA5AJR2nWZZR59hq // https://sfxr.me/#34T6PkzvrkfdahGDBAh1uYGXTwZ8rG54kxfHpgdVCPxqG7yyK5UuqgiK9Z8Q5177itxbkSNfLSHm4zTkemT4iyxJpW89VJx82feaq8qxZeA5AJR2nWZZR59hq
sf::SoundBuffer new_highscore_buffer; 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; sf::Sound new_highscore_sound;
new_highscore_sound.setBuffer(new_highscore_buffer); new_highscore_sound.setBuffer(new_highscore_buffer);

Loading…
Cancel
Save