TIC-80-guile/src/studio.c

1986 lines
41 KiB
C
Raw Normal View History

2017-09-26 08:59:34 +02:00
// MIT License
// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "studio.h"
#include "start.h"
#include "console.h"
#include "run.h"
#include "sprite.h"
#include "map.h"
#include "world.h"
#include "sfx.h"
#include "music.h"
#include "history.h"
#include "config.h"
#include "code.h"
#include "dialog.h"
#include "menu.h"
#include "surf.h"
#include "fs.h"
#include "ext/gif.h"
#include "ext/md5.h"
2018-02-08 13:29:14 +01:00
#include <zlib.h>
#include <ctype.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
2018-01-24 10:20:05 +01:00
// #define TEXTURE_SIZE (TIC80_FULLWIDTH)
// #define STUDIO_PIXEL_FORMAT SDL_PIXELFORMAT_ARGB8888
2017-11-06 08:57:40 +01:00
#define FRAME_SIZE (TIC80_FULLWIDTH * TIC80_FULLHEIGHT * sizeof(u32))
2018-01-24 10:20:05 +01:00
// #define OFFSET_LEFT ((TIC80_FULLWIDTH-TIC80_WIDTH)/2)
// #define OFFSET_TOP ((TIC80_FULLHEIGHT-TIC80_HEIGHT)/2)
2017-12-08 13:03:59 +01:00
#define POPUP_DUR (TIC_FRAMERATE*2)
2018-01-24 14:59:02 +01:00
#define KEYBOARD_HOLD 20
#define KEYBOARD_PERIOD 3
2017-12-14 15:19:35 +01:00
#if defined(TIC80_PRO)
#define TIC_EDITOR_BANKS (TIC_BANKS)
#else
#define TIC_EDITOR_BANKS 1
#endif
2017-09-26 08:59:34 +02:00
typedef struct
{
u8 data[16];
} CartHash;
typedef struct
{
bool down;
bool click;
2018-02-01 17:34:42 +01:00
tic_point start;
tic_point end;
2017-09-26 08:59:34 +02:00
} MouseState;
2017-12-13 21:04:22 +01:00
static const EditorMode Modes[] =
{
TIC_CODE_MODE,
TIC_SPRITE_MODE,
TIC_MAP_MODE,
TIC_SFX_MODE,
TIC_MUSIC_MODE,
};
2017-09-26 08:59:34 +02:00
static struct
{
2018-01-24 12:01:19 +01:00
Studio studio;
2018-02-07 16:21:24 +01:00
System* system;
2018-01-24 12:01:19 +01:00
2017-09-26 08:59:34 +02:00
tic80_local* tic80local;
2017-11-22 08:20:12 +01:00
struct
{
CartHash hash;
u64 mdate;
}cart;
2017-09-26 08:59:34 +02:00
EditorMode mode;
EditorMode prevMode;
EditorMode dialogMode;
struct
{
MouseState state[3];
} mouse;
struct
{
2018-02-01 17:34:42 +01:00
tic_point pos;
2017-09-26 08:59:34 +02:00
bool active;
} gesture;
2018-01-24 15:56:55 +01:00
tic_key keycodes[KEYMAP_COUNT];
2017-09-26 08:59:34 +02:00
2017-12-13 14:02:16 +01:00
struct
{
bool show;
bool chained;
2017-12-13 21:04:22 +01:00
union
{
struct
{
s8 code;
s8 sprites;
s8 map;
s8 sfx;
s8 music;
2017-12-13 21:04:22 +01:00
} index;
s8 indexes[COUNT_OF(Modes)];
2017-12-13 21:04:22 +01:00
};
2017-12-13 15:09:25 +01:00
} bank;
2017-12-13 14:02:16 +01:00
2017-09-26 08:59:34 +02:00
struct
{
s32 counter;
char message[STUDIO_TEXT_BUFFER_WIDTH];
2017-09-26 08:59:34 +02:00
} popup;
struct
{
char text[STUDIO_TEXT_BUFFER_WIDTH];
} tooltip;
struct
{
bool record;
u32* buffer;
s32 frames;
s32 frame;
} video;
2017-12-14 15:08:04 +01:00
struct
{
2017-12-14 18:31:04 +01:00
Code* code;
Sprite* sprite;
Map* map;
Sfx* sfx;
Music* music;
} editor[TIC_EDITOR_BANKS];
2017-12-14 15:08:04 +01:00
2017-09-26 08:59:34 +02:00
struct
{
2017-12-13 18:14:32 +01:00
Start* start;
Console* console;
Run* run;
World* world;
Config* config;
Dialog* dialog;
Menu* menu;
Surf* surf;
2017-09-26 08:59:34 +02:00
};
FileSystem* fs;
bool missedFrame;
2017-09-26 08:59:34 +02:00
s32 argc;
char **argv;
2018-01-24 12:01:19 +01:00
s32 samplerate;
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
} studioImpl =
2017-09-26 08:59:34 +02:00
{
.tic80local = NULL,
2017-11-22 08:20:12 +01:00
.cart =
{
.mdate = 0,
},
2017-09-26 08:59:34 +02:00
.mode = TIC_START_MODE,
.prevMode = TIC_CODE_MODE,
.dialogMode = TIC_CONSOLE_MODE,
.gesture =
{
.pos = {0, 0},
.active = false,
},
.keycodes =
2017-09-26 08:59:34 +02:00
{
2018-01-24 15:56:55 +01:00
tic_key_up,
tic_key_down,
tic_key_left,
tic_key_right,
2017-09-26 08:59:34 +02:00
2018-01-24 15:56:55 +01:00
tic_key_z, // a
tic_key_x, // b
tic_key_a, // x
tic_key_s, // y
2017-09-26 08:59:34 +02:00
},
2017-12-13 15:09:25 +01:00
.bank =
2017-12-13 14:02:16 +01:00
{
.show = false,
.chained = true,
2017-12-13 14:02:16 +01:00
},
.popup =
2017-09-26 08:59:34 +02:00
{
.counter = 0,
.message = "\0",
},
.tooltip =
2017-09-26 08:59:34 +02:00
{
.text = "\0",
},
.video =
2017-09-26 08:59:34 +02:00
{
.record = false,
.buffer = NULL,
.frames = 0,
},
.missedFrame = false,
2017-09-26 08:59:34 +02:00
.argc = 0,
.argv = NULL,
};
2018-01-24 14:39:29 +01:00
char getKeyboardText()
{
tic_mem* tic = studioImpl.studio.tic;
static const char Symbols[] = "abcdefghijklmnopqrstuvwxyz0123456789-=[]\\;'`,./ ";
static const char Shift[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ)!@#$%^&*(_+{}|:\"~<>? ";
2018-01-24 14:59:02 +01:00
enum{Count = sizeof Symbols};
2018-01-24 14:39:29 +01:00
for(s32 i = 0; i < TIC80_KEY_BUFFER; i++)
{
tic_key key = tic->ram.input.keyboard.keys[i];
2018-01-24 14:59:02 +01:00
if(key > 0 && key < Count && tic->api.keyp(tic, key, KEYBOARD_HOLD, KEYBOARD_PERIOD))
2018-01-24 14:39:29 +01:00
return tic->api.key(tic, tic_key_shift) ? Shift[key-1] : Symbols[key-1];
}
return 0;
}
2018-02-13 11:29:33 +01:00
bool keyWasPressed(tic_key key)
2018-01-24 14:59:02 +01:00
{
tic_mem* tic = studioImpl.studio.tic;
return tic->api.keyp(tic, key, KEYBOARD_HOLD, KEYBOARD_PERIOD);
}
2018-02-13 11:29:33 +01:00
bool anyKeyWasPressed()
2018-01-24 14:59:02 +01:00
{
tic_mem* tic = studioImpl.studio.tic;
for(s32 i = 0; i < TIC80_KEY_BUFFER; i++)
{
tic_key key = tic->ram.input.keyboard.keys[i];
if(tic->api.keyp(tic, key, KEYBOARD_HOLD, KEYBOARD_PERIOD))
return true;
}
return false;
}
2017-12-13 21:04:22 +01:00
tic_tiles* getBankTiles()
{
2018-01-24 12:01:19 +01:00
return &studioImpl.studio.tic->cart.banks[studioImpl.bank.index.sprites].tiles;
2017-12-13 21:04:22 +01:00
}
tic_map* getBankMap()
{
2018-01-24 12:01:19 +01:00
return &studioImpl.studio.tic->cart.banks[studioImpl.bank.index.map].map;
2017-12-13 21:04:22 +01:00
}
2017-09-26 08:59:34 +02:00
void playSystemSfx(s32 id)
{
2018-01-24 12:01:19 +01:00
const tic_sample* effect = &studioImpl.studio.tic->config.bank0.sfx.samples.data[id];
studioImpl.studio.tic->api.sfx_ex(studioImpl.studio.tic, id, effect->note, effect->octave, -1, 0, MAX_VOLUME, 0);
2017-09-26 08:59:34 +02:00
}
static void md5(const void* voidData, s32 length, u8* digest)
{
enum {Size = 512};
const u8* data = voidData;
MD5_CTX c;
2017-09-26 08:59:34 +02:00
MD5_Init(&c);
while (length > 0)
2017-09-26 08:59:34 +02:00
{
MD5_Update(&c, data, length > Size ? Size: length);
length -= Size;
data += Size;
}
MD5_Final(digest, &c);
}
static u8* getSpritePtr(tic_tile* tiles, s32 x, s32 y)
{
enum { SheetCols = (TIC_SPRITESHEET_SIZE / TIC_SPRITESIZE) };
return tiles[x / TIC_SPRITESIZE + y / TIC_SPRITESIZE * SheetCols].data;
}
void setSpritePixel(tic_tile* tiles, s32 x, s32 y, u8 color)
{
// TODO: check spritesheet rect
tic_tool_poke4(getSpritePtr(tiles, x, y), (x % TIC_SPRITESIZE) + (y % TIC_SPRITESIZE) * TIC_SPRITESIZE, color);
}
u8 getSpritePixel(tic_tile* tiles, s32 x, s32 y)
{
// TODO: check spritesheet rect
return tic_tool_peek4(getSpritePtr(tiles, x, y), (x % TIC_SPRITESIZE) + (y % TIC_SPRITESIZE) * TIC_SPRITESIZE);
}
void toClipboard(const void* data, s32 size, bool flip)
{
if(data)
{
enum {Len = 2};
2018-02-06 20:15:56 +01:00
char* clipboard = (char*)malloc(size*Len + 1);
2017-09-26 08:59:34 +02:00
if(clipboard)
{
char* ptr = clipboard;
for(s32 i = 0; i < size; i++, ptr+=Len)
{
sprintf(ptr, "%02x", ((u8*)data)[i]);
if(flip)
{
char tmp = ptr[0];
ptr[0] = ptr[1];
ptr[1] = tmp;
2017-09-26 08:59:34 +02:00
}
}
2018-02-06 20:15:56 +01:00
setClipboardText(clipboard);
free(clipboard);
}
2017-09-26 08:59:34 +02:00
}
}
2017-11-11 12:44:41 +01:00
void str2buf(const char* str, s32 size, void* buf, bool flip)
2017-09-26 08:59:34 +02:00
{
char val[] = "0x00";
const char* ptr = str;
for(s32 i = 0; i < size/2; i++)
{
if(flip)
{
val[3] = *ptr++;
val[2] = *ptr++;
}
else
{
val[2] = *ptr++;
2017-09-26 08:59:34 +02:00
val[3] = *ptr++;
}
((u8*)buf)[i] = (u8)strtol(val, NULL, 16);
}
}
2017-12-11 10:46:52 +01:00
static void removeWhiteSpaces(char* str)
{
s32 i = 0;
s32 len = strlen(str);
for (s32 j = 0; j < len; j++)
2018-02-06 20:15:56 +01:00
if(!isspace(str[j]))
2017-12-11 10:46:52 +01:00
str[i++] = str[j];
str[i] = '\0';
}
bool fromClipboard(void* data, s32 size, bool flip, bool remove_white_spaces)
2017-09-26 08:59:34 +02:00
{
if(data)
{
2018-02-06 20:15:56 +01:00
if(hasClipboardText())
2017-09-26 08:59:34 +02:00
{
2018-02-06 20:15:56 +01:00
char* clipboard = getClipboardText();
2017-09-26 08:59:34 +02:00
if(clipboard)
{
2017-12-11 10:46:52 +01:00
if (remove_white_spaces)
removeWhiteSpaces(clipboard);
2017-09-26 08:59:34 +02:00
bool valid = strlen(clipboard) == size * 2;
2017-11-11 12:44:41 +01:00
if(valid) str2buf(clipboard, strlen(clipboard), data, flip);
2017-09-26 08:59:34 +02:00
2018-02-06 20:15:56 +01:00
free(clipboard);
2017-09-26 08:59:34 +02:00
return valid;
}
}
2017-09-26 08:59:34 +02:00
}
return false;
}
void showTooltip(const char* text)
{
2018-01-24 12:01:19 +01:00
strncpy(studioImpl.tooltip.text, text, sizeof studioImpl.tooltip.text - 1);
2017-09-26 08:59:34 +02:00
}
2017-12-13 14:02:16 +01:00
static void drawExtrabar(tic_mem* tic)
2017-09-26 08:59:34 +02:00
{
enum {Size = 7};
s32 x = (COUNT_OF(Modes) + 1) * Size + 17 * TIC_FONT_WIDTH;
s32 y = 0;
static const u8 Icons[] =
{
0b00000000,
0b00101000,
0b00101000,
0b00010000,
0b01101100,
0b01101100,
0b00000000,
0b00000000,
0b00000000,
0b01111000,
0b01001000,
0b01011100,
0b01110100,
0b00011100,
0b00000000,
0b00000000,
0b00000000,
0b00111000,
0b01000100,
0b01111100,
0b01101100,
0b01111100,
0b00000000,
0b00000000,
0b00000000,
0b00011000,
0b00110000,
0b01111100,
0b00110000,
0b00011000,
0b00000000,
0b00000000,
0b00000000,
0b00110000,
0b00011000,
0b01111100,
0b00011000,
0b00110000,
0b00000000,
0b00000000,
};
static const s32 Colors[] = {8, 9, 6, 5, 5};
static const StudioEvent Events[] = {TIC_TOOLBAR_CUT, TIC_TOOLBAR_COPY, TIC_TOOLBAR_PASTE, TIC_TOOLBAR_UNDO, TIC_TOOLBAR_REDO};
static const char* Tips[] = {"CUT [ctrl+x]", "COPY [ctrl+c]", "PASTE [ctrl+v]", "UNDO [ctrl+z]", "REDO [ctrl+y]"};
2017-09-26 08:59:34 +02:00
for(s32 i = 0; i < sizeof Icons / BITS_IN_BYTE; i++)
{
2018-02-01 17:34:42 +01:00
tic_rect rect = {x + i*Size, y, Size, Size};
2017-09-26 08:59:34 +02:00
2017-10-20 09:37:30 +02:00
u8 bgcolor = (tic_color_white);
u8 color = (tic_color_light_blue);
2017-09-26 08:59:34 +02:00
if(checkMousePos(&rect))
{
2018-02-06 20:15:56 +01:00
setCursor(tic_cursor_hand);
2017-09-26 08:59:34 +02:00
color = Colors[i];
showTooltip(Tips[i]);
2018-02-01 16:32:31 +01:00
if(checkMouseDown(&rect, tic_mouse_left))
2017-09-26 08:59:34 +02:00
{
bgcolor = color;
2017-10-20 09:37:30 +02:00
color = (tic_color_white);
2017-09-26 08:59:34 +02:00
}
2018-02-01 16:32:31 +01:00
else if(checkMouseClick(&rect, tic_mouse_left))
2017-09-26 08:59:34 +02:00
{
setStudioEvent(Events[i]);
}
}
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.rect(tic, x + i * Size, y, Size, Size, bgcolor);
2017-09-26 08:59:34 +02:00
drawBitIcon(x + i * Size, y, Icons + i*BITS_IN_BYTE, color);
}
}
const StudioConfig* getConfig()
{
2018-01-24 12:01:19 +01:00
return &studioImpl.config->data;
2017-09-26 08:59:34 +02:00
}
2017-12-13 14:02:16 +01:00
#if defined (TIC80_PRO)
static void drawBankIcon(s32 x, s32 y)
{
2018-01-24 12:01:19 +01:00
tic_mem* tic = studioImpl.studio.tic;
2017-12-13 15:09:25 +01:00
2018-02-01 17:34:42 +01:00
tic_rect rect = {x, y, TIC_FONT_WIDTH, TIC_FONT_HEIGHT};
2017-12-13 14:02:16 +01:00
static const u8 Icon[] =
{
0b00000000,
0b01111100,
0b01000100,
0b01000100,
0b01111100,
0b01111000,
0b00000000,
0b00000000,
};
bool over = false;
2017-12-13 21:04:22 +01:00
s32 mode = 0;
for(s32 i = 0; i < COUNT_OF(Modes); i++)
2018-01-24 12:01:19 +01:00
if(Modes[i] == studioImpl.mode)
2017-12-13 21:04:22 +01:00
{
mode = i;
break;
}
2017-12-13 14:02:16 +01:00
if(checkMousePos(&rect))
{
2018-02-06 20:15:56 +01:00
setCursor(tic_cursor_hand);
2017-12-13 14:02:16 +01:00
over = true;
showTooltip("SWITCH BANK");
2018-02-01 16:32:31 +01:00
if(checkMouseClick(&rect, tic_mouse_left))
2018-01-24 12:01:19 +01:00
studioImpl.bank.show = !studioImpl.bank.show;
2017-12-13 14:02:16 +01:00
}
2018-01-24 12:01:19 +01:00
if(studioImpl.bank.show)
2017-12-13 15:09:25 +01:00
{
drawBitIcon(x, y, Icon, tic_color_red);
2017-12-13 15:09:25 +01:00
enum{Size = TOOLBAR_SIZE};
2017-12-14 15:19:35 +01:00
for(s32 i = 0; i < TIC_EDITOR_BANKS; i++)
2017-12-13 15:09:25 +01:00
{
2018-02-01 17:34:42 +01:00
tic_rect rect = {x + 2 + (i+1)*Size, 0, Size, Size};
2017-12-13 15:09:25 +01:00
bool over = false;
if(checkMousePos(&rect))
{
2018-02-06 20:15:56 +01:00
setCursor(tic_cursor_hand);
2017-12-13 15:09:25 +01:00
over = true;
2018-02-01 16:32:31 +01:00
if(checkMouseClick(&rect, tic_mouse_left))
{
2018-01-24 12:01:19 +01:00
if(studioImpl.bank.chained)
2018-02-06 20:15:56 +01:00
memset(studioImpl.bank.indexes, i, sizeof studioImpl.bank.indexes);
2018-01-24 12:01:19 +01:00
else studioImpl.bank.indexes[mode] = i;
}
2017-12-13 15:09:25 +01:00
}
2018-01-24 12:01:19 +01:00
if(i == studioImpl.bank.indexes[mode])
2017-12-13 15:09:25 +01:00
tic->api.rect(tic, rect.x, rect.y, rect.w, rect.h, tic_color_red);
2018-01-24 12:01:19 +01:00
tic->api.draw_char(tic, '0' + i, rect.x+1, rect.y+1, i == studioImpl.bank.indexes[mode] ? tic_color_white : over ? tic_color_red : tic_color_peach);
}
{
static const u8 PinIcon[] =
{
0b00000000,
0b00111000,
0b00101000,
0b01111100,
0b00010000,
0b00010000,
0b00000000,
0b00000000,
};
2018-02-01 17:34:42 +01:00
tic_rect rect = {x + 4 + (TIC_EDITOR_BANKS+1)*Size, 0, Size, Size};
bool over = false;
if(checkMousePos(&rect))
{
2018-02-06 20:15:56 +01:00
setCursor(tic_cursor_hand);
over = true;
2018-02-01 16:32:31 +01:00
if(checkMouseClick(&rect, tic_mouse_left))
{
2018-01-24 12:01:19 +01:00
studioImpl.bank.chained = !studioImpl.bank.chained;
2018-01-24 12:01:19 +01:00
if(studioImpl.bank.chained)
2018-02-06 20:15:56 +01:00
memset(studioImpl.bank.indexes, studioImpl.bank.indexes[mode], sizeof studioImpl.bank.indexes);
}
}
2018-01-24 12:01:19 +01:00
drawBitIcon(rect.x, rect.y, PinIcon, studioImpl.bank.chained ? tic_color_black : over ? tic_color_dark_gray : tic_color_light_blue);
2017-12-13 15:09:25 +01:00
}
}
else
{
drawBitIcon(x, y, Icon, over ? tic_color_red : tic_color_peach);
}
2017-12-13 14:02:16 +01:00
}
#endif
2017-09-26 08:59:34 +02:00
void drawToolbar(tic_mem* tic, u8 color, bool bg)
{
if(bg)
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.rect(tic, 0, 0, TIC80_WIDTH, TOOLBAR_SIZE, (tic_color_white));
2017-09-26 08:59:34 +02:00
static const u8 TabIcon[] =
{
0b11111110,
0b11111110,
0b11111110,
0b11111110,
0b11111110,
0b11111110,
0b11111110,
0b00000000,
};
static const u8 Icons[] =
{
0b00000000,
0b01101100,
0b01000100,
0b01000100,
0b01000100,
0b01101100,
0b00000000,
0b00000000,
0b00000000,
0b00111000,
0b01010100,
0b01111100,
0b01111100,
0b01010100,
0b00000000,
0b00000000,
0b00000000,
0b01101100,
0b01101100,
0b00000000,
0b01101100,
0b01101100,
0b00000000,
0b00000000,
0b00000000,
0b00011000,
0b00110100,
0b01110100,
0b00110100,
0b00011000,
0b00000000,
0b00000000,
0b00000000,
0b00111100,
0b00100100,
0b00100100,
0b01101100,
0b01101100,
0b00000000,
0b00000000,
};
enum {Size = 7};
static const char* Tips[] = {"CODE EDITOR [f1]", "SPRITE EDITOR [f2]", "MAP EDITOR [f3]", "SFX EDITOR [f4]", "MUSIC EDITOR [f5]",};
s32 mode = -1;
for(s32 i = 0; i < COUNT_OF(Modes); i++)
{
2018-02-01 17:34:42 +01:00
tic_rect rect = {i * Size, 0, Size, Size};
2017-09-26 08:59:34 +02:00
bool over = false;
if(checkMousePos(&rect))
{
2018-02-06 20:15:56 +01:00
setCursor(tic_cursor_hand);
2017-09-26 08:59:34 +02:00
over = true;
showTooltip(Tips[i]);
2018-02-01 16:32:31 +01:00
if(checkMouseClick(&rect, tic_mouse_left))
2017-09-26 08:59:34 +02:00
setStudioMode(Modes[i]);
}
if(getStudioMode() == Modes[i]) mode = i;
if(mode == i)
drawBitIcon(i * Size, 0, TabIcon, color);
2017-12-25 21:57:58 +01:00
if(mode == i)
drawBitIcon(i * Size, 1, Icons + i * BITS_IN_BYTE, tic_color_black);
2017-10-20 09:37:30 +02:00
drawBitIcon(i * Size, 0, Icons + i * BITS_IN_BYTE, mode == i ? (tic_color_white) : (over ? (tic_color_dark_gray) : (tic_color_light_blue)));
2017-09-26 08:59:34 +02:00
}
if(mode >= 0) drawExtrabar(tic);
static const char* Names[] =
{
"CODE EDITOR",
"SPRITE EDITOR",
"MAP EDITOR",
"SFX EDITOR",
"MUSIC EDITOR",
};
2017-12-13 14:02:16 +01:00
#if defined (TIC80_PRO)
enum {TextOffset = (COUNT_OF(Modes) + 2) * Size - 2};
drawBankIcon(COUNT_OF(Modes) * Size + 2, 0);
#else
enum {TextOffset = (COUNT_OF(Modes) + 1) * Size};
#endif
2018-01-24 12:01:19 +01:00
if(mode >= 0 && !studioImpl.bank.show)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
if(strlen(studioImpl.tooltip.text))
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.text(tic, studioImpl.tooltip.text, TextOffset, 1, (tic_color_black));
2017-09-26 08:59:34 +02:00
}
else
{
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.text(tic, Names[mode], TextOffset, 1, (tic_color_dark_gray));
2017-09-26 08:59:34 +02:00
}
}
}
void setStudioEvent(StudioEvent event)
{
2018-01-24 12:01:19 +01:00
switch(studioImpl.mode)
2017-09-26 08:59:34 +02:00
{
2017-12-14 12:38:06 +01:00
case TIC_CODE_MODE:
{
2018-01-24 12:01:19 +01:00
Code* code = studioImpl.editor[studioImpl.bank.index.code].code;
2017-12-14 12:38:06 +01:00
code->event(code, event);
}
break;
2017-12-14 15:08:04 +01:00
case TIC_SPRITE_MODE:
{
2018-01-24 12:01:19 +01:00
Sprite* sprite = studioImpl.editor[studioImpl.bank.index.sprites].sprite;
2017-12-14 15:08:04 +01:00
sprite->event(sprite, event);
}
break;
case TIC_MAP_MODE:
{
2018-01-24 12:01:19 +01:00
Map* map = studioImpl.editor[studioImpl.bank.index.map].map;
2017-12-14 15:08:04 +01:00
map->event(map, event);
}
break;
case TIC_SFX_MODE:
{
2018-01-24 12:01:19 +01:00
Sfx* sfx = studioImpl.editor[studioImpl.bank.index.sfx].sfx;
2017-12-14 15:08:04 +01:00
sfx->event(sfx, event);
}
break;
case TIC_MUSIC_MODE:
{
2018-01-24 12:01:19 +01:00
Music* music = studioImpl.editor[studioImpl.bank.index.music].music;
2017-12-14 15:08:04 +01:00
music->event(music, event);
}
break;
2017-09-26 08:59:34 +02:00
default: break;
}
}
2018-02-12 17:15:05 +01:00
ClipboardEvent getClipboardEvent()
2017-09-26 08:59:34 +02:00
{
2018-02-12 17:15:05 +01:00
tic_mem* tic = studioImpl.studio.tic;
2017-09-26 08:59:34 +02:00
2018-02-12 17:15:05 +01:00
bool shift = tic->api.key(tic, tic_key_shift);
bool ctrl = tic->api.key(tic, tic_key_ctrl);
if(ctrl)
{
2018-02-13 11:29:33 +01:00
if(keyWasPressed(tic_key_insert) || keyWasPressed(tic_key_c)) return TIC_CLIPBOARD_COPY;
else if(keyWasPressed(tic_key_x)) return TIC_CLIPBOARD_CUT;
else if(keyWasPressed(tic_key_v)) return TIC_CLIPBOARD_PASTE;
2018-02-12 17:15:05 +01:00
}
else if(shift)
{
2018-02-13 11:29:33 +01:00
if(keyWasPressed(tic_key_delete)) return TIC_CLIPBOARD_CUT;
else if(keyWasPressed(tic_key_insert)) return TIC_CLIPBOARD_PASTE;
2018-02-12 17:15:05 +01:00
}
2017-09-26 08:59:34 +02:00
return TIC_CLIPBOARD_NONE;
}
static void showPopupMessage(const char* text)
{
2018-01-24 12:01:19 +01:00
studioImpl.popup.counter = POPUP_DUR;
strcpy(studioImpl.popup.message, text);
2017-09-26 08:59:34 +02:00
}
static void exitConfirm(bool yes, void* data)
{
2018-01-24 17:09:22 +01:00
studioImpl.studio.quit = yes;
2017-09-26 08:59:34 +02:00
}
void exitStudio()
{
2018-01-24 12:01:19 +01:00
if(studioImpl.mode != TIC_START_MODE && studioCartChanged())
2017-09-26 08:59:34 +02:00
{
static const char* Rows[] =
2017-09-26 08:59:34 +02:00
{
"YOU HAVE",
"UNSAVED CHANGES",
"",
"DO YOU REALLY WANT",
"TO EXIT?",
};
showDialog(Rows, COUNT_OF(Rows), exitConfirm, NULL);
}
else exitConfirm(true, NULL);
}
void drawBitIcon(s32 x, s32 y, const u8* ptr, u8 color)
{
for(s32 i = 0; i < TIC_SPRITESIZE; i++, ptr++)
for(s32 col = 0; col < TIC_SPRITESIZE; col++)
if(*ptr & 1 << col)
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.pixel(studioImpl.studio.tic, x + TIC_SPRITESIZE - col - 1, y + i, color);
2017-09-26 08:59:34 +02:00
}
static void initWorldMap()
{
2018-01-24 12:01:19 +01:00
initWorld(studioImpl.world, studioImpl.studio.tic, studioImpl.editor[studioImpl.bank.index.map].map);
2017-09-26 08:59:34 +02:00
}
static void initRunMode()
{
2018-01-24 12:01:19 +01:00
initRun(studioImpl.run, studioImpl.console, studioImpl.studio.tic);
2017-09-26 08:59:34 +02:00
}
static void initSurfMode()
{
2018-01-24 12:01:19 +01:00
initSurf(studioImpl.surf, studioImpl.studio.tic, studioImpl.console);
2017-09-26 08:59:34 +02:00
}
2017-09-28 16:20:27 +02:00
void gotoSurf()
{
initSurfMode();
setStudioMode(TIC_SURF_MODE);
}
void gotoCode()
{
setStudioMode(TIC_CODE_MODE);
}
2017-09-26 08:59:34 +02:00
static void initMenuMode()
{
2018-01-24 12:01:19 +01:00
initMenu(studioImpl.menu, studioImpl.studio.tic, studioImpl.fs);
2017-09-26 08:59:34 +02:00
}
void runGameFromSurf()
{
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.reset(studioImpl.studio.tic);
2017-09-26 08:59:34 +02:00
setStudioMode(TIC_RUN_MODE);
2018-01-24 12:01:19 +01:00
studioImpl.prevMode = TIC_SURF_MODE;
2017-09-26 08:59:34 +02:00
}
void exitFromGameMenu()
{
2018-01-24 12:01:19 +01:00
if(studioImpl.prevMode == TIC_SURF_MODE)
2017-09-26 08:59:34 +02:00
{
setStudioMode(TIC_SURF_MODE);
}
else
{
setStudioMode(TIC_CONSOLE_MODE);
}
2017-11-21 17:47:36 +01:00
2018-01-24 12:01:19 +01:00
studioImpl.console->showGameMenu = false;
2017-09-26 08:59:34 +02:00
}
2017-12-19 12:02:56 +01:00
void resumeRunMode()
{
2018-01-24 12:01:19 +01:00
studioImpl.mode = TIC_RUN_MODE;
2017-12-19 12:02:56 +01:00
}
2017-09-26 08:59:34 +02:00
void setStudioMode(EditorMode mode)
{
2018-01-24 12:01:19 +01:00
if(mode != studioImpl.mode)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
EditorMode prev = studioImpl.mode;
2017-09-26 08:59:34 +02:00
if(prev == TIC_RUN_MODE)
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.pause(studioImpl.studio.tic);
2017-09-26 08:59:34 +02:00
if(mode != TIC_RUN_MODE)
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.reset(studioImpl.studio.tic);
2017-09-26 08:59:34 +02:00
switch (prev)
{
case TIC_START_MODE:
case TIC_CONSOLE_MODE:
case TIC_RUN_MODE:
2017-09-26 08:59:34 +02:00
case TIC_DIALOG_MODE:
case TIC_MENU_MODE:
break;
case TIC_SURF_MODE:
2018-01-24 12:01:19 +01:00
studioImpl.prevMode = TIC_CODE_MODE;
2017-09-26 08:59:34 +02:00
break;
2018-01-24 12:01:19 +01:00
default: studioImpl.prevMode = prev; break;
2017-09-26 08:59:34 +02:00
}
switch(mode)
{
case TIC_WORLD_MODE: initWorldMap(); break;
case TIC_RUN_MODE: initRunMode(); break;
2018-01-24 12:01:19 +01:00
case TIC_SURF_MODE: studioImpl.surf->resume(studioImpl.surf); break;
2017-09-26 08:59:34 +02:00
default: break;
}
2018-01-24 12:01:19 +01:00
studioImpl.mode = mode;
2017-09-26 08:59:34 +02:00
}
}
EditorMode getStudioMode()
{
2018-01-24 12:01:19 +01:00
return studioImpl.mode;
2017-09-26 08:59:34 +02:00
}
void changeStudioMode(s32 dir)
{
2018-02-01 17:34:42 +01:00
const size_t modeCount = sizeof(Modes)/sizeof(Modes[0]);
for(size_t i = 0; i < modeCount; i++)
{
if(studioImpl.mode == Modes[i])
{
setStudioMode(Modes[(i+dir+modeCount) % modeCount]);
return;
}
}
}
2018-02-13 10:44:16 +01:00
static void showGameMenu()
{
studioImpl.studio.tic->api.pause(studioImpl.studio.tic);
studioImpl.studio.tic->api.reset(studioImpl.studio.tic);
2017-10-20 10:48:04 +02:00
2018-02-13 10:44:16 +01:00
initMenuMode();
studioImpl.mode = TIC_MENU_MODE;
}
2017-09-26 08:59:34 +02:00
void hideGameMenu()
{
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.resume(studioImpl.studio.tic);
studioImpl.mode = TIC_RUN_MODE;
2017-09-26 08:59:34 +02:00
}
s32 getMouseX()
{
2018-01-24 12:55:13 +01:00
tic_mem* tic = studioImpl.studio.tic;
return tic->ram.input.mouse.x;
2017-09-26 08:59:34 +02:00
}
s32 getMouseY()
{
2018-01-24 12:55:13 +01:00
tic_mem* tic = studioImpl.studio.tic;
return tic->ram.input.mouse.y;
2017-09-26 08:59:34 +02:00
}
2018-02-01 17:34:42 +01:00
static inline bool pointInRect(const tic_point* pt, const tic_rect* rect)
2017-09-26 08:59:34 +02:00
{
2018-02-01 17:34:42 +01:00
return (pt->x >= rect->x)
&& (pt->x < (rect->x + rect->w))
&& (pt->y >= rect->y)
&& (pt->y < (rect->y + rect->h));
2017-09-26 08:59:34 +02:00
}
2018-02-01 17:34:42 +01:00
bool checkMousePos(const tic_rect* rect)
{
tic_point pos = {getMouseX(), getMouseY()};
return pointInRect(&pos, rect);
}
bool checkMouseClick(const tic_rect* rect, s32 button)
2017-09-26 08:59:34 +02:00
{
2018-02-01 16:32:31 +01:00
MouseState* state = &studioImpl.mouse.state[button];
2017-09-26 08:59:34 +02:00
bool value = state->click
2018-02-01 17:34:42 +01:00
&& pointInRect(&state->start, rect)
&& pointInRect(&state->end, rect);
2017-09-26 08:59:34 +02:00
if(value) state->click = false;
return value;
}
2018-02-01 17:34:42 +01:00
bool checkMouseDown(const tic_rect* rect, s32 button)
2017-09-26 08:59:34 +02:00
{
2018-02-01 16:32:31 +01:00
MouseState* state = &studioImpl.mouse.state[button];
2017-09-26 08:59:34 +02:00
2018-02-01 17:34:42 +01:00
return state->down && pointInRect(&state->start, rect);
2017-09-26 08:59:34 +02:00
}
2018-02-01 17:34:42 +01:00
bool getGesturePos(tic_point* pos)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
if(studioImpl.gesture.active)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
*pos = studioImpl.gesture.pos;
2017-09-26 08:59:34 +02:00
return true;
}
return false;
}
2018-02-06 20:15:56 +01:00
void setCursor(tic_cursor id)
2017-09-26 08:59:34 +02:00
{
2018-02-12 12:51:57 +01:00
tic_mem* tic = studioImpl.studio.tic;
tic->ram.vram.vars.cursor.sprite = id;
2017-09-26 08:59:34 +02:00
}
void hideDialog()
{
2018-01-24 12:01:19 +01:00
if(studioImpl.dialogMode == TIC_RUN_MODE)
{
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.resume(studioImpl.studio.tic);
studioImpl.mode = TIC_RUN_MODE;
}
2018-01-24 12:01:19 +01:00
else setStudioMode(studioImpl.dialogMode);
2017-09-26 08:59:34 +02:00
}
void showDialog(const char** text, s32 rows, DialogCallback callback, void* data)
{
2018-01-24 12:01:19 +01:00
if(studioImpl.mode != TIC_DIALOG_MODE)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
initDialog(studioImpl.dialog, studioImpl.studio.tic, text, rows, callback, data);
studioImpl.dialogMode = studioImpl.mode;
2017-09-26 08:59:34 +02:00
setStudioMode(TIC_DIALOG_MODE);
}
}
static void resetBanks()
{
2018-02-06 20:15:56 +01:00
memset(studioImpl.bank.indexes, 0, sizeof studioImpl.bank.indexes);
}
2017-09-26 08:59:34 +02:00
static void initModules()
{
2018-01-24 12:01:19 +01:00
tic_mem* tic = studioImpl.studio.tic;
2017-12-14 15:08:04 +01:00
resetBanks();
2017-12-14 15:19:35 +01:00
for(s32 i = 0; i < TIC_EDITOR_BANKS; i++)
2017-12-14 15:08:04 +01:00
{
2018-01-24 12:01:19 +01:00
initCode(studioImpl.editor[i].code, studioImpl.studio.tic, &tic->cart.banks[i].code);
initSprite(studioImpl.editor[i].sprite, studioImpl.studio.tic, &tic->cart.banks[i].tiles);
initMap(studioImpl.editor[i].map, studioImpl.studio.tic, &tic->cart.banks[i].map);
initSfx(studioImpl.editor[i].sfx, studioImpl.studio.tic, &tic->cart.banks[i].sfx);
initMusic(studioImpl.editor[i].music, studioImpl.studio.tic, &tic->cart.banks[i].music);
2017-12-14 15:08:04 +01:00
}
2017-12-14 12:38:06 +01:00
2017-09-26 08:59:34 +02:00
initWorldMap();
}
static void updateHash()
{
2018-01-24 12:01:19 +01:00
md5(&studioImpl.studio.tic->cart, sizeof(tic_cartridge), studioImpl.cart.hash.data);
2017-11-22 08:20:12 +01:00
}
static void updateMDate()
{
2018-01-24 12:01:19 +01:00
studioImpl.cart.mdate = fsMDate(studioImpl.console->fs, studioImpl.console->romName);
2017-09-26 08:59:34 +02:00
}
static void updateTitle()
2017-09-26 08:59:34 +02:00
{
2018-02-13 11:38:11 +01:00
char name[FILENAME_MAX] = TIC_TITLE;
2017-09-26 08:59:34 +02:00
2018-02-13 11:38:11 +01:00
if(strlen(studioImpl.console->romName))
sprintf(name, "%s [%s]", TIC_TITLE, studioImpl.console->romName);
2017-09-26 08:59:34 +02:00
2018-02-13 11:38:11 +01:00
studioImpl.system->setWindowTitle(name);
}
2017-09-26 08:59:34 +02:00
void studioRomSaved()
{
updateTitle();
2017-09-26 08:59:34 +02:00
updateHash();
2017-11-22 08:20:12 +01:00
updateMDate();
2017-09-26 08:59:34 +02:00
}
void studioRomLoaded()
{
initModules();
updateTitle();
2017-09-26 08:59:34 +02:00
updateHash();
2017-11-22 08:20:12 +01:00
updateMDate();
2017-09-26 08:59:34 +02:00
}
bool studioCartChanged()
{
CartHash hash;
2018-01-24 12:01:19 +01:00
md5(&studioImpl.studio.tic->cart, sizeof(tic_cartridge), hash.data);
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
return memcmp(hash.data, studioImpl.cart.hash.data, sizeof(CartHash)) != 0;
2017-09-26 08:59:34 +02:00
}
2018-01-24 15:56:55 +01:00
tic_key* getKeymap()
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
return studioImpl.keycodes;
2017-09-26 08:59:34 +02:00
}
2018-01-24 15:56:55 +01:00
static void processGamepadMapping()
2017-09-26 08:59:34 +02:00
{
2018-01-24 15:56:55 +01:00
tic_mem* tic = studioImpl.studio.tic;
2017-09-26 08:59:34 +02:00
for(s32 i = 0; i < KEYMAP_COUNT; i++)
2018-01-24 15:56:55 +01:00
if(tic->api.key(tic, studioImpl.keycodes[i]))
tic->ram.input.gamepads.data |= 1 << i;
2017-09-26 08:59:34 +02:00
}
static bool isGameMenu()
{
2018-01-24 12:01:19 +01:00
return (studioImpl.mode == TIC_RUN_MODE && studioImpl.console->showGameMenu) || studioImpl.mode == TIC_MENU_MODE;
2017-09-26 08:59:34 +02:00
}
static void goFullscreen()
2017-09-26 08:59:34 +02:00
{
2018-02-13 10:44:16 +01:00
studioImpl.system->goFullscreen();
2017-11-08 12:07:23 +01:00
}
2017-10-08 20:28:51 +02:00
void runProject()
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.reset(studioImpl.studio.tic);
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
if(studioImpl.mode == TIC_RUN_MODE)
2017-09-26 08:59:34 +02:00
{
initRunMode();
}
else setStudioMode(TIC_RUN_MODE);
}
static void saveProject()
{
2018-01-24 12:01:19 +01:00
CartSaveResult rom = studioImpl.console->save(studioImpl.console);
2017-09-26 08:59:34 +02:00
if(rom == CART_SAVE_OK)
{
char buffer[FILENAME_MAX];
2018-01-24 12:01:19 +01:00
sprintf(buffer, "%s SAVED :)", studioImpl.console->romName);
2017-09-26 08:59:34 +02:00
for(s32 i = 0; i < (s32)strlen(buffer); i++)
2018-02-06 20:15:56 +01:00
buffer[i] = toupper(buffer[i]);
2017-09-26 08:59:34 +02:00
showPopupMessage(buffer);
}
else if(rom == CART_SAVE_MISSING_NAME) showPopupMessage("SAVE: MISSING CART NAME :|");
else showPopupMessage("SAVE ERROR :(");
}
2018-02-01 17:34:42 +01:00
static void screen2buffer(u32* buffer, const u32* pixels, tic_rect rect)
2017-09-26 08:59:34 +02:00
{
pixels += rect.y * TIC80_FULLWIDTH;
for(s32 i = 0; i < rect.h; i++)
2017-09-26 08:59:34 +02:00
{
2018-02-06 20:15:56 +01:00
memcpy(buffer, pixels + rect.x, rect.w * sizeof(pixels[0]));
pixels += TIC80_FULLWIDTH;
buffer += rect.w;
}
2017-09-26 08:59:34 +02:00
}
static void setCoverImage()
{
2018-02-13 14:02:05 +01:00
tic_mem* tic = studioImpl.studio.tic;
2018-02-13 14:02:05 +01:00
if(studioImpl.mode == TIC_RUN_MODE)
{
enum {Pitch = TIC80_FULLWIDTH*sizeof(u32)};
tic->api.blit(tic, tic->api.scanline, tic->api.overlap, NULL);
2017-09-26 08:59:34 +02:00
2018-02-13 14:02:05 +01:00
u32* buffer = malloc(TIC80_WIDTH * TIC80_HEIGHT * sizeof(u32));
2017-09-26 08:59:34 +02:00
2018-02-13 14:02:05 +01:00
if(buffer)
{
enum{OffsetLeft = (TIC80_FULLWIDTH-TIC80_WIDTH)/2, OffsetTop = (TIC80_FULLHEIGHT-TIC80_HEIGHT)/2};
2018-02-13 14:02:05 +01:00
tic_rect rect = {OffsetLeft, OffsetTop, TIC80_WIDTH, TIC80_HEIGHT};
2018-02-13 14:02:05 +01:00
screen2buffer(buffer, tic->screen, rect);
2017-09-26 08:59:34 +02:00
2018-02-13 14:02:05 +01:00
gif_write_animation(studioImpl.studio.tic->cart.cover.data, &studioImpl.studio.tic->cart.cover.size,
TIC80_WIDTH, TIC80_HEIGHT, (const u8*)buffer, 1, TIC_FRAMERATE, 1);
2017-09-26 08:59:34 +02:00
2018-02-13 14:02:05 +01:00
free(buffer);
2017-09-26 08:59:34 +02:00
2018-02-13 14:02:05 +01:00
showPopupMessage("COVER IMAGE SAVED :)");
}
}
2017-09-26 08:59:34 +02:00
}
static void onVideoExported(GetResult result, void* data)
{
if(result == FS_FILE_NOT_DOWNLOADED)
showPopupMessage("GIF NOT EXPORTED :|");
else if (result == FS_FILE_DOWNLOADED)
showPopupMessage("GIF EXPORTED :)");
}
static void stopVideoRecord()
{
2018-01-24 12:01:19 +01:00
if(studioImpl.video.buffer)
2017-09-26 08:59:34 +02:00
{
{
s32 size = 0;
2018-02-06 20:15:56 +01:00
u8* data = malloc(FRAME_SIZE * studioImpl.video.frame);
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
gif_write_animation(data, &size, TIC80_FULLWIDTH, TIC80_FULLHEIGHT, (const u8*)studioImpl.video.buffer, studioImpl.video.frame, TIC_FRAMERATE, getConfig()->gifScale);
2017-09-26 08:59:34 +02:00
fsGetFileData(onVideoExported, "screen.gif", data, size, DEFAULT_CHMOD, NULL);
}
2018-02-06 20:15:56 +01:00
free(studioImpl.video.buffer);
2018-01-24 12:01:19 +01:00
studioImpl.video.buffer = NULL;
2017-09-26 08:59:34 +02:00
}
2018-01-24 12:01:19 +01:00
studioImpl.video.record = false;
2017-09-26 08:59:34 +02:00
}
#if !defined(__EMSCRIPTEN__)
static void startVideoRecord()
{
2018-01-24 12:01:19 +01:00
if(studioImpl.video.record)
2017-09-26 08:59:34 +02:00
{
stopVideoRecord();
}
else
{
2018-01-24 12:01:19 +01:00
studioImpl.video.frames = getConfig()->gifLength * TIC_FRAMERATE;
2018-02-06 20:15:56 +01:00
studioImpl.video.buffer = malloc(FRAME_SIZE * studioImpl.video.frames);
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
if(studioImpl.video.buffer)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
studioImpl.video.record = true;
studioImpl.video.frame = 0;
2017-09-26 08:59:34 +02:00
}
}
}
#endif
static void takeScreenshot()
{
2018-01-24 12:01:19 +01:00
studioImpl.video.frames = 1;
2018-02-06 20:15:56 +01:00
studioImpl.video.buffer = malloc(FRAME_SIZE);
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
if(studioImpl.video.buffer)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
studioImpl.video.record = true;
studioImpl.video.frame = 0;
2017-09-26 08:59:34 +02:00
}
}
2018-02-13 11:29:33 +01:00
static inline bool keyWasPressedOnce(s32 key)
2017-09-26 08:59:34 +02:00
{
2018-02-01 17:11:59 +01:00
tic_mem* tic = studioImpl.studio.tic;
return tic->api.keyp(tic, key, -1, -1);
}
static void processShortcuts()
{
tic_mem* tic = studioImpl.studio.tic;
2017-12-07 14:42:06 +01:00
2018-02-01 17:11:59 +01:00
if(studioImpl.mode == TIC_START_MODE) return;
if(studioImpl.mode == TIC_CONSOLE_MODE && !studioImpl.console->active) return;
2018-02-13 10:44:16 +01:00
bool alt = tic->api.key(tic, tic_key_alt);
bool ctrl = tic->api.key(tic, tic_key_ctrl);
2017-09-26 08:59:34 +02:00
if(isGameMenu())
{
2018-02-13 11:29:33 +01:00
if(keyWasPressedOnce(tic_key_escape))
2018-02-13 10:44:16 +01:00
{
studioImpl.mode == TIC_MENU_MODE ? hideGameMenu() : showGameMenu();
// studioImpl.gamepad.backProcessed = true;
}
2018-02-13 11:29:33 +01:00
else if(keyWasPressedOnce(tic_key_f11)) goFullscreen();
else if(keyWasPressedOnce(tic_key_return))
2018-02-13 10:44:16 +01:00
{
if(alt) goFullscreen();
}
2018-02-13 11:29:33 +01:00
else if(keyWasPressedOnce(tic_key_f7)) setCoverImage();
else if(keyWasPressedOnce(tic_key_f8)) takeScreenshot();
2018-02-13 10:44:16 +01:00
#if !defined(__EMSCRIPTEN__)
2018-02-13 11:29:33 +01:00
else if(keyWasPressedOnce(tic_key_f9)) startVideoRecord();
2018-02-13 10:44:16 +01:00
#endif
2017-09-26 08:59:34 +02:00
2018-02-13 11:38:11 +01:00
return;
2018-02-13 10:44:16 +01:00
}
2018-02-01 17:11:59 +01:00
if(alt)
{
2018-02-13 11:29:33 +01:00
if(keyWasPressedOnce(tic_key_grave)) setStudioMode(TIC_CONSOLE_MODE);
else if(keyWasPressedOnce(tic_key_1)) setStudioMode(TIC_CODE_MODE);
else if(keyWasPressedOnce(tic_key_2)) setStudioMode(TIC_SPRITE_MODE);
else if(keyWasPressedOnce(tic_key_3)) setStudioMode(TIC_MAP_MODE);
else if(keyWasPressedOnce(tic_key_4)) setStudioMode(TIC_SFX_MODE);
else if(keyWasPressedOnce(tic_key_5)) setStudioMode(TIC_MUSIC_MODE);
else if(keyWasPressedOnce(tic_key_return)) goFullscreen();
2017-09-26 08:59:34 +02:00
}
2018-02-01 17:34:42 +01:00
else if(ctrl)
{
2018-02-13 11:29:33 +01:00
if(keyWasPressedOnce(tic_key_pageup)) changeStudioMode(-1);
else if(keyWasPressedOnce(tic_key_pagedown)) changeStudioMode(1);
else if(keyWasPressedOnce(tic_key_q)) exitStudio();
else if(keyWasPressedOnce(tic_key_r)) runProject();
else if(keyWasPressedOnce(tic_key_return)) runProject();
else if(keyWasPressedOnce(tic_key_s)) saveProject();
2018-02-01 17:34:42 +01:00
}
2017-09-26 08:59:34 +02:00
else
{
2018-02-13 11:29:33 +01:00
if(keyWasPressedOnce(tic_key_f1)) setStudioMode(TIC_CODE_MODE);
else if(keyWasPressedOnce(tic_key_f2)) setStudioMode(TIC_SPRITE_MODE);
else if(keyWasPressedOnce(tic_key_f3)) setStudioMode(TIC_MAP_MODE);
else if(keyWasPressedOnce(tic_key_f4)) setStudioMode(TIC_SFX_MODE);
else if(keyWasPressedOnce(tic_key_f5)) setStudioMode(TIC_MUSIC_MODE);
else if(keyWasPressedOnce(tic_key_f7)) setCoverImage();
else if(keyWasPressedOnce(tic_key_f8)) takeScreenshot();
2017-09-26 08:59:34 +02:00
#if !defined(__EMSCRIPTEN__)
2018-02-13 11:29:33 +01:00
else if(keyWasPressedOnce(tic_key_f9)) startVideoRecord();
2017-09-26 08:59:34 +02:00
#endif
2018-02-13 11:29:33 +01:00
else if(keyWasPressedOnce(tic_key_f11)) goFullscreen();
else if(keyWasPressedOnce(tic_key_escape))
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
Code* code = studioImpl.editor[studioImpl.bank.index.code].code;
2017-12-14 12:38:06 +01:00
2018-01-24 12:01:19 +01:00
if(studioImpl.mode == TIC_CODE_MODE && code->mode != TEXT_EDIT_MODE)
2017-09-26 08:59:34 +02:00
{
2017-12-14 12:38:06 +01:00
code->escape(code);
2018-02-01 17:11:59 +01:00
return;
2017-09-26 08:59:34 +02:00
}
2018-01-24 12:01:19 +01:00
if(studioImpl.mode == TIC_DIALOG_MODE)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
studioImpl.dialog->escape(studioImpl.dialog);
2018-02-01 17:11:59 +01:00
return;
2017-09-26 08:59:34 +02:00
}
2018-01-24 12:01:19 +01:00
setStudioMode(studioImpl.mode == TIC_CONSOLE_MODE ? studioImpl.prevMode : TIC_CONSOLE_MODE);
2017-09-26 08:59:34 +02:00
}
}
}
2018-02-13 14:02:05 +01:00
#if defined(TIC80_PRO)
2017-09-26 08:59:34 +02:00
2018-02-13 14:02:05 +01:00
static void reloadConfirm(bool yes, void* data)
{
if(yes)
studioImpl.console->updateProject(studioImpl.console);
}
2018-02-13 14:02:05 +01:00
#endif
2017-12-29 13:25:29 +01:00
2018-02-13 14:02:05 +01:00
void updateStudioProject()
{
#if defined(TIC80_PRO)
2017-12-29 13:25:29 +01:00
2018-02-13 14:02:05 +01:00
if(studioImpl.mode != TIC_START_MODE)
{
Console* console = studioImpl.console;
2017-12-29 13:25:29 +01:00
2018-02-13 14:02:05 +01:00
u64 mdate = fsMDate(console->fs, console->romName);
2018-02-13 14:02:05 +01:00
if(studioImpl.cart.mdate && mdate > studioImpl.cart.mdate)
{
if(studioCartChanged())
{
static const char* Rows[] =
{
"",
"CART HAS CHANGED!",
"",
"DO YOU WANT",
"TO RELOAD IT?"
};
showDialog(Rows, COUNT_OF(Rows), reloadConfirm, NULL);
}
else console->updateProject(console);
}
}
2017-11-22 08:20:12 +01:00
2018-02-13 14:02:05 +01:00
#endif
{
Code* code = studioImpl.editor[studioImpl.bank.index.code].code;
studioImpl.console->codeLiveReload.reload(studioImpl.console, code->src);
if(studioImpl.console->codeLiveReload.active && code->update)
code->update(code);
}
2017-11-21 17:47:36 +01:00
2018-02-13 14:02:05 +01:00
}
2017-11-22 08:20:12 +01:00
static void drawRecordLabel(u32* frame, s32 sx, s32 sy, const u32* color)
2017-09-26 08:59:34 +02:00
{
static const u16 RecLabel[] =
2017-09-26 08:59:34 +02:00
{
0b0111001100110011,
0b1111101010100100,
0b1111101100110100,
0b1111101010100100,
0b0111001010110011,
};
2017-09-26 08:59:34 +02:00
for(s32 y = 0; y < 5; y++)
{
for(s32 x = 0; x < sizeof RecLabel[0]*BITS_IN_BYTE; x++)
{
if(RecLabel[y] & (1 << x))
2017-12-01 07:40:06 +01:00
memcpy(&frame[sx + 15 - x + ((y+sy) << TIC80_FULLWIDTH_BITS)], color, sizeof *color);
}
2017-09-26 08:59:34 +02:00
}
}
static void drawDesyncLabel(u32* frame)
{
2018-01-24 12:01:19 +01:00
if(getConfig()->showSync && studioImpl.missedFrame)
{
static const u16 DesyncLabel[] =
{
0b0110101010010011,
0b1000101011010100,
0b1110111010110100,
0b0010001010010100,
0b1100110010010011,
};
enum{sx = TIC80_WIDTH-24, sy = 8, Cols = sizeof DesyncLabel[0]*BITS_IN_BYTE, Rows = COUNT_OF(DesyncLabel)};
2018-01-24 12:01:19 +01:00
const u32* pal = tic_palette_blit(&studioImpl.studio.tic->config.palette);
const u32* color = &pal[tic_color_red];
for(s32 y = 0; y < Rows; y++)
{
for(s32 x = 0; x < Cols; x++)
{
if(DesyncLabel[y] & (1 << x))
memcpy(&frame[sx + Cols - 1 - x + ((y+sy) << TIC80_FULLWIDTH_BITS)], color, sizeof *color);
}
}
}
}
static void recordFrame(u32* pixels)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
if(studioImpl.video.record)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
if(studioImpl.video.frame < studioImpl.video.frames)
2017-09-26 08:59:34 +02:00
{
2018-02-01 17:34:42 +01:00
tic_rect rect = {0, 0, TIC80_FULLWIDTH, TIC80_FULLHEIGHT};
2018-01-24 12:01:19 +01:00
screen2buffer(studioImpl.video.buffer + (TIC80_FULLWIDTH*TIC80_FULLHEIGHT) * studioImpl.video.frame, pixels, rect);
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
if(studioImpl.video.frame % TIC_FRAMERATE < TIC_FRAMERATE / 2)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
const u32* pal = tic_palette_blit(&studioImpl.studio.tic->config.palette);
drawRecordLabel(pixels, TIC80_WIDTH-24, 8, &pal[tic_color_red]);
2017-09-26 08:59:34 +02:00
}
2018-01-24 12:01:19 +01:00
studioImpl.video.frame++;
2017-09-26 08:59:34 +02:00
}
else
{
stopVideoRecord();
}
}
}
2017-10-20 10:48:04 +02:00
static void useSystemPalette()
2017-10-20 08:58:35 +02:00
{
2018-01-24 12:01:19 +01:00
memcpy(studioImpl.studio.tic->ram.vram.palette.data, studioImpl.studio.tic->config.palette.data, sizeof(tic_palette));
2017-10-20 08:58:35 +02:00
}
2017-12-08 13:03:59 +01:00
static void drawPopup()
{
2018-01-24 12:01:19 +01:00
if(studioImpl.popup.counter > 0)
2017-12-08 13:03:59 +01:00
{
2018-01-24 12:01:19 +01:00
studioImpl.popup.counter--;
2017-12-08 13:03:59 +01:00
s32 anim = 0;
enum{Dur = TIC_FRAMERATE/2};
2018-01-24 12:01:19 +01:00
if(studioImpl.popup.counter < Dur)
anim = -((Dur - studioImpl.popup.counter) * (TIC_FONT_HEIGHT+1) / Dur);
else if(studioImpl.popup.counter >= (POPUP_DUR - Dur))
anim = (((POPUP_DUR - Dur) - studioImpl.popup.counter) * (TIC_FONT_HEIGHT+1) / Dur);
2017-12-08 13:03:59 +01:00
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.rect(studioImpl.studio.tic, 0, anim, TIC80_WIDTH, TIC_FONT_HEIGHT+1, (tic_color_red));
studioImpl.studio.tic->api.text(studioImpl.studio.tic, studioImpl.popup.message,
(s32)(TIC80_WIDTH - strlen(studioImpl.popup.message)*TIC_FONT_WIDTH)/2,
2017-12-08 13:03:59 +01:00
anim + 1, (tic_color_white));
}
}
2017-09-26 08:59:34 +02:00
static void renderStudio()
{
2018-01-24 12:01:19 +01:00
tic_mem* tic = studioImpl.studio.tic;
2017-09-26 08:59:34 +02:00
showTooltip("");
2018-01-24 12:01:19 +01:00
studioImpl.gesture.active = false;
2017-09-26 08:59:34 +02:00
{
2017-12-10 10:12:09 +01:00
const tic_sfx* sfx = NULL;
const tic_music* music = NULL;
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
switch(studioImpl.mode)
2017-09-26 08:59:34 +02:00
{
case TIC_RUN_MODE:
2018-01-24 12:01:19 +01:00
sfx = &studioImpl.studio.tic->ram.sfx;
music = &studioImpl.studio.tic->ram.music;
2017-09-26 08:59:34 +02:00
break;
case TIC_START_MODE:
case TIC_DIALOG_MODE:
case TIC_MENU_MODE:
case TIC_SURF_MODE:
2018-01-24 12:01:19 +01:00
sfx = &studioImpl.studio.tic->config.bank0.sfx;
music = &studioImpl.studio.tic->config.bank0.music;
2017-09-26 08:59:34 +02:00
break;
default:
2018-01-24 12:01:19 +01:00
sfx = &studioImpl.studio.tic->cart.banks[studioImpl.bank.index.sfx].sfx;
music = &studioImpl.studio.tic->cart.banks[studioImpl.bank.index.music].music;
2017-09-26 08:59:34 +02:00
}
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.tick_start(studioImpl.studio.tic, sfx, music);
2017-09-26 08:59:34 +02:00
}
2018-01-24 12:01:19 +01:00
switch(studioImpl.mode)
2017-09-26 08:59:34 +02:00
{
2018-01-24 12:01:19 +01:00
case TIC_START_MODE: studioImpl.start->tick(studioImpl.start); break;
case TIC_CONSOLE_MODE: studioImpl.console->tick(studioImpl.console); break;
case TIC_RUN_MODE: studioImpl.run->tick(studioImpl.run); break;
2017-12-14 12:38:06 +01:00
case TIC_CODE_MODE:
{
2018-01-24 12:01:19 +01:00
Code* code = studioImpl.editor[studioImpl.bank.index.code].code;
2017-12-14 12:38:06 +01:00
code->tick(code);
}
break;
2017-12-14 15:08:04 +01:00
case TIC_SPRITE_MODE:
{
2018-01-24 12:01:19 +01:00
Sprite* sprite = studioImpl.editor[studioImpl.bank.index.sprites].sprite;
2017-12-14 15:08:04 +01:00
sprite->tick(sprite);
}
break;
case TIC_MAP_MODE:
{
2018-01-24 12:01:19 +01:00
Map* map = studioImpl.editor[studioImpl.bank.index.map].map;
2017-12-14 15:08:04 +01:00
map->tick(map);
}
break;
case TIC_SFX_MODE:
{
2018-01-24 12:01:19 +01:00
Sfx* sfx = studioImpl.editor[studioImpl.bank.index.sfx].sfx;
2017-12-14 15:08:04 +01:00
sfx->tick(sfx);
}
break;
case TIC_MUSIC_MODE:
{
2018-01-24 12:01:19 +01:00
Music* music = studioImpl.editor[studioImpl.bank.index.music].music;
2017-12-14 15:08:04 +01:00
music->tick(music);
}
break;
2018-01-24 12:01:19 +01:00
case TIC_WORLD_MODE: studioImpl.world->tick(studioImpl.world); break;
case TIC_DIALOG_MODE: studioImpl.dialog->tick(studioImpl.dialog); break;
case TIC_MENU_MODE: studioImpl.menu->tick(studioImpl.menu); break;
case TIC_SURF_MODE: studioImpl.surf->tick(studioImpl.surf); break;
2017-09-26 08:59:34 +02:00
default: break;
}
2017-12-08 13:03:59 +01:00
drawPopup();
2017-09-26 08:59:34 +02:00
if(getConfig()->noSound)
2018-02-06 20:15:56 +01:00
memset(tic->ram.registers, 0, sizeof tic->ram.registers);
2018-01-24 12:01:19 +01:00
studioImpl.studio.tic->api.tick_end(studioImpl.studio.tic);
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
if(studioImpl.mode != TIC_RUN_MODE)
2017-10-20 11:25:05 +02:00
useSystemPalette();
2018-01-24 10:20:05 +01:00
// renderCursor();
}
2017-09-26 08:59:34 +02:00
static void updateSystemFont()
{
2018-02-06 20:15:56 +01:00
memset(studioImpl.studio.tic->font.data, 0, sizeof(tic_font));
2017-09-26 08:59:34 +02:00
for(s32 i = 0; i < TIC_FONT_CHARS; i++)
for(s32 y = 0; y < TIC_SPRITESIZE; y++)
for(s32 x = 0; x < TIC_SPRITESIZE; x++)
2018-01-24 12:01:19 +01:00
if(tic_tool_peek4(&studioImpl.studio.tic->config.bank0.sprites.data[i], TIC_SPRITESIZE*(y+1) - x-1))
studioImpl.studio.tic->font.data[i*BITS_IN_BYTE+y] |= 1 << x;
2017-09-26 08:59:34 +02:00
}
void studioConfigChanged()
{
2018-01-24 12:01:19 +01:00
Code* code = studioImpl.editor[studioImpl.bank.index.code].code;
2017-12-14 12:38:06 +01:00
if(code->update)
code->update(code);
2017-09-26 08:59:34 +02:00
2018-01-24 10:20:05 +01:00
// initTouchGamepad();
2017-09-26 08:59:34 +02:00
updateSystemFont();
}
u32 unzip(u8** dest, const u8* source, size_t size)
{
// TODO: remove this size
enum{DestSize = 10*1024*1024};
unsigned long destSize = DestSize;
2018-02-06 20:15:56 +01:00
u8* buffer = (u8*)malloc(destSize);
2017-09-26 08:59:34 +02:00
if(buffer)
{
if(uncompress(buffer, &destSize, source, (unsigned long)size) == Z_OK)
{
2018-02-06 20:15:56 +01:00
*dest = (u8*)malloc(destSize+1);
2017-09-26 08:59:34 +02:00
memcpy(*dest, buffer, destSize);
(*dest)[destSize] = '\0';
}
2018-02-06 20:15:56 +01:00
free(buffer);
2017-09-26 08:59:34 +02:00
return destSize;
}
return 0;
}
static void initKeymap()
{
2018-02-13 11:33:55 +01:00
FileSystem* fs = studioImpl.fs;
2018-02-13 11:33:55 +01:00
s32 size = 0;
u8* data = (u8*)fsLoadFile(fs, KEYMAP_DAT_PATH, &size);
2018-02-13 11:33:55 +01:00
if(data)
{
if(size == KEYMAP_SIZE)
memcpy(getKeymap(), data, KEYMAP_SIZE);
2018-02-13 11:33:55 +01:00
free(data);
}
}
2018-02-14 11:58:46 +01:00
// #if defined(__EMSCRIPTEN__)
// #define DEFAULT_CART "cart.tic"
// static void onEmscriptenWget(const char* file)
// {
// studioImpl.argv[1] = DEFAULT_CART;
// createFileSystem(NULL, onFSInitialized);
// }
// static void onEmscriptenWgetError(const char* error) {}
// static void emstick()
// {
// static double nextTick = -1.0;
// studioImpl.missedFrame = false;
// if(nextTick < 0.0)
// nextTick = emscripten_get_now();
// nextTick += 1000.0/TIC_FRAMERATE;
// tick();
// double delay = nextTick - emscripten_get_now();
// if(delay < 0.0)
// {
// nextTick -= delay;
// studioImpl.missedFrame = true;
// }
// else
// emscripten_set_main_loop_timing(EM_TIMING_SETTIMEOUT, delay);
// }
// #endif
// #if defined(__EMSCRIPTEN__)
// if(studioImpl.argc == 2)
// {
// emscripten_async_wget(studioImpl.argv[1], DEFAULT_CART, onEmscriptenWget, onEmscriptenWgetError);
// }
// else createFileSystem(NULL, onFSInitialized);
// // emscripten_set_main_loop(emstick, TIC_FRAMERATE, 1);
// #else
// FileSystem* fs = createFileSystem(argc > 1 && fsExists(argv[1]) ? fsBasename(argv[1]) : folder);
// onFSInitialized(fs);
// #endif
Studio* studioInit(s32 argc, char **argv, s32 samplerate, const char* folder, System* system)
2017-09-26 08:59:34 +02:00
{
2018-02-14 11:58:46 +01:00
setbuf(stdout, NULL);
studioImpl.argc = argc;
studioImpl.argv = argv;
studioImpl.samplerate = samplerate;
studioImpl.system = system;
studioImpl.fs = createFileSystem(argc > 1 && fsExists(argv[1]) ? fsBasename(argv[1]) : folder);
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
studioImpl.tic80local = (tic80_local*)tic80_create(studioImpl.samplerate);
studioImpl.studio.tic = studioImpl.tic80local->memory;
2017-09-26 08:59:34 +02:00
2017-12-13 18:14:32 +01:00
{
2017-12-14 15:19:35 +01:00
for(s32 i = 0; i < TIC_EDITOR_BANKS; i++)
2017-12-14 15:08:04 +01:00
{
2018-02-06 20:15:56 +01:00
studioImpl.editor[i].code = calloc(1, sizeof(Code));
studioImpl.editor[i].sprite = calloc(1, sizeof(Sprite));
studioImpl.editor[i].map = calloc(1, sizeof(Map));
studioImpl.editor[i].sfx = calloc(1, sizeof(Sfx));
studioImpl.editor[i].music = calloc(1, sizeof(Music));
2017-12-14 15:08:04 +01:00
}
2018-02-06 20:15:56 +01:00
studioImpl.start = calloc(1, sizeof(Start));
studioImpl.console = calloc(1, sizeof(Console));
studioImpl.run = calloc(1, sizeof(Run));
studioImpl.world = calloc(1, sizeof(World));
studioImpl.config = calloc(1, sizeof(Config));
studioImpl.dialog = calloc(1, sizeof(Dialog));
studioImpl.menu = calloc(1, sizeof(Menu));
studioImpl.surf = calloc(1, sizeof(Surf));
2017-12-13 18:14:32 +01:00
}
2018-02-14 11:58:46 +01:00
fsMakeDir(studioImpl.fs, TIC_LOCAL);
2018-01-24 12:01:19 +01:00
initConfig(studioImpl.config, studioImpl.studio.tic, studioImpl.fs);
initKeymap();
2017-09-26 08:59:34 +02:00
2018-01-24 12:01:19 +01:00
initStart(studioImpl.start, studioImpl.studio.tic);
initConsole(studioImpl.console, studioImpl.studio.tic, studioImpl.fs, studioImpl.config, studioImpl.argc, studioImpl.argv);
2017-09-26 08:59:34 +02:00
initSurfMode();
initRunMode();
initModules();
2018-01-24 12:01:19 +01:00
if(studioImpl.console->skipStart)
2017-09-26 08:59:34 +02:00
{
setStudioMode(TIC_CONSOLE_MODE);
}
2018-01-24 12:01:19 +01:00
if(studioImpl.console->goFullscreen)
{
goFullscreen();
2017-09-26 08:59:34 +02:00
}
2018-01-04 13:22:33 +01:00
2018-01-24 12:01:19 +01:00
return &studioImpl.studio;
2018-01-24 10:20:05 +01:00
}
2018-01-04 13:22:33 +01:00
2018-01-24 12:55:13 +01:00
static void processMouseStates()
{
for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++)
studioImpl.mouse.state[i].click = false;
tic_mem* tic = studioImpl.studio.tic;
2018-02-12 12:51:57 +01:00
tic->ram.vram.vars.cursor.sprite = tic_cursor_arrow;
tic->ram.vram.vars.cursor.system = true;
2018-01-24 12:55:13 +01:00
for(int i = 0; i < COUNT_OF(studioImpl.mouse.state); i++)
{
MouseState* state = &studioImpl.mouse.state[i];
2018-02-01 16:32:31 +01:00
if(!state->down && (tic->ram.input.mouse.btns & (1 << i)))
2018-01-24 12:55:13 +01:00
{
state->down = true;
state->start.x = tic->ram.input.mouse.x;
state->start.y = tic->ram.input.mouse.y;
}
2018-02-01 16:32:31 +01:00
else if(state->down && !(tic->ram.input.mouse.btns & (1 << i)))
2018-01-24 12:55:13 +01:00
{
state->end.x = tic->ram.input.mouse.x;
state->end.y = tic->ram.input.mouse.y;
state->click = true;
state->down = false;
}
}
}
2018-01-24 10:20:05 +01:00
void studioTick(void* pixels)
{
2018-02-01 17:11:59 +01:00
processShortcuts();
2018-01-24 12:55:13 +01:00
processMouseStates();
2018-01-24 15:56:55 +01:00
processGamepadMapping();
2018-01-24 12:55:13 +01:00
2018-01-24 10:20:05 +01:00
renderStudio();
2018-01-04 13:22:33 +01:00
2018-01-24 12:01:19 +01:00
tic_mem* tic = studioImpl.studio.tic;
2018-01-24 10:20:05 +01:00
{
tic_scanline scanline = NULL;
tic_overlap overlap = NULL;
void* data = NULL;
2018-01-24 12:01:19 +01:00
switch(studioImpl.mode)
{
2018-01-24 10:20:05 +01:00
case TIC_RUN_MODE:
scanline = tic->api.scanline;
overlap = tic->api.overlap;
break;
case TIC_SPRITE_MODE:
{
2018-01-24 12:01:19 +01:00
Sprite* sprite = studioImpl.editor[studioImpl.bank.index.sprites].sprite;
2018-01-24 10:20:05 +01:00
overlap = sprite->overlap;
data = sprite;
}
2018-01-24 10:20:05 +01:00
break;
case TIC_MAP_MODE:
{
2018-01-24 12:01:19 +01:00
Map* map = studioImpl.editor[studioImpl.bank.index.map].map;
2018-01-24 10:20:05 +01:00
overlap = map->overlap;
data = map;
}
break;
default:
break;
}
2018-01-24 10:20:05 +01:00
tic->api.blit(tic, scanline, overlap, data);
2018-02-06 20:15:56 +01:00
memcpy(pixels, tic->screen, sizeof tic->screen);
2017-09-26 08:59:34 +02:00
2018-01-24 10:20:05 +01:00
recordFrame(pixels);
drawDesyncLabel(pixels);
}
}
void studioClose()
{
2017-12-13 19:14:03 +01:00
{
2017-12-14 15:19:35 +01:00
for(s32 i = 0; i < TIC_EDITOR_BANKS; i++)
2017-12-14 15:08:04 +01:00
{
2018-02-06 20:15:56 +01:00
free(studioImpl.editor[i].code);
free(studioImpl.editor[i].sprite);
free(studioImpl.editor[i].map);
free(studioImpl.editor[i].sfx);
free(studioImpl.editor[i].music);
2017-12-14 15:08:04 +01:00
}
2017-12-14 12:38:06 +01:00
2018-02-06 20:15:56 +01:00
free(studioImpl.start);
free(studioImpl.console);
free(studioImpl.run);
free(studioImpl.world);
free(studioImpl.config);
free(studioImpl.dialog);
free(studioImpl.menu);
free(studioImpl.surf);
2017-12-13 19:14:03 +01:00
}
2018-01-24 12:01:19 +01:00
if(studioImpl.tic80local)
tic80_delete((tic80*)studioImpl.tic80local);
2018-02-07 16:21:24 +01:00
}
void setClipboardText(const char* text)
{
studioImpl.system->setClipboardText(text);
}
bool hasClipboardText()
{
return studioImpl.system->hasClipboardText();
}
char* getClipboardText()
{
return studioImpl.system->getClipboardText();
}
u64 getPerformanceCounter()
{
return studioImpl.system->getPerformanceCounter();
}
u64 getPerformanceFrequency()
{
return studioImpl.system->getPerformanceFrequency();
}
2018-02-08 12:26:17 +01:00
void _file_dialog_load(file_dialog_load_callback callback, void* data)
{
studioImpl.system->file_dialog_load(callback, data);
}
void _file_dialog_save(file_dialog_save_callback callback, const char* name, const u8* buffer, size_t size, void* data, u32 mode)
{
studioImpl.system->file_dialog_save(callback, name, buffer, size, data, mode);
}
2018-02-08 13:29:14 +01:00
2018-02-12 17:22:15 +01:00
void showMessageBox(const char* title, const char* message)
{
2018-02-13 11:33:55 +01:00
studioImpl.system->showMessageBox(title, message);
2018-02-13 17:39:26 +01:00
}
void openSystemPath(const char* path)
{
studioImpl.system->openSystemPath(path);
2018-02-14 12:31:10 +01:00
}
void* getUrlRequest(const char* url, s32* size)
{
return studioImpl.system->getUrlRequest(url, size);
2018-02-08 13:29:14 +01:00
}