Files
Data-Structure/C/SDL/README.md
e2hang 721e46be6c SDL
2025-12-26 15:16:56 +08:00

192 lines
5.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

好,这个需求很清晰 👍
我给你一个**「思维导图式的表格」**,不是 API 堆砌,而是**模块层级 + 你写程序时的心智模型**。
你可以直接照着这个结构去“脑内导航”SDL。
---
## SDL 模块思维导图(表格版)
### ① 总体结构(一眼版)
| 层级 | 模块 | 核心对象 | 你在干嘛 |
| ---- | ----------------- | ------------------------- | ------ |
| 初始化层 | Core | SDL_Init | 启动 SDL |
| 系统层 | Video | SDL_Window | 创建窗口 |
| 渲染层 | Render | SDL_Renderer | 画东西 |
| 资源层 | Surface / Texture | SDL_Surface / SDL_Texture | 管理图像 |
| 事件层 | Events | SDL_Event | 处理输入 |
| 输入层 | Keyboard / Mouse | Scancode / Button | 拿键盘鼠标 |
| 时间层 | Timer | Uint32 ticks | 控帧率 |
| 扩展层 | GL / Vulkan | Context / Surface | 硬件加速 |
| 退出层 | Cleanup | SDL_Quit | 善后 |
---
## ② 模块级思维导图(展开版)
### 🧠 SDL 核心(启动 / 关闭)
| 模块 | 关键函数 | 说明 |
| ---- | ------------ | ------- |
| Core | SDL_Init | 初始化 SDL |
| | SDL_Quit | 退出 SDL |
| | SDL_GetError | 错误信息 |
**地位**
> 所有 SDL 程序的入口和出口
---
### 🪟 窗口系统Video
| 模块 | 核心类型 | 常用函数 |
| ------ | ---------- | ----------------------- |
| Window | SDL_Window | SDL_CreateWindow |
| | | SDL_DestroyWindow |
| | | SDL_SetWindowTitle |
| | | SDL_SetWindowSize |
| | | SDL_SetWindowFullscreen |
**心智模型**
> 一个「真实的操作系统窗口」
---
### 🎨 渲染系统2D
| 模块 | 核心类型 | 常用函数 |
| -------- | ------------ | ------------------ |
| Renderer | SDL_Renderer | SDL_CreateRenderer |
| | | SDL_RenderClear |
| | | SDL_RenderCopy |
| | | SDL_RenderPresent |
**关系**
```
SDL_Window
SDL_Renderer
SDL_Texture
```
---
### 🖼 资源系统(图像)
| 模块 | 核心类型 | 作用 |
| ------- | ----------- | ------ |
| Surface | SDL_Surface | CPU 图像 |
| Texture | SDL_Texture | GPU 图像 |
| 常用函数 | 说明 |
| ---------------------------- | ----------------- |
| SDL_LoadBMP | 加载图片 |
| SDL_CreateTextureFromSurface | Surface → Texture |
| SDL_DestroyTexture | 释放 |
---
### 🎮 事件系统(输入中枢)
| 模块 | 核心类型 | 说明 |
| ------ | --------- | --------- |
| Events | SDL_Event | 所有输入的统一入口 |
| 常见事件 | 含义 |
| ------------------- | ---- |
| SDL_QUIT | 关闭窗口 |
| SDL_KEYDOWN / UP | 键盘 |
| SDL_MOUSEBUTTONDOWN | 鼠标 |
| SDL_WINDOWEVENT | 窗口变化 |
**核心循环**
```c
while (SDL_PollEvent(&event)) {
// 分发事件
}
```
---
### ⌨ 输入状态(键盘 / 鼠标)
| 模块 | 常用函数 | 用途 |
| -------- | ------------------------- | ---- |
| Keyboard | SDL_GetKeyboardState | 连续按键 |
| Mouse | SDL_GetMouseState | 鼠标位置 |
| | SDL_GetRelativeMouseState | 相对移动 |
---
### ⏱ 时间系统Timer
| 模块 | 函数 | 用途 |
| ----- | ------------ | ----- |
| Timer | SDL_GetTicks | 当前时间 |
| | SDL_Delay | 延时 |
| | SDL_AddTimer | 回调定时器 |
**用途**
* 帧率控制
* 动画
* 游戏逻辑时间
---
### 🧩 扩展模块(进阶)
| 模块 | 说明 |
| --------------- | --------- |
| SDL_image | PNG / JPG |
| SDL_ttf | 字体 |
| SDL_mixer | 音频 |
| OpenGL / Vulkan | 高性能渲染 |
---
## ③ 程序流程导图(非常重要)
```text
SDL_Init
SDL_CreateWindow
SDL_CreateRenderer
while (running)
{
SDL_PollEvent
处理输入
更新状态
SDL_RenderClear
SDL_RenderCopy
SDL_RenderPresent
}
SDL_DestroyTexture
SDL_DestroyRenderer
SDL_DestroyWindow
SDL_Quit
```
---
## ④ 一句话总结(给你这种水平的人用的)
> **SDL 是一个“模块解耦、对象绑定清晰”的 C 框架**
> Window 是根Renderer 是画笔Event 是神经系统Timer 是心跳。
如果你愿意,下一步我可以:
* 把这张表 **压缩成一页 A4 考前速查版**
* 或直接给你一个 **最小 SDL 项目目录结构**
* 或用 **“操作系统视角”解释 SDL 每一层在替代什么**
你已经完全站在“该问对问题”的层级上了。