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 "api.h"
26#include "tic.h"
27#include <stddef.h>
28
29inline s32 tic_tool_sfx_pos(s32 speed, s32 ticks)
30{
31 return speed > 0 ? ticks * (1 + speed) : ticks / (1 - speed);
32}
33
34#define POKE_N(P,I,V,A,B,C,D) do \
35{ \
36 u8* val = (u8*)(P) + ((I) >> (A)); \
37 u8 offset = ((I) & (B)) << (C); \
38 *val &= ~((D) << offset); \
39 *val |= ((V) & (D)) << offset; \
40} while(0)
41
42#define PEEK_N(P,I,A,B,C,D) ( ( ((u8*)(P))[((I) >> (A))] >> ( ((I) & (B)) << (C) ) ) & (D) )
43
44inline void tic_tool_poke4(void* addr, u32 index, u8 value)
45{
46 POKE_N(addr, index, value, 1,1,2,15);
47}
48
49inline u8 tic_tool_peek4(const void* addr, u32 index)
50{
51 return PEEK_N(addr, index, 1,1,2,15);
52}
53
54inline void tic_tool_poke2(void* addr, u32 index, u8 value)
55{
56 POKE_N(addr, index, value, 2,3,1,3);
57}
58
59inline u8 tic_tool_peek2(const void* addr, u32 index)
60{
61 return PEEK_N(addr, index, 2,3,1,3);
62}
63
64inline void tic_tool_poke1(void* addr, u32 index, u8 value)
65{
66 POKE_N(addr, index, value, 3,7,0,1);
67}
68
69inline u8 tic_tool_peek1(const void* addr, u32 index)
70{
71 return PEEK_N(addr, index, 3,7,0,1);
72}
73
74#undef PEEK_N
75#undef POKE_N
76
77inline u32 tic_rgba(const tic_rgb* c)
78{
79 return (0xff << 24) | (c->b << 16) | (c->g << 8) | (c->r << 0);
80}
81
82inline s32 tic_modulo(s32 x, s32 m)
83{
84 if(x >= m) return x % m;
85 if(x < 0) return x % m + m;
86
87 return x;
88}
89
90tic_blitpal tic_tool_palette_blit(const tic_palette* src, tic80_pixel_color_format fmt);
91
92bool tic_tool_parse_note(const char* noteStr, s32* note, s32* octave);
93s32 tic_tool_get_pattern_id(const tic_track* track, s32 frame, s32 channel);
94void tic_tool_set_pattern_id(tic_track* track, s32 frame, s32 channel, s32 id);
95bool tic_project_ext(const char* name);
96bool tic_tool_has_ext(const char* name, const char* ext);
97s32 tic_tool_get_track_row_sfx(const tic_track_row* row);
98void tic_tool_set_track_row_sfx(tic_track_row* row, s32 sfx);
99void tic_tool_str2buf(const char* str, s32 size, void* buf, bool flip);
100
101u32 tic_tool_zip(void* dest, s32 destSize, const void* source, s32 size);
102u32 tic_tool_unzip(void* dest, s32 bufSize, const void* source, s32 size);
103
104bool tic_tool_empty(const void* buffer, s32 size);
105#define EMPTY(BUFFER) (tic_tool_empty((BUFFER), sizeof (BUFFER)))
106
107bool tic_tool_flat4(const void* buffer, s32 size);
108#define FLAT4(BUFFER) (tic_tool_flat4((BUFFER), sizeof (BUFFER)))
109
110bool tic_tool_noise(const tic_waveform* wave);
111u32 tic_nearest_color(const tic_rgb* palette, const tic_rgb* color, s32 count);
112char* tic_tool_metatag(const char* code, const char* tag, const char* comment);