WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit d630b3f

Browse files
Copilottsa96
andcommitted
Add partial C# bindings for vtfpp from PR craftablescience#72
Co-authored-by: tsa96 <[email protected]>
1 parent 91a1c67 commit d630b3f

File tree

6 files changed

+811
-16
lines changed

6 files changed

+811
-16
lines changed

cmake/AddSourcePPLibrary.cmake

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
include_guard(GLOBAL)
2-
31
function(add_sourcepp_library TARGET)
4-
cmake_parse_arguments(PARSE_ARGV 1 OPTIONS "C;CSHARP;PYTHON;WASM;TEST;BENCH" "" "")
2+
cmake_parse_arguments(PARSE_ARGV 1 OPTIONS "C;CSHARP;PYTHON;TEST;BENCH" "" "")
53
string(TOUPPER ${TARGET} TARGET_UPPER)
64
if(SOURCEPP_USE_${TARGET_UPPER})
75
set(PROPAGATE_VARS "")
@@ -21,23 +19,22 @@ function(add_sourcepp_library TARGET)
2119
configure_file("${CMAKE_CURRENT_SOURCE_DIR}/lang/csharp/src/sourcepp/TARGET.csproj.in" "${CMAKE_CURRENT_SOURCE_DIR}/lang/csharp/src/${TARGET}/${TARGET}.csproj")
2220
add_custom_target(sourcepp_${TARGET}_csharp DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/lang/csharp/src/${TARGET}/${TARGET}.csproj")
2321
add_dependencies(sourcepp_${TARGET}_csharp sourcepp::${TARGET}c)
24-
add_custom_command(TARGET sourcepp::${TARGET}c POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR}/sourcepp_${TARGET}c${CMAKE_SHARED_LIBRARY_SUFFIX}" "${CMAKE_CURRENT_SOURCE_DIR}/lang/csharp/src/${TARGET}")
22+
23+
# Quick hack to let tell VS to place the dlls in the right spot
24+
# Can be removed/dropped if needed
25+
if(CMAKE_GENERATOR MATCHES "Visual Studio")
26+
add_custom_command(TARGET sourcepp::${TARGET}c POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR}/${CMAKE_CFG_INTDIR}/sourcepp_${TARGET}c${CMAKE_SHARED_LIBRARY_SUFFIX}" "${CMAKE_CURRENT_SOURCE_DIR}/lang/csharp/src/${TARGET}")
27+
else()
28+
add_custom_command(TARGET sourcepp::${TARGET}c POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR}/sourcepp_${TARGET}c${CMAKE_SHARED_LIBRARY_SUFFIX}" "${CMAKE_CURRENT_SOURCE_DIR}/lang/csharp/src/${TARGET}")
29+
endif()
2530
endif()
2631

2732
# Add Python
2833
if(SOURCEPP_BUILD_PYTHON_WRAPPERS AND OPTIONS_PYTHON)
29-
list(APPEND ${${PROJECT_NAME}_PYTHON}_DEPS sourcepp::${TARGET})
30-
list(APPEND ${${PROJECT_NAME}_PYTHON}_DEFINES ${TARGET_UPPER})
31-
list(APPEND ${${PROJECT_NAME}_PYTHON}_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/${TARGET}.h")
32-
list(APPEND PROPAGATE_VARS ${${PROJECT_NAME}_PYTHON}_DEPS ${${PROJECT_NAME}_PYTHON}_DEFINES ${${PROJECT_NAME}_PYTHON}_SOURCES)
33-
endif()
34-
35-
# Add WASM
36-
if(SOURCEPP_BUILD_WASM_WRAPPERS AND OPTIONS_WASM)
37-
list(APPEND ${${PROJECT_NAME}_WASM}_DEPS sourcepp::${TARGET})
38-
list(APPEND ${${PROJECT_NAME}_WASM}_DEFINES ${TARGET_UPPER})
39-
list(APPEND ${${PROJECT_NAME}_WASM}_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/lang/wasm/src/${TARGET}.h")
40-
list(APPEND PROPAGATE_VARS ${${PROJECT_NAME}_WASM}_DEPS ${${PROJECT_NAME}_WASM}_DEFINES ${${PROJECT_NAME}_WASM}_SOURCES)
34+
list(APPEND ${SOURCEPP_PYTHON_NAME}_DEPS sourcepp::${TARGET})
35+
list(APPEND ${SOURCEPP_PYTHON_NAME}_DEFINES ${TARGET_UPPER})
36+
list(APPEND ${SOURCEPP_PYTHON_NAME}_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/lang/python/src/${TARGET}.h")
37+
list(APPEND PROPAGATE_VARS ${SOURCEPP_PYTHON_NAME}_DEPS ${SOURCEPP_PYTHON_NAME}_DEFINES ${SOURCEPP_PYTHON_NAME}_SOURCES)
4138
endif()
4239

4340
# Add tests
Lines changed: 318 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,318 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.InteropServices;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace vtfpp
9+
{
10+
public enum ImageFormat
11+
{
12+
RGBA8888 = 0,
13+
ABGR8888,
14+
RGB888,
15+
BGR888,
16+
RGB565,
17+
I8,
18+
IA88,
19+
P8,
20+
A8,
21+
RGB888_BLUESCREEN,
22+
BGR888_BLUESCREEN,
23+
ARGB8888,
24+
BGRA8888,
25+
DXT1,
26+
DXT3,
27+
DXT5,
28+
BGRX8888,
29+
BGR565,
30+
BGRX5551,
31+
BGRA4444,
32+
DXT1_ONE_BIT_ALPHA,
33+
BGRA5551,
34+
UV88,
35+
UVWQ8888,
36+
RGBA16161616F,
37+
RGBA16161616,
38+
UVLX8888,
39+
R32F,
40+
RGB323232F,
41+
RGBA32323232F,
42+
RG1616F,
43+
RG3232F,
44+
RGBX8888,
45+
EMPTY,
46+
ATI2N,
47+
ATI1N,
48+
RGBA1010102,
49+
BGRA1010102,
50+
R16F,
51+
52+
CONSOLE_BGRX8888_LINEAR = 42,
53+
CONSOLE_RGBA8888_LINEAR,
54+
CONSOLE_ABGR8888_LINEAR,
55+
CONSOLE_ARGB8888_LINEAR,
56+
CONSOLE_BGRA8888_LINEAR,
57+
CONSOLE_RGB888_LINEAR,
58+
CONSOLE_BGR888_LINEAR,
59+
CONSOLE_BGRX5551_LINEAR,
60+
CONSOLE_I8_LINEAR,
61+
CONSOLE_RGBA16161616_LINEAR,
62+
CONSOLE_BGRX8888_LE,
63+
CONSOLE_BGRA8888_LE,
64+
65+
R8 = 69,
66+
BC7,
67+
BC6H,
68+
}
69+
70+
71+
internal static unsafe partial class Extern
72+
{
73+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_red")]
74+
public static partial sbyte ImageFormatDetailsRed(ImageFormat format);
75+
76+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_decompressed_red")]
77+
public static partial sbyte ImageFormatDetailsDecompressedRed(ImageFormat format);
78+
79+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_green")]
80+
public static partial sbyte ImageFormatDetailsGreen(ImageFormat format);
81+
82+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_decompressed_green")]
83+
public static partial sbyte ImageFormatDetailsDecompressedGreen(ImageFormat format);
84+
85+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_blue")]
86+
public static partial sbyte ImageFormatDetailsBlue(ImageFormat format);
87+
88+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_decompressed_blue")]
89+
public static partial sbyte ImageFormatDetailsDecompressedBlue(ImageFormat format);
90+
91+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_alpha")]
92+
public static partial sbyte ImageFormatDetailsAlpha(ImageFormat format);
93+
94+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_decompressed_alpha")]
95+
public static partial sbyte ImageFormatDetailsDecompressedAlpha(ImageFormat format);
96+
97+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_bpp")]
98+
public static partial byte ImageFormatDetailsBPP(ImageFormat format);
99+
100+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_container_format")]
101+
public static partial ImageFormat ImageFormatDetailsContainerFormat(ImageFormat format);
102+
103+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_large")]
104+
public static partial int ImageFormatDetailsLarge(ImageFormat format);
105+
106+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_decimal")]
107+
public static partial int ImageFormatDetailsDecimal(ImageFormat format);
108+
109+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_compressed")]
110+
public static partial int ImageFormatDetailsCompressed(ImageFormat format);
111+
112+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_transparent")]
113+
public static partial int ImageFormatDetailsTransparent(ImageFormat format);
114+
115+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_opaque")]
116+
public static partial int ImageFormatDetailsOpaque(ImageFormat format);
117+
118+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_dimensions_get_mip_dim")]
119+
public static partial uint ImageDimensionsGetMipDim(byte mip, ushort dim);
120+
121+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_dimensions_get_recommended_mip_count_for_dim")]
122+
public static partial byte ImageDimensionsGetRecommendedMipCountForDim(ImageFormat format, ushort width, ushort height);
123+
124+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_dimensions_get_actual_mip_count_for_dims_on_console")]
125+
public static partial byte ImageDimensionsGetActualMipCountForDimsOnConsole(ushort width, ushort height);
126+
127+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_get_data_length")]
128+
public static partial uint ImageFormatDetailsGetDataLength(ImageFormat format, ushort width, ushort height, ushort sliceCount);
129+
130+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_get_data_length_ex")]
131+
public static partial uint ImageFormatDetailsGetDataLengthEx(ImageFormat format, byte mipCount, ushort frameCount, byte faceCount, ushort width, ushort height, ushort sliceCount);
132+
133+
[LibraryImport("sourcepp_vtfppc", EntryPoint = "vtfpp_image_format_details_get_data_position")]
134+
public static partial int ImageFormatGetDataPosition(uint* offset, uint* length, ImageFormat format, byte mip, byte mipCount, ushort frame, ushort frameCount, byte face, byte faceCount, ushort width, ushort height, ushort slice, ushort sliceCount);
135+
}
136+
137+
public class ImageFormatDetails
138+
{
139+
public static sbyte Red(ImageFormat format)
140+
{
141+
unsafe
142+
{
143+
return Extern.ImageFormatDetailsRed(format);
144+
}
145+
}
146+
147+
public static sbyte DecompressedRed(ImageFormat format)
148+
{
149+
unsafe
150+
{
151+
return Extern.ImageFormatDetailsDecompressedRed(format);
152+
}
153+
}
154+
155+
public static sbyte Green(ImageFormat format)
156+
{
157+
unsafe
158+
{
159+
return Extern.ImageFormatDetailsGreen(format);
160+
}
161+
}
162+
163+
public static sbyte DecompressedGreen(ImageFormat format)
164+
{
165+
unsafe
166+
{
167+
return Extern.ImageFormatDetailsDecompressedGreen(format);
168+
}
169+
}
170+
171+
public static sbyte Blue(ImageFormat format)
172+
{
173+
unsafe
174+
{
175+
return Extern.ImageFormatDetailsBlue(format);
176+
}
177+
}
178+
179+
public static sbyte DecompressedBlue(ImageFormat format)
180+
{
181+
unsafe
182+
{
183+
return Extern.ImageFormatDetailsDecompressedBlue(format);
184+
}
185+
}
186+
187+
public static sbyte Alpha(ImageFormat format)
188+
{
189+
unsafe
190+
{
191+
return Extern.ImageFormatDetailsAlpha(format);
192+
}
193+
}
194+
195+
public static sbyte DecompressedAlpha(ImageFormat format)
196+
{
197+
unsafe
198+
{
199+
return Extern.ImageFormatDetailsDecompressedAlpha(format);
200+
}
201+
}
202+
203+
public static byte BPP(ImageFormat format)
204+
{
205+
unsafe
206+
{
207+
return Extern.ImageFormatDetailsBPP(format);
208+
}
209+
}
210+
211+
public static ImageFormat ContainerFormat(ImageFormat format)
212+
{
213+
unsafe
214+
{
215+
return Extern.ImageFormatDetailsContainerFormat(format);
216+
}
217+
}
218+
219+
public static bool Large(ImageFormat format)
220+
{
221+
unsafe
222+
{
223+
return Convert.ToBoolean(Extern.ImageFormatDetailsLarge(format));
224+
}
225+
}
226+
227+
public static bool Decimal(ImageFormat format)
228+
{
229+
unsafe
230+
{
231+
return Convert.ToBoolean(Extern.ImageFormatDetailsDecimal(format));
232+
}
233+
}
234+
235+
public static bool Compressed(ImageFormat format)
236+
{
237+
unsafe
238+
{
239+
return Convert.ToBoolean(Extern.ImageFormatDetailsCompressed(format));
240+
}
241+
}
242+
243+
public static bool Transparent(ImageFormat format)
244+
{
245+
unsafe
246+
{
247+
return Convert.ToBoolean(Extern.ImageFormatDetailsTransparent(format));
248+
}
249+
}
250+
251+
public static bool Opaque(ImageFormat format)
252+
{
253+
unsafe
254+
{
255+
return Convert.ToBoolean(Extern.ImageFormatDetailsOpaque(format));
256+
}
257+
}
258+
259+
public static uint GetDataLength(ImageFormat format, ushort width, ushort height, ushort sliceCount)
260+
{
261+
unsafe
262+
{
263+
return Extern.ImageFormatDetailsGetDataLength(format, width, height, sliceCount);
264+
}
265+
}
266+
267+
public static uint GetDataLength(ImageFormat format, byte mipCount, ushort frameCount, byte faceCount, ushort width, ushort height, ushort sliceCount)
268+
{
269+
unsafe
270+
{
271+
return Extern.ImageFormatDetailsGetDataLengthEx(format, mipCount, frameCount, faceCount, width, height, sliceCount);
272+
}
273+
}
274+
275+
public static bool GetDataPosition(ref uint offset, ref uint length, ImageFormat format, byte mip, byte mipCount, ushort frame, byte face, byte faceCount, ushort frameCount, ushort width, ushort height, ushort slice = 0, ushort sliceCount = 1)
276+
{
277+
unsafe
278+
{
279+
/// @todo Is there a better way for this?
280+
fixed (uint* offsetPtr = &offset)
281+
{
282+
fixed (uint* lengthPtr = &length)
283+
{
284+
return Convert.ToBoolean(Extern.ImageFormatGetDataPosition(offsetPtr, lengthPtr, format, mip, mipCount, frame, frameCount, face, faceCount, width, height, slice, sliceCount));
285+
}
286+
}
287+
}
288+
}
289+
}
290+
291+
class ImageDimensions
292+
{
293+
public static uint GetMipDim(byte mip, ushort dim)
294+
{
295+
unsafe
296+
{
297+
return Extern.ImageDimensionsGetMipDim(mip, dim);
298+
}
299+
}
300+
301+
public static byte GetRecommendedMipCountForDim(ImageFormat format, ushort width, ushort height)
302+
{
303+
unsafe
304+
{
305+
return Extern.ImageDimensionsGetRecommendedMipCountForDim(format, width, height);
306+
}
307+
}
308+
309+
public static byte GetActualMipCountForDimsOnConsole(ushort width, ushort height)
310+
{
311+
unsafe
312+
{
313+
return Extern.ImageDimensionsGetActualMipCountForDimsOnConsole(width, height);
314+
}
315+
}
316+
317+
}
318+
}

0 commit comments

Comments
 (0)