shithub: tinygl

Download patch

ref: 84f9ab3e4e0292fdab1aabcc5522751d8cbbb8b3
parent: 459ef75c8b3e1081d3ef9264c533553a880a2431
author: David <gek@katherine>
date: Sun Feb 14 08:18:24 EST 2021

16 bit support

--- a/README.md
+++ b/README.md
@@ -109,12 +109,23 @@
   
 ## FULLY COMPATIBLE WITH RGBA!
 
-The library is now configured properly for RGBA rendering. Note that the output *is actually ABGR* 
+The library is now able to be configured properly for RGBA rendering. Note that the output *is actually ABGR* 
 but adjusting it is easy, see the SDL examples under SDL_EXAMPLES (They require SDL 1.2 and Mixer to compile)
 
-It is a notable loss in this version of TinyGL that 16 bit color support was removed. The only supported mode is 32 bit
+The library is sometimes by default configured for RGBA or 5R6G5B, check zfeatures.h and change the values in this table:
+```c
+#define TGL_FEATURE_8_BITS         0
+#define TGL_FEATURE_24_BITS        0
+//These are the only maintained modes.
+#define TGL_FEATURE_16_BITS        1
+#define TGL_FEATURE_32_BITS        0
+```
 
-This was done to keep this library maintainable.
+make sure that only ONE of these values is 1.
+
+
+## ALSO COMPATIBLE WITH 16 BIT 
+
 
 # Here is the old description of TinyGL, saved for historical/attribution purposes:
 
--- a/src/SDL_Examples/gears.c
+++ b/src/SDL_Examples/gears.c
@@ -289,7 +289,7 @@
     }
     ainit(0);
     SDL_Surface* screen = NULL;
