deepstone

diff src/mglrast.c @ 25:5ff8ce78059a

first pass at converting the rasterizer to fixed point
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 22 Sep 2013 02:21:30 +0300
parents bce78aaafc68
children 11d14f688485
line diff
     1.1 --- a/src/mglrast.c	Sat Sep 21 20:18:28 2013 +0300
     1.2 +++ b/src/mglrast.c	Sun Sep 22 02:21:30 2013 +0300
     1.3 @@ -1,20 +1,3 @@
     1.4 -/*
     1.5 -256-color 3D graphics hack for real-mode DOS.
     1.6 -Copyright (C) 2011  John Tsiombikas <nuclear@member.fsf.org>
     1.7 -
     1.8 -This program is free software: you can redistribute it and/or modify
     1.9 -it under the terms of the GNU General Public License as published by
    1.10 -the Free Software Foundation, either version 3 of the License, or
    1.11 -(at your option) any later version.
    1.12 -
    1.13 -This program is distributed in the hope that it will be useful,
    1.14 -but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 -GNU General Public License for more details.
    1.17 -
    1.18 -You should have received a copy of the GNU General Public License
    1.19 -along with this program.  If not, see <http://www.gnu.org/licenses/>.
    1.20 -*/
    1.21  #include <stdio.h>
    1.22  #include <stdlib.h>
    1.23  #include <string.h>
    1.24 @@ -24,7 +7,13 @@
    1.25  #include "mglimpl.h"
    1.26  
    1.27  
    1.28 -static struct vertex *vleft, *vright;
    1.29 +#ifdef RAST_FLOAT
    1.30 +typedef struct vertex VERTEX;
    1.31 +#else
    1.32 +typedef struct fixed_vertex VERTEX;
    1.33 +#endif
    1.34 +
    1.35 +static VERTEX *vleft, *vright;
    1.36  static struct framebuffer *fb;
    1.37  static struct state *st;
    1.38  
    1.39 @@ -102,9 +91,10 @@
    1.40  #undef SCAN_LINE
    1.41  
    1.42  
    1.43 -static void (*scan_edge)(struct vertex*, struct vertex*);
    1.44 +static void (*scan_edge)(VERTEX*, VERTEX*);
    1.45  static void (*scan_line)(int, unsigned char*);
    1.46  
    1.47 +
    1.48  int mgl_rast_init(struct state *state, struct framebuffer *fbuf)
    1.49  {
    1.50  	fb = fbuf;
    1.51 @@ -132,7 +122,7 @@
    1.52  
    1.53  void mgl_rast_prepare(void)
    1.54  {
    1.55 -	static void (*sedge[])(struct vertex*, struct vertex*) = {
    1.56 +	static void (*sedge[])(VERTEX*, VERTEX*) = {
    1.57  							/* tez */
    1.58  		scan_edge_flat,		/* 000 */
    1.59  		scan_edge_z,		/* 001 */
    1.60 @@ -190,6 +180,7 @@
    1.61  
    1.62  void mgl_draw_poly(struct vertex *v, int numv)
    1.63  {
    1.64 +#ifdef RAST_FLOAT
    1.65  	int ybeg, yend, i;
    1.66  	unsigned char *sline;
    1.67  
    1.68 @@ -215,4 +206,37 @@
    1.69  		scan_line(i, sline);
    1.70  		sline += fb->width;
    1.71  	}
    1.72 +#else
    1.73 +	int ybeg, yend, i;
    1.74 +	unsigned char *sline;
    1.75 +
    1.76 +	ybeg = fb->height;
    1.77 +	yend = 0;
    1.78 +
    1.79 +	for(i=0; i<numv; i++) {
    1.80 +		int y;
    1.81 +		struct vertex *v0 = v + i;
    1.82 +		struct vertex *v1 = v + (i + 1) % numv;
    1.83 +		struct fixed_vertex vx0, vx1;
    1.84 +
    1.85 +		vertex_to_fixedvertex(*v0, vx0);
    1.86 +		vertex_to_fixedvertex(*v1, vx1);
    1.87 +
    1.88 +		y = fixed_round(vx0.pos.y);
    1.89 +
    1.90 +		scan_edge(&vx0, &vx1);
    1.91 +
    1.92 +		if(y > yend) yend = y;
    1.93 +		if(y < ybeg) ybeg = y;
    1.94 +	}
    1.95 +
    1.96 +	if(ybeg < 0) ybeg = 0;
    1.97 +	if(yend >= fb->height) yend = fb->height - 1;
    1.98 +
    1.99 +	sline = fb->pixels + ybeg * fb->width;
   1.100 +	for(i=ybeg; i<yend; i++) {
   1.101 +		scan_line(i, sline);
   1.102 +		sline += fb->width;
   1.103 +	}
   1.104 +#endif
   1.105  }