sprite.cpp

Go to the documentation of this file.
00001 /* $Id: sprite.cpp 23740 2012-01-03 21:32:51Z rubidium $ */
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 "sprite.h"
00014 #include "viewport_func.h"
00015 #include "landscape.h"
00016 #include "spritecache.h"
00017 #include "zoom_func.h"
00018 
00019 
00030 void DrawCommonTileSeq(const TileInfo *ti, const DrawTileSprites *dts, TransparencyOption to, int32 orig_offset, uint32 newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
00031 {
00032   bool parent_sprite_encountered = false;
00033   const DrawTileSeqStruct *dtss;
00034   bool skip_childs = false;
00035   foreach_draw_tile_seq(dtss, dts->seq) {
00036     SpriteID image = dtss->image.sprite;
00037     PaletteID pal = dtss->image.pal;
00038 
00039     if (skip_childs) {
00040       if (!dtss->IsParentSprite()) continue;
00041       skip_childs = false;
00042     }
00043 
00044     /* TTD sprite 0 means no sprite */
00045     if ((GB(image, 0, SPRITE_WIDTH) == 0 && !HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) ||
00046         (IsInvisibilitySet(to) && !HasBit(image, SPRITE_MODIFIER_OPAQUE))) {
00047       skip_childs = dtss->IsParentSprite();
00048       continue;
00049     }
00050 
00051     image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
00052     if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
00053 
00054     pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
00055 
00056     if (dtss->IsParentSprite()) {
00057       parent_sprite_encountered = true;
00058       AddSortableSpriteToDraw(
00059         image, pal,
00060         ti->x + dtss->delta_x, ti->y + dtss->delta_y,
00061         dtss->size_x, dtss->size_y,
00062         dtss->size_z, ti->z + dtss->delta_z,
00063         !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(to)
00064       );
00065     } else {
00066       int offs_x = child_offset_is_unsigned ? (uint8)dtss->delta_x : dtss->delta_x;
00067       int offs_y = child_offset_is_unsigned ? (uint8)dtss->delta_y : dtss->delta_y;
00068       bool transparent = !HasBit(image, SPRITE_MODIFIER_OPAQUE) && IsTransparencySet(to);
00069       if (parent_sprite_encountered) {
00070         AddChildSpriteScreen(image, pal, offs_x, offs_y, transparent);
00071       } else {
00072         if (transparent) {
00073           SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
00074           pal = PALETTE_TO_TRANSPARENT;
00075         }
00076         DrawGroundSprite(image, pal, NULL, offs_x, offs_y);
00077       }
00078     }
00079   }
00080 }
00081 
00092 void DrawCommonTileSeqInGUI(int x, int y, const DrawTileSprites *dts, int32 orig_offset, uint32 newgrf_offset, PaletteID default_palette, bool child_offset_is_unsigned)
00093 {
00094   const DrawTileSeqStruct *dtss;
00095   Point child_offset = {0, 0};
00096 
00097   bool skip_childs = false;
00098   foreach_draw_tile_seq(dtss, dts->seq) {
00099     SpriteID image = dtss->image.sprite;
00100     PaletteID pal = dtss->image.pal;
00101 
00102     if (skip_childs) {
00103       if (!dtss->IsParentSprite()) continue;
00104       skip_childs = false;
00105     }
00106 
00107     /* TTD sprite 0 means no sprite */
00108     if (GB(image, 0, SPRITE_WIDTH) == 0 && !HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE)) {
00109       skip_childs = dtss->IsParentSprite();
00110       continue;
00111     }
00112 
00113     image += (HasBit(image, SPRITE_MODIFIER_CUSTOM_SPRITE) ? newgrf_offset : orig_offset);
00114     if (HasBit(pal, SPRITE_MODIFIER_CUSTOM_SPRITE)) pal += newgrf_offset;
00115 
00116     pal = SpriteLayoutPaletteTransform(image, pal, default_palette);
00117 
00118     if (dtss->IsParentSprite()) {
00119       Point pt = RemapCoords(dtss->delta_x, dtss->delta_y, dtss->delta_z);
00120       DrawSprite(image, pal, x + UnScaleByZoom(pt.x, ZOOM_LVL_GUI), y + UnScaleByZoom(pt.y, ZOOM_LVL_GUI));
00121 
00122       const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
00123       child_offset.x = UnScaleByZoom(pt.x + spr->x_offs, ZOOM_LVL_GUI);
00124       child_offset.y = UnScaleByZoom(pt.y + spr->y_offs, ZOOM_LVL_GUI);
00125     } else {
00126       int offs_x = child_offset_is_unsigned ? (uint8)dtss->delta_x : dtss->delta_x;
00127       int offs_y = child_offset_is_unsigned ? (uint8)dtss->delta_y : dtss->delta_y;
00128       DrawSprite(image, pal, x + child_offset.x + offs_x, y + child_offset.y + offs_y);
00129     }
00130   }
00131 }