1 /************************************************************************
2  *                                                                      *
3  *  FreeSynd - a remake of the classic Bullfrog game "Syndicate".       *
4  *                                                                      *
5  *   Copyright (C) 2005  Stuart Binge  <skbinge@gmail.com>              *
6  *   Copyright (C) 2005  Joost Peters  <joostp@users.sourceforge.net>   *
7  *   Copyright (C) 2006  Trent Waddington <qg@biodome.org>              *
8  *                                                                      *
9  *    This program is free software;  you can redistribute it and / or  *
10  *  modify it  under the  terms of the  GNU General  Public License as  *
11  *  published by the Free Software Foundation; either version 2 of the  *
12  *  License, or (at your option) any later version.                     *
13  *                                                                      *
14  *    This program is  distributed in the hope that it will be useful,  *
15  *  but WITHOUT  ANY WARRANTY;  without even  the implied  warranty of  *
16  *  MERCHANTABILITY  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
17  *  General Public License for more details.                            *
18  *                                                                      *
19  *    You can view the GNU  General Public License, online, at the GNU  *
20  *  project's  web  site;  see <http://www.gnu.org/licenses/gpl.html>.  *
21  *  The full text of the license is also included in the file COPYING.  *
22  *                                                                      *
23  ************************************************************************/
24 
25 #include <stdio.h>
26 #include <string.h>
27 #include <assert.h>
28 
29 #include "tile.h"
30 #include "gfx/screen.h"
31 
32 
Tile(uint8 id_set,uint8 * tile_Data,bool not_alpha,EType type_set)33 Tile::Tile(uint8 id_set, uint8 *tile_Data, bool not_alpha, EType type_set)
34 {
35     i_id_ = id_set;
36     e_type_ = type_set;
37     a_pixels_ = new uint8[TILE_WIDTH * TILE_HEIGHT];
38     memcpy(a_pixels_, tile_Data, TILE_WIDTH * TILE_HEIGHT);
39     not_alpha_ = not_alpha;
40 }
41 
~Tile()42 Tile::~Tile()
43 {
44     delete[] a_pixels_;
45 }
46 
drawTo(uint8 * screen,int swidth,int sheight,int x,int y)47 bool Tile::drawTo(uint8 * screen, int swidth, int sheight, int x, int y)
48 {
49     if (x + TILE_WIDTH < 0 || y + TILE_HEIGHT < 0
50         || x >= swidth || y >= sheight)
51     {
52         return false;
53     }
54 
55     int xlow = x < 0 ? 0 : x;
56     int clipped_w = TILE_WIDTH - (xlow - x);
57     int xhigh = xlow + clipped_w >= swidth ? swidth : xlow + clipped_w;
58     int ylow = y < 0 ? 0 : y;
59     int clipped_h = TILE_HEIGHT - (ylow - y);
60     int yhigh = ylow + clipped_h >= sheight ? sheight : ylow + clipped_h;
61 
62     uint8 *ptr_a_pixels = a_pixels_ + ((TILE_HEIGHT - 1) - (ylow - y)) * TILE_WIDTH;
63     uint8 *ptr_screen = screen + ylow * swidth + xlow;
64     for (int j = ylow; j < yhigh; ++j)
65     {
66         uint8 *cp_ptr_a_pixels = ptr_a_pixels;
67         ptr_a_pixels -= TILE_WIDTH;
68         uint8 *cp_ptr_screen = ptr_screen;
69         ptr_screen += swidth;
70         for (int i = xlow; i < xhigh; ++i) {
71             uint8 c = *cp_ptr_a_pixels++;
72             if (c != 255)
73                 *cp_ptr_screen = c;
74             ++cp_ptr_screen;
75         }
76     }
77     return true;
78 }
79 
drawToScreen(int x,int y)80 bool Tile::drawToScreen(int x, int y)
81 {
82     return drawTo((uint8*) g_Screen.pixels(), g_Screen.gameScreenWidth(), g_Screen.gameScreenHeight(), x, y);
83 }
84