在windows下使用C语言进行图形编程 SDL2开发环境搭建
你是否对C语言命令行黑框感到厌倦,今天来为你的C编程增加一些可视化窗口吧!
1、windows下安装C编译器
这对于一个开发者来说简直是太业余了,如果你是一个开发者,你的电脑上怎么会没有gcc呢,如果你是一个新手,可以转到这个链接:https://zhuanlan.zhihu.com/p/355510947。我们可以通过在命令行输入gcc来验证是否安装成功
PS C:\Users\13588\Desktop\SDL> gcc -v
Using built-in specs.
COLLECT_GCC=C:\mingw64\bin\gcc.exe
COLLECT_LTO_WRAPPER=C:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/8.1.0/lto-wrapper.exe
Target: x86_64-w64-mingw32
Configured with: ../../../src/gcc-8.1.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --
Thread model: win32
省略···
gcc version 8.1.0 (x86_64-win32-seh-rev0, Built by MinGW-W64 project)
2、下载SDL2
在这个网站下载SDL2,官网已经将下载链接迁移至github:https://github.com/libsdl-org/SDL/releases/tag/release-2.28.3 ,对于windows来说,将SDL2-devel-2.28.3-mingw.zip下载解压即可,我们只需要的文件夹为x86_64-w64-mingw32
3、新建工程
新建一个文件夹为SDL_test,将上一步中的x86_64-w64-mingw32重命名为SDL2放在根目录中,并且新建main.c文件,将SDL_test\SDL2\bin文件夹下的SDL2.dll移动到main.c同目录中,工程建立完毕后的目录结构为
PS C:\Users\13588\Desktop\SDL_test> ls
目录: C:\Users\13588\Desktop\SDL_test
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2023/9/7 17:01 SDL2
-a---- 2023/9/7 17:13 1055 main.c
-a---- 2023/9/2 2:28 2499072 SDL2.dll
编辑main.c 实现一个最简单的画图显示demo,窗口在打开3S后自动关闭
#include "SDL.h"
#include <stdio.h>
#define POINTS_COUNT 4
static SDL_Point points[POINTS_COUNT] = {
{320, 200},
{300, 240},
{340, 240},
{320, 200}
};
static SDL_Rect bigrect = {0,0,540, 380};
int main(int argc, char* argv[]) {
int flag = 1;
SDL_Window *window; // Declare a pointer
SDL_Renderer *renderer;
SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2
// Create an application window with the following settings:
window = SDL_CreateWindow(
"An SDL2 window", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x position
SDL_WINDOWPOS_UNDEFINED, // initial y position
640, // width, in pixels
480, // height, in pixels
SDL_WINDOW_SHOWN | SDL_WINDOW_BORDERLESS// flags - see below
);
// Check that the window was successfully created
if (window == NULL) {
// In the case that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}
/* We must call SDL_CreateRenderer in order for draw calls to affect this window. */
renderer = SDL_CreateRenderer(window, -1, 0);
/* Select the color for drawing. It is set to red here. */
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
/* Clear the entire screen to our selected color. */
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
//SDL_RenderDrawLine(renderer, 100, 20, 500, 400);
SDL_RenderDrawLines(renderer, points, POINTS_COUNT);
SDL_Rect rect = {200, 300, 100, 100};
SDL_RenderDrawRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255);
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255);
SDL_RenderFillRect(renderer, &bigrect);
/* Up until now everything was drawn behind the scenes.
This will show the new, red contents of the window. */
SDL_RenderPresent(renderer);
// The window is open: could enter program loop here (see SDL_PollEvent())
SDL_Delay(3000); // Pause execution for 3000 milliseconds, for example
//destory renderer
if (renderer) {
SDL_DestroyRenderer(renderer);
}
// Close and destroy the window
SDL_DestroyWindow(window);
// Clean up
SDL_Quit();
return 0;
}
4、编译运行
在当前文件夹下使用gcc编译,传参中加入头文件固定地址和库文件地址
gcc ./main.c -I.\SDL2\include -L.\SDL2\lib -lmingw32 -lSDL2main -lSDL2
运行a.exe文件,桌面会显示一个彩色的窗口:
5、总结
整体来说SDL2的运行较为简单的,学会使用SDL后,就可以写出来一些带有界面的小程序,并且可以用来提前验证GUI程序,不用频繁的烧录到单片机
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。