Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <ssb.h>
- bool save_tga(const char* filename, unsigned width, unsigned height, bool has_alpha, const unsigned char* data){
- // Create TGA header
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wnarrowing"
- unsigned char tga_header[18] = {
- 0, // ID length = no ID
- 0, // Color map type = no color map (=true colors)
- 2, // Image type = uncompressed true-color
- 0, 0, // Color map offset = 0 bytes
- 0, 0, // Color map length = 0 bytes
- 0, // Color map BPP = 0 bits
- 0, 0, 0, 0, // Image origin = 0/0
- width & 0xff, (width >> 8) & 0xff, // Image width = width parameter
- height & 0xff, (height >> 8) & 0xff, // Image height = height parameter
- has_alpha ? 32 : 24, // Pixel depth = has_alpha parameter
- has_alpha ? 8 : 0 // Image descriptor = bottom-up + has alpha with 8 bits (has_alpha parameter)?
- };
- #pragma GCC diagnostic pop
- // Open output file
- std::ofstream file(filename, std::ios::binary);
- if(!file)
- // Couldn't create image
- return false;
- // Write header in output file
- file.write(reinterpret_cast<const char*>(tga_header), sizeof(tga_header));
- // Write data in output file
- file.write(reinterpret_cast<const char*>(data), has_alpha ? (width * height) << 2 : width * height * 3);
- // Image creation successed
- return true;
- }
- int main(){
- // Create BGRA image
- const int width = 704, height = 396;
- unsigned char data[(width * height) << 2] = {0};
- // Fill BGRA data
- for(unsigned long i = 0; i < sizeof(data); i+=4){
- // Half transparent green
- data[i] = 0;
- data[i+1] = 255;
- data[i+2] = 0;
- data[i+3] = 127;
- }
- // Render on image with SSB
- char warning[SSB_WARNING_LENGTH];
- ssb_renderer renderer = ssb_create_renderer(width, height, SSB_BGRA, "test.ssb", warning);
- if(renderer){
- ssb_render(renderer, data, width << 2, 2000);
- ssb_free_renderer(renderer);
- }else{
- puts(warning);
- return 1;
- }
- // Save image as TGA file
- if(!save_tga("ssb_test.tga", width, height, true, data)){
- puts("Couldn't create image!");
- return 1;
- }
- return 0;
- }
- /*
- g++ -c main.cpp -o main.o
- g++ -o SSB_Test main.o -lSSBRenderer
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement