I'm trying to follow Lazy Foo's tutorials. But when I try to run one of his examples I get this compiler error:
error: SDL/SDL_image.h: No such file or directory
The compiler/linker is set up correctly, I'm using Code::Blocks on Windows XP.
However, the problem is simply that there are no SDL_image.h. I've checked in the folder that it supposedly should have been. I tried to download the SDL library again and checked again, still no SDL_image.h file. Where did the SDL_image.h file go?
The library I dowloaded was the 'SDL-devel-1.2.14-mingw32.tar.gz' under 'Development Libraries' for Win32 from this link: http://www.libsdl.org/download-1.2.php
You need to install SDL_image separately. It's not shipped with SDL.
You need to install SDL_image library like mentioned in the other answers, if you are on a Debian based systems you can simply install with the following command:
sudo apt-get install libsdl-image1.2-dev
In the third tutorial of lazyfoo is completely explained.
Basically, you must add "-lSDL_image" to the compilation line.
In your case as you are using windows, then you should first install sdl_image and then
#include <SDL_image.h>
not
#include <SDL/SDL_image.h>
If you were using linux and your sdl-image package is installed to /usr/include/SDL then you need to use
#include <SDL_image.h>
In most cases when you install from source in linux. Your package may not be resident in /usr/include/SDL
In these kind of situation, I use
#include <SDL/SDL_image.h>
and it works
For anyone who tries this, an update would be to actually add "-lSDL2_image" to your compilation line. Everyone else simply has -lSDL_image" which changed when SDL2 released. After that just go to the bin and add all of your .dll files to System32 and you should be all set!
You have to Download
"SDL_image-devel-1.2.4-VC6.zip"
For code blocks
download link »
http://www.libsdl.org/projects/SDL_image/release/SDL_image-devel-1.2.4-VC6.zip
copy the files present in the include folder that you will find inside the zip file after extraction.And paste it to the C:\SDL\include\SDL in my case or to the directory where your other
SDL *.h are present.
Similary, Copy the files present in the lib folder of the zip file and paste it to C:\SDL\lib or to the folder where other lib files are present..
Then copy all the *.dll files present in the archive to the C:\windows\system32
Further you have to add "-lSDL_image" to the compilation line by openning settings > compiler& debugger > linker.
Then open a empty file project and add empty file to the project then #include "SDL\SDL_image.h"
Hope it works for you !!
Or
First download SDL_image-devel-1.2.4-VC6.zip from above given link and
Goto link >> http://www.lazyfoo.net/SDL_tutorials/lesson03/windows/codeblocks/index.php for more detailed explaination.
SDL2 Windows Setup for (32b) that worked for me (C language):
download SDL2_image-devel-2.0.5-mingw.tar.gz and SDL2_image-2.0.5-win32-x86.zip (32 chose other for 64) from here: https://www.libsdl.org/projects/SDL_image/.
copy "SDL2_image-devel-2.0.5-mingw\SDL2_image-2.0.5\i686-w64-mingw32\include\SDL2\SDL_image.h" to you SDL folder where all your headers are my case "MinGW\include\SDL2".
copy content from "SDL2_image-devel-2.0.5-mingw\SDL2_image-2.0.5\i686-w64-mingw32\bin" to "\MinGW\bin".
copy content of : "SDL2_image-devel-2.0.5-mingw\SDL2_image-2.0.5\i686-w64-mingw32\lib" to "MinGW\lib"
include header like this :
#include <SDL2/SDL_image.h>
link it in your makefile (see this '... -llibSDL2_image ...'):
build:
gcc -Wfatal-errors \
-std=c99 \
./*.c \
-I"C:\libsdl\include" \
-L"C:\libsdl\lib" \
-lmingw32 \
-lSDL2main \
-lSDL2 \
-lSDL2 \
-llibSDL2_image \
-o example.exe
Dummy CodeExample.c
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
#include <stdio.h>
int main(int argc, char *args[])
{
// attempt to initialize graphics and timer system
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0)
{
printf("Error initializing SDL: %s\n", SDL_GetError());
}
// Declare pointers
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *bitmapTex = NULL;
SDL_Surface *bitmapSurface = NULL;
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_CENTERED, // initial x position
SDL_WINDOWPOS_CENTERED, // initial y position
840, // width, in pixels
480, // height, in pixels
SDL_WINDOW_OPENGL // flags - see below
);
// Check that the window was successfully created
if (!window)
{
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
// create renderer which sets up graphics hardware
Uint32 render_flags = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC;
renderer = SDL_CreateRenderer(window, -1, render_flags);
if (!renderer)
{
printf("error creating renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// Load theimage into memory using SDL_Image library function
bitmapSurface = IMG_Load("image.png");
bitmapTex = SDL_CreateTextureFromSurface(renderer, bitmapSurface);
SDL_FreeSurface(bitmapSurface);
if (!bitmapTex)
{
// In the case that the window could not be made...
printf("Error creating texture: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
// The window is open: could enter program loop here (see SDL_PollEvent())
while (1)
{
SDL_Event e;
if (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
break;
}
}
// Clear the window
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, bitmapTex, NULL, NULL);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(bitmapTex);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
The SDL library is modular and only the core is distributed, by default, when you acquire the library. For SDL 1, this includes SDL, itself, and (for those writing SDL applications) SDL-devel; for SDL 2, the libraries are SDL2 and SDL2-devel. The corresponding include files in developer's applications are <SDL/SDL.h> and <SDL2/SDL.h>, with the include files having been installed in whatever location is standard for your system, when the *-devel libraries are acquired and installed.
The modules all follow a similar pattern, SDL_X, SDL_X-devel for SDL 1 and SDL2_X and SDL2_X-devel for SDL 2, for module X, with the corresponding developers' include files being <SDL/SDL_X.h> and <SDL2/SDL_X.h>. For instance, for the image module, X = image, the libraries are SDL_image, SDL_image-devel, SDL2_image, SDL2_image-devel, and the include files are <SDL/SDL_image.h> and <SDL2/SDL_image.h>.
The modules are: "image" (as just mentioned) for handling images in the different standard formats (e.g. PNG); "mixer", for handling the different audio file formats, "gfx" for graphics drawing primitives, "net" for networked applications, "rtf" for Rich Text Format, "ttf" for the font-handling (ttf stands for "TrueTypeFont").
The Source Code for SDL is over here:
https://github.com/orgs/libsdl-org/repositories?type=all
Related
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!
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.
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!
I am using Ubuntu 12.04 and I have installed OpenGL4.
I also have a CUDA-enable NVIDIA graphics card. Note that, I have been doing parallel computation with CUDA on my PC and that works.
[eeuser#roadrunner sample_opengl]$ glxinfo | grep gl
server glx vendor string: NVIDIA Corporation
server glx version string: 1.4
server glx extensions:
client glx vendor string: NVIDIA Corporation
client glx version string: 1.4
client glx extensions:
GL_ARB_texture_query_lod, GL_ARB_texture_rectangle, GL_ARB_texture_rg,
GL_NV_texture_multisample, GL_NV_texture_rectangle, GL_NV_texture_shader,
I cannot get a simple program to work. Can anyone give a sample program that can work on my PC
Here is the code I am using:
#include <GL/glew.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library
#include <stdio.h>
int main () {
// start GL context and O/S window using the GLFW helper library
if (!glfwInit ()) {
fprintf (stderr, "ERROR: could not start GLFW3\n");
return 1;
}
// uncomment these lines if on Apple OS X
/*glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);*/
GLFWwindow* window = glfwCreateWindow (640, 480, "Hello Triangle", NULL, NULL);
if (!window) {
fprintf (stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent (window);
// start GLEW extension handler
glewExperimental = GL_TRUE;
glewInit ();
// get version info
const GLubyte* renderer = glGetString (GL_RENDERER); // get renderer string
const GLubyte* version = glGetString (GL_VERSION); // version as a string
printf ("Renderer: %s\n", renderer);
printf ("OpenGL version supported %s\n", version);
// tell GL to only draw onto a pixel if the shape is closer to the viewer
glEnable (GL_DEPTH_TEST); // enable depth-testing
glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
/* OTHER STUFF GOES HERE NEXT */
// close GL context and any other GLFW resources
glfwTerminate();
return 0;
}
I tried compiling with -
g++ main2.cpp -lglut -lGL -lGLEW -lGLU
I get an error :
main2.cpp:2:47: fatal error: GLFW/glfw3.h: No such file or directory
compilation terminated.
I am now wondering what is this GLFW? More precisely how do I install it? Following #eapert
I installed libglfw as -
sudo apt-get install libglfw-dev
Still get the following errors
main2.cpp: In function ‘int main()’:
main2.cpp:18:2: error: ‘GLFWwindow’ was not declared in this scope
main2.cpp:18:14: error: ‘window’ was not declared in this scope
main2.cpp:18:79: error: ‘glfwCreateWindow’ was not declared in this scope
main2.cpp:24:32: error: ‘glfwMakeContextCurrent’ was not declared in this scope
Install the "dev" package for GLFW: sudo apt-get install libglfw3-dev
Warning: the version you get from the package manager may be out of date.
You can follow the instructions here or here.
I tried compiling with -
g++ main2.cpp -lglut -lGL -lGLEW -lGLU
Why are you linking GLUT when you want to use GLFW? Also you don't need GLU for your program. Try this:
g++ main2.cpp -lGL -lGLEW -lglfw
WARNING: The question was changed quite significantly since this answer was made
"I tried a few tutorials but I cannot get a simple program to work" I assume, given what you said earlier, your CUDA programs work on Windows, just not on Ubuntu?
1) Try installing a newer Ubuntu version first (if you have the option on that PC). 12.04 is a bit old and you probably shouldn't be using it if you don't have a good reason to (i.e. it's a company PC and you would violate upgrade policy, something along those lines)
2) Try installing the proprietary NVIDIA drivers for your card (this should also give you the NVIDIA OpenGL implementation); You probably have mesa installed. I suppose current mesa version have at least some GPGPU support, but I'm not sure if they support CUDA (if you aren't particularly fond of CUDA you can also try OpenCL, you might have better chances with that. Although there's still a good chance you'll need the proprietary NVIDIA drivers).
GLFW is a helper library for creating windows, setting up OpenGL in them and provides access to input functions etc. It's similar to GLUT - which you seem to be familiar with - in that regard, but a much more modern alternative. You'll of course need to install it to use it. You should be able to install it through Ubuntus package manager as per usual, try apt-cache search glfw which should help you find the exact package name. If there are multiple "lib" versions, install the one ending in -dev.
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>