I spent the last 2 hours trying to figure out what is wrong with my code. Some solutions on Google/StackOverflow didn't change the problem.
Here's this simple code :
#include <SFML/Graphics.hpp>
#include <fstream>
int WinMain()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "BBS");
window.setFramerateLimit(60);
// redirect sfml stderr to this file
std::ofstream file("sfml-log.txt");
std::streambuf* previous = sf::err().rdbuf(file.rdbuf());
sf::err() << "ABCD" << std::endl;
sf::Font font;
// this line fail and make stderr crash
font.loadFromFile("E:\\arial.ttf");
sf::err() << "XYZ" << std::endl;
return 0;
}
The file "sfml-log.txt" is created but it contain everytime the same thing :
ABCD
Failed to load font "
For the context I launch the executable within the same directory as sfml DLLs.
I tried to modify the font path to "arial.ttf" and put it in the same directory, nothing changed.
Thanks in advance for your help.
Ok, I just solved it.
I was compiling with debug settings in VS and was linking with
sfml-graphics.lib sfml-window-s.lib sfml-window.lib sfml-system-s.lib sfml-system.lib
instead of
sfml-graphics-d.lib sfml-window-s-d.lib sfml-window-d.lib sfml-system-s-d.lib sfml-system-d.lib
Related
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++.
// End Of Line.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <streambuf>
#include <cstdio>
using namespace std;
ifstream input_file;
int EOL = 0;
int main()
{
//Enter end of line data into file
input_file.open("EOL Data", ios::in);
do
{
cout << "\n" << "\n" << "\n" << "\n";
++EOL;
} while (EOL == 0);
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu
// Tips for Getting Started:
// 1. Use the Solution Explorer window to add/manage files
// 2. Use the Team Explorer window to connect to source control
// 3. Use the Output window to see build output and other messages
// 4. Use the Error List window to view errors
// 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add
existing code files to the project
// 6. In the future, to open this project again, go to File > Open > Project and select the .sln
file
When trying to execute the above, get the error" Not enough resources to execute the program". Not
sure why this error is being posted.
Found the problem, opening the file is input, should have been opened as output. Not sure why this would generate a resource limitation problem.
I've created a repository that has the most basic way to reproduce my issue. I use Cmake to build and it's set up to statically link freetype into my executable.
FreeType is a submodule, so you can clone it all at once with:
git clone --recurse-submodules https://github.com/jeffw387/freetype_minimum_test
or if you prefer just clone freetype from git into the externals directory.
I'm currently testing on Ubuntu.
I can initialize FreeType, create a face from the font, and load a glyph from a character code with the FT_LOAD_RENDER flag all without errors.
Then when I inspect the bitmap buffer, it's zero-initialized on every row. I used gdb from within VS Code in order to inspect the buffer, but before that I also tried copying out the data line by line.
The font I'm testing with is a free font I found online, but I was able to test it in Libre Office and it seems to work just fine.
Can anyone spot a problem with the code? If anyone is willing I'd love to know if this repo creates the same issue on their machine.
Here's my cpp file for easy viewing:
#include <ft2build.h>
#include FT_FREETYPE_H
#include <iostream>
#include <vector>
int main() {
FT_Library library{};
if (FT_Init_FreeType(&library)) {
std::cout << "Error initializing FreeType.";
}
FT_Face face{};
auto faceResult = FT_New_Face(library, "Anke.ttf", 0, &face);
if (faceResult) {
std::cout << "Error creating face.";
}
if ((face->face_flags & FT_FACE_FLAG_SCALABLE) != FT_FACE_FLAG_SCALABLE) {
std::cout << "Error: font is not scalable.";
}
if (FT_Set_Pixel_Sizes(face, 0, 50)) {
std::cout << "Error setting font pixel size.";
}
if (FT_Load_Char(face, 'P', FT_LOAD_RENDER)) {
std::cout << "Error loading or rendering glyph.";
}
auto bmp = face->glyph->bitmap;
return 0;
}
I've done some further testing and it seems like there was a problem with my freetype fork. I haven't gone to the trouble of figuring out what it was, but in any case now that I've tried it with a fresh clone from https://github.com/aseprite/freetype2 it works.
I want to have my DLL's in a subdirectory of the directory where my executable is. My current directory looks like:
Main Folder: [Folder]
Program.exe
sfml.dll
Assets [Folder]
Picture.png
Music.wav
When I really want it to look like:
Main Folder: [Folder]
Program.exe
Assets [Folder]
Picture.png
Music.wav
MyDlls[Folder]
sfml.dll
When I try to put them (DLL's) in a folder I get the error message:
The program can't start because sfml-system-d-2.dll is missing from your computer. Try reinstalling the program to fix this problem.
So, then I looked into explicit linking, and followed the tutorial here:
http://www.dreamincode.net/forums/topic/118076-dlls-explicit-linking/
If explicit linking is not what I need to use, then please tell me what I need to do. Else, please tell me what is wrong with my code below: (Also, I do not know if this is static or dynamic linking..??)
// Startup.h
#ifndef STARTUP_H
#define STARTUP_H
#include <iostream>
#include <windows.h>
class Startup
{
private:
HINSTANCE hDLL;
public:
// Explicitly link SFML DLL's
typedef int(*funcAdd) (int, int);
typedef int(*funcSubtract) (int, int);
void LoadDLLs()
{
// Retrieve DLL handle.
vector<LPCSTR> libraries = {"openal32.dll",
"sfml-audio-2.dll",
"sfml-audio-d-2.dll",
"sfml-graphics-2.dll",
"sfml-graphics-d-2.dll",
"sfml-system-2.dll",
"sfml-system-d-2.dll",
"sfml-window-2.dll",
"sfml-window-d-2.dll"};
for (int i = 0; i < libraries.size(); i++)
{
hDLL = LoadLibrary(libraries[i]);
if (hDLL == NULL)
{
std::cout << "Failed to load library.\n";
}
else
{
funcAdd Add = (funcAdd)GetProcAddress(hDLL, "Add");
funcSubtract Subtract = (funcSubtract)GetProcAddress(hDLL, "Subtract");
if (Add)
std::cout << "10+10=" << Add(10, 10) << std::endl;
if (Subtract)
std::cout << "50-10=" << Subtract(50, 10) << std::endl;
FreeLibrary(hDLL);
}
std::cin.get();
}
};
#endif
You could register an App Path (see link), making sure you add your Applications alternate DLL folder location to the App Path PATH value.
You cannot do what you want directly. The code you attached will work only for dynamic loading dlls, but it is not the case.
What you want to do will be platform specific and you need to set the path for the library before executing the program.
I am trying to do console application to read pixels from image:
#include <QtCore/QCoreApplication>
#include <QtGui/QImage>
#include <iostream>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QImage *img = new QImage("adadad.jpg");
//std::cout << "Type filename:" << std::endl;
img->isNull();
return a.exec();
}
That doesn't work I got: (IT doesn't compile, but anyway file isn't exist yet...)
File not found: tmp/obj/debug_shared/main.o:: In function `main':
What is going on? Is it impossible to use Qimage with console app?!
EDIT:
screen
It is possible to use QImage in a console application, you must make sure that QtGui is configured though. If you chose a console app, your .pro file might contain something like
CONFIG += console
QT -= gui
If that's the case, remove the QT -= gui line.
QImage("adadad.jpg");
Will probably look for a file called adadad.jpg on the current working directory for your application. Check if that file is present. Otherwise, use a fully qualified path.
img->isNull() doesn't do anything on it's own, try this instead:
if(img->isNull())
std::cout << "Image isNull!\n";
else
std::cout << "Image loaded\n";
My guess is that the local directory of the executable is not the same as the location of that image, so Qt can't find the file. Try specifying the complete path.
EDIT: Ahh... didn't realize it was a compilation problem. That looks suspiciously like a moc issue. What build system are you using? and can you confirm that the moc step is executing?
This modification of your code will compile and run as expected if there is a valid image file in the current working directory when you run the app. It will display Image loaded
#include <QtGui/QImage>
#include <iostream>
int main(int argc, char *argv[])
{
QImage *img = new QImage("adadad.jpg");
if(img->isNull())
std::cout << "Image is null";
else
std::cout << "Image loaded";
return 0;
}
You do not need to create an instance of QCoreApplication unless you have subclassed it and put your program code in that subclass.
Update:
Your program does not exit so you are probably getting that compile error because it can't replace the executable because it is still running (and locked). The file locking is more likely to be an issue under Windows.
An important note when you are loading a file using directly "adadad.jpg" in your code. Even if you put the file inside the debug/release folder, QImage will always be null if loaded this way.
I run into this problem yesterday and I fixed it by using the Qt library to get the full path: QCoreApplication::applicationDirPath().
There is two way to achieve that, first one is when you create the img object.
QImage img( QCoreApplication::applicationDirPath() + "adadad.jpg");
if( img.isNull())
{
qDebug() << "Loading Error - file: adadad.jpg.";
return false;
}
or using the load function
QImage img;
if( !img.load(QCoreApplication::applicationDirPath() + "adadad.jpg"))
{
qDebug() << "Loading Error - file: adadad.jpg.";
return false;
}