From 295dd90766893196f950ebc1f6c8fd9fe36a5e83 Mon Sep 17 00:00:00 2001 From: "BADIM-PC\\Vadim" Date: Thu, 14 Dec 2017 17:08:04 +0300 Subject: [PATCH] added separate editor per bank --- src/code.c | 82 +++++++++++------------ src/code.h | 2 +- src/map.c | 25 +++---- src/map.h | 4 +- src/music.c | 9 +-- src/music.h | 5 +- src/sfx.c | 56 ++++++---------- src/sfx.h | 10 ++- src/sprite.c | 11 ++-- src/sprite.h | 4 +- src/studio.c | 180 ++++++++++++++++++++++++++++++++------------------- src/studio.h | 7 +- 12 files changed, 215 insertions(+), 180 deletions(-) diff --git a/src/code.c b/src/code.c index ef81dab..fa31e14 100644 --- a/src/code.c +++ b/src/code.c @@ -68,7 +68,7 @@ static void drawCode(Code* code, bool withCursor) s32 xStart = code->rect.x - code->scroll.x * STUDIO_TEXT_WIDTH; s32 x = xStart; s32 y = code->rect.y - code->scroll.y * STUDIO_TEXT_HEIGHT; - char* pointer = code->data; + char* pointer = code->src; u8* colorPointer = code->colorBuffer; @@ -116,7 +116,7 @@ static void getCursorPosition(Code* code, s32* x, s32* y) *x = 0; *y = 0; - const char* pointer = code->data; + const char* pointer = code->src; while(*pointer) { @@ -135,7 +135,7 @@ static void getCursorPosition(Code* code, s32* x, s32* y) static s32 getLinesCount(Code* code) { - char* text = code->data; + char* text = code->src; s32 count = 0; while(*text) @@ -177,10 +177,10 @@ static void updateEditor(Code* code) sprintf(status, "line %i/%i col %i", line + 1, count + 1, column + 1); memcpy(code->status, status, strlen(status)); - size_t codeLen = strlen(code->data); + size_t codeLen = strlen(code->src); sprintf(status, "%i/%i", (u32)codeLen, TIC_CODE_SIZE); - memset(code->data + codeLen, '\0', TIC_CODE_SIZE - codeLen); + memset(code->src + codeLen, '\0', TIC_CODE_SIZE - codeLen); memcpy(code->status + sizeof code->status - strlen(status) - 1, status, strlen(status)); } } @@ -213,7 +213,7 @@ static void highlightStrings(Code* code, const char* text, u8* color, char separ static void highlightNumbers(Code* code, u8* color) { - const char* text = code->data; + const char* text = code->src; const char* pointer = text; while(*pointer) @@ -267,7 +267,7 @@ static void highlightWords(const char* text, u8* color, const char* const string static void highlightMoonKeywords(Code* code, u8* color) { - const char* text = code->data; + const char* text = code->src; static const char* const MoonKeywords [] = { @@ -285,7 +285,7 @@ static void highlightMoonKeywords(Code* code, u8* color) static void highlightLuaKeywords(Code* code, u8* color) { - const char* text = code->data; + const char* text = code->src; static const char* const LuaKeywords [] = { @@ -300,7 +300,7 @@ static void highlightLuaKeywords(Code* code, u8* color) static void highlightJsKeywords(Code* code, u8* color) { - const char* text = code->data; + const char* text = code->src; static const char* const JsKeywords [] = { @@ -317,13 +317,13 @@ static void highlightApi(Code* code, u8* color) { static const char* const ApiKeywords[] = API_KEYWORDS; - const char* text = code->data; + const char* text = code->src; highlightWords(text, color, ApiKeywords, COUNT_OF(ApiKeywords), getConfig()->theme.code.api); } static void highlightNonChars(Code* code, u8* color) { - const char* text = code->data; + const char* text = code->src; while(*text) { @@ -337,7 +337,7 @@ static void highlightNonChars(Code* code, u8* color) static void highlightSigns(Code* code, u8* color) { - const char* text = code->data; + const char* text = code->src; static const char* const LuaSigns [] = { @@ -364,7 +364,7 @@ static void highlightSigns(Code* code, u8* color) static void highlightCommentsBase(Code* code, u8* color, const char* pattern1, const char* pattern2, s32 extraSize) { - const char* text = code->data; + const char* text = code->src; const char* pointer = text; while(*pointer) @@ -417,7 +417,7 @@ static void parseSyntaxColor(Code* code) highlightNumbers(code, color); highlightSigns(code, color); highlightCommentsBase(code, color, "--", "\n", 0); - highlightStrings(code, code->data, color, '"'); + highlightStrings(code, code->src, color, '"'); break; case tic_script_lua: highlightNonChars(code, color); @@ -426,7 +426,7 @@ static void parseSyntaxColor(Code* code) highlightNumbers(code, color); highlightSigns(code, color); highlightComments(code, color); - highlightStrings(code, code->data, color, '"'); + highlightStrings(code, code->src, color, '"'); break; case tic_script_js: highlightNonChars(code, color); @@ -435,14 +435,14 @@ static void parseSyntaxColor(Code* code) highlightNumbers(code, color); highlightSigns(code, color); highlightJsComments(code, color); - highlightStrings(code, code->data, color, '"'); + highlightStrings(code, code->src, color, '"'); break; } } static char* getLineByPos(Code* code, char* pos) { - char* text = code->data; + char* text = code->src; char* line = text; while(text < pos) @@ -459,7 +459,7 @@ static char* getLine(Code* code) static char* getPrevLine(Code* code) { - char* text = code->data; + char* text = code->src; char* pos = code->cursor.position; char* prevLine = text; char* line = text; @@ -510,7 +510,7 @@ static void setCursorPosition(Code* code, s32 cx, s32 cy) { s32 x = 0; s32 y = 0; - char* pointer = code->data; + char* pointer = code->src; while(*pointer) { @@ -559,7 +559,7 @@ static void downLine(Code* code) static void leftColumn(Code* code) { - char* start = code->data; + char* start = code->src; if(code->cursor.position > start) { @@ -579,7 +579,7 @@ static void rightColumn(Code* code) static void leftWord(Code* code) { - const char* start = code->data; + const char* start = code->src; char* pos = code->cursor.position-1; if(pos > start) @@ -595,7 +595,7 @@ static void leftWord(Code* code) static void rightWord(Code* code) { - const char* end = code->data + strlen(code->data); + const char* end = code->src + strlen(code->src); char* pos = code->cursor.position; if(pos < end) @@ -625,14 +625,14 @@ static void goEnd(Code* code) static void goCodeHome(Code *code) { - code->cursor.position = code->data; + code->cursor.position = code->src; updateColumn(code); } static void goCodeEnd(Code *code) { - code->cursor.position = code->data + strlen(code->data); + code->cursor.position = code->src + strlen(code->src); updateColumn(code); } @@ -692,7 +692,7 @@ static void deleteChar(Code* code) static void backspaceChar(Code* code) { - if(!replaceSelection(code) && code->cursor.position > code->data) + if(!replaceSelection(code) && code->cursor.position > code->src) { char* pos = --code->cursor.position; memmove(pos, pos + 1, strlen(pos)); @@ -703,7 +703,7 @@ static void backspaceChar(Code* code) static void inputSymbolBase(Code* code, char sym) { - if (strlen(code->data) >= sizeof(tic_code)) + if (strlen(code->src) >= sizeof(tic_code)) return; char* pos = code->cursor.position; @@ -747,7 +747,7 @@ static void newLine(Code* code) static void selectAll(Code* code) { - code->cursor.selection = code->data; + code->cursor.selection = code->src; code->cursor.position = code->cursor.selection + strlen(code->cursor.selection); } @@ -807,7 +807,7 @@ static void copyFromClipboard(Code* code) // cut clipboard code if overall code > max code size { - size_t codeSize = strlen(code->data); + size_t codeSize = strlen(code->src); if (codeSize + size > sizeof(tic_code)) { @@ -995,7 +995,7 @@ static void updateOutlineCode(Code* code) } else { - code->cursor.position = code->data; + code->cursor.position = code->src; code->cursor.selection = NULL; } @@ -1008,7 +1008,7 @@ static void setMoonscriptOutlineMode(Code* code) OutlineItem* out = code->outline.items; OutlineItem* end = out + OUTLINE_SIZE; - char* ptr = code->data; + char* ptr = code->src; static const char FuncString[] = "=->"; char buffer[STUDIO_TEXT_BUFFER_WIDTH]; @@ -1020,18 +1020,18 @@ static void setMoonscriptOutlineMode(Code* code) { ptr = strstr(ptr, FuncString); - if(ptr && ptr > code->data) + if(ptr && ptr > code->src) { char* endPtr = ptr; ptr += sizeof FuncString - 1; - while(endPtr >= code->data && !isLetter(*endPtr) && !isNumber(*endPtr)) endPtr--; + while(endPtr >= code->src && !isLetter(*endPtr) && !isNumber(*endPtr)) endPtr--; char* start = endPtr; - for (const char* val = start-1; val >= code->data && (isLetter(*val) || isNumber(*val)); val--, start--); + for (const char* val = start-1; val >= code->src && (isLetter(*val) || isNumber(*val)); val--, start--); - if(start >= code->data) + if(start >= code->src) { memset(buffer, 0, sizeof buffer); memcpy(buffer, start, endPtr - start + 1); @@ -1065,7 +1065,7 @@ static void setLuaOutlineMode(Code* code) OutlineItem* out = code->outline.items; OutlineItem* end = out + OUTLINE_SIZE; - char* ptr = code->data; + char* ptr = code->src; static const char FuncString[] = "function "; char buffer[STUDIO_TEXT_BUFFER_WIDTH]; @@ -1156,7 +1156,7 @@ static void commentLine(Code* code) if(memcmp(line, Comment, Size)) { - if (strlen(code->data) + Size >= sizeof(tic_code)) + if (strlen(code->src) + Size >= sizeof(tic_code)) return; memmove(line + Size, line, strlen(line)+1); @@ -1481,7 +1481,7 @@ static void textFindTick(Code* code) bool reverse = keycode == SDLK_UP || keycode == SDLK_LEFT; char* (*func)(const char*, const char*, const char*) = reverse ? upStrStr : downStrStr; char* from = reverse ? SDL_min(code->cursor.position, code->cursor.selection) : SDL_max(code->cursor.position, code->cursor.selection); - char* pos = func(code->data, from, code->popup.text); + char* pos = func(code->src, from, code->popup.text); updateFindCode(code, pos); } break; @@ -1489,7 +1489,7 @@ static void textFindTick(Code* code) if(*code->popup.text) { code->popup.text[strlen(code->popup.text)-1] = '\0'; - updateFindCode(code, strstr(code->data, code->popup.text)); + updateFindCode(code, strstr(code->src, code->popup.text)); } break; default: break; @@ -1501,7 +1501,7 @@ static void textFindTick(Code* code) if(strlen(code->popup.text) + 1 < sizeof code->popup.text) { strcat(code->popup.text, event->text.text); - updateFindCode(code, strstr(code->data, code->popup.text)); + updateFindCode(code, strstr(code->src, code->popup.text)); } } break; @@ -1838,7 +1838,7 @@ void initCode(Code* code, tic_mem* tic, tic_code* src) *code = (Code) { .tic = tic, - .data = src->data, + .src = src->data, .tick = tick, .escape = escape, .cursor = {{src->data, NULL, 0, 0}, NULL, 0}, @@ -1863,7 +1863,7 @@ void initCode(Code* code, tic_mem* tic, tic_code* src) .update = update, }; - code->history = history_create(code->data, sizeof(tic_code)); + code->history = history_create(code->src, sizeof(tic_code)); code->cursorHistory = history_create(&code->cursor, sizeof code->cursor); update(code); diff --git a/src/code.h b/src/code.h index a05bad7..70e196d 100644 --- a/src/code.h +++ b/src/code.h @@ -31,7 +31,7 @@ struct Code { tic_mem* tic; - char* data; + char* src; struct { diff --git a/src/map.c b/src/map.c index 64a6d0a..063732c 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, getBankMap(), tx, ty); + index = map->tic->api.map_get(map->tic, map->src, tx, ty); } } @@ -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, getBankMap(), (x+i)%TIC_MAP_WIDTH, (y+j)%TIC_MAP_HEIGHT, (mx+i) + (my+j) * SHEET_COLS); + map->tic->api.map_set(map->tic, map->src, (x+i)%TIC_MAP_WIDTH, (y+j)%TIC_MAP_HEIGHT, (mx+i) + (my+j) * SHEET_COLS); history_add(map->history); } @@ -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, getBankMap(), tx, ty); + s32 index = map->tic->api.map_get(map->tic, map->src, 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, getBankMap(), (mx+i)%TIC_MAP_WIDTH, (my+j)%TIC_MAP_HEIGHT, data[i + j * w]); + map->tic->api.map_set(map->tic, map->src, (mx+i)%TIC_MAP_WIDTH, (my+j)%TIC_MAP_HEIGHT, data[i + j * w]); history_add(map->history); @@ -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, getBankMap(), x+i, y+j, (mx+i) + (my+j) * SHEET_COLS); + map->tic->api.map_set(map->tic, map->src, 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, getBankMap(), nx+i, ny+j) != tile) + if(map->tic->api.map_get(map->tic, map->src, 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, getBankMap(), tx, ty)); + fillMap(map, tx, ty, map->tic->api.map_get(map->tic, map->src, 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, getBankMap(), getBankTiles(), map->scroll.x / TIC_SPRITESIZE, map->scroll.y / TIC_SPRITESIZE, + map->tic->api.map(map->tic, map->src, 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++ = getBankMap()->data[index]; + *ptr++ = map->src->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; - getBankMap()->data[index] = 0; + map->src->data[index] = 0; } history_add(map->history); @@ -1147,7 +1147,7 @@ static void overlap(tic_mem* tic, void* data) drawSheetOvr(map, TIC80_WIDTH - TIC_SPRITESHEET_SIZE - 1, TOOLBAR_SIZE); } -void initMap(Map* map, tic_mem* tic) +void initMap(Map* map, tic_mem* tic, tic_map* src) { if(map->history) history_delete(map->history); @@ -1155,6 +1155,7 @@ void initMap(Map* map, tic_mem* tic) { .tic = tic, .tick = tick, + .src = src, .mode = MAP_DRAW_MODE, .canvas = { @@ -1185,7 +1186,7 @@ void initMap(Map* map, tic_mem* tic) .gesture = false, .start = {0, 0}, }, - .history = history_create(&tic->cart.bank0.map, sizeof(tic_map)), + .history = history_create(src, sizeof(tic_map)), .event = onStudioEvent, .overlap = overlap, }; diff --git a/src/map.h b/src/map.h index fbd6b5f..7c9f173 100644 --- a/src/map.h +++ b/src/map.h @@ -29,6 +29,8 @@ typedef struct Map Map; struct Map { tic_mem* tic; + + tic_map* src; s32 tickCounter; @@ -83,4 +85,4 @@ struct Map void(*overlap)(tic_mem* tic, void* data); }; -void initMap(Map*, tic_mem*); +void initMap(Map*, tic_mem*, tic_map* src); diff --git a/src/music.c b/src/music.c index 9156718..4f8ca64 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 &getBankMusic()->tracks.data[music->track]; + return &music->src->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 ? &getBankMusic()->patterns.data[patternId - PATTERN_START] : NULL; + return patternId ? &music->src->patterns.data[patternId - PATTERN_START] : NULL; } static tic_track_pattern* getChannelPattern(Music* music) @@ -1608,7 +1608,7 @@ static void onStudioEvent(Music* music, StudioEvent event) } } -void initMusic(Music* music, tic_mem* tic) +void initMusic(Music* music, tic_mem* tic, tic_music* src) { if (music->history) history_delete(music->history); @@ -1616,6 +1616,7 @@ void initMusic(Music* music, tic_mem* tic) { .tic = tic, .tick = tick, + .src = src, .track = 0, .tracker = { @@ -1643,7 +1644,7 @@ void initMusic(Music* music, tic_mem* tic) }, .tab = MUSIC_TRACKER_TAB, - .history = history_create(&tic->cart.bank0.music, sizeof(tic_music)), + .history = history_create(src, sizeof(tic_music)), .event = onStudioEvent, }; diff --git a/src/music.h b/src/music.h index 7f2b795..01782dd 100644 --- a/src/music.h +++ b/src/music.h @@ -29,6 +29,9 @@ typedef struct Music Music; struct Music { tic_mem* tic; + + tic_music* src; + u8 track:MUSIC_TRACKS_BITS; struct @@ -72,4 +75,4 @@ struct Music void(*event)(Music*, StudioEvent); }; -void initMusic(Music*, tic_mem*); \ No newline at end of file +void initMusic(Music*, tic_mem*, tic_music* src); \ No newline at end of file diff --git a/src/sfx.c b/src/sfx.c index 48957c8..cda5368 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 getBankSfx()->data + sfx->index; + return sfx->src->data + sfx->index; } static void setIndex(Sfx* sfx, s32 delta) @@ -120,7 +120,7 @@ static void setSpeed(Sfx* sfx, s32 delta) effect->speed += delta; - history_add(sfx->history.envelope); + history_add(sfx->history); } static void drawTopPanel(Sfx* sfx, s32 x, s32 y) @@ -141,7 +141,7 @@ static void setLoopStart(Sfx* sfx, s32 delta) loop->start += delta; - history_add(sfx->history.envelope); + history_add(sfx->history); } static void setLoopSize(Sfx* sfx, s32 delta) @@ -151,7 +151,7 @@ static void setLoopSize(Sfx* sfx, s32 delta) loop->size += delta; - history_add(sfx->history.envelope); + history_add(sfx->history); } static void drawLoopPanel(Sfx* sfx, s32 x, s32 y) @@ -169,7 +169,7 @@ static void drawLoopPanel(Sfx* sfx, s32 x, s32 y) static tic_waveform* getWaveformById(Sfx* sfx, s32 i) { - return &getBankSfx()->waveform.envelopes[i]; + return &sfx->src->waveform.envelopes[i]; } static tic_waveform* getWaveform(Sfx* sfx) @@ -398,7 +398,7 @@ static void drawCanvas(Sfx* sfx, s32 x, s32 y) default: break; } - history_add(sfx->history.envelope); + history_add(sfx->history); } } @@ -563,22 +563,12 @@ static void playSound(Sfx* sfx) static void undo(Sfx* sfx) { - history_undo(sfx->history.envelope); + history_undo(sfx->history); } static void redo(Sfx* sfx) { - history_redo(sfx->history.envelope); -} - -static void undoWave(Sfx* sfx) -{ - history_undo(sfx->history.waveform); -} - -static void redoWave(Sfx* sfx) -{ - history_redo(sfx->history.waveform); + history_redo(sfx->history); } static void copyToClipboard(Sfx* sfx) @@ -598,7 +588,7 @@ static void resetSfx(Sfx* sfx) tic_sound_effect* effect = getEffect(sfx); memset(effect, 0, sizeof(tic_sound_effect)); - history_add(sfx->history.envelope); + history_add(sfx->history); } static void resetWave(Sfx* sfx) @@ -606,7 +596,7 @@ static void resetWave(Sfx* sfx) tic_waveform* wave = getWaveform(sfx); memset(wave, 0, sizeof(tic_waveform)); - history_add(sfx->history.waveform); + history_add(sfx->history); } static void cutToClipboard(Sfx* sfx) @@ -626,7 +616,7 @@ static void copyFromClipboard(Sfx* sfx) tic_sound_effect* effect = getEffect(sfx); if(fromClipboard(effect, sizeof(tic_sound_effect), true, false)) - history_add(sfx->history.envelope); + history_add(sfx->history); } static void copyWaveFromClipboard(Sfx* sfx) @@ -634,7 +624,7 @@ static void copyWaveFromClipboard(Sfx* sfx) tic_waveform* wave = getWaveform(sfx); if(fromClipboard(wave, sizeof(tic_waveform), true, false)) - history_add(sfx->history.waveform); + history_add(sfx->history); } static void processKeyboard(Sfx* sfx) @@ -728,8 +718,8 @@ static void processWaveformKeydown(Sfx* sfx, SDL_Keycode keycode) { switch(keycode) { - case SDLK_z: undoWave(sfx); break; - case SDLK_y: redoWave(sfx); break; + case SDLK_z: undo(sfx); break; + case SDLK_y: redo(sfx); break; } } @@ -948,7 +938,7 @@ static void drawWaveformCanvas(Sfx* sfx, s32 x, s32 y) tic_tool_poke4(wave->data, mx, Rows - my - 1); - history_add(sfx->history.waveform); + history_add(sfx->history); } } @@ -1019,8 +1009,8 @@ static void onStudioWaveformEvent(Sfx* sfx, StudioEvent event) case TIC_TOOLBAR_CUT: cutWaveToClipboard(sfx); break; case TIC_TOOLBAR_COPY: copyWaveToClipboard(sfx); break; case TIC_TOOLBAR_PASTE: copyWaveFromClipboard(sfx); break; - case TIC_TOOLBAR_UNDO: undoWave(sfx); break; - case TIC_TOOLBAR_REDO: redoWave(sfx); break; + case TIC_TOOLBAR_UNDO: undo(sfx); break; + case TIC_TOOLBAR_REDO: redo(sfx); break; default: break; } } @@ -1035,15 +1025,15 @@ static void onStudioEvent(Sfx* sfx, StudioEvent event) } } -void initSfx(Sfx* sfx, tic_mem* tic) +void initSfx(Sfx* sfx, tic_mem* tic, tic_sfx* src) { - if(sfx->history.envelope) history_delete(sfx->history.envelope); - if(sfx->history.waveform) history_delete(sfx->history.waveform); + if(sfx->history) history_delete(sfx->history); *sfx = (Sfx) { .tic = tic, .tick = tick, + .src = src, .index = 0, .play = { @@ -1056,11 +1046,7 @@ void initSfx(Sfx* sfx, tic_mem* tic) }, .canvasTab = SFX_WAVE_TAB, .tab = SFX_ENVELOPES_TAB, - .history = - { - .envelope = history_create(&tic->cart.bank0.sfx.data, sizeof tic->cart.bank0.sfx.data), - .waveform = history_create(&tic->cart.bank0.sfx.waveform, sizeof tic->cart.bank0.sfx.waveform), - }, + .history = history_create(src, sizeof(tic_sfx)), .event = onStudioEvent, }; } \ No newline at end of file diff --git a/src/sfx.h b/src/sfx.h index 19108b7..4a0feb9 100644 --- a/src/sfx.h +++ b/src/sfx.h @@ -30,6 +30,8 @@ struct Sfx { tic_mem* tic; + tic_sfx* src; + u8 index:SFX_COUNT_BITS; struct @@ -57,14 +59,10 @@ struct Sfx SFX_ENVELOPES_TAB, } tab; - struct - { - struct History* envelope; - struct History* waveform; - } history; + struct History* history; void(*tick)(Sfx*); void(*event)(Sfx*, StudioEvent); }; -void initSfx(Sfx*, tic_mem*); \ No newline at end of file +void initSfx(Sfx*, tic_mem*, tic_sfx* src); \ No newline at end of file diff --git a/src/sprite.c b/src/sprite.c index 0bb39c9..bdcb30f 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(getBankTiles()->data, x, sprite->index >= TIC_BANK_SPRITES ? y + TIC_SPRITESHEET_SIZE: y); + return getSpritePixel(sprite->src->data, x, sprite->index >= TIC_BANK_SPRITES ? y + TIC_SPRITESHEET_SIZE: y); } static void setSheetPixel(Sprite* sprite, s32 x, s32 y, u8 color) { - setSpritePixel(getBankTiles()->data, x, sprite->index >= TIC_BANK_SPRITES ? y + TIC_SPRITESHEET_SIZE: y, color); + setSpritePixel(sprite->src->data, x, sprite->index >= TIC_BANK_SPRITES ? y + TIC_SPRITESHEET_SIZE: y, color); } static s32 getIndexPosX(Sprite* sprite) @@ -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, getBankTiles(), index, x + i, y + j, NULL, 0); + sprite->tic->api.sprite(sprite->tic, sprite->src, index, x + i, y + j, NULL, 0); { s32 bx = getIndexPosX(sprite) + x - 1; s32 by = getIndexPosY(sprite) + y - 1; @@ -1544,7 +1544,7 @@ static void overlap(tic_mem* tic, void* data) drawSheetOvr(sprite, TIC80_WIDTH - TIC_SPRITESHEET_SIZE - 1, 7); } -void initSprite(Sprite* sprite, tic_mem* tic) +void initSprite(Sprite* sprite, tic_mem* tic, tic_tiles* src) { if(sprite->select.back == NULL) sprite->select.back = (u8*)SDL_malloc(CANVAS_SIZE*CANVAS_SIZE); if(sprite->select.front == NULL) sprite->select.front = (u8*)SDL_malloc(CANVAS_SIZE*CANVAS_SIZE); @@ -1555,6 +1555,7 @@ void initSprite(Sprite* sprite, tic_mem* tic) .tic = tic, .tick = tick, .tickCounter = 0, + .src = src, .index = 0, .color = 1, .color2 = 0, @@ -1570,7 +1571,7 @@ void initSprite(Sprite* sprite, tic_mem* tic) .front = sprite->select.front, }, .mode = SPRITE_DRAW_MODE, - .history = history_create(&tic->cart.bank0.tiles, TIC_SPRITES * sizeof(tic_tile)), + .history = history_create(src, TIC_SPRITES * sizeof(tic_tile)), .event = onStudioEvent, .overlap = overlap, }; diff --git a/src/sprite.h b/src/sprite.h index a147108..2b13752 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -30,6 +30,8 @@ struct Sprite { tic_mem* tic; + tic_tiles* src; + u32 tickCounter; u16 index; @@ -64,4 +66,4 @@ struct Sprite void (*overlap)(tic_mem* tic, void* data); }; -void initSprite(Sprite*, tic_mem*); \ No newline at end of file +void initSprite(Sprite*, tic_mem*, tic_tiles* src); \ No newline at end of file diff --git a/src/studio.c b/src/studio.c index 3ad34b3..2a8d421 100644 --- a/src/studio.c +++ b/src/studio.c @@ -162,7 +162,7 @@ static struct struct { s32 code; - s32 tiles; + s32 sprites; s32 map; s32 sfx; s32 music; @@ -196,17 +196,21 @@ static struct bool fullscreen; + struct + { + Code* code[TIC_BANKS]; + Sprite* sprite[TIC_BANKS]; + Map* map[TIC_BANKS]; + Sfx* sfx[TIC_BANKS]; + Music* music[TIC_BANKS]; + } editor; + struct { Start* start; Console* console; Run* run; - Code* code[TIC_BANKS]; - Sprite* sprite; - Map* map; World* world; - Sfx* sfx; - Music* music; Config* config; Keymap* keymap; Dialog* dialog; @@ -312,12 +316,7 @@ static struct 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; + return &studio.tic->cart.banks[studio.bank.index.sprites].tiles; } tic_map* getBankMap() @@ -325,16 +324,6 @@ 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; -} - void playSystemSfx(s32 id) { const tic_sound_effect* effect = &studio.tic->config.bank0.sfx.data[id]; @@ -782,14 +771,34 @@ void setStudioEvent(StudioEvent event) { case TIC_CODE_MODE: { - Code* code = studio.code[studio.bank.index.code]; + Code* code = studio.editor.code[studio.bank.index.code]; code->event(code, event); } break; - case TIC_SPRITE_MODE: studio.sprite->event(studio.sprite, event); break; - case TIC_MAP_MODE: studio.map->event(studio.map, event); break; - case TIC_SFX_MODE: studio.sfx->event(studio.sfx, event); break; - case TIC_MUSIC_MODE: studio.music->event(studio.music, event); break; + case TIC_SPRITE_MODE: + { + Sprite* sprite = studio.editor.sprite[studio.bank.index.sprites]; + sprite->event(sprite, event); + } + break; + case TIC_MAP_MODE: + { + Map* map = studio.editor.map[studio.bank.index.map]; + map->event(map, event); + } + break; + case TIC_SFX_MODE: + { + Sfx* sfx = studio.editor.sfx[studio.bank.index.sfx]; + sfx->event(sfx, event); + } + break; + case TIC_MUSIC_MODE: + { + Music* music = studio.editor.music[studio.bank.index.music]; + music->event(music, event); + } + break; default: break; } } @@ -864,7 +873,7 @@ void drawBitIcon(s32 x, s32 y, const u8* ptr, u8 color) static void initWorldMap() { - initWorld(studio.world, studio.tic, studio.map); + initWorld(studio.world, studio.tic, studio.editor.map[studio.bank.index.map]); } static void initRunMode() @@ -1074,14 +1083,18 @@ void showDialog(const char** text, s32 rows, DialogCallback callback, void* data static void initModules() { - for(s32 i = 0; i < TIC_BANKS; i++) - initCode(studio.code[i], studio.tic, &studio.tic->cart.banks[i].code); + tic_mem* tic = studio.tic; + + for(s32 i = 0; i < TIC_BANKS; i++) + { + initCode(studio.editor.code[i], studio.tic, &tic->cart.banks[i].code); + initSprite(studio.editor.sprite[i], studio.tic, &tic->cart.banks[i].tiles); + initMap(studio.editor.map[i], studio.tic, &tic->cart.banks[i].map); + initSfx(studio.editor.sfx[i], studio.tic, &tic->cart.banks[i].sfx); + initMusic(studio.editor.music[i], studio.tic, &tic->cart.banks[i].music); + } - initSprite(studio.sprite, studio.tic); - initMap(studio.map, studio.tic); initWorldMap(); - initSfx(studio.sfx, studio.tic); - initMusic(studio.music, studio.tic); } static void updateHash() @@ -1781,7 +1794,7 @@ static bool processShortcuts(SDL_KeyboardEvent* event) case SDLK_ESCAPE: case SDLK_AC_BACK: { - Code* code = studio.code[studio.bank.index.code]; + Code* code = studio.editor.code[studio.bank.index.code]; if(studio.mode == TIC_CODE_MODE && code->mode != TEXT_EDIT_MODE) { @@ -1919,8 +1932,8 @@ SDL_Event* pollEvent() #endif { - Code* code = studio.code[studio.bank.index.code]; - studio.console->codeLiveReload.reload(studio.console, code->data); + Code* code = studio.editor.code[studio.bank.index.code]; + studio.console->codeLiveReload.reload(studio.console, code->src); if(studio.console->codeLiveReload.active && code->update) code->update(code); } @@ -2083,12 +2096,18 @@ static void blitTexture() overlap = tic->api.overlap; break; case TIC_SPRITE_MODE: - overlap = studio.sprite->overlap; - data = studio.sprite; + { + Sprite* sprite = studio.editor.sprite[studio.bank.index.sprites]; + overlap = sprite->overlap; + data = sprite; + } break; case TIC_MAP_MODE: - overlap = studio.map->overlap; - data = studio.map; + { + Map* map = studio.editor.map[studio.bank.index.map]; + overlap = map->overlap; + data = map; + } break; default: break; @@ -2268,8 +2287,8 @@ static void renderStudio() music = &studio.tic->config.bank0.music; break; default: - sfx = getBankSfx(); - music = getBankMusic(); + sfx = &studio.tic->cart.banks[studio.bank.index.sfx].sfx; + music = &studio.tic->cart.banks[studio.bank.index.music].music; } studio.tic->api.tick_start(studio.tic, sfx, music); @@ -2282,15 +2301,36 @@ static void renderStudio() case TIC_RUN_MODE: studio.run->tick(studio.run); break; case TIC_CODE_MODE: { - Code* code = studio.code[studio.bank.index.code]; + Code* code = studio.editor.code[studio.bank.index.code]; code->tick(code); } break; - case TIC_SPRITE_MODE: studio.sprite->tick(studio.sprite); break; - case TIC_MAP_MODE: studio.map->tick(studio.map); break; + case TIC_SPRITE_MODE: + { + Sprite* sprite = studio.editor.sprite[studio.bank.index.sprites]; + sprite->tick(sprite); + } + break; + case TIC_MAP_MODE: + { + Map* map = studio.editor.map[studio.bank.index.map]; + map->tick(map); + } + break; + case TIC_SFX_MODE: + { + Sfx* sfx = studio.editor.sfx[studio.bank.index.sfx]; + sfx->tick(sfx); + } + break; + case TIC_MUSIC_MODE: + { + Music* music = studio.editor.music[studio.bank.index.music]; + music->tick(music); + } + break; + case TIC_WORLD_MODE: studio.world->tick(studio.world); break; - case TIC_SFX_MODE: studio.sfx->tick(studio.sfx); break; - case TIC_MUSIC_MODE: studio.music->tick(studio.music); break; case TIC_KEYMAP_MODE: studio.keymap->tick(studio.keymap); break; case TIC_DIALOG_MODE: studio.dialog->tick(studio.dialog); break; case TIC_MENU_MODE: studio.menu->tick(studio.menu); break; @@ -2481,7 +2521,7 @@ static void updateSystemFont() void studioConfigChanged() { - Code* code = studio.code[studio.bank.index.code]; + Code* code = studio.editor.code[studio.bank.index.code]; if(code->update) code->update(code); @@ -2567,21 +2607,23 @@ static void onFSInitialized(FileSystem* fs) { for(s32 i = 0; i < TIC_BANKS; i++) - studio.code[i] = SDL_malloc(sizeof(Code)); + { + studio.editor.code[i] = SDL_malloc(sizeof(Code)); + studio.editor.sprite[i] = SDL_malloc(sizeof(Sprite)); + studio.editor.map[i] = SDL_malloc(sizeof(Map)); + studio.editor.sfx[i] = SDL_malloc(sizeof(Sfx)); + studio.editor.music[i] = SDL_malloc(sizeof(Music)); + } - studio.start = SDL_malloc(sizeof(Start)); - studio.console = SDL_malloc(sizeof(Console)); - studio.run = SDL_malloc(sizeof(Run)); - 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.world = SDL_malloc(sizeof(World)); + 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); @@ -2677,16 +2719,18 @@ s32 main(s32 argc, char **argv) { for(s32 i = 0; i < TIC_BANKS; i++) - SDL_free(studio.code[i]); + { + SDL_free(studio.editor.code[i]); + SDL_free(studio.editor.sprite[i]); + SDL_free(studio.editor.map[i]); + SDL_free(studio.editor.sfx[i]); + SDL_free(studio.editor.music[i]); + } SDL_free(studio.start); SDL_free(studio.console); SDL_free(studio.run); - 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); diff --git a/src/studio.h b/src/studio.h index 1115995..67288b9 100644 --- a/src/studio.h +++ b/src/studio.h @@ -200,8 +200,5 @@ void gotoSurf(); void exitFromGameMenu(); void runProject(); -tic_tiles* getBankTiles(); -tic_tiles* getBankSprites(); -tic_map* getBankMap(); -tic_sfx* getBankSfx(); -tic_music* getBankMusic(); +tic_tiles* getBankTiles(); +tic_map* getBankMap();