add error handling
code broken in fullscreen due to double(or more) buffering
This commit is contained in:
parent
5f7c070f78
commit
9a11684363
1 changed files with 45 additions and 11 deletions
56
main.cpp
56
main.cpp
|
@ -3,16 +3,44 @@
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <format>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <source_location>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
auto sdl_throw_current_error(const std::source_location location) {
|
||||||
|
throw std::runtime_error{std::format(
|
||||||
|
"error at {}:{}:{} {}", location.file_name(), location.line(),
|
||||||
|
location.column(), SDL_GetError()
|
||||||
|
)};
|
||||||
|
}
|
||||||
|
|
||||||
|
auto sdl_check(
|
||||||
|
int errc,
|
||||||
|
const std::source_location location = std::source_location::current()
|
||||||
|
) {
|
||||||
|
if (errc != 0) { sdl_throw_current_error(location); }
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure someone already owns passed sdl_obj, or memory will be leaked
|
||||||
|
auto sdl_check(
|
||||||
|
auto* sdl_obj,
|
||||||
|
const std::source_location location = std::source_location::current()
|
||||||
|
) {
|
||||||
|
if (sdl_obj == nullptr) { sdl_throw_current_error(location); }
|
||||||
|
return sdl_obj;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename SDLT, auto deletef>
|
template <typename SDLT, auto deletef>
|
||||||
struct sdl_wrapper_t : std::unique_ptr<SDLT, decltype(deletef)> {
|
struct sdl_wrapper_t : std::unique_ptr<SDLT, decltype(deletef)> {
|
||||||
explicit sdl_wrapper_t(SDLT* p)
|
explicit sdl_wrapper_t(SDLT* p)
|
||||||
: std::unique_ptr<SDLT, decltype(deletef)>{p, deletef} {}
|
: std::unique_ptr<SDLT, decltype(deletef)>{p, deletef} {
|
||||||
|
sdl_check(p);
|
||||||
|
}
|
||||||
|
|
||||||
operator SDLT*() { return std::unique_ptr<SDLT, decltype(deletef)>::get(); }
|
operator SDLT*() { return std::unique_ptr<SDLT, decltype(deletef)>::get(); }
|
||||||
};
|
};
|
||||||
|
@ -38,7 +66,7 @@ auto operator==(const SDL_Point& a, const SDL_Point& b) {
|
||||||
// constexpr int width = 1000;
|
// constexpr int width = 1000;
|
||||||
// constexpr int height = 1000;
|
// constexpr int height = 1000;
|
||||||
constexpr int width = 3440;
|
constexpr int width = 3440;
|
||||||
constexpr int height = 1300;
|
constexpr int height = 1440;
|
||||||
|
|
||||||
template <auto EF>
|
template <auto EF>
|
||||||
struct [[nodiscard("give this a name so SDL_Quit is called at the end"
|
struct [[nodiscard("give this a name so SDL_Quit is called at the end"
|
||||||
|
@ -61,7 +89,7 @@ auto poll_events(bool& continu) {
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
int main() {
|
int main() try {
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||||
std::println("fuck");
|
std::println("fuck");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
@ -70,7 +98,7 @@ int main() {
|
||||||
|
|
||||||
window_t window{SDL_CreateWindow(
|
window_t window{SDL_CreateWindow(
|
||||||
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
|
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
|
||||||
height, 0
|
height, SDL_WINDOW_FULLSCREEN
|
||||||
)};
|
)};
|
||||||
if (!window) {
|
if (!window) {
|
||||||
std::println(
|
std::println(
|
||||||
|
@ -80,19 +108,22 @@ int main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// owned by window
|
// owned by window
|
||||||
auto* window_surface = SDL_GetWindowSurface(window);
|
auto* window_surface = sdl_check(SDL_GetWindowSurface(window));
|
||||||
if (!window_surface) { return EXIT_FAILURE; }
|
if (window_surface->format->palette) {
|
||||||
if (window_surface->format->palette) { return EXIT_FAILURE; }
|
std::println("unsupported window format, palette");
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
std::println(
|
std::println(
|
||||||
"got surface with {} bits per pixel",
|
"got surface with {} bits per pixel",
|
||||||
window_surface->format->BitsPerPixel
|
window_surface->format->BitsPerPixel
|
||||||
);
|
);
|
||||||
if (!(window_surface->format->format & SDL_PIXELFORMAT_RGBA32)) {
|
if (!(window_surface->format->format & SDL_PIXELFORMAT_RGBA32)) {
|
||||||
|
std::println("unsupported window format, rgb");
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Point pos{.x = width / 2, .y = height / 2};
|
SDL_Point pos{.x = width / 2, .y = height / 2};
|
||||||
const SDL_Point directions[] = {
|
constexpr SDL_Point directions[] = {
|
||||||
{.x = -1, .y = 0}, {.x = 1, .y = 0}, {.x = 0, .y = -1}, {.x = 0, .y = 1}
|
{.x = -1, .y = 0}, {.x = 1, .y = 0}, {.x = 0, .y = -1}, {.x = 0, .y = 1}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -103,7 +134,7 @@ int main() {
|
||||||
constexpr Uint32 walk_color = 0xff0d0f14;
|
constexpr Uint32 walk_color = 0xff0d0f14;
|
||||||
constexpr auto batchsize = 1000;
|
constexpr auto batchsize = 1000;
|
||||||
|
|
||||||
SDL_LockSurface(window_surface);
|
sdl_check(SDL_LockSurface(window_surface));
|
||||||
for (int i = 0; i < window_surface->w * window_surface->h; ++i) {
|
for (int i = 0; i < window_surface->w * window_surface->h; ++i) {
|
||||||
static_cast<Uint32*>(window_surface->pixels)[i] = bg_color;
|
static_cast<Uint32*>(window_surface->pixels)[i] = bg_color;
|
||||||
}
|
}
|
||||||
|
@ -127,7 +158,7 @@ int main() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LockSurface(window_surface);
|
sdl_check(SDL_LockSurface(window_surface));
|
||||||
for (auto step = 0z; step < batchsize; ++step) {
|
for (auto step = 0z; step < batchsize; ++step) {
|
||||||
SDL_Point newpoint;
|
SDL_Point newpoint;
|
||||||
do {
|
do {
|
||||||
|
@ -139,8 +170,11 @@ int main() {
|
||||||
)[pos.x + pos.y * window_surface->w] = walk_color;
|
)[pos.x + pos.y * window_surface->w] = walk_color;
|
||||||
}
|
}
|
||||||
SDL_UnlockSurface(window_surface);
|
SDL_UnlockSurface(window_surface);
|
||||||
SDL_UpdateWindowSurface(window);
|
sdl_check(SDL_UpdateWindowSurface(window));
|
||||||
}
|
}
|
||||||
|
|
||||||
// leaking memory, nothing I can do
|
// leaking memory, nothing I can do
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::puts(e.what());
|
||||||
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
Loading…
Reference in a new issue