SDL 2.0 compiles, but failt to run - c++

Im using the latest SDL 2.0 version on Xubuntu 64-bits. I installed through the provided install script on the source code.
Compiling works well, however when trying to open a font or image (regardless of its extension), it will always fail to open.
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
int main (int argc, char *argvp[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
cout << SDL_GetError() << endl;
}
if (TTF_Init() == -1)
{
std::cout << TTF_GetError() << std::endl;
return 2;
}
TTF_Font *font1 = NULL;
font1 = TTF_OpenFont("SourceSansPro-Regular.ttf", 20);
if (font1 == NULL)
{
std::cout << "ERROR OPENING FONT = " << TTF_GetError() << std::endl;
}
TTF_CloseFont(font1);
SDL_Quit();
return 0;
}
I compiled with
g++ -Wall fontTEST.cpp -o TEST -lSDL2 -lSDL_ttf (NOTE that SDL_ttf installs as such, not as SDL2_ttf)
And get the following error: Failed to load font: 0 Couldn't load font file
This happens with images as well. I've already tried with different fonts and images, apparently it works if I compile with SDL 1.2, just not with 2.0.
Also why does the provided install script installs the lib and include folders in /user/local/?
I moved them to /usr/ but the problem persists.

Remember the following:
On Unix, file paths are case-sensitive
As said in Xonar's comment, tilde '~' expansion is a shell feature, it does not work in C/C++ programs, you should use the real path instead.
The strace log says clearly that something is wrong with the path.
You should try the following:
Rename your font file to "font.ttf"
put it in /home/user/font.ttf
use "/home/user/font.ttf" as the path in your code.

Related

libsndfile Emscripten environment

I am running just a little code using libsndfile, in the emscripten environment
#include <iostream>
#include <sndfile.h>
int main()
{
SF_INFO info;
const char * path = "~/data/somefile.wav";
SNDFILE* sf = sf_open(path,SFM_READ, &info);
if(sf == NULL)
{
std::cout<< sf_strerror(sf) << std::endl;
return 1;
}
std::cout<<info.samplerate<<std::endl;
std::cout<<"Hello world" << std::endl;
}
So ideally if I run this with normal cmake (Apple Clang compiler) everything works fine, the samplerate and hello world are printed, but when I run this with emcmake cmake (em++ compiler) and run the compiled node main.js file it says System error: no such file or directory. Who can help me with this? Who has experienced such thing?
So I figured it out.
The problem is that Emscripten has its virtual file environment. So if you want this file to be uploaded and later be seen in compiled .js file, you need to add compile flag --preload-file <FILE_PATH> , after that the file with given path will be recognized by emscripten environment.

Unable to initialise sdl2

