Search code examples
c++cmakelinkerpngsdl

I'm trying to SDL_DisplayFormat a .png file, I think, I may be missing a dependency


This is the function that gives me a segmentation fault dump, it's the line:

optimizedImage = SDL_DisplayFormat( loadedImage );

Trying to build this on ubuntu Linux using codeblocks for my IDE.

I'm very close and want to use SDL mixers capability and png would be nice to have fully set up as well.

Just discovered the function may be deprecated. SDL_ConvertSurfaceFormat(); seems to be the replacement and is part of sdl2.. But i get an undefined error when I try it.. sdl and sdl2 appear to be linked properly.

SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    cout<< "entered load image files"<< endl;
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );
cout<< "image image should be set"<< endl;
    //If the image loaded
    if( loadedImage != NULL )
    {
    cout<< "loaded image isn't null, hooray!"<< endl;
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );
cout<< "optimized image should be in display format now"<< endl;
        //Free the old surface
        SDL_FreeSurface( loadedImage );
cout<< "sdl surface is free"<< endl;
        //If the surface was optimized
        if( optimizedImage != NULL )
        {cout<< "optimized image is not null"<< endl;
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
            cout<< "sdl setcolor key is ok"<< endl;
        }
    }

    //Return the optimized surface
    return optimizedImage;
}

Heres my cmakelists.text file


# CMake entry point
cmake_minimum_required (VERSION 3.0)


project (maficengine LANGUAGES C CXX ASM)




set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/external/")


find_package(PNG REQUIRED)
find_package(ZLIB)
find_package(OpenGL REQUIRED)
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)

add_definitions(${PNG_DEFINITIONS})


include_directories(${SDL2_INCLUDE_DIRS}
 ${SDL2_IMAGE_INCLUDE_DIRS})

include_directories(${PNG_INCLUDE_DIR})

find_path(PNG_INCLUDE_DIR png.h)

find_file(SDL2_INCLUDE_DIR NAME SDL.h HINTS SDL2)
find_library(SDL2_LIBRARY NAME SDL2)

