前天我想找一个图形库写个小工具,想找个小巧一点的图形库,于是就找到了ege(easy graphics engine),花了点时间配置到了vscode上,顺便写了个文章,这是教程地址:https://blog.csdn.net/qq272508839/article/details/104287255

今天想起一个比较知名的图形库叫SDL,很早之前就听过,于是我就下载了一个打算试试,是在MinGW环境下的,具体配置MinGW的方法我就不说了,很简单。

先到官网:http://www.libsdl.org/下载两个东西,如下图:

对应自己的平台下载,我是win10 64位。

下载完是这俩东西:

509kb那个是用来编译完之后放到exe同级目录的,下面那个tar是需要自己安装的部分。

解压tar.gz,里面有这么几个文件夹:

标出的这俩文件夹是需要放到MinGW安装目录里的,不是俩都要复制,对应自己的MinGW版本复制一个就好,我是64位MinGW,我就要x86_64开头的那个了。

打开x86_64开头的文件夹,里面有几个文件夹(bin、include、lib、share):

把这几个文件夹全部选中,复制到MinGW安装目录里面\x86_64-w64-mingw32目录中,要是32位的MinGW呢就是MinGW安装目录里面\i686-w64-mingw32,复制完了之后会发现下图中的目录里多出一个SDL2,这就说明安装完成了:

然后就是配置vscode了,新建一个工程文件夹,在工程根目录新建.vscode目录(vscode前一个点别忘了),在.vscode目录里新建3个json文件:tasks.json、launch.json、c_cpp_properties.json,然后把509kb那个压缩包解压出一个文件叫SDL2.dll,把这个dll复制到exe同级目录,在根目录下建一个main.c,如下图:

然后就是复制代码粘贴到对应的文件里了,直接上代码。

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            //注意改成自己的MinGW目录
            "command": "C:\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-lmingw32",
                "-lSDL2main",
                "-lSDL2",
                "-mwindows"
            ],
            "options": {
                //注意改成自己的MinGW目录
                "cwd": "C:\\mingw64\\bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            //注意改成自己的MinGW目录
            "miDebuggerPath": "C:\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "g++.exe build active file"
        }
    ]
}

c_cpp_properties.json

{
    "configurations": [
        {
            "name": "Win32",
            "includePath": [
                "${workspaceFolder}/**",
                //注意改成自己的MinGW目录
                "C:\\mingw64\\x86_64-w64-mingw32\\include\\**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "compilerPath": "C:\\mingw64\\bin\\gcc.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

main.c


#include <SDL2/SDL.h>

SDL_Window *window = NULL;
SDL_Surface *surface = NULL;

//main函数中的参数不能省略,不然会报错
int main(int args, char *argv[])
{
    SDL_Init(SDL_INIT_VIDEO);
    window = SDL_CreateWindow("SDL2 Test", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
    if (!window)
        return -1;
    surface = SDL_GetWindowSurface(window);
    if (!surface)
        return -2;

    SDL_Rect rec;
    rec.x = 700;
    rec.y = 10,
    rec.w = 20;
    rec.h = 20;

    while (1)
    {
        SDL_FillRect(surface, &rec, SDL_MapRGB(surface->format, 180, 10, 140));

        rec.x += 6;
        rec.y += 2;
        rec.x = rec.x > 800 ? 0 : rec.x;
        rec.y = rec.y > 600 ? 0 : rec.y;

        SDL_FillRect(surface, &rec, SDL_MapRGB(surface->format, 10, 200, 120));

        SDL_UpdateWindowSurface(window);
        SDL_Delay((1.0 / 60) * 1000);
    }

    SDL_FreeSurface(surface);
    SDL_DestroyWindow(window);
    SDL_Quit();

    return 0;
}

大功告成,按Ctrl+Shift+B编译,得到main.exe,双击运行:

GitHub 加速计划 / sd / SDL
8.9 K
1.68 K
下载
Simple Directmedia Layer
最近提交(Master分支:3 个月前 )
a57c5669 - 3 个月前
20a6193e - 3 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