Project

General

Profile

Download (6.37 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / DistortedSurface.java @ c204c69d

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 c204c69d leszek
  private static long mNextID = 0;
42 af4cc5db Leszek Koltunski
43 c204c69d leszek
  private long mID;
44 af4cc5db Leszek Koltunski
  private boolean mMarked;
45
  int[] mColorH = new int[1];
46
  int mSizeX, mSizeY;  // in screen space
47
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
50 f8377ef8 leszek
  abstract void create();
51 af4cc5db Leszek Koltunski
  abstract void delete();
52 f8377ef8 leszek
  abstract void recreate();
53 af4cc5db Leszek Koltunski
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
// must be called form a thread holding OpenGL Context
56
57 f8377ef8 leszek
  static synchronized void toDo()
58 af4cc5db Leszek Koltunski
    {
59 f8377ef8 leszek
    if( mToDo )
60 af4cc5db Leszek Koltunski
      {
61 f8377ef8 leszek
      DistortedSurface surface;
62 af4cc5db Leszek Koltunski
63 f8377ef8 leszek
      int num = mToDoList.size();
64
65
      for(int i=0; i<num; i++)
66 af4cc5db Leszek Koltunski
        {
67 f8377ef8 leszek
        surface = mToDoList.removeFirst();
68 af4cc5db Leszek Koltunski
69 f8377ef8 leszek
        if(surface.mMarked)
70
          {
71
          surface.delete();
72
          surface.mMarked = false;
73
          }
74
        else
75 af4cc5db Leszek Koltunski
          {
76 f8377ef8 leszek
          surface.create();
77
          mDoneList.add(surface);
78 af4cc5db Leszek Koltunski
          }
79
        }
80
81 f8377ef8 leszek
      mToDo = false;
82 af4cc5db Leszek Koltunski
      }
83
    }
84
85 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87 af4cc5db Leszek Koltunski
  static synchronized void onDestroy()
88
    {
89 f8377ef8 leszek
    DistortedSurface surface;
90
91
    int num = mDoneList.size();
92
93
    for(int i=0; i<num; i++)
94 af4cc5db Leszek Koltunski
      {
95 f8377ef8 leszek
      surface = mDoneList.removeFirst();
96
97
      if( !surface.mMarked )
98
        {
99
        surface.recreate();
100
        mToDoList.add(surface);
101
        }
102 af4cc5db Leszek Koltunski
      }
103
104 f8377ef8 leszek
    mToDo = true;
105 c204c69d leszek
    mNextID = 0;
106 f8377ef8 leszek
    }
107
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109
110
  synchronized void moveToToDo()
111
    {
112
    if ( mDoneList.remove(this) )
113
      {
114
      mToDoList.add(this);
115
      mToDo = true;
116
      }
117 af4cc5db Leszek Koltunski
    }
118
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
121
  DistortedSurface(int width, int height, int color)
122
    {
123
    mSizeX    = width ;
124
    mSizeY    = height;
125
    mColorH[0]= color;
126
    mMarked   = false;
127 c204c69d leszek
    mID       = mNextID++;
128 f8377ef8 leszek
129
    if( color!=DONT_CREATE )
130
      {
131
      mToDoList.add(this);
132
      mToDo = true;
133
      }
134 af4cc5db Leszek Koltunski
    }
135
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// PUBLIC API
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139 c5369f1b leszek
/**
140 af4cc5db Leszek Koltunski
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
141 c5369f1b leszek
 */
142 af4cc5db Leszek Koltunski
  public void markForDeletion()
143
    {
144 f8377ef8 leszek
    if( !mMarked )
145
      {
146
      mToDo   = true;
147
      mMarked = true;
148
      mDoneList.remove(this);
149
      mToDoList.add(this);
150
      }
151 af4cc5db Leszek Koltunski
    }
152 c5369f1b leszek
153 af4cc5db Leszek Koltunski
////////////////////////////////////////////////////////////////////////////////////////////////////
154 c5369f1b leszek
/**
155 af4cc5db Leszek Koltunski
 * Return unique ID of the Surface.
156 c5369f1b leszek
 */
157 af4cc5db Leszek Koltunski
  public long getID()
158
    {
159 c204c69d leszek
    return mID;
160 af4cc5db Leszek Koltunski
    }
161
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
164 c5369f1b leszek
/**
165 af4cc5db Leszek Koltunski
 * Returns the height of the Surface.
166
 *
167
 * @return height of the object, in pixels.
168 c5369f1b leszek
 */
169 af4cc5db Leszek Koltunski
  public int getWidth()
170
    {
171
    return mSizeX;
172
    }
173
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175 c5369f1b leszek
/**
176 af4cc5db Leszek Koltunski
 * Returns the width of the Surface.
177
 *
178
 * @return width of the Object, in pixels.
179 c5369f1b leszek
 */
180 af4cc5db Leszek Koltunski
  public int getHeight()
181
    {
182
    return mSizeY;
183
    }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186 c5369f1b leszek
/**
187 af4cc5db Leszek Koltunski
 * Returns the depth of the Surface.
188
 * <p>
189
 * Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine
190
 * 'depth' of a Surface (bitmap really!) when rendered based only on the texture itself, that depends
191
 * on the Mesh it is rendered with.
192
 *
193
 * @return depth of the Object, in pixels.
194 c5369f1b leszek
 */
195 af4cc5db Leszek Koltunski
  public int getDepth(MeshObject mesh)
196
    {
197
    return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
198
    }
199
  }