From 4b46414ae153124b07f7be22ba3fad71e28706f4 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Tue, 27 Feb 2018 14:55:25 +0300 Subject: [PATCH] added palette per bank --- src/console.c | 53 +++++++++++++++++++++++---------------------------- src/map.c | 2 +- src/sprite.c | 12 ++++++------ src/studio.c | 16 +++++++++++----- src/studio.h | 1 + src/surf.c | 2 +- src/system.c | 4 ++-- src/tic.c | 35 +++++++++++++++------------------- src/tic.h | 14 +++++++------- 9 files changed, 68 insertions(+), 71 deletions(-) diff --git a/src/console.c b/src/console.c index f8a1716..0dfe6c5 100644 --- a/src/console.c +++ b/src/console.c @@ -342,7 +342,7 @@ static s32 writeGifData(const tic_mem* tic, u8* dst, const u8* src, s32 width, s if(palette) { - const tic_rgb* pal = tic->cart.palette.colors; + const tic_rgb* pal = getBankPalette()->colors; for(s32 i = 0; i < TIC_PALETTE_SIZE; i++, pal++) palette[i].r = pal->r, palette[i].g = pal->g, palette[i].b = pal->b; @@ -359,7 +359,8 @@ static void loadCart(tic_mem* tic, tic_cartridge* cart, const u8* buffer, s32 si tic->api.load(cart, buffer, size, palette); if(!palette) - memcpy(cart->palette.data, tic->config.palette.data, sizeof(tic_palette)); + for(s32 i = 0; i < TIC_BANKS; i++) + memcpy(cart->banks[i].palette.data, tic->config.banks[i].palette.data, sizeof(tic_palette)); } static bool loadRom(tic_mem* tic, const void* data, s32 size, bool palette) @@ -412,13 +413,13 @@ static bool onConsoleLoadSectionCommand(Console* console, const char* param) switch(i) { - case 0: memcpy(&tic->cart.cover, &cart->cover, sizeof cart->cover); break; - case 1: memcpy(&tic->cart.bank0.tiles, &cart->bank0.tiles, sizeof(tic_tiles)*2); break; - case 2: memcpy(&tic->cart.bank0.map, &cart->bank0.map, sizeof(tic_map)); break; - case 3: memcpy(&tic->cart.bank0.code, &cart->bank0.code, sizeof(tic_code)); break; - case 4: memcpy(&tic->cart.bank0.sfx, &cart->bank0.sfx, sizeof(tic_sfx)); break; - case 5: memcpy(&tic->cart.bank0.music, &cart->bank0.music, sizeof(tic_music)); break; - case 6: memcpy(&tic->cart.palette, &cart->palette, sizeof(tic_palette)); break; + case 0: memcpy(&tic->cart.cover, &cart->cover, sizeof cart->cover); break; + case 1: memcpy(&tic->cart.bank0.tiles, &cart->bank0.tiles, sizeof(tic_tiles)*2); break; + case 2: memcpy(&tic->cart.bank0.map, &cart->bank0.map, sizeof(tic_map)); break; + case 3: memcpy(&tic->cart.bank0.code, &cart->bank0.code, sizeof(tic_code)); break; + case 4: memcpy(&tic->cart.bank0.sfx, &cart->bank0.sfx, sizeof(tic_sfx)); break; + case 5: memcpy(&tic->cart.bank0.music, &cart->bank0.music, sizeof(tic_music)); break; + case 6: memcpy(&tic->cart.bank0.palette, &cart->bank0.palette, sizeof(tic_palette)); break; } studioRomLoaded(); @@ -719,16 +720,17 @@ static char* saveBinarySection(char* ptr, const char* comment, const char* tag, return ptr; } -typedef struct {char* tag; s32 count; s32 offset; s32 size;} BinarySection; +typedef struct {char* tag; s32 count; s32 offset; s32 size; bool flip;} BinarySection; static const BinarySection BinarySections[] = { - {"TILES", TIC_BANK_SPRITES, offsetof(tic_bank, tiles), sizeof(tic_tile)}, - {"SPRITES", TIC_BANK_SPRITES, offsetof(tic_bank, sprites), sizeof(tic_tile)}, - {"MAP", TIC_MAP_HEIGHT, offsetof(tic_bank, map), TIC_MAP_WIDTH}, - {"WAVES", ENVELOPES_COUNT, offsetof(tic_bank, sfx.waveform), sizeof(tic_waveform)}, - {"SFX", SFX_COUNT, offsetof(tic_bank, sfx.samples), sizeof(tic_sample)}, - {"PATTERNS", MUSIC_PATTERNS, offsetof(tic_bank, music.patterns), sizeof(tic_track_pattern)}, - {"TRACKS", MUSIC_TRACKS, offsetof(tic_bank, music.tracks), sizeof(tic_track)}, + {"TILES", TIC_BANK_SPRITES, offsetof(tic_bank, tiles), sizeof(tic_tile), true}, + {"SPRITES", TIC_BANK_SPRITES, offsetof(tic_bank, sprites), sizeof(tic_tile), true}, + {"MAP", TIC_MAP_HEIGHT, offsetof(tic_bank, map), TIC_MAP_WIDTH, true}, + {"WAVES", ENVELOPES_COUNT, offsetof(tic_bank, sfx.waveform), sizeof(tic_waveform), true}, + {"SFX", SFX_COUNT, offsetof(tic_bank, sfx.samples), sizeof(tic_sample), true}, + {"PATTERNS", MUSIC_PATTERNS, offsetof(tic_bank, music.patterns), sizeof(tic_track_pattern), true}, + {"TRACKS", MUSIC_TRACKS, offsetof(tic_bank, music.tracks), sizeof(tic_track), true}, + {"PALETTE", 1, offsetof(tic_bank, palette), sizeof(tic_palette), false}, }; static void makeTag(const char* tag, char* out, s32 bank) @@ -760,11 +762,10 @@ static s32 saveProject(Console* console, void* buffer, const char* comment) makeTag(section->tag, tag, b); ptr = saveBinarySection(ptr, comment, tag, section->count, - (u8*)&tic->cart.banks[b] + section->offset, section->size, true); + (u8*)&tic->cart.banks[b] + section->offset, section->size, section->flip); } } - ptr = saveBinarySection(ptr, comment, "PALETTE", 1, &tic->cart.palette, sizeof(tic_palette), false); ptr = saveBinarySection(ptr, comment, "COVER", 1, &tic->cart.cover, tic->cart.cover.size + sizeof(s32), true); return strlen(stream); @@ -885,8 +886,6 @@ static bool loadBinarySection(const char* project, const char* comment, const ch static bool loadProject(Console* console, const char* name, const char* data, s32 size, tic_cartridge* dst) { - tic_mem* tic = console->tic; - char* project = (char*)malloc(size+1); bool done = false; @@ -907,7 +906,6 @@ static bool loadProject(Console* console, const char* name, const char* data, s3 if(cart) { memset(cart, 0, sizeof(tic_cartridge)); - memcpy(&cart->palette, &tic->config.palette.data, sizeof(tic_palette)); const char* comment = projectComment(name); char tag[16]; @@ -931,14 +929,11 @@ static bool loadProject(Console* console, const char* name, const char* data, s3 { makeTag(section->tag, tag, b); - if(loadBinarySection(project, comment, tag, section->count, (u8*)&cart->banks[b] + section->offset, section->size, true)) + if(loadBinarySection(project, comment, tag, section->count, (u8*)&cart->banks[b] + section->offset, section->size, section->flip)) done = true; } } - if(loadBinarySection(project, comment, "PALETTE", 1, &cart->palette, sizeof(tic_palette), false)) - done = true; - if(loadBinarySection(project, comment, "COVER", 1, &cart->cover, -1, true)) done = true; @@ -1571,7 +1566,7 @@ static void onImportSprites(const char* name, const void* buffer, size_t size, v u8 src = image->buffer[x + y * image->width]; const gif_color* c = &image->palette[src]; tic_rgb rgb = {c->r, c->g, c->b}; - u8 color = tic_tool_find_closest_color(console->tic->cart.palette.colors, &rgb); + u8 color = tic_tool_find_closest_color(getBankPalette()->colors, &rgb); setSpritePixel(getBankTiles()->data, x, y, color); } @@ -2970,7 +2965,7 @@ static bool cmdInjectSprites(Console* console, const char* param, const char* na u8 src = image->buffer[x + y * image->width]; const gif_color* c = &image->palette[src]; tic_rgb rgb = {c->r, c->g, c->b}; - u8 color = tic_tool_find_closest_color(console->embed.file->palette.colors, &rgb); + u8 color = tic_tool_find_closest_color(getBankPalette()->colors, &rgb); setSpritePixel(console->embed.file->bank0.tiles.data, x, y, color); } @@ -3093,7 +3088,7 @@ void initConsole(Console* console, tic_mem* tic, FileSystem* fs, Config* config, if(argc > 1) { - memcpy(console->embed.file->palette.data, tic->config.palette.data, sizeof(tic_palette)); + memcpy(console->embed.file->bank0.palette.data, tic->config.bank0.palette.data, sizeof(tic_palette)); u32 argp = 1; diff --git a/src/map.c b/src/map.c index c6820ea..6fee908 100644 --- a/src/map.c +++ b/src/map.c @@ -1086,7 +1086,7 @@ static void onStudioEvent(Map* map, StudioEvent event) static void scanline(tic_mem* tic, s32 row, void* data) { if(row == 0) - memcpy(tic->ram.vram.palette.data, tic->config.palette.data, sizeof(tic_palette)); + memcpy(tic->ram.vram.palette.data, tic->config.bank0.palette.data, sizeof(tic_palette)); } static void overline(tic_mem* tic, void* data) diff --git a/src/sprite.c b/src/sprite.c index 8bf9878..856a490 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -775,8 +775,8 @@ static void drawRGBSlider(Sprite* sprite, s32 x, s32 y, u8* value) static void pasteColor(Sprite* sprite) { - fromClipboard(sprite->tic->cart.palette.data, sizeof(tic_palette), false, true); - fromClipboard(&sprite->tic->cart.palette.colors[sprite->color], sizeof(tic_rgb), false, true); + fromClipboard(getBankPalette()->data, sizeof(tic_palette), false, true); + fromClipboard(&getBankPalette()->colors[sprite->color], sizeof(tic_rgb), false, true); } static void drawRGBTools(Sprite* sprite, s32 x, s32 y) @@ -811,7 +811,7 @@ static void drawRGBTools(Sprite* sprite, s32 x, s32 y) down = true; if(checkMouseClick(&rect, tic_mouse_left)) - toClipboard(sprite->tic->cart.palette.data, sizeof(tic_palette), false); + toClipboard(getBankPalette()->data, sizeof(tic_palette), false); } if(down) @@ -875,7 +875,7 @@ static void drawRGBSliders(Sprite* sprite, s32 x, s32 y) { enum{Gap = 6, Count = sizeof(tic_rgb)}; - u8* data = &sprite->tic->cart.palette.data[sprite->color * Count]; + u8* data = &getBankPalette()->data[sprite->color * Count]; for(s32 i = 0; i < Count; i++) drawRGBSlider(sprite, x, y + Gap*i, &data[i]); @@ -887,7 +887,7 @@ static void drawRGBSlidersOvr(Sprite* sprite, s32 x, s32 y) { enum{Gap = 6, Count = sizeof(tic_rgb), Size = CANVAS_SIZE, Max = 255}; - u8* data = &sprite->tic->cart.palette.data[sprite->color * Count]; + u8* data = &getBankPalette()->data[sprite->color * Count]; for(s32 i = 0; i < Count; i++) { @@ -1628,7 +1628,7 @@ static void onStudioEvent(Sprite* sprite, StudioEvent event) static void scanline(tic_mem* tic, s32 row, void* data) { if(row == 0) - memcpy(tic->ram.vram.palette.data, tic->config.palette.data, sizeof(tic_palette)); + memcpy(tic->ram.vram.palette.data, tic->config.bank0.palette.data, sizeof(tic_palette)); } static void overline(tic_mem* tic, void* data) diff --git a/src/studio.c b/src/studio.c index 1ebee8c..8a92cf5 100644 --- a/src/studio.c +++ b/src/studio.c @@ -286,6 +286,11 @@ tic_map* getBankMap() return &impl.studio.tic->cart.banks[impl.bank.index.map].map; } +tic_palette* getBankPalette() +{ + return &impl.studio.tic->cart.banks[impl.bank.index.sprites].palette; +} + void playSystemSfx(s32 id) { const tic_sample* effect = &impl.studio.tic->config.bank0.sfx.samples.data[id]; @@ -1476,7 +1481,7 @@ static void drawDesyncLabel(u32* frame) enum{sx = TIC80_WIDTH-24, sy = 8, Cols = sizeof DesyncLabel[0]*BITS_IN_BYTE, Rows = COUNT_OF(DesyncLabel)}; - const u32* pal = tic_palette_blit(&impl.studio.tic->config.palette); + const u32* pal = tic_palette_blit(&impl.studio.tic->config.bank0.palette); const u32* color = &pal[tic_color_red]; for(s32 y = 0; y < Rows; y++) @@ -1501,7 +1506,7 @@ static void recordFrame(u32* pixels) if(impl.video.frame % TIC_FRAMERATE < TIC_FRAMERATE / 2) { - const u32* pal = tic_palette_blit(&impl.studio.tic->config.palette); + const u32* pal = tic_palette_blit(&impl.studio.tic->config.bank0.palette); drawRecordLabel(pixels, TIC80_WIDTH-24, 8, &pal[tic_color_red]); } @@ -1742,19 +1747,20 @@ static void studioTick() overline = sprite->overline; scanline = sprite->scanline; data = sprite; - memcpy(&tic->ram.vram.palette, &tic->cart.palette, sizeof(tic_palette)); + memcpy(&tic->ram.vram.palette, getBankPalette(), sizeof(tic_palette)); } break; case TIC_MAP_MODE: { Map* map = impl.editor[impl.bank.index.map].map; overline = map->overline; + scanline = map->scanline; data = map; - memcpy(&tic->ram.vram.palette, &tic->cart.palette, sizeof(tic_palette)); + memcpy(&tic->ram.vram.palette, getBankPalette(), sizeof(tic_palette)); } break; default: - memcpy(&tic->ram.vram.palette, &tic->config.palette, sizeof(tic_palette)); + memcpy(&tic->ram.vram.palette, &tic->config.bank0.palette, sizeof(tic_palette)); break; } diff --git a/src/studio.h b/src/studio.h index 3470714..aecf933 100644 --- a/src/studio.h +++ b/src/studio.h @@ -154,6 +154,7 @@ void exitFromGameMenu(); void runProject(); tic_tiles* getBankTiles(); +tic_palette* getBankPalette(); tic_map* getBankMap(); char getKeyboardText(); diff --git a/src/surf.c b/src/surf.c index 90c2d9e..1364807 100644 --- a/src/surf.c +++ b/src/surf.c @@ -507,7 +507,7 @@ static void updateMenuItemCover(Surf* surf, const u8* cover, s32 size) { const gif_color* c = &image->palette[image->buffer[i]]; tic_rgb rgb = { c->r, c->g, c->b }; - u8 color = tic_tool_find_closest_color(tic->config.palette.colors, &rgb); + u8 color = tic_tool_find_closest_color(tic->config.bank0.palette.colors, &rgb); tic_tool_poke4(item->cover->data, i, color); } } diff --git a/src/system.c b/src/system.c index 458e5ed..70978a2 100644 --- a/src/system.c +++ b/src/system.c @@ -117,7 +117,7 @@ static void setWindowIcon() u32* pixels = SDL_malloc(Size * Size * sizeof(u32)); - const u32* pal = tic_palette_blit(&platform.studio->tic->config.palette); + const u32* pal = tic_palette_blit(&platform.studio->tic->config.bank0.palette); for(s32 j = 0, index = 0; j < Size; j++) for(s32 i = 0; i < Size; i++, index++) @@ -182,7 +182,7 @@ static void initTouchGamepad() const u8* in = platform.studio->tic->ram.vram.screen.data; const u8* end = in + sizeof(platform.studio->tic->ram.vram.screen); - const u32* pal = tic_palette_blit(&platform.studio->tic->config.palette); + const u32* pal = tic_palette_blit(&platform.studio->tic->config.bank0.palette); const u32 Delta = ((TIC80_FULLWIDTH*sizeof(u32))/sizeof *out - TIC80_WIDTH); s32 col = 0; diff --git a/src/tic.c b/src/tic.c index 1601149..26a49af 100644 --- a/src/tic.c +++ b/src/tic.c @@ -150,7 +150,7 @@ static void runNoise(blip_buffer_t* blip, tic_sound_register* reg, tic_sound_reg static void resetPalette(tic_mem* memory) { static const u8 DefaultMapping[] = {16, 50, 84, 118, 152, 186, 220, 254}; - memcpy(memory->ram.vram.palette.data, memory->cart.palette.data, sizeof(tic_palette)); + memcpy(memory->ram.vram.palette.data, memory->cart.bank0.palette.data, sizeof(tic_palette)); memcpy(memory->ram.vram.mapping, DefaultMapping, sizeof DefaultMapping); } @@ -1449,13 +1449,13 @@ static void initCover(tic_mem* tic) { const gif_color* c = &image->palette[image->buffer[i]]; tic_rgb rgb = { c->r, c->g, c->b }; - u8 color = tic_tool_find_closest_color(tic->cart.palette.colors, &rgb); + u8 color = tic_tool_find_closest_color(tic->cart.bank0.palette.colors, &rgb); tic_tool_poke4(tic->ram.vram.screen.data, i, color); } } gif_close(image); - } + } } } @@ -1465,14 +1465,15 @@ static void api_sync(tic_mem* tic, u32 mask, s32 bank, bool toCart) static const struct {s32 bank; s32 ram; s32 size;} Sections[] = { - {offsetof(tic_bank, tiles), offsetof(tic_ram, tiles), sizeof(tic_tiles) }, - {offsetof(tic_bank, sprites), offsetof(tic_ram, sprites), sizeof(tic_tiles) }, - {offsetof(tic_bank, map), offsetof(tic_ram, map), sizeof(tic_map) }, - {offsetof(tic_bank, sfx), offsetof(tic_ram, sfx), sizeof(tic_sfx) }, - {offsetof(tic_bank, music), offsetof(tic_ram, music), sizeof(tic_music) }, + {offsetof(tic_bank, tiles), offsetof(tic_ram, tiles), sizeof(tic_tiles) }, + {offsetof(tic_bank, sprites), offsetof(tic_ram, sprites), sizeof(tic_tiles) }, + {offsetof(tic_bank, map), offsetof(tic_ram, map), sizeof(tic_map) }, + {offsetof(tic_bank, sfx), offsetof(tic_ram, sfx), sizeof(tic_sfx) }, + {offsetof(tic_bank, music), offsetof(tic_ram, music), sizeof(tic_music) }, + {offsetof(tic_bank, palette), offsetof(tic_ram, vram.palette), sizeof(tic_palette) }, }; - enum{Count = COUNT_OF(Sections), Mask = (1 << (Count+1)) - 1, PaletteMask = 1 << Count}; + enum{Count = COUNT_OF(Sections), Mask = (1 << Count) - 1}; if(mask == 0) mask = Mask; @@ -1488,11 +1489,6 @@ static void api_sync(tic_mem* tic, u32 mask, s32 bank, bool toCart) : memcpy((u8*)&tic->ram + Sections[i].ram, (u8*)&tic->cart.banks[bank] + Sections[i].bank, Sections[i].size); } - if(mask & PaletteMask) - toCart - ? memcpy(&tic->cart.palette, &tic->ram.vram.palette, sizeof(tic_palette)) - : memcpy(&tic->ram.vram.palette, &tic->cart.palette, sizeof(tic_palette)); - machine->state.synced |= mask; } @@ -1778,7 +1774,9 @@ static void api_load(tic_cartridge* cart, const u8* buffer, s32 size, bool palet if(palette) { static const u8 DB16[] = {0x14, 0x0c, 0x1c, 0x44, 0x24, 0x34, 0x30, 0x34, 0x6d, 0x4e, 0x4a, 0x4e, 0x85, 0x4c, 0x30, 0x34, 0x65, 0x24, 0xd0, 0x46, 0x48, 0x75, 0x71, 0x61, 0x59, 0x7d, 0xce, 0xd2, 0x7d, 0x2c, 0x85, 0x95, 0xa1, 0x6d, 0xaa, 0x2c, 0xd2, 0xaa, 0x99, 0x6d, 0xc2, 0xca, 0xda, 0xd4, 0x5e, 0xde, 0xee, 0xd6}; - memcpy(cart->palette.data, DB16, sizeof(tic_palette)); + + for(s32 i = 0; i < TIC_BANKS; i++) + memcpy(cart->banks[i].palette.data, DB16, sizeof(tic_palette)); } #define LOAD_CHUNK(to) memcpy(&to, buffer, min(sizeof(to), chunk.size)) @@ -1799,10 +1797,7 @@ static void api_load(tic_cartridge* cart, const u8* buffer, s32 size, bool palet case CHUNK_WAVEFORM: LOAD_CHUNK(cart->banks[chunk.bank].sfx.waveform); break; case CHUNK_MUSIC: LOAD_CHUNK(cart->banks[chunk.bank].music.tracks); break; case CHUNK_PATTERNS: LOAD_CHUNK(cart->banks[chunk.bank].music.patterns); break; - case CHUNK_PALETTE: - if(palette) - LOAD_CHUNK(cart->palette); - break; + case CHUNK_PALETTE: LOAD_CHUNK(cart->banks[chunk.bank].palette); break; case CHUNK_COVER: LOAD_CHUNK(cart->cover.data); cart->cover.size = chunk.size; @@ -1870,9 +1865,9 @@ static s32 api_save(const tic_cartridge* cart, u8* buffer) buffer = SAVE_CHUNK(CHUNK_WAVEFORM, cart->banks[i].sfx.waveform, i); buffer = SAVE_CHUNK(CHUNK_PATTERNS, cart->banks[i].music.patterns, i); buffer = SAVE_CHUNK(CHUNK_MUSIC, cart->banks[i].music.tracks, i); + buffer = SAVE_CHUNK(CHUNK_PALETTE, cart->banks[i].palette, i); } - buffer = SAVE_CHUNK(CHUNK_PALETTE, cart->palette, 0); buffer = saveFixedChunk(buffer, CHUNK_COVER, cart->cover.data, cart->cover.size, 0); #undef SAVE_CHUNK diff --git a/src/tic.h b/src/tic.h index 7e779fa..f2ce43d 100644 --- a/src/tic.h +++ b/src/tic.h @@ -328,12 +328,13 @@ typedef struct typedef struct { - tic_tiles tiles; - tic_tiles sprites; - tic_map map; - tic_sfx sfx; - tic_music music; - tic_code code; + tic_tiles tiles; + tic_tiles sprites; + tic_map map; + tic_sfx sfx; + tic_music music; + tic_code code; + tic_palette palette; } tic_bank; typedef struct @@ -355,7 +356,6 @@ typedef struct tic_bank banks[TIC_BANKS]; }; - tic_palette palette; tic_cover_image cover; } tic_cartridge;