MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
defender Namespace Reference

Classes

struct  Asteroid
class  DefenderWindow
struct  SpriteAlphaBounds
struct  Ufo

Enumerations

enum class  GameMode { Intro , IntroFadeIn , Countdown , Playing }
enum class  UfoSpriteSet : std::size_t { Classic = 0 , Ufox = 1 , Alien = 2 }

Functions

SpriteAlphaBounds calculate_alpha_bounds (SDL_Surface *surface, std::uint8_t threshold)
SDL_Surface * load_light_background_png (const std::string &path)
glm::vec3 nearest_world_position (glm::vec3 position, float origin_x)
float nearest_world_x (float target, float origin)
float terrain_height (float world_x)
float wrap_world_x (float x)
float wrapped_delta_x (float target, float origin)

Variables

constexpr float CAMERA_DISTANCE = 26.0f
constexpr float CAMERA_HEIGHT = 1.6f
constexpr float CAMERA_SCROLL_EDGE_FRACTION = 0.68f
constexpr float CONTROLLER_AXIS_MAX = 32767.0f
constexpr Sint16 CONTROLLER_DEAD_ZONE = 8000
constexpr int FIRE_COOLDOWN = 4
constexpr int GAME_VIEWPORT_TOP = RADAR_TOP + RADAR_HEIGHT + 8
constexpr int MAX_ASTEROIDS = 5
constexpr int MAX_PROJECTILES = 64
constexpr int MAX_UFOS = 6
constexpr glm::vec4 PROJECTILE_COLOR {1.0f, 0.96f, 0.60f, 1.0f}
constexpr float PROJECTILE_LIFETIME = 0.72f
constexpr float PROJECTILE_SPEED = 118.0f
constexpr int RADAR_HEIGHT = 104
constexpr int RADAR_TOP = 42
constexpr float SHIP_MODEL_SCALE = 1.65f
constexpr int STAR_COUNT = 90000
constexpr int UFO_ANIMATION_FRAMES = 8
constexpr int UFO_SPRITE_SET_COUNT = 3
constexpr float WORLD_BOTTOM = -8.5f
constexpr float WORLD_HALF_WIDTH = WORLD_WIDTH * 0.5f
constexpr float WORLD_TOP = 8.5f
constexpr float WORLD_WIDTH = 160.0f

Enumeration Type Documentation

◆ GameMode

enum class defender::GameMode
strong
Enumerator
Intro 
IntroFadeIn 
Countdown 
Playing 

Definition at line 12 of file defender_entities.hpp.

◆ UfoSpriteSet

enum class defender::UfoSpriteSet : std::size_t
strong
Enumerator
Classic 
Ufox 
Alien 

Definition at line 19 of file defender_entities.hpp.

Function Documentation

◆ calculate_alpha_bounds()

SpriteAlphaBounds defender::calculate_alpha_bounds ( SDL_Surface * surface,
std::uint8_t threshold )
nodiscard

Definition at line 18 of file defender_assets.cpp.

18 {
19 if (surface == nullptr || surface->w <= 0 || surface->h <= 0) {
20 return {};
21 }
22
23 const SDL_PixelFormatDetails *format_details = SDL_GetPixelFormatDetails(surface->format);
24 if (format_details == nullptr || !SDL_LockSurface(surface)) {
25 return {};
26 }
27
28 int min_x = surface->w;
29 int min_y = surface->h;
30 int max_x = -1;
31 int max_y = -1;
32 const auto *bytes = static_cast<const std::uint8_t *>(surface->pixels);
33
34 for (int y = 0; y < surface->h; ++y) {
35 const auto *row = reinterpret_cast<const std::uint32_t *>(bytes + static_cast<std::size_t>(y) * static_cast<std::size_t>(surface->pitch));
36 for (int x = 0; x < surface->w; ++x) {
37 std::uint8_t r = 0;
38 std::uint8_t g = 0;
39 std::uint8_t b = 0;
40 std::uint8_t a = 0;
41 SDL_GetRGBA(row[x], format_details, nullptr, &r, &g, &b, &a);
42 if (a <= threshold) {
43 continue;
44 }
45 min_x = std::min(min_x, x);
46 min_y = std::min(min_y, y);
47 max_x = std::max(max_x, x);
48 max_y = std::max(max_y, y);
49 }
50 }
51
52 SDL_UnlockSurface(surface);
53
54 if (max_x < min_x || max_y < min_y) {
55 return {};
56 }
57
58 const float width = static_cast<float>(surface->w);
59 const float height = static_cast<float>(surface->h);
60 return {
61 static_cast<float>(min_x) / width - 0.5f,
62 static_cast<float>(max_x + 1) / width - 0.5f,
63 0.5f - static_cast<float>(max_y + 1) / height,
64 0.5f - static_cast<float>(min_y) / height,
65 };
66 }

◆ load_light_background_png()

SDL_Surface * defender::load_light_background_png ( const std::string & path)
nodiscard

Definition at line 68 of file defender_assets.cpp.

