This commit is contained in:
HP
2026-06-07 14:37:58 +08:00
13 changed files with 445 additions and 43 deletions
+45 -34
View File
@@ -6,10 +6,10 @@
#include <cstdio>
#include <iostream>
#include <thread>
#include "BitmapFont.h"
#include <chrono>
#include "Color.h"
#include "Display.h"
#include <cstdlib>
#include "Timer.h"
#include "DrawContext.h"
#include "Image.h"
#include "TimeSource.h"
@@ -23,10 +23,15 @@
#include "SDLDisplay.h"
#endif
namespace
const int32_t width = 1024;
const int32_t height = 600;
static void PrintUsage(const char *program_name)
{
const int32_t DemoWidth = 800;
const int32_t DemoHeight = 600;
std::cout
<< "Usage: " << program_name << " [--fps 30|45|60]\n"
<< " " << program_name << " [--fps=30|45|60]\n";
}
struct ProgramOptions
{
@@ -141,39 +146,46 @@ int main(int argc, char* argv[])
font.columns = font_columns;
font.first_char = font_first_char;
RenderData::Image sprite(test_sprite_pixels, test_sprite_width, test_sprite_height, 0x00000000u);
RenderData::Image sprite_img(test_sprite_pixels, test_sprite_width, test_sprite_height, 0x00000000);
RenderData::SpriteRegion sprite_region(&sprite_img, 0, 0, sprite_img.width, sprite_img.height);
const std::array<uint16_t, 8 * 4> tileIds = {
0, RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile,
RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile, 0,
0, RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile,
RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile, 0, RenderData::Tilemap::EmptyTile, 0};
RenderData::Tilemap testTilemap(tileIds.data(), 8, 4, &sprite_img, sprite_img.width, sprite_img.height, 1);
const RenderData::Color clearColor(18, 18, 24, 255);
const RenderData::Color fpsColor(0, 255, 80, 255);
const RenderData::Color fpsBg(0, 0, 0, 200);
int32_t fps = 0;
int32_t frame_count = 0;
uint32_t last_fps_time = 0;
uint32_t animation_time_ms = 0;
char fps_text[32];
char perf_text[64];
uint32_t last_frame_ms = 0;
uint32_t last_present_ms = 0;
std::cout << "Demo started. Target FPS: " << timer.target_fps() << std::endl;
bool is_running = true;
while (is_running)
bool isRunning = true;
while (isRunning)
{
timer.begin_frame(time_source.get_time_ms());
animation_time_ms += timer.fixed_delta_ms();
const uint32_t frame_start_ms = time_source.get_time_ms();
timer.begin_frame(frame_start_ms);
bool should_quit = false;
display->poll_events(should_quit);
if (should_quit)
{
is_running = false;
}
display->poll_events(isRunning);
ctx.clear(RenderData::Color(18, 18, 24, 255));
ctx.fill_rect(48, 72, 704, 360, RenderData::Color(42, 48, 60, 255));
ctx.fill_rect(64, 88, 672, 328, RenderData::Color(24, 28, 36, 255));
ctx.clear_color(clearColor);
const int32_t travel = std::max(1, DemoWidth - sprite.width - 128);
const int32_t sprite_x = 64 + static_cast<int32_t>((animation_time_ms / 8u) % static_cast<uint32_t>(travel));
const int32_t sprite_y = 220;
ctx.draw_sprite(sprite_x, sprite_y, sprite);
ctx.draw_text(font, 64, 96, RenderData::Color(240, 240, 240, 255), "GFX DEMO");
// sprite 测试
ctx.draw_sprite(10, 10, sprite_img);
ctx.draw_sprite_region_ex(30, 10, sprite_region, 2, false, false);
ctx.draw_sprite_region_ex(10, 30, sprite_region, 3, true, false);
ctx.draw_tilemap(testTilemap, 650, 500, 96, 48, static_cast<int32_t>(frame_start_ms / 20u) % 32, 0);
// FPS 计数
++frame_count;
const uint32_t now = time_source.get_time_ms();
if (now - last_fps_time >= 1000u)
@@ -184,15 +196,14 @@ int main(int argc, char* argv[])
}
std::snprintf(fps_text, sizeof(fps_text), "FPS: %d", fps);
ctx.draw_text(
font,
4,
4,
RenderData::Color(0, 255, 80, 255),
RenderData::Color(0, 0, 0, 200),
fps_text);
ctx.draw_text(font, 4, 4, fpsColor, fpsBg, fps_text);
std::snprintf(perf_text, sizeof(perf_text), "Frame:%ums Present:%ums", last_frame_ms, last_present_ms);
ctx.draw_text(font, 4, 4 + font.char_h, fpsColor, fpsBg, perf_text);
const uint32_t present_start_ms = time_source.get_time_ms();
ctx.present(display);
last_present_ms = time_source.get_time_ms() - present_start_ms;
last_frame_ms = time_source.get_time_ms() - frame_start_ms;
SleepRemainingFrameTime(timer, time_source);
}