1// MIT License
2
3// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com
4
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22
23#pragma once
24
25#include <stdbool.h>
26#include <string.h>
27#include <stdio.h>
28#include <stdlib.h>
29#include <stddef.h>
30
31#include "tic.h"
32#include "api.h"
33#include "defines.h"
34#include "tools.h"
35#include "system.h"
36#include "anim.h"
37#include "ext/png.h"
38
39#define KEYBOARD_HOLD 20
40#define KEYBOARD_PERIOD 3
41
42#define TIC_LOCAL ".local/"
43#define TIC_LOCAL_VERSION TIC_LOCAL TIC_VERSION_HASH "/"
44#define TIC_CACHE TIC_LOCAL "cache/"
45
46#define TOOLBAR_SIZE 7
47#define STUDIO_TEXT_WIDTH (TIC_FONT_WIDTH)
48#define STUDIO_TEXT_HEIGHT (TIC_FONT_HEIGHT+1)
49#define STUDIO_TEXT_BUFFER_WIDTH (TIC80_WIDTH / STUDIO_TEXT_WIDTH)
50#define STUDIO_TEXT_BUFFER_HEIGHT (TIC80_HEIGHT / STUDIO_TEXT_HEIGHT)
51#define STUDIO_TEXT_BUFFER_SIZE (STUDIO_TEXT_BUFFER_WIDTH * STUDIO_TEXT_BUFFER_HEIGHT)
52#define STUDIO_ANIM_TIME 8
53
54#define TIC_COLOR_BG tic_color_black
55
56#define CONFIG_TIC "config.tic"
57#define CONFIG_TIC_PATH TIC_LOCAL_VERSION CONFIG_TIC
58
59#define CART_EXT ".tic"
60#define PNG_EXT ".png"
61
62#if defined(CRT_SHADER_SUPPORT)
63# define CRT_CMD_PARAM(macro) \
64 macro(crt, bool, BOOLEAN, "", "enable CRT monitor effect")
65#else
66# define CRT_CMD_PARAM(macro)
67#endif
68
69#define CMD_PARAMS_LIST(macro) \
70 macro(skip, bool, BOOLEAN, "", "skip startup animation") \
71 macro(volume, s32, INTEGER, "=<int>", "global volume value [0-15]") \
72 macro(cli, bool, BOOLEAN, "", "console only output") \
73 macro(fullscreen, bool, BOOLEAN, "", "enable fullscreen mode") \
74 macro(vsync, bool, BOOLEAN, "", "enable VSYNC") \
75 macro(soft, bool, BOOLEAN, "", "use software rendering") \
76 macro(fs, char*, STRING, "=<str>", "path to the file system folder") \
77 macro(scale, s32, INTEGER, "=<int>", "main window scale") \
78 macro(cmd, char*, STRING, "=<str>", "run commands in the console") \
79 macro(keepcmd, bool, BOOLEAN, "", "re-execute commands on every run") \
80 macro(version, bool, BOOLEAN, "", "print program version") \
81 CRT_CMD_PARAM(macro)
82
83#define SHOW_TOOLTIP(STUDIO, FORMAT, ...) \
84do{ \
85 static const char Format[] = FORMAT; \
86 static char buf[sizeof Format]; \
87 sprintf(buf, Format, __VA_ARGS__); \
88 showTooltip(STUDIO, buf); \
89}while(0)
90
91typedef struct
92{
93 char *cart;
94#define CMD_PARAMS_DEF(name, ctype, type, post, help) ctype name;
95 CMD_PARAMS_LIST(CMD_PARAMS_DEF)
96#undef CMD_PARAMS_DEF
97} StartArgs;
98
99typedef enum
100{
101 TIC_START_MODE,
102 TIC_CONSOLE_MODE,
103 TIC_RUN_MODE,
104 TIC_CODE_MODE,
105 TIC_SPRITE_MODE,
106 TIC_MAP_MODE,
107 TIC_WORLD_MODE,
108 TIC_SFX_MODE,
109 TIC_MUSIC_MODE,
110 TIC_MENU_MODE,
111 TIC_SURF_MODE,
112
113 TIC_MODES_COUNT
114} EditorMode;
115
116enum
117{
118 tic_icon_cut = 80,
119 tic_icon_copy = 81,
120 tic_icon_paste = 82,
121 tic_icon_undo = 83,
122 tic_icon_redo = 84,
123 tic_icon_bank = 85,
124 tic_icon_pin = 86,
125 tic_icon_tab = 87,
126 tic_icon_code = 88,
127 tic_icon_sprite = 89,
128 tic_icon_map = 90,
129 tic_icon_sfx = 91,
130 tic_icon_music = 92,
131 tic_icon_rec = 93,
132 tic_icon_rec2 = 94,
133 tic_icon_bookmark = 95,
134 tic_icon_shadow = 96,
135 tic_icon_shadow2 = 97,
136 tic_icon_run = 98,
137 tic_icon_hand = 99,
138 tic_icon_find = 100,
139 tic_icon_goto = 101,
140 tic_icon_outline = 102,
141 tic_icon_world = 103,
142 tic_icon_grid = 104,
143 tic_icon_down = 105,
144 tic_icon_up = 106,
145 tic_icon_fill = 107,
146 tic_icon_select = 108,
147 tic_icon_pen = 109,
148 tic_icon_tiles = 110,
149 tic_icon_sprites = 111,
150 tic_icon_left = 112,
151 tic_icon_right = 113,
152 tic_icon_piano = 114,
153 tic_icon_tracker = 115,
154 tic_icon_follow = 116,
155 tic_icon_sustain = 117,
156 tic_icon_playnow = 118,
157 tic_icon_playframe = 119,
158 tic_icon_stop = 120,
159 tic_icon_rgb = 121,
160 tic_icon_tinyleft = 122,
161 tic_icon_pos = 123,
162 tic_icon_tinyright = 124,
163 tic_icon_bigup = 125,
164 tic_icon_bigdown = 126,
165 tic_icon_bigleft = 127,
166 tic_icon_bigright = 128,
167 tic_icon_fliphorz = 129,
168 tic_icon_flipvert = 130,
169 tic_icon_rotate = 131,
170 tic_icon_erase = 132,
171 tic_icon_bigpen = 133,
172 tic_icon_bigpicker = 134,
173 tic_icon_bigselect = 135,
174 tic_icon_bigfill = 136,
175 tic_icon_loop = 137,
176};
177
178void setCursor(Studio* studio, tic_cursor id);
179
180bool checkMousePos(Studio* studio, const tic_rect* rect);
181bool checkMouseClick(Studio* studio, const tic_rect* rect, tic_mouse_btn button);
182bool checkMouseDblClick(Studio* studio, const tic_rect* rect, tic_mouse_btn button);
183bool checkMouseDown(Studio* studio, const tic_rect* rect, tic_mouse_btn button);
184
185void drawToolbar(Studio* studio, tic_mem* tic, bool bg);
186void drawBitIcon(Studio* studio, s32 id, s32 x, s32 y, u8 color);
187
188tic_cartridge* loadPngCart(png_buffer buffer);
189void studioRomLoaded(Studio* studio);
190void studioRomSaved(Studio* studio);
191void studioConfigChanged(Studio* studio);
192
193void setStudioMode(Studio* studio, EditorMode mode);
194EditorMode getStudioMode(Studio* studio);
195void exitStudio(Studio* studio);
196
197void toClipboard(const void* data, s32 size, bool flip);
198bool fromClipboard(void* data, s32 size, bool flip, bool remove_white_spaces, bool sameSize);
199
200typedef enum
201{
202 TIC_CLIPBOARD_NONE,
203 TIC_CLIPBOARD_CUT,
204 TIC_CLIPBOARD_COPY,
205 TIC_CLIPBOARD_PASTE,
206} ClipboardEvent;
207
208ClipboardEvent getClipboardEvent(Studio* studio);
209
210typedef enum
211{
212 TIC_TOOLBAR_CUT,
213 TIC_TOOLBAR_COPY,
214 TIC_TOOLBAR_PASTE,
215 TIC_TOOLBAR_UNDO,
216 TIC_TOOLBAR_REDO,
217} StudioEvent;
218
219void setStudioEvent(Studio* studio, StudioEvent event);
220void showTooltip(Studio* studio, const char* text);
221
222void setSpritePixel(tic_tile* tiles, s32 x, s32 y, u8 color);
223u8 getSpritePixel(tic_tile* tiles, s32 x, s32 y);
224
225typedef void(*ConfirmCallback)(Studio* studio, bool yes, void* data);
226void confirmDialog(Studio* studio, const char** text, s32 rows, ConfirmCallback callback, void* data);
227void confirmLoadCart(Studio* studio, ConfirmCallback callback, void* data);
228
229bool studioCartChanged(Studio* studio);
230void playSystemSfx(Studio* studio, s32 id);
231
232void gotoMenu(Studio* studio);
233void gotoCode(Studio* studio);
234void gotoSurf(Studio* studio);
235
236void runGame(Studio* studio);
237void exitGame(Studio* studio);
238void resumeGame(Studio* studio);
239
240tic_tiles* getBankTiles(Studio* studio);
241tic_palette* getBankPalette(Studio* studio, bool bank);
242tic_flags* getBankFlags(Studio* studio);
243tic_map* getBankMap(Studio* studio);
244
245char getKeyboardText(Studio* studio);
246bool keyWasPressed(Studio* studio, tic_key key);
247bool anyKeyWasPressed(Studio* studio);
248
249const StudioConfig* getConfig(Studio* studio);
250struct Start* getStartScreen(Studio* studio);
251struct Sprite* getSpriteEditor(Studio* studio);
252
253const char* studioExportMusic(Studio* studio, s32 track, const char* filename);
254const char* studioExportSfx(Studio* studio, s32 sfx, const char* filename);
255
256tic_mem* getMemory(Studio* studio);
257
258const char* md5str(const void* data, s32 length);
259void sfx_stop(tic_mem* tic, s32 channel);
260s32 calcWaveAnimation(tic_mem* tic, u32 index, s32 channel);
261void map2ram(tic_ram* ram, const tic_map* src);
262void tiles2ram(tic_ram* ram, const tic_tiles* src);
263void fadePalette(tic_palette* pal, s32 value);
264