SDL2_image not initializing - c++

Im trying to load a .png file with SDL2 and SDL2_image. SDL2 initializes properly while SDL2_image just fails for no apparent reason, please help.
IMG_Init() returns 0
IMG_GetError() returns nothing
I'm using SDL2 2.0.10 and SDL2_image 2.0.5 (latest versions afaik)
I do have all needed dll files in the same folder as my executable (SDL2.dll, SDL2_image.dll, libpng16-16.dll, zlib1.dll as I'm only using png files right now)
I have no idea why this isn't working and I have been trying to find an answer for at least 2 hours now
My code:
if(IMG_Init(IMG_INIT_PNG) != 0) {
std::cout << "Failed to init sdl_image"<<IMG_GetError()<<std::endl;
return 0;
}

As HolyBlackCat pointed out, it should be IMG_INIT(IMG_INIT_PNG) != IMG_INIT_PNG instead of IMG_INIT(IMG_INIT_PNG) != 0

Related

SDL Mix_LoadMUS not loading .mp3

So Apparently i've been trying to load an .mp3 using SDL_mixer. However, this does not work, as opposed to the libsdl wiki: SDL_mixer Mix_LoadMUS
I was hoping for it to work, but when loading and playing the file, the following errors popped in my console app:
Mix_LoadMUS: Unrecognized audio format
Mix_PlayMusic: music parameter was NULL
To my extent, i've been trying to load test.mp3 the following way:
Mix_Music * m_mainMusic;
m_mainMusic = Mix_LoadMUS("test.mp3");
if (m_mainMusic != nullptr)
printf("Loaded the file\n");
else
printf("Mix_LoadMUS: %s\n", Mix_GetError());
if (Mix_PlayMusic(m_mainMusic, -1) == -1)
printf("Mix_PlayMusic: %s\n", Mix_GetError());
I have obviously initialized the SDL subsystem.
With the help of the above explanations I tried to reproduce your issue, and the only thing that caused something similar was the lack of runtime libraries. Make sure that all the necessary libraries are available during runtime (copy them in the folder of your executable or set an environment variable or use static linking.) The runtime libraries distributed with SDL_mixer are the following ones: libmpg123-0, libmodplug-1, libFLAC-8, libogg-0, libopus-0, libopusfile-0, libvorbis-0, libvorbisfile-3 and SDL2_mixer.
These can be acquired here: https://www.libsdl.org/projects/SDL_mixer/

SDL_ttf "Couldn't load font file" with SDL2 in c++

I have the following code to draw some text in an SDL2 application. When I build and run, I'm consistently seeing an error of TTF_OpenFont() Failed: Couldn't load font file. I've tried the following:
Ensured the font file is in the current directory of running program. In fact, I've put the font in almost any directory the program could be running from and have tried with an absolute path.
Setting different permissions and owning the file
Opening the file separately with SDL_RWFromFile as described here: http://www.gamedev.net/topic/275525-sdl_ttf-weirdness/
Downloaded and recompiled a newer version of SDL_ttf (2.0.14)
Here's my code:
void SDLRenderer::drawText(
const Vector2d& pos,
string message,
const Color& color)
{
if(!TTF_WasInit()) {
cerr << "TTF_Init failed " << TTF_GetError() << endl;
exit(1);
}
TTF_Font* fixed = TTF_OpenFont("./DejaVuSansMono.ttf", 16);
if (fixed == NULL) {
cerr << "TTF_OpenFont() Failed: " << TTF_GetError() << endl;
TTF_Quit();
SDL_Quit();
exit(1);
}
...
I'm also calling TTF_Init() from the constructor of this code's class. I'm also a little unsure how to debug further because gdb doesn't even give a backtrace after the error and doesn't seem to let me step into the TTF_OpenFont function.
I ran into this issue and it was caused by linking against the incorrect version of the SDL_ttf library. I was using SDL 2.0, but I was linking against libSDL_ttf.so instead of libSDL2_ttf.so. libSDL_ttf.so is for SDL 1.2, and is not compatible with SDK 2.0.
My original command line was:
$ gcc -o showfont showfont.c `sdl2-config --cflags --libs` -lSDL_ttf
$ ./showfont /usr/share/fonts/truetype/freefont/FreeSans.ttf
Couldn't load 18 pt font from /usr/share/fonts/truetype/freefont/FreeSans.ttf: Couldn't load font file
I fixed it by linking against libSDL2_ttf.so instead:
$ gcc -o showfont showfont.c `sdl2-config --cflags --libs` -lSDL2_ttf
$ ./showfont /usr/share/fonts/truetype/freefont/FreeSans.ttf
Font is generally 21 big, and string is 21 big
The showfont.c program is an example included with SDL_ttf.
My thoughts probably belong in a comment but I don't have enough reputation. You can make sure you're in the right directory by explicitly setting the current working directory (chdir in unistd.h on Linux, or SetCurrentDirectory in windows.h on Windows). I don't think you need to include ./ in the file name.
I recall having problems with SDL_ttf when calling TTF_Init, TTF_Quit, and then TTF_Init again. This may not be causing your problem but I would recommend doing your TTF_Init just once at the beginning of the program and TTF_Quit once at the end, not every time your constructor runs.
If this doesn't work look into building a debug version of SDL_ttf that will play nicer with GDB.
I've had your same problem and managed to fix it by entering the full path to the font.
Instead of just passing the string "./font.ttf"
I used: "/User/MyUsername/Projects/MyProject/font.tff"
Hope this helps!

SDL - TTF - font not loading

I am having Problem loading SDL_TTF font.
my program is so big, so there's a piece of code which is creating problem.
TTF_Font *font = NULL;
SDL_Color textColor = {255, 255, 255};
if(SDL_Init() == -1 || TTF_Init() == -1)
return 0;
font = TTF_OpenFont("calibri.ttf", 28);
if(font == NULL)
return 0;
Now please tell me why the funtion TTF_OpenFont is not working.
font remains NULL after the funtion and the program exits...
EDIT:
I cannot cout or printf TTF_GetError() but i used breakpoints to see which function is not working correctly and found the function TTF_OpenFont() returning null every time.
(BTW if there any way to print the Erroe on screen the tell please).
TTF_Init() is working correctly.
FOR PEOPLE WHO ARE ASKING TO INSTALL SDL_TTF SEPARATELY:
I have written the link below from which i am learning SDL in first tutorial it guides how to install SDL and integrate it which project, In 3rd lesson comes the SDL_IMAGE extension library which is to be needed to install separately (which i did and used all image functions easily), In 7th lesson it didn't teach how to install SDL_TFF rather says to do the same steps as for SDL_IMAGE but this time for SDL_TTf so i downloaded SDL_TTF and intalled and you know the rest after....
Windows 8.1, CodeBlocks, MinGW32, SDL1
Here's the link i am learning SDL from
SDL Tutorials
This is how i installed the Extension Library(SDL_TTF)
Setting Up Extention Library
If font is NULL you should use TTF_GetError to know what went wrong.
If the error message is opaque you may want then to take a look at TTF_OpenFont source.
Suggestions:
1) substitute this code for TTF_Init():
if(TTF_Init()==-1) {
printf("TTF_Init: %s\n", TTF_GetError());
exit(2);
}
2) Make sure you call it before any other SDL_ttf functions.
If you're still having problems, please specify
your platform (Windows? Linux?)
how you installed your TTF fonts.
3) See also: https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf.html
===================================================================
I'm sorry you're still having problems displaying fonts. A few additional suggestions:
1) I'm sure the tutorial you're using is very good. But you can't assume that just because you followed all the steps, you didn't inadvertantly miss something. To resolve the problem, you're going to have to "look beyond" the tutorial.
2) For starters, we need to confirm whether or not TTF_Init() succeeded. We must be able to see TTF_Init() return status, and TTF_GetError() messages.
Q: What is the return value of TTF_Init?
There are several ways you can display "TTF_GetError":
Start your program from a command prompt ("cmd"):printf("TTF_Init: %s\n", TTF_GetError());
Copy the error to a string and look at it under a debugger: char[80] msg; strcpy (msg, TTF_GetError());
Use a Win32 MessageBox to display the error in a pop-up: MSDN - MessageBox function
3) Make sure you have these .dll's on your filesystem:
libfreetype*.dll
SDL2_ttf*.dll
zlib*.dll
4) Look at the fonts you have installed on your filesystem, for example: C:\Windows\Fonts\*.ttf
See also SDL_ttf - Font directory/Where do fonts go?
PROBLEM SOLVED.
Thanks EveryOne for giving your time...
the problem was program was not recognizing font(candara )
so it copy pasted in my project folder.
I know this was already answerd but i ran into a similar problem with SDL2_ttf under Visual Studio 2015 and want to share my solution to help maybe others in the future.
My problem was that when i started my SDLe application that used SDLe_ttf it could not load the font that was placed in the same directory. But when i executed the the build executable manualy from the command line everything was fine.
I think the problem is that Visual Studio 2015 executes the build executable from a different directory and since i used only the font file name(Sans.ttf) it could not load the font because it treats it as a relative path.
My soultion was to just prefix the font file name with the execution directory which can be optained by calling SDL_GetBasePath.