Im trying to check if sdl is properly installed on my Ubuntu 20.04 and its not.
Running all on 64bit type.
This is my code:
#include <iostream>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
bool initSDL()
{
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
std::cout << "Failed to init sdl: " << SDL_GetError() << std::endl;
return false;
}
if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG)
{
std::cout << "Failed to init sdl_image: " << IMG_GetError() << std::endl;
return false;
}
return true;
}
int main(int argc, char *argv[])
{
std::cout << "Code Starting..." << std::endl;
initSDL();
int winX = 900, winY = 600;
std::cout << "Code Exited Properly." << std::endl;
return 0;
}
And this is my output:
Code Starting...
Failed to init sdl: No available video device
Code Exited Properly.
Im running my code with this commands:
gcc -c src/*.cpp -I include -m64 -lstdc++ -std=c++11
gcc *.o -o out/main -lSDL2main -lSDL2 -lSDL2_image -m64 -lstdc++ -std=c++11
./out/main
My code strucure is this way:
Main directory 'sdl_hello_world'
->src -> contains 'main.cpp'
->out -> contains compiled 'main'
Some methods i tried was:
export DISPLAY=:0
export SDL_VIDEODRIVER=x11
So how do i fix this ?
EDIT: I also followed this tutorial (https://www.youtube.com/watch?v=P3_xhDIP7bc&list=PLvv0ScY6vfd-p1gSnbQhY7vMe2rng0IL0&ab_channel=MikeShah), and still it gave the same error. So i guess the problem is in some computer setting.
EDIT: SOLVED:
i followed almost all tutorials and fix methods on the internet to solve my issue which uses gcc in linux. Some solutions on stack overflow seemed to prefer to compile the source code on their own computer WHICH WORKED.
So im pretty sure the way ubuntu installs it on my computer/ idk im new to using sdl. Updating this as might come handy to anyone else.
Still curious, is there a chance i did something wrong, or could this be considered a bug ?
EDIT: followed this btw https://wiki.libsdl.org/Installation

Font cannot be loaded with Emscripten but with g++

I am trying to use FreeType the first time with OpenGL in C/C++ with this tutorial: https://learnopengl.com/In-Practice/Text-Rendering.
#include <iostream>
#include <ft2build.h>
#include FT_FREETYPE_H
FT_Library ft;
FT_Face face;
int main(int argc, char** argv)
{
FT_Init_FreeType(&ft);
if (FT_New_Face(ft, "/usr/share/fonts/truetype/malayalam/Uroob-Regular.ttf", 0, &face))
{
std::cout << "Font could not be loaded" << std::endl;
return -1;
}
}
Now when I try to compile this it works with g++ so far as there is no error output, but with em++ (Emscripten) the font cannot be loaded as the error output "Font could not be loaded" then indicates.
I do not need THIS kind of font "Uroob-Regular.ttf", any readable Latin font would do it for me. I am under Ubuntu.
I use the flag -s USE_FREETYPE=1 with Emscripten.
It seems like Emscripten cannot find files below the current directory, because when I try to compile it with --preload-file "/usr/share/fonts/truetype/malayalam/Uroob-Regular.ttf" to help it find the font it indicates: "Embedding "/usr/share/fonts/truetype/malayalam/Uroob-Regular.ttf" which is below the current directory "xxx". This is invalid since the current directory becomes the root that the generated code will see".
I don't know if this is a valid way to do so, but then I put the font file in the same directory as the cpp file. Then I used --preload-file "Uroob-Regular.ttf" and changed the corresponding code line to if (FT_New_Face(ft, "Uroob-Regular.ttf", 0, &face)) and it worked for both Emscripten and g++.

How to run gdcm examples in ubuntu?

I am trying to run this simple example in GDCM. I have installed the library c++ version and the installation works perfectly fine but I am not able to figure out how to compile and run a example.
#include "gdcmReader.h"
#include "gdcmWriter.h"
#include "gdcmAttribute.h"
#include <iostream>
int main(int argc, char *argv[])
{
if( argc < 3 )
{
std::cerr << argv[0] << " input.dcm output.dcm" << std::endl;
return 1;
}
const char *filename = argv[1];
const char *outfilename = argv[2];
// Instanciate the reader:
gdcm::Reader reader;
reader.SetFileName( filename );
if( !reader.Read() )
{
std::cerr << "Could not read: " << filename << std::endl;
return 1;
}
// If we reach here, we know for sure only 1 thing:
// It is a valid DICOM file (potentially an old ACR-NEMA 1.0/2.0 file)
// (Maybe, it's NOT a Dicom image -could be a DICOMDIR, a RTSTRUCT, etc-)
// The output of gdcm::Reader is a gdcm::File
gdcm::File &file = reader.GetFile();
// the dataset is the the set of element we are interested in:
gdcm::DataSet &ds = file.GetDataSet();
// Contruct a static(*) type for Image Comments :
gdcm::Attribute<0x0020,0x4000> imagecomments;
imagecomments.SetValue( "Hello, World !" );
// Now replace the Image Comments from the dataset with our:
ds.Replace( imagecomments.GetAsDataElement() );
// Write the modified DataSet back to disk
gdcm::Writer writer;
writer.CheckFileMetaInformationOff(); // Do not attempt to reconstruct the file meta to preserve the file
// as close to the original as possible.
writer.SetFileName( outfilename );
writer.SetFile( file );
if( !writer.Write() )
{
std::cerr << "Could not write: " << outfilename << std::endl;
return 1;
}
return 0;
}
/*
* (*) static type, means that extra DICOM information VR & VM are computed at compilation time.
* The compiler is deducing those values from the template arguments of the class.
*/
It has a few header files that it is looking for namely gdcmreader, gdcmwriter and I want to figure out the compiler flags to use to be able to run this file.
I am doing g++ a.cpp -lgdcmCommon -lgdcmDICT but that gives me the error
a.cpp:18:24: fatal error: gdcmReader.h: No such file or directory
compilation terminated.
Can you please help me out? I have searched everywhere but I can't seem to figure out how to run this file.
When using files that are in different locations of your "normal" files you must instruct the compiler and the linker how to find them.
Your code has a #include <someFile.h> command.
The <> usage means "in other path". The compiler already knows common "other paths" as for "stdio" for common libraries.
In case of "not normal", you can tell g++ where to find the headers by adding -Imydir to the command line (replace 'mydir' with the proper path)
For the libraries, static (.a) or dynamic (.so) the same history stands.
The -Lmydir tells g++ where to look for libraries.
Your command line may look like
g++ a.cpp -I/usr/include -L/usr/local/lib -lgdcmCommon -lgdcmDICT
You did not tell how did you install gdcm library, I assume that using apt system. There are two types of libraries, "normal" and "developer" ones. To be able to compile your own software, you need the latter. So, for example in Ubuntu 16.04, type apt-get install libgdcm2-dev. Then all necessary headers will be installed in /usr/include/gdcm-2.6.

