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: r:/prj/lib/src/2d/RCS/genfl8.c $
21  * $Revision: 1.7 $
22  * $Author: kevin $
23  * $Date: 1994/11/01 11:45:21 $
24  *
25  * Generic flat 8 bitmap routines.
26  *
27  * $Log: genfl8.c $
28  * Revision 1.7  1994/11/01  11:45:21  kevin
29  * Don't try to draw bitmaps with height or width of zero.
30  *
31  * Revision 1.6  1994/08/16  13:06:23  kevin
32  * gen_flat8_bitmap chains to gr_bitmap instead of gr_flat8_bitmap
33  * so it may now be used with translucent bitmaps as well.
34  *
35  * Revision 1.5  1993/10/19  09:51:11  kaboom
36  * Replaced #include "grd.h" with new headers split from grd.h.
37  *
38  * Revision 1.4  1993/10/02  01:17:22  kaboom
39  * Changed include of clip.h to include of clpcon.h and/or clpfcn.h.
40  *
41  * Revision 1.3  1993/06/04  10:28:04  kaboom
42  * Inlined clipping code so clipped bitmap can be called from an
43  * interrupt service routine.
44  *
45  * Revision 1.2  1993/04/29  18:40:27  kaboom
46  * Changed include of gr.h to smaller more specific grxxx.h.
47  *
48  * Revision 1.1  1993/02/16  15:42:37  kaboom
49  * Initial revision
50  *
51  ********************************************************************
52  * Log from old general.c:
53  *
54  * Revision 1.9  1992/12/14  18:13:37  kaboom
55  * Fixed bug in gen_flat8_bitmap -- was checking result of clip before
56  * doing the clip.
57  *
58  * Revision 1.7  1992/12/11  13:58:09  kaboom
59  * Changed all the calls from gr_set_pixel to gr_set_upixel in primitives
60  * that have analytic clipping.  Changed chain in gen_mono_bitmap()
61  * from general to specific (e.g., gr_ubitmap->gr_mono_ubitmap).
62  */
63 
64 #include "bitmap.h"
65 #include "clpcon.h"
66 #include "cnvdat.h"
67 #include "grdbm.h"
68 #include "grpix.h"
69 
70 #include <stdio.h> // printf()
71 
72 /* bozo flat8 bitmap drawer. */
gen_flat8_ubitmap(grs_bitmap * bm,short x,short y)73 void gen_flat8_ubitmap (grs_bitmap *bm, short x, short y)
74 {
75    uchar *src  = bm->bits;
76    short right = x+bm->w;
77    short bot   = y+bm->h;
78    short cur_x;
79 
80    if (bm->flags & BMF_TRANS) {
81       for ( ; y<bot; y++, src+=bm->row-bm->w) {
82          for (cur_x=x ; cur_x<right; cur_x++, src++)
83             if (*src)
84                gr_set_upixel (*src, cur_x, y);
85       }
86    }
87    else {
88       printf("gen_flat8_ubitmap %i %i\n", bm->row, bm->w);
89       for ( ; y<bot; y++, src+=bm->row-bm->w) {
90          for (cur_x=x ; cur_x<right; cur_x++, src++)
91             gr_set_upixel (*src, cur_x, y);
92       }
93    }
94 }
95 
96 /* clip flat8 bitmap against cliprect and jump to unclipped drawer. */
gen_flat8_bitmap(grs_bitmap * bm,short x,short y)97 int gen_flat8_bitmap (grs_bitmap *bm, short x, short y)
98 {
99    short w,h;
100    uchar *p;
101    int extra;
102    int code = CLIP_NONE;
103 
104    /* save stuff that clipping changes. */
105    w = bm->w; h = bm->h; p = bm->bits;
106 
107    /* check for trivial reject. */
108    if (x+bm->w<grd_clip.left || x>=grd_clip.right ||
109        y+bm->h<grd_clip.top  || y>=grd_clip.bot) {
110       return CLIP_ALL;
111    }
112 
113    /* clip & draw that sucker. */
114    if (x < grd_clip.left) {            /* off left edge */
115       extra = grd_clip.left - x;
116       bm->w -= extra;
117       bm->bits += extra;
118       x = grd_clip.left;
119       code |= CLIP_LEFT;
120    }
121    if (x+bm->w > grd_clip.right) {     /* off right edge */
122       bm->w -= x+bm->w-grd_clip.right;
123       code |= CLIP_RIGHT;
124    }
125    if (y < grd_clip.top) {             /* off top */
126       extra = grd_clip.top - y;
127       bm->h -= extra;
128       bm->bits += bm->row*extra;
129       y = grd_clip.top;
130       code |= CLIP_TOP;
131    }
132    if (y+bm->h > grd_clip.bot) {      /* off bottom */
133       bm->h -= y+bm->h-grd_clip.bot;
134       code |= CLIP_BOT;
135    }
136    if ((bm->h>0)&&(bm->w>0))
137       gr_ubitmap (bm, x, y);
138 
139    /* restore bitmap to normal. */
140    bm->w = w; bm->h = h; bm->bits = p;
141    return code;
142 }
143