Thursday, September 19, 2019

Clion + SDL2 (MSYS2)

cmake_minimum_required(VERSION 3.15)
project(untitled5)

set(CMAKE_CXX_STANDARD 17)
set(EXECUTABLE_NAME "game")

set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -mwindows")

file(GLOB source ${CMAKE_SOURCE_DIR}/src/*.cpp)

add_executable(${EXECUTABLE_NAME} ${source})

target_link_libraries(${EXECUTABLE_NAME}
        lua
        libz
        tinyxml
        libsfml-audio
        libsfml-graphics
        libsfml-network
        libsfml-system
        libsfml-window
        mingw32
        SDL2main
        SDL2
        SDL2_image
        SDL2_ttf
        SDL2_mixer
        Qt5Core
        Qt5Gui
        Qt5Widgets)


=====================================================================
#include <SDL2/SDL.h>

SDL_Window *g_pWindow = nullptr;
SDL_Renderer *g_pRenderer = nullptr;


int main(int argc, char *argv[]) {
    // initialize SDL
    if (SDL_Init(SDL_INIT_EVERYTHING) >= 0) {
        // create window
        g_pWindow = SDL_CreateWindow("Game",
                                     SDL_WINDOWPOS_CENTERED,
                                     SDL_WINDOWPOS_CENTERED,
                                     640,
                                     480,
                                     SDL_WINDOW_SHOWN);
        if (g_pWindow != nullptr) {
            // create renderer
            g_pRenderer = SDL_CreateRenderer(g_pWindow, -1, 0);
        } else {
            // sdl not initialized
            return 1;
        }
    }
    // sdl is initialized
    // set to black
    SDL_SetRenderDrawColor(g_pRenderer, 0, 0, 0, 255);
    // clear window to black
    SDL_RenderClear(g_pRenderer);
    // show window
    SDL_RenderPresent(g_pRenderer);
    // set a delay
    SDL_Delay(5000);
    //
    // clean up SDL an quit
    SDL_DestroyRenderer(g_pRenderer);
    SDL_DestroyWindow(g_pWindow);
    SDL_Quit();

    return 0;
}