shithub: tinygl

Download patch

ref: c8908cb16fdcd055e404a5f4d59ae507eb65e50d
parent: bf72bd3e49c0b313115f1635b6c17dd2c21f1f05
author: David <gek@katherine>
date: Thu Feb 18 09:20:30 EST 2021

Bloat reduction update

--- a/README.md
+++ b/README.md
@@ -109,8 +109,10 @@
 */
 
 //Next, open a framebuffer.
-ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0, 0, 0, 0);
-//Tell TinyGL to init on that framebuffer
+//The "0" parameter is where you pass in a framebuffer pointer if you've already made one.
+ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0);
+
+//Tell TinyGL to initialize on that framebuffer
 glInit(frameBuffer);
 
 //Begin making OpenGL calls!
--- a/SDL_Examples/gears.c
+++ b/SDL_Examples/gears.c
@@ -370,7 +370,8 @@
 		return 1;
 		break;
 	}
-	ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0, 0, 0, 0);
+	ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0);
+	if(!frameBuffer){printf("\nZB_open failed!");exit(1);}
 	glInit(frameBuffer);
 
 	// initialize GL:
--- a/SDL_Examples/helloworld.c
+++ b/SDL_Examples/helloworld.c
@@ -200,7 +200,7 @@
 		return 1;
 		break;
 	}
-	ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0, 0, 0, 0);
+	ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0);
 	glInit(frameBuffer);
 
 	// initialize GL:
--- a/SDL_Examples/menu.c
+++ b/SDL_Examples/menu.c
@@ -242,7 +242,7 @@
 		return 1;
 		break;
 	}
-	ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0, 0, 0, 0);
+	ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0);
 	glInit(frameBuffer);
 
 	// initialize GL:
--- a/SDL_Examples/model.c
+++ b/SDL_Examples/model.c
@@ -318,7 +318,7 @@
 		break;
 	}
 
-	ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0, 0, 0, 0);
+	ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0);
 	glInit(frameBuffer);
 
 	srand(time(NULL));
--- a/SDL_Examples/texture.c
+++ b/SDL_Examples/texture.c
@@ -189,7 +189,7 @@
 		return 1;
 		break;
 	}
-	ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0, 0, 0, 0);
+	ZBuffer* frameBuffer = ZB_open(winSizeX, winSizeY, mode, 0);
 	glInit(frameBuffer);
 
 	// initialize GL:
--- a/include/zbuffer.h
+++ b/include/zbuffer.h
@@ -221,10 +221,10 @@
     GLint depth_write;
 	/* point size*/
     GLfloat pointsize;
-    
-    GLint nb_colors;
-    unsigned char *dctable;
-    GLint *ctable;
+
+//    GLint nb_colors;
+//    unsigned char *dctable;
+//    GLint *ctable;
     PIXEL *current_texture;
     /* opengl polygon stipple*/
     GLuint dostipple;
@@ -246,9 +246,9 @@
 /* zbuffer.c */
 
 ZBuffer *ZB_open(int xsize,int ysize,int mode,
-		 GLint nb_colors,
-		 unsigned char *color_indexes,
-		 GLint *color_table,
+//		 GLint nb_colors,
+//		 unsigned char *color_indexes,
+//		 GLint *color_table,
 		 void *frame_buffer);
 
 
@@ -262,12 +262,13 @@
 
 /* zdither.c */
 
+/*
 void ZB_initDither(ZBuffer *zb,GLint nb_colors,
 		   unsigned char *color_indexes,GLint *color_table);
 void ZB_closeDither(ZBuffer *zb);
 void ZB_ditherFrameBuffer(ZBuffer *zb,unsigned char *dest,
 			  GLint linesize);
-
+*/
 /* zline.c */
 
 void ZB_plot(ZBuffer *zb,ZBufferPoint *p);
--- a/src/Makefile
+++ b/src/Makefile
@@ -2,7 +2,7 @@
 
 OBJS= api.o list.o vertex.o init.o matrix.o texture.o \
       misc.o clear.o light.o clip.o select.o get.o \
-      zbuffer.o zline.o zdither.o ztriangle.o \
+      zbuffer.o zline.o ztriangle.o \
       zmath.o image_util.o msghandling.o \
       arrays.o specbuf.o memory.o ztext.o
 ifdef TINYGL_USE_GLX
--- a/src/zbuffer.c
+++ b/src/zbuffer.c
@@ -10,7 +10,9 @@
 //#include "../include/GL/gl.h"
 #include "../include/zbuffer.h"
 
-ZBuffer* ZB_open(GLint xsize, GLint ysize, GLint mode, GLint nb_colors, GLubyte* color_indexes, GLint* color_table, void* frame_buffer) {
+ZBuffer* ZB_open(GLint xsize, GLint ysize, GLint mode,
+// GLint nb_colors, GLubyte* color_indexes, GLint* color_table, 
+ void* frame_buffer) {
 	ZBuffer* zb;
 	GLint size;
 
@@ -24,20 +26,23 @@
 	zb->linesize = (xsize * PSZB + 3) & ~3;
 
 	switch (mode) {
-#ifdef TGL_FEATURE_8_BITS
+#if TGL_FEATURE_8_BITS ==1 
 	case ZB_MODE_INDEX:
 		ZB_initDither(zb, nb_colors, color_indexes, color_table);
 		break;
 #endif
-#ifdef TGL_FEATURE_32_BITS
-	case ZB_MODE_RGBA:
+#if TGL_FEATURE_32_BITS == 1
+	case ZB_MODE_RGBA:	break;
 #endif
-#ifdef TGL_FEATURE_24_BITS
-	case ZB_MODE_RGB24:
+#if TGL_FEATURE_24_BITS == 1
+	case ZB_MODE_RGB24:	break;
 #endif
+#if TGL_FEATURE_16_BITS == 1
 	case ZB_MODE_5R6G5B:
-		zb->nb_colors = 0;
-		break;
+//		zb->nb_colors = 0;
+	break;
+#endif
+
 	default:
 		goto error;
 	}
@@ -69,7 +74,7 @@
 }
 
 void ZB_close(ZBuffer* zb) {
-#ifdef TGL_FEATURE_8_BITS
+#if TGL_FEATURE_8_BITS == 1
 	if (zb->mode == ZB_MODE_INDEX)
 		ZB_closeDither(zb);
 #endif
--- a/src/zdither.c
+++ /dev/null
@@ -1,154 +1,0 @@
-/*
- * Highly optimised dithering 16 bits -> 8 bits.
- * The formulas were taken in Mesa (Bob Mercier mercier@hollywood.cinenet.net).
- */
-
-#include "../include/zbuffer.h"
-#include <assert.h>
-//#include <stdio.h>
-#include <stdlib.h>
-
-#if defined(TGL_FEATURE_8_BITS)
-
-#define _R 5
-#define _G 9
-#define _B 5
-#define _DX 4
-#define _DY 4
-#define _D (_DX * _DY)
-#define _MIX(r, g, b) (((g) << 6) | ((b) << 3) | (r))
-
-#define DITHER_TABLE_SIZE (1 << 15)
-
-#define DITHER_INDEX(r, g, b) ((b) + (g)*_B + (r) * (_B * _G))
-
-#define MAXC 256
-static GLint kernel8[_DY * _DX] = {
-	0 * MAXC, 8 * MAXC,  2 * MAXC, 10 * MAXC, 12 * MAXC, 4 * MAXC, 14 * MAXC, 6 * MAXC,
-	3 * MAXC, 11 * MAXC, 1 * MAXC, 9 * MAXC,  15 * MAXC, 7 * MAXC, 13 * MAXC, 5 * MAXC,
-};
-
-/* we build the color table and the lookup table */
-
-void ZB_initDither(ZBuffer* zb, GLint nb_colors, GLubyte* color_indexes, GLint* color_table) {
-	GLint c, r, g, b, i, index, r1, g1, b1;
-
-	if (nb_colors < (_R * _G * _B)) {
-		//		tgl_fixme("zdither: not enough colors\n");
-		exit(1);
-	}
-
-	for (i = 0; i < nb_colors; i++)
-		color_table[i] = 0;
-
-	zb->nb_colors = nb_colors;
-	zb->ctable = gl_malloc(nb_colors * sizeof(GLint));
-
-	for (r = 0; r < _R; r++) {
-		for (g = 0; g < _G; g++) {
-			for (b = 0; b < _B; b++) {
-				r1 = (r * 255) / (_R - 1);
-				g1 = (g * 255) / (_G - 1);
-				b1 = (b * 255) / (_B - 1);
-				index = DITHER_INDEX(r, g, b);
-				c = (r1 << 16) | (g1 << 8) | b1;
-				zb->ctable[index] = c;
-				color_table[index] = c;
-			}
-		}
-	}
-
-	zb->dctable = gl_malloc(DITHER_TABLE_SIZE);
-
-	for (i = 0; i < DITHER_TABLE_SIZE; i++) {
-		r = (i >> 12) & 0x7;
-		g = (i >> 8) & 0xF;
-		b = (i >> 3) & 0x7;
-		index = DITHER_INDEX(r, g, b);
-		zb->dctable[i] = color_indexes[index];
-	}
-}
-
-void ZB_closeDither(ZBuffer* zb) {
-	gl_free(zb->ctable);
-	gl_free(zb->dctable);
-}
-
-#if 0
-int ZDither_lookupColor(GLint r,GLint g,GLint b)
-{
-  GLubyte *ctable=zdither_color_table;
-  return ctable[_MIX(_DITH0(_R, r), _DITH0(_G, g),_DITH0(_B, b))];
-}
-#endif
-
-#define DITHER_PIXEL2(a)                                                                                                                                       \
-	{                                                                                                                                                          \
-		register GLint v, t, r, g, c;                                                                                                                          \
-		v = *(GLuint*)(pp + (a));                                                                                                                              \
-		g = (v & 0x07DF07DF) + g_d;                                                                                                                            \
-		r = (((v & 0xF800F800) >> 2) + r_d) & 0x70007000;                                                                                                      \
-		t = r | g;                                                                                                                                             \
-		c = ctable[t & 0xFFFF] | (ctable[t >> 16] << 8);                                                                                                       \
-		*(GLushort*)(dest + (a)) = c;                                                                                                                          \
-	}
-
-/* NOTE: all the memory access are 16 bit aligned, so if buf or
-   linesize are not multiple of 2, it cannot work efficiently (or
-   hang!) */
-
-void ZB_ditherFrameBuffer(ZBuffer* zb, GLubyte* buf, GLint linesize) {
-	GLint xk, yk, x, y, c1, c2;
-	GLubyte* dest1;
-	GLushort* pp1;
-	GLint r_d, g_d, b_d;
-	GLubyte* ctable = zb->dctable;
-	register GLubyte* dest;
-	register GLushort* pp;
-
-	assert(((long)buf & 1) == 0 && (linesize & 1) == 0);
-
-	for (yk = 0; yk < 4; yk++) {
-		for (xk = 0; xk < 4; xk += 2) {
-#if BYTE_ORDER == BIG_ENDIAN
-			c1 = kernel8[yk * 4 + xk + 1];
-			c2 = kernel8[yk * 4 + xk];
-#else
-			c1 = kernel8[yk * 4 + xk];
-			c2 = kernel8[yk * 4 + xk + 1];
-#endif
-			r_d = ((c1 << 2) & 0xF800) >> 2;
-			g_d = (c1 >> 4) & 0x07C0;
-			b_d = (c1 >> 9) & 0x001F;
-
-			r_d |= (((c2 << 2) & 0xF800) >> 2) << 16;
-			g_d |= ((c2 >> 4) & 0x07C0) << 16;
-			b_d |= ((c2 >> 9) & 0x001F) << 16;
-			g_d = b_d | g_d;
-
-			dest1 = buf + (yk * linesize) + xk;
-			// NOTE BY GEK: The following line was modified to fix a compiler warning,
-			// casting zb->pbuf to GLushort*
-			pp1 = (GLushort*)(zb->pbuf) + (yk * zb->xsize) + xk;
-
-			for (y = yk; y < zb->ysize; y += 4) {
-				dest = dest1;
-				pp = pp1;
-				for (x = xk; x < zb->xsize; x += 16) {
-
-					DITHER_PIXEL2(0);
-					DITHER_PIXEL2(1 * 4);
-					DITHER_PIXEL2(2 * 4);
-					DITHER_PIXEL2(3 * 4);
-
-					pp += 16;
-					dest += 16;
-				}
-				dest1 += linesize * 4;
-				pp1 += zb->xsize * 4;
-			}
-		}
-	}
-}
-
-#endif
--- /dev/null
+++ b/src/zdither.c.unused
@@ -1,0 +1,154 @@
+/*
+ * Highly optimised dithering 16 bits -> 8 bits.
+ * The formulas were taken in Mesa (Bob Mercier mercier@hollywood.cinenet.net).
+ */
+
+#include "../include/zbuffer.h"
+#include <assert.h>
+//#include <stdio.h>
+#include <stdlib.h>
+
+#if defined(TGL_FEATURE_8_BITS)
+
+#define _R 5
+#define _G 9
+#define _B 5
+#define _DX 4
+#define _DY 4
+#define _D (_DX * _DY)
+#define _MIX(r, g, b) (((g) << 6) | ((b) << 3) | (r))
+
+#define DITHER_TABLE_SIZE (1 << 15)
+
+#define DITHER_INDEX(r, g, b) ((b) + (g)*_B + (r) * (_B * _G))
+
+#define MAXC 256
+static GLint kernel8[_DY * _DX] = {
+	0 * MAXC, 8 * MAXC,  2 * MAXC, 10 * MAXC, 12 * MAXC, 4 * MAXC, 14 * MAXC, 6 * MAXC,
+	3 * MAXC, 11 * MAXC, 1 * MAXC, 9 * MAXC,  15 * MAXC, 7 * MAXC, 13 * MAXC, 5 * MAXC,
+};
+
+/* we build the color table and the lookup table */
+
+void ZB_initDither(ZBuffer* zb, GLint nb_colors, GLubyte* color_indexes, GLint* color_table) {
+	GLint c, r, g, b, i, index, r1, g1, b1;
+
+	if (nb_colors < (_R * _G * _B)) {
+		//		tgl_fixme("zdither: not enough colors\n");
+		exit(1);
+	}
+
+	for (i = 0; i < nb_colors; i++)
+		color_table[i] = 0;
+
+	zb->nb_colors = nb_colors;
+	zb->ctable = gl_malloc(nb_colors * sizeof(GLint));
+
+	for (r = 0; r < _R; r++) {
+		for (g = 0; g < _G; g++) {
+			for (b = 0; b < _B; b++) {
+				r1 = (r * 255) / (_R - 1);
+				g1 = (g * 255) / (_G - 1);
+				b1 = (b * 255) / (_B - 1);
+				index = DITHER_INDEX(r, g, b);
+				c = (r1 << 16) | (g1 << 8) | b1;
+				zb->ctable[index] = c;
+				color_table[index] = c;
+			}
+		}
+	}
+
+	zb->dctable = gl_malloc(DITHER_TABLE_SIZE);
+
+	for (i = 0; i < DITHER_TABLE_SIZE; i++) {
+		r = (i >> 12) & 0x7;
+		g = (i >> 8) & 0xF;
+		b = (i >> 3) & 0x7;
+		index = DITHER_INDEX(r, g, b);
+		zb->dctable[i] = color_indexes[index];
+	}
+}
+
+void ZB_closeDither(ZBuffer* zb) {
+	gl_free(zb->ctable);
+	gl_free(zb->dctable);
+}
+
+#if 0
+int ZDither_lookupColor(GLint r,GLint g,GLint b)
+{
+  GLubyte *ctable=zdither_color_table;
+  return ctable[_MIX(_DITH0(_R, r), _DITH0(_G, g),_DITH0(_B, b))];
+}
+#endif
+
+#define DITHER_PIXEL2(a)                                                                                                                                       \
+	{                                                                                                                                                          \
+		register GLint v, t, r, g, c;                                                                                                                          \
+		v = *(GLuint*)(pp + (a));                                                                                                                              \
+		g = (v & 0x07DF07DF) + g_d;                                                                                                                            \
+		r = (((v & 0xF800F800) >> 2) + r_d) & 0x70007000;                                                                                                      \
+		t = r | g;                                                                                                                                             \
+		c = ctable[t & 0xFFFF] | (ctable[t >> 16] << 8);                                                                                                       \
+		*(GLushort*)(dest + (a)) = c;                                                                                                                          \
+	}
+
+/* NOTE: all the memory access are 16 bit aligned, so if buf or
+   linesize are not multiple of 2, it cannot work efficiently (or
+   hang!) */
+
+void ZB_ditherFrameBuffer(ZBuffer* zb, GLubyte* buf, GLint linesize) {
+	GLint xk, yk, x, y, c1, c2;
+	GLubyte* dest1;
+	GLushort* pp1;
+	GLint r_d, g_d, b_d;
+	GLubyte* ctable = zb->dctable;
+	register GLubyte* dest;
+	register GLushort* pp;
+
+	assert(((long)buf & 1) == 0 && (linesize & 1) == 0);
+
+	for (yk = 0; yk < 4; yk++) {
+		for (xk = 0; xk < 4; xk += 2) {
+#if BYTE_ORDER == BIG_ENDIAN
+			c1 = kernel8[yk * 4 + xk + 1];
+			c2 = kernel8[yk * 4 + xk];
+#else
+			c1 = kernel8[yk * 4 + xk];
+			c2 = kernel8[yk * 4 + xk + 1];
+#endif
+			r_d = ((c1 << 2) & 0xF800) >> 2;
+			g_d = (c1 >> 4) & 0x07C0;
+			b_d = (c1 >> 9) & 0x001F;
+
+			r_d |= (((c2 << 2) & 0xF800) >> 2) << 16;
+			g_d |= ((c2 >> 4) & 0x07C0) << 16;
+			b_d |= ((c2 >> 9) & 0x001F) << 16;
+			g_d = b_d | g_d;
+
+			dest1 = buf + (yk * linesize) + xk;
+			// NOTE BY GEK: The following line was modified to fix a compiler warning,
+			// casting zb->pbuf to GLushort*
+			pp1 = (GLushort*)(zb->pbuf) + (yk * zb->xsize) + xk;
+
+			for (y = yk; y < zb->ysize; y += 4) {
+				dest = dest1;
+				pp = pp1;
+				for (x = xk; x < zb->xsize; x += 16) {
+
+					DITHER_PIXEL2(0);
+					DITHER_PIXEL2(1 * 4);
+					DITHER_PIXEL2(2 * 4);
+					DITHER_PIXEL2(3 * 4);
+
+					pp += 16;
+					dest += 16;
+				}
+				dest1 += linesize * 4;
+				pp1 += zb->xsize * 4;
+			}
+		}
+	}
+}
+
+#endif