1 /*
2 
3 Copyright (C) 2015-2018 Night Dive Studios, LLC.
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 /*
20  * $Source: n:/project/lib/src/2d/RCS/genfl8c.c $
21  * $Revision: 1.1 $
22  * $Author: kevin $
23  * $Date: 1994/03/15 13:15:15 $
24  *
25  * Generic flat8 clut bitmap routines.
26  *
27  * $Log: genfl8c.c $
28  * Revision 1.1  1994/03/15  13:15:15  kevin
29  * Initial revision
30  *
31  */
32 
33 #include "scrdat.h"
34 #include "bitmap.h"
35 #include "clpcon.h"
36 #include "cnvdat.h"
37 #include "grcbm.h"
38 #include "grpix.h"
39 
40 /* bozo flat8 bitmap drawer. */
gen_flat8_clut_ubitmap(grs_bitmap * bm,short x,short y,uchar * cl)41 void gen_flat8_clut_ubitmap (grs_bitmap *bm, short x, short y, uchar *cl)
42 {
43    uchar *src  = bm->bits;
44    short right = x+bm->w;
45    short bot   = y+bm->h;
46    short cur_x;
47 
48    if (cl==NULL) cl=grd_screen->clut;
49    if (bm->flags & BMF_TRANS) {
50       for ( ; y<bot; y++, src+=bm->row-bm->w) {
51          for (cur_x=x ; cur_x<right; cur_x++, src++)
52             if (*src)
53                gr_set_upixel (cl[*src], cur_x, y);
54       }
55    }
56    else {
57       for ( ; y<bot; y++, src+=bm->row-bm->w) {
58          for (cur_x=x ; cur_x<right; cur_x++, src++)
59             gr_set_upixel (cl[*src], cur_x, y);
60       }
61    }
62 }
63 
64 /* clip flat8 bitmap against cliprect and jump to unclipped drawer. */
gen_flat8_clut_bitmap(grs_bitmap * bm,short x,short y,uchar * cl)65 int gen_flat8_clut_bitmap (grs_bitmap *bm, short x, short y, uchar *cl)
66 {
67    short w,h;
68    uchar *p;
69    int extra;
70    int code = CLIP_NONE;
71 
72    /* save stuff that clipping changes. */
73    w = bm->w; h = bm->h; p = bm->bits;
74 
75    /* check for trivial reject. */
76    if (x+bm->w<grd_clip.left || x>=grd_clip.right ||
77        y+bm->h<grd_clip.top  || y>=grd_clip.bot)
78       return CLIP_ALL;
79 
80    /* clip & draw that sucker. */
81    if (x < grd_clip.left) {            /* off left edge */
82       extra = grd_clip.left - x;
83       bm->w -= extra;
84       bm->bits += extra;
85       x = grd_clip.left;
86       code |= CLIP_LEFT;
87    }
88    if (x+bm->w > grd_clip.right) {     /* off right edge */
89       bm->w -= x+bm->w-grd_clip.right;
90       code |= CLIP_RIGHT;
91    }
92    if (y < grd_clip.top) {             /* off top */
93       extra = grd_clip.top - y;
94       bm->h -= extra;
95       bm->bits += bm->row*extra;
96       y = grd_clip.top;
97       code |= CLIP_TOP;
98    }
99    if (y+bm->h > grd_clip.bot) {      /* off bottom */
100       bm->h -= y+bm->h-grd_clip.bot;
101       code |= CLIP_BOT;
102    }
103    gr_flat8_clut_ubitmap (bm, x, y, cl);
104 
105    /* restore bitmap to normal. */
106    bm->w = w; bm->h = h; bm->bits = p;
107    return code;
108 }
109