From 9d98d3ea6a817bd18d80f221eb66673d5d8c174e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Valor=20Mart=C3=ADnez?= Date: Mon, 10 Mar 2025 22:38:23 +0100 Subject: [PATCH] Passat gif.c a gif.cpp Afegit reloadTextures a Resources --- source/{gif.c => gif.cpp} | 96 ++------------------------------------- source/gif.h | 66 +++++++++++++++++++++++++++ source/global_events.cpp | 0 source/global_events.h | 0 source/resource.cpp | 8 ++++ source/resource.h | 3 ++ source/texture.cpp | 2 +- 7 files changed, 82 insertions(+), 93 deletions(-) rename source/{gif.c => gif.cpp} (86%) create mode 100644 source/gif.h create mode 100644 source/global_events.cpp create mode 100644 source/global_events.h diff --git a/source/gif.c b/source/gif.cpp similarity index 86% rename from source/gif.c rename to source/gif.cpp index e0efb4b..4d495f9 100644 --- a/source/gif.c +++ b/source/gif.cpp @@ -1,93 +1,6 @@ -#include -#include -#include -#include - -#define EXTENSION_INTRODUCER 0x21 -#define IMAGE_DESCRIPTOR 0x2C -#define TRAILER 0x3B - -#define GRAPHIC_CONTROL 0xF9 -#define APPLICATION_EXTENSION 0xFF -#define COMMENT_EXTENSION 0xFE -#define PLAINTEXT_EXTENSION 0x01 - -#define READ(dst, size) memcpy(dst, buffer, size); buffer += size - -typedef struct -{ - unsigned short width; - unsigned short height; - unsigned char fields; - unsigned char background_color_index; - unsigned char pixel_aspect_ratio; -} -screen_descriptor_t; - -typedef struct -{ - unsigned char r; - unsigned char g; - unsigned char b; -} -rgb; - -typedef struct -{ - unsigned short image_left_position; - unsigned short image_top_position; - unsigned short image_width; - unsigned short image_height; - unsigned char fields; -} -image_descriptor_t; - -typedef struct -{ - unsigned char byte; - int prev; - int len; -} -dictionary_entry_t; - -typedef struct -{ - unsigned char extension_code; - unsigned char block_size; -} -extension_t; - -typedef struct -{ - unsigned char fields; - unsigned short delay_time; - unsigned char transparent_color_index; -} -graphic_control_extension_t; - -typedef struct -{ - unsigned char application_id[ 8 ]; - unsigned char version[ 3 ]; -} -application_extension_t; - -typedef struct -{ - unsigned short left; - unsigned short top; - unsigned short width; - unsigned short height; - unsigned char cell_width; - unsigned char cell_height; - unsigned char foreground_color; - unsigned char background_color; -} -plaintext_extension_t; - -//static unsigned short width = 0; -//static unsigned short height = 0; -//static unsigned char* uncompressed_data = NULL; +#include "gif.h" +#include // for NULL, fprintf, stderr +#include // for malloc, realloc, exit, calloc, free void uncompress( int code_length, const unsigned char *input, @@ -104,7 +17,6 @@ void uncompress( int code_length, int clear_code; // This varies depending on code_length int stop_code; // one more than clear code int match_len; - clear_code = 1 << ( code_length ); stop_code = clear_code + 1; @@ -142,7 +54,7 @@ void uncompress( int code_length, { code = 0x0; // Always read one more bit than the code length - for ( i = 0; i < ( code_length + 1 ); ++i ) + for ( i = 0; i < ( code_length + 1 ); i++ ) { // This is different than in the file read example; that // was a call to "next_bit" diff --git a/source/gif.h b/source/gif.h new file mode 100644 index 0000000..5e6641f --- /dev/null +++ b/source/gif.h @@ -0,0 +1,66 @@ +#pragma once + +#include // for uint32_t +#include // for memcpy + +#define EXTENSION_INTRODUCER 0x21 +#define IMAGE_DESCRIPTOR 0x2C +#define TRAILER 0x3B +#define GRAPHIC_CONTROL 0xF9 +#define APPLICATION_EXTENSION 0xFF +#define COMMENT_EXTENSION 0xFE +#define PLAINTEXT_EXTENSION 0x01 + +#define READ(dst, size) memcpy(dst, buffer, size); buffer += size + +typedef struct { + unsigned short width; + unsigned short height; + unsigned char fields; + unsigned char background_color_index; + unsigned char pixel_aspect_ratio; +} screen_descriptor_t; + +typedef struct { + unsigned char r, g, b; +} rgb; + +typedef struct { + unsigned short image_left_position; + unsigned short image_top_position; + unsigned short image_width; + unsigned short image_height; + unsigned char fields; +} image_descriptor_t; + +typedef struct { + unsigned char byte; + int prev; + int len; +} dictionary_entry_t; + +typedef struct { + unsigned char extension_code; + unsigned char block_size; +} extension_t; + +typedef struct { + unsigned char fields; + unsigned short delay_time; + unsigned char transparent_color_index; +} graphic_control_extension_t; + +typedef struct { + unsigned char application_id[8]; + unsigned char version[3]; +} application_extension_t; + +typedef struct { + unsigned short left, top, width, height; + unsigned char cell_width, cell_height; + unsigned char foreground_color, background_color; +} plaintext_extension_t; + +void uncompress(int code_length, const unsigned char *input, int input_length, unsigned char *out); +uint32_t* LoadPalette(unsigned char *buffer); +unsigned char* LoadGif(unsigned char *buffer, unsigned short* w, unsigned short* h); \ No newline at end of file diff --git a/source/global_events.cpp b/source/global_events.cpp new file mode 100644 index 0000000..e69de29 diff --git a/source/global_events.h b/source/global_events.h new file mode 100644 index 0000000..e69de29 diff --git a/source/resource.cpp b/source/resource.cpp index e9f4b8c..0fbf853 100644 --- a/source/resource.cpp +++ b/source/resource.cpp @@ -73,6 +73,14 @@ void Resource::reload() load(); } +// Recarga las texturas +void Resource::reloadTextures() +{ + loadTextures(); + addPalettes(); + createTextures(); +} + // Obtiene el sonido a partir de un nombre JA_Sound_t *Resource::getSound(const std::string &name) { diff --git a/source/resource.h b/source/resource.h index a17265b..8741586 100644 --- a/source/resource.h +++ b/source/resource.h @@ -170,4 +170,7 @@ public: // Recarga todos los recursos void reload(); + + // Recarga las texturas + void reloadTextures(); }; \ No newline at end of file diff --git a/source/texture.cpp b/source/texture.cpp index 20f7dd5..23dbe15 100644 --- a/source/texture.cpp +++ b/source/texture.cpp @@ -7,7 +7,7 @@ #include // Para runtime_error #include // Para char_traits, operator<<, operator+ #include // Para vector -#include "gif.c" // Para LoadGif, LoadPalette +#include "gif.h" // Para LoadGif, LoadPalette #include "stb_image.h" // Para stbi_image_free, stbi_load, STBI_rgb_a... #include "utils.h" // Para getFileName, printWithDots