Add font width/height to the config #568

This commit is contained in:
BADIM-PC\Vadim 2018-03-12 15:33:18 +03:00
parent fbf9daa68d
commit 03efd90518
22 changed files with 558 additions and 435 deletions

File diff suppressed because one or more lines are too long

Binary file not shown.

View File

@ -27,16 +27,19 @@
#define TEXT_CURSOR_DELAY (TIC_FRAMERATE / 2) #define TEXT_CURSOR_DELAY (TIC_FRAMERATE / 2)
#define TEXT_CURSOR_BLINK_PERIOD TIC_FRAMERATE #define TEXT_CURSOR_BLINK_PERIOD TIC_FRAMERATE
#define TEXT_BUFFER_WIDTH STUDIO_TEXT_BUFFER_WIDTH
#define TEXT_BUFFER_HEIGHT ((TIC80_HEIGHT - TOOLBAR_SIZE - STUDIO_TEXT_HEIGHT) / STUDIO_TEXT_HEIGHT) static inline s32 TextBufferHeight(const tic_mem* tic)
{
return ((TIC80_HEIGHT - TOOLBAR_SIZE - TextHeight(tic)) / TextHeight(tic));
}
struct OutlineItem struct OutlineItem
{ {
char name[STUDIO_TEXT_BUFFER_WIDTH]; char name[TIC80_WIDTH];
char* pos; char* pos;
}; };
#define OUTLINE_SIZE ((TIC80_HEIGHT - TOOLBAR_SIZE*2)/TIC_FONT_HEIGHT) #define OUTLINE_SIZE (TIC80_HEIGHT)
#define OUTLINE_ITEMS_SIZE (OUTLINE_SIZE * sizeof(OutlineItem)) #define OUTLINE_ITEMS_SIZE (OUTLINE_SIZE * sizeof(OutlineItem))
static void history(Code* code) static void history(Code* code)
@ -47,18 +50,23 @@ static void history(Code* code)
static void drawStatus(Code* code) static void drawStatus(Code* code)
{ {
const s32 Height = TIC_FONT_HEIGHT + 1; tic_mem* tic = code->tic;
code->tic->api.rect(code->tic, 0, TIC80_HEIGHT - Height, TIC80_WIDTH, Height, (tic_color_white)); const s32 Height = tic->font.height + 1;
code->tic->api.fixed_text(code->tic, code->status, 0, TIC80_HEIGHT - TIC_FONT_HEIGHT, getConfig()->theme.code.bg);
tic->api.rect(tic, 0, TIC80_HEIGHT - Height, TIC80_WIDTH, Height, (tic_color_white));
tic->api.fixed_text(tic, code->status.left, 0, TIC80_HEIGHT - tic->font.height, getConfig()->theme.code.bg);
tic->api.fixed_text(tic, code->status.right, TIC80_WIDTH - (strlen(code->status.right) * tic->font.width),
TIC80_HEIGHT - tic->font.height, getConfig()->theme.code.bg);
} }
static void drawCursor(Code* code, s32 x, s32 y, char symbol) static void drawCursor(Code* code, s32 x, s32 y, char symbol)
{ {
tic_mem* tic = code->tic;
bool inverse = code->cursor.delay || code->tickCounter % TEXT_CURSOR_BLINK_PERIOD < TEXT_CURSOR_BLINK_PERIOD / 2; bool inverse = code->cursor.delay || code->tickCounter % TEXT_CURSOR_BLINK_PERIOD < TEXT_CURSOR_BLINK_PERIOD / 2;
if(inverse) if(inverse)
{ {
code->tic->api.rect(code->tic, x-1, y-1, TIC_FONT_WIDTH+1, TIC_FONT_HEIGHT+1, getConfig()->theme.code.cursor); code->tic->api.rect(code->tic, x-1, y-1, tic->font.width+1, tic->font.height+1, getConfig()->theme.code.cursor);
if(symbol) if(symbol)
code->tic->api.draw_char(code->tic, symbol, x, y, getConfig()->theme.code.bg); code->tic->api.draw_char(code->tic, symbol, x, y, getConfig()->theme.code.bg);
@ -67,9 +75,10 @@ static void drawCursor(Code* code, s32 x, s32 y, char symbol)
static void drawCode(Code* code, bool withCursor) static void drawCode(Code* code, bool withCursor)
{ {
s32 xStart = code->rect.x - code->scroll.x * STUDIO_TEXT_WIDTH; tic_mem* tic = code->tic;
s32 xStart = code->rect.x - code->scroll.x * TextWidth(tic);
s32 x = xStart; s32 x = xStart;
s32 y = code->rect.y - code->scroll.y * STUDIO_TEXT_HEIGHT; s32 y = code->rect.y - code->scroll.y * TextHeight(tic);
char* pointer = code->src; char* pointer = code->src;
u8* colorPointer = code->colorBuffer; u8* colorPointer = code->colorBuffer;
@ -83,10 +92,10 @@ static void drawCode(Code* code, bool withCursor)
{ {
char symbol = *pointer; char symbol = *pointer;
if(x >= -TIC_FONT_WIDTH && x < TIC80_WIDTH && y >= -TIC_FONT_HEIGHT && y < TIC80_HEIGHT ) if(x >= -tic->font.width && x < TIC80_WIDTH && y >= -tic->font.height && y < TIC80_HEIGHT )
{ {
if(code->cursor.selection && pointer >= selection.start && pointer < selection.end) if(code->cursor.selection && pointer >= selection.start && pointer < selection.end)
code->tic->api.rect(code->tic, x-1, y-1, TIC_FONT_WIDTH+1, TIC_FONT_HEIGHT+1, getConfig()->theme.code.select); code->tic->api.rect(code->tic, x-1, y-1, tic->font.width+1, tic->font.height+1, getConfig()->theme.code.select);
else if(getConfig()->theme.code.shadow) else if(getConfig()->theme.code.shadow)
{ {
code->tic->api.draw_char(code->tic, symbol, x+1, y+1, 0); code->tic->api.draw_char(code->tic, symbol, x+1, y+1, 0);
@ -101,9 +110,9 @@ static void drawCode(Code* code, bool withCursor)
if(symbol == '\n') if(symbol == '\n')
{ {
x = xStart; x = xStart;
y += STUDIO_TEXT_HEIGHT; y += TextHeight(tic);
} }
else x += STUDIO_TEXT_WIDTH; else x += TextWidth(tic);
pointer++; pointer++;
colorPointer++; colorPointer++;
@ -159,34 +168,31 @@ static void removeInvalidChars(char* code)
static void updateEditor(Code* code) static void updateEditor(Code* code)
{ {
tic_mem* tic = code->tic;
s32 column = 0; s32 column = 0;
s32 line = 0; s32 line = 0;
getCursorPosition(code, &column, &line); getCursorPosition(code, &column, &line);
if(column < code->scroll.x) code->scroll.x = column; if(column < code->scroll.x) code->scroll.x = column;
else if(column >= code->scroll.x + TEXT_BUFFER_WIDTH) else if(column >= code->scroll.x + BufferWidth(tic))
code->scroll.x = column - TEXT_BUFFER_WIDTH + 1; code->scroll.x = column - BufferWidth(tic) + 1;
if(line < code->scroll.y) code->scroll.y = line; if(line < code->scroll.y) code->scroll.y = line;
else if(line >= code->scroll.y + TEXT_BUFFER_HEIGHT) else if(line >= code->scroll.y + TextBufferHeight(tic))
code->scroll.y = line - TEXT_BUFFER_HEIGHT + 1; code->scroll.y = line - TextBufferHeight(tic) + 1;
code->cursor.delay = TEXT_CURSOR_DELAY; code->cursor.delay = TEXT_CURSOR_DELAY;
// update status // update status
{ {
memset(code->status, ' ', sizeof code->status - 1);
char status[STUDIO_TEXT_BUFFER_WIDTH];
s32 count = getLinesCount(code); s32 count = getLinesCount(code);
sprintf(status, "line %i/%i col %i", line + 1, count + 1, column + 1); sprintf(code->status.left, "line %i/%i col %i", line + 1, count + 1, column + 1);
memcpy(code->status, status, strlen(status));
size_t codeLen = strlen(code->src); size_t codeLen = strlen(code->src);
sprintf(status, "%i/%i", (u32)codeLen, TIC_CODE_SIZE); sprintf(code->status.right, "%i/%i", (u32)codeLen, TIC_CODE_SIZE);
memset(code->src + 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));
} }
} }
@ -403,19 +409,21 @@ static void goCodeEnd(Code *code)
static void pageUp(Code* code) static void pageUp(Code* code)
{ {
tic_mem* tic = code->tic;
s32 column = 0; s32 column = 0;
s32 line = 0; s32 line = 0;
getCursorPosition(code, &column, &line); getCursorPosition(code, &column, &line);
setCursorPosition(code, column, line > TEXT_BUFFER_HEIGHT ? line - TEXT_BUFFER_HEIGHT : 0); setCursorPosition(code, column, line > TextBufferHeight(tic) ? line - TextBufferHeight(tic) : 0);
} }
static void pageDown(Code* code) static void pageDown(Code* code)
{ {
tic_mem* tic = code->tic;
s32 column = 0; s32 column = 0;
s32 line = 0; s32 line = 0;
getCursorPosition(code, &column, &line); getCursorPosition(code, &column, &line);
s32 lines = getLinesCount(code); s32 lines = getLinesCount(code);
setCursorPosition(code, column, line < lines - TEXT_BUFFER_HEIGHT ? line + TEXT_BUFFER_HEIGHT : lines); setCursorPosition(code, column, line < lines - TextBufferHeight(tic) ? line + TextBufferHeight(tic) : lines);
} }
static bool replaceSelection(Code* code) static bool replaceSelection(Code* code)
@ -726,10 +734,12 @@ static void normalizeScroll(Code* code)
static void centerScroll(Code* code) static void centerScroll(Code* code)
{ {
tic_mem* tic = code->tic;
s32 col, line; s32 col, line;
getCursorPosition(code, &col, &line); getCursorPosition(code, &col, &line);
code->scroll.x = col - TEXT_BUFFER_WIDTH / 2; code->scroll.x = col - BufferWidth(tic) / 2;
code->scroll.y = line - TEXT_BUFFER_HEIGHT / 2; code->scroll.y = line - TextBufferHeight(tic) / 2;
normalizeScroll(code); normalizeScroll(code);
} }
@ -772,8 +782,8 @@ static void initOutlineMode(Code* code)
tic_mem* tic = code->tic; tic_mem* tic = code->tic;
char buffer[STUDIO_TEXT_BUFFER_WIDTH] = {0}; char buffer[TIC80_WIDTH] = {0};
char filter[STUDIO_TEXT_BUFFER_WIDTH] = {0}; char filter[TIC80_WIDTH] = {0};
strncpy(filter, code->popup.text, sizeof(filter)); strncpy(filter, code->popup.text, sizeof(filter));
ticStrlwr(filter); ticStrlwr(filter);
@ -792,8 +802,8 @@ static void initOutlineMode(Code* code)
if(out < end) if(out < end)
{ {
out->pos = code->src + item->pos; out->pos = code->src + item->pos;
memset(out->name, 0, STUDIO_TEXT_BUFFER_WIDTH); memset(out->name, 0, TIC80_WIDTH);
memcpy(out->name, out->pos, MIN(item->size, STUDIO_TEXT_BUFFER_WIDTH-1)); memcpy(out->name, out->pos, MIN(item->size, TIC80_WIDTH-1));
if(*filter) if(*filter)
{ {
@ -966,8 +976,8 @@ static void processMouse(Code* code)
{ {
if(checkMouseDown(&code->rect, tic_mouse_right)) if(checkMouseDown(&code->rect, tic_mouse_right))
{ {
code->scroll.x = (code->scroll.start.x - getMouseX()) / STUDIO_TEXT_WIDTH; code->scroll.x = (code->scroll.start.x - getMouseX()) / TextWidth(tic);
code->scroll.y = (code->scroll.start.y - getMouseY()) / STUDIO_TEXT_HEIGHT; code->scroll.y = (code->scroll.start.y - getMouseY()) / TextHeight(tic);
normalizeScroll(code); normalizeScroll(code);
} }
@ -980,8 +990,8 @@ static void processMouse(Code* code)
s32 mx = getMouseX(); s32 mx = getMouseX();
s32 my = getMouseY(); s32 my = getMouseY();
s32 x = (mx - code->rect.x) / STUDIO_TEXT_WIDTH; s32 x = (mx - code->rect.x) / TextWidth(tic);
s32 y = (my - code->rect.y) / STUDIO_TEXT_HEIGHT; s32 y = (my - code->rect.y) / TextHeight(tic);
char* position = code->cursor.position; char* position = code->cursor.position;
setCursorPosition(code, x + code->scroll.x, y + code->scroll.y); setCursorPosition(code, x + code->scroll.x, y + code->scroll.y);
@ -1009,8 +1019,8 @@ static void processMouse(Code* code)
{ {
code->scroll.active = true; code->scroll.active = true;
code->scroll.start.x = getMouseX() + code->scroll.x * STUDIO_TEXT_WIDTH; code->scroll.start.x = getMouseX() + code->scroll.x * TextWidth(tic);
code->scroll.start.y = getMouseY() + code->scroll.y * STUDIO_TEXT_HEIGHT; code->scroll.start.y = getMouseY() + code->scroll.y * TextHeight(tic);
} }
} }
@ -1058,14 +1068,16 @@ static void textEditTick(Code* code)
static void drawPopupBar(Code* code, const char* title) static void drawPopupBar(Code* code, const char* title)
{ {
tic_mem* tic = code->tic;
enum {TextY = TOOLBAR_SIZE + 1}; enum {TextY = TOOLBAR_SIZE + 1};
code->tic->api.rect(code->tic, 0, TOOLBAR_SIZE, TIC80_WIDTH, TIC_FONT_HEIGHT + 1, (tic_color_blue)); code->tic->api.rect(code->tic, 0, TOOLBAR_SIZE, TIC80_WIDTH, tic->font.height + 1, (tic_color_blue));
code->tic->api.fixed_text(code->tic, title, 0, TextY, (tic_color_white)); code->tic->api.fixed_text(code->tic, title, 0, TextY, (tic_color_white));
code->tic->api.fixed_text(code->tic, code->popup.text, (s32)strlen(title)*TIC_FONT_WIDTH, TextY, (tic_color_white)); code->tic->api.fixed_text(code->tic, code->popup.text, (s32)strlen(title)*tic->font.width, TextY, (tic_color_white));
drawCursor(code, (s32)(strlen(title) + strlen(code->popup.text)) * TIC_FONT_WIDTH, TextY, ' '); drawCursor(code, (s32)(strlen(title) + strlen(code->popup.text)) * tic->font.width, TextY, ' ');
} }
static void updateFindCode(Code* code, char* pos) static void updateFindCode(Code* code, char* pos)
@ -1203,8 +1215,8 @@ static void textGoToTick(Code* code)
tic->api.clear(tic, getConfig()->theme.code.bg); tic->api.clear(tic, getConfig()->theme.code.bg);
if(code->jump.line >= 0) if(code->jump.line >= 0)
tic->api.rect(tic, 0, (code->jump.line - code->scroll.y) * (TIC_FONT_HEIGHT+1) + TOOLBAR_SIZE, tic->api.rect(tic, 0, (code->jump.line - code->scroll.y) * (tic->font.height+1) + TOOLBAR_SIZE,
TIC80_WIDTH, TIC_FONT_HEIGHT+2, getConfig()->theme.code.select); TIC80_WIDTH, tic->font.height+2, getConfig()->theme.code.select);
drawCode(code, false); drawCode(code, false);
drawPopupBar(code, " GOTO:"); drawPopupBar(code, " GOTO:");
@ -1213,12 +1225,14 @@ static void textGoToTick(Code* code)
static void drawOutlineBar(Code* code, s32 x, s32 y) static void drawOutlineBar(Code* code, s32 x, s32 y)
{ {
tic_mem* tic = code->tic;
tic_rect rect = {x, y, TIC80_WIDTH - x, TIC80_HEIGHT - y}; tic_rect rect = {x, y, TIC80_WIDTH - x, TIC80_HEIGHT - y};
if(checkMousePos(&rect)) if(checkMousePos(&rect))
{ {
s32 mx = getMouseY() - rect.y; s32 mx = getMouseY() - rect.y;
mx /= STUDIO_TEXT_HEIGHT; mx /= TextHeight(tic);
if(mx < OUTLINE_SIZE && code->outline.items[mx].pos) if(mx < OUTLINE_SIZE && code->outline.items[mx].pos)
{ {
@ -1244,13 +1258,13 @@ static void drawOutlineBar(Code* code, s32 x, s32 y)
if(ptr->pos) if(ptr->pos)
{ {
code->tic->api.rect(code->tic, rect.x - 1, rect.y + code->outline.index*STUDIO_TEXT_HEIGHT, code->tic->api.rect(code->tic, rect.x - 1, rect.y + code->outline.index*TextHeight(tic),
rect.w + 1, TIC_FONT_HEIGHT + 1, (tic_color_red)); rect.w + 1, tic->font.height + 1, (tic_color_red));
while(ptr->pos) while(ptr->pos)
{ {
code->tic->api.fixed_text(code->tic, ptr->name, x, y, (tic_color_white)); code->tic->api.fixed_text(code->tic, ptr->name, x, y, (tic_color_white));
ptr++; ptr++;
y += STUDIO_TEXT_HEIGHT; y += TextHeight(tic);
} }
} }
else code->tic->api.fixed_text(code->tic, "(empty)", x, y, (tic_color_white)); else code->tic->api.fixed_text(code->tic, "(empty)", x, y, (tic_color_white));
@ -1258,6 +1272,8 @@ static void drawOutlineBar(Code* code, s32 x, s32 y)
static void textOutlineTick(Code* code) static void textOutlineTick(Code* code)
{ {
tic_mem* tic = code->tic;
if(keyWasPressed(tic_key_up)) if(keyWasPressed(tic_key_up))
{ {
if(code->outline.index > 0) if(code->outline.index > 0)
@ -1305,7 +1321,7 @@ static void textOutlineTick(Code* code)
drawCode(code, false); drawCode(code, false);
drawPopupBar(code, " FUNC:"); drawPopupBar(code, " FUNC:");
drawStatus(code); drawStatus(code);
drawOutlineBar(code, TIC80_WIDTH - 12 * TIC_FONT_WIDTH, 2*(TIC_FONT_HEIGHT+1)); drawOutlineBar(code, TIC80_WIDTH - 12 * tic->font.width, 2*(tic->font.height+1));
} }
static void drawCodeToolbar(Code* code) static void drawCodeToolbar(Code* code)
@ -1455,7 +1471,7 @@ void initCode(Code* code, tic_mem* tic, tic_code* src)
.tick = tick, .tick = tick,
.escape = escape, .escape = escape,
.cursor = {{src->data, NULL, 0}, NULL, 0}, .cursor = {{src->data, NULL, 0}, NULL, 0},
.rect = {0, TOOLBAR_SIZE + 1, TIC80_WIDTH, TIC80_HEIGHT - TOOLBAR_SIZE - TIC_FONT_HEIGHT - 1}, .rect = {0, TOOLBAR_SIZE + 1, TIC80_WIDTH, TIC80_HEIGHT - TOOLBAR_SIZE - tic->font.height - 1},
.scroll = {0, 0, {0, 0}, false}, .scroll = {0, 0, {0, 0}, false},
.tickCounter = 0, .tickCounter = 0,
.history = NULL, .history = NULL,

View File

@ -62,7 +62,11 @@ struct Code
u8 colorBuffer[TIC_CODE_SIZE]; u8 colorBuffer[TIC_CODE_SIZE];
char status[STUDIO_TEXT_BUFFER_WIDTH+1]; struct
{
char left[TIC80_WIDTH];
char right[TIC80_WIDTH];
} status;
u32 tickCounter; u32 tickCounter;
@ -80,7 +84,7 @@ struct Code
struct struct
{ {
char text[STUDIO_TEXT_BUFFER_WIDTH - sizeof "FIND:"]; char text[TIC80_WIDTH];
char* prevPos; char* prevPos;
char* prevSel; char* prevSel;

View File

@ -196,6 +196,34 @@ static void readCodeTheme(Config* config, lua_State* lua)
lua_pop(lua, 1); lua_pop(lua, 1);
} }
static void readFontTheme(Config* config, lua_State* lua)
{
lua_getfield(lua, -1, "FONT");
if(lua_type(lua, -1) == LUA_TTABLE)
{
{
lua_getfield(lua, -1, "WIDTH");
if(lua_isinteger(lua, -1))
config->data.theme.font.width = lua_tointeger(lua, -1);
lua_pop(lua, 1);
}
{
lua_getfield(lua, -1, "HEIGHT");
if(lua_isinteger(lua, -1))
config->data.theme.font.height = lua_tointeger(lua, -1);
lua_pop(lua, 1);
}
}
lua_pop(lua, 1);
}
static void readGamepadTheme(Config* config, lua_State* lua) static void readGamepadTheme(Config* config, lua_State* lua)
{ {
lua_getfield(lua, -1, "GAMEPAD"); lua_getfield(lua, -1, "GAMEPAD");
@ -229,6 +257,7 @@ static void readTheme(Config* config, lua_State* lua)
readCursorTheme(config, lua); readCursorTheme(config, lua);
readCodeTheme(config, lua); readCodeTheme(config, lua);
readGamepadTheme(config, lua); readGamepadTheme(config, lua);
readFontTheme(config, lua);
} }
lua_pop(lua, 1); lua_pop(lua, 1);

View File

@ -45,10 +45,7 @@
#define CONSOLE_ERROR_TEXT_COLOR ((tic_color_red)) #define CONSOLE_ERROR_TEXT_COLOR ((tic_color_red))
#define CONSOLE_CURSOR_BLINK_PERIOD (TIC_FRAMERATE) #define CONSOLE_CURSOR_BLINK_PERIOD (TIC_FRAMERATE)
#define CONSOLE_CURSOR_DELAY (TIC_FRAMERATE / 2) #define CONSOLE_CURSOR_DELAY (TIC_FRAMERATE / 2)
#define CONSOLE_BUFFER_WIDTH (STUDIO_TEXT_BUFFER_WIDTH)
#define CONSOLE_BUFFER_HEIGHT (STUDIO_TEXT_BUFFER_HEIGHT)
#define CONSOLE_BUFFER_SCREENS 64 #define CONSOLE_BUFFER_SCREENS 64
#define CONSOLE_BUFFER_SIZE (CONSOLE_BUFFER_WIDTH * CONSOLE_BUFFER_HEIGHT * CONSOLE_BUFFER_SCREENS)
typedef enum typedef enum
{ {
@ -109,7 +106,11 @@ static const char DefaultJSTicPath[] = TIC_LOCAL "default_js.tic";
#if defined(TIC_BUILD_WITH_WREN) #if defined(TIC_BUILD_WITH_WREN)
static const char DefaultWrenTicPath[] = TIC_LOCAL "default_wren.tic"; static const char DefaultWrenTicPath[] = TIC_LOCAL "default_wren.tic";
#endif #endif
static inline s32 BufferSize(const tic_mem* tic)
{
return BufferWidth(tic) * BufferHeight(tic) * CONSOLE_BUFFER_SCREENS;
}
static const char* getName(const char* name, const char* ext) static const char* getName(const char* name, const char* ext)
{ {
@ -131,29 +132,33 @@ static const char* getCartName(const char* name)
return getName(name, CART_EXT); return getName(name, CART_EXT);
} }
static void scrollBuffer(char* buffer) static void scrollBuffer(tic_mem* tic, char* buffer)
{ {
memmove(buffer, buffer + CONSOLE_BUFFER_WIDTH, CONSOLE_BUFFER_SIZE - CONSOLE_BUFFER_WIDTH); memmove(buffer, buffer + BufferWidth(tic), BufferSize(tic) - BufferWidth(tic));
memset(buffer + CONSOLE_BUFFER_SIZE - CONSOLE_BUFFER_WIDTH, 0, CONSOLE_BUFFER_WIDTH); memset(buffer + BufferSize(tic) - BufferWidth(tic), 0, BufferWidth(tic));
} }
static void scrollConsole(Console* console) static void scrollConsole(Console* console)
{ {
while(console->cursor.y >= CONSOLE_BUFFER_HEIGHT * CONSOLE_BUFFER_SCREENS) tic_mem* tic = console->tic;
while(console->cursor.y >= BufferHeight(tic) * CONSOLE_BUFFER_SCREENS)
{ {
scrollBuffer(console->buffer); scrollBuffer(tic, console->buffer);
scrollBuffer((char*)console->colorBuffer); scrollBuffer(tic, (char*)console->colorBuffer);
console->cursor.y--; console->cursor.y--;
} }
s32 minScroll = console->cursor.y - CONSOLE_BUFFER_HEIGHT + 1; s32 minScroll = console->cursor.y - BufferHeight(tic) + 1;
if(console->scroll.pos < minScroll) if(console->scroll.pos < minScroll)
console->scroll.pos = minScroll; console->scroll.pos = minScroll;
} }
static void consolePrint(Console* console, const char* text, u8 color) static void consolePrint(Console* console, const char* text, u8 color)
{ {
tic_mem* tic = console->tic;
printf("%s", text); printf("%s", text);
const char* textPointer = text; const char* textPointer = text;
@ -172,13 +177,13 @@ static void consolePrint(Console* console, const char* text, u8 color)
} }
else else
{ {
s32 offset = console->cursor.x + console->cursor.y * CONSOLE_BUFFER_WIDTH; s32 offset = console->cursor.x + console->cursor.y * BufferWidth(tic);
*(console->buffer + offset) = symbol; *(console->buffer + offset) = symbol;
*(console->colorBuffer + offset) = color; *(console->colorBuffer + offset) = color;
console->cursor.x++; console->cursor.x++;
if(console->cursor.x >= CONSOLE_BUFFER_WIDTH) if(console->cursor.x >= BufferWidth(tic))
{ {
console->cursor.x = 0; console->cursor.x = 0;
console->cursor.y++; console->cursor.y++;
@ -228,20 +233,22 @@ static void commandDone(Console* console)
static void drawCursor(Console* console, s32 x, s32 y, u8 symbol) static void drawCursor(Console* console, s32 x, s32 y, u8 symbol)
{ {
tic_mem* tic = console->tic;
bool inverse = console->cursor.delay || console->tickCounter % CONSOLE_CURSOR_BLINK_PERIOD < CONSOLE_CURSOR_BLINK_PERIOD / 2; bool inverse = console->cursor.delay || console->tickCounter % CONSOLE_CURSOR_BLINK_PERIOD < CONSOLE_CURSOR_BLINK_PERIOD / 2;
if(inverse) if(inverse)
console->tic->api.rect(console->tic, x-1, y-1, TIC_FONT_WIDTH+1, TIC_FONT_HEIGHT+1, CONSOLE_CURSOR_COLOR); console->tic->api.rect(console->tic, x-1, y-1, tic->font.width+1, tic->font.height+1, CONSOLE_CURSOR_COLOR);
console->tic->api.draw_char(console->tic, symbol, x, y, inverse ? TIC_COLOR_BG : CONSOLE_FRONT_TEXT_COLOR); console->tic->api.draw_char(console->tic, symbol, x, y, inverse ? TIC_COLOR_BG : CONSOLE_FRONT_TEXT_COLOR);
} }
static void drawConsoleText(Console* console) static void drawConsoleText(Console* console)
{ {
char* pointer = console->buffer + console->scroll.pos * CONSOLE_BUFFER_WIDTH; tic_mem* tic = console->tic;
u8* colorPointer = console->colorBuffer + console->scroll.pos * CONSOLE_BUFFER_WIDTH; char* pointer = console->buffer + console->scroll.pos * BufferWidth(tic);
u8* colorPointer = console->colorBuffer + console->scroll.pos * BufferWidth(tic);
const char* end = console->buffer + CONSOLE_BUFFER_SIZE; const char* end = console->buffer + BufferSize(tic);
s32 x = 0; s32 x = 0;
s32 y = 0; s32 y = 0;
@ -251,9 +258,9 @@ static void drawConsoleText(Console* console)
u8 color = *colorPointer++; u8 color = *colorPointer++;
if(symbol) if(symbol)
console->tic->api.draw_char(console->tic, symbol, x * STUDIO_TEXT_WIDTH, y * STUDIO_TEXT_HEIGHT, color); console->tic->api.draw_char(console->tic, symbol, x * TextWidth(tic), y * TextHeight(tic), color);
if(++x == CONSOLE_BUFFER_WIDTH) if(++x == BufferWidth(tic))
{ {
y++; y++;
x = 0; x = 0;
@ -263,8 +270,9 @@ static void drawConsoleText(Console* console)
static void drawConsoleInputText(Console* console) static void drawConsoleInputText(Console* console)
{ {
s32 x = console->cursor.x * STUDIO_TEXT_WIDTH; tic_mem* tic = console->tic;
s32 y = (console->cursor.y - console->scroll.pos) * STUDIO_TEXT_HEIGHT; s32 x = console->cursor.x * TextWidth(tic);
s32 y = (console->cursor.y - console->scroll.pos) * TextHeight(tic);
const char* pointer = console->inputBuffer; const char* pointer = console->inputBuffer;
const char* end = pointer + strlen(console->inputBuffer); const char* end = pointer + strlen(console->inputBuffer);
@ -281,10 +289,10 @@ static void drawConsoleInputText(Console* console)
index++; index++;
x += STUDIO_TEXT_WIDTH; x += TextWidth(tic);
if(x == (CONSOLE_BUFFER_WIDTH * STUDIO_TEXT_WIDTH)) if(x == (BufferWidth(tic) * TextWidth(tic)))
{ {
y += STUDIO_TEXT_HEIGHT; y += TextHeight(tic);
x = 0; x = 0;
} }
@ -1315,8 +1323,9 @@ static void onConsoleFolderCommand(Console* console, const char* param)
static void onConsoleClsCommand(Console* console, const char* param) static void onConsoleClsCommand(Console* console, const char* param)
{ {
memset(console->buffer, 0, CONSOLE_BUFFER_SIZE); tic_mem* tic = console->tic;
memset(console->colorBuffer, TIC_COLOR_BG, CONSOLE_BUFFER_SIZE); memset(console->buffer, 0, BufferSize(tic));
memset(console->colorBuffer, TIC_COLOR_BG, BufferSize(tic));
console->scroll.pos = 0; console->scroll.pos = 0;
console->cursor.x = console->cursor.y = 0; console->cursor.x = console->cursor.y = 0;
@ -2240,6 +2249,8 @@ static void onConsoleDelCommand(Console* console, const char* param)
static void printTable(Console* console, const char* text) static void printTable(Console* console, const char* text)
{ {
tic_mem* tic = console->tic;
printf("%s", text); printf("%s", text);
const char* textPointer = text; const char* textPointer = text;
@ -2258,7 +2269,7 @@ static void printTable(Console* console, const char* text)
} }
else else
{ {
s32 offset = console->cursor.x + console->cursor.y * CONSOLE_BUFFER_WIDTH; s32 offset = console->cursor.x + console->cursor.y * BufferWidth(tic);
*(console->buffer + offset) = symbol; *(console->buffer + offset) = symbol;
u8 color = 0; u8 color = 0;
@ -2278,7 +2289,7 @@ static void printTable(Console* console, const char* text)
console->cursor.x++; console->cursor.x++;
if(console->cursor.x >= CONSOLE_BUFFER_WIDTH) if(console->cursor.x >= BufferWidth(tic))
{ {
console->cursor.x = 0; console->cursor.x = 0;
console->cursor.y++; console->cursor.y++;
@ -2290,7 +2301,7 @@ static void printTable(Console* console, const char* text)
static void printRamInfo(Console* console, s32 addr, const char* name, s32 size) static void printRamInfo(Console* console, s32 addr, const char* name, s32 size)
{ {
char buf[STUDIO_TEXT_BUFFER_WIDTH]; char buf[TIC80_WIDTH];
sprintf(buf, "\n| %05X | %-17s | %-5i |", addr, name, size); sprintf(buf, "\n| %05X | %-17s | %-5i |", addr, name, size);
printTable(console, buf); printTable(console, buf);
} }
@ -3023,8 +3034,8 @@ static bool cmdInjectMap(Console* console, const char* param, const char* name)
void initConsole(Console* console, tic_mem* tic, FileSystem* fs, Config* config, s32 argc, char **argv) void initConsole(Console* console, tic_mem* tic, FileSystem* fs, Config* config, s32 argc, char **argv)
{ {
if(!console->buffer) console->buffer = malloc(CONSOLE_BUFFER_SIZE); if(!console->buffer) console->buffer = malloc(BufferSize(tic));
if(!console->colorBuffer) console->colorBuffer = malloc(CONSOLE_BUFFER_SIZE); if(!console->colorBuffer) console->colorBuffer = malloc(BufferSize(tic));
if(!console->embed.file) console->embed.file = malloc(sizeof(tic_cartridge)); if(!console->embed.file) console->embed.file = malloc(sizeof(tic_cartridge));
*console = (Console) *console = (Console)
@ -3076,8 +3087,8 @@ void initConsole(Console* console, tic_mem* tic, FileSystem* fs, Config* config,
.crtMonitor = false, .crtMonitor = false,
}; };
memset(console->buffer, 0, CONSOLE_BUFFER_SIZE); memset(console->buffer, 0, BufferSize(tic));
memset(console->colorBuffer, TIC_COLOR_BG, CONSOLE_BUFFER_SIZE); memset(console->colorBuffer, TIC_COLOR_BG, BufferSize(tic));
memset(console->codeLiveReload.fileName, 0, FILENAME_MAX); memset(console->codeLiveReload.fileName, 0, FILENAME_MAX);

View File

@ -78,7 +78,7 @@ struct Console
char* buffer; char* buffer;
u8* colorBuffer; u8* colorBuffer;
char inputBuffer[STUDIO_TEXT_BUFFER_WIDTH * STUDIO_TEXT_BUFFER_HEIGHT]; char inputBuffer[TIC80_WIDTH];
size_t inputPosition; size_t inputPosition;
tic_mem* tic; tic_mem* tic;

View File

@ -57,7 +57,7 @@ static void drawButton(Dialog* dlg, const char* label, s32 x, s32 y, u8 color, u
tic->api.rect(tic, rect.x, rect.y, rect.w, rect.h, (tic_color_white)); tic->api.rect(tic, rect.x, rect.y, rect.w, rect.h, (tic_color_white));
} }
s32 size = tic->api.text(tic, label, 0, -TIC_FONT_HEIGHT, 0); s32 size = tic->api.text(tic, label, 0, -tic->font.height, 0);
tic->api.text(tic, label, rect.x + (BtnWidth - size+1)/2, rect.y + (down?3:2), over ? overColor : color); tic->api.text(tic, label, rect.x + (BtnWidth - size+1)/2, rect.y + (down?3:2), over ? overColor : color);
if(dlg->focus == id) if(dlg->focus == id)
@ -162,7 +162,7 @@ static void drawDialog(Dialog* dlg)
{ {
static const char Label[] = "WARNING!"; static const char Label[] = "WARNING!";
s32 size = tic->api.text(tic, Label, 0, -TIC_FONT_HEIGHT, 0); s32 size = tic->api.text(tic, Label, 0, -tic->font.height, 0);
tic->api.text(tic, Label, rect.x + (Width - size)/2, rect.y-(TOOLBAR_SIZE-2), (tic_color_gray)); tic->api.text(tic, Label, rect.x + (Width - size)/2, rect.y-(TOOLBAR_SIZE-2), (tic_color_gray));
} }
@ -174,10 +174,10 @@ static void drawDialog(Dialog* dlg)
{ {
for(s32 i = 0; i < dlg->rows; i++) for(s32 i = 0; i < dlg->rows; i++)
{ {
s32 size = tic->api.text(tic, dlg->text[i], 0, -TIC_FONT_HEIGHT, 0); s32 size = tic->api.text(tic, dlg->text[i], 0, -tic->font.height, 0);
s32 x = rect.x + (Width - size)/2; s32 x = rect.x + (Width - size)/2;
s32 y = rect.y + (TIC_FONT_HEIGHT+1)*(i+1); s32 y = rect.y + (tic->font.height+1)*(i+1);
tic->api.text(tic, dlg->text[i], x, y+1, (tic_color_black)); tic->api.text(tic, dlg->text[i], x, y+1, (tic_color_black));
tic->api.text(tic, dlg->text[i], x, y, (tic_color_white)); tic->api.text(tic, dlg->text[i], x, y, (tic_color_white));
} }

View File

@ -334,9 +334,11 @@ static void drawTileIndex(Map* map, s32 x, s32 y)
static void drawMapToolbar(Map* map, s32 x, s32 y) static void drawMapToolbar(Map* map, s32 x, s32 y)
{ {
tic_mem* tic = map->tic;
map->tic->api.rect(map->tic, 0, 0, TIC80_WIDTH, TOOLBAR_SIZE, (tic_color_white)); map->tic->api.rect(map->tic, 0, 0, TIC80_WIDTH, TOOLBAR_SIZE, (tic_color_white));
drawTileIndex(map, TIC80_WIDTH/2 - TIC_FONT_WIDTH, y); drawTileIndex(map, TIC80_WIDTH/2 - tic->font.width, y);
x = drawSheetButton(map, TIC80_WIDTH, 0); x = drawSheetButton(map, TIC80_WIDTH, 0);
x = drawFillButton(map, x, 0); x = drawFillButton(map, x, 0);
@ -417,6 +419,8 @@ static void drawSheetOvr(Map* map, s32 x, s32 y)
static void drawCursorPos(Map* map, s32 x, s32 y) static void drawCursorPos(Map* map, s32 x, s32 y)
{ {
tic_mem* tic = map->tic;
char pos[] = "999:999"; char pos[] = "999:999";
s32 tx = 0, ty = 0; s32 tx = 0, ty = 0;
@ -429,10 +433,10 @@ static void drawCursorPos(Map* map, s32 x, s32 y)
s32 px = x + (TIC_SPRITESIZE + 3); s32 px = x + (TIC_SPRITESIZE + 3);
if(px + width >= TIC80_WIDTH) px = x - (width + 2); if(px + width >= TIC80_WIDTH) px = x - (width + 2);
s32 py = y - (TIC_FONT_HEIGHT + 2); s32 py = y - (tic->font.height + 2);
if(py <= TOOLBAR_SIZE) py = y + (TIC_SPRITESIZE + 3); if(py <= TOOLBAR_SIZE) py = y + (TIC_SPRITESIZE + 3);
map->tic->api.rect(map->tic, px - 1, py - 1, width + 1, TIC_FONT_HEIGHT + 1, (tic_color_white)); map->tic->api.rect(map->tic, px - 1, py - 1, width + 1, tic->font.height + 1, (tic_color_white));
map->tic->api.text(map->tic, pos, px, py, (tic_color_light_blue)); map->tic->api.text(map->tic, pos, px, py, (tic_color_light_blue));
} }
@ -1060,13 +1064,14 @@ static void processKeyboard(Map* map)
static void tick(Map* map) static void tick(Map* map)
{ {
tic_mem* tic = map->tic;
map->tickCounter++; map->tickCounter++;
processKeyboard(map); processKeyboard(map);
map->tic->api.clear(map->tic, TIC_COLOR_BG); map->tic->api.clear(map->tic, TIC_COLOR_BG);
drawSheet(map, TIC80_WIDTH - TIC_SPRITESHEET_SIZE - 1, TOOLBAR_SIZE); drawSheet(map, TIC80_WIDTH - TIC_SPRITESHEET_SIZE - 1, TOOLBAR_SIZE);
drawMapToolbar(map, TIC80_WIDTH - 9*TIC_FONT_WIDTH, 1); drawMapToolbar(map, TIC80_WIDTH - 9*tic->font.width, 1);
drawToolbar(map->tic, TIC_COLOR_BG, false); drawToolbar(map->tic, TIC_COLOR_BG, false);
} }

View File

@ -118,7 +118,7 @@ static void drawDialog(Menu* menu)
{ {
static const char Label[] = "GAME MENU"; static const char Label[] = "GAME MENU";
s32 size = tic->api.text(tic, Label, 0, -TIC_FONT_HEIGHT, 0); s32 size = tic->api.text(tic, Label, 0, -tic->font.height, 0);
tic->api.text(tic, Label, rect.x + (DIALOG_WIDTH - size)/2, rect.y-(TOOLBAR_SIZE-2), (tic_color_gray)); tic->api.text(tic, Label, rect.x + (DIALOG_WIDTH - size)/2, rect.y-(TOOLBAR_SIZE-2), (tic_color_gray));
} }
@ -252,7 +252,7 @@ static void drawGamepadMenu(Menu* menu)
static const char Label[] = "BACK"; static const char Label[] = "BACK";
tic_rect rect = {dlgRect.x + 25, dlgRect.y + 49, (sizeof(Label)-1)*TIC_FONT_WIDTH, TIC_FONT_HEIGHT}; tic_rect rect = {dlgRect.x + 25, dlgRect.y + 49, (sizeof(Label)-1)*tic->font.width, tic->font.height};
bool over = false; bool over = false;
bool down = false; bool down = false;
@ -320,7 +320,7 @@ static void drawMainMenu(Menu* menu)
{ {
if(!*Rows[i])continue; if(!*Rows[i])continue;
tic_rect label = {rect.x + 22, rect.y + (TIC_FONT_HEIGHT+1)*i + 16, 86, TIC_FONT_HEIGHT+1}; tic_rect label = {rect.x + 22, rect.y + (tic->font.height+1)*i + 16, 86, tic->font.height+1};
bool over = false; bool over = false;
bool down = false; bool down = false;

View File

@ -66,6 +66,7 @@ static void drawDownBorder(Music* music, s32 x, s32 y, s32 w, s32 h)
static void drawEditbox(Music* music, s32 x, s32 y, s32 value, void(*set)(Music*, s32, s32 channel), s32 channel) static void drawEditbox(Music* music, s32 x, s32 y, s32 value, void(*set)(Music*, s32, s32 channel), s32 channel)
{ {
tic_mem* tic = music->tic;
static const u8 LeftArrow[] = static const u8 LeftArrow[] =
{ {
0b00100000, 0b00100000,
@ -90,8 +91,12 @@ static void drawEditbox(Music* music, s32 x, s32 y, s32 value, void(*set)(Music*
0b00000000, 0b00000000,
}; };
enum{ArrowWidth = 4, ArrowHeight = 6};
x -= ArrowWidth + 2;
{ {
tic_rect rect = { x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT }; tic_rect rect = { x, y, ArrowWidth, ArrowHeight };
bool over = false; bool over = false;
bool down = false; bool down = false;
@ -111,9 +116,9 @@ static void drawEditbox(Music* music, s32 x, s32 y, s32 value, void(*set)(Music*
} }
{ {
x += TIC_FONT_WIDTH; x += ArrowWidth + 2;
tic_rect rect = { x-1, y-1, TIC_FONT_WIDTH*2+1, TIC_FONT_HEIGHT+1 }; tic_rect rect = { x-1, y-1, tic->font.width*2+1, tic->font.height+1 };
if (checkMousePos(&rect)) if (checkMousePos(&rect))
{ {
@ -125,7 +130,7 @@ static void drawEditbox(Music* music, s32 x, s32 y, s32 value, void(*set)(Music*
music->tracker.col = channel * CHANNEL_COLS; music->tracker.col = channel * CHANNEL_COLS;
s32 mx = getMouseX() - rect.x; s32 mx = getMouseX() - rect.x;
music->tracker.patternCol = mx / TIC_FONT_WIDTH; music->tracker.patternCol = mx / tic->font.width;
} }
} }
@ -134,7 +139,7 @@ static void drawEditbox(Music* music, s32 x, s32 y, s32 value, void(*set)(Music*
if(music->tracker.row == -1 && music->tracker.col / CHANNEL_COLS == channel) if(music->tracker.row == -1 && music->tracker.col / CHANNEL_COLS == channel)
{ {
music->tic->api.rect(music->tic, x - 1 + music->tracker.patternCol * TIC_FONT_WIDTH, y - 1, TIC_FONT_WIDTH + 1, TIC_FONT_HEIGHT + 1, (tic_color_red)); music->tic->api.rect(music->tic, x - 1 + music->tracker.patternCol * tic->font.width, y - 1, tic->font.width + 1, tic->font.height + 1, (tic_color_red));
} }
char val[] = "99"; char val[] = "99";
@ -143,9 +148,9 @@ static void drawEditbox(Music* music, s32 x, s32 y, s32 value, void(*set)(Music*
} }
{ {
x += 2*TIC_FONT_WIDTH; x += 2*tic->font.width;
tic_rect rect = { x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT }; tic_rect rect = { x, y, ArrowWidth, ArrowHeight };
bool over = false; bool over = false;
bool down = false; bool down = false;
@ -167,13 +172,14 @@ static void drawEditbox(Music* music, s32 x, s32 y, s32 value, void(*set)(Music*
static void drawSwitch(Music* music, s32 x, s32 y, const char* label, s32 value, void(*set)(Music*, s32, void* data), void* data) static void drawSwitch(Music* music, s32 x, s32 y, const char* label, s32 value, void(*set)(Music*, s32, void* data), void* data)
{ {
tic_mem* tic = music->tic;
static const u8 LeftArrow[] = static const u8 LeftArrow[] =
{ {
0b00010000, 0b00100000,
0b00110000, 0b01100000,
0b01110000, 0b11100000,
0b00110000, 0b01100000,
0b00010000, 0b00100000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
@ -181,23 +187,25 @@ static void drawSwitch(Music* music, s32 x, s32 y, const char* label, s32 value,
static const u8 RightArrow[] = static const u8 RightArrow[] =
{ {
0b01000000, 0b10000000,
0b01100000, 0b11000000,
0b01110000, 0b11100000,
0b01100000, 0b11000000,
0b01000000, 0b10000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
}; };
enum{ArrowWidth = 4, ArrowHeight = 6};
music->tic->api.text(music->tic, label, x, y+1, (tic_color_black)); music->tic->api.text(music->tic, label, x, y+1, (tic_color_black));
music->tic->api.text(music->tic, label, x, y, (tic_color_white)); music->tic->api.text(music->tic, label, x, y, (tic_color_white));
{ {
x += (s32)strlen(label)*TIC_FONT_WIDTH; x += (s32)strlen(label)*tic->font.width + 1;
tic_rect rect = { x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT }; tic_rect rect = { x, y, ArrowWidth, ArrowHeight};
bool over = false; bool over = false;
bool down = false; bool down = false;
@ -218,16 +226,17 @@ static void drawSwitch(Music* music, s32 x, s32 y, const char* label, s32 value,
} }
{ {
x += ArrowWidth;
char val[] = "999"; char val[] = "999";
sprintf(val, "%02i", value); sprintf(val, "%02i", value);
music->tic->api.fixed_text(music->tic, val, x + TIC_FONT_WIDTH, y+1, (tic_color_black)); music->tic->api.fixed_text(music->tic, val, x, y+1, (tic_color_black));
music->tic->api.fixed_text(music->tic, val, x += TIC_FONT_WIDTH, y, (tic_color_white)); music->tic->api.fixed_text(music->tic, val, x, y, (tic_color_white));
} }
{ {
x += (value > 99 ? 3 : 2)*TIC_FONT_WIDTH; x += (value > 99 ? 3 : 2)*tic->font.width;
tic_rect rect = { x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT }; tic_rect rect = { x, y, ArrowWidth, ArrowHeight};
bool over = false; bool over = false;
bool down = false; bool down = false;
@ -1105,24 +1114,28 @@ static void setRows(Music* music, s32 delta, void* data)
static void drawTopPanel(Music* music, s32 x, s32 y) static void drawTopPanel(Music* music, s32 x, s32 y)
{ {
tic_mem* tic = music->tic;
tic_track* track = getTrack(music); tic_track* track = getTrack(music);
drawSwitch(music, x, y, "TRACK", music->track, setIndex, NULL); drawSwitch(music, x, y, "TRACK", music->track, setIndex, NULL);
drawSwitch(music, x += TIC_FONT_WIDTH * 10, y, "TEMPO", track->tempo + DEFAULT_TEMPO, setTempo, NULL); drawSwitch(music, x += tic->font.width * 10, y, "TEMPO", track->tempo + DEFAULT_TEMPO, setTempo, NULL);
drawSwitch(music, x += TIC_FONT_WIDTH * 11, y, "SPD", track->speed + DEFAULT_SPEED, setSpeed, NULL); drawSwitch(music, x += tic->font.width * 11, y, "SPD", track->speed + DEFAULT_SPEED, setSpeed, NULL);
drawSwitch(music, x += TIC_FONT_WIDTH * 8, y, "ROWS", MUSIC_PATTERN_ROWS - track->rows, setRows, NULL); drawSwitch(music, x += tic->font.width * 8, y, "ROWS", MUSIC_PATTERN_ROWS - track->rows, setRows, NULL);
} }
static void drawTrackerFrames(Music* music, s32 x, s32 y) static void drawTrackerFrames(Music* music, s32 x, s32 y)
{ {
tic_mem* tic = music->tic;
enum enum
{ {
Border = 1, Border = 1,
Width = TIC_FONT_WIDTH * 2 + Border,
}; };
s32 width = tic->font.width * 2 + Border;
{ {
tic_rect rect = { x - Border, y - Border, Width, MUSIC_FRAMES * TIC_FONT_HEIGHT + Border }; tic_rect rect = { x - Border, y - Border, width, MUSIC_FRAMES * tic->font.height + Border };
if (checkMousePos(&rect)) if (checkMousePos(&rect))
{ {
@ -1131,7 +1144,7 @@ static void drawTrackerFrames(Music* music, s32 x, s32 y)
if (checkMouseDown(&rect, tic_mouse_left)) if (checkMouseDown(&rect, tic_mouse_left))
{ {
s32 my = getMouseY() - rect.y - Border; s32 my = getMouseY() - rect.y - Border;
music->tracker.frame = my / TIC_FONT_HEIGHT; music->tracker.frame = my / tic->font.height;
} }
} }
@ -1155,18 +1168,18 @@ static void drawTrackerFrames(Music* music, s32 x, s32 y)
0b00000000, 0b00000000,
}; };
drawBitIcon(x - TIC_FONT_WIDTH-1, y + i*TIC_FONT_HEIGHT, Icon, tic_color_black); drawBitIcon(x - 7, y + i*tic->font.height, Icon, tic_color_black);
drawBitIcon(x - TIC_FONT_WIDTH-1, y - 1 + i*TIC_FONT_HEIGHT, Icon, tic_color_white); drawBitIcon(x - 7, y - 1 + i*tic->font.height, Icon, tic_color_white);
} }
if (i == music->tracker.frame) if (i == music->tracker.frame)
{ {
music->tic->api.rect(music->tic, x - 1, y - 1 + i*TIC_FONT_HEIGHT, Width, TIC_FONT_HEIGHT + 1, (tic_color_white)); music->tic->api.rect(music->tic, x - 1, y - 1 + i*tic->font.height, width, tic->font.height + 1, (tic_color_white));
} }
char buf[] = "99"; char buf[] = "99";
sprintf(buf, "%02i", i); sprintf(buf, "%02i", i);
music->tic->api.fixed_text(music->tic, buf, x, y + i*TIC_FONT_HEIGHT, (tic_color_dark_gray)); music->tic->api.fixed_text(music->tic, buf, x, y + i*tic->font.height, (tic_color_dark_gray));
} }
if(music->tracker.row >= 0) if(music->tracker.row >= 0)
@ -1200,11 +1213,12 @@ static void drawTrackerChannel(Music* music, s32 x, s32 y, s32 channel)
enum enum
{ {
Border = 1, Border = 1,
Rows = TRACKER_ROWS, Rows = TRACKER_ROWS,
Width = TIC_FONT_WIDTH * 8 + Border,
}; };
tic_rect rect = {x - Border, y - Border, Width, Rows*TIC_FONT_HEIGHT + Border}; s32 width = tic->font.width * 8 + Border;
tic_rect rect = {x - Border, y - Border, width, Rows*tic->font.height + Border};
if(checkMousePos(&rect)) if(checkMousePos(&rect))
{ {
@ -1215,8 +1229,8 @@ static void drawTrackerChannel(Music* music, s32 x, s32 y, s32 channel)
s32 mx = getMouseX() - rect.x - Border; s32 mx = getMouseX() - rect.x - Border;
s32 my = getMouseY() - rect.y - Border; s32 my = getMouseY() - rect.y - Border;
s32 col = music->tracker.col = channel * CHANNEL_COLS + mx / TIC_FONT_WIDTH; s32 col = music->tracker.col = channel * CHANNEL_COLS + mx / tic->font.width;
s32 row = music->tracker.row = my / TIC_FONT_HEIGHT + music->tracker.scroll; s32 row = music->tracker.row = my / tic->font.height + music->tracker.scroll;
if(music->tracker.select.drag) if(music->tracker.select.drag)
{ {
@ -1252,11 +1266,11 @@ static void drawTrackerChannel(Music* music, s32 x, s32 y, s32 channel)
for (s32 i = start, pos = 0; i < end; i++, pos++) for (s32 i = start, pos = 0; i < end; i++, pos++)
{ {
s32 rowy = y + pos*TIC_FONT_HEIGHT; s32 rowy = y + pos*tic->font.height;
if (i == music->tracker.row) if (i == music->tracker.row)
{ {
music->tic->api.rect(music->tic, x - 1, rowy - 1, Width, TIC_FONT_HEIGHT + 1, (tic_color_dark_red)); music->tic->api.rect(music->tic, x - 1, rowy - 1, width, tic->font.height + 1, (tic_color_dark_red));
} }
// draw selection // draw selection
@ -1266,13 +1280,13 @@ static void drawTrackerChannel(Music* music, s32 x, s32 y, s32 channel)
if (rect.h > 1 && i >= rect.y && i < rect.y + rect.h) if (rect.h > 1 && i >= rect.y && i < rect.y + rect.h)
{ {
s32 sx = x - 1; s32 sx = x - 1;
tic->api.rect(tic, sx, rowy - 1, CHANNEL_COLS * TIC_FONT_WIDTH + 1, TIC_FONT_HEIGHT + 1, (tic_color_yellow)); tic->api.rect(tic, sx, rowy - 1, CHANNEL_COLS * tic->font.width + 1, tic->font.height + 1, (tic_color_yellow));
} }
} }
if (checkPlayRow(music, i)) if (checkPlayRow(music, i))
{ {
music->tic->api.rect(music->tic, x - 1, rowy - 1, Width, TIC_FONT_HEIGHT + 1, (tic_color_white)); music->tic->api.rect(music->tic, x - 1, rowy - 1, width, tic->font.height + 1, (tic_color_white));
} }
char rowStr[] = "--------"; char rowStr[] = "--------";
@ -1301,7 +1315,7 @@ static void drawTrackerChannel(Music* music, s32 x, s32 y, s32 channel)
bool beetRow = i % NOTES_PER_BEET == 0; bool beetRow = i % NOTES_PER_BEET == 0;
for (s32 c = 0, colx = x; c < sizeof rowStr - 1; c++, colx += TIC_FONT_WIDTH) for (s32 c = 0, colx = x; c < sizeof rowStr - 1; c++, colx += tic->font.width)
{ {
char sym = rowStr[c]; char sym = rowStr[c];
const u8* colors = beetRow || sym != '-' ? Colors : DarkColors; const u8* colors = beetRow || sym != '-' ? Colors : DarkColors;
@ -1317,14 +1331,14 @@ static void drawTrackerChannel(Music* music, s32 x, s32 y, s32 channel)
if (music->tracker.col / CHANNEL_COLS == channel) if (music->tracker.col / CHANNEL_COLS == channel)
{ {
s32 col = music->tracker.col % CHANNEL_COLS; s32 col = music->tracker.col % CHANNEL_COLS;
s32 colx = x - 1 + col * TIC_FONT_WIDTH; s32 colx = x - 1 + col * tic->font.width;
music->tic->api.rect(music->tic, colx, rowy - 1, TIC_FONT_WIDTH + 1, TIC_FONT_HEIGHT + 1, (tic_color_red)); music->tic->api.rect(music->tic, colx, rowy - 1, tic->font.width + 1, tic->font.height + 1, (tic_color_red));
music->tic->api.draw_char(music->tic, rowStr[col], colx + 1, rowy, (tic_color_black)); music->tic->api.draw_char(music->tic, rowStr[col], colx + 1, rowy, (tic_color_black));
} }
} }
if (i % NOTES_PER_BEET == 0) if (i % NOTES_PER_BEET == 0)
music->tic->api.pixel(music->tic, x - 4, y + pos*TIC_FONT_HEIGHT + 2, (tic_color_black)); music->tic->api.pixel(music->tic, x - 4, y + pos*tic->font.height + 2, (tic_color_black));
} }
} }
@ -1344,12 +1358,12 @@ static void drawTumbler(Music* music, s32 x, s32 y, s32 index)
if(checkMouseClick(&rect, tic_mouse_left)) if(checkMouseClick(&rect, tic_mouse_left))
{ {
// if (SDL_GetModState() & KMOD_CTRL) if (tic->api.key(tic, tic_key_ctrl))
// { {
// for (s32 i = 0; i < TIC_SOUND_CHANNELS; i++) for (s32 i = 0; i < TIC_SOUND_CHANNELS; i++)
// music->tracker.patterns[i] = i == index; music->tracker.patterns[i] = i == index;
// } }
// else music->tracker.patterns[index] = !music->tracker.patterns[index]; else music->tracker.patterns[index] = !music->tracker.patterns[index];
} }
} }
@ -1359,21 +1373,25 @@ static void drawTumbler(Music* music, s32 x, s32 y, s32 index)
static void drawTracker(Music* music, s32 x, s32 y) static void drawTracker(Music* music, s32 x, s32 y)
{ {
tic_mem* tic = music->tic;
drawTrackerFrames(music, x, y); drawTrackerFrames(music, x, y);
x += TIC_FONT_WIDTH * 3; enum{Gap = 6, Cols = 8};
enum{ChannelWidth = TIC_FONT_WIDTH * 9}; x += tic->font.width * 2 + Gap;
s32 channelWidth = tic->font.width * Cols + Gap;
for (s32 i = 0; i < TIC_SOUND_CHANNELS; i++) for (s32 i = 0; i < TIC_SOUND_CHANNELS; i++)
{ {
s32 patternId = tic_tool_get_pattern_id(getTrack(music), music->tracker.frame, i); s32 patternId = tic_tool_get_pattern_id(getTrack(music), music->tracker.frame, i);
drawEditbox(music, x + ChannelWidth * i + 2*TIC_FONT_WIDTH, y - 11, patternId, setChannelPattern, i); drawEditbox(music, x + channelWidth * i + 3*tic->font.width, y - 11, patternId, setChannelPattern, i);
drawTumbler(music, x + ChannelWidth * i + 7*TIC_FONT_WIDTH, y - 11, i); drawTumbler(music, x + channelWidth * (i+1) - 4 - Gap, y - 11, i);
} }
for (s32 i = 0; i < TIC_SOUND_CHANNELS; i++) for (s32 i = 0; i < TIC_SOUND_CHANNELS; i++)
drawTrackerChannel(music, x + ChannelWidth * i, y, i); drawTrackerChannel(music, x + channelWidth * i, y, i);
} }
static void enableFollowMode(Music* music) static void enableFollowMode(Music* music)
@ -1517,10 +1535,11 @@ static void drawMusicToolbar(Music* music)
static void drawPianoLayout(Music* music) static void drawPianoLayout(Music* music)
{ {
tic_mem* tic = music->tic;
music->tic->api.clear(music->tic, (tic_color_gray)); music->tic->api.clear(music->tic, (tic_color_gray));
static const char Wip[] = "PIANO MODE - WORK IN PROGRESS..."; static const char Wip[] = "PIANO MODE - WORK IN PROGRESS...";
music->tic->api.fixed_text(music->tic, Wip, (TIC80_WIDTH - (sizeof Wip - 1) * TIC_FONT_WIDTH) / 2, TIC80_HEIGHT / 2, (tic_color_white)); music->tic->api.fixed_text(music->tic, Wip, (TIC80_WIDTH - (sizeof Wip - 1) * tic->font.width) / 2, TIC80_HEIGHT / 2, (tic_color_white));
} }
static void scrollNotes(Music* music, s32 delta) static void scrollNotes(Music* music, s32 delta)

View File

@ -39,13 +39,15 @@
static void drawSwitch(Sfx* sfx, s32 x, s32 y, const char* label, s32 value, void(*set)(Sfx*, s32)) static void drawSwitch(Sfx* sfx, s32 x, s32 y, const char* label, s32 value, void(*set)(Sfx*, s32))
{ {
tic_mem* tic = sfx->tic;
static const u8 LeftArrow[] = static const u8 LeftArrow[] =
{ {
0b00010000, 0b00100000,
0b00110000, 0b01100000,
0b01110000, 0b11100000,
0b00110000, 0b01100000,
0b00010000, 0b00100000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
@ -53,22 +55,24 @@ static void drawSwitch(Sfx* sfx, s32 x, s32 y, const char* label, s32 value, voi
static const u8 RightArrow[] = static const u8 RightArrow[] =
{ {
0b01000000, 0b10000000,
0b01100000, 0b11000000,
0b01110000, 0b11100000,
0b01100000, 0b11000000,
0b01000000, 0b10000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
0b00000000, 0b00000000,
}; };
enum{ArrowWidth = 4, ArrowHeight = 6};
sfx->tic->api.text(sfx->tic, label, x, y, (tic_color_white)); sfx->tic->api.text(sfx->tic, label, x, y, (tic_color_white));
{ {
x += (s32)strlen(label)*TIC_FONT_WIDTH; x += (s32)strlen(label)*tic->font.width;
tic_rect rect = {x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT}; tic_rect rect = {x, y, ArrowWidth, ArrowHeight};
if(checkMousePos(&rect)) if(checkMousePos(&rect))
{ {
@ -84,13 +88,13 @@ static void drawSwitch(Sfx* sfx, s32 x, s32 y, const char* label, s32 value, voi
{ {
char val[] = "99"; char val[] = "99";
sprintf(val, "%02i", value); sprintf(val, "%02i", value);
sfx->tic->api.fixed_text(sfx->tic, val, x += TIC_FONT_WIDTH, y, (tic_color_white)); sfx->tic->api.fixed_text(sfx->tic, val, x += ArrowWidth, y, (tic_color_white));
} }
{ {
x += 2*TIC_FONT_WIDTH; x += 2*tic->font.width;
tic_rect rect = {x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT}; tic_rect rect = {x, y, ArrowWidth, ArrowHeight};
if(checkMousePos(&rect)) if(checkMousePos(&rect))
{ {
@ -125,7 +129,8 @@ static void setSpeed(Sfx* sfx, s32 delta)
static void drawTopPanel(Sfx* sfx, s32 x, s32 y) static void drawTopPanel(Sfx* sfx, s32 x, s32 y)
{ {
const s32 Gap = 8*TIC_FONT_WIDTH; tic_mem* tic = sfx->tic;
const s32 Gap = 8*tic->font.width;
drawSwitch(sfx, x, y, "IDX", sfx->index, setIndex); drawSwitch(sfx, x, y, "IDX", sfx->index, setIndex);
@ -156,6 +161,7 @@ static void setLoopSize(Sfx* sfx, s32 delta)
static void drawLoopPanel(Sfx* sfx, s32 x, s32 y) static void drawLoopPanel(Sfx* sfx, s32 x, s32 y)
{ {
tic_mem* tic = sfx->tic;
sfx->tic->api.text(sfx->tic, "LOOP:", x, y, (tic_color_dark_gray)); sfx->tic->api.text(sfx->tic, "LOOP:", x, y, (tic_color_dark_gray));
enum {Gap = 2}; enum {Gap = 2};
@ -163,8 +169,8 @@ static void drawLoopPanel(Sfx* sfx, s32 x, s32 y)
tic_sample* effect = getEffect(sfx); tic_sample* effect = getEffect(sfx);
tic_sound_loop* loop = effect->loops + sfx->canvasTab; tic_sound_loop* loop = effect->loops + sfx->canvasTab;
drawSwitch(sfx, x, y += Gap + TIC_FONT_HEIGHT, "", loop->size, setLoopSize); drawSwitch(sfx, x, y += Gap + tic->font.height, "", loop->size, setLoopSize);
drawSwitch(sfx, x, y += Gap + TIC_FONT_HEIGHT, "", loop->start, setLoopStart); drawSwitch(sfx, x, y += Gap + tic->font.height, "", loop->start, setLoopStart);
} }
static tic_waveform* getWaveformById(Sfx* sfx, s32 i) static tic_waveform* getWaveformById(Sfx* sfx, s32 i)
@ -287,15 +293,16 @@ static void drawWaveButtons(Sfx* sfx, s32 x, s32 y)
static void drawCanvasTabs(Sfx* sfx, s32 x, s32 y) static void drawCanvasTabs(Sfx* sfx, s32 x, s32 y)
{ {
tic_mem* tic = sfx->tic;
static const char* Labels[] = {"WAVE", "VOLUME", "ARPEGG", "PITCH"}; static const char* Labels[] = {"WAVE", "VOLUME", "ARPEGG", "PITCH"};
enum {Height = TIC_FONT_HEIGHT+2}; s32 height = tic->font.height+2;
for(s32 i = 0, sy = y; i < COUNT_OF(Labels); sy += Height, i++) for(s32 i = 0, sy = y; i < COUNT_OF(Labels); sy += height, i++)
{ {
s32 size = sfx->tic->api.text(sfx->tic, Labels[i], 0, -TIC_FONT_HEIGHT, (tic_color_black)); s32 size = sfx->tic->api.text(sfx->tic, Labels[i], 0, -tic->font.height, (tic_color_black));
tic_rect rect = {x - size, sy, size, TIC_FONT_HEIGHT}; tic_rect rect = {x - size, sy, size, tic->font.height};
if(checkMousePos(&rect)) if(checkMousePos(&rect))
{ {
@ -317,8 +324,8 @@ static void drawCanvasTabs(Sfx* sfx, s32 x, s32 y)
case SFX_PITCH_TAB: case SFX_PITCH_TAB:
{ {
static const char Label[] = "x16"; static const char Label[] = "x16";
enum{Width = (sizeof Label - 1) * TIC_FONT_WIDTH}; s32 width = (sizeof Label - 1) * tic->font.width;
tic_rect rect = {(x - Width)/2, y + Height * 6, Width, TIC_FONT_HEIGHT}; tic_rect rect = {(x - width)/2, y + height * 6, width, tic->font.height};
if(checkMousePos(&rect)) if(checkMousePos(&rect))
{ {
@ -334,8 +341,8 @@ static void drawCanvasTabs(Sfx* sfx, s32 x, s32 y)
case SFX_ARPEGGIO_TAB: case SFX_ARPEGGIO_TAB:
{ {
static const char Label[] = "DOWN"; static const char Label[] = "DOWN";
enum{Width = (sizeof Label - 1) * TIC_FONT_WIDTH}; s32 width = (sizeof Label - 1) * tic->font.width;
tic_rect rect = {(x - Width)/2, y + Height * 6, Width, TIC_FONT_HEIGHT}; tic_rect rect = {(x - width)/2, y + height * 6, width, tic->font.height};
if(checkMousePos(&rect)) if(checkMousePos(&rect))
{ {
@ -514,18 +521,19 @@ static void drawPiano(Sfx* sfx, s32 x, s32 y)
static void drawOctavePanel(Sfx* sfx, s32 x, s32 y) static void drawOctavePanel(Sfx* sfx, s32 x, s32 y)
{ {
tic_mem* tic = sfx->tic;
tic_sample* effect = getEffect(sfx); tic_sample* effect = getEffect(sfx);
static const char Label[] = "OCT"; static const char Label[] = "OCT";
sfx->tic->api.text(sfx->tic, Label, x, y, (tic_color_white)); sfx->tic->api.text(sfx->tic, Label, x, y, (tic_color_white));
x += sizeof(Label)*TIC_FONT_WIDTH; x += sizeof(Label)*tic->font.width;
enum {Gap = 5}; enum {Gap = 5};
for(s32 i = 0; i < OCTAVES; i++) for(s32 i = 0; i < OCTAVES; i++)
{ {
tic_rect rect = {x + i * (TIC_FONT_WIDTH + Gap), y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT}; tic_rect rect = {x + i * (tic->font.width + Gap), y, tic->font.width, tic->font.height};
if(checkMousePos(&rect)) if(checkMousePos(&rect))
{ {
@ -781,13 +789,14 @@ static void drawModeTabs(Sfx* sfx)
static void drawSfxToolbar(Sfx* sfx) static void drawSfxToolbar(Sfx* sfx)
{ {
tic_mem* tic = sfx->tic;
sfx->tic->api.rect(sfx->tic, 0, 0, TIC80_WIDTH, TOOLBAR_SIZE, (tic_color_white)); sfx->tic->api.rect(sfx->tic, 0, 0, TIC80_WIDTH, TOOLBAR_SIZE, (tic_color_white));
enum{Width = 3 * TIC_FONT_WIDTH}; s32 width = 3 * tic->font.width;
s32 x = TIC80_WIDTH - Width - TIC_SPRITESIZE*3; s32 x = TIC80_WIDTH - width - TIC_SPRITESIZE*3;
s32 y = 1; s32 y = 1;
tic_rect rect = {x, y, Width, TIC_FONT_HEIGHT}; tic_rect rect = {x, y, width, tic->font.height};
bool over = false; bool over = false;
if(checkMousePos(&rect)) if(checkMousePos(&rect))
@ -818,6 +827,7 @@ static void drawSfxToolbar(Sfx* sfx)
static void envelopesTick(Sfx* sfx) static void envelopesTick(Sfx* sfx)
{ {
tic_mem* tic = sfx->tic;
processKeyboard(sfx); processKeyboard(sfx);
processEnvelopesKeyboard(sfx); processEnvelopesKeyboard(sfx);
@ -830,13 +840,13 @@ static void envelopesTick(Sfx* sfx)
drawToolbar(sfx->tic, TIC_COLOR_BG, false); drawToolbar(sfx->tic, TIC_COLOR_BG, false);
drawTopPanel(sfx, Start, TOOLBAR_SIZE + Gap); drawTopPanel(sfx, Start, TOOLBAR_SIZE + Gap);
drawCanvasTabs(sfx, Start-Gap, TOOLBAR_SIZE + Gap + TIC_FONT_HEIGHT+2); drawCanvasTabs(sfx, Start-Gap, TOOLBAR_SIZE + Gap + tic->font.height+2);
if(sfx->canvasTab == SFX_WAVE_TAB) if(sfx->canvasTab == SFX_WAVE_TAB)
drawWaveButtons(sfx, Start + CANVAS_WIDTH + Gap-1, TOOLBAR_SIZE + Gap + TIC_FONT_HEIGHT+2); drawWaveButtons(sfx, Start + CANVAS_WIDTH + Gap-1, TOOLBAR_SIZE + Gap + tic->font.height+2);
drawLoopPanel(sfx, Gap, TOOLBAR_SIZE + Gap + TIC_FONT_HEIGHT+92); drawLoopPanel(sfx, Gap, TOOLBAR_SIZE + Gap + tic->font.height+92);
drawCanvas(sfx, Start-1, TOOLBAR_SIZE + Gap + TIC_FONT_HEIGHT + 1); drawCanvas(sfx, Start-1, TOOLBAR_SIZE + Gap + tic->font.height + 1);
drawOctavePanel(sfx, Start + Gap + PIANO_WIDTH + Gap-1, TIC80_HEIGHT - TIC_FONT_HEIGHT - (PIANO_HEIGHT - TIC_FONT_HEIGHT)/2 - Gap); drawOctavePanel(sfx, Start + Gap + PIANO_WIDTH + Gap-1, TIC80_HEIGHT - tic->font.height - (PIANO_HEIGHT - tic->font.height)/2 - Gap);
} }
static void drawWaveformBar(Sfx* sfx, s32 x, s32 y) static void drawWaveformBar(Sfx* sfx, s32 x, s32 y)

View File

@ -359,12 +359,14 @@ static void drawBrushSlider(Sprite* sprite, s32 x, s32 y)
static void drawCanvas(Sprite* sprite, s32 x, s32 y) static void drawCanvas(Sprite* sprite, s32 x, s32 y)
{ {
tic_mem* tic = sprite->tic;
if(!hasCanvasSelection(sprite)) if(!hasCanvasSelection(sprite))
{ {
char buf[] = "#255"; char buf[] = "#255";
sprintf(buf, "#%03i", sprite->index); sprintf(buf, "#%03i", sprite->index);
s32 ix = x + (CANVAS_SIZE - 4*TIC_FONT_WIDTH)/2; s32 ix = x + (CANVAS_SIZE - 4*tic->font.width)/2;
s32 iy = TIC_SPRITESIZE + 2; s32 iy = TIC_SPRITESIZE + 2;
sprite->tic->api.text(sprite->tic, buf, ix, iy+1, (tic_color_black)); sprite->tic->api.text(sprite->tic, buf, ix, iy+1, (tic_color_black));
sprite->tic->api.text(sprite->tic, buf, ix, iy, (tic_color_white)); sprite->tic->api.text(sprite->tic, buf, ix, iy, (tic_color_white));
@ -1492,6 +1494,7 @@ static void processKeyboard(Sprite* sprite)
static void drawSpriteToolbar(Sprite* sprite) static void drawSpriteToolbar(Sprite* sprite)
{ {
tic_mem* tic = sprite->tic;
sprite->tic->api.rect(sprite->tic, 0, 0, TIC80_WIDTH, TOOLBAR_SIZE, (tic_color_white)); sprite->tic->api.rect(sprite->tic, 0, 0, TIC80_WIDTH, TOOLBAR_SIZE, (tic_color_white));
// draw sprite size control // draw sprite size control
@ -1533,7 +1536,7 @@ static void drawSpriteToolbar(Sprite* sprite)
{ {
static const char Label[] = "BG"; static const char Label[] = "BG";
tic_rect rect = {TIC80_WIDTH - 2 * TIC_FONT_WIDTH - 2, 0, 2 * TIC_FONT_WIDTH + 1, TIC_SPRITESIZE-1}; tic_rect rect = {TIC80_WIDTH - 2 * tic->font.width - 2, 0, 2 * tic->font.width + 1, TIC_SPRITESIZE-1};
sprite->tic->api.rect(sprite->tic, rect.x, rect.y, rect.w, rect.h, bg ? (tic_color_black) : (tic_color_gray)); sprite->tic->api.rect(sprite->tic, rect.x, rect.y, rect.w, rect.h, bg ? (tic_color_black) : (tic_color_gray));
sprite->tic->api.fixed_text(sprite->tic, Label, rect.x+1, rect.y+1, (tic_color_white)); sprite->tic->api.fixed_text(sprite->tic, Label, rect.x+1, rect.y+1, (tic_color_white));
@ -1553,7 +1556,7 @@ static void drawSpriteToolbar(Sprite* sprite)
{ {
static const char Label[] = "FG"; static const char Label[] = "FG";
tic_rect rect = {TIC80_WIDTH - 4 * TIC_FONT_WIDTH - 4, 0, 2 * TIC_FONT_WIDTH + 1, TIC_SPRITESIZE-1}; tic_rect rect = {TIC80_WIDTH - 4 * tic->font.width - 4, 0, 2 * tic->font.width + 1, TIC_SPRITESIZE-1};
sprite->tic->api.rect(sprite->tic, rect.x, rect.y, rect.w, rect.h, bg ? (tic_color_gray) : (tic_color_black)); sprite->tic->api.rect(sprite->tic, rect.x, rect.y, rect.w, rect.h, bg ? (tic_color_gray) : (tic_color_black));
sprite->tic->api.fixed_text(sprite->tic, Label, rect.x+1, rect.y+1, (tic_color_white)); sprite->tic->api.fixed_text(sprite->tic, Label, rect.x+1, rect.y+1, (tic_color_white));

View File

@ -24,23 +24,25 @@
static void reset(Start* start) static void reset(Start* start)
{ {
u8* tile = (u8*)start->tic->ram.tiles.data; tic_mem* tic = start->tic;
u8* tile = (u8*)tic->ram.tiles.data;
start->tic->api.clear(start->tic, (tic_color_black)); tic->api.clear(tic, (tic_color_black));
static const u8 Reset[] = {0x00, 0x06, 0x96, 0x00}; static const u8 Reset[] = {0x00, 0x06, 0x96, 0x00};
u8 val = Reset[sizeof(Reset) * (start->ticks % TIC_FRAMERATE) / TIC_FRAMERATE]; u8 val = Reset[sizeof(Reset) * (start->ticks % TIC_FRAMERATE) / TIC_FRAMERATE];
for(s32 i = 0; i < sizeof(tic_tile); i++) tile[i] = val; for(s32 i = 0; i < sizeof(tic_tile); i++) tile[i] = val;
start->tic->api.map(start->tic, &start->tic->ram.map, &start->tic->ram.tiles, 0, 0, TIC_MAP_SCREEN_WIDTH, TIC_MAP_SCREEN_HEIGHT + (TIC80_HEIGHT % TIC_SPRITESIZE ? 1 : 0), 0, 0, -1, 1); tic->api.map(tic, &tic->ram.map, &tic->ram.tiles, 0, 0, TIC_MAP_SCREEN_WIDTH, TIC_MAP_SCREEN_HEIGHT + (TIC80_HEIGHT % TIC_SPRITESIZE ? 1 : 0), 0, 0, -1, 1);
} }
static void drawHeader(Start* start) static void drawHeader(Start* start)
{ {
start->tic->api.fixed_text(start->tic, TIC_NAME_FULL, STUDIO_TEXT_WIDTH, STUDIO_TEXT_HEIGHT, (tic_color_white)); tic_mem* tic = start->tic;
start->tic->api.fixed_text(start->tic, TIC_VERSION_LABEL, (sizeof(TIC_NAME_FULL) + 1) * STUDIO_TEXT_WIDTH, STUDIO_TEXT_HEIGHT, (tic_color_dark_gray)); tic->api.fixed_text(tic, TIC_NAME_FULL, TextWidth(tic), TextHeight(tic), (tic_color_white));
start->tic->api.fixed_text(start->tic, TIC_COPYRIGHT, STUDIO_TEXT_WIDTH, STUDIO_TEXT_HEIGHT*2, (tic_color_dark_gray)); tic->api.fixed_text(tic, TIC_VERSION_LABEL, (sizeof(TIC_NAME_FULL) + 1) * TextWidth(tic), TextHeight(tic), (tic_color_dark_gray));
tic->api.fixed_text(tic, TIC_COPYRIGHT, TextWidth(tic), TextHeight(tic)*2, (tic_color_dark_gray));
} }
static void header(Start* start) static void header(Start* start)
@ -57,9 +59,10 @@ static void header(Start* start)
static void end(Start* start) static void end(Start* start)
{ {
tic_mem* tic = start->tic;
if(start->play) if(start->play)
{ {
start->tic->api.sfx_stop(start->tic, 0); tic->api.sfx_stop(tic, 0);
start->play = false; start->play = false;
} }
@ -70,6 +73,8 @@ static void end(Start* start)
static void tick(Start* start) static void tick(Start* start)
{ {
tic_mem* tic = start->tic;
if(!start->initialized) if(!start->initialized)
{ {
start->phase = 1; start->phase = 1;
@ -78,7 +83,7 @@ static void tick(Start* start)
start->initialized = true; start->initialized = true;
} }
start->tic->api.clear(start->tic, TIC_COLOR_BG); tic->api.clear(tic, TIC_COLOR_BG);
static void(*const steps[])(Start*) = {reset, header, end}; static void(*const steps[])(Start*) = {reset, header, end};

View File

@ -133,12 +133,12 @@ static struct
struct struct
{ {
s32 counter; s32 counter;
char message[STUDIO_TEXT_BUFFER_WIDTH]; char message[TIC80_WIDTH];
} popup; } popup;
struct struct
{ {
char text[STUDIO_TEXT_BUFFER_WIDTH]; char text[TIC80_WIDTH];
} tooltip; } tooltip;
struct struct
@ -436,7 +436,7 @@ static void drawExtrabar(tic_mem* tic)
{ {
enum {Size = 7}; enum {Size = 7};
s32 x = (COUNT_OF(Modes) + 1) * Size + 17 * TIC_FONT_WIDTH; s32 x = (COUNT_OF(Modes) + 1) * Size + 102;
s32 y = 0; s32 y = 0;
static const u8 Icons[] = static const u8 Icons[] =
@ -532,7 +532,7 @@ static void drawBankIcon(s32 x, s32 y)
{ {
tic_mem* tic = impl.studio.tic; tic_mem* tic = impl.studio.tic;
tic_rect rect = {x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT}; tic_rect rect = {x, y, tic->font.width, tic->font.height};
static const u8 Icon[] = static const u8 Icon[] =
{ {
@ -574,9 +574,11 @@ static void drawBankIcon(s32 x, s32 y)
enum{Size = TOOLBAR_SIZE}; enum{Size = TOOLBAR_SIZE};
x += Size+2;
for(s32 i = 0; i < TIC_EDITOR_BANKS; i++) for(s32 i = 0; i < TIC_EDITOR_BANKS; i++)
{ {
tic_rect rect = {x + 2 + (i+1)*Size, 0, Size, Size}; tic_rect rect = {x + i*Size, 0, Size, Size};
bool over = false; bool over = false;
if(checkMousePos(&rect)) if(checkMousePos(&rect))
@ -595,7 +597,7 @@ static void drawBankIcon(s32 x, s32 y)
if(i == impl.bank.indexes[mode]) if(i == impl.bank.indexes[mode])
tic->api.rect(tic, rect.x, rect.y, rect.w, rect.h, tic_color_red); 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 == impl.bank.indexes[mode] ? tic_color_white : over ? tic_color_red : tic_color_peach); tic->api.draw_char(tic, '0' + i, rect.x+(Size-tic->font.width+1)/2, rect.y+1, i == impl.bank.indexes[mode] ? tic_color_white : over ? tic_color_red : tic_color_peach);
} }
@ -612,7 +614,7 @@ static void drawBankIcon(s32 x, s32 y)
0b00000000, 0b00000000,
}; };
tic_rect rect = {x + 4 + (TIC_EDITOR_BANKS+1)*Size, 0, Size, Size}; tic_rect rect = {x + 2 + TIC_EDITOR_BANKS*Size, 0, Size, Size};
bool over = false; bool over = false;
@ -1522,6 +1524,8 @@ static void recordFrame(u32* pixels)
static void drawPopup() static void drawPopup()
{ {
tic_mem* tic = impl.studio.tic;
if(impl.popup.counter > 0) if(impl.popup.counter > 0)
{ {
impl.popup.counter--; impl.popup.counter--;
@ -1531,13 +1535,13 @@ static void drawPopup()
enum{Dur = TIC_FRAMERATE/2}; enum{Dur = TIC_FRAMERATE/2};
if(impl.popup.counter < Dur) if(impl.popup.counter < Dur)
anim = -((Dur - impl.popup.counter) * (TIC_FONT_HEIGHT+1) / Dur); anim = -((Dur - impl.popup.counter) * (tic->font.height+1) / Dur);
else if(impl.popup.counter >= (POPUP_DUR - Dur)) else if(impl.popup.counter >= (POPUP_DUR - Dur))
anim = (((POPUP_DUR - Dur) - impl.popup.counter) * (TIC_FONT_HEIGHT+1) / Dur); anim = (((POPUP_DUR - Dur) - impl.popup.counter) * (tic->font.height+1) / Dur);
impl.studio.tic->api.rect(impl.studio.tic, 0, anim, TIC80_WIDTH, TIC_FONT_HEIGHT+1, (tic_color_red)); impl.studio.tic->api.rect(impl.studio.tic, 0, anim, TIC80_WIDTH, tic->font.height+1, (tic_color_red));
impl.studio.tic->api.text(impl.studio.tic, impl.popup.message, impl.studio.tic->api.text(impl.studio.tic, impl.popup.message,
(s32)(TIC80_WIDTH - strlen(impl.popup.message)*TIC_FONT_WIDTH)/2, (s32)(TIC80_WIDTH - strlen(impl.popup.message)*tic->font.width)/2,
anim + 1, (tic_color_white)); anim + 1, (tic_color_white));
} }
} }
@ -1626,7 +1630,12 @@ static void renderStudio()
static void updateSystemFont() static void updateSystemFont()
{ {
memset(impl.studio.tic->font.data, 0, sizeof(tic_font)); tic_mem* tic = impl.studio.tic;
memset(tic->font.data, 0, sizeof tic->font.data);
tic->font.width = impl.config->data.theme.font.width;
tic->font.height = impl.config->data.theme.font.height;
for(s32 i = 0; i < TIC_FONT_CHARS; i++) for(s32 i = 0; i < TIC_FONT_CHARS; i++)
for(s32 y = 0; y < TIC_SPRITESIZE; y++) for(s32 y = 0; y < TIC_SPRITESIZE; y++)

View File

@ -38,10 +38,7 @@
#define TIC_CACHE TIC_LOCAL "cache/" #define TIC_CACHE TIC_LOCAL "cache/"
#define TOOLBAR_SIZE 7 #define TOOLBAR_SIZE 7
#define STUDIO_TEXT_WIDTH (TIC_FONT_WIDTH) #define TEXT_LINE_SPACE 1
#define STUDIO_TEXT_HEIGHT (TIC_FONT_HEIGHT+1)
#define STUDIO_TEXT_BUFFER_WIDTH (TIC80_WIDTH / STUDIO_TEXT_WIDTH)
#define STUDIO_TEXT_BUFFER_HEIGHT (TIC80_HEIGHT / STUDIO_TEXT_HEIGHT)
#define TIC_COLOR_BG (tic_color_black) #define TIC_COLOR_BG (tic_color_black)
#define DEFAULT_CHMOD 0755 #define DEFAULT_CHMOD 0755
@ -163,3 +160,8 @@ bool anyKeyWasPressed();
const StudioConfig* getConfig(); const StudioConfig* getConfig();
System* getSystem(); System* getSystem();
inline s32 TextWidth(const tic_mem* tic) { return tic->font.width; }
inline s32 TextHeight(const tic_mem* tic) { return tic->font.height + TEXT_LINE_SPACE; }
inline s32 BufferWidth(const tic_mem* tic) { return TIC80_WIDTH/TextWidth(tic); }
inline s32 BufferHeight(const tic_mem* tic) { return TIC80_HEIGHT/TextHeight(tic); }

View File

@ -193,7 +193,7 @@ static void drawTopToolbar(Surf* surf, s32 x, s32 y)
sprintf(label, "%s", "TIC-80 SURF"); sprintf(label, "%s", "TIC-80 SURF");
s32 xl = x + MAIN_OFFSET; s32 xl = x + MAIN_OFFSET;
s32 yl = y + (Height - TIC_FONT_HEIGHT)/2; s32 yl = y + (Height - tic->font.height)/2;
tic->api.text(tic, label, xl, yl+1, tic_color_black); tic->api.text(tic, label, xl, yl+1, tic_color_black);
tic->api.text(tic, label, xl, yl, tic_color_white); tic->api.text(tic, label, xl, yl, tic_color_white);
} }
@ -231,7 +231,7 @@ static void drawBottomToolbar(Surf* surf, s32 x, s32 y)
sprintf(label, "/%s", dir); sprintf(label, "/%s", dir);
s32 xl = x + MAIN_OFFSET; s32 xl = x + MAIN_OFFSET;
s32 yl = y + (Height - TIC_FONT_HEIGHT)/2; s32 yl = y + (Height - tic->font.height)/2;
tic->api.text(tic, label, xl, yl+1, tic_color_black); tic->api.text(tic, label, xl, yl+1, tic_color_black);
tic->api.text(tic, label, xl, yl, tic_color_white); tic->api.text(tic, label, xl, yl, tic_color_white);
} }
@ -327,13 +327,13 @@ static void drawMenu(Surf* surf, s32 x, s32 y, bool bg)
{ {
const char* name = surf->menu.items[i].label; const char* name = surf->menu.items[i].label;
s32 ym = Height * i + y - surf->menu.pos*MENU_HEIGHT - surf->menu.anim + (MENU_HEIGHT - TIC_FONT_HEIGHT)/2; s32 ym = Height * i + y - surf->menu.pos*MENU_HEIGHT - surf->menu.anim + (MENU_HEIGHT - tic->font.height)/2;
if(bg) if(bg)
{ {
s32 size = tic->api.text(tic, name, 0, -TIC_FONT_HEIGHT, 0); s32 size = tic->api.text(tic, name, 0, -tic->font.height, 0);
drawInverseRect(tic, x + MAIN_OFFSET - 1, ym-1, size+1, TIC_FONT_HEIGHT+2); drawInverseRect(tic, x + MAIN_OFFSET - 1, ym-1, size+1, tic->font.height+2);
} }
else else
{ {
@ -837,8 +837,8 @@ static void tick(Surf* surf)
else else
{ {
static const char Label[] = "You don't have any files..."; static const char Label[] = "You don't have any files...";
s32 size = tic->api.text(tic, Label, 0, -TIC_FONT_HEIGHT, tic_color_white); s32 size = tic->api.text(tic, Label, 0, -tic->font.height, tic_color_white);
tic->api.text(tic, Label, (TIC80_WIDTH - size) / 2, (TIC80_HEIGHT - TIC_FONT_HEIGHT)/2, tic_color_white); tic->api.text(tic, Label, (TIC80_WIDTH - size) / 2, (TIC80_HEIGHT - tic->font.height)/2, tic_color_white);
} }
} }

View File

@ -63,6 +63,12 @@ typedef struct
} gamepad; } gamepad;
struct
{
s32 width;
s32 height;
} font;
} theme; } theme;
s32 gifScale; s32 gifScale;

396
src/tic.c
View File

@ -147,11 +147,11 @@ static void runNoise(blip_buffer_t* blip, tic_sound_register* reg, tic_sound_reg
} }
} }
static void resetPalette(tic_mem* memory) static void resetPalette(tic_mem* tic)
{ {
static const u8 DefaultMapping[] = {16, 50, 84, 118, 152, 186, 220, 254}; static const u8 DefaultMapping[] = {16, 50, 84, 118, 152, 186, 220, 254};
memcpy(memory->ram.vram.palette.data, memory->cart.bank0.palette.data, sizeof(tic_palette)); memcpy(tic->ram.vram.palette.data, tic->cart.bank0.palette.data, sizeof(tic_palette));
memcpy(memory->ram.vram.mapping, DefaultMapping, sizeof DefaultMapping); memcpy(tic->ram.vram.mapping, DefaultMapping, sizeof DefaultMapping);
} }
static inline u8 mapColor(tic_mem* tic, u8 color) static inline u8 mapColor(tic_mem* tic, u8 color)
@ -214,19 +214,19 @@ static u8 getPixel(tic_machine* machine, s32 x, s32 y)
return machine->state.getpix(&machine->memory, x, y); return machine->state.getpix(&machine->memory, x, y);
} }
static void drawHLineDma(tic_mem* memory, s32 xl, s32 xr, s32 y, u8 color) static void drawHLineDma(tic_mem* tic, s32 xl, s32 xr, s32 y, u8 color)
{ {
color = color << 4 | color; color = color << 4 | color;
if (xl >= xr) return; if (xl >= xr) return;
if (xl & 1) { if (xl & 1) {
tic_tool_poke4(&memory->ram.vram.screen.data, y * TIC80_WIDTH + xl, color); tic_tool_poke4(&tic->ram.vram.screen.data, y * TIC80_WIDTH + xl, color);
xl++; xl++;
} }
s32 count = (xr - xl) >> 1; s32 count = (xr - xl) >> 1;
u8 *screen = memory->ram.vram.screen.data + ((y * TIC80_WIDTH + xl) >> 1); u8 *screen = tic->ram.vram.screen.data + ((y * TIC80_WIDTH + xl) >> 1);
for(s32 i = 0; i < count; i++) *screen++ = color; for(s32 i = 0; i < count; i++) *screen++ = color;
if (xr & 1) { if (xr & 1) {
tic_tool_poke4(&memory->ram.vram.screen.data, y * TIC80_WIDTH + xr - 1, color); tic_tool_poke4(&tic->ram.vram.screen.data, y * TIC80_WIDTH + xr - 1, color);
} }
} }
@ -377,9 +377,9 @@ static void resetSfx(Channel* channel)
channel->tick = -1; channel->tick = -1;
} }
static void channelSfx(tic_mem* memory, s32 index, s32 note, s32 octave, s32 duration, Channel* c, s32 volume, s32 speed) static void channelSfx(tic_mem* tic, s32 index, s32 note, s32 octave, s32 duration, Channel* c, s32 volume, s32 speed)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
c->volume = volume; c->volume = volume;
@ -401,34 +401,34 @@ static void channelSfx(tic_mem* memory, s32 index, s32 note, s32 octave, s32 dur
resetSfx(c); resetSfx(c);
} }
static void musicSfx(tic_mem* memory, s32 index, s32 note, s32 octave, s32 volume, s32 channel) static void musicSfx(tic_mem* tic, s32 index, s32 note, s32 octave, s32 volume, s32 channel)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
channelSfx(memory, index, note, octave, -1, &machine->state.music.channels[channel], MAX_VOLUME - volume, SFX_DEF_SPEED); channelSfx(tic, index, note, octave, -1, &machine->state.music.channels[channel], MAX_VOLUME - volume, SFX_DEF_SPEED);
} }
static void resetMusic(tic_mem* memory) static void resetMusic(tic_mem* tic)
{ {
for (s32 c = 0; c < TIC_SOUND_CHANNELS; c++) for (s32 c = 0; c < TIC_SOUND_CHANNELS; c++)
musicSfx(memory, -1, 0, 0, 0, c); musicSfx(tic, -1, 0, 0, 0, c);
} }
static void setMusic(tic_machine* machine, s32 index, s32 frame, s32 row, bool loop) static void setMusic(tic_machine* machine, s32 index, s32 frame, s32 row, bool loop)
{ {
tic_mem* memory = (tic_mem*)machine; tic_mem* tic = (tic_mem*)machine;
memory->ram.music_pos.track = index; tic->ram.music_pos.track = index;
if(index < 0) if(index < 0)
{ {
machine->state.music.play = MusicStop; machine->state.music.play = MusicStop;
resetMusic(memory); resetMusic(tic);
} }
else else
{ {
memory->ram.music_pos.row = row; tic->ram.music_pos.row = row;
memory->ram.music_pos.frame = frame < 0 ? 0 : frame; tic->ram.music_pos.frame = frame < 0 ? 0 : frame;
memory->ram.music_pos.flag.loop = loop; tic->ram.music_pos.flag.loop = loop;
machine->state.music.play = MusicPlay; machine->state.music.play = MusicPlay;
const tic_track* track = &machine->sound.music->tracks.data[index]; const tic_track* track = &machine->sound.music->tracks.data[index];
@ -436,9 +436,9 @@ static void setMusic(tic_machine* machine, s32 index, s32 frame, s32 row, bool l
} }
} }
static void api_music(tic_mem* memory, s32 index, s32 frame, s32 row, bool loop) static void api_music(tic_mem* tic, s32 index, s32 frame, s32 row, bool loop)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
setMusic(machine, index, frame, row, loop); setMusic(machine, index, frame, row, loop);
@ -446,10 +446,10 @@ static void api_music(tic_mem* memory, s32 index, s32 frame, s32 row, bool loop)
machine->state.music.play = MusicPlay; machine->state.music.play = MusicPlay;
} }
static void soundClear(tic_mem* memory) static void soundClear(tic_mem* tic)
{ {
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
Channel channel = Channel channel =
{ {
@ -474,7 +474,7 @@ static void soundClear(tic_mem* memory)
memcpy(machine->state.music.channels+i, &channel, sizeof channel); memcpy(machine->state.music.channels+i, &channel, sizeof channel);
{ {
tic_sound_register* reg = &memory->ram.registers[i]; tic_sound_register* reg = &tic->ram.registers[i];
memset(reg, 0, sizeof(tic_sound_register)); memset(reg, 0, sizeof(tic_sound_register));
} }
@ -484,17 +484,17 @@ static void soundClear(tic_mem* memory)
} }
} }
api_music(memory, -1, 0, 0, false); api_music(tic, -1, 0, 0, false);
} }
memset(memory->samples.buffer, 0, memory->samples.size); memset(tic->samples.buffer, 0, tic->samples.size);
} }
static void updateSaveid(tic_mem* memory); static void updateSaveid(tic_mem* tic);
static void api_clip(tic_mem* memory, s32 x, s32 y, s32 width, s32 height) static void api_clip(tic_mem* tic, s32 x, s32 y, s32 width, s32 height)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
machine->state.clip.l = x; machine->state.clip.l = x;
machine->state.clip.t = y; machine->state.clip.t = y;
@ -507,17 +507,17 @@ static void api_clip(tic_mem* memory, s32 x, s32 y, s32 width, s32 height)
if(machine->state.clip.b > TIC80_HEIGHT) machine->state.clip.b = TIC80_HEIGHT; if(machine->state.clip.b > TIC80_HEIGHT) machine->state.clip.b = TIC80_HEIGHT;
} }
static void api_reset(tic_mem* memory) static void api_reset(tic_mem* tic)
{ {
resetPalette(memory); resetPalette(tic);
memset(&memory->ram.vram.vars, 0, sizeof memory->ram.vram.vars); memset(&tic->ram.vram.vars, 0, sizeof tic->ram.vram.vars);
api_clip(memory, 0, 0, TIC80_WIDTH, TIC80_HEIGHT); api_clip(tic, 0, 0, TIC80_WIDTH, TIC80_HEIGHT);
soundClear(memory); soundClear(tic);
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
machine->state.initialized = false; machine->state.initialized = false;
machine->state.scanline = NULL; machine->state.scanline = NULL;
machine->state.ovr.callback = NULL; machine->state.ovr.callback = NULL;
@ -526,60 +526,60 @@ static void api_reset(tic_mem* memory)
machine->state.getpix = getPixelDma; machine->state.getpix = getPixelDma;
machine->state.drawhline = drawHLineDma; machine->state.drawhline = drawHLineDma;
updateSaveid(memory); updateSaveid(tic);
} }
static void api_pause(tic_mem* memory) static void api_pause(tic_mem* tic)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
memcpy(&machine->pause.state, &machine->state, sizeof(MachineState)); memcpy(&machine->pause.state, &machine->state, sizeof(MachineState));
memcpy(&machine->pause.ram, &memory->ram, sizeof(tic_ram)); memcpy(&machine->pause.ram, &tic->ram, sizeof(tic_ram));
machine->pause.time.start = machine->data->start; machine->pause.time.start = machine->data->start;
machine->pause.time.paused = machine->data->counter(); machine->pause.time.paused = machine->data->counter();
} }
static void api_resume(tic_mem* memory) static void api_resume(tic_mem* tic)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
if (machine->data) if (machine->data)
{ {
memcpy(&machine->state, &machine->pause.state, sizeof(MachineState)); memcpy(&machine->state, &machine->pause.state, sizeof(MachineState));
memcpy(&memory->ram, &machine->pause.ram, sizeof(tic_ram)); memcpy(&tic->ram, &machine->pause.ram, sizeof(tic_ram));
machine->data->start = machine->pause.time.start + machine->data->counter() - machine->pause.time.paused; machine->data->start = machine->pause.time.start + machine->data->counter() - machine->pause.time.paused;
} }
} }
void tic_close(tic_mem* memory) void tic_close(tic_mem* tic)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
machine->state.initialized = false; machine->state.initialized = false;
#if defined(TIC_BUILD_WITH_LUA) #if defined(TIC_BUILD_WITH_LUA)
getLuaScriptConfig()->close(memory); getLuaScriptConfig()->close(tic);
# if defined(TIC_BUILD_WITH_MOON) # if defined(TIC_BUILD_WITH_MOON)
getMoonScriptConfig()->close(memory); getMoonScriptConfig()->close(tic);
# endif # endif
#endif /* defined(TIC_BUILD_WITH_LUA) */ #endif /* defined(TIC_BUILD_WITH_LUA) */
#if defined(TIC_BUILD_WITH_JS) #if defined(TIC_BUILD_WITH_JS)
getJsScriptConfig()->close(memory); getJsScriptConfig()->close(tic);
#endif #endif
#if defined(TIC_BUILD_WITH_WREN) #if defined(TIC_BUILD_WITH_WREN)
getWrenScriptConfig()->close(memory); getWrenScriptConfig()->close(tic);
#endif #endif
blip_delete(machine->blip); blip_delete(machine->blip);
free(memory->samples.buffer); free(tic->samples.buffer);
free(machine); free(machine);
} }
@ -587,49 +587,49 @@ void tic_close(tic_mem* memory)
// API //////////////////////////////////////////////////////////////////////// // API ////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static void api_rect(tic_mem* memory, s32 x, s32 y, s32 width, s32 height, u8 color) static void api_rect(tic_mem* tic, s32 x, s32 y, s32 width, s32 height, u8 color)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
drawRect(machine, x, y, width, height, color); drawRect(machine, x, y, width, height, color);
} }
static void api_clear(tic_mem* memory, u8 color) static void api_clear(tic_mem* tic, u8 color)
{ {
static const Clip EmptyClip = {0, 0, TIC80_WIDTH, TIC80_HEIGHT}; static const Clip EmptyClip = {0, 0, TIC80_WIDTH, TIC80_HEIGHT};
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
if(memcmp(&machine->state.clip, &EmptyClip, sizeof(Clip)) == 0) if(memcmp(&machine->state.clip, &EmptyClip, sizeof(Clip)) == 0)
{ {
color &= 0b00001111; color &= 0b00001111;
memset(memory->ram.vram.screen.data, color | (color << TIC_PALETTE_BPP), sizeof(memory->ram.vram.screen.data)); memset(tic->ram.vram.screen.data, color | (color << TIC_PALETTE_BPP), sizeof(tic->ram.vram.screen.data));
} }
else else
{ {
api_rect(memory, machine->state.clip.l, machine->state.clip.t, machine->state.clip.r - machine->state.clip.l, machine->state.clip.b - machine->state.clip.t, color); api_rect(tic, machine->state.clip.l, machine->state.clip.t, machine->state.clip.r - machine->state.clip.l, machine->state.clip.b - machine->state.clip.t, color);
} }
} }
static s32 drawChar(tic_mem* memory, u8 symbol, s32 x, s32 y, s32 width, s32 height, u8 color, s32 scale) static s32 drawChar(tic_mem* tic, u8 symbol, s32 x, s32 y, s32 width, s32 height, u8 color, s32 scale)
{ {
const u8* ptr = memory->font.data + symbol*BITS_IN_BYTE; const u8* ptr = tic->font.data + symbol*BITS_IN_BYTE;
x += (BITS_IN_BYTE - 1)*scale; x += (BITS_IN_BYTE - 1)*scale;
for(s32 i = 0, ys = y; i < TIC_FONT_HEIGHT; i++, ptr++, ys += scale) for(s32 i = 0, ys = y; i < tic->font.height; i++, ptr++, ys += scale)
for(s32 col = BITS_IN_BYTE - TIC_FONT_WIDTH, xs = x - col; col < BITS_IN_BYTE; col++, xs -= scale) for(s32 col = BITS_IN_BYTE - tic->font.width, xs = x - col; col < BITS_IN_BYTE; col++, xs -= scale)
if(*ptr & 1 << col) if(*ptr & 1 << col)
api_rect(memory, xs, ys, scale, scale, color); api_rect(tic, xs, ys, scale, scale, color);
return TIC_FONT_WIDTH*scale; return tic->font.width*scale;
} }
static s32 api_draw_char(tic_mem* memory, u8 symbol, s32 x, s32 y, u8 color) static s32 api_draw_char(tic_mem* tic, u8 symbol, s32 x, s32 y, u8 color)
{ {
return drawChar(memory, symbol, x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT, color, 1); return drawChar(tic, symbol, x, y, tic->font.width, tic->font.height, color, 1);
} }
s32 drawText(tic_mem* memory, const char* text, s32 x, s32 y, s32 width, s32 height, u8 color, s32 scale, DrawCharFunc* func) s32 drawText(tic_mem* tic, const char* text, s32 x, s32 y, s32 width, s32 height, u8 color, s32 scale, DrawCharFunc* func)
{ {
s32 pos = x; s32 pos = x;
s32 max = x; s32 max = x;
@ -645,69 +645,69 @@ s32 drawText(tic_mem* memory, const char* text, s32 x, s32 y, s32 width, s32 hei
pos = x; pos = x;
y += height * scale; y += height * scale;
} }
else pos += func(memory, sym, pos, y, width, height, color, scale); else pos += func(tic, sym, pos, y, width, height, color, scale);
} }
return pos > max ? pos - x : max - x; return pos > max ? pos - x : max - x;
} }
static s32 api_fixed_text(tic_mem* memory, const char* text, s32 x, s32 y, u8 color) static s32 api_fixed_text(tic_mem* tic, const char* text, s32 x, s32 y, u8 color)
{ {
return drawText(memory, text, x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT, color, 1, drawChar); return drawText(tic, text, x, y, tic->font.width, tic->font.height, color, 1, drawChar);
} }
static s32 drawNonFixedChar(tic_mem* memory, u8 symbol, s32 x, s32 y, s32 width, s32 height, u8 color, s32 scale) static s32 drawNonFixedChar(tic_mem* tic, u8 symbol, s32 x, s32 y, s32 width, s32 height, u8 color, s32 scale)
{ {
const u8* ptr = memory->font.data + (symbol)*BITS_IN_BYTE; const u8* ptr = tic->font.data + (symbol)*BITS_IN_BYTE;
s32 start = 0; s32 start = 0;
s32 end = TIC_FONT_WIDTH; s32 end = tic->font.width;
s32 i = 0; s32 i = 0;
for(s32 col = 0; col < TIC_FONT_WIDTH; col++) for(s32 col = 0; col < tic->font.width; col++)
{ {
for(i = 0; i < TIC_FONT_HEIGHT; i++) for(i = 0; i < tic->font.height; i++)
if(*(ptr + i) & 0b10000000 >> col) break; if(*(ptr + i) & 0b10000000 >> col) break;
if(i < TIC_FONT_HEIGHT) break; else start++; if(i < tic->font.height) break; else start++;
} }
x -= start * scale; x -= start * scale;
for(s32 col = TIC_FONT_WIDTH - 1; col >= start; col--) for(s32 col = tic->font.width - 1; col >= start; col--)
{ {
for(i = 0; i < TIC_FONT_HEIGHT; i++) for(i = 0; i < tic->font.height; i++)
if(*(ptr + i) & 0b10000000 >> col) break; if(*(ptr + i) & 0b10000000 >> col) break;
if(i < TIC_FONT_HEIGHT) break; else end--; if(i < tic->font.height) break; else end--;
} }
for(s32 ys = y, i = 0; i < TIC_FONT_HEIGHT; i++, ptr++, ys += scale) for(s32 ys = y, i = 0; i < tic->font.height; i++, ptr++, ys += scale)
for(s32 col = start, xs = x + start*scale; col < end; col++, xs += scale) for(s32 col = start, xs = x + start*scale; col < end; col++, xs += scale)
if(*ptr & 0b10000000 >> col) if(*ptr & 0b10000000 >> col)
api_rect(memory, xs, ys, scale, scale, color); api_rect(tic, xs, ys, scale, scale, color);
s32 size = end - start; s32 size = end - start;
return (size ? size + 1 : TIC_FONT_WIDTH - 2) * scale; return (size ? size + 1 : tic->font.width - 2) * scale;
} }
static s32 api_text(tic_mem* memory, const char* text, s32 x, s32 y, u8 color) static s32 api_text(tic_mem* tic, const char* text, s32 x, s32 y, u8 color)
{ {
return drawText(memory, text, x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT, color, 1, drawNonFixedChar); return drawText(tic, text, x, y, tic->font.width, tic->font.height, color, 1, drawNonFixedChar);
} }
static s32 api_text_ex(tic_mem* memory, const char* text, s32 x, s32 y, u8 color, bool fixed, s32 scale) static s32 api_text_ex(tic_mem* tic, const char* text, s32 x, s32 y, u8 color, bool fixed, s32 scale)
{ {
return drawText(memory, text, x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT, color, scale, fixed ? drawChar : drawNonFixedChar); return drawText(tic, text, x, y, tic->font.width, tic->font.height, color, scale, fixed ? drawChar : drawNonFixedChar);
} }
static void drawSprite(tic_mem* memory, const tic_tiles* src, s32 index, s32 x, s32 y, u8* colors, s32 count, s32 scale, tic_flip flip, tic_rotate rotate) static void drawSprite(tic_mem* tic, const tic_tiles* src, s32 index, s32 x, s32 y, u8* colors, s32 count, s32 scale, tic_flip flip, tic_rotate rotate)
{ {
if(index < TIC_SPRITES) if(index < TIC_SPRITES)
drawTile((tic_machine*)memory, src->data + index, x, y, colors, count, scale, flip, rotate); drawTile((tic_machine*)tic, src->data + index, x, y, colors, count, scale, flip, rotate);
} }
static void api_sprite_ex(tic_mem* memory, const tic_tiles* src, s32 index, s32 x, s32 y, s32 w, s32 h, u8* colors, s32 count, s32 scale, tic_flip flip, tic_rotate rotate) static void api_sprite_ex(tic_mem* tic, const tic_tiles* src, s32 index, s32 x, s32 y, s32 w, s32 h, u8* colors, s32 count, s32 scale, tic_flip flip, tic_rotate rotate)
{ {
s32 step = TIC_SPRITESIZE * scale; s32 step = TIC_SPRITESIZE * scale;
@ -742,23 +742,23 @@ static void api_sprite_ex(tic_mem* memory, const tic_tiles* src, s32 index, s32
enum {Cols = TIC_SPRITESHEET_SIZE / TIC_SPRITESIZE}; enum {Cols = TIC_SPRITESHEET_SIZE / TIC_SPRITESIZE};
if(rotate==0 || rotate==2) if(rotate==0 || rotate==2)
drawSprite(memory, src, index + mx+my*Cols, x+i*step, y+j*step, colors, count, scale, flip, rotate); drawSprite(tic, src, index + mx+my*Cols, x+i*step, y+j*step, colors, count, scale, flip, rotate);
else else
drawSprite(memory, src, index + mx+my*Cols, x+j*step, y+i*step, colors, count, scale, flip, rotate); drawSprite(tic, src, index + mx+my*Cols, x+j*step, y+i*step, colors, count, scale, flip, rotate);
} }
} }
} }
s32 drawSpriteFont(tic_mem* memory, u8 symbol, s32 x, s32 y, s32 width, s32 height, u8 chromakey, s32 scale) s32 drawSpriteFont(tic_mem* tic, u8 symbol, s32 x, s32 y, s32 width, s32 height, u8 chromakey, s32 scale)
{ {
api_sprite_ex(memory, &memory->ram.sprites, symbol, x, y, 1, 1, &chromakey, 1, scale, tic_no_flip, tic_no_rotate); api_sprite_ex(tic, &tic->ram.sprites, symbol, x, y, 1, 1, &chromakey, 1, scale, tic_no_flip, tic_no_rotate);
return width * scale; return width * scale;
} }
s32 drawFixedSpriteFont(tic_mem* memory, u8 index, s32 x, s32 y, s32 width, s32 height, u8 chromakey, s32 scale) s32 drawFixedSpriteFont(tic_mem* tic, u8 index, s32 x, s32 y, s32 width, s32 height, u8 chromakey, s32 scale)
{ {
const u8* ptr = memory->ram.sprites.data[index].data; const u8* ptr = tic->ram.sprites.data[index].data;
enum {Size = TIC_SPRITESIZE}; enum {Size = TIC_SPRITESIZE};
@ -791,7 +791,7 @@ s32 drawFixedSpriteFont(tic_mem* memory, u8 index, s32 x, s32 y, s32 width, s32
u8 color = tic_tool_peek4(ptr, col + row * Size); u8 color = tic_tool_peek4(ptr, col + row * Size);
if(color != chromakey) if(color != chromakey)
api_rect(memory, xs, ys, scale, scale, color); api_rect(tic, xs, ys, scale, scale, color);
} }
} }
@ -799,23 +799,23 @@ s32 drawFixedSpriteFont(tic_mem* memory, u8 index, s32 x, s32 y, s32 width, s32
return (size ? size + 1 : width) * scale; return (size ? size + 1 : width) * scale;
} }
static void api_pixel(tic_mem* memory, s32 x, s32 y, u8 color) static void api_pixel(tic_mem* tic, s32 x, s32 y, u8 color)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
setPixel(machine, x, y, color); setPixel(machine, x, y, color);
} }
static u8 api_get_pixel(tic_mem* memory, s32 x, s32 y) static u8 api_get_pixel(tic_mem* tic, s32 x, s32 y)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
return getPixel(machine, x, y); return getPixel(machine, x, y);
} }
static void api_rect_border(tic_mem* memory, s32 x, s32 y, s32 width, s32 height, u8 color) static void api_rect_border(tic_mem* tic, s32 x, s32 y, s32 width, s32 height, u8 color)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
drawRectBorder(machine, x, y, width, height, color); drawRectBorder(machine, x, y, width, height, color);
} }
@ -861,9 +861,9 @@ static void setSideTexPixel(s32 x, s32 y, float u, float v)
} }
} }
static void api_circle(tic_mem* memory, s32 xm, s32 ym, s32 radius, u8 color) static void api_circle(tic_mem* tic, s32 xm, s32 ym, s32 radius, u8 color)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
initSidesBuffer(); initSidesBuffer();
@ -891,23 +891,23 @@ static void api_circle(tic_mem* memory, s32 xm, s32 ym, s32 radius, u8 color)
} }
} }
static void api_circle_border(tic_mem* memory, s32 xm, s32 ym, s32 radius, u8 color) static void api_circle_border(tic_mem* tic, s32 xm, s32 ym, s32 radius, u8 color)
{ {
s32 r = radius; s32 r = radius;
s32 x = -r, y = 0, err = 2-2*r; s32 x = -r, y = 0, err = 2-2*r;
do { do {
api_pixel(memory, xm-x, ym+y, color); api_pixel(tic, xm-x, ym+y, color);
api_pixel(memory, xm-y, ym-x, color); api_pixel(tic, xm-y, ym-x, color);
api_pixel(memory, xm+x, ym-y, color); api_pixel(tic, xm+x, ym-y, color);
api_pixel(memory, xm+y, ym+x, color); api_pixel(tic, xm+y, ym+x, color);
r = err; r = err;
if (r <= y) err += ++y*2+1; if (r <= y) err += ++y*2+1;
if (r > x || err > y) err += ++x*2+1; if (r > x || err > y) err += ++x*2+1;
} while (x < 0); } while (x < 0);
} }
typedef void(*linePixelFunc)(tic_mem* memory, s32 x, s32 y, u8 color); typedef void(*linePixelFunc)(tic_mem* tic, s32 x, s32 y, u8 color);
static void ticLine(tic_mem* memory, s32 x0, s32 y0, s32 x1, s32 y1, u8 color, linePixelFunc func) static void ticLine(tic_mem* tic, s32 x0, s32 y0, s32 x1, s32 y1, u8 color, linePixelFunc func)
{ {
s32 dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1; s32 dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
s32 dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1; s32 dy = abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
@ -915,7 +915,7 @@ static void ticLine(tic_mem* memory, s32 x0, s32 y0, s32 x1, s32 y1, u8 color, l
for(;;) for(;;)
{ {
func(memory, x0, y0, color); func(tic, x0, y0, color);
if (x0 == x1 && y0 == y1) break; if (x0 == x1 && y0 == y1) break;
e2 = err; e2 = err;
if (e2 >-dx) { err -= dy; x0 += sx; } if (e2 >-dx) { err -= dy; x0 += sx; }
@ -923,20 +923,20 @@ static void ticLine(tic_mem* memory, s32 x0, s32 y0, s32 x1, s32 y1, u8 color, l
} }
} }
static void triPixelFunc(tic_mem* memory, s32 x, s32 y, u8 color) static void triPixelFunc(tic_mem* tic, s32 x, s32 y, u8 color)
{ {
setSidePixel(x, y); setSidePixel(x, y);
} }
static void api_tri(tic_mem* memory, s32 x1, s32 y1, s32 x2, s32 y2, s32 x3, s32 y3, u8 color) static void api_tri(tic_mem* tic, s32 x1, s32 y1, s32 x2, s32 y2, s32 x3, s32 y3, u8 color)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
initSidesBuffer(); initSidesBuffer();
ticLine(memory, x1, y1, x2, y2, color, triPixelFunc); ticLine(tic, x1, y1, x2, y2, color, triPixelFunc);
ticLine(memory, x2, y2, x3, y3, color, triPixelFunc); ticLine(tic, x2, y2, x3, y3, color, triPixelFunc);
ticLine(memory, x3, y3, x1, y1, color, triPixelFunc); ticLine(tic, x3, y3, x1, y1, color, triPixelFunc);
u8 final_color = mapColor(&machine->memory, color); u8 final_color = mapColor(&machine->memory, color);
s32 yt = max(machine->state.clip.t, min(y1, min(y2, y3))); s32 yt = max(machine->state.clip.t, min(y1, min(y2, y3)));
@ -956,7 +956,7 @@ typedef struct
} TexVert; } TexVert;
static void ticTexLine(tic_mem* memory, TexVert *v0, TexVert *v1) static void ticTexLine(tic_mem* tic, TexVert *v0, TexVert *v1)
{ {
TexVert *top = v0; TexVert *top = v0;
TexVert *bot = v1; TexVert *bot = v1;
@ -1008,12 +1008,12 @@ static void ticTexLine(tic_mem* memory, TexVert *v0, TexVert *v1)
} }
} }
static void api_textri(tic_mem* memory, float x1, float y1, float x2, float y2, float x3, float y3, float u1, float v1, float u2, float v2, float u3, float v3, bool use_map, u8 chroma) static void api_textri(tic_mem* tic, float x1, float y1, float x2, float y2, float x3, float y3, float u1, float v1, float u2, float v2, float u3, float v3, bool use_map, u8 chroma)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
TexVert V0, V1, V2; TexVert V0, V1, V2;
const u8* ptr = memory->ram.tiles.data[0].data; const u8* ptr = tic->ram.tiles.data[0].data;
const u8* map = memory->ram.map.data; const u8* map = tic->ram.map.data;
V0.x = x1; V0.y = y1; V0.u = u1; V0.v = v1; V0.x = x1; V0.y = y1; V0.u = u1; V0.v = v1;
V1.x = x2; V1.y = y2; V1.u = u2; V1.v = v2; V1.x = x2; V1.y = y2; V1.u = u2; V1.v = v2;
@ -1037,9 +1037,9 @@ static void api_textri(tic_mem* memory, float x1, float y1, float x2, float y2,
// fill the buffer // fill the buffer
initSidesBuffer(); initSidesBuffer();
// parse each line and decide where in the buffer to store them ( left or right ) // parse each line and decide where in the buffer to store them ( left or right )
ticTexLine(memory, &V0, &V1); ticTexLine(tic, &V0, &V1);
ticTexLine(memory, &V1, &V2); ticTexLine(tic, &V1, &V2);
ticTexLine(memory, &V2, &V0); ticTexLine(tic, &V2, &V0);
for (s32 y = 0; y < TIC80_HEIGHT; y++) for (s32 y = 0; y < TIC80_HEIGHT; y++)
{ {
@ -1106,38 +1106,38 @@ static void api_textri(tic_mem* memory, float x1, float y1, float x2, float y2,
} }
static void api_sprite(tic_mem* memory, const tic_tiles* src, s32 index, s32 x, s32 y, u8* colors, s32 count) static void api_sprite(tic_mem* tic, const tic_tiles* src, s32 index, s32 x, s32 y, u8* colors, s32 count)
{ {
drawSprite(memory, src, index, x, y, colors, count, 1, tic_no_flip, tic_no_rotate); drawSprite(tic, src, index, x, y, colors, count, 1, tic_no_flip, tic_no_rotate);
} }
static void api_map(tic_mem* memory, const tic_map* src, const tic_tiles* tiles, s32 x, s32 y, s32 width, s32 height, s32 sx, s32 sy, u8 chromakey, s32 scale) static void api_map(tic_mem* tic, const tic_map* src, const tic_tiles* tiles, s32 x, s32 y, s32 width, s32 height, s32 sx, s32 sy, u8 chromakey, s32 scale)
{ {
drawMap((tic_machine*)memory, src, tiles, x, y, width, height, sx, sy, chromakey, scale, NULL, NULL); drawMap((tic_machine*)tic, src, tiles, x, y, width, height, sx, sy, chromakey, scale, NULL, NULL);
} }
static void api_remap(tic_mem* memory, const tic_map* src, const tic_tiles* tiles, s32 x, s32 y, s32 width, s32 height, s32 sx, s32 sy, u8 chromakey, s32 scale, RemapFunc remap, void* data) static void api_remap(tic_mem* tic, const tic_map* src, const tic_tiles* tiles, s32 x, s32 y, s32 width, s32 height, s32 sx, s32 sy, u8 chromakey, s32 scale, RemapFunc remap, void* data)
{ {
drawMap((tic_machine*)memory, src, tiles, x, y, width, height, sx, sy, chromakey, scale, remap, data); drawMap((tic_machine*)tic, src, tiles, x, y, width, height, sx, sy, chromakey, scale, remap, data);
} }
static void api_map_set(tic_mem* memory, tic_map* src, s32 x, s32 y, u8 value) static void api_map_set(tic_mem* tic, tic_map* src, s32 x, s32 y, u8 value)
{ {
if(x < 0 || x >= TIC_MAP_WIDTH || y < 0 || y >= TIC_MAP_HEIGHT) return; if(x < 0 || x >= TIC_MAP_WIDTH || y < 0 || y >= TIC_MAP_HEIGHT) return;
*(src->data + y * TIC_MAP_WIDTH + x) = value; *(src->data + y * TIC_MAP_WIDTH + x) = value;
} }
static u8 api_map_get(tic_mem* memory, const tic_map* src, s32 x, s32 y) static u8 api_map_get(tic_mem* tic, const tic_map* src, s32 x, s32 y)
{ {
if(x < 0 || x >= TIC_MAP_WIDTH || y < 0 || y >= TIC_MAP_HEIGHT) return 0; if(x < 0 || x >= TIC_MAP_WIDTH || y < 0 || y >= TIC_MAP_HEIGHT) return 0;
return *(src->data + y * TIC_MAP_WIDTH + x); return *(src->data + y * TIC_MAP_WIDTH + x);
} }
static void api_line(tic_mem* memory, s32 x0, s32 y0, s32 x1, s32 y1, u8 color) static void api_line(tic_mem* tic, s32 x0, s32 y0, s32 x1, s32 y1, u8 color)
{ {
ticLine(memory, x0, y0, x1, y1, color, api_pixel); ticLine(tic, x0, y0, x1, y1, color, api_pixel);
} }
static s32 calcLoopPos(const tic_sound_loop* loop, s32 pos) static s32 calcLoopPos(const tic_sound_loop* loop, s32 pos)
@ -1158,9 +1158,9 @@ static s32 calcLoopPos(const tic_sound_loop* loop, s32 pos)
return offset; return offset;
} }
static void sfx(tic_mem* memory, s32 index, s32 freq, Channel* channel, tic_sound_register* reg) static void sfx(tic_mem* tic, s32 index, s32 freq, Channel* channel, tic_sound_register* reg)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
if(channel->duration > 0) if(channel->duration > 0)
channel->duration--; channel->duration--;
@ -1204,13 +1204,13 @@ static void sfx(tic_mem* memory, s32 index, s32 freq, Channel* channel, tic_soun
} }
} }
static void processMusic(tic_mem* memory) static void processMusic(tic_mem* tic)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
if(machine->state.music.play == MusicStop) return; if(machine->state.music.play == MusicStop) return;
const tic_track* track = &machine->sound.music->tracks.data[memory->ram.music_pos.track]; const tic_track* track = &machine->sound.music->tracks.data[tic->ram.music_pos.track];
s32 row = machine->state.music.ticks * (track->tempo + DEFAULT_TEMPO) * DEFAULT_SPEED / (track->speed + DEFAULT_SPEED) / NOTES_PER_MUNUTE; s32 row = machine->state.music.ticks * (track->tempo + DEFAULT_TEMPO) * DEFAULT_SPEED / (track->speed + DEFAULT_SPEED) / NOTES_PER_MUNUTE;
s32 rows = MUSIC_PATTERN_ROWS - track->rows; s32 rows = MUSIC_PATTERN_ROWS - track->rows;
@ -1218,19 +1218,19 @@ static void processMusic(tic_mem* memory)
{ {
row = 0; row = 0;
machine->state.music.ticks = 0; machine->state.music.ticks = 0;
resetMusic(memory); resetMusic(tic);
if(machine->state.music.play == MusicPlay) if(machine->state.music.play == MusicPlay)
{ {
memory->ram.music_pos.frame++; tic->ram.music_pos.frame++;
if(memory->ram.music_pos.frame >= MUSIC_FRAMES) if(tic->ram.music_pos.frame >= MUSIC_FRAMES)
{ {
if(memory->ram.music_pos.flag.loop) if(tic->ram.music_pos.flag.loop)
memory->ram.music_pos.frame = 0; tic->ram.music_pos.frame = 0;
else else
{ {
api_music(memory, -1, 0, 0, false); api_music(tic, -1, 0, 0, false);
return; return;
} }
} }
@ -1238,15 +1238,15 @@ static void processMusic(tic_mem* memory)
{ {
s32 val = 0; s32 val = 0;
for (s32 c = 0; c < TIC_SOUND_CHANNELS; c++) for (s32 c = 0; c < TIC_SOUND_CHANNELS; c++)
val += tic_tool_get_pattern_id(track, memory->ram.music_pos.frame, c); val += tic_tool_get_pattern_id(track, tic->ram.music_pos.frame, c);
if(!val) if(!val)
{ {
if(memory->ram.music_pos.flag.loop) if(tic->ram.music_pos.flag.loop)
memory->ram.music_pos.frame = 0; tic->ram.music_pos.frame = 0;
else else
{ {
api_music(memory, -1, 0, 0, false); api_music(tic, -1, 0, 0, false);
return; return;
} }
} }
@ -1254,37 +1254,37 @@ static void processMusic(tic_mem* memory)
} }
else if(machine->state.music.play == MusicPlayFrame) else if(machine->state.music.play == MusicPlayFrame)
{ {
if(!memory->ram.music_pos.flag.loop) if(!tic->ram.music_pos.flag.loop)
{ {
api_music(memory, -1, 0, 0, false); api_music(tic, -1, 0, 0, false);
return; return;
} }
} }
} }
if (row != memory->ram.music_pos.row) if (row != tic->ram.music_pos.row)
{ {
memory->ram.music_pos.row = row; tic->ram.music_pos.row = row;
for (s32 channel = 0; channel < TIC_SOUND_CHANNELS; channel++) for (s32 channel = 0; channel < TIC_SOUND_CHANNELS; channel++)
{ {
s32 patternId = tic_tool_get_pattern_id(track, memory->ram.music_pos.frame, channel); s32 patternId = tic_tool_get_pattern_id(track, tic->ram.music_pos.frame, channel);
if (!patternId) continue; if (!patternId) continue;
const tic_track_pattern* pattern = &machine->sound.music->patterns.data[patternId - PATTERN_START]; const tic_track_pattern* pattern = &machine->sound.music->patterns.data[patternId - PATTERN_START];
s32 note = pattern->rows[memory->ram.music_pos.row].note; s32 note = pattern->rows[tic->ram.music_pos.row].note;
if (note > NoteNone) if (note > NoteNone)
{ {
musicSfx(memory, -1, 0, 0, 0, channel); musicSfx(tic, -1, 0, 0, 0, channel);
if (note >= NoteStart) if (note >= NoteStart)
{ {
s32 octave = pattern->rows[memory->ram.music_pos.row].octave; s32 octave = pattern->rows[tic->ram.music_pos.row].octave;
s32 sfx = (pattern->rows[row].sfxhi << MUSIC_SFXID_LOW_BITS) | pattern->rows[row].sfxlow; s32 sfx = (pattern->rows[row].sfxhi << MUSIC_SFXID_LOW_BITS) | pattern->rows[row].sfxlow;
s32 volume = pattern->rows[memory->ram.music_pos.row].volume; s32 volume = pattern->rows[tic->ram.music_pos.row].volume;
musicSfx(memory, sfx, note - NoteStart, octave, volume, channel); musicSfx(tic, sfx, note - NoteStart, octave, volume, channel);
} }
} }
} }
@ -1295,7 +1295,7 @@ static void processMusic(tic_mem* memory)
Channel* c = &machine->state.music.channels[i]; Channel* c = &machine->state.music.channels[i];
if(c->index >= 0) if(c->index >= 0)
sfx(memory, c->index, c->freq, c, &memory->ram.registers[i]); sfx(tic, c->index, c->freq, c, &tic->ram.registers[i]);
} }
machine->state.music.ticks++; machine->state.music.ticks++;
@ -1317,24 +1317,24 @@ static bool isKeyPressed(const tic80_keyboard* input, tic_key key)
return false; return false;
} }
static void api_tick_start(tic_mem* memory, const tic_sfx* sfxsrc, const tic_music* music) static void api_tick_start(tic_mem* tic, const tic_sfx* sfxsrc, const tic_music* music)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
machine->sound.sfx = sfxsrc; machine->sound.sfx = sfxsrc;
machine->sound.music = music; machine->sound.music = music;
for (s32 i = 0; i < TIC_SOUND_CHANNELS; ++i ) for (s32 i = 0; i < TIC_SOUND_CHANNELS; ++i )
memset(&memory->ram.registers[i], 0, sizeof(tic_sound_register)); memset(&tic->ram.registers[i], 0, sizeof(tic_sound_register));
processMusic(memory); processMusic(tic);
for (s32 i = 0; i < TIC_SOUND_CHANNELS; ++i ) for (s32 i = 0; i < TIC_SOUND_CHANNELS; ++i )
{ {
Channel* c = &machine->state.channels[i]; Channel* c = &machine->state.channels[i];
if(c->index >= 0) if(c->index >= 0)
sfx(memory, c->index, c->freq, c, &memory->ram.registers[i]); sfx(tic, c->index, c->freq, c, &tic->ram.registers[i]);
} }
// process gamepad // process gamepad
@ -1342,7 +1342,7 @@ static void api_tick_start(tic_mem* memory, const tic_sfx* sfxsrc, const tic_mus
{ {
u32 mask = 1 << i; u32 mask = 1 << i;
u32 prevDown = machine->state.gamepads.previous.data & mask; u32 prevDown = machine->state.gamepads.previous.data & mask;
u32 down = memory->ram.input.gamepads.data & mask; u32 down = tic->ram.input.gamepads.data & mask;
u32* hold = &machine->state.gamepads.holds[i]; u32* hold = &machine->state.gamepads.holds[i];
if(prevDown && prevDown == down) (*hold)++; if(prevDown && prevDown == down) (*hold)++;
@ -1353,7 +1353,7 @@ static void api_tick_start(tic_mem* memory, const tic_sfx* sfxsrc, const tic_mus
for(s32 i = 0; i < tic_keys_count; i++) for(s32 i = 0; i < tic_keys_count; i++)
{ {
bool prevDown = isKeyPressed(&machine->state.keyboard.previous, i); bool prevDown = isKeyPressed(&machine->state.keyboard.previous, i);
bool down = isKeyPressed(&memory->ram.input.keyboard, i); bool down = isKeyPressed(&tic->ram.input.keyboard, i);
u32* hold = &machine->state.keyboard.holds[i]; u32* hold = &machine->state.keyboard.holds[i];
@ -1367,9 +1367,9 @@ static void api_tick_start(tic_mem* memory, const tic_sfx* sfxsrc, const tic_mus
machine->state.drawhline = drawHLineDma; machine->state.drawhline = drawHLineDma;
} }
static void api_tick_end(tic_mem* memory) static void api_tick_end(tic_mem* tic)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
machine->state.gamepads.previous.data = machine->memory.ram.input.gamepads.data; machine->state.gamepads.previous.data = machine->memory.ram.input.gamepads.data;
machine->state.keyboard.previous.data = machine->memory.ram.input.keyboard.data; machine->state.keyboard.previous.data = machine->memory.ram.input.keyboard.data;
@ -1377,7 +1377,7 @@ static void api_tick_end(tic_mem* memory)
enum {EndTime = CLOCKRATE / TIC_FRAMERATE}; enum {EndTime = CLOCKRATE / TIC_FRAMERATE};
for (s32 i = 0; i < TIC_SOUND_CHANNELS; ++i ) for (s32 i = 0; i < TIC_SOUND_CHANNELS; ++i )
{ {
tic_sound_register* reg = &memory->ram.registers[i]; tic_sound_register* reg = &tic->ram.registers[i];
tic_sound_register_data* data = &machine->state.registers[i]; tic_sound_register_data* data = &machine->state.registers[i];
isNoiseWaveform(&reg->waveform) isNoiseWaveform(&reg->waveform)
@ -1396,34 +1396,34 @@ static void api_tick_end(tic_mem* memory)
} }
static tic_sfx_pos api_sfx_pos(tic_mem* memory, s32 channel) static tic_sfx_pos api_sfx_pos(tic_mem* tic, s32 channel)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
Channel* c = &machine->state.channels[channel]; Channel* c = &machine->state.channels[channel];
return c->pos; return c->pos;
} }
static void api_sfx_ex(tic_mem* memory, s32 index, s32 note, s32 octave, s32 duration, s32 channel, s32 volume, s32 speed) static void api_sfx_ex(tic_mem* tic, s32 index, s32 note, s32 octave, s32 duration, s32 channel, s32 volume, s32 speed)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
channelSfx(memory, index, note, octave, duration, &machine->state.channels[channel], volume, speed); channelSfx(tic, index, note, octave, duration, &machine->state.channels[channel], volume, speed);
} }
static void api_sfx(tic_mem* memory, s32 index, s32 note, s32 octave, s32 duration, s32 channel) static void api_sfx(tic_mem* tic, s32 index, s32 note, s32 octave, s32 duration, s32 channel)
{ {
api_sfx_ex(memory, index, note, octave, duration, channel, MAX_VOLUME, SFX_DEF_SPEED); api_sfx_ex(tic, index, note, octave, duration, channel, MAX_VOLUME, SFX_DEF_SPEED);
} }
static void api_sfx_stop(tic_mem* memory, s32 channel) static void api_sfx_stop(tic_mem* tic, s32 channel)
{ {
api_sfx(memory, -1, 0, 0, -1, channel); api_sfx(tic, -1, 0, 0, -1, channel);
} }
static void api_music_frame(tic_mem* memory, s32 index, s32 frame, s32 row, bool loop) static void api_music_frame(tic_mem* tic, s32 index, s32 frame, s32 row, bool loop)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
setMusic(machine, index, frame, row, loop); setMusic(machine, index, frame, row, loop);
@ -1492,11 +1492,11 @@ static void api_sync(tic_mem* tic, u32 mask, s32 bank, bool toCart)
machine->state.synced |= mask; machine->state.synced |= mask;
} }
static void cart2ram(tic_mem* memory) static void cart2ram(tic_mem* tic)
{ {
api_sync(memory, 0, 0, false); api_sync(tic, 0, 0, false);
initCover(memory); initCover(tic);
} }
static const char* readMetatag(const char* code, const char* tag, const char* comment) static const char* readMetatag(const char* code, const char* tag, const char* comment)
@ -1586,18 +1586,18 @@ static const tic_script_config* getScriptConfig(const char* code)
#endif #endif
} }
static const tic_script_config* api_get_script_config(tic_mem* memory) static const tic_script_config* api_get_script_config(tic_mem* tic)
{ {
return getScriptConfig(memory->cart.bank0.code.data); return getScriptConfig(tic->cart.bank0.code.data);
} }
static void updateSaveid(tic_mem* memory) static void updateSaveid(tic_mem* tic)
{ {
memset(memory->saveid, 0, sizeof memory->saveid); memset(tic->saveid, 0, sizeof tic->saveid);
const char* saveid = readMetatag(memory->cart.bank0.code.data, "saveid", api_get_script_config(memory)->singleComment); const char* saveid = readMetatag(tic->cart.bank0.code.data, "saveid", api_get_script_config(tic)->singleComment);
if(saveid) if(saveid)
{ {
strncpy(memory->saveid, saveid, TIC_SAVEID_SIZE-1); strncpy(tic->saveid, saveid, TIC_SAVEID_SIZE-1);
free((void*)saveid); free((void*)saveid);
} }
} }
@ -1673,25 +1673,25 @@ static void api_tick(tic_mem* tic, tic_tick_data* data)
machine->state.tick(tic); machine->state.tick(tic);
} }
static void api_scanline(tic_mem* memory, s32 row, void* data) static void api_scanline(tic_mem* tic, s32 row, void* data)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
if(machine->state.initialized) if(machine->state.initialized)
machine->state.scanline(memory, row, data); machine->state.scanline(tic, row, data);
} }
static void api_overline(tic_mem* memory, void* data) static void api_overline(tic_mem* tic, void* data)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
if(machine->state.initialized) if(machine->state.initialized)
machine->state.ovr.callback(memory, data); machine->state.ovr.callback(tic, data);
} }
static double api_time(tic_mem* memory) static double api_time(tic_mem* tic)
{ {
tic_machine* machine = (tic_machine*)memory; tic_machine* machine = (tic_machine*)tic;
return (double)((machine->data->counter() - machine->data->start)*1000)/machine->data->freq(); return (double)((machine->data->counter() - machine->data->start)*1000)/machine->data->freq();
} }

View File

@ -53,8 +53,6 @@
#define TIC_VRAM_SIZE (16*1024) //16K #define TIC_VRAM_SIZE (16*1024) //16K
#define TIC_RAM_SIZE (80*1024) //80K #define TIC_RAM_SIZE (80*1024) //80K
#define TIC_FONT_WIDTH 6
#define TIC_FONT_HEIGHT 6
#define TIC_PALETTE_BPP 4 #define TIC_PALETTE_BPP 4
#define TIC_PALETTE_SIZE (1 << TIC_PALETTE_BPP) #define TIC_PALETTE_SIZE (1 << TIC_PALETTE_BPP)
#define TIC_FRAMERATE 60 #define TIC_FRAMERATE 60
@ -111,7 +109,6 @@
#define TIC_GAMEPADS (sizeof(tic80_gamepads) / sizeof(tic80_gamepad)) #define TIC_GAMEPADS (sizeof(tic80_gamepads) / sizeof(tic80_gamepad))
#define SFX_NOTES {"C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-"} #define SFX_NOTES {"C-", "C#", "D-", "D#", "E-", "F-", "F#", "G-", "G#", "A-", "A#", "B-"}
#define TIC_FONT_CHARS 128
enum enum
{ {
@ -359,11 +356,6 @@ typedef struct
tic_cover_image cover; tic_cover_image cover;
} tic_cartridge; } tic_cartridge;
typedef struct
{
u8 data[TIC_FONT_CHARS * BITS_IN_BYTE];
} tic_font;
typedef struct typedef struct
{ {
u8 data[TIC80_WIDTH * TIC80_HEIGHT * TIC_PALETTE_BPP / BITS_IN_BYTE]; u8 data[TIC80_WIDTH * TIC80_HEIGHT * TIC_PALETTE_BPP / BITS_IN_BYTE];

File diff suppressed because one or more lines are too long

View File

@ -24,6 +24,8 @@
#include "tic.h" #include "tic.h"
#define TIC_FONT_CHARS 128
typedef struct { u8 index; tic_flip flip; tic_rotate rotate; } RemapResult; typedef struct { u8 index; tic_flip flip; tic_rotate rotate; } RemapResult;
typedef void(*RemapFunc)(void*, s32 x, s32 y, RemapResult* result); typedef void(*RemapFunc)(void*, s32 x, s32 y, RemapResult* result);
typedef struct typedef struct
@ -173,7 +175,6 @@ struct tic_mem
{ {
tic_ram ram; tic_ram ram;
tic_cartridge cart; tic_cartridge cart;
tic_font font;
tic_api api; tic_api api;
tic_persistent persistent; tic_persistent persistent;
@ -198,6 +199,13 @@ struct tic_mem
} samples; } samples;
u32 screen[TIC80_FULLWIDTH * TIC80_FULLHEIGHT]; u32 screen[TIC80_FULLWIDTH * TIC80_FULLHEIGHT];
struct
{
u8 data[TIC_FONT_CHARS * BITS_IN_BYTE];
s32 width;
s32 height;
} font;
}; };
tic_mem* tic_create(s32 samplerate); tic_mem* tic_create(s32 samplerate);