68 {
69 SDL_Surface *loaded_surface = mxvk::LoadPNG(path.c_str());
70 if (loaded_surface == nullptr) {
71 throw mxvk::Exception("Failed to load PNG: " + path);
72 }
73
74 SDL_Surface *surface = SDL_ConvertSurface(loaded_surface, SDL_PIXELFORMAT_RGBA32);
75 SDL_DestroySurface(loaded_surface);
76 if (surface == nullptr) {
77 throw mxvk::Exception("Failed to convert PNG to RGBA: " + path);
78 }
79
80 const SDL_PixelFormatDetails *format_details = SDL_GetPixelFormatDetails(surface->format);
81 if (format_details == nullptr) {
82 SDL_DestroySurface(surface);
83 throw mxvk::Exception("Failed to query pixel format details for: " + path);
84 }
85
86 if (!SDL_LockSurface(surface)) {
87 SDL_DestroySurface(surface);
88 throw mxvk::Exception("Failed to lock PNG surface: " + path);
89 }
90
91 auto *pixels = static_cast<std::uint32_t *>(surface->pixels);
92 const int width = surface->w;
93 const int height = surface->h;
94 const int pixel_count = width * height;
95 std::vector<std::uint8_t> background(static_cast<std::size_t>(pixel_count), 0);
96 std::vector<int> stack;
97 stack.reserve(static_cast<std::size_t>(width + height) * 2);
98
99 const auto is_light_checker_pixel = [&](const int index) {
100 std::uint8_t r = 0;
101 std::uint8_t g = 0;
102 std::uint8_t b = 0;
103 std::uint8_t a = 0;
104 SDL_GetRGBA(pixels[index], format_details, nullptr, &r, &g, &b, &a);
105 const int min_channel = std::min({static_cast<int>(r), static_cast<int>(g), static_cast<int>(b)});
106 const int max_channel = std::max({static_cast<int>(r), static_cast<int>(g), static_cast<int>(b)});
107 return a != 0 && min_channel >= 215 && (max_channel - min_channel) <= 28;
108 };
109
110 const auto push_background = [&](const int index) {
111 if (index < 0 || index >= pixel_count || background[static_cast<std::size_t>(index)] != 0 || !is_light_checker_pixel(index)) {
112 return;
113 }
114 background[static_cast<std::size_t>(index)] = 1;
115 stack.push_back(index);
116 };
117
118 for (int x = 0; x < width; ++x) {
119 push_background(x);
120 push_background((height - 1) * width + x);
121 }
122 for (int y = 0; y < height; ++y) {
123 push_background(y * width);
124 push_background(y * width + width - 1);
125 }
126
127 while (!stack.empty()) {
128 const int index = stack.back();
129 stack.pop_back();
130 const int x = index % width;
131 const int y = index / width;
132 if (x > 0) {
133 push_background(index - 1);
134 }
135 if (x + 1 < width) {
136 push_background(index + 1);
137 }
138 if (y > 0) {
139 push_background(index - width);
140 }
141 if (y + 1 < height) {
142 push_background(index + width);
143 }
144 }
145
146 for (int i = 0; i < pixel_count; ++i) {
147 if (background[static_cast<std::size_t>(i)] == 0) {
148 continue;
149 }
150 std::uint8_t r = 0;
151 std::uint8_t g = 0;
152 std::uint8_t b = 0;
153 std::uint8_t a = 0;
154 SDL_GetRGBA(pixels[i], format_details, nullptr, &r, &g, &b, &a);
155 pixels[i] = SDL_MapRGBA(format_details, nullptr, r, g, b, 0);
156 }
157
158 SDL_UnlockSurface(surface);
159 return surface;
160 }
SDL_Surface * LoadPNG(const char *file)
Load a PNG file into an SDL_Surface.
Definition mxvk_png.cpp:103

◆ nearest_world_position()

glm::vec3 defender::nearest_world_position ( glm::vec3 position,
float origin_x )
inlinenodiscard

Definition at line 52 of file defender_types.hpp.

52 {
53 position.x = nearest_world_x(position.x, origin_x);
54 return position;
55 }
float nearest_world_x(float target, float origin)

◆ nearest_world_x()

float defender::nearest_world_x ( float target,
float origin )
inlinenodiscard

Definition at line 48 of file defender_types.hpp.

48 {
49 return origin + wrapped_delta_x(target, origin);
50 }
float wrapped_delta_x(float target, float origin)

◆ terrain_height()

float defender::terrain_height ( float world_x)
inlinenodiscard

Definition at line 57 of file defender_types.hpp.

57 {
58 constexpr float TERRAIN_SEGMENT_WIDTH = 8.0f;
59 const float wrapped_x = wrap_world_x(world_x) + WORLD_HALF_WIDTH;
60 const int segment = static_cast<int>(std::floor(wrapped_x / TERRAIN_SEGMENT_WIDTH));
61 const float segment_progress = std::fmod(wrapped_x, TERRAIN_SEGMENT_WIDTH) / TERRAIN_SEGMENT_WIDTH;
62 const auto height_for = [](int index) {
63 const uint32_t hash = static_cast<uint32_t>(index) * 1103515245U + 12345U;
64 return WORLD_BOTTOM + 1.0f + static_cast<float>((hash >> 16U) % 7U) * 0.26f;
65 };
66 return std::lerp(height_for(segment), height_for(segment + 1), segment_progress);
67 }
constexpr float WORLD_BOTTOM
float wrap_world_x(float x)
constexpr float WORLD_HALF_WIDTH

