开发者

Cant execute a simple "SDL2HelloWorld" on Windows with CMake

I don't understand why when I run the program nothing happens (no windows or cout):

.\src\main.cpp :

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

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480

int main(int argc, char* args[]) {
  std::cout << "Le programme se lance"; // should at least show
  SDL_Window* window = NULL;
  SDL_Surface* screenSurface = NULL;
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    std::cout << "could not initialize sdl2 :" << SDL_GetError() << std::endl;
    return 1;
  }
  window = SDL_CreateWindow(
                "hello_sdl2",
                SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                SCREEN_WIDTH, SCREEN_HEIGHT,
                SDL_WINDOW_SHOWN
                );
  if (window == NULL) {
    std::cout << "could not cre开发者_Python百科ate window:" << SDL_GetError() << std::endl;
    return 1;
  }
  screenSurface = SDL_GetWindowSurface(window);
  SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
  SDL_UpdateWindowSurface(window);
  SDL_Delay(10000);
  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}

.\CMakeLists.txt :

cmake_minimum_required(VERSION 3.25.1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
project("Test")
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}")

set(SDL2_DIR "${SRC_DIR}/lib/cmake/SDL2")

find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})

add_executable(MAIN "${SRC_DIR}/src/main.cpp")
target_link_libraries(MAIN ${SDL2_LIBRARIES})

The architecture of my project is the following :

Cant execute a simple "SDL2HelloWorld" on Windows with CMake

All files are from the assets SDL2-devel-2.26.1-mingw.zip

The commands I used in order :

cd .\build\
cmake .. -G "MinGW Makefiles"
make
.\MAIN.exe

make and .\Main.exe results :

Cant execute a simple "SDL2HelloWorld" on Windows with CMake


In fact I just had to copy SDL2.dll in the build folder.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