MAC OS操作系统下载SDL库链接:MAC OS安装SDL——【音视频开发学习笔记3】-CSDN博客
目录
2、将SDL-2.30.6整个文件夹拷贝到创建的项目文件夹下面
本篇记录MAC OS操作系统在QT中编译使用SDL库。
1、在QT中创建一个新的项目:
2、将SDL-2.30.6整个文件夹拷贝到创建的项目文件夹下面
3、在.pro文件下添加SDL 库
mac{
INCLUDEPATH += $$PWD/SDL-2.30.6/include
LIBS += -L$$PWD/SDL-2.30.6/lib -lSDL2
}
4、main.c中加入SDL.h头文件
#include <stdio.h>
#include <SDL.h>
#undef main
int main()
{
printf("Hello World!\n");
int run = 1;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *texture = NULL;
SDL_Rect rect; // 长方形,原点在左上角
rect.w = 50; //方块大小
rect.h = 50;
SDL_Init(SDL_INIT_VIDEO);//初始化函数,可以确定希望激活的子系统
window = SDL_CreateWindow("2 Window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);// 创建窗口
if (!window)
{
return -1;
}
renderer = SDL_CreateRenderer(window, -1, 0);//基于窗口创建渲染器
if (!renderer)
{
return -1;
}
texture = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
640,
480); //创建纹理
if (!texture)
{
return -1;
}
int show_count = 0;
while (run)
{
rect.x = rand() % 600;
rect.y = rand() % 400;
SDL_SetRenderTarget(renderer, texture); // 设置渲染目标为纹理
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // 纹理背景为黑色
SDL_RenderClear(renderer); //清屏
SDL_RenderDrawRect(renderer, &rect); //绘制一个长方形
SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255); //长方形为白色
SDL_RenderFillRect(renderer, &rect);
SDL_SetRenderTarget(renderer, NULL); //恢复默认,渲染目标为窗口
SDL_RenderCopy(renderer, texture, NULL, NULL); //拷贝纹理到CPU
SDL_RenderPresent(renderer); //输出到目标窗口上
SDL_Delay(300);
if(show_count++ > 30)
{
run = 0; // 不跑了
}
}
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window); //销毁窗口
SDL_Quit();
return 0;
}
编译运行,无报错