Starsector API
Loading...
Searching...
No Matches
WarpingSpriteRendererUtilV2.java
Go to the documentation of this file.
1package com.fs.starfarer.api.impl.combat.dweller;
2
3import java.awt.Color;
4
5import org.lwjgl.opengl.GL11;
6
7import com.fs.starfarer.api.graphics.SpriteAPI;
8import com.fs.starfarer.api.util.WarpingSpriteRendererUtil.MutatingValue;
9
11
12 public static class WSVertex {
13 public MutatingValue theta;
14 public MutatingValue radius;
15
16 public WSVertex() {
17 //theta = new MutatingValue(-360f * ((float) Math.random() * 3f + 1f), 360f * ((float) Math.random() * 3f + 1f), 30f + 70f * (float) Math.random());
18 theta = new MutatingValue(-360f * ((float) Math.random() * 30f + 1f), 360f * ((float) Math.random() * 30f + 1f), 30f + 70f * (float) Math.random());
19 radius = new MutatingValue(0, 10f + 15f * (float) Math.random(), 3f + 7f * (float) Math.random());
20 }
21
22 public void advance(float amount) {
23 theta.advance(amount);
24 radius.advance(amount);
25 }
26
27 Object writeReplace() {
28 theta.setMax((int)theta.getMax());
29 theta.setMin((int)theta.getMin());
30 theta.setRate((int)theta.getRate());
31 theta.setValue((int)theta.getValue());
32
33 radius.setMax((int)radius.getMax());
34 radius.setMin((int)radius.getMin());
35 radius.setRate((int)radius.getRate());
36 radius.setValue((int)radius.getValue());
37 return this;
38 }
39 }
40
41 protected int verticesWide, verticesTall;
42 protected WSVertex [] [] vertices;
43 protected SpriteAPI sprite;
44 protected boolean mirror = false;
45
47 float minWarpRadius, float maxWarpRadius, float warpRateMult) {
48
49 this.sprite = sprite;
50 this.verticesWide = verticesWide;
51 this.verticesTall = verticesTall;
52
53 vertices = new WSVertex[verticesWide][verticesTall];
54 for (int i = 0; i < verticesWide; i++) {
55 for (int j = 0; j < verticesTall; j++) {
56 vertices[i][j] = new WSVertex();
57
58 vertices[i][j].radius.set(minWarpRadius, maxWarpRadius);
59 vertices[i][j].radius.rate *= warpRateMult;
60 vertices[i][j].theta.rate *= warpRateMult;
61
62 }
63 }
64 }
65
70 public void setWarpRateMult(float mult) {
71 for (int i = 0; i < verticesWide; i++) {
72 for (int j = 0; j < verticesTall; j++) {
73 vertices[i][j].radius.rate *= mult;
74 vertices[i][j].theta.rate *= mult;
75 }
76 }
77 }
78
79 public void advance(float amount) {
80 for (int i = 0; i < verticesWide; i++) {
81 for (int j = 0; j < verticesTall; j++) {
82 vertices[i][j].advance(amount);
83 }
84 }
85 }
86
87 public void renderAtCenter(float x, float y) {
88 float w = sprite.getWidth();
89 float h = sprite.getHeight();
90
91 x -= w/2f;
92 y -= h/2f;
93
95 GL11.glPushMatrix();
96
97 Color color = sprite.getColor();
98 GL11.glColor4ub((byte) color.getRed(), (byte) color.getGreen(),
99 (byte) color.getBlue(), (byte)(color.getAlpha() * sprite.getAlphaMult()));
100
101 // translate to the right location and prepare to draw
102 GL11.glTranslatef(x, y, 0);
103
104 float centerX = sprite.getCenterX();
105 float centerY = sprite.getCenterY();
106 float angle = sprite.getAngle();
107 // translate to center, rotate, translate back
108 if (centerX != -1 && centerY != -1) {
109 GL11.glTranslatef(w / 2, h / 2, 0);
110 GL11.glRotatef(angle, 0, 0, 1);
111 GL11.glTranslatef(- centerX, - centerY, 0);
112 } else {
113 GL11.glTranslatef(w / 2, h / 2, 0);
114 GL11.glRotatef(angle, 0, 0, 1);
115 GL11.glTranslatef(-w / 2, -h / 2, 0);
116 }
117
118 int blendSrc = sprite.getBlendSrc();
119 int blendDest = sprite.getBlendDest();
120 GL11.glEnable(GL11.GL_TEXTURE_2D);
121 GL11.glEnable(GL11.GL_BLEND);
122 GL11.glBlendFunc(blendSrc, blendDest);
123
124 float tw = sprite.getTextureWidth() - 0.001f;
125 float th = sprite.getTextureHeight() - 0.001f;
126
127 float cw = w / (float) (verticesWide - 1);
128 float ch = h / (float) (verticesTall - 1);
129 float ctw = tw / (float) (verticesWide - 1);
130 float cth = th / (float) (verticesTall- 1);
131
132 for (float i = 0; i < verticesWide - 1; i++) {
133 //for (float i = 5; i < 7; i++) {
134 GL11.glBegin(GL11.GL_QUAD_STRIP);
135 {
136 for (float j = 0; j < verticesTall; j++) {
137 float x1 = cw * i;
138 float y1 = ch * j;
139 float x2 = cw * (i + 1f);
140 float y2 = ch * j;
141
142 float tx1 = ctw * i;
143 float ty1 = cth * j;
144 float tx2 = ctw * (i + 1f);
145 float ty2 = cth * j;
146
147 if (mirror) {
148 tx1 = tw - tx1;
149 tx2 = th - tx2;
150 }
151
152 if (i != 0 && i != verticesWide - 1 && j != 0 && j != verticesTall - 1) {
153 float theta = (float) Math.toRadians(vertices[(int)i][(int)j].theta.getValue());
154 float radius = vertices[(int)i][(int)j].radius.getValue();
155 float sin = (float) Math.sin(theta);
156 float cos = (float) Math.cos(theta);
157
158 x1 += cos * radius;
159 y1 += sin * radius;
160 //System.out.println("Radius: " + radius);
161 }
162
163 if (i + 1 != 0 && i + 1 != verticesWide - 1 && j != 0 && j != verticesTall - 1) {
164 float theta = (float) Math.toRadians(vertices[(int)i + 1][(int)j].theta.getValue());
165 float radius = vertices[(int)i + 1][(int)j].radius.getValue();
166 float sin = (float) Math.sin(theta);
167 float cos = (float) Math.cos(theta);
168
169 x2 += cos * radius;
170 y2 += sin * radius;
171 //System.out.println("Radius: " + radius);
172 }
173
174 GL11.glTexCoord2f(tx1, ty1);
175 GL11.glVertex2f(x1, y1);
176
177 GL11.glTexCoord2f(tx2, ty2);
178 GL11.glVertex2f(x2, y2);
179 }
180 }
181 GL11.glEnd();
182
183 }
184
185 GL11.glPopMatrix();
186 }
187
188 public int getVerticesWide() {
189 return verticesWide;
190 }
191
192 public int getVerticesTall() {
193 return verticesTall;
194 }
195
197 return sprite;
198 }
199
200 public boolean isMirror() {
201 return mirror;
202 }
203
204 public void setMirror(boolean mirror) {
205 this.mirror = mirror;
206 }
207
208}
209
210
211
212
213
214
215
216
217
218
219
220
221
222
WarpingSpriteRendererUtilV2(SpriteAPI sprite, int verticesWide, int verticesTall, float minWarpRadius, float maxWarpRadius, float warpRateMult)