Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedRenderable.java @ 4ebbb17a

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 android.opengl.GLES30;
23

    
24
import java.util.Iterator;
25
import java.util.LinkedList;
26

    
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28

    
29
abstract class DistortedRenderable
30
  {
31
  static final int FAILED_TO_CREATE = -1;
32
  static final int NOT_CREATED_YET  = -2;
33
  static final int DONT_CREATE      = -3;
34

    
35
  private static boolean mListMarked = false;
36
  private static LinkedList<DistortedRenderable> mList = new LinkedList<>();
37

    
38
  private boolean mMarked;
39
  int[] mColorH = new int[1];
40
  int mSizeX, mSizeY;  // in screen space
41

    
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43

    
44
  abstract void create();
45
  abstract void delete();
46
  abstract void destroy();
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
// must be called form a thread holding OpenGL Context
50

    
51
  static synchronized void deleteAllMarked()
52
    {
53
    if( mListMarked )
54
      {
55
      DistortedRenderable tmp;
56
      Iterator<DistortedRenderable> iterator = mList.iterator();
57

    
58
      while(iterator.hasNext())
59
        {
60
        tmp = iterator.next();
61

    
62
        if( tmp.mMarked )
63
          {
64
          tmp.delete();
65
          tmp.mMarked = false;
66
          iterator.remove();
67
          }
68
        }
69

    
70
      mListMarked = false;
71
      }
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

    
76
  long getID()
77
    {
78
    return mColorH[0];
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82

    
83
  boolean setAsInput()
84
    {
85
    if( mColorH[0]>0 )
86
      {
87
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
88
      return true;
89
      }
90

    
91
    return false;
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  static synchronized void onDestroy()
97
    {
98
    for( DistortedRenderable ren : mList)
99
      {
100
      ren.destroy();
101
      ren.mMarked = false;
102
      }
103

    
104
    mListMarked = false;
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  DistortedRenderable(int width, int height, int color)
110
    {
111
    mSizeX    = width ;
112
    mSizeY    = height;
113
    mColorH[0]= color;
114
    mMarked   = false;
115
    mList.add(this);
116
    }
117

    
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
// PUBLIC API
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121
/**
122
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
123
 */
124
  public void markForDeletion()
125
    {
126
    mListMarked = true;
127
    mMarked     = true;
128
    }
129

    
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
/**
133
 * Returns the height of the Renderable.
134
 *
135
 * @return height of the object, in pixels.
136
 */
137
  public int getWidth()
138
    {
139
    return mSizeX;
140
    }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
/**
144
 * Returns the width of the Renderable.
145
 *
146
 * @return width of the Object, in pixels.
147
 */
148
  public int getHeight()
149
    {
150
    return mSizeY;
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
/**
155
 * Returns the depth of the Renderable.
156
 * <p>
157
 * Admittedly quite a strange method. Why do we need to pass a Mesh to it? Because one cannot determine
158
 * 'depth' of a Renderable (bitmap really!) when rendered based only on the texture itself, that depends
159
 * on the Mesh it is rendered with.
160
 *
161
 * @return depth of the Object, in pixels.
162
 */
163
  public int getDepth(MeshObject mesh)
164
    {
165
    return mesh==null ? 0 : (int)(mSizeX*mesh.zFactor);
166
    }
167
  }
(4-4/17)