Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Dev C++ 用户:新建项目 > 添加 `main.cpp` > 编译运行,完事。

## 示例程序

`examples/` 目录包含 15 个由浅入深的示例,每个兼容 Win32 和 SDL 两条产品线:
`examples/` 目录包含 16 个由浅入深的示例,每个兼容 Win32 和 SDL 两条产品线:

### 入门基础

Expand Down Expand Up @@ -142,6 +142,7 @@ Dev C++ 用户:新建项目 > 添加 `main.cpp` > 编译运行,完事。
| `13_clip_rect.cpp` | 裁剪矩形 | SetClip/ClearClip |
| `14_space_shooter.cpp` | 太空射击 | 综合实战 |
| `15_ui_controls.cpp` | UI 控件演示 | Button、Checkbox、RadioBox |
| `16_coreball.cpp` | 见缝插针 | 圆周运动、角度碰撞、状态机 |

编译任意示例:

Expand Down
4 changes: 2 additions & 2 deletions SDL2PORT.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,5 +186,5 @@ Windows 下如果使用动态库版本,需要让运行时能找到这些 DLL

### 5.6 迁移现状

- 当前 SDL 回归入口是 `examples/01_hello.cpp` ~ `examples/15_ui_controls.cpp`,这 15 个统一示例通过预处理器自动选择 Win32 或 SDL 后端,同时作为两条产品线的回归验证。
- 覆盖路径包括:基础窗口、图元、精灵、动画、字体、音频、Tilemap、裁剪矩形、缩放、旋转与 UI 控件
- 当前 SDL 回归入口是 `examples/01_hello.cpp` ~ `examples/16_coreball.cpp`,这 16 个统一示例通过预处理器自动选择 Win32 或 SDL 后端,同时作为两条产品线的回归验证。
- 覆盖路径包括:基础窗口、图元、精灵、动画、字体、音频、Tilemap、裁剪矩形、缩放、旋转、UI 控件与角度碰撞小游戏
2 changes: 1 addition & 1 deletion docs/GameLib.SDL.md
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,7 @@ static bool _srandDone;
- `docs/GameLib.SDL.md` 已同步到当前实现状态,仓库根目录也已新增 `SDL2PORT.md` 作为简版移植说明。
- `AGENTS.md` 已补充 `GameLib.SDL.h` / `docs/GameLib.SDL.md` 的索引与用途。
- README 仍只保留一句 SDL 产品线提示,主叙事继续突出 Win32 零依赖主线;具体 SDL 编译命令与限制统一放到 `SDL2PORT.md`。
- `examples/01_hello.cpp` ~ `examples/15_ui_controls.cpp` 已统一为 Win32 / SDL 双后端示例,通过预处理器自动选择后端,覆盖基础窗口、图元、精灵、动画、字体、音频、Tilemap、裁剪矩形、缩放、旋转与 UI 控件等路径,同时作为 SDL 版的回归入口。
- `examples/01_hello.cpp` ~ `examples/16_coreball.cpp` 已统一为 Win32 / SDL 双后端示例,通过预处理器自动选择后端,覆盖基础窗口、图元、精灵、动画、字体、音频、Tilemap、裁剪矩形、缩放、旋转、UI 控件与角度碰撞小游戏等路径,同时作为 SDL 版的回归入口。
- 本阶段收口结论:`GameLib.SDL.h` 已具备独立产品线的最小可维护状态,后续工作可以从“补齐代表性回归入口”切换到“按具体差异或新需求增量演进”。

### 14.5 当前已知差异与可继续完善项
Expand Down
48 changes: 35 additions & 13 deletions examples/08_breakout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
#define BRICK_OFFSET_X 12
#define BRICK_OFFSET_Y 50

static const int PADDLE_W = 80;
static const int PADDLE_H = 12;
static const int PADDLE_START_X = 280;
static const int PADDLE_Y = 450;
static const int PADDLE_SPEED = 6;

static const float BALL_START_VY = -4.0f;
static const float BALL_START_MIN_ABS_VX = 1.5f;
static const float BALL_START_MAX_ABS_VX = 4.0f;
static const int BALL_R = 5;
static const float BALL_PADDLE_MAX_VX = 8.0f;
static const int LIVES_START = 1;