-    if((screen=SDL_SetVideoMode( winSizeX, winSizeY, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == 0 ) {
+    if((screen=SDL_SetVideoMode( winSizeX, winSizeY, TGL_FEATURE_RENDER_BITS, SDL_SWSURFACE)) == 0 ) {
         fprintf(stderr,"ERROR: Video mode set failed.\n");
         return 1;
     }
@@ -320,9 +320,9 @@
         return 1;
     case 16:
         pitch = screen->pitch;
-        fprintf(stderr,"\nUnsupported by maintainer!!!");
+        //fprintf(stderr,"\nUnsupported by maintainer!!!");
         mode = ZB_MODE_5R6G5B;
-        return 1;
+        //return 1;
         break;
     case 24:
         pitch = ( screen->pitch * 2 ) / 3;
@@ -397,7 +397,12 @@
         // draw scene:
         glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
         draw();
-		glDrawText((unsigned char*)"\nBlitting text\nto the screen!", 0, 0, 0x000000FF);
+        
+		glDrawText((unsigned char*)"RED text", 0, 0,   0x000000FF);
+
+		glDrawText((unsigned char*)"GREEN text", 0, 24,0x0000FF00);
+
+		glDrawText((unsigned char*)"BLUE text", 0, 48,  0x00FF0000);
         // swap buffers:
         if ( SDL_MUSTLOCK(screen) && (SDL_LockSurface(screen)<0) ) {
             fprintf(stderr, "SDL ERROR: Can't lock screen: %s\n", SDL_GetError());
@@ -410,6 +415,7 @@
 		printf("\nAMASK IS %u",screen->format->Amask);
         */
         //Quickly convert all pixels to the correct format
+#if TGL_FEATURE_RENDER_BITS == 32
         for(int i = 0; i < frameBuffer->xsize* frameBuffer->ysize;i++){
 #define DATONE (frameBuffer->pbuf[i])
 			DATONE = ((DATONE & 0x000000FF)     ) << screen->format->Rshift | 
@@ -416,6 +422,7 @@
 					 ((DATONE & 0x0000FF00) >> 8) << screen->format->Gshift |
 					 ((DATONE & 0x00FF0000) >>16) << screen->format->Bshift;
         }
+#endif
         ZB_copyFrameBuffer(frameBuffer, screen->pixels, screen->pitch);
         if ( SDL_MUSTLOCK(screen) ) SDL_UnlockSurface(screen);
         SDL_Flip(screen);
--- a/src/SDL_Examples/model.c
+++ b/src/SDL_Examples/model.c
@@ -278,7 +278,7 @@
     }
     ainit(0);
     SDL_Surface* screen = NULL;
-    if((screen=SDL_SetVideoMode( winSizeX, winSizeY, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == 0 ) {
+    if((screen=SDL_SetVideoMode( winSizeX, winSizeY, TGL_FEATURE_RENDER_BITS, SDL_SWSURFACE)) == 0 ) {
         fprintf(stderr,"ERROR: Video mode set failed.\n");
         return 1;
     }
@@ -308,11 +308,11 @@
         fprintf(stderr,"\nUnsupported by maintainer!!!");
         return 1;
     case 16:
-        pitch = screen->pitch;
-        fprintf(stderr,"\nUnsupported by maintainer!!!");
-        mode = ZB_MODE_5R6G5B;
-        return 1;
-        break;
+            pitch = screen->pitch;
+            //fprintf(stderr,"\nUnsupported by maintainer!!!");
+            mode = ZB_MODE_5R6G5B;
+            //return 1;
+            break;
     case 24:
         pitch = ( screen->pitch * 2 ) / 3;
         fprintf(stderr,"\nUnsupported by maintainer!!!");
@@ -513,6 +513,7 @@
 		printf("\nAMASK IS %u",screen->format->Amask);
         */
         //Quickly convert all pixels to the correct format
+#if TGL_FEATURE_RENDER_BITS == 32        
         for(int i = 0; i < frameBuffer->xsize* frameBuffer->ysize;i++){
 #define DATONE (frameBuffer->pbuf[i])
 			DATONE = ((DATONE & 0x000000FF)     ) << screen->format->Rshift | 
@@ -519,6 +520,7 @@
 					 ((DATONE & 0x0000FF00) >> 8) << screen->format->Gshift |
 					 ((DATONE & 0x00FF0000) >>16) << screen->format->Bshift;
         }
+#endif        
         ZB_copyFrameBuffer(frameBuffer, screen->pixels, screen->pitch);
         if ( SDL_MUSTLOCK(screen) ) SDL_UnlockSurface(screen);
         SDL_Flip(screen);
--- a/src/SDL_Examples/texture.c
+++ b/src/SDL_Examples/texture.c
@@ -137,7 +137,7 @@
     }
     ainit(0);
     SDL_Surface* screen = NULL;
-    if((screen=SDL_SetVideoMode( winSizeX, winSizeY, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == 0 ) {
+    if((screen=SDL_SetVideoMode( winSizeX, winSizeY, TGL_FEATURE_RENDER_BITS, SDL_SWSURFACE)) == 0 ) {
         fprintf(stderr,"ERROR: Video mode set failed.\n");
         return 1;
     }
@@ -167,11 +167,11 @@
         fprintf(stderr,"\nUnsupported by maintainer!!!");
         return 1;
     case 16:
-        pitch = screen->pitch;
-        fprintf(stderr,"\nUnsupported by maintainer!!!");
-        mode = ZB_MODE_5R6G5B;
-        return 1;
-        break;
+            pitch = screen->pitch;
+            //fprintf(stderr,"\nUnsupported by maintainer!!!");
+            mode = ZB_MODE_5R6G5B;
+            //return 1;
+            break;
     case 24:
         pitch = ( screen->pitch * 2 ) / 3;
         fprintf(stderr,"\nUnsupported by maintainer!!!");
@@ -258,6 +258,7 @@
 		printf("\nAMASK IS %u",screen->format->Amask);
         */
         //Quickly convert all pixels to the correct format
+#if TGL_FEATURE_RENDER_BITS == 32        
         for(int i = 0; i < frameBuffer->xsize* frameBuffer->ysize;i++){
 #define DATONE (frameBuffer->pbuf[i])
 			DATONE = ((DATONE & 0x000000FF)     ) << screen->format->Rshift | 
@@ -264,6 +265,7 @@
 					 ((DATONE & 0x0000FF00) >> 8) << screen->format->Gshift |
 					 ((DATONE & 0x00FF0000) >>16) << screen->format->Bshift;
         }
+#endif
         ZB_copyFrameBuffer(frameBuffer, screen->pixels, screen->pitch);
         if ( SDL_MUSTLOCK(screen) ) SDL_UnlockSurface(screen);
         SDL_Flip(screen);
--- a/src/zbuffer.c
+++ b/src/zbuffer.c
@@ -115,7 +115,8 @@
 }
 
 
-inline PIXEL pxReverse(PIXEL x)
+#if TGL_FEATURE_32_BITS == 1
+inline PIXEL pxReverse32(PIXEL x)
 {
 
     return
@@ -126,7 +127,9 @@
         ((x & 0x000000FF) << 24);  //BB______
     // Return value is in format:  0xBBGGRRAA
 }
+#endif
 
+
 static void ZB_copyBuffer(ZBuffer * zb,
                           void *buf,
                           int linesize)
@@ -161,6 +164,7 @@
 #if TGL_FEATURE_RENDER_BITS == 16
 
 /* 32 bpp copy */
+/*
 
 #ifdef TGL_FEATURE_32_BITS
 
@@ -174,6 +178,7 @@
     p1 = (gb >> 16) | ((v & 0xF8000000) >> 8);\
 }
 
+//This function is never called.
 static void ZB_copyFrameBufferRGB32(ZBuffer * zb,
                                     void *buf,
                                     int linesize)
@@ -184,7 +189,7 @@
 
     q = zb->pbuf;
     p1 = (unsigned int *) buf;
-	puts("\nBEING CALLED\n");
+	//puts("\nBEING CALLED\n");
     for (y = 0; y < zb->ysize; y++) {
 	p = p1;
 	n = zb->xsize >> 2;
@@ -205,7 +210,7 @@
 	p1 += linesize;
     }
 }
-
+*/
 #endif
 
 /* 24 bit packed pixel handling */
@@ -216,7 +221,7 @@
 
 /* XXX: packed pixel 24 bit support not tested */
 /* XXX: big endian case not optimised */
-
+/*
 #if BYTE_ORDER == BIG_ENDIAN
 
 #define RGB16_TO_RGB24(p0,p1,p2,v1,v2)\
@@ -254,7 +259,8 @@
 }
 
 #endif
-
+*/
+/*
 static void ZB_copyFrameBufferRGB24(ZBuffer * zb,
                                     void *buf,
                                     int linesize)
@@ -285,46 +291,57 @@
 	*((char *) p1) += linesize;
     }
 }
-
+*/
 #endif
 
+#if TGL_FEATURE_RENDER_BITS == 16
+
+
 void ZB_copyFrameBuffer(ZBuffer * zb, void *buf,
 			int linesize)
 {
-    switch (zb->mode) {
-#ifdef TGL_FEATURE_8_BITS
-    case ZB_MODE_INDEX:
-	ZB_ditherFrameBuffer(zb, buf, linesize >> 1);
-	break;
-#endif
-#ifdef TGL_FEATURE_16_BITS
-    case ZB_MODE_5R6G5B:
+	
 	ZB_copyBuffer(zb, buf, linesize);
-	break;
-#endif
-#ifdef TGL_FEATURE_32_BITS
-    case ZB_MODE_RGBA:
-	ZB_copyFrameBufferRGB32(zb, buf, linesize >> 1);
-	break;
-#endif
-#ifdef TGL_FEATURE_24_BITS
-    case ZB_MODE_RGB24:
-	ZB_copyFrameBufferRGB24(zb, buf, linesize >> 1);
-	break;
-#endif
-    default:
-	assert(0);
-    }
+		//switch (zb->mode) {
+	/*
+	#ifdef TGL_FEATURE_8_BITS
+		case ZB_MODE_INDEX:
+		ZB_ditherFrameBuffer(zb, buf, linesize >> 1);
+		break;
+	#endif
+	*/
+	/*#ifdef TGL_FEATURE_16_BITS
+		case ZB_MODE_5R6G5B:
+		ZB_copyBuffer(zb, buf, linesize);
+		break;
+	#endif*/
+
+
+	/*#ifdef TGL_FEATURE_32_BITS
+		case ZB_MODE_RGBA:
+		ZB_copyFrameBufferRGB32(zb, buf, linesize >> 1);
+		break;
+	#endif
+	*/
+	//    default:
+	//	assert(0);
+	//    }
 }
 
 #endif /* TGL_FEATURE_RENDER_BITS == 16 */
 
+
+
+
+
+/*
+
 #if TGL_FEATURE_RENDER_BITS == 24
 
 #define RGB24_TO_RGB16(r, g, b) \
   ((((r) >> 3) << 11) | (((g) >> 2) << 5) | ((b) >> 3))
 
-/* XXX: not optimized */
+// XXX: not optimized 
 static void ZB_copyFrameBuffer5R6G5B(ZBuffer * zb, 
                                      void *buf, int linesize) 
 {
@@ -369,7 +386,10 @@
     }
 }
 
-#endif /* TGL_FEATURE_RENDER_BITS == 24 */
+#endif 
+*/
+/* TGL_FEATURE_RENDER_BITS == 24 
+*/
 
 #if TGL_FEATURE_RENDER_BITS == 32
 
@@ -377,6 +397,7 @@
   (((v >> 8) & 0xf800) | (((v) >> 5) & 0x07e0) | (((v) & 0xff) >> 3))
 
 /* XXX: not optimized */
+/*
 static void ZB_copyFrameBuffer5R6G5B(ZBuffer * zb, 
                                      void *buf, int linesize) 
 {
@@ -401,16 +422,20 @@
 	p1 = (unsigned short *)((char *)p1 + linesize);
     }
 }
-
+*/
 void ZB_copyFrameBuffer(ZBuffer * zb, void *buf,
 			int linesize)
 {
-    switch (zb->mode) {
+	ZB_copyBuffer(zb, buf, linesize);
+    //switch (zb->mode) {
+		/*
 #ifdef TGL_FEATURE_16_BITS
-    case ZB_MODE_5R6G5B:
+	case ZB_MODE_5R6G5B:
 	ZB_copyFrameBuffer5R6G5B(zb, buf, linesize);
 	break;
 #endif
+		*/
+/*
 #ifdef TGL_FEATURE_32_BITS
     case ZB_MODE_RGBA:
 	ZB_copyBuffer(zb, buf, linesize);
@@ -419,6 +444,7 @@
     default:
 	assert(0);
     }
+*/
 }
 
 #endif /* TGL_FEATURE_RENDER_BITS == 32 */
@@ -427,6 +453,7 @@
 /*
  * adr must be aligned on an 'int'
  */
+ //Used in 16 bit mode
 void memset_s(void *adr, int val, int count)
 {
     int i, n, v;
@@ -451,6 +478,7 @@
 	*q++ = val;
 }
 
+//Used in 32 bit mode
 void memset_l(void *adr, int val, int count)
 {
     int i, n, v;
@@ -473,6 +501,7 @@
 }
 
 /* count must be a multiple of 4 and >= 4 */
+//Gek's note: Should never be used.
 void memset_RGB24(void *adr,int r, int v, int b,long count)
 {
     long i, n;
@@ -533,10 +562,8 @@
             color = RGB_TO_PIXEL(r, g, b);
         #endif
 	    memset_l(pp, color, zb->xsize);
-// #elif TGL_FEATURE_RENDER_BITS == 24
-            // memset_RGB24(pp,r>>8,g>>8,b>>8,zb->xsize);
 #else
-#error TODO
+#error BADJUJU
 #endif
 	    pp = (PIXEL *) ((char *) pp + zb->linesize);
 	}
--- a/src/zbuffer.h
+++ b/src/zbuffer.h
@@ -36,14 +36,24 @@
 
 #if TGL_FEATURE_RENDER_BITS == 32
 
+/* 32 bit mode */
 #define RGB_TO_PIXEL(r,g,b) ( ((b&65280)<<8) | ((g&65280)) | ((r&65280)>>8) )
 typedef unsigned int PIXEL;
 #define PSZB 4
 #define PSZSH 5
 
+#elif TGL_FEATURE_RENDER_BITS == 16
+
+/* 16 bit mode */
+#define RGB_TO_PIXEL(r,g,b) (((r) & 0xF800) | (((g) >> 5) & 0x07E0) | ((b) >> 11))
+typedef unsigned short PIXEL;
+#define PSZB 2 
+#define PSZSH 4 
+
+
 #else
 
-#error Incorrect number of bits per pixel
+#error wrong TGL_FEATURE_RENDER_BITS buddy
 
 #endif
 
--- a/src/zfeatures.h
+++ b/src/zfeatures.h
@@ -48,18 +48,29 @@
 
 /* enable various convertion code from internal pixel format (usually
    16 bits per pixel) to any external format */
-//CURRENT MAINTAINER'S NOTE: Only FEATURE_32_BITS is being maintained.
-#define TGL_FEATURE_16_BITS        0
 #define TGL_FEATURE_8_BITS         0
 #define TGL_FEATURE_24_BITS        0
-#define TGL_FEATURE_32_BITS        1
+//These are the only maintained modes.
+#define TGL_FEATURE_16_BITS        1
+#define TGL_FEATURE_32_BITS        0
 
 //MAINTAINER'S NOTE: Only TGL_FEATURE_RENDER_BITS 32 is maintained.
+//TODO: Include support for 16 bit.
 //All others are experimental.
 //24 bit is broken.
 //#define TGL_FEATURE_RENDER_BITS    15
 //#define TGL_FEATURE_RENDER_BITS    16
 //#define TGL_FEATURE_RENDER_BITS    24 //BROKEN!
+#if TGL_FEATURE_32_BITS == 1
 #define TGL_FEATURE_RENDER_BITS    32
+
+#elif TGL_FEATURE_16_BITS == 1
+#define TGL_FEATURE_RENDER_BITS    16
+
+#else
+#error Unsupported TGL_FEATURE_RENDER_BITS
+
+#endif
+
 
 #endif /* _tgl_features_h_ */
--- a/src/ztext.c
+++ b/src/ztext.c
@@ -9,7 +9,7 @@
 GLTEXTSIZE textsize = 1;
 
 void glTextSize(GLTEXTSIZE mode){textsize = mode;}//Set text size
-void renderchar(char *bitmap, int _x, int _y, PIXEL p) {
+void renderchar(char *bitmap, int _x, int _y, unsigned int p) {
     int x,y;
     int set;
     //int mask;
@@ -24,7 +24,15 @@
         }
     }
 }
-void glPlotPixel(int x, int y, PIXEL p){
+
+
+void glPlotPixel(int x, int y, unsigned int p){
+//	int x = p[1].i;
+//	int y = p[2].i;
+//	unsigned int p = p[3].ui;
+#if TGL_FEATURE_RENDER_BITS == 16
+	p = RGB_TO_PIXEL( ( (p & 255) << 8) , ( p & 65280) , ( (p >>16)<<8 ) );
+#endif
 	PIXEL* pbuf = gl_get_context()->zb->pbuf;
 	int w = gl_get_context()->zb->xsize;
 	int h = gl_get_context()->zb->ysize;
@@ -32,7 +40,7 @@
 	if(x>0 && x<w && y>0 && y < h)
 		pbuf[x+y*w] = p;
 }
-void glDrawText(const unsigned char* text, int x, int y, PIXEL p){
+void glDrawText(const unsigned char* text, int x, int y, unsigned int p){
 	if(!text)return;
 	//PIXEL* pbuf = gl_get_context()->zb->pbuf;
 	int w = gl_get_context()->zb->xsize;
--- a/src/ztriangle.c
+++ b/src/ztriangle.c
@@ -7,8 +7,8 @@
 #if TGL_FEATURE_RENDER_BITS == 32
 
 
+#elif TGL_FEATURE_RENDER_BITS == 16
 
-
 #else
 
 #error "USE 32 BIT MODE!!!"
@@ -42,7 +42,7 @@
 
 
 
-    unsigned int color;
+    PIXEL color;
 
 
 #define INTERP_Z
@@ -75,7 +75,9 @@
 void ZB_fillTriangleSmooth(ZBuffer *zb,
 			   ZBufferPoint *p0,ZBufferPoint *p1,ZBufferPoint *p2)
 {
-
+#if TGL_FEATURE_RENDER_BITS == 16
+	int _drgbdx;
+#endif
 //unsigned int color;
 #define INTERP_Z
 #define INTERP_RGB
@@ -83,7 +85,7 @@
 #define SAR_RND_TO_ZERO(v,n) (v / (1<<n))
 
 
-
+#if TGL_FEATURE_RENDER_BITS == 32
 #define DRAW_INIT()				\
 {						\
   	\
@@ -102,6 +104,62 @@
     ob1+=dbdx;					\
 }
 
+//END OF 32 bit mode 
+#elif TGL_FEATURE_RENDER_BITS == 16
+
+
+#define DRAW_INIT()                                       \
+{                                                         \
+_drgbdx=(SAR_RND_TO_ZERO(drdx, 6) << 22) & 0xFFC00000;     \
+_drgbdx|=SAR_RND_TO_ZERO(dgdx,5) & 0x000007FF;             \
+_drgbdx|=(SAR_RND_TO_ZERO(dbdx,7) << 12) & 0x001FF000;     \
+}
+
+#define PUT_PIXEL(_a)				\
+{						\
+    zz=z >> ZB_POINT_Z_FRAC_BITS;		\
+    if (ZCMP(zz,pz[_a],_a)) {				\
+      tmp=rgb&0xF81F07E0;				\
+      pp[_a] = tmp | (tmp >> 16);\
+      pz[_a]=zz;				\
+    }\
+    z+=dzdx;					\
+    rgb=(rgb+drgbdx) & ( ~ 0x00200800);\
+}
+
+#define DRAW_LINE()							   \
+{									   \
+  register unsigned short *pz;					   \
+  register PIXEL *pp;					   \
+  register unsigned int tmp,z,zz,rgb,drgbdx;				   \
+  register int n;							   \
+  n=(x2 >> 16) - x1;							   \
+  pp=pp1+x1;								   \
+  pz=pz1+x1;								   \
+  z=z1;									   \
+  rgb=(r1 << 16) & 0xFFC00000;						   \
+  rgb|=(g1 >> 5) & 0x000007FF;						   \
+  rgb|=(b1 << 5) & 0x001FF000;						   \
+  drgbdx=_drgbdx;							   \
+  while (n>=3) {							   \
+    PUT_PIXEL(0);							   \
+    PUT_PIXEL(1);							   \
+    PUT_PIXEL(2);							   \
+    PUT_PIXEL(3);							   \
+    pz+=4;								   \
+    pp+=4;								   \
+    n-=4;								   \
+  }									   \
+  while (n>=0) {							   \
+    PUT_PIXEL(0);							   \
+    pz+=1;								   \
+    pp+=1;								   \
+    n-=1;								   \
+  }									   \
+}
+
+#endif 
+//^ End of 16 bit mode stuff
 #include "ztriangle.h"
 } //EOF smooth fill triangle