citro3d
Loading...
Searching...
No Matches
tex3ds.h
Go to the documentation of this file.
1/*------------------------------------------------------------------------------
2 * Copyright (c) 2017
3 * Michael Theall (mtheall)
4 *
5 * This file is part of citro3d.
6 *
7 * This software is provided 'as-is', without any express or implied warranty.
8 * In no event will the authors be held liable for any damages arising from the
9 * use of this software.
10 *
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, subject to the following restrictions:
14 *
15 * 1. The origin of this software must not be misrepresented; you must not
16 * claim that you wrote the original software. If you use this software in
17 * a product, an acknowledgment in the product documentation would be
18 * appreciated but is not required.
19 * 2. Altered source versions must be plainly marked as such, and must not be
20 * misrepresented as being the original software.
21 * 3. This notice may not be removed or altered from any source distribution.
22 *----------------------------------------------------------------------------*/
23/** @file tex3ds.h
24 * @brief tex3ds support
25 */
26#pragma once
27#ifdef CITRO3D_BUILD
28#include "c3d/texture.h"
29#else
30#include <citro3d.h>
31#endif
32
33#include <stdint.h>
34#include <stdio.h>
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/** @brief Subtexture
41 * @note If top < bottom, the subtexture is rotated 1/4 revolution counter-clockwise
42 */
43typedef struct Tex3DS_SubTexture
44{
45 u16 width; ///< Sub-texture width (pixels)
46 u16 height; ///< Sub-texture height (pixels)
47 float left; ///< Left u-coordinate
48 float top; ///< Top v-coordinate
49 float right; ///< Right u-coordinate
50 float bottom; ///< Bottom v-coordinate
52
53/** @brief Texture */
54typedef struct Tex3DS_Texture_s* Tex3DS_Texture;
55
56/** @brief Import Tex3DS texture
57 * @param[in] input Input data
58 * @param[in] insize Size of the input data
59 * @param[out] tex citro3d texture
60 * @param[out] texcube citro3d texcube
61 * @param[in] vram Whether to store textures in VRAM
62 * @returns Tex3DS texture
63 */
64Tex3DS_Texture Tex3DS_TextureImport(const void* input, size_t insize, C3D_Tex* tex, C3D_TexCube* texcube, bool vram);
65
66/** @brief Import Tex3DS texture
67 *
68 * @description
69 * For example, use this if you want to import from a large file without
70 * pulling the entire file into memory.
71 *
72 * @param[out] tex citro3d texture
73 * @param[out] texcube citro3d texcube
74 * @param[in] vram Whether to store textures in VRAM
75 * @param[in] callback Data callback
76 * @param[in] userdata User data passed to callback
77 * @returns Tex3DS texture
78 */
79Tex3DS_Texture Tex3DS_TextureImportCallback(C3D_Tex* tex, C3D_TexCube* texcube, bool vram, decompressCallback callback, void* userdata);
80
81/** @brief Import Tex3DS texture
82 *
83 * Starts reading at the current file descriptor's offset. The file
84 * descriptor's position is left at the end of the decoded data. On error, the
85 * file descriptor's position is indeterminate.
86 *
87 * @param[in] fd Open file descriptor
88 * @param[out] tex citro3d texture
89 * @param[out] texcube citro3d texcube
90 * @param[in] vram Whether to store textures in VRAM
91 * @returns Tex3DS texture
92 */
93Tex3DS_Texture Tex3DS_TextureImportFD(int fd, C3D_Tex* tex, C3D_TexCube* texcube, bool vram);
94
95/** @brief Import Tex3DS texture
96 *
97 * Starts reading at the current file stream's offset. The file stream's
98 * position is left at the end of the decoded data. On error, the file
99 * stream's position is indeterminate.
100 *
101 * @param[in] fp Open file stream
102 * @param[out] tex citro3d texture
103 * @param[out] texcube citro3d texcube
104 * @param[in] vram Whether to store textures in VRAM
105 * @returns Tex3DS texture
106 */
107Tex3DS_Texture Tex3DS_TextureImportStdio(FILE* fp, C3D_Tex* tex, C3D_TexCube* texcube, bool vram);
108
109/** @brief Get number of subtextures
110 * @param[in] texture Tex3DS texture
111 * @returns Number of subtextures
112 */
114
115/** @brief Get subtexture
116 * @param[in] texture Tex3DS texture
117 * @param[in] index Subtexture index
118 * @returns Subtexture info
119 */
120const Tex3DS_SubTexture* Tex3DS_GetSubTexture(const Tex3DS_Texture texture, size_t index);
121
122/** @brief Check if subtexture is rotated
123 * @param[in] subtex Subtexture to check
124 * @returns whether subtexture is rotated
125 */
126static inline bool
128{
129 return subtex->top < subtex->bottom;
130}
131
132/** @brief Get bottom-left texcoords
133 * @param[in] subtex Subtexture
134 * @param[out] u u-coordinate
135 * @param[out] v v-coordinate
136 */
137static inline void
138Tex3DS_SubTextureBottomLeft(const Tex3DS_SubTexture* subtex, float* u, float* v)
139{
140 if (!Tex3DS_SubTextureRotated(subtex))
141 {
142 *u = subtex->left;
143 *v = subtex->bottom;
144 } else
145 {
146 *u = subtex->bottom;
147 *v = subtex->left;
148 }
149}
150
151/** @brief Get bottom-right texcoords
152 * @param[in] subtex Subtexture
153 * @param[out] u u-coordinate
154 * @param[out] v v-coordinate
155 */
156static inline void
157Tex3DS_SubTextureBottomRight(const Tex3DS_SubTexture* subtex, float* u, float* v)
158{
159 if (!Tex3DS_SubTextureRotated(subtex))
160 {
161 *u = subtex->right;
162 *v = subtex->bottom;
163 } else
164 {
165 *u = subtex->bottom;
166 *v = subtex->right;
167 }
168}
169
170/** @brief Get top-left texcoords
171 * @param[in] subtex Subtexture
172 * @param[out] u u-coordinate
173 * @param[out] v v-coordinate
174 */
175static inline void
176Tex3DS_SubTextureTopLeft(const Tex3DS_SubTexture* subtex, float* u, float* v)
177{
178 if (!Tex3DS_SubTextureRotated(subtex))
179 {
180 *u = subtex->left;
181 *v = subtex->top;
182 } else
183 {
184 *u = subtex->top;
185 *v = subtex->left;
186 }
187}
188
189/** @brief Get top-right texcoords
190 * @param[in] subtex Subtexture
191 * @param[out] u u-coordinate
192 * @param[out] v v-coordinate
193 */
194static inline void
195Tex3DS_SubTextureTopRight(const Tex3DS_SubTexture* subtex, float* u, float* v)
196{
197 if (!Tex3DS_SubTextureRotated(subtex))
198 {
199 *u = subtex->right;
200 *v = subtex->top;
201 } else
202 {
203 *u = subtex->top;
204 *v = subtex->right;
205 }
206}
207
208/** @brief Free Tex3DS texture
209 * @param[in] texture Tex3DS texture to free
210 */
212
213#ifdef __cplusplus
214}
215#endif
Central citro3d header. Includes all others.
Cubemap texture data.
Definition texture.h:10
Texture data.
Definition texture.h:16
Subtexture.
Definition tex3ds.h:44
u16 height
Sub-texture height (pixels)
Definition tex3ds.h:46
float left
Left u-coordinate.
Definition tex3ds.h:47
float right
Right u-coordinate.
Definition tex3ds.h:49
float top
Top v-coordinate.
Definition tex3ds.h:48
float bottom
Bottom v-coordinate.
Definition tex3ds.h:50
u16 width
Sub-texture width (pixels)
Definition tex3ds.h:45
static void Tex3DS_SubTextureBottomLeft(const Tex3DS_SubTexture *subtex, float *u, float *v)
Get bottom-left texcoords.
Definition tex3ds.h:138
Tex3DS_Texture Tex3DS_TextureImportFD(int fd, C3D_Tex *tex, C3D_TexCube *texcube, bool vram)
Import Tex3DS texture.
static void Tex3DS_SubTextureBottomRight(const Tex3DS_SubTexture *subtex, float *u, float *v)
Get bottom-right texcoords.
Definition tex3ds.h:157
Tex3DS_Texture Tex3DS_TextureImportStdio(FILE *fp, C3D_Tex *tex, C3D_TexCube *texcube, bool vram)
Import Tex3DS texture.
void Tex3DS_TextureFree(Tex3DS_Texture texture)
Free Tex3DS texture.
struct Tex3DS_Texture_s * Tex3DS_Texture
Texture.
Definition tex3ds.h:54
Tex3DS_Texture Tex3DS_TextureImportCallback(C3D_Tex *tex, C3D_TexCube *texcube, bool vram, decompressCallback callback, void *userdata)
Import Tex3DS texture.
size_t Tex3DS_GetNumSubTextures(const Tex3DS_Texture texture)
Get number of subtextures.
static void Tex3DS_SubTextureTopRight(const Tex3DS_SubTexture *subtex, float *u, float *v)
Get top-right texcoords.
Definition tex3ds.h:195
const Tex3DS_SubTexture * Tex3DS_GetSubTexture(const Tex3DS_Texture texture, size_t index)
Get subtexture.
static void Tex3DS_SubTextureTopLeft(const Tex3DS_SubTexture *subtex, float *u, float *v)
Get top-left texcoords.
Definition tex3ds.h:176
static bool Tex3DS_SubTextureRotated(const Tex3DS_SubTexture *subtex)
Check if subtexture is rotated.
Definition tex3ds.h:127
Tex3DS_Texture Tex3DS_TextureImport(const void *input, size_t insize, C3D_Tex *tex, C3D_TexCube *texcube, bool vram)
Import Tex3DS texture.
Create and manipulate textures.
uint16_t u16