Project

General

Profile

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

library / src / main / java / org / distorted / library / mesh / DeferredJobs.java @ 07206c71

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  : break;
83
        }
84
      }
85

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

    
92
  //////////////////////////////////////////////////////////////////////////
93

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

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

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

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

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

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

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

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

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

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

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

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

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

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

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

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

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

    
178
    node.mJob.mTarget.mJobNode = null;
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
  static JobNode vertex(MeshBase target, VertexEffect effect)
184
    {
185
    JobNode jn = target.mJobNode;
186

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

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214

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

    
220
///////////////////////////////////////////////////////////////////////////////////////////////////
221

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

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

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

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

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

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