Project

General

Profile

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

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

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.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

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  static synchronized void onDestroy()
81
    {
82
    for( DistortedSurface surface : mList)
83
      {
84
      surface.destroy();
85
      surface.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
///////////////////////////////////////////////////////////////////////////////////////////////////
105
/**
106
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
107
 */
108
  public void markForDeletion()
109
    {
110
    mListMarked = true;
111
    mMarked     = true;
112
    }
113

    
114
////////////////////////////////////////////////////////////////////////////////////////////////////
115
/**
116
 * Return unique ID of the Surface.
117
 */
118
  public long getID()
119
    {
120
    return mColorH[0];
121
    }
122

    
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124

    
125
/**
126
 * Returns the height of the Surface.
127
 *
128
 * @return height of the object, in pixels.
129
 */
130
  public int getWidth()
131
    {
132
    return mSizeX;
133
    }
134

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136
/**
137
 * Returns the width of the Surface.
138
 *
139
 * @return width of the Object, in pixels.
140
 */
141
  public int getHeight()
142
    {
143
    return mSizeY;
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
/**
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.
155
 */
156
  public int getDepth(MeshObject mesh)
157
    {
158
    return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
159
    }
160
  }
(8-8/20)