all repos — mgba @ df8cddb289bece5bd4ed0db9355048ecd006d12c

mGBA Game Boy Advance Emulator

src/gba/supervisor/export.c (view raw)

 1/* Copyright (c) 2013-2015 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#include "export.h"
 7
 8#include "gba/video.h"
 9#include "util/vfs.h"
10
11bool GBAExportPaletteRIFF(struct VFile* vf, size_t entries, const uint16_t* colors) {
12	if (entries > 0xFFFF) {
13		return false;
14	}
15	uint32_t chunkSize = 4 + 4 * entries;
16	uint32_t size = chunkSize + 12;
17
18	// Header
19	if (vf->write(vf, "RIFF", 4) < 4) {
20		return false;
21	}
22	if (VFileWrite32LE(vf, size) < 4) {
23		return false;
24	}
25	if (vf->write(vf, "PAL ", 4) < 4) {
26		return false;
27	}
28
29	// Data chunk
30	if (vf->write(vf, "data", 4) < 4) {
31		return false;
32	}
33	if (VFileWrite32LE(vf, chunkSize) < 4) {
34		return false;
35	}
36	if (VFileWrite16LE(vf, 0x0300) < 2) {
37		return false;
38	}
39	if (VFileWrite16LE(vf, entries) < 2) {
40		return false;
41	}
42
43	size_t i;
44	for (i = 0; i < entries; ++i) {
45		uint8_t block[4] = {
46			GBA_R8(colors[i]),
47			GBA_G8(colors[i]),
48			GBA_B8(colors[i]),
49			0
50		};
51		if (vf->write(vf, block, 4) < 4) {
52			return false;
53		}
54	}
55
56	return true;
57}
58
59bool GBAExportPaletteACT(struct VFile* vf, size_t entries, const uint16_t* colors) {
60	if (entries > 256) {
61		return false;
62	}
63	size_t i;
64	for (i = 0; i < entries; ++i) {
65		uint8_t block[3] = {
66			GBA_R8(colors[i]),
67			GBA_G8(colors[i]),
68			GBA_B8(colors[i]),
69		};
70		if (vf->write(vf, block, 3) < 3) {
71			return false;
72		}
73	}
74	for (; i < 256; ++i) {
75		uint8_t block[3] = { 0, 0, 0 };
76		if (vf->write(vf, block, 3) < 3) {
77			return false;
78		}
79	}
80	return true;
81}