Alright so I'm trying to make a game engine for myself, and I've decided that it would be best to start loading images as files other than bitmaps using the SDL Image library. I've set up the library correctly according to conversations online, include set up and linker set up, and yet when I try to load a file that does indeed exist it just returns an empty surface.
Heres the code loading the file...
SDL_Surface* background = IMG_Load("Assets/bg.png");
background = SDL_ConvertSurface(background, mtrx->format, 0);
if (!background) {
ofstream file("text.txt");
file << IMG_GetError() << endl;
file.close();
}
...And the error I get in "text.txt"...
Parameter 'surface' is invalid
At the beginning of the script I have included SDL.h, then SDL_image.h, and the window initiation has IMG_Init(IMG_INIT_PNG) after SDL_Init. Visual studio shows no errors whatsoever, and everything BUT IMG_Load works fine.
All help would be appreciated, and I can provide any other code that might be helpful!
Related
I'm trying to insert a song that plays on a loop in my c++ Allegro 5 game. It keeps saying that it can't load the audio.
I have:
tried to use .wav and .ogg files, both did not work.
put the audio file in the correct directory.
created a function to detect the error.
initialized al_init_acodec_addon() and al_install_audio()
ALLEGRO_SAMPLE* song = al_load_sample("liar.ogg");
void game_begin()
{
if (!song)
{
printf( "Audio clip sample not loaded!\n" );
show_err_msg(-6);
}
//Loop the song until the display closes
al_play_sample(song, 1,0,1,ALLEGRO_PLAYMODE_LOOP, NULL);
Basically the console almost always prints out the error message no matter what I do.
Is this a known Allegro 5 problem? I still can't think of a way to fix this...
On a side note, I have tested loading & playing audio in another project file and it worked. Is my file cursed? :(
:)
Try to put it in THIS ORDER before loading samples:
if(!al_init())
return -1;
if(!al_install_audio())
return -2;
if!(!al_init_acodec_addon()) // after installing audio
return -3;
if(!al_reserve_samples(1))
return -4;
And what do you mean by correct directory?
I'm trying to use the SFML library to create a sort of text interface in c++, but i ran into an error trying to load a font.
My code looks like this
sf::Font font;
if (!font.loadFromFile("courbd.ttf"))
{
std::cout << "Can't load the font file" << std::endl;
}
and both the code I'm trying to load the font from and the font are in this path:
C:\Users\Computer\Desktop\PruebaSFML\PruebaSFML
I tried using the whole path to load the font but that didn't work either.
Normally, loadFromFile is going to take a path starting from the file the code is in. Here is an example from when I worked with SFML:
if (!font.loadFromFile("Ressources\\Fonts\\CrimsonText.ttf"))
return false;
The cpp file is at the same level of the Ressources folder. Also, dont forget to use 2x \ to escape the character, because it's used to make instruction in the string like \n for a new line. So make sure of the location of the font file from the cpp file and if you need to use a path, use \ to separate folders.
I have created the a default DirectX12 application (the spinning 3D cube) and within my void DX::DeviceResources::Present() I am trying to write the backbuffer to a file:
// Present the contents of the swap chain to the screen.
void DX::DeviceResources::Present()
{
// The first argument instructs DXGI to block until VSync, putting the application
// to sleep until the next VSync. This ensures we don't waste any cycles rendering
// frames that will never be displayed to the screen.
HRESULT hr = m_swapChain->Present(1, 0);
UINT backBufferIndex = m_swapChain->GetCurrentBackBufferIndex();
ComPtr<ID3D12Resource> spBackBuffer;
m_swapChain->GetBuffer(backBufferIndex, IID_PPV_ARGS(&spBackBuffer));
//before writing the buffer, I want to check that the file is being
//created
ofstream myfile;
myfile.open("WHEREISTHEFILE.txt");
myfile << "Writing this to a file.\n";
myfile.close();
// If the device was removed either by a disconnection or a driver upgrade, we
// must recreate all device resources.
if (hr == DXGI_ERROR_DEVICE_REMOVED || hr == DXGI_ERROR_DEVICE_RESET)
{
m_deviceRemoved = true;
}
else
{
DX::ThrowIfFailed(hr);
MoveToNextFrame();
}
}
The problem occurs here:
ofstream myfile;
myfile.open("WHEREISTHEFILE.txt");
myfile << "Writing this to a file.\n";
myfile.close();
I just want to write a file first (as illustrated how here), before trying to write the contents of the back buffer. Now, for some reason, I cannot find the output file... I have searched everywhere, all directories in my project and even in the Microsoft DirectX SDK folder.
There are no exceptions thrown and I can step through each line while debugging without error.
Where could it be?
It should be in the directory of your project. If you are using Visual Studio, you can right-click your solution and click Open folder in File explorer.
Image: Open folder in File explorer
(I embedded it like this because I need 10 reputation to post an image directly)
Also with your code now, there is no way to determine whether your program is actually able to open the output file or not. I suggest you use something like this:
std::ofstream outputFile("./myOutput.txt");
if (outputFile.fail())
{
std::cout << "Failed to open outputfile.\n";
}
outputFile << "I like trains.";
outputFile.close();
The first line is an initialisation that does the same as .open(). Also mind the "./" in front of the path, this should not be mandatory but it can't hurt to do this (this means your current directory).
The important part about this piece of code is the .fail() check, if your program failed to open the outputfile, you will ofcourse not be able to find it anywhere. Hope this helped!
Where could it be?
Usually the file location is relative to your current working directory, i.e. WHEREISTHEFILE.txt should be located in whatever directory you were in when you started the program.
You can determine that directory inside your program via GetCurrentDirectory(), and change it to something else via SetCurrentDirectory().
But you did not check if the .open() was successful, so the writing could have failed altogether, for example due to insufficient permissions...?!
I am having an issue where my FbxImporter fails to initialize. The ErrorString it returns when I call GetStatus.GetErrorString() is "Unexpected File Type". The Error code for the status is EFailure(after looking this up all autodesk says about is it means it failed). I have tried many different fbx files, and none of them seem to work. Also, I have included them in the visual studio project, and even with the executable(I know this is not the reason because other files load just fine). After looking around, there really is no forum post or anything that helps me with my problem.
Here is a snippet of my importer code.
//set up the fbxmanager
FbxManager* fbxManager = FbxManager::Create();
//set the settings for the manager
FbxIOSettings* ioSettings = FbxIOSettings::Create(fbxManager, IOSROOT);
fbxManager->SetIOSettings(ioSettings);
//set the settings for the fbx io settings
//create and init the importer
FbxImporter *importer = FbxImporter::Create(fbxManager,"");
//create and init the scene
FbxScene* fbxScene = FbxScene::Create(fbxManager, "");
//init the importer
result = importer->Initialize(filename,-1, fbxManager->GetIOSettings());
if (!result)
{
string error = importer->GetStatus().GetErrorString();
FbxStatus status = importer->GetStatus().GetCode();
return false;
}
If I need to clarify the question or you need more information to answer my question please say so, thanks.
I figure it out how to solve it!
There are two types of FBX file.
Binary FBX
ASCII FBX
Whenever you try to do
importer->Initialize(filename,-1, fbxManager->GetIOSettings());
with Binary FBX, it causes "Unexpected File Type".
However, if you convert binary FBX to ASCII FBX by FBX Converter, your program will be able to read the file.
P.S: Sample program can read the binary FBX. I think FBX has a different version and it causes compatibility issues.
I'm trying to iterator over some directories containing approximately 3000 images. I load the image. If the image is loaded I release it.
That is the smallest program that I can write to reproduce the error.
After loading and releasing 124 images the program stops loading images. I think this a memory issue but I don't understand what exactly causes the program to stop loading images.
I'm using OpenCV on my Mac. I don't know how exactly I can figure out which version I'm using.
Here is the Code from my project.
bool FaceDetectionStrategy::detectFace(std::string imagePath) {
IplImage *img = cvLoadImage(imagePath.c_str(), CV_LOAD_IMAGE_COLOR);
if (img) {
std::cout << "Image loaded " << imagePath << std::endl;
cvReleaseImage(&img);
} else {
std::cout << "Image not loaded " << imagePath << std::endl;
}
return true;
}
This method is called for every image in the directorys I'm iterating through. After 124 images the if(img) part evaluates to false and the else branch is executed. If I try to load images from other parts of the program later on they also won't load.
Edit it is not a memory issue. Mac Os standard max open files is 256 after changing it to 512 I can open 251 images. so it seems that OpenCV doesn't closes the image files after loading them.
Searching the bugtracker from OpenCv showed this answer to the problem: cvLoadImage with Mac ImageIO leaves file handles open.
It seems that this is a bug in the OpenCV mac implementation and the only way to solve it is to install a newer version of OpenCV.
EDIT installing the last version of OpenCV from the repository trunk solves the problem. Sometimes it helps checking the BugTracker of the frameworks you are using...
Behavior concerning memory rarely has to do with a consistent number, in my experience. The only way it could be that consistent is if there is some sort of internal limit in cvLoadImage that happens to be the not-very-common number 124. But your logic seems fine to me, that your images should be released.
More likely, since I assume your directories aren't changing between tests, that 125th image is bad.
Have you verified that the image you are trying to load actually exists? If it does (which it probably does), check that the image file format is supported by OpenCV. If that is also true, make sure the file is not corrupted by opening it with another editor.
You can have OpenCV help you out with errors. Use cvGetErrStatus() to check if there was an error, then use cvErrorStr() to get a textual description of it. You can do something like this:
// I would recommend putting this in a file, like CVUtility.h
#include <exception>
void check_CV_Error(void)
{
int errorCode = cvGetErrStatus();
if (errorCode) // I'm assuming 0 means no reportable error
{
throw std::runtime_error(cvErrorStr(errorCode));
// std::cerr << cvErrorStr(errorCode);
// ^ if you would rather not use exceptions
}
}
Your code then becomes:
bool FaceDetectionStrategy::detectFace(std::string imagePath) {
IplImage *img = cvLoadImage(imagePath.c_str(), CV_LOAD_IMAGE_COLOR);
if (img) {
std::cout << "Image loaded " << imagePath << std::endl;
cvReleaseImage(&img);
} else {
std::cout << "Image not loaded " << imagePath << std::endl;
check_CV_Error(); // find reason for error
}
return true;
And that will throw an exception for you to catch and log, and possibly react on. (Or print the error to console if you use the std::cerr version)