disp
函数在 C 语言图形界面中的应用通常涉及到使用图形库,如 SDL、GLFW 或者 OpenGL
首先,确保已经安装了 SDL2 开发库。然后,创建一个名为 main.c
的文件,并添加以下代码:
#include <SDL.h>
#include<stdio.h>
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Error initializing SDL: %s\n", SDL_GetError());
return 1;
}
SDL_Window* window = SDL_CreateWindow("My Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
if (!window) {
printf("Error creating window: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
printf("Error creating renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Surface* surface = SDL_LoadBMP("image.bmp");
if (!surface) {
printf("Error loading image: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
if (!texture) {
printf("Error creating texture: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
这段代码首先初始化 SDL,然后创建一个窗口和渲染器。接着,它加载一个名为 “image.bmp” 的 BMP 图像文件,并将其转换为纹理。最后,它将纹理绘制到窗口上,并显示 3 秒钟。在这个例子中,disp
函数的作用是显示图像。
要编译和运行此程序,请使用以下命令:
gcc main.c -o main -lSDL2
./main
注意:这个例子仅适用于 Linux 系统。对于 Windows 和 macOS,您需要根据相应平台的要求进行调整。