Advertisement
Guest User

SSB Test

a guest
Feb 4th, 2014
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <fstream>
  2. #include <ssb.h>
  3.  
  4. bool save_tga(const char* filename, unsigned width, unsigned height, bool has_alpha, const unsigned char* data){
  5.     // Create TGA header
  6. #pragma GCC diagnostic push
  7. #pragma GCC diagnostic ignored "-Wnarrowing"
  8.     unsigned char tga_header[18] = {
  9.         0, // ID length = no ID
  10.         0, // Color map type = no color map (=true colors)
  11.         2, // Image type = uncompressed true-color
  12.         0, 0, // Color map offset = 0 bytes
  13.         0, 0, // Color map length = 0 bytes
  14.         0, // Color map BPP = 0 bits
  15.         0, 0, 0, 0, // Image origin = 0/0
  16.         width & 0xff, (width >> 8) & 0xff, // Image width = width parameter
  17.         height & 0xff, (height >> 8) & 0xff, // Image height = height parameter
  18.         has_alpha ? 32 : 24, // Pixel depth = has_alpha parameter
  19.         has_alpha ? 8 : 0 // Image descriptor = bottom-up + has alpha with 8 bits (has_alpha parameter)?
  20.     };
  21. #pragma GCC diagnostic pop
  22.     // Open output file
  23.     std::ofstream file(filename, std::ios::binary);
  24.     if(!file)
  25.         // Couldn't create image
  26.         return false;
  27.     // Write header in output file
  28.     file.write(reinterpret_cast<const char*>(tga_header), sizeof(tga_header));
  29.     // Write data in output file
  30.     file.write(reinterpret_cast<const char*>(data), has_alpha ? (width * height) << 2 : width * height * 3);
  31.     // Image creation successed
  32.     return true;
  33. }
  34.  
  35. int main(){
  36.     // Create BGRA image
  37.     const int width = 704, height = 396;
  38.     unsigned char data[(width * height) << 2] = {0};
  39.     // Fill BGRA data
  40.     for(unsigned long i = 0; i < sizeof(data); i+=4){
  41.         // Half transparent green
  42.         data[i] = 0;
  43.         data[i+1] = 255;
  44.         data[i+2] = 0;
  45.         data[i+3] = 255;
  46.     }
  47.     // Render on image with SSB
  48.     char warning[SSB_WARNING_LENGTH];
  49.     ssb_renderer renderer = ssb_create_renderer(width, height, SSB_BGRA, "test.ssb", warning);
  50.     if(renderer){
  51.         ssb_render(renderer, data, width << 2, 2000);
  52.         ssb_free_renderer(renderer);
  53.     }else{
  54.         puts(warning);
  55.         return 1;
  56.     }
  57.     // Save image as TGA file
  58.     if(!save_tga("ssb_test.tga", width, height, true, data)){
  59.         puts("Couldn't create image!");
  60.         return 1;
  61.     }
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement