remove rendering, we need software rendering

This commit is contained in:
Jakob Hördt 2024-08-03 23:06:35 +02:00
parent ae43c1c0ef
commit 39feecded0

View file

@ -36,8 +36,8 @@ auto operator==(const SDL_Point& a, const SDL_Point& b) {
return a.x == b.x && a.y == b.y;
}
constexpr int width = 3440;
constexpr int height = 1440;
constexpr int width = 1000;
constexpr int height = 1000;
template <auto EF>
struct [[nodiscard("give this a name so SDL_Quit is called at the end"
@ -60,7 +60,7 @@ int main() {
window_t window{SDL_CreateWindow(
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
height, SDL_WINDOW_FULLSCREEN
height, 0
)};
if (!window) {
std::println(
@ -70,20 +70,11 @@ int main() {
}
// owned by window
// auto* window_surface = SDL_GetWindowSurface(window);
auto* window_surface = SDL_GetWindowSurface(window);
if (!window_surface) { return EXIT_FAILURE; }
if (window_surface->format->palette) { return EXIT_FAILURE; }
renderer_t renderer{SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)
};
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
auto surface = surface_t{SDL_GetWindowSurface(window)};
std::vector<SDL_Point> points;
SDL_Point pos{.x = width / 2, .y = height / 2};
points.push_back(pos);
const SDL_Point directions[] = {
{.x = -1, .y = 0}, {.x = 1, .y = 0}, {.x = 0, .y = -1}, {.x = 0, .y = 1}
};
@ -91,11 +82,8 @@ int main() {
std::mt19937_64 rne(std::random_device{}());
std::uniform_int_distribution dist(0, 3);
bool continu = true;
const auto start = std::chrono::steady_clock::now();
auto next_frame = start;
long total_points = 0;
while (continu) {
for (bool continu = true; continu;) {
{
SDL_Point newpoint;
do {
@ -103,54 +91,13 @@ int main() {
} while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or
newpoint.y >= height);
pos = newpoint;
points.push_back(newpoint);
++total_points;
}
const auto goal = 200'000'000;
if (total_points >= goal) {
continu = false;
std::println(
"took {} to get to {} random steps",
std::chrono::duration_cast<std::chrono::duration<double>>(
std::chrono::steady_clock::now() - start
),
goal
);
}
// render present; poll events
auto render_start = std::chrono::steady_clock::now();
if (render_start > next_frame) {
next_frame += std::chrono::milliseconds{200};
{
std::ranges::sort(points, std::less<>{});
auto [first, last] = std::ranges::unique(points);
points.resize(std::ranges::distance(points.begin(), first));
}
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
SDL_RenderDrawPoints(renderer, points.data(), std::ssize(points));
SDL_RenderPresent(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_Event e;
while (SDL_PollEvent(&e)) {
for (SDL_Event e; SDL_PollEvent(&e);) {
if (e.type == SDL_QUIT) { continu = false; }
}
auto render_end = std::chrono::steady_clock::now();
std::println(
"drawn {} points in {}", std::ssize(points),
std::chrono::duration_cast<std::chrono::milliseconds>(
render_end - render_start
)
);
if (render_end > next_frame) {
next_frame = render_end + std::chrono::milliseconds{100};
}
}
SDL_UpdateWindowSurface(window);
}
// leaking memory, nothing I can do