VScode+SDL2 configuration tutorial

The day before yesterday, I wanted to find a graphic library to write a small tool, and I wanted to find a smaller graphic library, so I found ege(easy graphics engine), and spent some time to configure it on vscode, and wrote an article by the way. This is the tutorial address: https://blog.csdn.net/qq272508839/article/details/104287255

Today, I think of a well-known graphics library called SDL. I heard it a long time ago, so I downloaded a plan to try it. It's under the environment of MinGW. I won't say how to configure MinGW. It's very simple.

First to the official website: http://www.libsdl.org/ Download two things, as shown below:

Corresponding to my own platform download, I am win10 64 bit.

After downloading these two things:

509kb is used to put it into the project root directory after compilation. The following tar is the part that needs to be installed by yourself.

Unzip tar.gz, which contains the following folders:

The two folders marked need to be placed in the installation directory of MinGW, not both of them need to be copied, just copy one corresponding to their own version of MinGW. I am 64 bit MinGW, and I want the one starting with X86 ʄ.

Open the x86 ʄ folder, which contains several folders (bin, include, lib, share):

Select all these folders and copy them to the directory of MinGW installation \ x86 ʄ w64-mingw32. If the 32-bit MinGW is the directory of MinGW installation \ i686-w64-mingw32, after copying, you will find that there is an SDL2 in the directory below, which indicates that the installation is completed:

Then configure vscode, create a new project folder, create a new. Vscode directory in the project root directory (don't forget the previous point of vscode), and create three JSON files in the. Vscode Directory: tasks.json, launch.json, C ﹐ CPP ﹐ properties.json. Then extract a file called SDL2.dll from the 509kb compressed package, copy the DLL to the project root directory, and create it in the root directory A main.c, as shown below:

Then copy and paste the code into the corresponding file, and directly upload the code.

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "g++.exe build active file",
            //Pay attention to change to your own MinGW directory
            "command": "C:\\mingw64\\bin\\g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "-lmingw32",
                "-lSDL2main",
                "-lSDL2",
                "-mwindows"
            ],
            "options": {
                //Pay attention to change to your own MinGW directory
                "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",
            //Pay attention to change to your own MinGW directory
            "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}/**",
                //Pay attention to change to your own MinGW directory
                "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;

//The parameters in the main function cannot be omitted, or an error will be reported
int main(int arg, char *argv[])
{
    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;
}

When it is finished, press Ctrl+Shift+B to compile and get main.exe. Double click to run:

18 original articles published, 3 praised, 20000 visitors+
Private letter follow

Tags: JSON shell

Posted on Fri, 14 Feb 2020 07:42:49 -0500 by amo