Interfacing Qt application with Scilab via call_scilab (Scilab API)

I'm working on Qt application, that has to compute some data using Scilab's numerical engine. My OS is Ubuntu 14.04 with installed Scilab v.5.5.0 and QtCreator v.3.2.1 (Qt 5.3.2).
I'm using a simple code example provided in scilab help:
/****** INITIALIZATION **********/
#ifdef _MSC_VER
if ( StartScilab(NULL,NULL,NULL) == FALSE )
#else
if ( StartScilab(getenv("SCI"),NULL,NULL) == FALSE )
#endif
{
fprintf(stderr,"Error while calling StartScilab\n");
}
/****** ACTUAL Scilab TASKS *******/
SendScilabJob("myMatrix=['sample','for the help']");
SendScilabJob("disp(myMatrix);"); // Will display !sample for the help !
SendScilabJob("disp([2,3]+[-44,39]);"); // Will display - 42. 42.
/****** TERMINATION **********/
if ( TerminateScilab(NULL) == FALSE ) {
fprintf(stderr,"Error while calling TerminateScilab\n");
}
My problem is that after clicking "Run", I get an alert as follows:
/home/med/Dokumenty/QTWorkspace/build-QTtest-Desktop_Qt_5_3_GCC_32bit-Debug/QTtest: error while loading shared libraries: libscicall_scilab.so.5: cannot open shared object file: No such file or directory
I tried to add following line to qmake .pro file, but without any results:
LIBS += /usr/lib/scilab/libscicall_scilab.so.5
The library already is in specified dir (I've checked this manually). Before that I was trying a lot of another settings - still without success.
Could anyone provide the proper solution of this problem?
try adding the location of your lib to your LD_LIBRARY_PATH
Try to use this in your project file:
LIBS = -L/usr/lib/scilab -lscicall_scilab
You will need to rerun qmake then to regenerate the Makefile(s). Make sure that you have 32 bit library of scilab or change the build to 64 bit should that be the issue.

OpenCV on eclipse on windows

I'm trying to install opencv on windows, here are my steps:
downloaded opencv 2.4.3 from website
run the exe, extracted the folder in the same path
opened eclipse (with MinGW previously set and configured)
created new project XYZ
added new folder "src"
added new class "main.cpp"
added the following code:
hash include <cv.h>
hash include <highgui.h>
using namespace cv;
int main(int argc, char** argv) {
Mat image;
image = imread(argv[1], 1);
if (argc != 2 || !image.data) {
printf("No image data \n");
return -1;
}
namedWindow("Display Image", CV_WINDOW_AUTOSIZE);
imshow("Display Image", image);
waitKey(0);
return 0;
}
added the two paths
"E:\Sources\opencv\build\include"
"E:\Sources\opencv\build\include\opencv"
got the compilation error "Symbol 'cv' could not be resolved"
Please advice if any step is missing
you gonna need the latest stable version of openCV 2.4.3 .
Eclipse Juno ! (Eclipse IDE for C/C++ Developers )
And
MinGW - Minimalist GNU for Windows
we will ignore x86/64 choice, since we gonna work with a 32 compiler / and 32 openCV build, even though the system is a 64 one !
Step 1 : Download and Install
Eclipse
Download Eclipse from and decompress the archive . ( I assumed that you already have JRE on your computer , if not ! download and install it ) .
MinGW
Download MinGW . the installer will lead you through the process !
you might have to add the bin directory to the path ! (Default path : C/MinGW/bin )
OpenCV
Download openCV exe from the link , extract the files (in the C:/ directory in this tutorial ) .
Make sure you have the file structure below .
don't forget to add the bin directory => Path !
As I mentioned earlier ! I'll use x86 build even if i have a 64 OS to avoid compiler problems and to keep this tutorial open to x86 OS users !
Step 2 : Create and Configure
Open the Eclipse IDE !
Create a new C++ project : File > New > C++ Project
Choose a Hello Word Project to have a pre structured one !
Don't forget to select the MinGW toolchains
Click Finish and let's get to work !
Now that you have you first Hello word project ! replace the Code in the Soure file .cpp by the code below
///////////////CODE///////////
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat im = imread(argc == 2 ? argv[1] : "lenna.png", 1);
if (im.empty())
{
cout << "Cannot open image!" << endl;
return -1;
}
imshow("image", im);
waitKey(0);
return 0;
}
///////////////CODE///////////
Obviously there are multiple errors on the code , yes ! we have to link the libraries !
Now go to Properties >> C/C++ Build >> Settings
on the Tool Setting tab >> GCC C++ Compiler >> Includes and include opencv path !
[opencvDir\build\include]
Now scroll to MinGW C++ Linker >> Libraries and add the Library search path [opencvDIR\build\x86\mingw\lib]
in the Libraries part ! we add as much librarie as we need for the project !
here I added 4 libraries just for tutorial sake even if well need only the highgui one for our test code to work !
The libraries names can be found on the [opencvDIR\build\x86\mingw\lib]
Example ! for libopencv_video243.dll.a wee add opencv_video243 in the linker !
click OK !
Now we can build our first project !
You figured that you have to add a picture to the project as implied in the source code "lenna.png"
Use lenna for good luck
Build and Run the project !
If you see the beautiful lady :) Congrats :)
have a look here for snapshots!
opencveclipse-on-windows
cv.h is for the old C API. To use the Cpp API try the following:
#include <opencv2/opencv.hpp>