Revision af4cc5db
Added by Leszek Koltunski about 7 years ago
src/main/java/org/distorted/library/DistortedSurface.java | ||
---|---|---|
19 | 19 |
|
20 | 20 |
package org.distorted.library; |
21 | 21 |
|
22 |
import java.util.Iterator; |
|
23 |
import java.util.LinkedList; |
|
24 |
|
|
25 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
26 |
/** |
|
27 |
* Keep all objects created in a static LinkedList. The point: we need to be able to mark |
|
28 |
* Objects for deletion, and delete all marked Objects later at a convenient time (that's |
|
29 |
* because we can only delete from a thread that holds the OpenGL context so here we provide a |
|
30 |
* framework where one is able to mark for deletion at any time and actual deletion takes place |
|
31 |
* on the next render). |
|
32 |
*/ |
|
33 |
abstract class DistortedSurface |
|
34 |
{ |
|
35 |
static final int FAILED_TO_CREATE = -1; |
|
36 |
static final int NOT_CREATED_YET = -2; |
|
37 |
static final int DONT_CREATE = -3; |
|
38 |
|
|
39 |
private static boolean mListMarked = false; |
|
40 |
private static LinkedList<DistortedSurface> mList = new LinkedList<>(); |
|
41 |
|
|
42 |
private boolean mMarked; |
|
43 |
int[] mColorH = new int[1]; |
|
44 |
int mSizeX, mSizeY; // in screen space |
|
45 |
|
|
46 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
47 |
|
|
48 |
public abstract void create(); |
|
49 |
abstract void delete(); |
|
50 |
abstract void destroy(); |
|
51 |
|
|
52 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
53 |
// must be called form a thread holding OpenGL Context |
|
54 |
|
|
55 |
static synchronized void deleteAllMarked() |
|
56 |
{ |
|
57 |
if( mListMarked ) |
|
58 |
{ |
|
59 |
DistortedSurface tmp; |
|
60 |
Iterator<DistortedSurface> iterator = mList.iterator(); |
|
61 |
|
|
62 |
while(iterator.hasNext()) |
|
63 |
{ |
|
64 |
tmp = iterator.next(); |
|
65 |
|
|
66 |
if( tmp.mMarked ) |
|
67 |
{ |
|
68 |
tmp.delete(); |
|
69 |
tmp.mMarked = false; |
|
70 |
iterator.remove(); |
|
71 |
} |
|
72 |
} |
|
73 |
|
|
74 |
mListMarked = false; |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
22 | 78 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
23 | 79 |
|
80 |
static synchronized void onDestroy() |
|
81 |
{ |
|
82 |
for( DistortedSurface ren : mList) |
|
83 |
{ |
|
84 |
ren.destroy(); |
|
85 |
ren.mMarked = false; |
|
86 |
} |
|
87 |
|
|
88 |
mListMarked = false; |
|
89 |
} |
|
90 |
|
|
91 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
92 |
|
|
93 |
DistortedSurface(int width, int height, int color) |
|
94 |
{ |
|
95 |
mSizeX = width ; |
|
96 |
mSizeY = height; |
|
97 |
mColorH[0]= color; |
|
98 |
mMarked = false; |
|
99 |
mList.add(this); |
|
100 |
} |
|
101 |
|
|
102 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
103 |
// PUBLIC API |
|
104 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
24 | 105 |
/** |
25 |
* Abstract Surface.
|
|
106 |
* Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
|
|
26 | 107 |
*/ |
108 |
public void markForDeletion() |
|
109 |
{ |
|
110 |
mListMarked = true; |
|
111 |
mMarked = true; |
|
112 |
} |
|
27 | 113 |
|
28 |
interface DistortedSurface |
|
29 |
{ |
|
114 |
//////////////////////////////////////////////////////////////////////////////////////////////////// |
|
30 | 115 |
/** |
31 |
* Create the underlying OpenGL part of the Surface.
|
|
116 |
* Return unique ID of the Surface.
|
|
32 | 117 |
*/ |
33 |
void create(); |
|
118 |
public long getID() |
|
119 |
{ |
|
120 |
return mColorH[0]; |
|
121 |
} |
|
122 |
|
|
123 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
124 |
|
|
34 | 125 |
/** |
35 |
* Return a unique ID of this Surface. |
|
126 |
* Returns the height of the Surface. |
|
127 |
* |
|
128 |
* @return height of the object, in pixels. |
|
36 | 129 |
*/ |
37 |
long getID(); |
|
130 |
public int getWidth() |
|
131 |
{ |
|
132 |
return mSizeX; |
|
133 |
} |
|
134 |
|
|
135 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
38 | 136 |
/** |
39 |
* Return the width of this Surface. |
|
137 |
* Returns the width of the Surface. |
|
138 |
* |
|
139 |
* @return width of the Object, in pixels. |
|
40 | 140 |
*/ |
41 |
int getWidth(); |
|
141 |
public int getHeight() |
|
142 |
{ |
|
143 |
return mSizeY; |
|
144 |
} |
|
145 |
|
|
146 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
|
42 | 147 |
/** |
43 |
* Return the height of this Surface. |
|
148 |
* Returns the depth of the Surface. |
|
149 |
* <p> |
|
150 |
* Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine |
|
151 |
* 'depth' of a Surface (bitmap really!) when rendered based only on the texture itself, that depends |
|
152 |
* on the Mesh it is rendered with. |
|
153 |
* |
|
154 |
* @return depth of the Object, in pixels. |
|
44 | 155 |
*/ |
45 |
int getHeight(); |
|
46 |
} |
|
156 |
public int getDepth(MeshObject mesh) |
|
157 |
{ |
|
158 |
return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor); |
|
159 |
} |
|
160 |
} |
Also available in: Unified diff
Simplify yesterday's refactoring.