static const char *ChooseExistingPath(const char *pathA, const char *pathB)
{
FILE *file = fopen(pathA, "rb");
Expand All @@ -32,8 +45,17 @@ static const char *ChooseExistingPath(const char *pathA, const char *pathB)
return pathA;
}

int main()
{
static void ResetBallVelocity(float *vx, float *vy)
{
int minSpeedX10 = (int)(BALL_START_MIN_ABS_VX * 10.0f);
int maxSpeedX10 = (int)(BALL_START_MAX_ABS_VX * 10.0f);
float speed = (float)GameLib::Random(minSpeedX10, maxSpeedX10) / 10.0f;
if (GameLib::Random(0, 1) == 0) speed = -speed;
*vx = speed;
*vy = BALL_START_VY;
}
int main()
{
GameLib game;
game.Open(640, 480, "08 - Breakout", true);

Expand All @@ -55,14 +77,14 @@ int main()
bool bricks[BRICK_ROWS][BRICK_COLS];
uint32_t brickColors[] = {COLOR_RED, COLOR_ORANGE, COLOR_YELLOW, COLOR_GREEN, COLOR_CYAN, COLOR_PURPLE};

int padW = 80, padH = 12;
int padX = 280, padY = 450;
int padW = PADDLE_W, padH = PADDLE_H;
int padX = PADDLE_START_X, padY = PADDLE_Y;

float ballX = 320, ballY = 430;
float ballVX = 3.0f, ballVY = -4.0f;
int ballR = 5;
float ballVX = 0.0f, ballVY = BALL_START_VY;
int ballR = BALL_R;

int score = 0, lives = 3, totalBricks = 0;
int score = 0, lives = LIVES_START, totalBricks = 0;
bool started = false, gameOver = false, gameWin = false;

for (int r = 0; r < BRICK_ROWS; r++)
Expand All @@ -75,8 +97,8 @@ int main()
if (game.IsKeyPressed(KEY_ESCAPE)) break;

if (!gameOver && !gameWin) {
if (game.IsKeyDown(KEY_LEFT)) padX -= 6;
if (game.IsKeyDown(KEY_RIGHT)) padX += 6;
if (game.IsKeyDown(KEY_LEFT)) padX -= PADDLE_SPEED;
if (game.IsKeyDown(KEY_RIGHT)) padX += PADDLE_SPEED;
if (padX < 0) padX = 0;
if (padX + padW > game.GetWidth()) padX = game.GetWidth() - padW;

Expand All @@ -85,7 +107,7 @@ int main()
ballY = (float)(padY - ballR - 1);
if (game.IsKeyPressed(KEY_SPACE)) {
started = true;
ballVX = 3.0f; ballVY = -4.0f;
ResetBallVelocity(&ballVX, &ballVY);
sfxToPlay = launchSfx;
}
} else {
Expand All @@ -107,7 +129,7 @@ int main()
if (ballY + ballR > game.GetHeight()) {
lives--;
if (lives <= 0) { gameOver = true; sfxToPlay = gameOverSfx; }
else { started = false; ballVX = 3.0f; ballVY = -4.0f; sfxToPlay = loseLifeSfx; }
else { started = false; ballVX = 0.0f; ballVY = BALL_START_VY; sfxToPlay = loseLifeSfx; }
}

if (ballVY > 0 &&
Expand All @@ -116,7 +138,7 @@ int main()
ballVY = -ballVY;
ballY = (float)(padY - ballR);
float hitPos = (ballX - padX) / padW;
ballVX = (hitPos - 0.5f) * 8.0f;
ballVX = (hitPos - 0.5f) * BALL_PADDLE_MAX_VX;
if (!sfxToPlay) sfxToPlay = bounceSfx;
}

Expand Down Expand Up @@ -156,7 +178,7 @@ int main()
for (int c = 0; c < BRICK_COLS; c++)
bricks[r][c] = true;
totalBricks = BRICK_ROWS * BRICK_COLS;
score = 0; lives = 3; padX = 280;
score = 0; lives = LIVES_START; padX = PADDLE_START_X;
started = false; gameOver = false; gameWin = false;
sfxToPlay = restartSfx;
}
Expand Down
Loading