Skip to content

Commit

Permalink
Calling managed function from native code
Browse files Browse the repository at this point in the history
  • Loading branch information
JeppeSRC committed Feb 5, 2018
1 parent b5aca33 commit faf5253
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 27 deletions.
10 changes: 10 additions & 0 deletions FDConverter/src/converter/converter.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
#include "converter.h"

#include <stdio.h>

namespace FD {

void ConvertImage(const char* path, const char* outPath, TextureChannel channel, TextureChannelType type, FPProgressCallBack callback) {
callback(50);
}

}

1 change: 1 addition & 0 deletions FDconverter/FDconverter.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\converter\converter.h" />
<ClInclude Include="src\lib\stb_image.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\converter\converter.cpp" />
Expand Down
3 changes: 3 additions & 0 deletions FDconverter/FDconverter.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<ClInclude Include="src\converter\converter.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\lib\stb_image.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="src\converter\converter.cpp">
Expand Down
16 changes: 16 additions & 0 deletions FDconverter/src/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@

namespace FD {

enum class TextureChannel {
R,
RG,
RGB,
RGBA,
};

enum class TextureChannelType {
Uint8,
Uint16,
Uint32,
Float32
};

typedef void(__stdcall* FPProgressCallBack)(int);

void __stdcall ConvertImage(const char* path, const char* outPath, TextureChannel channel, TextureChannelType type, FPProgressCallBack callback);

}
2 changes: 2 additions & 0 deletions FDconverterCLI/FDconverterCLI.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<InlineFunctionExpansion>Disabled</InlineFunctionExpansion>
<AdditionalIncludeDirectories>src;$(SolutionDir)FDconverter\src</AdditionalIncludeDirectories>
<AssemblerOutput>AssemblyAndSourceCode</AssemblerOutput>
<PreprocessorDefinitions>FD_CLI;_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -77,6 +78,7 @@
<FloatingPointModel>Fast</FloatingPointModel>
<AdditionalIncludeDirectories>src;$(SolutionDir)FDconverter\src</AdditionalIncludeDirectories>
<AssemblerOutput>AssemblyAndSourceCode</AssemblerOutput>
<PreprocessorDefinitions>FD_CLI;_WINDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
Expand Down
11 changes: 10 additions & 1 deletion FDconverterCLI/src/converter/convertercli.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#include "convertercli.h"
#include <converter/converter.h>

using namespace System;
using namespace System::Runtime::InteropServices;


typedef void(__stdcall *FP)(const char*, const char*, FD::TextureChannel, FD::TextureChannelType, FDconverterCLI::FPProgressCallBack^);

namespace FDconverterCLI {


bool FDConverter::ConvertImage(System::String^ path, System::String^ outPath, TextureChannel channel, TextureChannelType type, FPProgressCallBack^ callback) {


FP fp = (FP)FD::ConvertImage;

fp(0, 0, FD::TextureChannel::R, FD::TextureChannelType::Uint8, callback);

return false;
}
Expand Down
2 changes: 2 additions & 0 deletions FDconverterCLI/src/converter/convertercli.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <converter/converter.h>

namespace FDconverterCLI {

Expand All @@ -17,6 +18,7 @@ public enum class TextureChannelType {
Float32
};


public delegate void FPProgressCallBack(int);

public ref class FDConverter {
Expand Down
8 changes: 4 additions & 4 deletions FDconverterUI/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ private static FileType GetFileTypeFromExtension(String filename) {

private int _progress;
private FileType _type;
private bool _included;
private bool? _included;

public string path { get; }
public long size { get; }
public string sizeString { get; set; }
public FileType type { get { return _type; } set { _type = value; OnPropertyChanged(); } }
public int progress { get { return _progress; } set { _progress = value; OnPropertyChanged(); } }
public bool included { get { return _included; } set { _included = value; OnPropertyChanged(); } }
public bool? included { get { return _included; } set { _included = value; OnPropertyChanged(); } }

public event PropertyChangedEventHandler PropertyChanged;

Expand All @@ -73,7 +73,7 @@ protected FDFile(FDFile other, FileType type) {
path = other.path;
size = other.size;
this.type = type;
progress = 0;
progress = -1;
included = other.included;
sizeString = other.sizeString;
}
Expand All @@ -83,7 +83,7 @@ protected FDFile(string path, FileType type) {
this.path = path;
this.size = file.Length;
this.type = type;
progress = 0;
progress = -1;
included = true;


Expand Down
13 changes: 7 additions & 6 deletions FDconverterUI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@
</ListView>


<Button Content="Add" HorizontalAlignment="Left" Margin="10,304,0,0" VerticalAlignment="Top" Width="100" Click="Button_Click_Add" ToolTip="Add files to the list for conversion"/>
<Button Content="Remove" HorizontalAlignment="Left" Margin="115,304,0,0" VerticalAlignment="Top" Width="100" Click="Button_Click_Remove" ToolTip="Removes files from the list"/>
<Button Content="Start" HorizontalAlignment="Left" Margin="220,304,0,0" VerticalAlignment="Top" Width="100" ToolTip="Starts the conversion of included files"/>
<ProgressBar x:Name="pbTotal" HorizontalAlignment="Left" Height="20" Margin="325,304,0,0" VerticalAlignment="Top" Width="639"/>
<Button x:Name="btnAdd" Content="Add" HorizontalAlignment="Left" Margin="10,304,0,0" VerticalAlignment="Top" Width="100" Click="Button_Click_Add" ToolTip="Add files to the list for conversion"/>
<Button x:Name="btnRemove" Content="Remove" HorizontalAlignment="Left" Margin="115,304,0,0" VerticalAlignment="Top" Width="100" IsEnabled="False" Click="Button_Click_Remove" ToolTip="Removes files from the list"/>
<Button x:Name="btnStart" Content="Start" HorizontalAlignment="Left" Margin="220,304,0,0" VerticalAlignment="Top" Width="100" IsEnabled="False" ToolTip="Starts the conversion of included files"/>
<Button x:Name="btnCancel" Content="Cancel" HorizontalAlignment="Left" Margin="325,304,0,0" VerticalAlignment="Top" Width="100" IsEnabled="False" Click="Button_Click_Cancel" ToolTip="Cancels conversion for all selected resources"/>
<ProgressBar x:Name="pbTotal" HorizontalAlignment="Left" Height="20" Margin="430,304,0,0" VerticalAlignment="Top" Width="534"/>
<Label Content="Type: " HorizontalAlignment="Left" Margin="10,-4,0,0" VerticalAlignment="Top" Width="41" />
<ComboBox x:Name="cbType" HorizontalAlignment="Left" Margin="51,0,0,0" VerticalAlignment="Top" Width="150" SelectionChanged="cbType_SelectionChanged" ToolTip="Sets the type of resource the file should be converted to"/>
<Button Content="Destination" HorizontalAlignment="Left" Margin="10,521,0,0" VerticalAlignment="Top" Width="100" Click="Button_Click_Destination" ToolTip="Sets an output folder for all the converted files"/>
<TextBox x:Name="tvDstPath" HorizontalAlignment="Left" Height="20" Margin="115,521,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="849"/>
<Label Content="Included: " HorizontalAlignment="Left" Margin="206,-4,0,0" VerticalAlignment="Top" Width="60"/>
<CheckBox x:Name="ckbIncluded" HorizontalAlignment="Left" Margin="266,2,0,0" VerticalAlignment="Top" Width="18" Height="16" Checked="ckbIncluded_Checked" Unchecked="ckbIncluded_Unchecked" ToolTip="Includes the resource(s) for conversion"/>
<CheckBox x:Name="ckbIncluded" IsThreeState="False" HorizontalAlignment="Left" Margin="266,2,0,0" VerticalAlignment="Top" Width="18" Height="16" Checked="ckbIncluded_Checked" Unchecked="ckbIncluded_Unchecked" ToolTip="Includes the resource(s) for conversion"/>

<GroupBox x:Name="imageOptionsGroup" Header="Image Options" HorizontalAlignment="Left" Height="272" Margin="10,27,0,0" VerticalAlignment="Top" Width="454" Visibility="Visible">
<Grid HorizontalAlignment="Left" Height="262" Margin="10,0,-2,-12" VerticalAlignment="Top" Width="434">
Expand All @@ -55,5 +56,5 @@
<GroupBox x:Name="modelOptionsGroup" Header="Image Options" HorizontalAlignment="Left" Height="{Binding ElementName=imageOptionsGroup, Path=ActualHeight}" Margin="10,27,0,0" VerticalAlignment="Top" Width="{Binding ElementName=imageOptionsGroup, Path=ActualWidth}" Visibility="Hidden">
<Label Content="Model conversion support not yet implemented!" HorizontalAlignment="Left" VerticalAlignment="Top" Width="100"/>
</GroupBox>
</Grid>
</Grid>
</Window>
70 changes: 55 additions & 15 deletions FDconverterUI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
using System.Windows.Navigation;
using System.Windows.Shapes;

using FDconverterCLI;

namespace FDconverter {
/// <summary>
/// Interaction logic for MainWindow.xaml
Expand Down Expand Up @@ -46,7 +48,6 @@ void Sort(ref List<int> indices) {

public MainWindow() {
InitializeComponent();

files = new ObservableCollection<FDFile>();

lvFiles.ItemsSource = files;
Expand All @@ -66,9 +67,9 @@ public MainWindow() {
channels[i] = (TextureChannel)i;
}

TextureChannelType[] channelTypes = new TextureChannelType[4];
TextureChannelType[] channelTypes = new TextureChannelType[1];

for (int i = 0; i < 4; i++) {
for (int i = 0; i < 1; i++) {
channelTypes[i] = (TextureChannelType)i;
}

Expand Down Expand Up @@ -114,6 +115,10 @@ private void AddFiles(string[] files) {
for (int i = 0; i < files.Length; i++) {
if (AddFolder(files[i]) == false) this.files.Add(FDFile.CreateFile(files[i]));
}

if (this.files.Count > 0 ) {
btnStart.IsEnabled = true;
}
}

private void SetOptionsGroupVisibility(FileType type) {
Expand Down Expand Up @@ -175,6 +180,19 @@ private List<int> GetFileIndices(List<FDFile> list) {
return indices;
}

private void RemoveFiles(List<FDFile> files) {

for (int i = 0; i < files.Count; i++) {
if (files[i].progress != -1) files.RemoveAt(i);
}

List<int> indices = GetFileIndices(files);

for (int i = 0; i < indices.Count; i++) {
this.files.RemoveAt(indices[i]);
}
}

private void lvFiles_Drop(object sender, DragEventArgs e) {
AddFiles((string[])e.Data.GetData(DataFormats.FileDrop));
}
Expand All @@ -183,12 +201,7 @@ private void lvFiles_KeyDown(object sender, KeyEventArgs e) {
if (e.Key == Key.Delete) {
List<FDFile> tmp = lvFiles.SelectedItems.Cast<FDFile>().ToList();


List<int> indices = GetFileIndices(tmp);

for (int i = 0; i < indices.Count; i++) {
files.RemoveAt(indices[i]);
}
RemoveFiles(tmp);
}
}

Expand All @@ -208,11 +221,7 @@ private void Button_Click_Add(object sender, RoutedEventArgs e) {
private void Button_Click_Remove(object sender, RoutedEventArgs e) {
List<FDFile> tmp = lvFiles.SelectedItems.Cast<FDFile>().ToList();

List<int> indices = GetFileIndices(tmp);

for (int i = 0; i < indices.Count; i++) {
files.RemoveAt(indices[i]);
}
RemoveFiles(tmp);
}

private void Button_Click_Destination(object sender, RoutedEventArgs e) {
Expand All @@ -233,6 +242,8 @@ private void cbType_SelectionChanged(object sender, SelectionChangedEventArgs e)

FileType type = (FileType)cbType.SelectedIndex;

lvFiles.SelectionChanged -= lvFiles_SelectionChanged;

for (int i = 0; i < indices.Count; i++) {
FDFile current = files[indices[i]];
switch (type) {
Expand All @@ -249,11 +260,25 @@ private void cbType_SelectionChanged(object sender, SelectionChangedEventArgs e)
lvFiles.SelectedItems.Add(files[indices[i]]);
}

lvFiles.SelectionChanged += lvFiles_SelectionChanged;

SetOptionsGroupVisibility((FileType)cbType.SelectedIndex);
}

private void lvFiles_SelectionChanged(object sender, SelectionChangedEventArgs e) {
if (lvFiles.SelectedItems.Count == 0) return;
if (lvFiles.SelectedItems.Count == 0) {
btnCancel.IsEnabled = false;
btnRemove.IsEnabled = false;
} else {
foreach (FDFile f in lvFiles.SelectedItems) {
if (f.included == null) {
btnCancel.IsEnabled = true;
}
}

btnRemove.IsEnabled = true;
}

FDFile tmp = (FDFile)lvFiles.SelectedItems[0];

cbType.SelectionChanged -= cbType_SelectionChanged;
Expand Down Expand Up @@ -322,10 +347,21 @@ private void ckbIncluded_Unchecked(object sender, RoutedEventArgs e) {

private void ckbItem_Checked(object sender, RoutedEventArgs e) {
UpdateIncludeAllCheckbox();

btnStart.IsEnabled = true;
}

private void ckbItem_Unchecked(object sender, RoutedEventArgs e) {
UpdateIncludeAllCheckbox();

foreach (FDFile f in files) {
if (f.included == true) {
btnStart.IsEnabled = true;
return;
}
}

btnStart.IsEnabled = false;
}

private void cbImageChannels_SelectionChanged(object sender, SelectionChangedEventArgs e) {
Expand All @@ -339,5 +375,9 @@ private void cbImageSizes_SelectionChanged(object sender, SelectionChangedEventA
((FDTextureFile)lvFiles.SelectedItems[i]).channelType = (TextureChannelType)cbImageSizes.SelectedIndex;
}
}

private void Button_Click_Cancel(object sender, RoutedEventArgs e) {

}
}
}
3 changes: 2 additions & 1 deletion FDconverterUI/Texture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FDconverterCLI;

namespace FDconverter {

Expand All @@ -23,7 +24,7 @@ public FDTextureFile(string path) : base(path, FileType.Image) {
channel = TextureChannel.RGBA;
channelType = TextureChannelType.Uint8;

FDConverter.ConvertImage("", "", TextureChannel.R, TextureChannelType.Float32, new FDconverter.FPProgressCallBack(ProgressCallback));
FDConverter.ConvertImage("", "", TextureChannel.R, TextureChannelType.Float32, new FPProgressCallBack(ProgressCallback));
}

private void ProgressCallback(int progress) {
Expand Down
2 changes: 2 additions & 0 deletions Frodo-Tools.sln
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ Global
{1AF87795-F282-4CBD-B1E4-7D454C57B43B}.Release|x64.ActiveCfg = Release|x64
{1AF87795-F282-4CBD-B1E4-7D454C57B43B}.Release|x64.Build.0 = Release|x64
{DB5B99F0-CD9B-4B37-951D-A418C54AFCD9}.Debug|Any CPU.ActiveCfg = Debug|x64
{DB5B99F0-CD9B-4B37-951D-A418C54AFCD9}.Debug|Any CPU.Build.0 = Debug|x64
{DB5B99F0-CD9B-4B37-951D-A418C54AFCD9}.Debug|x64.ActiveCfg = Debug|x64
{DB5B99F0-CD9B-4B37-951D-A418C54AFCD9}.Debug|x64.Build.0 = Debug|x64
{DB5B99F0-CD9B-4B37-951D-A418C54AFCD9}.Release|Any CPU.ActiveCfg = Release|x64
{DB5B99F0-CD9B-4B37-951D-A418C54AFCD9}.Release|Any CPU.Build.0 = Release|x64
{DB5B99F0-CD9B-4B37-951D-A418C54AFCD9}.Release|x64.ActiveCfg = Release|x64
{DB5B99F0-CD9B-4B37-951D-A418C54AFCD9}.Release|x64.Build.0 = Release|x64
EndGlobalSection
Expand Down

0 comments on commit faf5253

Please sign in to comment.