系统:w10、编辑器:Visual Studio 2022、
下载地址
必要库:
SDL
https://github.com/libsdl-org/SDL
字体
https://github.com/libsdl-org/SDL_ttf
图片
https://github.com/libsdl-org/SDL_image
音频
https://github.com/libsdl-org/SDL_mixer
json
https://github.com/DaveGamble/cJSON
SDL的API说明
https://wiki.libsdl.org/SDL2/CategoryAPI
下载方式:
因为是vs,所以直接下载编译好的,注意选择合适的版本。
绘制图形(有问题,现在使用的:https://github.com/giroletm/SDL2_gfx/releases/tag/release-1.0.4 目前还不知道会不会出问题)
https://www.ferzkopp.net/wordpress/2016/01/02/sdl_gfx-sdl2_gfx/
开始配置
vs配置
创建空项目,创建main.cpp文件,设置Release、设置x64、
右键项目名,点击属性:C++ --> 代码生成 --> 运行库 --> 多线程/MT
将必要库下载后,我们只需要 lib、include(中的x64)、两个文件夹。(三大块:动态链接库.dll、库文件.lib、头文件.h).
VS添加库:右键选择项目的属性
1、C/C++ --> 常规 --> 附加包含目录,添加include路径
2、链接器 --> 常规 --> 附加库目录, 添加lib文件夹的路径
3、链接器 --> 输入 --> 附加依赖项,添加lib文件的路径
4、将.dll文件复制到项目根目录中
(1)添加时使用相对路径
在项目目录下新建了thirdparty的文件夹,放置所有库。
我把cjson其他文件删除了,只保留了cJSON.h、cJSON.c、两个文件
我新建了个include文件夹,将cJSON.c、放了进去,方便配置时格式统一。
其他差不多,只留include、lib、。
(2)cjson的添加
在VS中的源文件右键,–> 添加 --> 新建筛选器
C/C++ --> 常规 --> 附加包含目录,添加include路径
这里使用的相对路径
…\thirdparty\cJSON\include
…\thirdparty\SDL2_mixer\include
…\thirdparty\SDL2\include
…\thirdparty\SDL2_gfx\include
…\thirdparty\SDL2_image\include
…\thirdparty\SDL2_ttf\include
链接器 --> 常规 --> 附加库目录, 添加
这里使用的相对路径
…\thirdparty\SDL2\lib\x64
…\thirdparty\SDL2_gfx\lib\x64
…\thirdparty\SDL2_image\lib\x64
…\thirdparty\SDL2_ttf\lib\x64
…\thirdparty\SDL2_mixer\lib\x64
链接器 --> 输入 --> 附加依赖项,添加lib文件的路径
这里没有添加具体的路径,直接写lib的文件名即可。
SDL2.lib
SDL2main.lib
SDL2_image.lib
SDL2_mixer.lib
SDL2_ttf.lib
SDL2_gfx.lib
将.dll文件复制到项目根目录中
简单测试代码:
#define SDL_MAIN_HANDLED
#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL2_gfxPrimitives.h>
int main()
{
std::cout << "Hello Demo" << std::endl;
return 0;
}
功能测试代码
需要:图片jpg、mp3、字体ttf、
#define SDL_MAIN_HANDLED
#include <iostream>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL2_gfxPrimitives.h>
int main()
{
//std::cout << "Hello Demo" << std::endl;
// 初始化所有子系统
SDL_Init(SDL_INIT_EVERYTHING);
// 图像库初始化 jpg png
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
// 音频库初始化 mp3
Mix_Init(MIX_INIT_MP3);
// 字体初始化
TTF_Init();
// 音频:采样率、解码的音频格式、声道数、音频缓冲区大小、
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048);
// 创建游戏窗口: u8标识后面就可以跟中文、位置居中、大小、显示、
SDL_Window* Window = SDL_CreateWindow(u8"你好, 世界", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, SDL_WINDOW_SHOWN);
// 渲染器GPU:窗口、自动选择索引、硬件加速
SDL_Renderer* renderer = SDL_CreateRenderer(Window, -1, SDL_RENDERER_ACCELERATED);
// 加载图片
SDL_Surface* suf_img = IMG_Load("avatar.jpg");
// 内存传入到GPU
SDL_Texture* tex_img = SDL_CreateTextureFromSurface(renderer, suf_img);
// 加载字体对象:字体、字体大小
TTF_Font* font = TTF_OpenFont("ipix.ttf", 32);
// 字体颜色
SDL_Color color = { 255, 255, 255, 255 };
// 渲染字体内容:字体对象、内容、颜色、
SDL_Surface* suf_text = TTF_RenderUTF8_Blended(font, u8"你好,世界", color);
// 内存传入到GPU
SDL_Texture* tex_text = SDL_CreateTextureFromSurface(renderer, suf_text);
// 加载mp3
Mix_Music* music = Mix_LoadMUS("music.mp3");
// 淡入播放:文件对象、-1为循环播放、淡入效果维持时长、
Mix_FadeInMusic(music, -1, 1500);
int fps = 60; // 固定60帧数
// 游戏主循环变量
bool is_quit = false;
SDL_Event event; // 事件结构体对象
SDL_Point pos_cursor = { 0, 0 }; // 记录鼠标位置
SDL_Rect rect_img, rect_text; // 描述一个矩形
// 高精度SDL计时器
// 当前计时
Uint64 last_counter = SDL_GetPerformanceCounter();
// 每秒跳几次
Uint64 conter_freq = SDL_GetPerformanceFrequency();
// 矩形的宽高是原图的宽高
rect_img.w = suf_img->w;
rect_img.h = suf_img->h;
rect_text.w = suf_text->w;
rect_text.h = suf_text->h;
// 游戏主循环开始
while (!is_quit) {
// 拉取事件结构体对象
while (SDL_PollEvent(&event)) {
// 关闭
if (event.type == SDL_QUIT) {
is_quit = true;
}
// 检测到鼠标事件,更新鼠标位置
if (event.type == SDL_MOUSEMOTION) {
pos_cursor.x = event.motion.x;
pos_cursor.y = event.motion.y;
}
}
// 当前计数
Uint64 current_counter = SDL_GetPerformanceCounter();
// 当前计数与上次计数相减 除 每秒次数
double delta = (double)(current_counter - last_counter) / conter_freq;
// 更新计数
last_counter = current_counter;
// 转换毫秒后,若超出设定数
if (delta * 1000 < 1000.0 / fps)
// 舍弃
SDL_Delay((Uint32)(1000.0 / fps - delta * 1000));
// 处理数据
// 将更新的坐标赋值给矩形
rect_img.x = pos_cursor.x; // 图片
rect_img.y = pos_cursor.y;
rect_text.x = pos_cursor.x; // 字体
rect_text.y = pos_cursor.y;
// 渲染黑色背景
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
// 清屏
SDL_RenderClear(renderer);
// 渲染绘图: 渲染器、图片、原始图截取(当前为空)、贴到某处(自定义矩形xyhw)、
SDL_RenderCopy(renderer, tex_img, nullptr, &rect_img);
// 使用绘制库 绘制圆:半径50、透明度125、
filledCircleRGBA(renderer, pos_cursor.x, pos_cursor.y, 50, 255, 0, 0, 125);
// 绘制字体
SDL_RenderCopy(renderer, tex_text, nullptr, &rect_text);
// 更新
SDL_RenderPresent(renderer);
}
return 0;
}