splash.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../../stdafx.h"
00013 #include "../../openttd.h"
00014 #include "../../debug.h"
00015 #include "../../gfx_func.h"
00016 #include "../../fileio_func.h"
00017 #include "../../blitter/factory.hpp"
00018 #include "../../core/mem_func.hpp"
00019 
00020 #include "splash.h"
00021 
00022 #ifdef WITH_PNG
00023 
00024 #include <png.h>
00025 
00026 static void PNGAPI png_my_error(png_structp png_ptr, png_const_charp message)
00027 {
00028   DEBUG(misc, 0, "[libpng] error: %s - %s", message, (char *)png_get_error_ptr(png_ptr));
00029   longjmp(png_jmpbuf(png_ptr), 1);
00030 }
00031 
00032 static void PNGAPI png_my_warning(png_structp png_ptr, png_const_charp message)
00033 {
00034   DEBUG(misc, 1, "[libpng] warning: %s - %s", message, (char *)png_get_error_ptr(png_ptr));
00035 }
00036 
00037 void DisplaySplashImage()
00038 {
00039   FILE *f = FioFOpenFile(SPLASH_IMAGE_FILE);
00040   if (f == NULL) return;
00041 
00042   png_byte header[8];
00043   fread(header, sizeof(png_byte), 8, f);
00044   if (png_sig_cmp(header, 0, 8) != 0) {
00045     fclose(f);
00046     return;
00047   }
00048 
00049   png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp) NULL, png_my_error, png_my_warning);
00050 
00051   if (png_ptr == NULL) {
00052     fclose(f);
00053     return;
00054   }
00055 
00056   png_infop info_ptr = png_create_info_struct(png_ptr);
00057   if (info_ptr == NULL) {
00058     png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
00059     fclose(f);
00060     return;
00061   }
00062 
00063   png_infop end_info = png_create_info_struct(png_ptr);
00064   if (end_info == NULL) {
00065     png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
00066     fclose(f);
00067     return;
00068   }
00069 
00070   if (setjmp(png_jmpbuf(png_ptr))) {
00071     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
00072     fclose(f);
00073     return;
00074   }
00075 
00076   png_init_io(png_ptr, f);
00077   png_set_sig_bytes(png_ptr, 8);
00078 
00079   png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
00080 
00081   uint width      = png_get_image_width(png_ptr, info_ptr);
00082   uint height     = png_get_image_height(png_ptr, info_ptr);
00083   uint bit_depth  = png_get_bit_depth(png_ptr, info_ptr);
00084   uint color_type = png_get_color_type(png_ptr, info_ptr);
00085 
00086   if (color_type != PNG_COLOR_TYPE_PALETTE || bit_depth != 8) {
00087     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
00088     fclose(f);
00089     return;
00090   }
00091 
00092   if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_PLTE)) {
00093     png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
00094     fclose(f);
00095     return;
00096   }
00097 
00098   png_colorp palette;
00099   int num_palette;
00100   png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
00101 
00102   png_bytep *row_pointers = png_get_rows(png_ptr, info_ptr);
00103 
00104   if (width > (uint) _screen.width) width = _screen.width;
00105   if (height > (uint) _screen.height) height = _screen.height;
00106 
00107   uint xoff = (_screen.width - width) / 2;
00108   uint yoff = (_screen.height - height) / 2;
00109 
00110   switch (BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth()) {
00111     case 8: {
00112         uint8 *dst_ptr = (uint8 *)_screen.dst_ptr;
00113         /* Initialize buffer */
00114         MemSetT(dst_ptr, 0xff, _screen.pitch * _screen.height);
00115 
00116         for (uint y = 0; y < height; y++) {
00117           uint8 *src = row_pointers[y];
00118           uint8 *dst = dst_ptr + (yoff + y) * _screen.pitch + xoff;
00119 
00120           memcpy(dst, src, width);
00121         }
00122 
00123         for (int i = 0; i < num_palette; i++) {
00124           _cur_palette[i].a = i == 0 ? 0 : 0xff;
00125           _cur_palette[i].r = palette[i].red;
00126           _cur_palette[i].g = palette[i].green;
00127           _cur_palette[i].b = palette[i].blue;
00128         }
00129 
00130         _cur_palette[0xff].a = 0xff;
00131         _cur_palette[0xff].r = 0;
00132         _cur_palette[0xff].g = 0;
00133         _cur_palette[0xff].b = 0;
00134 
00135         _pal_first_dirty = 0;
00136         _pal_count_dirty = 256;
00137         break;
00138       }
00139     case 32: {
00140         uint32 *dst_ptr = (uint32 *)_screen.dst_ptr;
00141         /* Initialize buffer */
00142         MemSetT(dst_ptr, 0, _screen.pitch * _screen.height);
00143 
00144         for (uint y = 0; y < height; y++) {
00145           uint8 *src = row_pointers[y];
00146           uint32 *dst = dst_ptr + (yoff + y) * _screen.pitch + xoff;
00147 
00148           for (uint x = 0; x < width; x++) {
00149             dst[x] = palette[src[x]].blue | (palette[src[x]].green << 8) | (palette[src[x]].red << 16) | 0xff000000;
00150           }
00151         }
00152         break;
00153       }
00154   }
00155 
00156   png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
00157   fclose(f);
00158   return;
00159 }
00160 
00161 
00162 
00163 #else /* WITH_PNG */
00164 
00165 void DisplaySplashImage() {}
00166 
00167 #endif /* WITH_PNG */

Generated on Sun Jun 5 04:20:00 2011 for OpenTTD by  doxygen 1.6.1