MCPcopy Create free account
hub / github.com/DFHack/dfhack / canonicalize_format

Function canonicalize_format

library/modules/Textures.cpp:70–92  ·  view source on GitHub ↗

Converts an arbitrary Surface to something like the display format (32-bit RGBA), and converts magenta to transparency if convert_magenta is set and the source surface didn't already have an alpha channel. It also deletes the source surface. It uses the same pixel format (RGBA, R at lowest address) regardless of hardware.

Source from the content-addressed store, hash-verified

68// It uses the same pixel format (RGBA, R at lowest address) regardless of
69// hardware.
70SDL_Surface* canonicalize_format(SDL_Surface* src) {
71 // even though we have null check after DFIMG_Load
72 // in loadTileset() (the only consumer of this method)
73 // it's better put nullcheck here as well
74 if (!src)
75 return src;
76
77 auto fmt = DFSDL_AllocFormat(SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA32);
78 SDL_Surface* tgt = DFSDL_ConvertSurface(src, fmt, SDL_SWSURFACE);
79 DFSDL_FreeSurface(src);
80 for (int x = 0; x < tgt->w; ++x) {
81 for (int y = 0; y < tgt->h; ++y) {
82 Uint8* p = (Uint8*)tgt->pixels + y * tgt->pitch + x * 4;
83 if (p[3] == 0) {
84 for (int c = 0; c < 3; c++) {
85 p[c] = 0;
86 }
87 }
88 }
89 }
90
91 return tgt;
92}
93
94// register surface in texture raws, get a texpos
95static long add_texture(SDL_Surface* surface) {

Callers 1

loadTilesetMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected