Hi, so just as a disclaimer, I am very new to both the D programming language and SFML, so there could easily be a facepalm worthy cause behind my current issue, but if there is I can't find it. I'm currently trying to get a simple test program that uses DSFML to work correctly. Here is my current code:
module winmain;
import core.runtime;
import core.sys.windows.windows;
import dsfml.system;
import dsfml.window;
import dsfml.graphics;
import dsfml.audio;
extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
int result;
try
{
Runtime.initialize();
result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
catch (Throwable error) // catch any uncaught exceptions
{
MessageBoxA(null, cast(char *)error.toString(), "Error", MB_OK | MB_ICONEXCLAMATION);
result = 0; // failed
}
return result;
}
int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
void errorMessage(Throwable error)
{
MessageBoxA(null, cast(char *)error.toString(), "Error", MB_OK | MB_ICONEXCLAMATION);
}
// Load a sprite to display
Texture texture;
Sprite sprite;
try
{
texture = new Texture();
if(!texture.loadFromFile("cute_image.png"))
return 0;
sprite = new Sprite(texture);
}
catch (Throwable error)
{
errorMessage(error);
}
// Create the main window
auto size = texture.getSize();
auto window = new RenderWindow(VideoMode(size.x, size.y), "DSFML window");
// Create a graphical text to display
auto font = new Font();
if(!font.loadFromFile("arial.ttf"))
return 0;
auto text = new Text("Hello DSFML", font, 50);
text.setColor(Color.Black);
// Load a music to play
try
{
auto music = new Music();
if(!music.openFromFile("nice_music.ogg"))
return 0;
// Play the music
music.play();
}
catch (Throwable error)
{
errorMessage(error);
}
// Start the game loop
while(window.isOpen())
{
// Process events
Event event;
while(window.pollEvent(event))
{
// Close window
if(event.type == Event.EventType.Closed)
window.close();
}
// Clear screen
window.clear();
// Draw the sprite
window.draw(sprite);
// Draw the string
window.draw(text);
// Update the window
window.display();
}
return 0;
}
The problem is that while the audio file will load correctly, the program will throw a "Bad file descriptor" error when I actually try to play it. And if that weren't bad enough, when I try to close the program I get the following runtime error:
Unhandled exception at 0x7736A4B9 (ole32.dll) in DSFML_Example.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.
I really have no idea what's going on here. Looking through my code and configuration, it's not clear at all that I did anything horribly wrong. Here is a link to my entire Visual Studio project (https://mega.co.nz/#!f1QRxRSS!qytfVW8ewA_Hn_nld7nNGrA9R-l7fnZylD3lAQ8e4IQ) in case anyone wants to look at it (it requires you to have Visual D installed).
Okay, I think I solved the problem. I'm not really sure what the cause was, but by going through the whole process of building SFML and SFML-C on my own setup, I was able to get the audio working. :) But I did run into a couple of non-critical but still worrying issues during the process. First of all, when building dsfml-graphics, I encountered the following error:
Error 1 error LNK2001: unresolved external symbol "public: static struct sf::Shader::CurrentTextureType sf::Shader::CurrentTexture" (?CurrentTexture@Shader@sf@@2UCurrentTextureType@12@A) C:\Development\Libraries\DSFML-C\build\src\SFML\Graphics\Shader.obj dsfml-graphics
I was able to work around it with a lazy hack, but I couldn't figure out what was actually causing the error...
void sfShader_setCurrentTextureParameter(sfShader* shader, const char* name)
{
//CSFML_CALL(shader, setParameter(name, sf::Shader::CurrentTexture));
return;
}
Secondly, I ran into an issue where even after closing the main window of the program, the audio would keep playing and the program would not exit. I was able to sort of fix things by manually calling exit(), but this causes my program to throw the following error when running in the debugger:
Unhandled exception at 0x7736A4B9 (ole32.dll) in DSFML_Example.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.
Since I build all the libraries with debugging information, I was able to track down the issue to the destructor of the SFML AudioDevice class:
AudioDevice::~AudioDevice()
{
// Destroy the context
alcMakeContextCurrent(NULL);
if (audioContext)
alcDestroyContext(audioContext);
// Destroy the device
if (audioDevice)
alcCloseDevice(audioDevice);
}
It appears that the function is being called with an invalid "this" pointer. My best guess would some sort of garbage collection issue, but I don't really know enough about D to say for sure.
Here are the updated project files (https://mega.co.nz/#!7hAVjZwQ!5XrxH81HzQuQIPf20Chs1CWdmIhDtb3YXcdmc98ZxVM) in case you'd like to look at them.