1// MIT License
2
3// Copyright (c) 2017 Vadim Grigoruk @nesbox // grigoruk@gmail.com
4// Damien de Lemeny @ddelemeny // hello@ddelemeny.me
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to deal
8// in the Software without restriction, including without limitation the rights
9// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10// copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in all
14// copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22// SOFTWARE.
23
24#include "tilesheet.h"
25
26#include <stdlib.h>
27
28static const tic_blit_segment segments[] = {
29 // +page +nb_pages
30 // | +bank +bank_size
31 // | | | | +sheet_width
32 // | | | | | +tile_width
33 {0, 0, 1, 256, 16, 8, TIC_SPRITESIZE, tic_tool_peek1, tic_tool_poke1}, // system gfx
34 {0, 0, 1, 256, 16, 8, TIC_SPRITESIZE, tic_tool_peek1, tic_tool_poke1}, // system font
35 {0, 0, 1, 256, 16, 8, sizeof(tic_tile), tic_tool_peek4, tic_tool_poke4}, // 4bpp p0 bg
36 {0, 1, 1, 256, 16, 8, sizeof(tic_tile), tic_tool_peek4, tic_tool_poke4}, // 4bpp p0 fg
37
38 {0, 0, 2, 512, 32, 16, sizeof(tic_tile), tic_tool_peek2, tic_tool_poke2}, // 2bpp p0 bg
39 {1, 0, 2, 512, 32, 16, sizeof(tic_tile), tic_tool_peek2, tic_tool_poke2}, // 2bpp p1 bg
40 {0, 1, 2, 512, 32, 16, sizeof(tic_tile), tic_tool_peek2, tic_tool_poke2}, // 2bpp p0 fg
41 {1, 1, 2, 512, 32, 16, sizeof(tic_tile), tic_tool_peek2, tic_tool_poke2}, // 2bpp p1 fg
42
43 {0, 0, 4, 1024, 64, 32, sizeof(tic_tile), tic_tool_peek1, tic_tool_poke1}, // 1bpp p0 bg
44 {1, 0, 4, 1024, 64, 32, sizeof(tic_tile), tic_tool_peek1, tic_tool_poke1}, // 1bpp p1 bg
45 {2, 0, 4, 1024, 64, 32, sizeof(tic_tile), tic_tool_peek1, tic_tool_poke1}, // 1bpp p2 bg
46 {3, 0, 4, 1024, 64, 32, sizeof(tic_tile), tic_tool_peek1, tic_tool_poke1}, // 1bpp p3 bg
47 {0, 1, 4, 1024, 64, 32, sizeof(tic_tile), tic_tool_peek1, tic_tool_poke1}, // 1bpp p0 fg
48 {1, 1, 4, 1024, 64, 32, sizeof(tic_tile), tic_tool_peek1, tic_tool_poke1}, // 1bpp p1 fg
49 {2, 1, 4, 1024, 64, 32, sizeof(tic_tile), tic_tool_peek1, tic_tool_poke1}, // 1bpp p2 fg
50 {3, 1, 4, 1024, 64, 32, sizeof(tic_tile), tic_tool_peek1, tic_tool_poke1}, // 1bpp p3 fg
51};
52
53extern u8 tic_tilesheet_getpix(const tic_tilesheet* sheet, s32 x, s32 y);
54extern void tic_tilesheet_setpix(const tic_tilesheet* sheet, s32 x, s32 y, u8 value);
55extern u8 tic_tilesheet_gettilepix(const tic_tileptr* tile, s32 x, s32 y);
56extern void tic_tilesheet_settilepix(const tic_tileptr* tile, s32 x, s32 y, u8 value);
57
58tic_tilesheet tic_tilesheet_get(u8 segment, u8* ptr)
59{
60 return (tic_tilesheet) { &segments[segment], ptr };
61}
62
63tic_tileptr tic_tilesheet_gettile(const tic_tilesheet* sheet, s32 index, bool local)
64{
65 enum { Cols = 16, Size = 8 };
66 const tic_blit_segment* segment = sheet->segment;
67
68 s32 bank, page, iy, ix;
69 if (local) {
70 index = index & 255;
71 bank = segment->bank_orig;
72 page = segment->page_orig;
73 div_t ixy = div(index, Cols);
74 iy = ixy.quot;
75 ix = ixy.rem;
76 }
77 else {
78 // reindex
79 div_t ia = div(index, segment->bank_size); // bank, bank_index
80 div_t ib = div(ia.rem, segment->sheet_width); // yi, bank_xi
81 div_t ic = div(ib.rem, Cols); // page, xi
82 bank = (ia.quot + segment->bank_orig) % 2;
83 page = (ic.quot + segment->page_orig) % segment->nb_pages;
84 iy = ib.quot % Cols;
85 ix = ic.rem;
86 }
87
88 div_t xdiv = div(ix, segment->nb_pages); // xbuffer, xoffset
89 u32 ptr_offset = (bank * Cols + iy) * Cols + page * Cols / segment->nb_pages + xdiv.quot;
90 u8* ptr = sheet->ptr + segment->ptr_size * ptr_offset;
91 u32 offset = (xdiv.rem * Size);
92
93 return (tic_tileptr) { segment, offset, ptr };
94}
95
96extern s32 tic_blit_calc_segment(const tic_blit* blit);
97extern void tic_blit_update_bpp(tic_blit* blit, tic_bpp bpp);
98extern s32 tic_blit_calc_index(const tic_blit* blit);
99