Skip to content

Instantly share code, notes, and snippets.

@giupo
Last active January 12, 2021 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giupo/27dcf996609659a167b8de5ccaa6d717 to your computer and use it in GitHub Desktop.
Save giupo/27dcf996609659a167b8de5ccaa6d717 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <Windows.h>
#include "SFML/Graphics.hpp"
#include "spdlog/spdlog.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "dds.hpp"
#define FALCON_TEXTURE_SHM TEXT("FalconTexturesSharedMemoryArea")
#define ACCESS_RIGHTS FILE_MAP_READ
void print_header(DirectX::DDS_HEADER* header) {
auto log = spdlog::stdout_color_mt("bmstest");
log->info("Header Size: {}", header->size);
log->info("Flags: 0x{:X}", header->flags);
log->info("width: {}", header->width);
log->info("height: {}", header->height);
log->info("pitchOrLinearSize: {}", header->pitchOrLinearSize);
log->info("pitchOrLine-Evaluated - 2: {}", ((header->width+1) >> 1) * header->ddspf.RGBBitCount / 8 );
log->info("pitchOrLine-Evaluated - 3: {}", (header->width * header->ddspf.RGBBitCount + 7 ) / 8 );
log->info("depth: {}", header->depth);
log->info("mip map count: {}", header->mipMapCount);
log->info("RGBBitCount: {}", header->ddspf.RGBBitCount);
log->info("fourCC: {}", header->ddspf.fourCC);
log->info("---- PIXELFORMAT -----");
log->info("ddspf->dwSize: {}", header->ddspf.size);
log->info("ddspf->dwFlags: 0x{:X}", header->ddspf.flags);
log->info("ddspf->dwFourCC: {}", header->ddspf.fourCC);
log->info("ddspf->dwRGBBitCount: {}", header->ddspf.RGBBitCount);
log->info("ddspf->dwRBitMask: 0x{:X}", header->ddspf.RBitMask);
log->info("ddspf->dwGBitMask: 0x{:X}", header->ddspf.GBitMask);
log->info("ddspf->dwBBitMask: 0x{:X}", header->ddspf.BBitMask);
log->info("ddspf->dwABitMask: 0x{:X}", header->ddspf.ABitMask);
}
int main(int argc, char** argv) {
constexpr sf::Uint8 offset = sizeof(DirectX::DDS_MAGIC) + sizeof(DirectX::DDS_HEADER);
HANDLE textureDataMemHandle = NULL;
textureDataMemHandle = OpenFileMapping(ACCESS_RIGHTS, FALSE, FALCON_TEXTURE_SHM);
if (textureDataMemHandle == NULL) {
std::cout << "Unable to open file. Is Falcon Running?" << std::endl;
return 1;
}
// this points to the shared memory, magic+header+data of the DDS format
sf::Uint8* header_ptr = (sf::Uint8*) MapViewOfFile(textureDataMemHandle, ACCESS_RIGHTS, 0, 0, 0);
// this points at the beginning of the texture data...
sf::Uint8* texture_data = ((sf::Uint8*) header_ptr) + offset;
DirectX::DDS_HEADER* header = (DirectX::DDS_HEADER*) (header_ptr + sizeof(DirectX::DDS_MAGIC));
// bytes per pixel are the number of RGB total bits divided number of bits per byte (8)
const int bytes_per_pixel = (int) header->ddspf.RGBBitCount / 8;
std::size_t texture_size = header->width * header->height * bytes_per_pixel;
sf::Uint8* texture_buffer = new sf::Uint8[texture_size];
print_header(header);
for(unsigned int i = 0, j = 0; j < texture_size; i += bytes_per_pixel, j += bytes_per_pixel) {
//changes from BGRA to RGBA
texture_buffer[j] = texture_data[i + 2]; // R
texture_buffer[j + 1] = texture_data[i + 1]; // G
texture_buffer[j + 2] = texture_data[i]; // B
texture_buffer[j + 3] = 0xFF; // texture_data[i + 3]; // A 0xFF
// -----> here is the trick <-----
if(j % header->width == 0 && argc > 1) {
i += 16;
}
}
sf::Texture tex;
tex.create(header->width, header->height);
tex.update(texture_buffer);
bool result = tex.copyToImage().saveToFile("texture.png");
std::cout << "Save Result: " << result << std::endl;
std::cout << "offset dim: " << (unsigned int) offset << std::endl;
std::cout << sizeof(int);
delete[] texture_buffer;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment