imgui-sfml on cmake clion projest - c++

I linked SFML and imgui, and I get no errors, but when I run I get errors like
undefined reference to `__imp__ZN2sf6StringC1EPKcRKSt6locale'
This is my cmake:
cmake_minimum_required(VERSION 3.22)
project(ImGuiT)
set(CMAKE_CXX_STANDARD 20)
add_executable(ImGuiT main.cpp
imgui/imgui_widgets.cpp
imgui/imgui.cpp
imgui/imgui-SFML.cpp
imgui/imgui_draw.cpp
imgui/imgui_tables.cpp
imgui/imconfig.h
imgui/imstb_rectpack.h
imgui/imstb_textedit.h
imgui/imstb_truetype.h
imgui/imconfig-SFML.h
imgui/imgui-SFML.h
imgui/imgui-SFML_export.h
imgui/imgui_internal.h
imgui/imgui.h)
include_directories(headers /usr/include c:/SFML/include imgui)
set(SFML_ROOT c:/SFML)
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules")
find_package(SFML REQUIRED system window graphics network audio)
if (SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(ImGuiT ${SFML_LIBRARIES} ${SFML_DEPENDENCIES})
endif()
file(COPY assets DESTINATION ${CMAKE_BINARY_DIR})
# Copy dlls to build
if(WIN32)
file(GLOB BINARY_DEP_DLLS "${SFML_INCLUDE_DIR}/../bin/*.dll")
file(COPY ${BINARY_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
file(GLOB MINGW_DEP_DLLS "C:/mingw64/bin/*.dll")
file(COPY ${MINGW_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
endif()
I use sfml that I linked from drive and imgui files are in folder in project.
I guess the imgui-sfml can't see sfml, but I linked that, so I don't know how to link it properly
I also added
#include "imconfig-SFML.h"
to the imconfig.h for imgui to see sfml extension
And this is just a simple test main that I can't run:
#include "imgui.h"
#include "imgui-SFML.h"
#include "SFML/Graphics.hpp"
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 800), "Window Title");
ImGui::SFML::Init(window);
bool circleExists = true;
float circleRadius = 200.0f;
int circleSegments = 100;
float circleColor[3] = { (float)204 / 255, (float)77 / 255, (float)5 / 255 };
sf::CircleShape shape(circleRadius, circleSegments);
shape.setFillColor(sf::Color
(
(int)(circleColor[0] * 255),
(int)(circleColor[1] * 255),
(int)(circleColor[2] * 255)
)); // Color circle
shape.setOrigin(circleRadius, circleRadius);
shape.setPosition(400, 400); // Center circle
sf::Clock deltaClock;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
ImGui::SFML::ProcessEvent(event);
if (event.type == sf::Event::Closed)
window.close();
}
ImGui::SFML::Update(window, deltaClock.restart());
ImGui::Begin("Window title");
ImGui::Text("Window text!");
ImGui::Checkbox("Circle", &circleExists);
ImGui::SliderFloat("Radius", &circleRadius, 100.0f, 300.0f);
ImGui::SliderInt("Sides", &circleSegments, 3, 150);
ImGui::ColorEdit3("Color Circle", circleColor);
ImGui::End();
shape.setRadius(circleRadius);
shape.setOrigin(circleRadius, circleRadius);
shape.setPointCount(circleSegments);
shape.setFillColor(sf::Color
(
(int)(circleColor[0] * 255),
(int)(circleColor[1] * 255),
(int)(circleColor[2] * 255)
)); // Color circle
window.clear(sf::Color(18, 33, 43)); // Color background
if (circleExists)
window.draw(shape);
ImGui::SFML::Render(window);
window.display();
}
ImGui::SFML::Shutdown();
return 0;
}

Related

SDL2 Clion executable unable to start correctly 0xc000007b

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)

Cant find SDL.h - Windows, MingW, Cmake and SLD2 (in VSCode with CPP)

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

IMG_Load() doesn't work properly when using with CMake

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.

Executable can't find SDL2.dll

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

SDL_image Can't load .png file with IMG_LoadTexture()

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).