I'm trying to make an application with SDL and I would like to add an icon on my window but when I compile I get this error :
error: ‘IMG_Load’ was not declared in this scope
SDL_Surface * icon_surface = IMG_Load(icon);
^
I'am using cmake and make for the compilation, here is the CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
set(PROJECT_NAME "Engine")
project (${PROJECT_NAME})
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c++0x")
if( CMAKE_SIZEOF_VOID_P EQUAL 8 )
MESSAGE( "64 bits compiler detected" )
SET( EX_PLATFORM 64 )
SET( EX_PLATFORM_NAME "x64" )
SET( EXECUTABLE_NAME ${PROJECT_NAME}-x64 )
else( CMAKE_SIZEOF_VOID_P EQUAL 8 )
MESSAGE( "32 bits compiler detected" )
SET( EX_PLATFORM 32 )
SET( EX_PLATFORM_NAME "x86" )
SET( EXECUTABLE_NAME ${PROJECT_NAME}-x86 )
endif( CMAKE_SIZEOF_VOID_P EQUAL 8 )
# The executable file will be generate in the bin/ directory
set(EXECUTABLE_OUTPUT_PATH bin/${CMAKE_BUILD_TYPE})
# All .h files are in the include/ directory
include_directories(
../include/
)
# All .cpp files are in the src/ directory
file(
GLOB_RECURSE
SOURCE_FILES
../src/*
)
add_executable(
${EXECUTABLE_NAME}
${HEADER_FILES}
${SOURCE_FILES}
)
target_link_libraries(
${EXECUTABLE_NAME}
GL
GLEW
SDL2
)
and here the main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <SDL2/SDL.h>
#include <glm/glm.hpp>
using namespace glm;
using namespace std;
SDL_Window * CreateWindow(
const char * title,
int x,
int y,
int w,
int h,
Uint32 flags,
const char * icon
);
SDL_Renderer * CreateRenderer(
SDL_Window * window,
Uint32 flags,
int r,
int g,
int b
);
void Init(Uint32 flags);
int main(void)
{
Init(SDL_INIT_EVERYTHING);
SDL_Window * window = CreateWindow("Prism", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, /*SDL_WINDOW_FULLSCREEN_DESKTOP*/ SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL, "../../assets/icon.png");
SDL_Renderer * renderer = CreateRenderer(window, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC, 0, 0, 0);
bool end = false;
SDL_Event events;
while(!end) {
SDL_WaitEvent(&events);
if(events.window.event == SDL_WINDOWEVENT_CLOSE)
end = true;
SDL_RenderClear(renderer); // On nettoie l'écran
SDL_RenderPresent(renderer); // Et on effectue le rendu
}
SDL_DestroyWindow(window);
SDL_Quit();
return(EXIT_SUCCESS);
}
void Init(Uint32 flags){
if(SDL_Init(flags) < 0){
printf("Erreur lors de l'initialisation de la SDL : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
}
SDL_Window * CreateWindow(const char * title, int x, int y, int w, int h, Uint32 flags, const char * icon)
{
SDL_Window * window = SDL_CreateWindow(title, x, y, w, h, flags);
if(window == NULL){
printf("Erreur lors de la création de la fenêtre : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_Surface * icon_surface = IMG_Load(icon);
if(icon_surface == NULL){
printf("Erreur lors de la récupération de l'icone : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
SDL_SetWindowIcon(window, icon_surface);
SDL_FreeSurface(icon_surface);
SDL_ShowCursor(SDL_DISABLE);
return window;
}
SDL_Renderer * CreateRenderer(SDL_Window * window, Uint32 flags, int r, int g, int b)
{
SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, flags);
if(renderer == NULL){
printf("Erreur lors de la création du renderer : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
if(SDL_SetRenderDrawColor(renderer, r, g, b, 255) < 0){
printf("Erreur lors de la modification de la couleur de fond du renderer : %s\n", SDL_GetError());
exit(EXIT_FAILURE);
}
return renderer;
}
I tried to add SDL_image.h in the include directory but I get an error because the compiler isn't able to find the SDL.h specified in the SDL_image.h...
Thank you for your helps !
Hmm... not sure, but maybe try this?
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
Include both headers, in that order.
Aside from include <SDL2/SDL_image.h> as suggested by Justin Time, you would also have to supply the library (SDL2_image) to link against your executable in your cmake file, like so;
target_link_libraries(
${EXECUTABLE_NAME}
GL
GLEW
SDL2
SDL2_image
)
I just tested your code and it runs fine with above modification. Make sure your "../../assets/icon.png" is accessible to your executable. Hope that helps.
Ok
So I'm an idiot because I didn't install libsdl2-image-2.0-0... I looked at what you sent me Gestrong and I changed my CMakeList.txt to the following :
cmake_minimum_required (VERSION 2.6)
SET(PROJECT_NAME "Engine")
PROJECT(${PROJECT_NAME})
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c++0x")
INCLUDE(FindPkgConfig)
INCLUDE(FindOpenGL REQUIRED)
INCLUDE(FindGLEW REQUIRED)
INCLUDE(FindSDL REQUIRED)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image)
INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR})
INCLUDE_DIRECTORIES(${GLEW_INCLUDE_DIR})
SET(EXECUTABLE_OUTPUT_PATH bin/)
INCLUDE_DIRECTORIES(../include/)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS})
INCLUDE_DIRECTORIES(${SDL2IMAGE_INCLUDE_DIRS})
FILE(
GLOB_RECURSE
SOURCE_FILES
../src/*
)
ADD_EXECUTABLE(
${PROJECT_NAME}
${SOURCE_FILES}
)
TARGET_LINK_LIBRARIES(
${PROJECT_NAME}
${SDL2_LIBRARIES}
${SDL2IMAGE_LIBRARIES}
${OPENGL_LIBRARIES}
${GLEW_LIBRARIES}
)
I added #include <SDL2/SDL_image.h> in my code and now it WORKS :) thank you !
Related
I'm trying to run the executable of my application made with CLion and I get the error 0xc000007b (STATUS_INVALID_IMAGE_FORMAT).
If I launch the program from CLion the application start normally without any error, the problem occurs only if I try to launch the EXE.
I use SDL2, SDL2_image and SDL2_ttf. I always had this problem with SDL.
Project structure:
./cmake-build-debug
/SDL2.dll, SDL2_image.dll, SDL2_ttf.dll
/myProgram.exe
./include
/SDL2/
/*.a
./lib
/SDL2/
/*.h
./src
/main.cpp
main.cpp:
#include <iostream>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow(
"GameEngine",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
0
);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
bool should_quit = false;
while (!should_quit) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
should_quit = true;
break;
}
break;
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
CMakeList.txt:
cmake_minimum_required(VERSION 3.21)
project(GameEngine)
set(CMAKE_CXX_STANDARD 20)
set(SDL2_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/include/SDL2)
set(SDL2_LIB_DIR ${CMAKE_SOURCE_DIR}/lib/SDL2)
include_directories(${SDL2_INCLUDE_DIR})
link_directories(${SDL2_LIB_DIR})
file(GLOB_RECURSE SOURCES
${PROJECT_SOURCE_DIR}/src/*.cpp
)
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} mingw32 SDL2main SDL2 SDL2_image SDL2_ttf)
I want to add SDL2 to a Cmake Project, using C++ in VSCode on Windows, but using Mingw64 from Msys2 (g++).
This is my current CMakeLists.txt:
cmake_minimum_required(VERSION 3.5)
project(TileGameStudio_Runtime LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(PROJ_SRC
${PROJECT_SOURCE_DIR}/src
)
add_subdirectory(${PROJECT_SOURCE_DIR}/SDL2)
include_directories(${PROJECT_SOURCE_DIR}/SDL2/include)
file(GLOB PROJECT_SOURCES CONFIGURE_DEPENDS
${PROJ_SRC}/*.cpp
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}\\Game")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}\\Game")
# add the executable
add_executable(TileGameStudio_Runtime
${PROJECT_SOURCES}
)
target_link_libraries(TileGameStudio_Runtime PRIVATE
SDL2-static
SDL2main
)
set_target_properties(
TileGameStudio_Runtime
PROPERTIES
OUTPUT_NAME "Game"
SUFFIX ".exe"
)
This is my current Project Structure (img)
As you can see. i cloned the Repo from https://github.com/libsdl-org/SDL as a subdirectory to my Project. And this, i added via cmake add_subdirectory.
My Main.cpp is simply this:
#include <iostream>
#include <vector>
#include <string>
#include <SDL.h>
#ifdef main
# undef main
#endif /* main */
using namespace std;
int main(int argc, char *argv[]) {
SDL_Window *win = NULL;
SDL_Renderer *renderer = NULL;
int posX = 100, posY = 100, width = 320, height = 240;
SDL_Init(SDL_INIT_VIDEO);
win = SDL_CreateWindow("Hello World", posX, posY, width, height, 0);
renderer = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
//================================================ Draw a Text
//this opens a font style and sets a size
TTF_Font* Sans = TTF_OpenFont("Sans.ttf", 24);
// this is the color in rgb format,
// maxing out all would give you the color white,
// and it will be your text's color
SDL_Color White = {255, 255, 255};
// as TTF_RenderText_Solid could only be used on
// SDL_Surface then you have to create the surface first
SDL_Surface* surfaceMessage = TTF_RenderText_Solid(Sans, "put your text here", White);
SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
SDL_Rect Message_rect; //create a rect
Message_rect.x = 0; //controls the rect's x coordinate
Message_rect.y = 0; // controls the rect's y coordinte
Message_rect.w = 100; // controls the width of the rect
Message_rect.h = 100; // controls the height of the rect
SDL_RenderCopy(renderer, Message, NULL, &Message_rect);
//================================================ Draw a Text End
while (1) {
SDL_Event e;
if (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
break;
}
}
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
Which should create a Window.. and perhaps draw a text to it.
But, SDL.h can´t be found... so i can´t build the Stuff here..
I updated my CMakeLists.txt, cause i reinstalled the whole msys2 (since of a installation problem).
This is the new One:
cmake_minimum_required(VERSION 3.5)
project(TileGameStudio_Runtime LANGUAGES CXX)
set(CMAKE_CXX_FLAGS -v)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(PROJ_SRC
${PROJECT_SOURCE_DIR}/src
)
file(GLOB PROJECT_SOURCES CONFIGURE_DEPENDS
${PROJ_SRC}/*.cpp
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_SOURCE_DIR}\\Game")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}\\Game")
INCLUDE(FindPkgConfig)
pkg_check_modules(SDL2 REQUIRED sdl2)
pkg_check_modules(SDL2_IMAGE REQUIRED SDL2_image)
pkg_check_modules(SDL2_TTF REQUIRED SDL2_ttf)
pkg_check_modules(SDL2_MIXER REQUIRED SDL2_mixer)
include_directories(
${SDL2_INCLUDE_DIRS}
${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_TTF_INCLUDE_DIRS}
${SDL2_MIXER_INCLUDE_DIRS}
)
link_directories (
${SDL2_LIBRARY_DIRS}
${SDL2_IMAGE_LIBRARY_DIRS}
${SDL2_TTF_LIBRARY_DIRS}
${SDL2_MIXER_LIBRARY_DIRS}
)
# add the executable
add_executable(TileGameStudio_Runtime
${PROJECT_SOURCES}
)
target_link_libraries (TileGameStudio_Runtime
${SDL2_LIBRARIES}
${SDL2_IMAGE_LIBRARIES}
${SDL2_TTF_LIBRARIES}
${SDL2_MIXER_LIBRARIES}
)
set_target_properties(
TileGameStudio_Runtime
PROPERTIES
OUTPUT_NAME "Game"
SUFFIX ".exe"
)
I installed the whole mingw64 toolchain and SDL2 via msys (under MingW64 Mode of msys2) now.
it still couldn´t find the SDL.h or SDL2/SDL.h
I checked the SDL2_Dir in the Cmake GUI.. and it has the correct Path to: "C:/msys64/mingw64/lib/cmake/SDL2"
EDIT:
I now changed the Main Folder of all my Projects (unreal, unity..etc) from "Game Design" to "Game_Design".. Working like a Charm now... Cause.. Spaces are evil.. very evil...
Thanks for your Help Trys :D <3
I'm trying to make a game on vscode with CMake. Everything was fine until I try to show a png image on the screen. I had initialized and linked everything correctly and there are no errors that occur except this:
"Couldn't open ./Images/Mountain.png"
Here is my code:
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#undef main
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *tex;
int main(int argc, char const *argv[])
{
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
std::cout << "Initialize SDL" << std::endl;
window = SDL_CreateWindow("SDL2", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 640, SDL_WINDOW_SHOWN);
if (window)
std::cout << "Window created" << std::endl;
renderer = SDL_CreateRenderer(window, -1, 0);
if (renderer)
{
std::cout << "Render created" << std::endl;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
}
SDL_Surface *tempSurface = IMG_Load("./Images/Mountain.png");
if (!tempSurface)
std::cout << "Can't load image! Error: " << IMG_GetError() << std::endl;
tex = SDL_CreateTextureFromSurface(renderer, tempSurface);
SDL_FreeSurface(tempSurface);
}
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
SDL_Quit();
return 0;
}
And here is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.19)
project(SDL2 VERSION 1.0.0)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(SDL2_PATH "E:\\Code\\SDL2")
set(SDL2_IMAGE_PATH "E:\\Code\\SDL2")
set(SDL2_TTF_PATH "E:\\Code\\SDL2")
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
find_package(SDL2_ttf REQUIRED)
include_directories(
${SDL2_INCLUDE_DIR}
${SDL2_IMAGE_INCLUDE_DIRS}
${SDL2_TTF_INCLUDE_DIR})
set(SOURCES
src/main.cpp
)
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(
${PROJECT_NAME}
${SDL2_LIBRARY}
${SDL2_IMAGE_LIBRARIES}
${SDL2_TTF_LIBRARIES}
)
I use Visual Studio Build Tools 2019 Release - x86 kit for CMake and build it successfully with CMake extension. The error only appears when I run the program although my path is correct.
I have been trying to figure out why CMake is not adding the static libraries of SDL2. If I add the SDL2.dll file next to the executable, then it does work.
It al builds fine and the headers can be found nice and easily.
The error
My CMakeList.txt looks like this. Trust me, I have tried everything but clearly I am doing something wrong.
cmake_minimum_required(VERSION 3.7)
set (CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set (CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
SET(SDL2_DIR ./lib)
project(SDL2Test)
find_package(SDL2 REQUIRED)
include_directories(SDL2Test ${SDL2_INCLUDE_DIRS})
add_executable(SDL2Test Main.cpp)
target_link_libraries(SDL2Test ${SDL2_LIBRARIES})
This is the sdl2-config.cmake:
set(SDL2_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/../include")
# Support both 32 and 64 bit builds
if (${CMAKE_SIZEOF_VOID_P} MATCHES 8)
set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/x64/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/x64/SDL2main.lib")
else ()
set(SDL2_LIBRARIES "${CMAKE_CURRENT_LIST_DIR}/x86/SDL2.lib;${CMAKE_CURRENT_LIST_DIR}/x86/SDL2main.lib")
endif ()
string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
This is the Main.cpp
#include "SDL.h"
using namespace std;
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow(
"SDL2Test",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
0
);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Your SDL2.lib is import library, that requires accompanying DLL in the runtime.
To use static library (one that is completely linked with your EXE and does not require DLL), you will need to build static version of SDL2.lib yourself.
Keep in mind that this is discouraged by the author of SDL
While trying to load a .png file with IMG_LoadTexture(renderer, "idle.png")
SDL_GetError() says: "Couldn't open idle.png"
There are no compiler errors, just a black window appears.
This is my main.cpp
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
int main(int argc, char** argv) {
SDL_Event event;
SDL_Renderer *renderer = NULL;
SDL_Texture *texture = NULL;
SDL_Window *window = NULL;
SDL_Init(SDL_INIT_TIMER | SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(
800, 600,
0, &window, &renderer
);
IMG_Init(IMG_INIT_PNG);
texture = IMG_LoadTexture(renderer, "idle.png");
std::cout << SDL_GetError();
while (1) {
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
if (SDL_PollEvent(&event) && event.type == SDL_QUIT)
break;
}
SDL_DestroyTexture(texture);
IMG_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return EXIT_SUCCESS;
}
But I guess the problem is the way I link the library. I installed sdl2, sdl2_image and libpng.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.12)
project(untitled)
set(CMAKE_CXX_STANDARD 17)
add_executable(untitled main.cpp)
INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(untitled ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES})
You are loading the image from the current working directory (CWD) of your application. That is not necessarily the same directory as your executable is in (it depends on how it is launched), which you seem to assume.
3 easy ways to fix:
change the cwd at runtime to where the file is and load as you do now.
provide an absolute path to the file when loading, so cwd is irrelevant.
obtain the path to the executable at runtime and then construct a path to the file relative to where the executable is. (best option in my opinion since it's robust against moving your project around/installing to a different location).