MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
example::anonymous_namespace{example.cpp} Namespace Reference

Classes

struct  SaucerFlight
struct  StarParticle

Functions

SDL_Surface * create_star_surface ()
SDL_Surface * load_transparent_saucer (const std::string &png_path)
float random_float (float min_value, float max_value)

Variables

constexpr float k_pi = 3.14159265358979323846f

Function Documentation

◆ create_star_surface()

SDL_Surface * example::anonymous_namespace{example.cpp}::create_star_surface ( )

Definition at line 72 of file example.cpp.

72 {
73 SDL_Surface *surface = SDL_CreateSurface(40, 40, SDL_PIXELFORMAT_RGBA32);
74 if (surface == nullptr) {
75 return nullptr;
76 }
77
78 const SDL_PixelFormatDetails *format_details = SDL_GetPixelFormatDetails(surface->format);
79 if (format_details == nullptr) {
80 SDL_DestroySurface(surface);
81 return nullptr;
82 }
83
84 if (!SDL_LockSurface(surface)) {
85 SDL_DestroySurface(surface);
86 return nullptr;
87 }
88
89 auto *pixels = static_cast<std::uint32_t *>(surface->pixels);
90 for (int y = 0; y < surface->h; ++y) {
91 for (int x = 0; x < surface->w; ++x) {
92 const float dx = (static_cast<float>(x) + 0.5f) - (static_cast<float>(surface->w) * 0.5f);
93 const float dy = (static_cast<float>(y) + 0.5f) - (static_cast<float>(surface->h) * 0.5f);
94 const float dist = std::sqrt((dx * dx) + (dy * dy)) / (static_cast<float>(surface->w) * 0.5f);
95 const float core = std::clamp(1.0f - dist * 1.9f, 0.0f, 1.0f);
96 const float glow = std::clamp(1.0f - dist * 3.8f, 0.0f, 1.0f);
97 const std::uint8_t alpha = static_cast<std::uint8_t>(std::lround((core * core * 255.0f) + (glow * 80.0f)));
98 const std::uint8_t brightness = static_cast<std::uint8_t>(std::lround((core * 255.0f) + (glow * 150.0f)));
99 pixels[y * surface->w + x] = SDL_MapRGBA(format_details, nullptr, brightness, brightness, brightness, alpha);
100 }
101 }
102
103 SDL_UnlockSurface(surface);
104 return surface;
105 }

◆ load_transparent_saucer()

SDL_Surface * example::anonymous_namespace{example.cpp}::load_transparent_saucer ( const std::string & png_path)

Definition at line 27 of file example.cpp.

27 {
28 SDL_Surface *loaded_surface = mxvk::LoadPNG(png_path.c_str());
29 if (loaded_surface == nullptr) {
30 throw mxvk::Exception("Failed to load sprite3D image: " + png_path);
31 }
32
33 SDL_Surface *rgba_surface = SDL_ConvertSurface(loaded_surface, SDL_PIXELFORMAT_RGBA32);
34 SDL_DestroySurface(loaded_surface);
35 if (rgba_surface == nullptr) {
36 throw mxvk::Exception(std::format("Failed to convert sprite3D image to RGBA: {}", png_path));
37 }
38
39 const SDL_PixelFormatDetails *format_details = SDL_GetPixelFormatDetails(rgba_surface->format);
40 if (format_details == nullptr) {
41 SDL_DestroySurface(rgba_surface);
42 throw mxvk::Exception("Failed to query pixel format details for: " + png_path);
43 }
44
45 if (!SDL_LockSurface(rgba_surface)) {
46 SDL_DestroySurface(rgba_surface);
47 throw mxvk::Exception("Failed to lock sprite3D surface: " + png_path);
48 }
49
50 auto *pixels = static_cast<std::uint32_t *>(rgba_surface->pixels);
51 const int pixel_count = rgba_surface->w * rgba_surface->h;
52 for (int i = 0; i < pixel_count; ++i) {
53 std::uint8_t r = 0;
54 std::uint8_t g = 0;
55 std::uint8_t b = 0;
56 std::uint8_t a = 0;
57 SDL_GetRGBA(pixels[i], format_details, nullptr, &r, &g, &b, &a);
58
59 if ((r | g | b) == 0) {
60 a = 0;
61 } else {
62 a = 255;
63 }
64
65 pixels[i] = SDL_MapRGBA(format_details, nullptr, r, g, b, a);
66 }
67
68 SDL_UnlockSurface(rgba_surface);
69 return rgba_surface;
70 }
SDL_Surface * LoadPNG(const char *file)
Load a PNG file into an SDL_Surface.
Definition mxvk_png.cpp:103

◆ random_float()

float example::anonymous_namespace{example.cpp}::random_float ( float min_value,
float max_value )

Definition at line 135 of file example.cpp.

135 {
136 static thread_local std::default_random_engine engine{std::random_device{}()};
137 std::uniform_real_distribution<float> dist(min_value, max_value);
138 return dist(engine);
139 }

Variable Documentation

◆ k_pi

float example::anonymous_namespace{example.cpp}::k_pi = 3.14159265358979323846f
constexpr

Definition at line 25 of file example.cpp.