diff --git a/src/code.c b/src/code.c index d65211c..96e218d 100644 --- a/src/code.c +++ b/src/code.c @@ -1838,10 +1838,10 @@ void initCode(Code* code, tic_mem* tic) *code = (Code) { .tic = tic, - .data = tic->cart.bank.code.data, + .data = getBankCode()->data, .tick = tick, .escape = escape, - .cursor = {{tic->cart.bank.code.data, NULL, 0, 0}, NULL, 0}, + .cursor = {{getBankCode()->data, NULL, 0, 0}, NULL, 0}, .rect = {0, TOOLBAR_SIZE + 1, TIC80_WIDTH, TIC80_HEIGHT - TOOLBAR_SIZE - TIC_FONT_HEIGHT - 1}, .scroll = {0, 0, {0, 0}, false}, .tickCounter = 0, diff --git a/src/map.c b/src/map.c index d9600ce..28c939e 100644 --- a/src/map.c +++ b/src/map.c @@ -314,7 +314,7 @@ static void drawTileIndex(Map* map, s32 x, s32 y) { s32 tx = 0, ty = 0; getMouseMap(map, &tx, &ty); - index = map->tic->api.map_get(map->tic, &map->tic->cart.bank.map, tx, ty); + index = map->tic->api.map_get(map->tic, getBankMap(), tx, ty); } } @@ -397,7 +397,7 @@ static void drawSheetOvr(Map* map, s32 x, s32 y) for(s32 j = 0, index = 0; j < rect.h; j += TIC_SPRITESIZE) for(s32 i = 0; i < rect.w; i += TIC_SPRITESIZE, index++) - map->tic->api.sprite(map->tic, &map->tic->cart.bank.tiles, index, x + i, y + j, NULL, 0); + map->tic->api.sprite(map->tic, getBankTiles(), index, x + i, y + j, NULL, 0); { s32 bx = map->sheet.rect.x * TIC_SPRITESIZE - 1 + x; @@ -437,7 +437,7 @@ static void setMapSprite(Map* map, s32 x, s32 y) for(s32 j = 0; j < map->sheet.rect.h; j++) for(s32 i = 0; i < map->sheet.rect.w; i++) - map->tic->api.map_set(map->tic, &map->tic->cart.bank.map, (x+i)%TIC_MAP_WIDTH, (y+j)%TIC_MAP_HEIGHT, (mx+i) + (my+j) * SHEET_COLS); + map->tic->api.map_set(map->tic, getBankMap(), (x+i)%TIC_MAP_WIDTH, (y+j)%TIC_MAP_HEIGHT, (mx+i) + (my+j) * SHEET_COLS); history_add(map->history); } @@ -470,7 +470,7 @@ static void drawTileCursor(Map* map) for(s32 j = 0, ty=my; j < map->sheet.rect.h; j++, ty+=TIC_SPRITESIZE) for(s32 i = 0, tx=mx; i < map->sheet.rect.w; i++, tx+=TIC_SPRITESIZE) - map->tic->api.sprite(map->tic, &map->tic->cart.bank.tiles, (sx+i) + (sy+j) * SHEET_COLS, tx, ty, NULL, 0); + map->tic->api.sprite(map->tic, getBankTiles(), (sx+i) + (sy+j) * SHEET_COLS, tx, ty, NULL, 0); } drawCursorPos(map, mx, my); @@ -512,7 +512,7 @@ static void processMouseDrawMode(Map* map) { s32 tx = 0, ty = 0; getMouseMap(map, &tx, &ty); - s32 index = map->tic->api.map_get(map->tic, &map->tic->cart.bank.map, tx, ty); + s32 index = map->tic->api.map_get(map->tic, getBankMap(), tx, ty); map->sheet.rect = (SDL_Rect){index % SHEET_COLS, index / SHEET_COLS, 1, 1}; } @@ -593,7 +593,7 @@ static void drawPasteData(Map* map) for(s32 j = 0; j < h; j++) for(s32 i = 0; i < w; i++) - map->tic->api.map_set(map->tic, &map->tic->cart.bank.map, (mx+i)%TIC_MAP_WIDTH, (my+j)%TIC_MAP_HEIGHT, data[i + j * w]); + map->tic->api.map_set(map->tic, getBankMap(), (mx+i)%TIC_MAP_WIDTH, (my+j)%TIC_MAP_HEIGHT, data[i + j * w]); history_add(map->history); @@ -612,7 +612,7 @@ static void drawPasteData(Map* map) for(s32 j = 0; j < h; j++) for(s32 i = 0; i < w; i++) - map->tic->api.sprite(map->tic, &map->tic->cart.bank.tiles, data[i + j * w], mx + i*TIC_SPRITESIZE, my + j*TIC_SPRITESIZE, NULL, 0); + map->tic->api.sprite(map->tic, getBankTiles(), data[i + j * w], mx + i*TIC_SPRITESIZE, my + j*TIC_SPRITESIZE, NULL, 0); } } @@ -764,7 +764,7 @@ static void fillMap(Map* map, s32 x, s32 y, u8 tile) { for(s32 j = 0; j < map->sheet.rect.h; j++) for(s32 i = 0; i < map->sheet.rect.w; i++) - map->tic->api.map_set(map->tic, &map->tic->cart.bank.map, x+i, y+j, (mx+i) + (my+j) * SHEET_COLS); + map->tic->api.map_set(map->tic, getBankMap(), x+i, y+j, (mx+i) + (my+j) * SHEET_COLS); for(s32 i = 0; i < COUNT_OF(dx); i++) { @@ -776,7 +776,7 @@ static void fillMap(Map* map, s32 x, s32 y, u8 tile) bool match = true; for(s32 j = 0; j < map->sheet.rect.h; j++) for(s32 i = 0; i < map->sheet.rect.w; i++) - if(map->tic->api.map_get(map->tic, &map->tic->cart.bank.map, nx+i, ny+j) != tile) + if(map->tic->api.map_get(map->tic, getBankMap(), nx+i, ny+j) != tile) match = false; if(match) @@ -801,7 +801,7 @@ static void processMouseFillMode(Map* map) s32 tx = 0, ty = 0; getMouseMap(map, &tx, &ty); - fillMap(map, tx, ty, map->tic->api.map_get(map->tic, &map->tic->cart.bank.map, tx, ty)); + fillMap(map, tx, ty, map->tic->api.map_get(map->tic, getBankMap(), tx, ty)); history_add(map->history); } } @@ -864,7 +864,7 @@ static void drawMapOvr(Map* map) s32 scrollX = map->scroll.x % TIC_SPRITESIZE; s32 scrollY = map->scroll.y % TIC_SPRITESIZE; - map->tic->api.map(map->tic, &map->tic->cart.bank.map, &map->tic->cart.bank.tiles, map->scroll.x / TIC_SPRITESIZE, map->scroll.y / TIC_SPRITESIZE, + map->tic->api.map(map->tic, getBankMap(), getBankTiles(), map->scroll.x / TIC_SPRITESIZE, map->scroll.y / TIC_SPRITESIZE, TIC_MAP_SCREEN_WIDTH + 1, TIC_MAP_SCREEN_HEIGHT + 1, -scrollX, -scrollY, -1, 1); if(map->canvas.grid || map->scroll.active) @@ -950,7 +950,7 @@ static void copySelectionToClipboard(Map* map) normalizeMapRect(&x, &y); s32 index = x + y * TIC_MAP_WIDTH; - *ptr++ = map->tic->cart.bank.map.data[index]; + *ptr++ = getBankMap()->data[index]; } toClipboard(buffer, size, true); @@ -978,7 +978,7 @@ static void deleteSelection(Map* map) normalizeMapRect(&x, &y); s32 index = x + y * TIC_MAP_WIDTH; - map->tic->cart.bank.map.data[index] = 0; + getBankMap()->data[index] = 0; } history_add(map->history); diff --git a/src/music.c b/src/music.c index 9cd78e1..a2325a7 100644 --- a/src/music.c +++ b/src/music.c @@ -215,7 +215,7 @@ static void drawSwitch(Music* music, s32 x, s32 y, const char* label, s32 value, static tic_track* getTrack(Music* music) { - return &music->tic->cart.bank.music.tracks.data[music->track]; + return &getBankMusic()->tracks.data[music->track]; } static s32 getRows(Music* music) @@ -368,7 +368,7 @@ static tic_track_pattern* getPattern(Music* music, s32 channel) { s32 patternId = tic_tool_get_pattern_id(getTrack(music), music->tracker.frame, channel); - return patternId ? &music->tic->cart.bank.music.patterns.data[patternId - PATTERN_START] : NULL; + return patternId ? &getBankMusic()->patterns.data[patternId - PATTERN_START] : NULL; } static tic_track_pattern* getChannelPattern(Music* music) diff --git a/src/sfx.c b/src/sfx.c index 8dc0efb..1de5c4c 100644 --- a/src/sfx.c +++ b/src/sfx.c @@ -106,7 +106,7 @@ static void drawSwitch(Sfx* sfx, s32 x, s32 y, const char* label, s32 value, voi static tic_sound_effect* getEffect(Sfx* sfx) { - return sfx->tic->cart.bank.sfx.data + sfx->index; + return getBankSfx()->data + sfx->index; } static void setIndex(Sfx* sfx, s32 delta) @@ -169,7 +169,7 @@ static void drawLoopPanel(Sfx* sfx, s32 x, s32 y) static tic_waveform* getWaveformById(Sfx* sfx, s32 i) { - return &sfx->tic->cart.bank.sfx.waveform.envelopes[i]; + return &getBankSfx()->waveform.envelopes[i]; } static tic_waveform* getWaveform(Sfx* sfx) diff --git a/src/sprite.c b/src/sprite.c index ac693c4..8a9179a 100644 --- a/src/sprite.c +++ b/src/sprite.c @@ -38,12 +38,12 @@ static void clearCanvasSelection(Sprite* sprite) static u8 getSheetPixel(Sprite* sprite, s32 x, s32 y) { - return getSpritePixel(sprite->tic->cart.bank.tiles.data, x, sprite->index >= TIC_BANK_SPRITES ? y + TIC_SPRITESHEET_SIZE: y); + return getSpritePixel(getBankTiles()->data, x, sprite->index >= TIC_BANK_SPRITES ? y + TIC_SPRITESHEET_SIZE: y); } static void setSheetPixel(Sprite* sprite, s32 x, s32 y, u8 color) { - setSpritePixel(sprite->tic->cart.bank.tiles.data, x, sprite->index >= TIC_BANK_SPRITES ? y + TIC_SPRITESHEET_SIZE: y, color); + setSpritePixel(getBankTiles()->data, x, sprite->index >= TIC_BANK_SPRITES ? y + TIC_SPRITESHEET_SIZE: y, color); } static s32 getIndexPosX(Sprite* sprite) @@ -672,8 +672,8 @@ static void drawRGBSlider(Sprite* sprite, s32 x, s32 y, u8* value) static void pasteColor(Sprite* sprite) { - fromClipboard(sprite->tic->cart.bank.palette.data, sizeof(tic_palette), false, true); - fromClipboard(&sprite->tic->cart.bank.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) @@ -708,7 +708,7 @@ static void drawRGBTools(Sprite* sprite, s32 x, s32 y) down = true; if(checkMouseClick(&rect, SDL_BUTTON_LEFT)) - toClipboard(sprite->tic->cart.bank.palette.data, sizeof(tic_palette), false); + toClipboard(getBankPalette()->data, sizeof(tic_palette), false); } if(down) @@ -772,7 +772,7 @@ static void drawRGBSliders(Sprite* sprite, s32 x, s32 y) { enum{Gap = 6, Count = sizeof(tic_rgb)}; - u8* data = &sprite->tic->cart.bank.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]); @@ -784,7 +784,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.bank.palette.data[sprite->color * Count]; + u8* data = &getBankPalette()->data[sprite->color * Count]; for(s32 i = 0; i < Count; i++) { @@ -959,7 +959,7 @@ static void drawSheetOvr(Sprite* sprite, s32 x, s32 y) for(s32 j = 0, index = (sprite->index - sprite->index % TIC_BANK_SPRITES); j < rect.h; j += TIC_SPRITESIZE) for(s32 i = 0; i < rect.w; i += TIC_SPRITESIZE, index++) - sprite->tic->api.sprite(sprite->tic, &sprite->tic->cart.bank.tiles, index, x + i, y + j, NULL, 0); + sprite->tic->api.sprite(sprite->tic, getBankTiles(), index, x + i, y + j, NULL, 0); { s32 bx = getIndexPosX(sprite) + x - 1; s32 by = getIndexPosY(sprite) + y - 1; diff --git a/src/studio.c b/src/studio.c index ff5837b..8c73fcf 100644 --- a/src/studio.c +++ b/src/studio.c @@ -74,6 +74,15 @@ typedef struct } MouseState; +static const EditorMode Modes[] = +{ + TIC_CODE_MODE, + TIC_SPRITE_MODE, + TIC_MAP_MODE, + TIC_SFX_MODE, + TIC_MUSIC_MODE, +}; + static struct { tic80_local* tic80local; @@ -147,7 +156,21 @@ static struct struct { bool show; - s32 index; + + union + { + struct + { + s32 code; + s32 tiles; + s32 map; + s32 sfx; + s32 music; + } index; + + s32 indexes[COUNT_OF(Modes)]; + }; + } bank; struct @@ -260,7 +283,6 @@ static struct .bank = { .show = false, - .index = 0, }, .popup = @@ -288,6 +310,41 @@ static struct .floatSamples = NULL, }; +tic_tiles* getBankTiles() +{ + return &studio.tic->cart.banks[studio.bank.index.tiles].tiles; +} + +tic_tiles* getBankSprites() +{ + return &studio.tic->cart.banks[studio.bank.index.tiles].sprites; +} + +tic_map* getBankMap() +{ + return &studio.tic->cart.banks[studio.bank.index.map].map; +} + +tic_sfx* getBankSfx() +{ + return &studio.tic->cart.banks[studio.bank.index.sfx].sfx; +} + +tic_music* getBankMusic() +{ + return &studio.tic->cart.banks[studio.bank.index.music].music; +} + +tic_code* getBankCode() +{ + return &studio.tic->cart.banks[studio.bank.index.code].code; +} + +tic_palette* getBankPalette() +{ + return &studio.tic->cart.banks[studio.bank.index.tiles].palette; +} + void playSystemSfx(s32 id) { const tic_sound_effect* effect = &studio.tic->config.sfx.data[id]; @@ -429,15 +486,6 @@ void showTooltip(const char* text) strcpy(studio.tooltip.text, text); } -static const EditorMode Modes[] = -{ - TIC_CODE_MODE, - TIC_SPRITE_MODE, - TIC_MAP_MODE, - TIC_SFX_MODE, - TIC_MUSIC_MODE, -}; - static void drawExtrabar(tic_mem* tic) { enum {Size = 7}; @@ -553,6 +601,14 @@ static void drawBankIcon(s32 x, s32 y) }; bool over = false; + s32 mode = 0; + + for(s32 i = 0; i < COUNT_OF(Modes); i++) + if(Modes[i] == studio.mode) + { + mode = i; + break; + } if(checkMousePos(&rect)) { @@ -583,15 +639,15 @@ static void drawBankIcon(s32 x, s32 y) over = true; if(checkMouseClick(&rect, SDL_BUTTON_LEFT)) - studio.bank.index = i; + studio.bank.indexes[mode] = i; } - if(i == studio.bank.index) + if(i == studio.bank.indexes[mode]) { tic->api.rect(tic, rect.x, rect.y, rect.w, rect.h, tic_color_red); } - tic->api.draw_char(tic, '0' + i, rect.x+1, rect.y+1, i == studio.bank.index ? tic_color_white : over ? tic_color_red : tic_color_peach); + tic->api.draw_char(tic, '0' + i, rect.x+1, rect.y+1, i == studio.bank.indexes[mode] ? tic_color_white : over ? tic_color_red : tic_color_peach); } } else @@ -2212,8 +2268,8 @@ static void renderStudio() music = &studio.tic->config.music; break; default: - sfx = &studio.tic->cart.bank.sfx; - music = &studio.tic->cart.bank.music; + sfx = getBankSfx(); + music = getBankMusic(); } studio.tic->api.tick_start(studio.tic, sfx, music); @@ -2504,20 +2560,20 @@ static void onFSInitialized(FileSystem* fs) studio.tic = studio.tic80local->memory; { - studio.start = SDL_malloc(sizeof(Start)); - studio.console = SDL_malloc(sizeof(Console)); - studio.run = SDL_malloc(sizeof(Run)); - studio.code = SDL_malloc(sizeof(Code)); - studio.sprite = SDL_malloc(sizeof(Sprite)); - studio.map = SDL_malloc(sizeof(Map)); - studio.world = SDL_malloc(sizeof(World)); - studio.sfx = SDL_malloc(sizeof(Sfx)); - studio.music = SDL_malloc(sizeof(Music)); - studio.config = SDL_malloc(sizeof(Config)); - studio.keymap = SDL_malloc(sizeof(Keymap)); - studio.dialog = SDL_malloc(sizeof(Dialog)); - studio.menu = SDL_malloc(sizeof(Menu)); - studio.surf = SDL_malloc(sizeof(Surf)); + studio.start = SDL_malloc(sizeof(Start)); + studio.console = SDL_malloc(sizeof(Console)); + studio.run = SDL_malloc(sizeof(Run)); + studio.code = SDL_malloc(sizeof(Code)); + studio.sprite = SDL_malloc(sizeof(Sprite)); + studio.map = SDL_malloc(sizeof(Map)); + studio.world = SDL_malloc(sizeof(World)); + studio.sfx = SDL_malloc(sizeof(Sfx)); + studio.music = SDL_malloc(sizeof(Music)); + studio.config = SDL_malloc(sizeof(Config)); + studio.keymap = SDL_malloc(sizeof(Keymap)); + studio.dialog = SDL_malloc(sizeof(Dialog)); + studio.menu = SDL_malloc(sizeof(Menu)); + studio.surf = SDL_malloc(sizeof(Surf)); } fsMakeDir(fs, TIC_LOCAL); @@ -2611,6 +2667,23 @@ s32 main(s32 argc, char **argv) #endif + { + SDL_free(studio.start); + SDL_free(studio.console); + SDL_free(studio.run); + SDL_free(studio.code); + SDL_free(studio.sprite); + SDL_free(studio.map); + SDL_free(studio.world); + SDL_free(studio.sfx); + SDL_free(studio.music); + SDL_free(studio.config); + SDL_free(studio.keymap); + SDL_free(studio.dialog); + SDL_free(studio.menu); + SDL_free(studio.surf); + } + if(studio.tic80local) tic80_delete((tic80*)studio.tic80local); diff --git a/src/studio.h b/src/studio.h index 73b43e5..33f7bae 100644 --- a/src/studio.h +++ b/src/studio.h @@ -199,3 +199,11 @@ void gotoCode(); void gotoSurf(); void exitFromGameMenu(); void runProject(); + +tic_tiles* getBankTiles(); +tic_tiles* getBankSprites(); +tic_map* getBankMap(); +tic_sfx* getBankSfx(); +tic_music* getBankMusic(); +tic_code* getBankCode(); +tic_palette* getBankPalette(); diff --git a/src/world.c b/src/world.c index 614a32a..1d4f025 100644 --- a/src/world.c +++ b/src/world.c @@ -105,13 +105,13 @@ void initWorld(World* world, tic_mem* tic, Map* map) for(s32 i = 0; i < TIC80_WIDTH * TIC80_HEIGHT; i++) { - u8 index = tic->cart.bank.map.data[i]; + u8 index = getBankMap()->data[i]; if(index) { SDL_memset(colors, 0, sizeof colors); - tic_tile* tile = &tic->cart.bank.tiles.data[index]; + tic_tile* tile = &getBankTiles()->data[index]; for(s32 p = 0; p < TIC_SPRITESIZE * TIC_SPRITESIZE; p++) {