27 {
28 SDL_Surface *loaded_surface =
mxvk::LoadPNG(png_path.c_str());
29 if (loaded_surface == nullptr) {
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.