1 /* 2 Copyright (c) 2012 Andrew Caudwell (acaudwell@gmail.com) 3 All rights reserved. 4 5 Redistribution and use in source and binary forms, with or without 6 modification, are permitted provided that the following conditions 7 are met: 8 1. Redistributions of source code must retain the above copyright 9 notice, this list of conditions and the following disclaimer. 10 2. Redistributions in binary form must reproduce the above copyright 11 notice, this list of conditions and the following disclaimer in the 12 documentation and/or other materials provided with the distribution. 13 3. The name of the author may not be used to endorse or promote products 14 derived from this software without specific prior written permission. 15 16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef TGA_WRITER_H 29 #define TGA_WRITER_H 30 31 #include "SDL_thread.h" 32 33 #include <ostream> 34 #include <fstream> 35 #include <vector> 36 #include <string> 37 38 class TGAWriter { 39 protected: 40 bool rle; 41 42 size_t components; 43 size_t rle_count; 44 size_t raw_count; 45 46 std::ostream* out; 47 48 void writeHeader(); 49 50 inline void writeRLE(int pixel_count, const char* pixel); 51 inline void writeRaw(std::vector<char>& buffer, int start, int pixel_count); 52 53 void writeScanlines(std::vector<char>& buffer, int width, int height); 54 55 void init(); 56 57 public: 58 TGAWriter(int components = 3); 59 60 bool open(const std::string& filename); 61 void close(); 62 63 void setOutputStream(std::ostream* out); 64 65 void screenshot(const std::string& filename); 66 void capture(std::vector<char>& buffer); 67 void writeTGA(std::vector<char>& buffer); 68 }; 69 70 enum tga_exporter_state { TGA_EXPORTER_WAIT, TGA_EXPORTER_WRITE, TGA_EXPORTER_EXIT, TGA_EXPORTER_STOPPED }; 71 72 class TGAExporter { 73 protected: 74 TGAWriter writer; 75 76 std::vector<char>* buffer_shared_ptr; 77 78 std::vector<char> buffer1; 79 std::vector<char> buffer2; 80 81 SDL_cond* cond; 82 SDL_mutex* mutex; 83 SDL_Thread* thread; 84 85 int thread_state; 86 87 std::string filename; 88 89 static int startThread(void *exporter); 90 public: 91 TGAExporter(const std::string& filename); 92 ~TGAExporter(); 93 94 void run(); 95 void stop(); 96 97 void capture(); 98 }; 99 100 class TGAExporterException : public std::exception { 101 protected: 102 std::string filename; 103 public: TGAExporterException(const std::string & filename)104 TGAExporterException(const std::string& filename) : filename(filename) {}; 105 ~TGAExporterException()106 virtual ~TGAExporterException() throw () {}; what()107 virtual const char* what() const throw() { return filename.c_str(); }; 108 }; 109 110 #endif 111