ℹ️

SDL avec CMake

On pourrait utiliser le fichier FindSDL2.cmake pour configurer la compilation

FindSDL2.cmake6.1KB
find_package(SDL2 REQUIRED PATHS "${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/x86_64-w64-mingw32/lib/cmake/SDL2")

Sinon à la main pour le CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(X_DSL C)
set(CMAKE_C_STANDARD 11)
set(SDL2_PATH "${PROJECT_SOURCE_DIR}/x86_64-w64-mingw32")

SET(SDL2_SEARCH_PATHS
        ${SDL2_PATH}
        )

FIND_PATH(SDL2_INCLUDE_DIR
        PATHS ${SDL2_SEARCH_PATHS}
        )

if(CMAKE_SIZEOF_VOID_P EQUAL 8)
    set(PATH_SUFFIXES lib64 lib/x64 lib)
else()
    set(PATH_SUFFIXES lib/x86 lib)
endif()

FIND_LIBRARY(SDL2_LIBRARY_TEMP
        NAMES SDL2
        HINTS
        $ENV{SDL2DIR}
        PATH_SUFFIXES ${PATH_SUFFIXES}
        PATHS ${SDL2_SEARCH_PATHS}
        )

FIND_LIBRARY(SDL2MAIN_LIBRARY
        NAMES SDL2main
        HINTS
        $ENV{SDL2DIR}
        PATH_SUFFIXES ${PATH_SUFFIXES}
        PATHS ${SDL2_SEARCH_PATHS}
        )


SET(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "mwindows for MinGW")
SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")


add_executable(X_SDL XSDL.c)
target_link_libraries(X_SDL ${SDL2_LIBRARY} )

Le fichier de test est le suivant :

//
// Created by remi on 24/05/2020.
//
#include <stdlib.h>
#include <stdio.h>
#include "x86_64-w64-mingw32\include\SDL2\SDL.h"

int main(int argc, char *argv[])
{


    /* Initialisation simple */
    if (SDL_Init(SDL_INIT_VIDEO) != 0 )
    {
        fprintf(stdout,"Échec de l'initialisation de la SDL (%s)\n",SDL_GetError());
        return -1;
    }

    {
        /* Création de la fenêtre */
        SDL_Window* pWindow = NULL;
        pWindow = SDL_CreateWindow("Ma première application SDL2",SDL_WINDOWPOS_UNDEFINED,
                                   SDL_WINDOWPOS_UNDEFINED,
                                   640,
                                   480,
                                   SDL_WINDOW_SHOWN);

        if( pWindow )
        {
            SDL_Delay(1000); /* Attendre trois secondes, que l'utilisateur voie la fenêtre */

            SDL_DestroyWindow(pWindow);
        }
        else
        {
            fprintf(stderr,"Erreur de création de la fenêtre: %s\n",SDL_GetError());
        }
    }

    SDL_Quit();

    return 0;
}