1 /* 2 Copyright (c) 2013 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 PNG_WRITER_H 29 #define PNG_WRITER_H 30 31 #include "SDL_thread.h" 32 33 #include <fstream> 34 #include <vector> 35 36 class PNGWriter { 37 protected: 38 std::ostream* out; 39 size_t components; 40 41 void init(); 42 public: 43 PNGWriter(int components = 3); 44 45 bool open(const std::string& filename); 46 void close(); 47 48 void setOutputStream(std::ostream* out); 49 50 void screenshot(const std::string& filename); 51 void capture(std::vector<char>& buffer); 52 void writePNG(std::vector<char>& buffer); 53 }; 54 55 enum png_exporter_state { PNG_EXPORTER_WAIT, PNG_EXPORTER_WRITE, PNG_EXPORTER_EXIT, PNG_EXPORTER_STOPPED }; 56 57 class PNGExporter { 58 protected: 59 PNGWriter writer; 60 61 std::vector<char>* buffer_shared_ptr; 62 63 std::vector<char> buffer1; 64 std::vector<char> buffer2; 65 66 SDL_cond* cond; 67 SDL_mutex* mutex; 68 SDL_Thread* thread; 69 70 int thread_state; 71 72 std::string filename; 73 74 static int startThread(void *exporter); 75 public: 76 PNGExporter(const std::string& filename); 77 ~PNGExporter(); 78 79 void run(); 80 void stop(); 81 82 void capture(); 83 }; 84 85 class PNGExporterException : public std::exception { 86 protected: 87 std::string filename; 88 public: PNGExporterException(const std::string & filename)89 PNGExporterException(const std::string& filename) : filename(filename) {}; 90 ~PNGExporterException()91 virtual ~PNGExporterException() throw () {}; what()92 virtual const char* what() const throw() { return filename.c_str(); }; 93 }; 94 95 #endif 96