2026-02-25 06:23:53 +08:00
|
|
|
|
#include <extra2d.h>
|
2026-03-16 19:38:24 +08:00
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
#include <fstream>
|
2026-02-28 22:30:48 +08:00
|
|
|
|
#include <cstdio>
|
2026-03-16 19:38:24 +08:00
|
|
|
|
#include <string>
|
|
|
|
|
|
#include <vector>
|
2026-02-11 19:40:26 +08:00
|
|
|
|
|
|
|
|
|
|
using namespace extra2d;
|
|
|
|
|
|
|
2026-03-16 19:38:24 +08:00
|
|
|
|
static bool writeTestWav(const std::string &path) {
|
|
|
|
|
|
const int sampleRate = 22050;
|
|
|
|
|
|
const int channels = 1;
|
|
|
|
|
|
const int bitsPerSample = 16;
|
|
|
|
|
|
const int durationMs = 250;
|
|
|
|
|
|
const int sampleCount = sampleRate * durationMs / 1000;
|
|
|
|
|
|
const int byteRate = sampleRate * channels * bitsPerSample / 8;
|
|
|
|
|
|
const int blockAlign = channels * bitsPerSample / 8;
|
|
|
|
|
|
const int dataSize = sampleCount * blockAlign;
|
|
|
|
|
|
const int riffSize = 36 + dataSize;
|
|
|
|
|
|
|
|
|
|
|
|
std::ofstream file(path, std::ios::binary);
|
|
|
|
|
|
if (!file.is_open()) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
file.write("RIFF", 4);
|
|
|
|
|
|
file.write(reinterpret_cast<const char *>(&riffSize), 4);
|
|
|
|
|
|
file.write("WAVE", 4);
|
|
|
|
|
|
file.write("fmt ", 4);
|
|
|
|
|
|
const int fmtChunkSize = 16;
|
|
|
|
|
|
const short audioFormat = 1;
|
|
|
|
|
|
const short numChannels = static_cast<short>(channels);
|
|
|
|
|
|
const short bps = static_cast<short>(bitsPerSample);
|
|
|
|
|
|
file.write(reinterpret_cast<const char *>(&fmtChunkSize), 4);
|
|
|
|
|
|
file.write(reinterpret_cast<const char *>(&audioFormat), 2);
|
|
|
|
|
|
file.write(reinterpret_cast<const char *>(&numChannels), 2);
|
|
|
|
|
|
file.write(reinterpret_cast<const char *>(&sampleRate), 4);
|
|
|
|
|
|
file.write(reinterpret_cast<const char *>(&byteRate), 4);
|
|
|
|
|
|
const short ba = static_cast<short>(blockAlign);
|
|
|
|
|
|
file.write(reinterpret_cast<const char *>(&ba), 2);
|
|
|
|
|
|
file.write(reinterpret_cast<const char *>(&bps), 2);
|
|
|
|
|
|
file.write("data", 4);
|
|
|
|
|
|
file.write(reinterpret_cast<const char *>(&dataSize), 4);
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<int16_t> samples(static_cast<size_t>(sampleCount), 0);
|
|
|
|
|
|
file.write(reinterpret_cast<const char *>(samples.data()), dataSize);
|
|
|
|
|
|
return file.good();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-13 16:52:57 +08:00
|
|
|
|
int main(int argc, char **argv) {
|
2026-02-28 22:30:48 +08:00
|
|
|
|
// 创建应用(自动创建窗口)
|
|
|
|
|
|
auto app = Application::create();
|
|
|
|
|
|
|
2026-02-11 19:40:26 +08:00
|
|
|
|
AppConfig config;
|
2026-02-28 22:30:48 +08:00
|
|
|
|
config.title = "Hello World";
|
2026-02-11 19:40:26 +08:00
|
|
|
|
config.width = 1280;
|
|
|
|
|
|
config.height = 720;
|
2026-02-28 22:30:48 +08:00
|
|
|
|
|
|
|
|
|
|
if (!app->init(config)) {
|
2026-03-03 20:32:51 +08:00
|
|
|
|
printf("初始化失败!\n");
|
2026-02-11 19:40:26 +08:00
|
|
|
|
return -1;
|
|
|
|
|
|
}
|
2026-03-03 20:32:51 +08:00
|
|
|
|
|
|
|
|
|
|
printf("窗口: %dx%d\n", app->getWindowWidth(), app->getWindowHeight());
|
2026-03-16 19:38:24 +08:00
|
|
|
|
|
|
|
|
|
|
AssetsModule *assets = getAssets();
|
|
|
|
|
|
if (assets) {
|
|
|
|
|
|
Handle<Font> font = assets->load<Font>("assets/font.ttf");
|
|
|
|
|
|
if (font.isValid()) {
|
|
|
|
|
|
Font *fontPtr = assets->get(font);
|
|
|
|
|
|
float lineHeight = fontPtr ? fontPtr->lineHeight(32.0f) : 0.0f;
|
|
|
|
|
|
printf("字体加载成功,32px 行高: %.2f\n", lineHeight);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
printf("字体加载失败\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const std::string testMusicPath = "assets/generated_test.wav";
|
|
|
|
|
|
if (writeTestWav(testMusicPath)) {
|
|
|
|
|
|
Handle<Music> music = assets->load<Music>(testMusicPath);
|
|
|
|
|
|
if (music.isValid()) {
|
|
|
|
|
|
printf("音乐加载成功: %s\n", testMusicPath.c_str());
|
|
|
|
|
|
} else {
|
|
|
|
|
|
printf("音乐加载失败: %s\n", testMusicPath.c_str());
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
|
|
|
|
|
printf("测试音频文件生成失败\n");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-02-28 22:30:48 +08:00
|
|
|
|
|
2026-02-11 19:40:26 +08:00
|
|
|
|
// 运行应用
|
2026-02-28 22:30:48 +08:00
|
|
|
|
app->run();
|
|
|
|
|
|
|
|
|
|
|
|
// 自动销毁
|
2026-02-11 19:40:26 +08:00
|
|
|
|
return 0;
|
|
|
|
|
|
}
|