diff --git a/data/main.lua b/data/main.lua index 91753ab..1cde7fe 100644 --- a/data/main.lua +++ b/data/main.lua @@ -10,6 +10,14 @@ function _init() --turbo(false) --local perico = "péricòñ" --print(utf8.len(perico)) + s = surface.load("tiles01.gif") + --surface.source(s) + p = palette.load("tiles01.gif") + palette.set(p) + print(#p) + surface.save(s, "data/copy.gif", p) + s = surface.load("copy.gif") + --draw.source(s) end function _update() @@ -18,7 +26,8 @@ function _update() end draw.cls(5) - draw.print("HOLA",0,0) + draw.blit(s, 0, 0, 64, 64, 10, 10) + --draw.print(#p,0,0,2) end function normal_update() diff --git a/data/tiles01.gif b/data/tiles01.gif new file mode 100644 index 0000000..958bbf7 Binary files /dev/null and b/data/tiles01.gif differ diff --git a/gif.c b/gif.c index 8c19b14..631ae40 100644 --- a/gif.c +++ b/gif.c @@ -2,104 +2,31 @@ #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 +#include #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; +struct rgb { uint8_t r, g, b; }; -typedef struct +struct image_descriptor_t { - unsigned char r; - unsigned char g; - unsigned char b; -} -rgb; + uint16_t left, top, width, height; + uint8_t fields; +}; -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 +struct dictionary_entry_t { unsigned char byte; - int prev; - int len; -} -dictionary_entry_t; + int prev, len; +}; -typedef struct +void uncompress( int code_length, const uint8_t *input, int input_length, uint8_t *out ) { - 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; - -void uncompress( int code_length, - const unsigned char *input, - int input_length, - unsigned char *out ) -{ - //int maxbits; int i, bit; int code, prev = -1; dictionary_entry_t *dictionary; int dictionary_ind; - unsigned int mask = 0x01; + uint32_t mask = 0x01; int reset_code_length; int clear_code; // This varies depending on code_length int stop_code; // one more than clear code @@ -107,44 +34,24 @@ void uncompress( int code_length, clear_code = 1 << ( code_length ); stop_code = clear_code + 1; - // To handle clear codes reset_code_length = code_length; - // Create a dictionary large enough to hold "code_length" entries. - // Once the dictionary overflows, code_length increases dictionary = ( dictionary_entry_t * ) malloc( sizeof( dictionary_entry_t ) * ( 1 << ( code_length + 1 ) ) ); - // Initialize the first 2^code_len entries of the dictionary with their - // indices. The rest of the entries will be built up dynamically. - - // Technically, it shouldn't be necessary to initialize the - // dictionary. The spec says that the encoder "should output a - // clear code as the first code in the image data stream". It doesn't - // say must, though... - for ( dictionary_ind = 0; - dictionary_ind < ( 1 << code_length ); - dictionary_ind++ ) + for ( dictionary_ind = 0; dictionary_ind < ( 1 << code_length ); dictionary_ind++ ) { dictionary[ dictionary_ind ].byte = dictionary_ind; - // XXX this only works because prev is a 32-bit int (> 12 bits) dictionary[ dictionary_ind ].prev = -1; dictionary[ dictionary_ind ].len = 1; } + dictionary_ind+=2; - // 2^code_len + 1 is the special "end" code; don't give it an entry here - dictionary_ind++; - dictionary_ind++; - - // TODO verify that the very last byte is clear_code + 1 while ( input_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" bit = ( *input & mask ) ? 1 : 0; mask <<= 1; @@ -161,85 +68,54 @@ void uncompress( int code_length, if ( code == clear_code ) { code_length = reset_code_length; - dictionary = ( dictionary_entry_t * ) realloc( dictionary, - sizeof( dictionary_entry_t ) * ( 1 << ( code_length + 1 ) ) ); + dictionary = ( dictionary_entry_t * ) realloc( dictionary, sizeof( dictionary_entry_t ) * ( 1 << ( code_length + 1 ) ) ); - for ( dictionary_ind = 0; - dictionary_ind < ( 1 << code_length ); - dictionary_ind++ ) + for ( dictionary_ind = 0; dictionary_ind < ( 1 << code_length ); dictionary_ind++ ) { dictionary[ dictionary_ind ].byte = dictionary_ind; - // XXX this only works because prev is a 32-bit int (> 12 bits) dictionary[ dictionary_ind ].prev = -1; dictionary[ dictionary_ind ].len = 1; } - dictionary_ind++; - dictionary_ind++; + dictionary_ind+=2; prev = -1; continue; } - else if ( code == stop_code ) - { - /*if ( input_length > 1 ) - { - fprintf( stderr, "Malformed GIF (early stop code)\n" ); - exit( 0 ); - }*/ - break; - } + else if ( code == stop_code ) break; - // Update the dictionary with this character plus the _entry_ - // (character or string) that came before it - if ( ( prev > -1 ) && ( code_length < 12 ) ) + if ( (prev > -1) && (code_length < 12) ) { - if ( code > dictionary_ind ) + if (code > dictionary_ind) { - fprintf( stderr, "code = %.02x, but dictionary_ind = %.02x\n", - code, dictionary_ind ); + fprintf( stderr, "code = %.02x, but dictionary_ind = %.02x\n", code, dictionary_ind ); exit( 0 ); } - // Special handling for KwKwK if ( code == dictionary_ind ) { int ptr = prev; - - while ( dictionary[ ptr ].prev != -1 ) - { - ptr = dictionary[ ptr ].prev; - } + while ( dictionary[ ptr ].prev != -1 ) ptr = dictionary[ ptr ].prev; dictionary[ dictionary_ind ].byte = dictionary[ ptr ].byte; } else { int ptr = code; - while ( dictionary[ ptr ].prev != -1 ) - { - ptr = dictionary[ ptr ].prev; - } + while ( dictionary[ ptr ].prev != -1 ) ptr = dictionary[ ptr ].prev; dictionary[ dictionary_ind ].byte = dictionary[ ptr ].byte; } dictionary[ dictionary_ind ].prev = prev; - dictionary[ dictionary_ind ].len = dictionary[ prev ].len + 1; - dictionary_ind++; - // GIF89a mandates that this stops at 12 bits - if ( ( dictionary_ind == ( 1 << ( code_length + 1 ) ) ) && - ( code_length < 11 ) ) + if ( (dictionary_ind == (1 << (code_length + 1))) && (code_length < 11) ) { code_length++; - - dictionary = ( dictionary_entry_t * ) realloc( dictionary, - sizeof( dictionary_entry_t ) * ( 1 << ( code_length + 1 ) ) ); + dictionary = ( dictionary_entry_t * ) realloc( dictionary, sizeof( dictionary_entry_t ) * ( 1 << ( code_length + 1 ) ) ); } } prev = code; - // Now copy the dictionary entry backwards into "out" match_len = dictionary[ code ].len; while ( code != -1 ) { @@ -251,198 +127,107 @@ void uncompress( int code_length, } code = dictionary[ code ].prev; } - out += match_len; } } -static int read_sub_blocks( unsigned char* buffer, unsigned char **data ) +static int read_sub_blocks( uint8_t* buffer, uint8_t **data ) { - int data_length; - int index; - unsigned char block_size; - - // Everything following are data sub-blocks, until a 0-sized block is - // encountered. - data_length = 0; + int data_length = 0; *data = NULL; - index = 0; + int index = 0; - while ( 1 ) - { - READ(&block_size, 1); - - if ( block_size == 0 ) // end of sub-blocks - { - break; - } + while (1) { + uint8_t block_size = *(buffer++); + if ( block_size == 0 ) break; data_length += block_size; - *data = (unsigned char*)realloc( *data, data_length ); - - // TODO this could be split across block size boundaries + *data = (uint8_t*)realloc( *data, data_length ); READ(*data + index, block_size); - index += block_size; } return data_length; } -unsigned char* process_image_descriptor( unsigned char* buffer, - rgb *gct, - int gct_size, - int resolution_bits ) +uint8_t* process_image_descriptor( uint8_t* buffer, rgb *gct, int gct_size, int resolution_bits ) { image_descriptor_t image_descriptor; - int compressed_data_length; - unsigned char *compressed_data = NULL; - unsigned char lzw_code_size; - int uncompressed_data_length = 0; - unsigned char *uncompressed_data = NULL; - - // TODO there could actually be lots of these READ(&image_descriptor, 9); - // TODO if LCT = true, read the LCT - + uint8_t lzw_code_size; READ(&lzw_code_size, 1); - compressed_data_length = read_sub_blocks( buffer, &compressed_data ); + uint8_t *compressed_data = NULL; + const int compressed_data_length = read_sub_blocks( buffer, &compressed_data ); -// width = image_descriptor.image_width; -// height = image_descriptor.image_height; - uncompressed_data_length = image_descriptor.image_width * - image_descriptor.image_height; - uncompressed_data = (unsigned char*)malloc( uncompressed_data_length ); + const int uncompressed_data_length = image_descriptor.width * image_descriptor.height; + uint8_t *uncompressed_data = (uint8_t*)malloc( uncompressed_data_length ); - uncompress( lzw_code_size, compressed_data, compressed_data_length, - uncompressed_data ); + uncompress( lzw_code_size, compressed_data, compressed_data_length, uncompressed_data ); if ( compressed_data ) free( compressed_data ); - //if ( uncompressed_data ) - // free( uncompressed_data ); - return uncompressed_data; } -/** - * @param gif_file the file descriptor of a file containing a - * GIF-encoded file. This should point to the first byte in - * the file when invoked. - */ -#define rb (*(buffer++)) +uint32_t* LoadPalette(uint8_t *buffer, uint16_t *size = NULL ) +{ + buffer += 10; + const uint8_t fields = *buffer; -uint32_t* LoadPalette(unsigned char *buffer) { - unsigned char header[7]; - screen_descriptor_t screen_descriptor; - //int color_resolution_bits; + uint32_t *global_color_table = (uint32_t *)calloc(1, 1024);; - int global_color_table_size = 0; // number of entries in global_color_table - uint32_t *global_color_table = NULL; + if (size) *size = 0; + if (fields & 0x80) { + int global_color_table_size = 1 << (((fields & 0x07) + 1)); + if (size) *size = global_color_table_size; - READ(header, 6); - READ(&screen_descriptor, 7); - - //color_resolution_bits = ((screen_descriptor.fields & 0x70) >> 4) + 1; - global_color_table = (uint32_t *)calloc(1, 1024); - - if (screen_descriptor.fields & 0x80) { - global_color_table_size = 1 << (((screen_descriptor.fields & 0x07) + 1)); - - //global_color_table = (rgb *)malloc(3 * global_color_table_size); - //READ(global_color_table, 3 * global_color_table_size); for (int i=0; i> 4 ) + 1; - // XXX there's another format, GIF87a, that you may still find - // floating around. - /*if ( strcmp( "GIF89a", (char*)header ) ) + if ( fields & 0x80 ) { - fprintf( stderr, - "Invalid GIF file (header is '%s', should be 'GIF89a')\n", - header ); - return NULL; - }*/ - - // Followed by a logical screen descriptor - // Note that this works because GIFs specify little-endian order; on a - // big-endian machine, the height & width would need to be reversed. - - // Can't use sizeof here since GCC does byte alignment; - // sizeof( screen_descriptor_t ) = 8! - READ(&screen_descriptor, 7); - *w = screen_descriptor.width; - *h = screen_descriptor.height; - - color_resolution_bits = ( ( screen_descriptor.fields & 0x70 ) >> 4 ) + 1; - - if ( screen_descriptor.fields & 0x80 ) - { - //int i; - // If bit 7 is set, the next block is a global color table; read it - global_color_table_size = 1 << - ( ( ( screen_descriptor.fields & 0x07 ) + 1 ) ); - - global_color_table = ( rgb * ) malloc( 3 * global_color_table_size ); - - // XXX this could conceivably return a short count... + global_color_table_size = 1 << ( ( ( fields & 0x07 ) + 1 ) ); + global_color_table = (rgb*)malloc( 3 * global_color_table_size ); READ(global_color_table, 3 * global_color_table_size); } - while ( block_type != TRAILER ) + uint8_t block_type = 0x0; + while ( block_type != 0x3B ) { READ(&block_type, 1); - unsigned char size; + uint8_t size; switch ( block_type ) { - case IMAGE_DESCRIPTOR: - return process_image_descriptor(buffer, - global_color_table, - global_color_table_size, - color_resolution_bits); + case 0x2C: + return process_image_descriptor(buffer, global_color_table, global_color_table_size, color_resolution_bits); + case 0x21: + buffer++; //size = *(buffer++); buffer += size; + do { size = *(buffer++); buffer += size; } while (size != 0); break; - case EXTENSION_INTRODUCER: - buffer++; - size = *(buffer++); - buffer += size; - do { - size = *(buffer++); - buffer += size; - } while (size != 0); - - /*if ( !process_extension( buffer ) ) - { - return NULL; - }*/ - break; - case TRAILER: + case 0x3B: break; default: - fprintf( stderr, "Bailing on unrecognized block type %.02x\n", - block_type ); + fprintf( stderr, "Bailing on unrecognized block type %.02x\n", block_type ); return NULL; } } @@ -450,29 +235,6 @@ static unsigned char* process_gif_stream(unsigned char *buffer, unsigned short* } -unsigned char* LoadGif(unsigned char *buffer, unsigned short* w, unsigned short* h) { +/*unsigned char* LoadGif(unsigned char *buffer, unsigned short* w, unsigned short* h) { return process_gif_stream(buffer, w, h); -} - -/*int main( int argc, char *argv[] ) -{ - FILE* gif_file; - - if ( argc < 2 ) - { - fprintf( stderr, "Usage: %s \n", argv[ 0 ] ); - exit( 0 ); - } - - gif_file = fopen( argv[ 1 ], "rb" ); - - if ( gif_file == NULL ) - { - fprintf( stderr, "Unable to open file '%s'", argv[ 1 ] ); - perror( ": " ); - } - - process_gif_stream( gif_file ); - - fclose( gif_file ); }*/ diff --git a/gifenc.h b/gifenc.h new file mode 100644 index 0000000..40a9ae6 --- /dev/null +++ b/gifenc.h @@ -0,0 +1,228 @@ +#include +#include +#include +#include + +namespace gif +{ + struct gif_t { + uint16_t w, h; + int depth; + int bgindex; + FILE *fd; + int offset; + int nframes; + uint8_t *frame, *back; + uint32_t partial; + uint8_t buffer[0xFF]; + }; + + struct node_t { + uint16_t key; + node_t *children[]; + }; + + static node_t *new_node(uint16_t key, int degree) + { + node_t *node = (node_t*)calloc(1, sizeof(*node) + degree * sizeof(node_t *)); + if (node) node->key = key; + return node; + } + + static node_t *new_trie(int degree, int *nkeys) + { + node_t *root = new_node(0, degree); + /* Create nodes for single pixels. */ + for (*nkeys = 0; *nkeys < degree; (*nkeys)++) root->children[*nkeys] = new_node(*nkeys, degree); + *nkeys += 2; /* skip clear code and stop code */ + return root; + } + + static void del_trie(node_t *root, int degree) + { + if (!root) return; + for (int i = 0; i < degree; i++) del_trie(root->children[i], degree); + free(root); + } + + static void put_loop(gif_t *gif, uint16_t loop); + + gif_t *create(const char *fname, uint16_t width, uint16_t height, uint8_t *palette, uint8_t depth, uint8_t bgindex, int loop) + { + gif_t *gif = (gif_t*)calloc(1, sizeof(*gif) + (bgindex < 0 ? 2 : 1)*width*height); + gif->w = width; gif->h = height; + gif->bgindex = bgindex; + gif->frame = (uint8_t *) &gif[1]; + gif->back = &gif->frame[width*height]; + gif->fd = fopen(fname, "wb"); + if (!gif->fd) { free(gif); return NULL; } + fwrite("GIF89a", 6, 1, gif->fd); + fwrite(&width, 2, 1, gif->fd); + fwrite(&height, 2, 1, gif->fd); + gif->depth = depth; + fputc((!palette?0x70:0xF0|(depth-1)), gif->fd); + fputc(bgindex, gif->fd); fputc(0, gif->fd); + if (palette) fwrite(palette, 3 << depth, 1, gif->fd); + if (loop >= 0 && loop <= 0xFFFF) put_loop(gif, (uint16_t)loop); + return gif; + } + + static void put_loop(gif_t *gif, uint16_t loop) + { + fputc('!', gif->fd); fputc(0xFF, gif->fd); fputc(0x0B, gif->fd); + fwrite("NETSCAPE2.0", 11, 1, gif->fd); + fputc(0x03, gif->fd); fputc(0x01, gif->fd); + fwrite(&loop, 2, 1, gif->fd); + fputc(0, gif->fd); + } + + /* Add packed key to buffer, updating offset and partial. + * gif->offset holds position to put next *bit* + * gif->partial holds bits to include in next byte */ + static void put_key(gif_t *gif, uint16_t key, int key_size) + { + int byte_offset, bit_offset, bits_to_write; + byte_offset = gif->offset / 8; + bit_offset = gif->offset % 8; + gif->partial |= ((uint32_t) key) << bit_offset; + bits_to_write = bit_offset + key_size; + while (bits_to_write >= 8) { + gif->buffer[byte_offset++] = gif->partial & 0xFF; + if (byte_offset == 0xFF) { + fputc(0xFF, gif->fd); + fwrite(gif->buffer, 0xFF, 1, gif->fd); + byte_offset = 0; + } + gif->partial >>= 8; + bits_to_write -= 8; + } + gif->offset = (gif->offset + key_size) % (0xFF * 8); + } + + static void end_key(gif_t *gif) + { + uint8_t byte_offset; + byte_offset = gif->offset >> 3; + if (gif->offset & 0x07) gif->buffer[byte_offset++] = gif->partial & 0xFF; + if (byte_offset) + { + fputc(byte_offset, gif->fd); + fwrite(gif->buffer, byte_offset, 1, gif->fd); + } + fputc(0, gif->fd); + gif->offset = gif->partial = 0; + } + + static void put_image(gif_t *gif, uint16_t w, uint16_t h, uint16_t x, uint16_t y) + { + int nkeys, key_size, i, j; + node_t *node, *child, *root; + int degree = 1 << gif->depth; + + fputc(',', gif->fd); + fwrite(&x, 2, 1, gif->fd); + fwrite(&y, 2, 1, gif->fd); + fwrite(&w, 2, 1, gif->fd); + fwrite(&h, 2, 1, gif->fd); + fputc(0, gif->fd); fputc(gif->depth, gif->fd); + root = node = new_trie(degree, &nkeys); + key_size = gif->depth + 1; + put_key(gif, degree, key_size); /* clear code */ + for (i = y; i < y+h; i++) { + for (j = x; j < x+w; j++) { + uint8_t pixel = gif->frame[i*gif->w+j] & (degree - 1); + child = node->children[pixel]; + if (child) { + node = child; + } else { + put_key(gif, node->key, key_size); + if (nkeys < 0x1000) { + if (nkeys == (1 << key_size)) + key_size++; + node->children[pixel] = new_node(nkeys++, degree); + } else { + put_key(gif, degree, key_size); /* clear code */ + del_trie(root, degree); + root = node = new_trie(degree, &nkeys); + key_size = gif->depth + 1; + } + node = root->children[pixel]; + } + } + } + put_key(gif, node->key, key_size); + put_key(gif, degree + 1, key_size); /* stop code */ + end_key(gif); + del_trie(root, degree); + } + + static int get_bbox(gif_t *gif, uint16_t *w, uint16_t *h, uint16_t *x, uint16_t *y) + { + int i, j, k; + int left, right, top, bottom; + uint8_t back; + left = gif->w; right = 0; + top = gif->h; bottom = 0; + k = 0; + for (i = 0; i < gif->h; i++) { + for (j = 0; j < gif->w; j++, k++) { + back = gif->bgindex >= 0 ? gif->bgindex : gif->back[k]; + if (gif->frame[k] != back) { + if (j < left) left = j; + if (j > right) right = j; + if (i < top) top = i; + if (i > bottom) bottom = i; + } + } + } + if (left != gif->w && top != gif->h) { + *x = left; *y = top; + *w = right - left + 1; + *h = bottom - top + 1; + return 1; + } else { + return 0; + } + } + + static void add_graphics_control_extension(gif_t *gif, uint16_t d) + { + uint8_t flags = ((gif->bgindex >= 0 ? 2 : 1) << 2) + 1; + fputc('!', gif->fd); fputc(0xF9, gif->fd); fputc(0x04, gif->fd); fputc(flags, gif->fd); + fwrite(&d, 2, 1, gif->fd); + fputc(gif->bgindex, gif->fd); fputc(0, gif->fd); + } + + void addFrame(gif_t *gif, uint16_t delay) + { + uint16_t w, h, x, y; + uint8_t *tmp; + + if (delay || (gif->bgindex >= 0)) + add_graphics_control_extension(gif, delay); + if (gif->nframes == 0) { + w = gif->w; + h = gif->h; + x = y = 0; + } else if (!get_bbox(gif, &w, &h, &x, &y)) { + /* image's not changed; save one pixel just to add delay */ + w = h = 1; + x = y = 0; + } + put_image(gif, w, h, x, y); + gif->nframes++; + if (gif->bgindex < 0) { + tmp = gif->back; + gif->back = gif->frame; + gif->frame = tmp; + } + } + + void close(gif_t* gif) + { + fputc(';', gif->fd); + fclose(gif->fd); + free(gif); + } + +} diff --git a/lua.cpp b/lua.cpp index 5a4ab74..7b11909 100644 --- a/lua.cpp +++ b/lua.cpp @@ -24,13 +24,42 @@ extern "C" { return 1; } + static int cpp_surf_save(lua_State *L) { + uint8_t surface = luaL_checkinteger(L, 1); + const char* str = luaL_checkstring(L, 2); + + int r,g,b; + uint8_t pal[256*3]; + uint8_t *p=pal; + if (lua_istable(L, -1)) { + const int len = SDL_min(256, lua_rawlen(L, -1)); + for (int i=1;i<=len;++i) { + lua_rawgeti(L, -1, i); + lua_getfield(L, -1, "r"); + *(p++) = luaL_checknumber(L, -1); + lua_pop(L, 1); + lua_getfield(L, -1, "g"); + *(p++) = luaL_checknumber(L, -1); + lua_pop(L, 1); + lua_getfield(L, -1, "b"); + *(p++) = luaL_checknumber(L, -1); + lua_pop(L, 1); + lua_pop(L, 1); + //pal[i-1] = (r<<16)+(g<<8)+b; + } + } + + savesurf(surface, str, pal); + return 0; + } + static int cpp_surf_free(lua_State *L) { uint8_t surface = luaL_checkinteger(L, 1); freesurf(surface); return 0; } - static int cpp_surf_size(lua_State *L) { + static int cpp_surf_getSize(lua_State *L) { uint8_t surface = luaL_checkinteger(L, 1); lua_pushinteger(L, surfw(surface)); lua_pushinteger(L, surfh(surface)); @@ -43,9 +72,10 @@ extern "C" { static int cpp_pal_load(lua_State *L) { const char* str = luaL_checkstring(L, 1); - uint32_t *pal = loadpal(str); + uint16_t size; + uint32_t *pal = loadpal(str, &size); lua_createtable(L, 2, 0); - for (int i=0;i<=256;++i) { + for (int i=0;i= 1) { - uint8_t index = luaL_checkinteger(L, 1); - if (lua_gettop(L) > 1) { - uint8_t r = luaL_checkinteger(L, 2); - uint8_t g = luaL_optinteger(L, 3, 0); - uint8_t b = luaL_optinteger(L, 4, 0); - uint32_t color = (r<<16) + (g<<8) + b; - setcolor(index, color); - return 0; - } else { - uint32_t color = getcolor(index); - lua_pushinteger(L, (color>>16)&0xff); - lua_pushinteger(L, (color>>8)&0xff); - lua_pushinteger(L, color&0xff); - return 3; - } - } + static int cpp_pal_getColor(lua_State *L) { + if (lua_gettop(L) != 1) return luaL_error(L, "Function 'mini.palette.getColor' Unexpected number of parameters."); + uint8_t index = luaL_checkinteger(L, 1); + uint32_t color = getcolor(index); + lua_pushinteger(L, (color>>16)&0xff); + lua_pushinteger(L, (color>>8)&0xff); + lua_pushinteger(L, color&0xff); + return 3; + } + + static int cpp_pal_setColor(lua_State *L) { + if (lua_gettop(L) != 4) return luaL_error(L, "Function 'mini.palette.setColor' Unexpected number of parameters."); + uint8_t index = luaL_checkinteger(L, 1); + uint8_t r = luaL_checkinteger(L, 2); + uint8_t g = luaL_optinteger(L, 3, 0); + uint8_t b = luaL_optinteger(L, 4, 0); + uint32_t color = (r<<16) + (g<<8) + b; + setcolor(index, color); return 0; } @@ -774,17 +804,35 @@ void push_lua_funcs() { lua_newtable(L); lua_pushcfunction(L,cpp_surf_new); lua_setfield(L, -2, "new"); lua_pushcfunction(L,cpp_surf_load); lua_setfield(L, -2, "load"); + lua_pushcfunction(L,cpp_surf_save); lua_setfield(L, -2, "save"); lua_pushcfunction(L,cpp_surf_free); lua_setfield(L, -2, "free"); - lua_pushcfunction(L,cpp_surf_size); lua_setfield(L, -2, "size"); - lua_setglobal(L, "surf"); + lua_pushcfunction(L,cpp_surf_getSize); lua_setfield(L, -2, "getSize"); + // setTarget + // cls + // setPixel + // getPixel + lua_setglobal(L, "surface"); + map.load + map.save + map.draw + map.getTile + map.setTile + lua_newtable(L); - lua_pushcfunction(L,cpp_pal_load); lua_setfield(L, -2, "load"); - lua_pushcfunction(L,cpp_pal_set); lua_setfield(L, -2, "set"); - lua_pushcfunction(L,cpp_pal_color); lua_setfield(L, -2, "color"); - lua_pushcfunction(L,cpp_pal_trans); lua_setfield(L, -2, "trans"); - lua_pushcfunction(L,cpp_pal_sub); lua_setfield(L, -2, "sub"); - lua_setglobal(L, "pal"); + lua_pushcfunction(L,cpp_pal_load); lua_setfield(L, -2, "load"); + lua_pushcfunction(L,cpp_pal_set); lua_setfield(L, -2, "set"); + lua_pushcfunction(L,cpp_pal_getColor); lua_setfield(L, -2, "getColor"); + lua_pushcfunction(L,cpp_pal_setColor); lua_setfield(L, -2, "setColor"); + lua_pushcfunction(L,cpp_pal_trans); lua_setfield(L, -2, "trans"); + lua_pushcfunction(L,cpp_pal_sub); lua_setfield(L, -2, "sub"); + lua_setglobal(L, "palette"); + + subpalette.resetAll + subpalette.set + subpalette.reset + subpalette.setRange + subpalette.resetRange lua_newtable(L); lua_pushcfunction(L,cpp_draw_dest); lua_setfield(L, -2, "dest"); @@ -999,6 +1047,7 @@ void push_lua_funcs() { lua_pushinteger(L, 20); lua_setfield(L, -2, "TOUCHPAD"); lua_setglobal(L, "pad"); + } int MiniLoader(lua_State *L) { diff --git a/mini.cpp b/mini.cpp index 729e73a..236b553 100644 --- a/mini.cpp +++ b/mini.cpp @@ -3,6 +3,7 @@ #include #include "lua.h" #include "gif.c" +#include "gifenc.h" //#include "SDL2/SDL_mixer.h" #include "jail_audio.h" @@ -216,6 +217,14 @@ uint8_t loadsurf(const char* filename) { return i; } +void savesurf(uint8_t surface, const char* filename, uint8_t *pal) +{ + gif::gif_t *file = gif::create(filename, surfaces[surface].w, surfaces[surface].h, pal, (pal?8:0), 0, -1); + memcpy(file->frame, surfaces[surface].p, surfaces[surface].w*surfaces[surface].h); + gif::addFrame(file, 0); + gif::close(file); +} + void freesurf(uint8_t surface) { if (surfaces[surface].p != NULL) free(surfaces[surface].p); surfaces[surface].p = NULL; @@ -493,10 +502,10 @@ void bcolor(uint8_t color) { ds::back_color=color; } -uint32_t *loadpal(const char* filename) { +uint32_t *loadpal(const char* filename, uint16_t *palsize) { int size; uint8_t *buffer = (uint8_t*)file_getfilebuffer(filename, size); - uint32_t *pal = LoadPalette(buffer); + uint32_t *pal = LoadPalette(buffer, palsize); free(buffer); return pal; /* diff --git a/mini.h b/mini.h index d948802..985e796 100644 --- a/mini.h +++ b/mini.h @@ -119,6 +119,7 @@ int scrh(); uint8_t newsurf(int w, int h); uint8_t loadsurf(const char* filename); +void savesurf(uint8_t surface, const char* filename, uint8_t *pal); void freesurf(uint8_t surface); int surfw(uint8_t surface); int surfh(uint8_t surface); @@ -131,7 +132,7 @@ void cls(uint8_t color=0); void color(uint8_t color=6); void bcolor(uint8_t color=0); -uint32_t *loadpal(const char* filename); +uint32_t *loadpal(const char* filename, uint16_t *palsize=NULL); void setpal(uint32_t *pal); void setcolor(uint8_t index, uint32_t color); uint32_t getcolor(uint8_t index);