◆ wrap_world_x()

float defender::wrap_world_x ( float x)
inlinenodiscard

Definition at line 36 of file defender_types.hpp.

36 {
37 x = std::fmod(x + WORLD_HALF_WIDTH, WORLD_WIDTH);
38 if (x < 0.0f) {
39 x += WORLD_WIDTH;
40 }
41 return x - WORLD_HALF_WIDTH;
42 }
constexpr float WORLD_WIDTH

◆ wrapped_delta_x()

float defender::wrapped_delta_x ( float target,
float origin )
inlinenodiscard

Definition at line 44 of file defender_types.hpp.

44 {
45 return wrap_world_x(target - origin);
46 }

Variable Documentation

◆ CAMERA_DISTANCE

float defender::CAMERA_DISTANCE = 26.0f
constexpr

Definition at line 30 of file defender_types.hpp.

◆ CAMERA_HEIGHT

float defender::CAMERA_HEIGHT = 1.6f
constexpr

Definition at line 31 of file defender_types.hpp.

◆ CAMERA_SCROLL_EDGE_FRACTION

float defender::CAMERA_SCROLL_EDGE_FRACTION = 0.68f
constexpr

Definition at line 32 of file defender_types.hpp.

◆ CONTROLLER_AXIS_MAX

float defender::CONTROLLER_AXIS_MAX = 32767.0f
constexpr

Definition at line 34 of file defender_types.hpp.

◆ CONTROLLER_DEAD_ZONE

Sint16 defender::CONTROLLER_DEAD_ZONE = 8000
constexpr

Definition at line 33 of file defender_types.hpp.

◆ FIRE_COOLDOWN

int defender::FIRE_COOLDOWN = 4
constexpr

Definition at line 21 of file defender_types.hpp.

◆ GAME_VIEWPORT_TOP

int defender::GAME_VIEWPORT_TOP = RADAR_TOP + RADAR_HEIGHT + 8
constexpr

Definition at line 29 of file defender_types.hpp.

◆ MAX_ASTEROIDS

int defender::MAX_ASTEROIDS = 5
constexpr

Definition at line 15 of file defender_types.hpp.

◆ MAX_PROJECTILES

int defender::MAX_PROJECTILES = 64
constexpr

Definition at line 13 of file defender_types.hpp.

◆ MAX_UFOS

int defender::MAX_UFOS = 6
constexpr

Definition at line 14 of file defender_types.hpp.

◆ PROJECTILE_COLOR

glm::vec4 defender::PROJECTILE_COLOR {1.0f, 0.96f, 0.60f, 1.0f}
constexpr

Definition at line 22 of file defender_types.hpp.

22{1.0f, 0.96f, 0.60f, 1.0f};

◆ PROJECTILE_LIFETIME

float defender::PROJECTILE_LIFETIME = 0.72f
constexpr

Definition at line 20 of file defender_types.hpp.

◆ PROJECTILE_SPEED

float defender::PROJECTILE_SPEED = 118.0f
constexpr

Definition at line 19 of file defender_types.hpp.

◆ RADAR_HEIGHT

int defender::RADAR_HEIGHT = 104
constexpr

Definition at line 28 of file defender_types.hpp.

◆ RADAR_TOP

int defender::RADAR_TOP = 42
constexpr

Definition at line 27 of file defender_types.hpp.

◆ SHIP_MODEL_SCALE

float defender::SHIP_MODEL_SCALE = 1.65f
constexpr

Definition at line 18 of file defender_types.hpp.

◆ STAR_COUNT

int defender::STAR_COUNT = 90000
constexpr

Definition at line 12 of file defender_types.hpp.

◆ UFO_ANIMATION_FRAMES

int defender::UFO_ANIMATION_FRAMES = 8
constexpr

Definition at line 16 of file defender_types.hpp.

◆ UFO_SPRITE_SET_COUNT

int defender::UFO_SPRITE_SET_COUNT = 3
constexpr

Definition at line 17 of file defender_types.hpp.

◆ WORLD_BOTTOM

float defender::WORLD_BOTTOM = -8.5f
constexpr

Definition at line 24 of file defender_types.hpp.

◆ WORLD_HALF_WIDTH

float defender::WORLD_HALF_WIDTH = WORLD_WIDTH * 0.5f
constexpr

Definition at line 26 of file defender_types.hpp.

◆ WORLD_TOP

float defender::WORLD_TOP = 8.5f
constexpr

Definition at line 23 of file defender_types.hpp.

◆ WORLD_WIDTH

float defender::WORLD_WIDTH = 160.0f
constexpr

Definition at line 25 of file defender_types.hpp.