simple surface drawing
This commit is contained in:
parent
39feecded0
commit
d521c06c34
1 changed files with 19 additions and 13 deletions
20
main.cpp
20
main.cpp
|
@ -2,13 +2,9 @@
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <chrono>
|
|
||||||
#include <functional>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
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)> {
|
||||||
|
@ -73,6 +69,13 @@ int main() {
|
||||||
auto* window_surface = SDL_GetWindowSurface(window);
|
auto* window_surface = SDL_GetWindowSurface(window);
|
||||||
if (!window_surface) { return EXIT_FAILURE; }
|
if (!window_surface) { return EXIT_FAILURE; }
|
||||||
if (window_surface->format->palette) { return EXIT_FAILURE; }
|
if (window_surface->format->palette) { return EXIT_FAILURE; }
|
||||||
|
std::println(
|
||||||
|
"got surface with {} bits per pixel",
|
||||||
|
window_surface->format->BitsPerPixel
|
||||||
|
);
|
||||||
|
if (!(window_surface->format->format & SDL_PIXELFORMAT_RGBA32)) {
|
||||||
|
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[] = {
|
const SDL_Point directions[] = {
|
||||||
|
@ -81,17 +84,20 @@ int main() {
|
||||||
|
|
||||||
std::mt19937_64 rne(std::random_device{}());
|
std::mt19937_64 rne(std::random_device{}());
|
||||||
std::uniform_int_distribution dist(0, 3);
|
std::uniform_int_distribution dist(0, 3);
|
||||||
|
// aarrggbb??
|
||||||
|
constexpr Uint32 walk_color = 0xff00ff00;
|
||||||
|
|
||||||
for (bool continu = true; continu;) {
|
for (bool continu = true; continu;) {
|
||||||
|
|
||||||
{
|
|
||||||
SDL_Point newpoint;
|
SDL_Point newpoint;
|
||||||
do {
|
do {
|
||||||
newpoint = pos + directions[dist(rne)];
|
newpoint = pos + directions[dist(rne)];
|
||||||
} while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or
|
} while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or
|
||||||
newpoint.y >= height);
|
newpoint.y >= height);
|
||||||
pos = newpoint;
|
pos = newpoint;
|
||||||
}
|
SDL_LockSurface(window_surface);
|
||||||
|
static_cast<Uint32*>(window_surface->pixels
|
||||||
|
)[pos.x + pos.y * window_surface->h] = walk_color;
|
||||||
|
SDL_UnlockSurface(window_surface);
|
||||||
|
|
||||||
for (SDL_Event e; SDL_PollEvent(&e);) {
|
for (SDL_Event e; SDL_PollEvent(&e);) {
|
||||||
if (e.type == SDL_QUIT) { continu = false; }
|
if (e.type == SDL_QUIT) { continu = false; }
|
||||||
|
|
Loading…
Reference in a new issue