PNG's don't load with allegro5, but BMP's do - c++

I'm trying to learn how to use allegro5 so I installed it from source on their github and it was going fine until I tried to load an image. Here is my code that wasn't working.
#include<allegro5/allegro.h>
#include<allegro5/allegro_image.h>
#include<stdio.h>
int main(int argc, char **argv){
al_init();
al_init_image_addon();
al_create_display(640,560);
ALLEGRO_BITMAP * image = al_load_bitmap("pi.png");
while(true){
al_clear_to_color(al_map_rgb(100,0,0));
if(image == NULL){
printf("Image didn't load properly\n");
return -1;
}
al_draw_bitmap(image, 0, 0, 0);
al_flip_display();
}
}
But after making a pi.bmp in the same directory and then changing the
ALLEGRO_BITMAP * image = al_load_bitmap("pi.png");
to
ALLEGRO_BITMAP * image = al_load_bitmap("pi.bmp");
It works as expected. I already have libpng16-16 and libpng-dev installed. I am compiling the file (main.cpp) with this command:
g++ main.cpp -I/usr/local/include/allegro5 -L/usr/local/lib -lallegro -lallegro_image -lpng
Does anyone know what I am doing wrong?
EDIT: By not working I do mean that image == NULL is true. Also I just tried to do it on my laptop. I believe the problem must be somewhere with my installation. My desktop (the one that doesn't work) is running Ubuntu 18.10 (cosmic) and my laptop is running 18.04 (bionic) and works fine after adding the ppa and updating. It seems they only have pre-compiled binaries for bionic, which is what forced me to compile from source.
EDIT2: #rcorre's answer worked for me! Hopefully this will help someone else out in the future. When building you need to use the command cmake -D WANT_IMAGE_PNG=1 .. from the build directory.

Related

Allegro5 cannot decode png images on Ubuntu 20.04 (using cmake)

Allegro5 cannot seem to be able to decode png images for my game project. This function:
ALLEGRO_BITMAP* BrushCollection::LoadBitmapFromFile(const char* fileName)
{
ALLEGRO_PATH* path = al_get_standard_path(ALLEGRO_EXENAME_PATH);
al_append_path_component(path, "assets");
al_set_path_filename(path, fileName);
ALLEGRO_BITMAP* bitmap = al_load_bitmap(al_path_cstr(path, '\\'));
allegro_init(bitmap, L"UI Bitmap");
return bitmap;
}
returns NULL. I know image paths are correct because the same code works on Windows.
Related to this question: here
I'm having this same issue in Ubuntu 20.04 with the precompiled allegro 5.2.7 package! I added png as an external library to the project. I do have libpng16-16 and libpng-dev installed in my system, but allegro can't seem to be able to decode png images!
This is the command generated by cmake by the way, which seems to be including libpng in the linking:
/usr/bin/c++ -g CMakeFiles/Planetaire.dir/src/main.cpp.o CMakeFiles/Planetaire.dir/src/Game.cpp.o CMakeFiles/Planetaire.dir/src/brush/BrushCollection.cpp.o CMakeFiles/Planetaire.dir/src/events/EventStack.cpp.o CMakeFiles/Planetaire.dir/src/events/MouseEvents.cpp.o CMakeFiles/Planetaire.dir/src/text/FontCollection.cpp.o CMakeFiles/Planetaire.dir/src/tools/AllegroInit.cpp.o CMakeFiles/Planetaire.dir/src/tools/Debug.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/ContinueGame.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/HideInGameMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/HideMainMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/QuitGame.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/QuitToDesktop.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/ShowInGameMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/ShowMainMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/actions/StartNewGame.cpp.o CMakeFiles/Planetaire.dir/src/ui/components/AbstractComponent.cpp.o CMakeFiles/Planetaire.dir/src/ui/components/InGameMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/components/MainMenu.cpp.o CMakeFiles/Planetaire.dir/src/ui/AbstractUIElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/ButtonElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/ImageElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/MainUIElement.cpp.o CMakeFiles/Planetaire.dir/src/ui/PanelElement.cpp.o CMakeFiles/Planetaire.dir/src/world-generator/Terrain.cpp.o -o Planetaire -lallegro -lallegro_main -lallegro_font -lallegro_ttf -lallegro_image -lallegro_primitives /usr/lib/x86_64-linux-gnu/libpng.so
I wonder what I'm doing wrong here. This works out of the box on windows.
I think I know the answer!
The path is simply wrong, when doing:
ALLEGRO_BITMAP* bitmap = al_load_bitmap(al_path_cstr(path, '\\'));
This is building a backward slash path. Linux/Mac can't find this file. When I corrected this path, the application worked!

How to use LibVLC with Qt 5

I'm trying to use LibVLC in a Qt 5 program to open a VLC instance and play a video.
The following code comes from https://wiki.videolan.org/LibVLC_Tutorial/
I'm using Linux.
.pro :
TEMPLATE = app
TARGET = projectLoic
INCLUDEPATH += . vlc
QT += widgets
# Input
HEADERS +=
SOURCES += main.cpp
LIBS +=-lvlc
main :
#include <vlc/vlc.h>
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
libvlc_instance_t * inst;
libvlc_media_player_t *mp;
libvlc_media_t *m;
// Load the VLC engine
inst = libvlc_new(0, NULL);
// Create a new item
m = libvlc_media_new_path (inst, "/home/........mp3");
// Create a media player playing environement
mp = libvlc_media_player_new_from_media (m);
// play the media_player
libvlc_media_player_play (mp);
return app.exec();
}
The compilation is fine. But the program immediatly crashes when I build it (with Qt Creator). Any idea?
Many thanks
Many things could cause this crash. The best is to get VLC source code to trace back the issue. Passing the option '--verbose=2' when initializing libVLC can help as well.
In my case the cause of the crash was due to this bug in the ubuntu package of vlc:
https://bugs.launchpad.net/ubuntu/+source/vlc/+bug/1328466
When calling libvlc_new() vlc modules and their dependent libraries are loaded into memory. The qt module of LibVLC was searching for Qt4 shared objects instead of Qt5 (manually installed).
The solution was to rebuild the module cache which was outdated an pointing to Qt4 binaries. You can reset it on the command line:
sudo /usr/lib/vlc/vlc-cache-gen -f /usr/lib/vlc/plugins/
Or pass to vlc the option:
--reset-plugins-cache
I have never used this library, but Are you using exactly this code?
m = libvlc_media_new_path (inst, "/home/........mp3");
This path may be the problem.
What distribution of Linux are you using?
I ran into this same problem with Qt5 and LibVLC, and the primary cause (Ubuntu 12.04 LTS and 14.04 LTS), was that LibVLC was loading the qt4 interface plugin, which conflicted with Qt5. If you check your call stack, you'll most likely see that at a Qt4 library was being loaded, causing the crash.
If this is the problem, there are only 3 options (tested with LibVLC 2.2 and Qt5 on Ubuntu 12.04 and 14.04 LTS 64 bit).
The first (worst) is to delete the qt4 user interface plugin. You can test this is the problem by moving and running and then setting it back. Deleting will break your regular VLC player most likely.
Second option is to create a copy of the plugins directory and set that in your runtime path using VLC_PLUGIN_PATH, environment variable. but I've had trouble getting that to work without changing the original plugin path folder also (which will break your VLC player unless you modify your shortcuts, etc. to also set VLC_PLUGIN_PATH.
The third option, and what I ended up doing, was to custom build my own static LibVLC binary with a few edits to the source code so that it will not load the qt4 interface plugin installed with VLC. You can follow these steps to do this.
1) Download VLC Source Code for your platforms distribution.
http://download.videolan.org/pub/videolan/vlc/
Make sure you download the version matching your distribution. For
example, match the VLC version to what is installed with Ubuntu.
2) Extract source code to folder
3) Install dependencies for the OS distribution
sudo apt-get build-dep vlc
4) Modify src/modules/bank.c
Edit the module_InitDynamic function
Add the following code at the top of the function:
// HACK TO DISABLE QT4 PLUGIN
if(strstr(path, "qt4") != NULL)
return NULL;
// END HACK
3) From terminal
./bootstrap
./configure --disable-qt --disable-skins2 --enable-xcb --prefix=/home/$USER/vlc-custom_build_output_folder_name
./make
./make install
4) Copy/Save the resulting files in the install folder.
Then just link against this library instead.

Trying to compile a blank window with GTK3

I want to learn how to code GUI programs in Linux, so I chose GTK3, but I'm running into some issues compiling the code for a simple blank window in GTK3. I'm using the Code::Blocks IDE, and this is my code. I'm almost certain it's correct because I copy-and-pasted it from a well known GTK tutorial site. Anyway, here's the code I have:
#include <gtk-3.0/gtk/gtk.h>
int main( int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show(window);
gtk_main();
return 0;
}
The code can't compile, and Code::Blocks states: "fatal error: gdk/gdk.h: No such file or directory"
I don't know why it says this, because I can look in my usr/include folder and I see the gdk folder as well as gdk.h within it. Before I tried to compile the code I installed GTK 3.0+ via the following console command:
sudo apt-get install libgtk-3-dev
What am I doing wrong? I would greatly appreciate any help you guys can give me.
just use #include .
if u build with pkg-config and gcc, it should looks like gcc -Wall filename filename.c -o pkg-config --cflags --libs gtk+-3.0

How to configure a fresh installation of qtcreator?

I'm trying to configue QtCreator with no success. I want to open an existing project. When I try to compile and run it, I get only empty black console. It looks like it compiles fine but no output.
I've installed this 32bit version:
Qt 5.2.0 for Windows 32-bit (MinGW 4.8, OpenGL, 689 MB) (Info)
When I start a new project, QtCreator wants me to run cmake. When It's done, it works fine. I can also build HelloWorld with "g++ main.cpp"
Can anyone tell me what should I do step by step? I ran out of strength. I don't know why it doesn't work.
I think it is not very important, but I'm using Win7 64bit. First, I tried to install 64bit QtCreator:
Qt 5.2.0 for Windows 64-bit (VS 2012, OpenGL, 589 MB) (Info)
But I had some problems with compilers. It looked like I didn't have them installed. I also tried an online installer and now this 32bit version. Always same result.
EDIT:
I found out something. This works:
ComputerGraphics.pro
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
INCLUDEPATH += C:/Users/user/Desktop/projekty/PocitacovaGrafika2/opencv/include
LIBS += -LC:\\Users\user\\Desktop\\projekty\\PocitacovaGrafika2\\opencv\\bin \
libopencv_core246d \
libopencv_highgui246d \
libopencv_imgproc246d \
SOURCES += main.cpp \
and main.cpp
#include <iostream>
int main(){
std::cout << "Hello";
return 0;
}
This even doesn't print Hello (.pro is the same as previously):
#include <iostream>
int main(){
std::cout << "Hello";
IplImage* img = cvLoad("Desert.jpg",1);
cvShowImage("img",img);
cvWaitKey(0);
return 0;
}
In debug, it complains:
The gdb process terminated unexpectedly (code 0)
During startup program exited with code 0xc0000135.
My solution:
1)
I've gone through my own tutorial on how to compile opencv for qt-creator with cmake here:
How to link opencv in QtCreator and use Qt library
(but using the latest opencv 2.4.8)
2)
I created a folder opencv on desktop.
I went into newly created opencv_bin/install and copied a folder include into the opencv on the desktop.
I continued into directory: install/x64/mingw and copied both bin and lib into opencv on the desktop.
3)
I moved the desktop/opencv directory into a working directory of my project.
i.e cut: desktop/opencv; paste: somepath/myQtProject/
4)
My .pro contains a reference like this:
INCLUDEPATH += C:/Users/user/Desktop/projekty/PocitacovaGrafika2/opencv/include
LIBS += -LC:\\Users\\user\\Desktop\\projekty\\PocitacovaGrafika2\\opencv\\bin \
libopencv_core248d \
libopencv_highgui248d \
libopencv_imgproc248d \
And in the main.cpp just #include "opencv2/opencv.hpp"
PS: I hope this will also help somebody else.

