Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedRenderable.java @ 2e49718d

1 133cbb2b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 2e49718d Leszek Koltunski
import java.util.Iterator;
25
import java.util.LinkedList;
26
27 133cbb2b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 2e49718d Leszek Koltunski
  private static boolean mListMarked = false;
36
  private static LinkedList<DistortedRenderable> mList = new LinkedList<>();
37
38
  private boolean mMarked;
39 133cbb2b Leszek Koltunski
  int[] mColorH = new int[1];
40
  int mSizeX, mSizeY;  // in screen space
41
42 227ac49a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
  abstract void create();
45
  abstract void delete();
46
47 2e49718d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
48
// must be called form a thread holding OpenGL Context
49
50
  static synchronized void deleteAllMarked()
51
    {
52
    if( mListMarked )
53
      {
54
      DistortedRenderable tmp;
55
      Iterator<DistortedRenderable> iterator = mList.iterator();
56
57
      while(iterator.hasNext())
58
        {
59
        tmp = iterator.next();
60
61
        if( tmp.mMarked )
62
          {
63
          tmp.delete();
64
          tmp.mMarked = false;
65
          iterator.remove();
66
          }
67
        }
68
69
      mListMarked = false;
70
      }
71
    }
72
73 133cbb2b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
74
75
  long getID()
76
    {
77
    return mColorH[0];
78
    }
79
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81
82
  boolean setAsInput()
83
    {
84
    if( mColorH[0]>0 )
85
      {
86
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
87
      return true;
88
      }
89
90
    return false;
91
    }
92
93 2e49718d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95
  static synchronized void onDestroy()
96
    {
97
    for( DistortedRenderable ren : mList)
98
      {
99
      ren.delete();
100
      ren.mMarked = false;
101
      }
102
103
    mListMarked = false;
104
    mList.clear();
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 133cbb2b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
119
// PUBLIC API
120
///////////////////////////////////////////////////////////////////////////////////////////////////
121 2e49718d Leszek Koltunski
/**
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 133cbb2b Leszek Koltunski
/**
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
  }