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/fl8ply.c $
21  * $Revision: 1.1 $
22  * $Author: kevin $
23  * $Date: 1994/08/16 13:11:09 $
24  *
25  * Routines for drawing flat shaded polygons onto a flat 8 canvas.
26  *
27  * This file is part of the 2d library.
28  */
29 
30 #include "cnvdat.h"
31 #include "gente.h"
32 #include "lg.h"
33 #include "poly.h"
34 #include "tlucdat.h"
35 #include "tmapint.h"
36 #include <stdint.h>
37 #include <string.h>
38 
39 // prototypes
40 int gri_poly_loop(grs_tmap_loop_info *ti);
41 void gri_solid_poly_init(grs_tmap_loop_info *ti);
42 void gri_poly_init(grs_tmap_loop_info *ti);
43 void gri_clut_poly_init(grs_tmap_loop_info *ti);
44 void gri_tpoly_init(grs_tmap_loop_info *ti);
45 void gri_clut_tpoly_init(grs_tmap_loop_info *ti);
46 
gri_poly_loop(grs_tmap_loop_info * ti)47 int gri_poly_loop(grs_tmap_loop_info *ti) {
48     int d;
49     uchar c = (uchar)(intptr_t)(ti->bm.bits); /* actually, fill_parm */
50     uchar *bm_bits = ti->bm.bits;
51     uchar *ti_d = ti->d;
52     fix ti_left_x = ti->left.x;
53     fix ti_right_x = ti->right.x;
54     uchar *ti_clut = ti->clut;
55     uchar ti_hlog = ti->bm.hlog;
56     ushort grow = grd_bm.row;
57     fix ti_left_dx = ti->left.dx;
58     fix ti_right_dx = ti->right.dx;
59 
60     do {
61         if ((d = fix_cint(ti_right_x) - fix_cint(ti_left_x)) > 0) {
62             int x;
63 
64             switch (ti_hlog) {
65             case GRL_OPAQUE:
66                 LG_memset(ti_d + fix_cint(ti_left_x), c, d);
67                 break;
68             case GRL_TLUC8:
69                 for (x = fix_cint(ti_left_x); x < fix_cint(ti_right_x); x++)
70                     ti_d[x] = bm_bits[ti_d[x]];
71                 break;
72             case GRL_CLUT | GRL_TLUC8:
73                 for (x = fix_cint(ti_left_x); x < fix_cint(ti_right_x); x++)
74                     ti_d[x] = ti_clut[bm_bits[ti_d[x]]];
75                 break;
76             }
77         } else if (d < 0) {
78             return TRUE;
79         }
80         /* update span extrema and destination. */
81         ti_d += grow;
82         ti_left_x += ti_left_dx;
83         ti_right_x += ti_right_dx;
84     } while ((--(ti->n)) > 0);
85 
86     ti->d = ti_d;
87     ti->right.x = ti_right_x;
88     ti->left.x = ti_left_x;
89     return FALSE;
90 }
91 
gri_solid_poly_init(grs_tmap_loop_info * ti)92 void gri_solid_poly_init(grs_tmap_loop_info *ti) {
93     ti->bm.bits = ti->clut; /* set fill_parm */
94     ti->bm.hlog = GRL_OPAQUE;
95     ti->d = ti->y * grd_bm.row + grd_bm.bits;
96     ti->loop_func = (void (*)())gri_poly_loop;
97     ti->top_edge_func = (void (*)())gri_x_edge;
98     ti->bot_edge_func = (void (*)())gri_x_edge;
99 }
100 
gri_poly_init(grs_tmap_loop_info * ti)101 void gri_poly_init(grs_tmap_loop_info *ti) {
102     ti->bm.hlog = GRL_OPAQUE;
103     ti->d = ti->y * grd_bm.row + grd_bm.bits;
104     ti->loop_func = (void (*)())gri_poly_loop;
105     ti->top_edge_func = (void (*)())gri_x_edge;
106     ti->bot_edge_func = (void (*)())gri_x_edge;
107 }
108 
gri_clut_poly_init(grs_tmap_loop_info * ti)109 void gri_clut_poly_init(grs_tmap_loop_info *ti) {
110     ti->bm.bits = (uchar *)(intptr_t)ti->clut[(intptr_t)ti->bm.bits];
111     ti->bm.hlog = GRL_OPAQUE;
112     ti->d = ti->y * grd_bm.row + grd_bm.bits;
113     ti->loop_func = (void (*)())gri_poly_loop;
114     ti->top_edge_func = (void (*)())gri_x_edge;
115     ti->bot_edge_func = (void (*)())gri_x_edge;
116 }
117 
gri_tpoly_init(grs_tmap_loop_info * ti)118 void gri_tpoly_init(grs_tmap_loop_info *ti) {
119     if (tluc8tab[(intptr_t)(ti->bm.bits)] != NULL) {
120         ti->bm.bits = tluc8tab[(intptr_t)ti->bm.bits];
121         ti->bm.hlog = GRL_TLUC8;
122     } else {
123         ti->bm.hlog = GRL_OPAQUE;
124     }
125     ti->d = ti->y * grd_bm.row + grd_bm.bits;
126     ti->loop_func = (void (*)())gri_poly_loop;
127     ti->top_edge_func = (void (*)())gri_x_edge;
128     ti->bot_edge_func = (void (*)())gri_x_edge;
129 }
130 
gri_clut_tpoly_init(grs_tmap_loop_info * ti)131 void gri_clut_tpoly_init(grs_tmap_loop_info *ti) {
132     // DebugString("gri_clut_tpoly_init");
133     if (tluc8tab[(intptr_t)(ti->bm.bits)] != NULL) {
134         ti->bm.bits = tluc8tab[(intptr_t)ti->bm.bits];
135         ti->bm.hlog = GRL_TLUC8 | GRL_CLUT;
136     } else {
137         ti->bm.bits = (uchar *)(intptr_t)ti->clut[(intptr_t)ti->bm.bits];
138         ti->bm.hlog = GRL_OPAQUE;
139     }
140     ti->d = ti->y * grd_bm.row + grd_bm.bits;
141     ti->loop_func = (void (*)())gri_poly_loop;
142     ti->top_edge_func = (void (*)())gri_x_edge;
143     ti->bot_edge_func = (void (*)())gri_x_edge;
144 }
145