Project

General

Profile

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

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

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
  private static long mNextID = 0;
42

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

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
  abstract void create();
51
  abstract void delete();
52
  abstract void recreate();
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
// must be called form a thread holding OpenGL Context
56

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

    
63
      int num = mToDoList.size();
64

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

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

    
81
      mToDo = false;
82
      }
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  static synchronized void onDestroy()
88
    {
89
    DistortedSurface surface;
90

    
91
    int num = mDoneList.size();
92

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

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

    
104
    mToDo = true;
105
    mNextID = 0;
106
    }
107

    
108
///////////////////////////////////////////////////////////////////////////////////////////////////
109

    
110
  synchronized void moveToToDo()
111
    {
112
    if ( mDoneList.remove(this) )
113
      {
114
      mToDoList.add(this);
115
      mToDo = true;
116
      }
117
    }
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
    mID       = mNextID++;
128

    
129
    if( color!=DONT_CREATE )
130
      {
131
      mToDoList.add(this);
132
      mToDo = true;
133
      }
134
    }
135

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

    
153
////////////////////////////////////////////////////////////////////////////////////////////////////
154
/**
155
 * Return unique ID of the Surface.
156
 */
157
  public long getID()
158
    {
159
    return mID;
160
    }
161

    
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163

    
164
/**
165
 * Returns the height of the Surface.
166
 *
167
 * @return height of the object, in pixels.
168
 */
169
  public int getWidth()
170
    {
171
    return mSizeX;
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
/**
176
 * Returns the width of the Surface.
177
 *
178
 * @return width of the Object, in pixels.
179
 */
180
  public int getHeight()
181
    {
182
    return mSizeY;
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
/**
187
 * 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
 */
195
  public int getDepth(MeshObject mesh)
196
    {
197
    return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
198
    }
199
  }
(10-10/22)