Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / DeferredJobs.java @ f4c5a46e

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 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.mesh;
21

    
22
import org.distorted.library.effect.VertexEffect;
23
import org.distorted.library.effectqueue.EffectQueueVertex;
24
import org.distorted.library.main.DistortedLibrary;
25

    
26
import java.util.ArrayList;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * Not part of public API, do not document (public only because has to be cleaned from the main package)
31
 *
32
 * @y.exclude
33
 */
34
public class DeferredJobs
35
  {
36
  private static final int JOB_TYPE_VERTEX = 0;
37
  private static final int JOB_TYPE_MERGE  = 1;
38
  private static final int JOB_TYPE_JOIN   = 2;
39
  private static final int JOB_TYPE_COPY   = 3;
40

    
41
  private static ArrayList<JobNode> mJobs = new ArrayList<>();
42

    
43
  //////////////////////////////////////////////////////////////////////////
44

    
45
  private static class Job
46
    {
47
    private int mType;
48
    private MeshBase mTarget;
49
    private MeshBase[] mSource;
50
    private EffectQueueVertex mEffects;
51

    
52
    Job(int type, MeshBase target, MeshBase[] source, VertexEffect effect)
53
      {
54
      mType   = type;
55
      mTarget = target;
56
      mSource = source;
57
      mEffects= new EffectQueueVertex();
58
      mEffects.add(effect);
59
      }
60

    
61
    Job(Job job)
62
      {
63
      mType   = job.mType;
64
      mTarget = job.mTarget;
65
      mSource = job.mSource;
66
      mEffects= new EffectQueueVertex(job.mEffects);
67
      }
68

    
69
    void addEffect(VertexEffect effect)
70
      {
71
      mEffects.add(effect);
72
      }
73

    
74
    void execute()
75
      {
76
      switch(mType)
77
        {
78
        case JOB_TYPE_VERTEX: DistortedLibrary.adjustVertices(mTarget, mEffects);
79
                              break;
80
        case JOB_TYPE_MERGE : break;
81
        case JOB_TYPE_JOIN  : break;
82
        case JOB_TYPE_COPY  : mTarget.deepCopyAttribs1(mSource[0]);
83
                              break;
84
        }
85
      }
86

    
87
    void clear()
88
      {
89
      mEffects.removeAll(false);
90
      }
91
    }
92

    
93
  //////////////////////////////////////////////////////////////////////////
94

    
95
  static class JobNode
96
    {
97
    private ArrayList<JobNode> mPrevJobs;
98
    private ArrayList<JobNode> mNextJobs;
99
    private Job mJob;
100

    
101
    JobNode(Job job)
102
      {
103
      mPrevJobs = new ArrayList<>();
104
      mNextJobs = new ArrayList<>();
105
      mJob      = job;
106
      }
107

    
108
    JobNode(JobNode node)
109
      {
110
      mPrevJobs = new ArrayList<>();
111
      mNextJobs = new ArrayList<>();
112
      mJob      = new Job(node.mJob);
113

    
114
      int numPrev = node.mPrevJobs.size();
115

    
116
      for(int i=0; i<numPrev; i++)
117
        {
118
        mPrevJobs.add(node.mPrevJobs.get(i));
119
        }
120

    
121
      int numNext = node.mNextJobs.size();
122

    
123
      for(int i=0; i<numNext; i++)
124
        {
125
        mNextJobs.add(node.mNextJobs.get(i));
126
        }
127
      }
128

    
129
    void execute()
130
      {
131
      JobNode node;
132
      int numPrev = mPrevJobs.size();
133

    
134
      for(int i=0; i<numPrev; i++)
135
        {
136
        node = mPrevJobs.get(i);
137
        node.execute();
138
        }
139

    
140
      removeNode(this);
141
      mJob.execute();
142
      }
143

    
144
    void clear()
145
      {
146
      mPrevJobs.clear();
147
      mPrevJobs = null;
148
      mNextJobs.clear();
149
      mNextJobs = null;
150

    
151
      mJob.clear();
152
      mJob = null;
153
      }
154
    }
155

    
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157

    
158
  private static void removeNode(JobNode node)
159
    {
160
    mJobs.remove(node);
161
    JobNode jn;
162

    
163
    int numPrev = node.mPrevJobs.size();
164

    
165
    for(int i=0; i<numPrev; i++)
166
      {
167
      jn = node.mPrevJobs.get(i);
168
      jn.mNextJobs.remove(node);
169
      }
170

    
171
    int numNext = node.mNextJobs.size();
172

    
173
    for(int i=0; i<numNext; i++)
174
      {
175
      jn = node.mNextJobs.get(i);
176
      jn.mPrevJobs.remove(node);
177
      }
178

    
179
    node.mJob.mTarget.mJobNode[0] = null;
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  static JobNode vertex(MeshBase target, VertexEffect effect)
185
    {
186
    JobNode jn = target.mJobNode[0];
187

    
188
    if( jn==null )
189
      {
190
      Job job = new Job(JOB_TYPE_VERTEX,target,null,effect);
191
      JobNode node = new JobNode(job);
192
      mJobs.add(node);
193
      return node;
194
      }
195
    else
196
      {
197
      if( jn.mJob.mType==JOB_TYPE_VERTEX )
198
        {
199
        jn.mJob.addEffect(effect);
200
        return jn;
201
        }
202
      else
203
        {
204
        Job job = new Job(JOB_TYPE_VERTEX,target,null,effect);
205
        JobNode node = new JobNode(job);
206
        node.mPrevJobs.add(jn);
207
        jn.mNextJobs.add(node);
208
        mJobs.add(node);
209
        return node;
210
        }
211
      }
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  static JobNode merge(MeshBase target)
217
    {
218
    return null;
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  static JobNode join(MeshBase target, MeshBase[] meshes)
224
    {
225
    return null;
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229

    
230
  static JobNode copy(MeshBase target, MeshBase mesh)
231
    {
232
    return null;
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
/**
237
 * Only for use by the library itself.
238
 *
239
 * @y.exclude
240
 */
241
  public static void onDestroy()
242
    {
243
    int num = mJobs.size();
244

    
245
    for(int i=0; i<num; i++)
246
      {
247
      mJobs.get(i).clear();
248
      }
249

    
250
    mJobs.clear();
251
    }
252
  }
(1-1/8)