set(SDL2_INCLUDE_DIR /usr/include/SDL2)
set(SDL2_LIBRARY /usr/lib/x86_64-linux-gnu/libSDL2.so)
file(GLOB_RECURSE SOURCE_FILES/usr/lib/x86_64-linux-gnu
    ${CMAKE_SOURCE_DIR}/src/*.c
    ${CMAKE_SOURCE_DIR}/src/*.cpp)

INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL REQUIRED sdl)

set(SDL_INCLUDE_DIR "/usr/include/SDL2")
set(SDL_LIBRARY "SDL2")
include(FindSDL)

if(SDL_FOUND)
  message(STATUS "SDL FOUND")
  elseif(!SDL_FOUND)
     message(STATUS "SDL not FOUND")
endif()



find_library(SDL_MIXER_LIBRARY
  NAMES SDL2_mixer
  HINTS
    ENV SDLMIXERDIR
    ENV SDLDIR
  PATH_SUFFIXES lib
)

if( CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR )
    message( FATAL_ERROR "Please select another Build Directory ! (and give it a clever name, like bin_Visual2012_64bits/)" )
endif()
if( CMAKE_SOURCE_DIR MATCHES " " )
    message( "Your Source Directory contains spaces. If you experience problems when compiling, this can be the cause." )
endif()
if( CMAKE_BINARY_DIR MATCHES " " )
    message( "Your Build Directory contains spaces. If you experience problems when compiling, this can be the cause." )
endif()

file(GLOB SOURCES "external/myCustomHeaders/*.cpp")






add_library(loaders SHARED ${SOURCES}
${SDL2_INCLUDE_DIR}
external/SDL2_image-2.0.4/SDL_image.h
external/myCustomHeaders/include/loadTexture.h
external/myCustomHeaders/loadTexture.cpp
/usr/include/SDL2/SDL.h
/usr/include/SDL/SDL_image.h
external/SDL2_mixer-2.0.4/SDL_mixer.h
external/zlib-1.2.11/zlib.h
external/libpng-1.6.37/png.h
)
find_package(ZLIB)
if (ZLIB_FOUND)
     include_directories(external/zlib-1.2.11)
    #target_link_libraries(loaders ${ZLIB_LIBRARIES})
 endif()

if (!PNG_FOUND)
     message(STATUS "PNG not FOUND Don't use it")
 endif()

if(!ZLIB_FOUND)
     message(STATUS "ZLIB not FOUND")
endif()


include_directories(ZLIB external/zlib-1.2.11)
include_directories(PNG_INCLUDE_DIRS external/libpng-1.6.37)
include_directories( external/myCustomHeaders/include )
include_directories( external/SDL2_mixer-2.0.4/acinclude )
include_directories( ${CMAKE_CURRENT_SOURCE_DIR}external/SDL2_mixer-2.0.4 )
include_directories(/usr/include/SDL2)
include_directories(/usr/include/SDL)
include_directories(${SDL2_INCLUDE_DIRS})
include_directories(/usr/include)

include(FindPackageHandleStandardArgs)


find_path(
  SDL_MIXER_INCLUDE_DIR
  PATHS
  /usr/include/SDL
  /usr/include/SDL2
  /usr/include
  /usr/local/include
  /sw/include
  /opt/local/include
  ${SDL_MIXER_ROOT_DIR}/include
  DOC "The directory where SDL_mixer.h resides")


link_directories(external/myCustomHeaders/include)
link_directories(external/myCustomHeaders)
link_directories(external/SDL2_mixer-2.0.4)
link_directories(/usr/include/SDL2)
link_directories(/usr/include/SDL)
link_directories(/usr/include)
link_directories(external/libpng-1.6.37)
link_directories(ZLIB external/zlib-1.2.11)
add_subdirectory (external)


# On Visual 2005 and above, this module can set the debug working directory
cmake_policy(SET CMP0026 OLD)
#cmake_policy(SET CMP0079  NEW)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/rpavlik-cmake-modules-fe2273")
include(CreateLaunchers)
include(MSVCMultipleProcessCompile) # /MP

if(INCLUDE_DISTRIB)
    add_subdirectory(distrib)
endif(INCLUDE_DISTRIB)



include_directories(
    external/AntTweakBar-1.16/include/
    external/glfw-3.1.2/include/
    external/glm-0.9.7.1/
    external/glew-1.13.0/include/
    external/assimp-3.0.1270/include/
    external/bullet-2.81-rev2613/src/
    external/myCustomHeaders/include
    common/
)

set(ALL_LIBS
    ${OPENGL_LIBRARY}
    glfw
    GLEW_1130
    loaders

)



add_definitions(
    -DTW_STATIC
    -DTW_NO_LIB_PRAGMA
    -DTW_NO_DIRECT3D
    -DGLEW_STATIC
    -D_CRT_SECURE_NO_WARNINGS
)

# Tutorial 17
add_executable(mageengine
${SDL2_LIBRARY}
    Mafic/mafic.cpp
    common/shader.cpp
    common/shader.hpp
    common/controls.cpp
    common/controls.hpp
    common/texture.cpp
    common/texture.hpp
    common/objloader.cpp
    common/objloader.hpp
    common/vboindexer.cpp
    common/vboindexer.hpp
    common/quaternion_utils.cpp
    common/quaternion_utils.hpp
    Mafic/StandardShading.vertexshader
    Mafic/StandardShading.fragmentshader

)
target_link_libraries(loaders
                       -lSDLmain)
target_link_libraries(loaders
                       -lSDL)
        target_link_libraries(loaders
                       -lSDL2_ttf)
target_link_libraries(loaders ${ZLIB_LIBRARIES})
target_link_libraries(loaders
                       -lSDL2_mixer)
target_link_libraries(loaders -zlib)
target_link_libraries(loaders ${PNG_LIBRARIES})


set_target_properties(loaders
   PROPERTIES LINKER_LANGUAGE CXX)
target_link_libraries(loaders ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})


target_link_libraries(mageengine
    ${ALL_LIBS}
    ANTTWEAKBAR_116_OGLCORE_GLFW
    ${SDL2_LIBRARIES}

   loaders
)

set_target_properties(mageengine PROPERTIES XCODE_ATTRIBUTE_CONFIGURATION_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Mafic/")
create_target_launcher(mageengine WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/Mafic/")

SOURCE_GROUP(common REGULAR_EXPRESSION ".*/common/.*" )
SOURCE_GROUP(shaders REGULAR_EXPRESSION ".*/.*shader$" )


if (NOT ${CMAKE_GENERATOR} MATCHES "Xcode" )

add_custom_command(
   TARGET mageengine POST_BUILD
   COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/mageengine${CMAKE_EXECUTABLE_SUFFIX}" "${CMAKE_CURRENT_SOURCE_DIR}/Mafic/"
)
  #target_link_libraries(mageengine LINK_PUBLIC ${mylibrary}
  # )

elseif (${CMAKE_GENERATOR} MATCHES "Xcode" )

endif (NOT ${CMAKE_GENERATOR} MATCHES "Xcode" )


i expect my program to not crash so I can further implement and debug code.

here's the output, repleat with cute but useful messages.

 aaron@Zog:~/Desktop/maficengine/Mafic$ ./mageengine 
LoadTexture object got constructed in mageengine from loadtexture.cpp
Reading image ./paint.bmp
Compiling shader : StandardShading.vertexshader
Compiling shader : StandardShading.fragmentshader
Linking program
Loading OBJ file plane.obj...
loadtexture() function works, inboundtexture is 1
ERROR(AntTweakBar) >> Parsing error in def string: Unknown attribute [true            ...]
Compiling shader : StandardShading.vertexshader
Compiling shader : StandardShading.fragmentshader
Linking program
Loading OBJ file plane.obj...
passed sdl init everything
passed sdl init
entered load files
likely opened file background.png
entered load image files
image image should be set
loaded image isn't null, hooray!
Segmentation fault (core dumped)

the updated cmakelists.text after cleaning as advised in the comments below




# CMake entry point
cmake_minimum_required (VERSION 3.0)


project (maficengine LANGUAGES C CXX ASM)




set(CMAKE_MODULE_PATH "/home/aaron/Desktop/maficengine/cmake/")
find_package(PNG REQUIRED)
find_package(ZLIB  REQUIRED)
find_package(OpenGL REQUIRED)
find_package(SDL2 REQUIRED)
find_package(SDL_image REQUIRED)#  maybe sdl2_image
find_package(SDL_mixer REQUIRED)
#set(CMAKE_MODULE_PATH "/home/aaron/Desktop/maficengine/cmake/")

#et(SDL2_mixer_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
add_definitions(${PNG_DEFINITIONS})


include_directories(
  ${PNG_INCLUDE_DIRS}
  ${SDL2_INCLUDE_DIRS}
  ${SDL_MIXER_INCLUDE_DIR}
  ${SDL2_TTF_INCLUDE_DIRS}
  ${SDL_IMAGE_INCLUDE_DIRS}
  ${SDL2_IMAGE_INCLUDE_DIR}
)


file(GLOB_RECURSE SOURCE_FILES/usr/lib/x86_64-linux-gnu
    ${CMAKE_SOURCE_DIR}/src/*.c
    ${CMAKE_SOURCE_DIR}/src/*.cpp)

#include(FindSDL)


if( CMAKE_BINARY_DIR STREQUAL CMAKE_SOURCE_DIR )
    message( FATAL_ERROR "Please select another Build Directory ! (and give it a clever name, like bin_Visual2012_64bits/)" )
endif()
if( CMAKE_SOURCE_DIR MATCHES " " )
    message( "Your Source Directory contains spaces. If you experience problems when compiling, this can be the cause." )
endif()
if( CMAKE_BINARY_DIR MATCHES " " )
    message( "Your Build Directory contains spaces. If you experience problems when compiling, this can be the cause." )
endif()



#add_library(loaders

#external/myCustomHeaders/include/loadTexture.h
#external/myCustomHeaders/loadTexture.cpp

#)



if (ZLIB_FOUND)
     include_directories(external/zlib-1.2.11)
     message(STATUS "ZLIB FOUND")
  elseif (!ZLIB_FOUND)
     message(STATUS "Zlib not FOUND!")
endif()

if (PNG_FOUND)
     message(STATUS "PNG FOUND ")
   elseif (!PNG_FOUND)
     message(STATUS "PNG not FOUND! ")
endif()

#if(SDL_FOUND)
 #    message(STATUS "sdl LIB  FOUND!")
   #elseif(!SDL_FOUND)
  #   message(STATUS "sdl LIB not FOUND!")
#endif()

if(SDL2_FOUND)
     message(STATUS "sdl2  FOUND")
   elseif(!SDL2_FOUND)
     message(STATUS "sdl2  not FOUND!")
endif()

if(SDL_MIXER_FOUND)
     message(STATUS "sdl mixer  FOUND")
   elseif(!SDL_MIXER_FOUND)
     message(STATUS "sdl mixer not FOUND!")
endif()

if(SDL_IMAGE_FOUND)
     message(STATUS "sdl image  FOUND")
   elseif(!SDL_IMAGE_FOUND)
     message(STATUS "sdl image not FOUND!")
endif()



include_directories( external/myCustomHeaders/include )

include_directories(/usr/include)

include(FindPackageHandleStandardArgs)





#link_directories(external/myCustomHeaders/include)
#link_directories(external/myCustomHeaders)
link_directories(external/SDL2_mixer-2.0.4)
link_directories(/usr/include/SDL2)
link_directories(/usr/include/SDL)
link_directories(/usr/include)
link_directories(external/libpng-1.6.37)
link_directories(ZLIB external/zlib-1.2.11)
add_subdirectory (external)


# On Visual 2005 and above, this module can set the debug working directory
cmake_policy(SET CMP0026 OLD)


list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/external/rpavlik-cmake-modules-fe2273")
include(CreateLaunchers)
include(MSVCMultipleProcessCompile) # /MP

if(INCLUDE_DISTRIB)
    add_subdirectory(distrib)
endif(INCLUDE_DISTRIB)



include_directories(
    external/AntTweakBar-1.16/include/
    external/glfw-3.1.2/include/
    external/glm-0.9.7.1/
    external/glew-1.13.0/include/
    external/assimp-3.0.1270/include/
    external/bullet-2.81-rev2613/src/
    external/myCustomHeaders/include
    common/
)

set(ALL_LIBS
    ${OPENGL_LIBRARY}
    glfw
    GLEW_1130
    #loaders
 ${SDL2_LIBRARIES}
 ${SDL2_IMAGE_LIBRARIES}
 ${SDL2_MIXER_LIBRARIES}
 ${SDL2_TTF_LIBRARIES}
)



add_definitions(
    -DTW_STATIC
    -DTW_NO_LIB_PRAGMA
    -DTW_NO_DIRECT3D
    -DGLEW_STATIC
    -D_CRT_SECURE_NO_WARNINGS
)

# Tutorial 17
add_executable(mageengine
#${SDL2_LIBRARY}
    Mafic/mafic.cpp
    common/shader.cpp
    common/shader.hpp
    common/controls.cpp
    common/controls.hpp
    common/texture.cpp
    common/texture.hpp
    common/objloader.cpp
    common/objloader.hpp
    common/vboindexer.cpp
    common/vboindexer.hpp
    common/quaternion_utils.cpp
    common/quaternion_utils.hpp
    Mafic/StandardShading.vertexshader
    Mafic/StandardShading.fragmentshader

)
target_link_libraries(mageengine
                       -lSDLmain)
target_link_libraries(mageengine
                      -lSDL)
    #    target_link_libraries(loaders
 #                      -lSDL2_ttf)
#target_link_libraries(loaders ${ZLIB_LIBRARIES})
target_link_libraries(mageengine
                      -lSDL2_mixer)
#(loaders -zlib)
#target_link_libraries(loaders ${PNG_LIBRARIES})


set_target_properties(mageengine
   PROPERTIES LINKER_LANGUAGE CXX)
#target_link_libraries(loaders ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})


target_link_libraries(mageengine
    ${ALL_LIBS}
    ANTTWEAKBAR_116_OGLCORE_GLFW

)

set_target_properties(mageengine PROPERTIES XCODE_ATTRIBUTE_CONFIGURATION_BUILD_DIR "${CMAKE_CURRENT_SOURCE_DIR}/Mafic/")
create_target_launcher(mageengine WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/Mafic/")

SOURCE_GROUP(common REGULAR_EXPRESSION ".*/common/.*" )
SOURCE_GROUP(shaders REGULAR_EXPRESSION ".*/.*shader$" )


if (NOT ${CMAKE_GENERATOR} MATCHES "Xcode" )

add_custom_command(
   TARGET mageengine POST_BUILD
   COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_CFG_INTDIR}/mageengine${CMAKE_EXECUTABLE_SUFFIX}" "${CMAKE_CURRENT_SOURCE_DIR}/Mafic/"
)


elseif (${CMAKE_GENERATOR} MATCHES "Xcode" )

endif (NOT ${CMAKE_GENERATOR} MATCHES "Xcode" )


Solution

  • Finally...tried SDL sound libraries for my game-engine again .... and found a way through!!!, good sound ...clear as a bell now, but i still think I will have to add sdl_mixer libraries to be satisfied. But this time I think I can do it. I found an old blog that sent me in the right direction. i think I installed SDL then upgraded to dev libraries, but it's the wrong order, then I couldn't purge the old library properly, but dug deeper and added tools to trace and destroy and finesse files and symbols till.. magic happened.. totally incredibly hard to do. Here's the blog that sent me the right way->

    "If you built your own SDL, you probably didn't have development headers for PulseAudio (or ALSA), so it's trying to use /dev/dsp, which doesn't exist on many modern Linux systems (hence, SDL_Init(SDL_INIT_AUDIO) succeeds, but no devices are found when you try to open one). "apt-get install libasound2-dev libpulse-dev" and rebuild SDL...let the configure script find the new headers so it includes PulseAudio and ALSA support. "