当前位置: 首页 > article >正文

C++ 基于SDL库的 Visual Studio 2022 环境配置

系统: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.h、放了进去,方便配置时格式统一。

其他差不多,只留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、csv文件、json文件、

#define SDL_MAIN_HANDLED

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

#include <cJSON.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL_image.h>
#include <SDL_mixer.h>
#include <SDL2_gfxPrimitives.h>

void test_json()
{
    // json内容
    //{
    //    "name": "XiaoMing",
    //        "age" : 18,
    //        "pets" : ["dog", "cat", "bird"]
    //}

    std::ifstream file("test.json");
    if (!file.good())
    {
        std::cout << "无法打开文件" << std::endl;
        return;
    }

    // 创建
    std::stringstream str_stream;
    // 读取全部内容
    str_stream << file.rdbuf();
    // 关闭文件
    file.close();

    // 读取json文件的内容
    cJSON* json_root = cJSON_Parse(str_stream.str().c_str());

    cJSON* json_name = cJSON_GetObjectItem(json_root, "name");
    cJSON* json_age = cJSON_GetObjectItem(json_root, "age");
    cJSON* json_pets = cJSON_GetObjectItem(json_root, "pets");

    std::cout << json_name->string << ": " << json_name->valuestring << std::endl;
    std::cout << json_age->string << ": " << json_age->valueint << std::endl;

    std::cout << json_pets->string << ": " << std::endl;

    // 定义cjson指针
    cJSON* json_item = nullptr;
    // cJSON 的 for循环
    cJSON_ArrayForEach(json_item, json_pets)
    {
        std::cout << "\t" << json_item->valuestring << std::endl;
    }
}

void test_csv()
{
    // csv 内容
    //1, 2, 3
    //4, 5, 6
    //7, 8, 9

    std::ifstream file("test.csv");
    if (!file.good())
    {
        std::cout << "无法打开文件" << std::endl;
        return;
    }
    // 1行的文本内容
    std::string str_line;

    while (std::getline(file, str_line))
    {
        std::string str_grid;  // 分割后的

        std::stringstream str_stream(str_line);

        // 以逗号为分割,循环打印出来
        while (std::getline(str_stream, str_grid, ','))
        {
            std::cout << str_grid << " ";
        }

        std::cout << std::endl;
    }

    file.close();
}

int main()
{
    //std::cout << "Hello Demo" << std::endl;
    test_json();  // json 测试
    test_csv();   //  csv 测试

    // 初始化所有子系统
    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;
}





http://www.kler.cn/news/342971.html

相关文章:

  • 在培训考试小程序页面弹出半屏的弹窗交互实践
  • 使用Spring Security+jwt+redis实现登录注册逻辑
  • 请解释一下数据库的分区和分片?请解释一下数据库的日志和日志的重要性?
  • 【Python】使用优先级队列管理任务顺序
  • 设置 Notepad++ 制表符(Tab 缩进)宽度为2个空格大小
  • Bit-map按位存储--轻松应对面试被问到从10亿个数字中查找指定数是否存在
  • Oracle 12201非PDBS模式单机部署(静默安装)
  • 洗衣店订单管理:Spring Boot技术突破
  • 使⽤ Override 和 New 关键字进⾏版本控制(C#)
  • python调用父类同名成员
  • 高效批量重命名:Windows系统文件与文件夹管理技巧解析
  • [SQL] 数据库增删改操作
  • 前端在vue项目静态文件夹下引入非默认字体并使用
  • 内衣洗衣机和手洗哪个干净?2024内衣洗衣机实力排行揭晓
  • 毕设---中国移动网站平台管理系统的设计与实现
  • LeetCode518:零钱兑换
  • unity 2d 近战攻击判定的三种方式以及精确获取碰撞点
  • NTO和MPW
  • python 实现even_tree偶数树算法
  • Python入门笔记(三)