Qt Creator + OpenCV: Program runs from .exe but not from editor

Well, I need to start working with OpenCV and as I'm used to working with QtCreator, I'm trying to make it all work together. I downloaded the latest OpenCV version, and compiled it with MinGW. Then, I created this little console project to try it out. Below is the .pro file:
QT += core
QT -= gui
TARGET = OpenCV_test4
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
SOURCES += main.cpp
INCLUDEPATH += C:\\Librerias\\opencv2.3.1\\release\\include
LIBS += -LC:\\Librerias\\opencv2.3.1\\release\\lib \
-lopencv_core231.dll \
-lopencv_highgui231.dll \
-lopencv_imgproc231.dll \
-lopencv_features2d231.dll \
-lopencv_calib3d231.dll
Here is the main.cpp file:
#include <QtCore/QCoreApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// read an image
cv::Mat image= cv::imread("img.jpg");
// create image window named "My Image"
cv::namedWindow("OpenCV Window");
// show the image on window
cv::imshow("OpenCV Window", image);
// wait key for 5000 ms
cv::waitKey(5000);
return a.exec();
}
(I have tried this code with and without the QCoreApplication lines)
The deal is: It links and builds, and when runs from QtCreator only a terminal window called C:\QtSDK\QtCreator\bin\qtcreator_process_stub.exe appears with the line "Press RETURN to close this window..."
But, if I run the .exe from the project folder, it runs perfectly!! Why is QtCreator unable to launch the application? I found this really strange, and I would appreciate any hint about this. It's really not THAT important, but it is kind of a pain to have to run the .exe manually every time I change something to check how it works.
Thanks for your time :)
Additional Info:
I have tried both debug and release versions, the problem is the same in both of them.
Debugging does not work, it never stops at any breakpoint.
I'm running on Windows 7 Proffesional x64
SOLVED, I don't really know what I did, it suddenly worked and keeps working, I wish I could tell you how I fixed it but I have no idea, such a weird thing :(
Check Projects -> Run Settings -> Run in terminal. It have to be enabled, but seems to be disabled.
I have met same problem with QtCreator and OpenCL under Linux. Simple test program works after start from terminal and it does not work after start from the QtCreator.
I found the cause was hardcoded LD_LIBRARY_PATH in the project's run environment settings. I had dropped it to empty string and this had fixed issue.
I had the same issue with the following environment: Raspbian, Qt, openCV and a gui application.
old-ufo recommendation worked for me:
- First, enable "Run in terminal", which failed
- Then, disable "Run in terminal", which allowed me to correctly debug my app.
I understand that this is not scientific.