148 lines
4.3 KiB
C++
148 lines
4.3 KiB
C++
|
|
/**
|
||
|
|
* @file hello_world.cpp
|
||
|
|
* @brief Hello World 示例程序
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include <extra2d/app/application.h>
|
||
|
|
#include <extra2d/platform/window_module.h>
|
||
|
|
#include <extra2d/core/service_locator.h>
|
||
|
|
#include <extra2d/services/logger_service.h>
|
||
|
|
#include <extra2d/render/renderer.h>
|
||
|
|
#include <extra2d/core/color.h>
|
||
|
|
#include <extra2d/core/math_types.h>
|
||
|
|
#include <iostream>
|
||
|
|
|
||
|
|
using namespace extra2d;
|
||
|
|
|
||
|
|
int main(int argc, char* argv[]) {
|
||
|
|
std::cout << "========================================" << std::endl;
|
||
|
|
std::cout << " Extra2D - Hello World Example" << std::endl;
|
||
|
|
std::cout << "========================================" << std::endl;
|
||
|
|
|
||
|
|
auto& app = Application::get();
|
||
|
|
app.appName = "Hello World - Extra2D";
|
||
|
|
app.appVersion = "1.0.0";
|
||
|
|
|
||
|
|
app.use<WindowModule>([](WindowCfg& cfg) {
|
||
|
|
cfg.title = "Hello World - Extra2D";
|
||
|
|
cfg.w = 1024;
|
||
|
|
cfg.h = 768;
|
||
|
|
cfg.vsync = true;
|
||
|
|
});
|
||
|
|
|
||
|
|
if (!app.init()) {
|
||
|
|
std::cerr << "Failed to initialize application" << std::endl;
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
auto logger = ServiceLocator::instance().get<ILogger>();
|
||
|
|
if (logger) {
|
||
|
|
logger->info("Hello World example initialized");
|
||
|
|
}
|
||
|
|
|
||
|
|
auto renderer = IRenderer2D::create(true);
|
||
|
|
if (!renderer) {
|
||
|
|
std::cerr << "Failed to create renderer" << std::endl;
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
auto window = app.window();
|
||
|
|
if (!window) {
|
||
|
|
std::cerr << "Failed to get window" << std::endl;
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!renderer->init(window->native())) {
|
||
|
|
std::cerr << "Failed to initialize renderer" << std::endl;
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (logger) {
|
||
|
|
logger->info("Renderer: %s (Vulkan: %s)",
|
||
|
|
renderer->backendName(),
|
||
|
|
renderer->isVulkan() ? "yes" : "no");
|
||
|
|
}
|
||
|
|
|
||
|
|
float totalTime = 0.0f;
|
||
|
|
|
||
|
|
while (app.running() && !window->shouldClose()) {
|
||
|
|
float dt = app.dt();
|
||
|
|
totalTime += dt;
|
||
|
|
|
||
|
|
renderer->beginFrame();
|
||
|
|
|
||
|
|
i32 width = renderer->width();
|
||
|
|
i32 height = renderer->height();
|
||
|
|
|
||
|
|
Viewport viewport;
|
||
|
|
viewport.x = 0;
|
||
|
|
viewport.y = 0;
|
||
|
|
viewport.width = width;
|
||
|
|
viewport.height = height;
|
||
|
|
renderer->setViewport(viewport);
|
||
|
|
|
||
|
|
ClearCommand clearCmd;
|
||
|
|
clearCmd.color = Color(20, 20, 30, 255);
|
||
|
|
clearCmd.clearColor = true;
|
||
|
|
clearCmd.clearDepth = true;
|
||
|
|
renderer->clear(clearCmd);
|
||
|
|
|
||
|
|
float centerX = static_cast<float>(width) / 2.0f;
|
||
|
|
float centerY = static_cast<float>(height) / 2.0f;
|
||
|
|
|
||
|
|
float pulse = 0.5f + 0.5f * std::sin(totalTime * 2.0f);
|
||
|
|
Color textColor(
|
||
|
|
static_cast<u8>(100 + 155 * pulse),
|
||
|
|
static_cast<u8>(200 + 55 * pulse),
|
||
|
|
static_cast<u8>(255),
|
||
|
|
255
|
||
|
|
);
|
||
|
|
|
||
|
|
float rectWidth = 320.0f;
|
||
|
|
float rectHeight = 70.0f;
|
||
|
|
float rectX = centerX - rectWidth / 2;
|
||
|
|
float rectY = centerY - rectHeight / 2;
|
||
|
|
|
||
|
|
Color rectColor(50, 50, 70, 255);
|
||
|
|
renderer->drawRect(Vec2{rectX, rectY}, Vec2{rectWidth, rectHeight}, rectColor, true);
|
||
|
|
|
||
|
|
Color borderColor(
|
||
|
|
static_cast<u8>(100 + 100 * pulse),
|
||
|
|
static_cast<u8>(180 + 50 * pulse),
|
||
|
|
255,
|
||
|
|
255
|
||
|
|
);
|
||
|
|
renderer->drawRect(Vec2{rectX, rectY}, Vec2{rectWidth, rectHeight}, borderColor, false);
|
||
|
|
|
||
|
|
for (int i = 0; i < 5; ++i) {
|
||
|
|
float x = 100.0f + i * 200.0f;
|
||
|
|
float y = static_cast<float>(height) - 50.0f;
|
||
|
|
float radius = 20.0f;
|
||
|
|
|
||
|
|
float phase = totalTime * 1.5f + i * 0.5f;
|
||
|
|
float offsetY = std::sin(phase) * 20.0f;
|
||
|
|
|
||
|
|
Color circleColor(
|
||
|
|
static_cast<u8>(50 + i * 30),
|
||
|
|
static_cast<u8>(100 + i * 20),
|
||
|
|
static_cast<u8>(150 + i * 20),
|
||
|
|
200
|
||
|
|
);
|
||
|
|
|
||
|
|
renderer->drawCircle(Vec2{x, y + offsetY}, radius, circleColor, true, 16);
|
||
|
|
}
|
||
|
|
|
||
|
|
renderer->flushBatch();
|
||
|
|
renderer->endFrame();
|
||
|
|
renderer->present();
|
||
|
|
|
||
|
|
window->poll();
|
||
|
|
}
|
||
|
|
|
||
|
|
renderer->shutdown();
|
||
|
|
app.shutdown();
|
||
|
|
|
||
|
|
std::cout << "Application terminated successfully" << std::endl;
|
||
|
|
return 0;
|
||
|
|
}
|