SDL 2.0 won't initialize. Error: "Failed to connect to the Mir Server"

I am running Ubuntu 14.04, and using Eclipse CDT.
In my program, I am trying to initialize SDL and if it doesn't initialize then output the error, but SDL_GetError() returns "Failed to connect to the Mir Server". I am sure SDL is installed correctly since I can successfully initialize SDL on another project.
These are the libraries I am using: http://i.imgur.com/SS1mjzQ.png
#include <SDL2/SDL.h>
#include <iostream>
int main(int argc, char* args[]) {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cout << SDL_GetError() << std::endl;
}
return 0;
}
bash$ export DISPLAY=:0
Setting the DISPLAY run time environment variable fixes it for me — typical X Windows lossage, fails to default to your local display (designed for remote displays) which you'd only know if you'd gone to X11 summer camp.
A complete working example in bash:
(cd /tmp && g++ -xc++ - -lSDL2 && (DISPLAY=:0 ./a.out; echo \$? = $?)) <<.
#include <SDL2/SDL.h>
#include <iostream>
int main() {
if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
std::cout << SDL_GetError() << std::endl;
return 1;
}
return 0;
}
.
$? = 0
Does the environment you are running on have a windowing system?
This error has come up for me when I'm running tests initializing SDL2 on Travis CI using Ubuntu 14.04. Based on what I've been able to gather from further testing and hints I've gotten from google searches SDL_Init when initializing SDL if it is passed SDL_INIT_VIDEO (which you are doing implicitly with SDL_INIT_EVERYTHING), it will try and connect with the windowing system of your machine.
So perhaps try:
SDL_Init(0)
and then initialize the other subsystems later with:
SDL_InitSubSystem(SDL_INIT_EVERYTHING)
If you do keep in mind that you must quit every subsystem you initialize in this case it's just technically. You would do that like this:
SDL_QuitSubSystem(SDL_INIT_EVERYTHING)
I had this error when I was using gcc to compile.
When I used g++ to compile it fixes the issue. The Lazy Foo tutorial also recommends you to use g++ to compile.
If you are having this problem you can try using g++ to compile and see if this resolves the issue.