SDL2笔记01_搭建开发环境和编写helloworld

  1. 开发环境为Clion,和SDL2.0

    • 去官网http://www.libsdl.org/download-2.0.php下载SDL2-devel-2.0.14-mingw.tar.gz

    • SDL2-devel-2.0.14-mingw.tar.gz解压到目录G:/Xubuntu_Work_Space/From_Xubuntu/codeTest_2019_2_21/SDL2/win/SDL2-devel-2.0.14-mingw/SDL2-2.0.14/x86_64-w64-mingw32

  2. 网上有大佬翻译了教程
    http://adolfans.github.io/sdltutorialcn/blog/2013/01/25/sdl-2-dot-0-tutorial-index/连接

  3. 在Clion新建c++工程

    • 在cmakelists.txt里添加如下代码
         cmake_minimum_required(VERSION 3.19)
         project(main)
         set(SDL2_DIR G:/Xubuntu_Work_Space/From_Xubuntu/codeTest_2019_2_21/SDL2/win/SDL2-devel-2.0.14-mingw/SDL2-2.0.14/x86_64-w64-mingw32)
      
         set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -lmingw32")
         set(CMAKE_EXE_LINKER_FLAGS "-static-libgcc -static-libstdc++")
      
         include_directories(${SDL2_DIR}/include)
         link_directories(${SDL2_DIR}/lib)
      
         set(SOURCE_FILES main.cpp)
         add_executable(main ${SOURCE_FILES})
      
         target_link_libraries(main mingw32 SDL2main SDL2)
      
      
  4. 在main.cpp编写helloworld,并且准备一张hello.bmp

         #include "SDL2/SDL.h"
    
         int main(int argc, char** argv) {
    
             SDL_Init(SDL_INIT_EVERYTHING);
    
             SDL_Window *sdlWindow = SDL_CreateWindow("你好", 0, 0, 400, 320, SDL_WINDOW_SHOWN);
             // SDL_Randerer渲染器要和window绑定
             SDL_Renderer *sdlRenderer = SDL_CreateRenderer(sdlWindow, -1, /*-1让系统自动选一个显卡驱动*/
                                                            SDL_RENDERER_ACCELERATED /*启用硬件加速*/
                                                            | SDL_RENDERER_PRESENTVSYNC /*使用显示器的刷新率*/);
             // SDL_Surface是表面,使用CPU
             SDL_Surface *sdlSurface = SDL_LoadBMP("C:\\Users\\majiao\\Desktop\\hello.bmp");
    
             // 用SDL_Texture来GPU加速
             SDL_Texture *sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, sdlSurface);
             SDL_FreeSurface(sdlSurface);
    
             SDL_RenderClear(sdlRenderer);
             SDL_RenderCopy(sdlRenderer, sdlTexture,
                            NULL, /*绘制整个原图像, 并把它画在屏幕上(0,0 )的位置*/
                            NULL  /*拉伸这个图像让它填满整个窗口*/);
             // 发送给GPU
             SDL_RenderPresent(sdlRenderer);
    
             SDL_Delay(60000);
    
             return 0;
         }
    
    
  5. 编译后把SDL2.dllmain.exe放到同级目录(windows会优先找同级目录的dll),并运行,于是得到

posted @ 2021-08-07 17:16  马角的逆袭  阅读(240)  评论(0编辑  收藏  举报