1 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2 |
|
|
// Copyright 2016 Leszek Koltunski //
|
3 |
|
|
// //
|
4 |
|
|
// This file is part of Distorted. //
|
5 |
|
|
// //
|
6 |
|
|
// Distorted is free software: you can redistribute it and/or modify //
|
7 |
|
|
// it under the terms of the GNU General Public License as published by //
|
8 |
|
|
// the Free Software Foundation, either version 2 of the License, or //
|
9 |
|
|
// (at your option) any later version. //
|
10 |
|
|
// //
|
11 |
|
|
// Distorted is distributed in the hope that it will be useful, //
|
12 |
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13 |
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14 |
|
|
// GNU General Public License for more details. //
|
15 |
|
|
// //
|
16 |
|
|
// You should have received a copy of the GNU General Public License //
|
17 |
|
|
// along with Distorted. If not, see <http://www.gnu.org/licenses/>. //
|
18 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19 |
|
|
|
20 |
|
|
package org.distorted.library;
|
21 |
|
|
|
22 |
af4cc5db
|
Leszek Koltunski
|
import java.util.LinkedList;
|
23 |
|
|
|
24 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
25 |
|
|
/**
|
26 |
|
|
* Keep all objects created in a static LinkedList. The point: we need to be able to mark
|
27 |
|
|
* Objects for deletion, and delete all marked Objects later at a convenient time (that's
|
28 |
|
|
* because we can only delete from a thread that holds the OpenGL context so here we provide a
|
29 |
|
|
* framework where one is able to mark for deletion at any time and actual deletion takes place
|
30 |
|
|
* on the next render).
|
31 |
|
|
*/
|
32 |
|
|
abstract class DistortedSurface
|
33 |
|
|
{
|
34 |
|
|
static final int FAILED_TO_CREATE = -1;
|
35 |
|
|
static final int NOT_CREATED_YET = -2;
|
36 |
|
|
static final int DONT_CREATE = -3;
|
37 |
|
|
|
38 |
f8377ef8
|
leszek
|
private static boolean mToDo = false;
|
39 |
|
|
private static LinkedList<DistortedSurface> mDoneList = new LinkedList<>();
|
40 |
|
|
private static LinkedList<DistortedSurface> mToDoList = new LinkedList<>();
|
41 |
af4cc5db
|
Leszek Koltunski
|
|
42 |
|
|
private boolean mMarked;
|
43 |
|
|
int[] mColorH = new int[1];
|
44 |
|
|
int mSizeX, mSizeY; // in screen space
|
45 |
|
|
|
46 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
47 |
|
|
|
48 |
f8377ef8
|
leszek
|
abstract void create();
|
49 |
af4cc5db
|
Leszek Koltunski
|
abstract void delete();
|
50 |
f8377ef8
|
leszek
|
abstract void recreate();
|
51 |
af4cc5db
|
Leszek Koltunski
|
|
52 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
53 |
|
|
// must be called form a thread holding OpenGL Context
|
54 |
|
|
|
55 |
f8377ef8
|
leszek
|
static synchronized void toDo()
|
56 |
af4cc5db
|
Leszek Koltunski
|
{
|
57 |
f8377ef8
|
leszek
|
if( mToDo )
|
58 |
af4cc5db
|
Leszek Koltunski
|
{
|
59 |
f8377ef8
|
leszek
|
DistortedSurface surface;
|
60 |
af4cc5db
|
Leszek Koltunski
|
|
61 |
f8377ef8
|
leszek
|
int num = mToDoList.size();
|
62 |
|
|
|
63 |
|
|
for(int i=0; i<num; i++)
|
64 |
af4cc5db
|
Leszek Koltunski
|
{
|
65 |
f8377ef8
|
leszek
|
surface = mToDoList.removeFirst();
|
66 |
af4cc5db
|
Leszek Koltunski
|
|
67 |
f8377ef8
|
leszek
|
if(surface.mMarked)
|
68 |
|
|
{
|
69 |
|
|
surface.delete();
|
70 |
|
|
surface.mMarked = false;
|
71 |
|
|
}
|
72 |
|
|
else
|
73 |
af4cc5db
|
Leszek Koltunski
|
{
|
74 |
f8377ef8
|
leszek
|
surface.create();
|
75 |
|
|
mDoneList.add(surface);
|
76 |
af4cc5db
|
Leszek Koltunski
|
}
|
77 |
|
|
}
|
78 |
|
|
|
79 |
f8377ef8
|
leszek
|
mToDo = false;
|
80 |
af4cc5db
|
Leszek Koltunski
|
}
|
81 |
|
|
}
|
82 |
|
|
|
83 |
c5369f1b
|
leszek
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
84 |
|
|
|
85 |
af4cc5db
|
Leszek Koltunski
|
static synchronized void onDestroy()
|
86 |
|
|
{
|
87 |
f8377ef8
|
leszek
|
DistortedSurface surface;
|
88 |
|
|
|
89 |
|
|
int num = mDoneList.size();
|
90 |
|
|
|
91 |
|
|
for(int i=0; i<num; i++)
|
92 |
af4cc5db
|
Leszek Koltunski
|
{
|
93 |
f8377ef8
|
leszek
|
surface = mDoneList.removeFirst();
|
94 |
|
|
|
95 |
|
|
if( !surface.mMarked )
|
96 |
|
|
{
|
97 |
|
|
surface.recreate();
|
98 |
|
|
mToDoList.add(surface);
|
99 |
|
|
}
|
100 |
af4cc5db
|
Leszek Koltunski
|
}
|
101 |
|
|
|
102 |
f8377ef8
|
leszek
|
mToDo = true;
|
103 |
|
|
}
|
104 |
|
|
|
105 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106 |
|
|
|
107 |
|
|
synchronized void moveToToDo()
|
108 |
|
|
{
|
109 |
|
|
if ( mDoneList.remove(this) )
|
110 |
|
|
{
|
111 |
|
|
mToDoList.add(this);
|
112 |
|
|
mToDo = true;
|
113 |
|
|
}
|
114 |
af4cc5db
|
Leszek Koltunski
|
}
|
115 |
|
|
|
116 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
117 |
|
|
|
118 |
|
|
DistortedSurface(int width, int height, int color)
|
119 |
|
|
{
|
120 |
|
|
mSizeX = width ;
|
121 |
|
|
mSizeY = height;
|
122 |
|
|
mColorH[0]= color;
|
123 |
|
|
mMarked = false;
|
124 |
f8377ef8
|
leszek
|
|
125 |
|
|
if( color!=DONT_CREATE )
|
126 |
|
|
{
|
127 |
|
|
mToDoList.add(this);
|
128 |
|
|
mToDo = true;
|
129 |
|
|
}
|
130 |
af4cc5db
|
Leszek Koltunski
|
}
|
131 |
|
|
|
132 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
133 |
|
|
// PUBLIC API
|
134 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
135 |
c5369f1b
|
leszek
|
/**
|
136 |
af4cc5db
|
Leszek Koltunski
|
* Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
|
137 |
c5369f1b
|
leszek
|
*/
|
138 |
af4cc5db
|
Leszek Koltunski
|
public void markForDeletion()
|
139 |
|
|
{
|
140 |
f8377ef8
|
leszek
|
if( !mMarked )
|
141 |
|
|
{
|
142 |
|
|
mToDo = true;
|
143 |
|
|
mMarked = true;
|
144 |
|
|
mDoneList.remove(this);
|
145 |
|
|
mToDoList.add(this);
|
146 |
|
|
}
|
147 |
af4cc5db
|
Leszek Koltunski
|
}
|
148 |
c5369f1b
|
leszek
|
|
149 |
af4cc5db
|
Leszek Koltunski
|
////////////////////////////////////////////////////////////////////////////////////////////////////
|
150 |
c5369f1b
|
leszek
|
/**
|
151 |
af4cc5db
|
Leszek Koltunski
|
* Return unique ID of the Surface.
|
152 |
c5369f1b
|
leszek
|
*/
|
153 |
af4cc5db
|
Leszek Koltunski
|
public long getID()
|
154 |
|
|
{
|
155 |
|
|
return mColorH[0];
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159 |
|
|
|
160 |
c5369f1b
|
leszek
|
/**
|
161 |
af4cc5db
|
Leszek Koltunski
|
* Returns the height of the Surface.
|
162 |
|
|
*
|
163 |
|
|
* @return height of the object, in pixels.
|
164 |
c5369f1b
|
leszek
|
*/
|
165 |
af4cc5db
|
Leszek Koltunski
|
public int getWidth()
|
166 |
|
|
{
|
167 |
|
|
return mSizeX;
|
168 |
|
|
}
|
169 |
|
|
|
170 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
171 |
c5369f1b
|
leszek
|
/**
|
172 |
af4cc5db
|
Leszek Koltunski
|
* Returns the width of the Surface.
|
173 |
|
|
*
|
174 |
|
|
* @return width of the Object, in pixels.
|
175 |
c5369f1b
|
leszek
|
*/
|
176 |
af4cc5db
|
Leszek Koltunski
|
public int getHeight()
|
177 |
|
|
{
|
178 |
|
|
return mSizeY;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
182 |
c5369f1b
|
leszek
|
/**
|
183 |
af4cc5db
|
Leszek Koltunski
|
* Returns the depth of the Surface.
|
184 |
|
|
* <p>
|
185 |
|
|
* Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine
|
186 |
|
|
* 'depth' of a Surface (bitmap really!) when rendered based only on the texture itself, that depends
|
187 |
|
|
* on the Mesh it is rendered with.
|
188 |
|
|
*
|
189 |
|
|
* @return depth of the Object, in pixels.
|
190 |
c5369f1b
|
leszek
|
*/
|
191 |
af4cc5db
|
Leszek Koltunski
|
public int getDepth(MeshObject mesh)
|
192 |
|
|
{
|
193 |
|
|
return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
|
194 |
|
|
}
|
195 |
|
|
}
|