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