Project

General

Profile

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

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

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.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 c5369f1b leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
79
80 af4cc5db Leszek Koltunski
  static synchronized void onDestroy()
81
    {
82 29a06526 Leszek Koltunski
    for( DistortedSurface surface : mList)
83 af4cc5db Leszek Koltunski
      {
84 29a06526 Leszek Koltunski
      surface.destroy();
85
      surface.mMarked = false;
86 af4cc5db Leszek Koltunski
      }
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 c5369f1b leszek
/**
106 af4cc5db Leszek Koltunski
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
107 c5369f1b leszek
 */
108 af4cc5db Leszek Koltunski
  public void markForDeletion()
109
    {
110
    mListMarked = true;
111
    mMarked     = true;
112
    }
113 c5369f1b leszek
114 af4cc5db Leszek Koltunski
////////////////////////////////////////////////////////////////////////////////////////////////////
115 c5369f1b leszek
/**
116 af4cc5db Leszek Koltunski
 * Return unique ID of the Surface.
117 c5369f1b leszek
 */
118 af4cc5db Leszek Koltunski
  public long getID()
119
    {
120
    return mColorH[0];
121
    }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125 c5369f1b leszek
/**
126 af4cc5db Leszek Koltunski
 * Returns the height of the Surface.
127
 *
128
 * @return height of the object, in pixels.
129 c5369f1b leszek
 */
130 af4cc5db Leszek Koltunski
  public int getWidth()
131
    {
132
    return mSizeX;
133
    }
134
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136 c5369f1b leszek
/**
137 af4cc5db Leszek Koltunski
 * Returns the width of the Surface.
138
 *
139
 * @return width of the Object, in pixels.
140 c5369f1b leszek
 */
141 af4cc5db Leszek Koltunski
  public int getHeight()
142
    {
143
    return mSizeY;
144
    }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147 c5369f1b leszek
/**
148 af4cc5db Leszek Koltunski
 * 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 c5369f1b leszek
 */
156 af4cc5db Leszek Koltunski
  public int getDepth(MeshObject mesh)
157
    {
158
    return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
159
    }
160
  }