1 #include <stdint.h> 2 #include <stdlib.h> 3 #include <boolean.h> 4 #include "DepthBuffer.h" 5 6 DepthBufferInfo depthBuffer; 7 DepthBuffer_Init(void)8void DepthBuffer_Init(void) 9 { 10 depthBuffer.current = NULL; 11 depthBuffer.top = NULL; 12 depthBuffer.bottom = NULL; 13 depthBuffer.numBuffers = 0; 14 } 15 DepthBuffer_RemoveBottom(void)16void DepthBuffer_RemoveBottom(void) 17 { 18 DepthBuffer *newBottom = depthBuffer.bottom->higher; 19 20 if (depthBuffer.bottom == depthBuffer.top) 21 depthBuffer.top = NULL; 22 23 free( depthBuffer.bottom ); 24 25 depthBuffer.bottom = newBottom; 26 27 if (depthBuffer.bottom != NULL) 28 depthBuffer.bottom->lower = NULL; 29 30 depthBuffer.numBuffers--; 31 } 32 DepthBuffer_Remove(DepthBuffer * buffer)33void DepthBuffer_Remove( DepthBuffer *buffer ) 34 { 35 if ((buffer == depthBuffer.bottom) && 36 (buffer == depthBuffer.top)) 37 { 38 depthBuffer.top = NULL; 39 depthBuffer.bottom = NULL; 40 } 41 else if (buffer == depthBuffer.bottom) 42 { 43 depthBuffer.bottom = buffer->higher; 44 45 if (depthBuffer.bottom) 46 depthBuffer.bottom->lower = NULL; 47 } 48 else if (buffer == depthBuffer.top) 49 { 50 depthBuffer.top = buffer->lower; 51 52 if (depthBuffer.top) 53 depthBuffer.top->higher = NULL; 54 } 55 else 56 { 57 buffer->higher->lower = buffer->lower; 58 buffer->lower->higher = buffer->higher; 59 } 60 61 free( buffer ); 62 depthBuffer.numBuffers--; 63 } 64 DepthBuffer_RemoveBuffer(uint32_t address)65void DepthBuffer_RemoveBuffer( uint32_t address ) 66 { 67 DepthBuffer *current = depthBuffer.bottom; 68 while (current != NULL) 69 { 70 if (current->address == address) 71 { 72 DepthBuffer_Remove( current ); 73 return; 74 } 75 current = current->higher; 76 } 77 } 78 DepthBuffer_AddTop(void)79DepthBuffer *DepthBuffer_AddTop(void) 80 { 81 DepthBuffer *newtop = (DepthBuffer*)malloc( sizeof( DepthBuffer ) ); 82 83 newtop->lower = depthBuffer.top; 84 newtop->higher = NULL; 85 86 if (depthBuffer.top) 87 depthBuffer.top->higher = newtop; 88 89 if (!depthBuffer.bottom) 90 depthBuffer.bottom = newtop; 91 92 depthBuffer.top = newtop; 93 94 depthBuffer.numBuffers++; 95 96 return newtop; 97 } 98 DepthBuffer_MoveToTop(DepthBuffer * newtop)99void DepthBuffer_MoveToTop( DepthBuffer *newtop ) 100 { 101 if (newtop == depthBuffer.top) 102 return; 103 104 if (newtop == depthBuffer.bottom) 105 { 106 depthBuffer.bottom = newtop->higher; 107 depthBuffer.bottom->lower = NULL; 108 } 109 else 110 { 111 newtop->higher->lower = newtop->lower; 112 newtop->lower->higher = newtop->higher; 113 } 114 115 newtop->higher = NULL; 116 newtop->lower = depthBuffer.top; 117 depthBuffer.top->higher = newtop; 118 depthBuffer.top = newtop; 119 } 120 DepthBuffer_Destroy(void)121void DepthBuffer_Destroy(void) 122 { 123 while (depthBuffer.bottom) 124 DepthBuffer_RemoveBottom(); 125 126 depthBuffer.top = NULL; 127 } 128 DepthBuffer_SetBuffer(uint32_t address)129void DepthBuffer_SetBuffer( uint32_t address ) 130 { 131 DepthBuffer *current = depthBuffer.top; 132 133 // Search through saved depth buffers 134 while (current != NULL) 135 { 136 if (current->address == address) 137 { 138 DepthBuffer_MoveToTop( current ); 139 depthBuffer.current = current; 140 return; 141 } 142 current = current->lower; 143 } 144 145 current = DepthBuffer_AddTop(); 146 147 current->address = address; 148 current->cleared = true; 149 depthBuffer.current = current; 150 } 151 DepthBuffer_FindBuffer(uint32_t address)152DepthBuffer *DepthBuffer_FindBuffer( uint32_t address ) 153 { 154 DepthBuffer *current = depthBuffer.top; 155 156 while (current) 157 { 158 if (current->address == address) 159 return current; 160 current = current->lower; 161 } 162 163 return NULL; 164 } 165