zoukankan      html  css  js  c++  java
  • 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),并运行,于是得到

  • 相关阅读:
    Codeforces 631D Messenger KMP
    Google Codejam 2016 Round1A Problem C BFFs 简单图论
    ACM常用数论知识 总结
    C++ 虚函数表
    HDU 5661 Claris and XOR 贪心
    2013亚洲区域赛长沙站 ZOJ 3732 Graph Reconstruction
    [C++] upper_bound和lower_bound
    2015合肥网络赛 HDU 5489 Removed Interval LIS+线段树(树状数组)
    hdu 3532 Max Angle(atan2的使用)
    poj 1106 Transmitters (计算几何,叉积||极角排序)
  • 原文地址:https://www.cnblogs.com/majiao61/p/15112473.html
Copyright © 2011-2022 走看看