Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
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
  private static boolean mToDo = false;
39
  private static LinkedList<DistortedSurface> mDoneList = new LinkedList<>();
40
  private static LinkedList<DistortedSurface> mToDoList = new LinkedList<>();
41

    
42
  private boolean mMarked;
43
  int[] mColorH = new int[1];
44
  int mSizeX, mSizeY;  // in screen space
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  abstract void create();
49
  abstract void delete();
50
  abstract void recreate();
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
// must be called form a thread holding OpenGL Context
54

    
55
  static synchronized void toDo()
56
    {
57
    if( mToDo )
58
      {
59
      DistortedSurface surface;
60

    
61
      int num = mToDoList.size();
62

    
63
      for(int i=0; i<num; i++)
64
        {
65
        surface = mToDoList.removeFirst();
66

    
67
        if(surface.mMarked)
68
          {
69
          surface.delete();
70
          surface.mMarked = false;
71
          }
72
        else
73
          {
74
          surface.create();
75
          mDoneList.add(surface);
76
          }
77
        }
78

    
79
      mToDo = false;
80
      }
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  static synchronized void onDestroy()
86
    {
87
    DistortedSurface surface;
88

    
89
    int num = mDoneList.size();
90

    
91
    for(int i=0; i<num; i++)
92
      {
93
      surface = mDoneList.removeFirst();
94

    
95
      if( !surface.mMarked )
96
        {
97
        surface.recreate();
98
        mToDoList.add(surface);
99
        }
100
      }
101

    
102
    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
    }
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

    
125
    if( color!=DONT_CREATE )
126
      {
127
      mToDoList.add(this);
128
      mToDo = true;
129
      }
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
// PUBLIC API
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
/**
136
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
137
 */
138
  public void markForDeletion()
139
    {
140
    if( !mMarked )
141
      {
142
      mToDo   = true;
143
      mMarked = true;
144
      mDoneList.remove(this);
145
      mToDoList.add(this);
146
      }
147
    }
148

    
149
////////////////////////////////////////////////////////////////////////////////////////////////////
150
/**
151
 * Return unique ID of the Surface.
152
 */
153
  public long getID()
154
    {
155
    return mColorH[0];
156
    }
157

    
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159

    
160
/**
161
 * Returns the height of the Surface.
162
 *
163
 * @return height of the object, in pixels.
164
 */
165
  public int getWidth()
166
    {
167
    return mSizeX;
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
/**
172
 * Returns the width of the Surface.
173
 *
174
 * @return width of the Object, in pixels.
175
 */
176
  public int getHeight()
177
    {
178
    return mSizeY;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
/**
183
 * 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
 */
191
  public int getDepth(MeshObject mesh)
192
    {
193
    return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
194
    }
195
  }
(8-8/20)