Skip to content

Commit cd94cc3

Browse files
authored
Merge pull request #4 from Interrupt/texture-picker
Refactored textures into intermediate surfaces. Fixed a bunch of saving / loading bugs.
2 parents 15f15f0 + 209f5bd commit cd94cc3

File tree

11 files changed

+153
-56
lines changed

11 files changed

+153
-56
lines changed

Diff for: core/src/com/interrupt/doomtest/DoomLikeEditor.java

+58-13
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66
import com.badlogic.gdx.InputMultiplexer;
77
import com.badlogic.gdx.files.FileHandle;
88
import com.badlogic.gdx.graphics.*;
9-
import com.badlogic.gdx.graphics.g2d.TextureRegion;
109
import com.badlogic.gdx.graphics.g3d.Material;
1110
import com.badlogic.gdx.graphics.g3d.ModelInstance;
1211
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
13-
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
1412
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
1513
import com.badlogic.gdx.math.Intersector;
1614
import com.badlogic.gdx.math.Plane;
@@ -30,6 +28,7 @@
3028
import com.interrupt.doomtest.levels.Level;
3129
import com.interrupt.doomtest.levels.Line;
3230
import com.interrupt.doomtest.levels.Sector;
31+
import com.interrupt.doomtest.levels.Surface;
3332
import com.interrupt.doomtest.levels.editor.Editor;
3433

3534
public class DoomLikeEditor extends ApplicationAdapter {
@@ -85,7 +84,7 @@ public enum EditorModes { SECTOR, POINT, SPLIT };
8584

8685
public static float GRID_SNAP = 2f;
8786

88-
public TextureRegion currentTexture;
87+
public Surface currentTexture;
8988
public Stage hudStage;
9089

9190
@Override
@@ -113,8 +112,8 @@ public void create () {
113112
OrthographicCamera hudCamera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
114113

115114
// Load textures
116-
Array<TextureRegion> textures = loadTexturesFromAtlas("textures/textures.png");
117-
textures.add(new TextureRegion(Art.getTexture("textures/wall1.png")));
115+
Array<Surface> textures = loadTexturesFromAtlas("textures/textures.png");
116+
textures.add(new Surface("textures/wall1.png"));
118117

119118
// Setup the menu / HUD
120119
hudStage = Hud.create(textures, textures.get(textures.size - 1), this);
@@ -379,7 +378,6 @@ else if(editorMode == EditorModes.POINT) {
379378
else if(hoveredLine != null) pickedLine = hoveredLine;
380379
else if(hoveredSector != null) pickedSector = hoveredSector;
381380

382-
setHighlights();
383381
refreshRenderer();
384382

385383
lastMousePoint.set(Gdx.input.getX(), Gdx.input.getY());
@@ -481,8 +479,8 @@ public void finishSector() {
481479
level.sectors.add(current);
482480

483481
// set texture
484-
current.floorMaterial.set(TextureAttribute.createDiffuse(currentTexture));
485-
current.ceilingMaterial.set(TextureAttribute.createDiffuse(currentTexture));
482+
current.floorMaterial.match(currentTexture);
483+
current.ceilingMaterial.match(currentTexture);
486484
}
487485

488486

@@ -500,7 +498,7 @@ public void finishSector() {
500498
editPlane.set(Vector3.Zero, Vector3.Y);
501499
}
502500

503-
public void setHighlights() {
501+
/*public void setHighlights() {
504502
for(Sector s : level.sectors) {
505503
resetSectorHighlights(s);
506504
}
@@ -525,7 +523,7 @@ public void resetSectorHighlights(Sector sector) {
525523
526524
public void resetWallHighlights(Line line) {
527525
line.lowerMaterial.set(ColorAttribute.createDiffuse(Color.WHITE));
528-
}
526+
}*/
529527

530528
@Override
531529
public void render () {
@@ -731,11 +729,15 @@ public void renderGrid() {
731729
lineRenderer.end();
732730
}
733731

734-
public Array<TextureRegion> loadTexturesFromAtlas(String filename) {
732+
public String getTextureAtlasKey(String filename, int x, int y) {
733+
return filename + "_" + x + "_" + y;
734+
}
735+
736+
public Array<Surface> loadTexturesFromAtlas(String filename) {
735737
Pixmap atlas = new Pixmap(Gdx.files.local(filename));
736738
int texSize = atlas.getWidth() / 4;
737739

738-
Array<TextureRegion> textures = new Array<TextureRegion>();
740+
Array<Surface> textures = new Array<Surface>();
739741

740742
for(int x = 0; x < atlas.getWidth() / texSize; x++) {
741743
for(int y = 0; y < atlas.getHeight() / texSize; y++) {
@@ -746,13 +748,24 @@ public Array<TextureRegion> loadTexturesFromAtlas(String filename) {
746748
texture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
747749
texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
748750

749-
textures.add(new TextureRegion(texture));
751+
String atlasKey = getTextureAtlasKey(filename, x, y);
752+
Surface surface = new Surface(atlasKey);
753+
754+
Art.cacheTexture(atlasKey, texture);
755+
756+
textures.add(surface);
750757
}
751758
}
752759

753760
return textures;
754761
}
755762

763+
public void newLevel() {
764+
level = new Level();
765+
editor.level = level;
766+
refreshRenderer();
767+
}
768+
756769
public void saveLevel(FileHandle file) {
757770
Json json = new Json();
758771
file.writeString(json.prettyPrint(level), false);
@@ -762,5 +775,37 @@ public void openLevel(FileHandle file) {
762775
Json json = new Json();
763776
String js = file.readString();
764777
level = json.fromJson(Level.class, js);
778+
779+
for(Sector s : level.sectors) {
780+
matchSectorVertices(level.vertices, s);
781+
}
782+
for(Line l : level.lines) {
783+
matchLineVertices(level.vertices, l);
784+
}
785+
editor.level = level;
786+
refreshRenderer();
787+
}
788+
789+
public void matchSectorVertices(Array<Vector2> vertices, Sector sector) {
790+
for(int i = 0; i < sector.points.size; i++) {
791+
Vector2 p = sector.points.get(i);
792+
int existing = vertices.indexOf(p, false);
793+
if(existing >= 0) {
794+
sector.points.set(i, vertices.get(existing));
795+
}
796+
}
797+
798+
for(Sector s : sector.subsectors) {
799+
matchSectorVertices(vertices, s);
800+
}
801+
}
802+
803+
public void matchLineVertices(Array<Vector2> vertices, Line line) {
804+
Vector2 start = line.start;
805+
Vector2 end = line.end;
806+
int existingStart = vertices.indexOf(start, false);
807+
int existingEnd = vertices.indexOf(end, false);
808+
if(existingStart >= 0) line.start = vertices.get(existingStart);
809+
if(existingEnd >= 0) line.end = vertices.get(existingEnd);
765810
}
766811
}

Diff for: core/src/com/interrupt/doomtest/editor/ui/Hud.java

+11-15
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
import com.badlogic.gdx.files.FileHandle;
66
import com.badlogic.gdx.graphics.OrthographicCamera;
77
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
8-
import com.badlogic.gdx.graphics.g2d.TextureRegion;
9-
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
108
import com.badlogic.gdx.scenes.scene2d.Actor;
119
import com.badlogic.gdx.scenes.scene2d.InputEvent;
1210
import com.badlogic.gdx.scenes.scene2d.InputListener;
@@ -25,6 +23,7 @@
2523
import com.interrupt.doomtest.editor.ui.menu.MenuItem;
2624
import com.interrupt.doomtest.editor.ui.menu.Scene2dMenuBar;
2725
import com.interrupt.doomtest.levels.Level;
26+
import com.interrupt.doomtest.levels.Surface;
2827

2928
import javax.swing.*;
3029
import java.awt.*;
@@ -42,7 +41,7 @@ private static Skin loadSkin() {
4241
new TextureAtlas(Gdx.files.local("ui/HoloSkin/Holo-dark-ldpi.atlas")));
4342
}
4443

45-
public static Stage create(final Array<TextureRegion> textures, TextureRegion current, final DoomLikeEditor doomLikeEditor) {
44+
public static Stage create(final Array<Surface> textures, Surface current, final DoomLikeEditor doomLikeEditor) {
4645

4746
editor = doomLikeEditor;
4847

@@ -60,7 +59,7 @@ public static Stage create(final Array<TextureRegion> textures, TextureRegion cu
6059
.addItem(new MenuItem("Open", hudSkin, openAction)));
6160
menuBar.pack();
6261

63-
final Image texturePickerButton = new Image(new TextureRegionDrawable(current));
62+
final Image texturePickerButton = new Image(new TextureRegionDrawable(current.getTextureRegion()));
6463
texturePickerButton.setScaling(Scaling.stretch);
6564

6665
Table wallPickerLayoutTable = new Table();
@@ -77,9 +76,9 @@ public static Stage create(final Array<TextureRegion> textures, TextureRegion cu
7776
public void clicked(InputEvent event, float x, float y) {
7877
TextureRegionPicker picker = new TextureRegionPicker("Pick Current Texture", hudSkin, textures) {
7978
@Override
80-
public void result(Integer value, TextureRegion region) {
79+
public void result(Integer value, Surface region) {
8180
setTexture(region, editor);
82-
texturePickerButton.setDrawable(new TextureRegionDrawable(region));
81+
texturePickerButton.setDrawable(new TextureRegionDrawable(region.getTextureRegion()));
8382
}
8483
};
8584
stage.addActor(picker);
@@ -105,13 +104,13 @@ public boolean touchDown(InputEvent event, float x, float y, int pointer, int bu
105104
return stage;
106105
}
107106

108-
public static void setTexture(TextureRegion texture, DoomLikeEditor editor) {
107+
public static void setTexture(Surface texture, DoomLikeEditor editor) {
109108
editor.currentTexture = texture;
110109

111110
if (editor.pickedLine != null) {
112-
editor.pickedLine.lowerMaterial.set(TextureAttribute.createDiffuse(texture));
111+
editor.pickedLine.lowerMaterial.match(texture);
113112
} else if (editor.pickedSector != null) {
114-
editor.pickedSector.floorMaterial.set(TextureAttribute.createDiffuse(texture));
113+
editor.pickedSector.floorMaterial.match(texture);
115114
}
116115
editor.refreshRenderer();
117116
}
@@ -161,7 +160,7 @@ public boolean accept(File dir, String name) {
161160
if (dir != null && file != null && file.trim().length() != 0) {
162161
currentFileName = file;
163162
currentDirectory = dir;
164-
editor.saveLevel(new FileHandle(dir + file));
163+
editor.saveLevel(Gdx.files.absolute((dir + file)));
165164
}
166165

167166
frame.dispose();
@@ -171,8 +170,7 @@ public boolean accept(File dir, String name) {
171170
private static ActionListener newAction = new ActionListener() {
172171
@Override
173172
public void actionPerformed(ActionEvent e) {
174-
editor.level = new Level();
175-
editor.refreshRenderer();
173+
editor.newLevel();
176174
}
177175
};
178176

@@ -200,7 +198,7 @@ public boolean accept(File dir, String name) {
200198

201199
if (dialog.getFile() != null) {
202200
currentFileName = dialog.getFile();
203-
currentDirectory = dialog.getDirectory();
201+
currentDirectory = "";
204202
}
205203

206204
final String file = dialog.getFile();
@@ -211,8 +209,6 @@ public boolean accept(File dir, String name) {
211209
if (level.exists()) {
212210
currentFileName = level.path();
213211
editor.openLevel(Gdx.files.absolute(dir + file));
214-
editor.refreshRenderer();
215-
216212
frame.dispose();
217213
}
218214
}

Diff for: core/src/com/interrupt/doomtest/editor/ui/TextureRegionPicker.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
1111
import com.badlogic.gdx.utils.Array;
1212
import com.badlogic.gdx.utils.Scaling;
13+
import com.interrupt.doomtest.gfx.Art;
14+
import com.interrupt.doomtest.levels.Surface;
1315

1416
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.sequence;
1517

@@ -21,11 +23,10 @@ public abstract class TextureRegionPicker extends Window {
2123
ScrollPane pane;
2224
Table buttonLayout;
2325
Cell paneCell;
24-
SelectBox selectBox;
2526

26-
final Array<TextureRegion> regions;
27+
final Array<Surface> regions;
2728

28-
public TextureRegionPicker(String title, Skin skin, Array<TextureRegion> regions) {
29+
public TextureRegionPicker(String title, Skin skin, Array<Surface> regions) {
2930
super(title, skin);
3031

3132
this.regions = regions;
@@ -77,8 +78,8 @@ protected void makeRegionButtons() {
7778
buttonLayout.reset();
7879

7980
int num = 0;
80-
for(TextureRegion region : regions) {
81-
ImageButton button = new ImageButton(new TextureRegionDrawable(region));
81+
for(Surface region : regions) {
82+
ImageButton button = new ImageButton(new TextureRegionDrawable(region.getTextureRegion()));
8283
button.getImage().setScaling(Scaling.fill);
8384
button.getImageCell().width(spriteSize).height(spriteSize);
8485

@@ -107,7 +108,7 @@ public boolean touchDown (InputEvent event, float x, float y, int pointer, int b
107108
}
108109
};
109110

110-
public void button(Button button, final Integer value, final TextureRegion region) {
111+
public void button(Button button, final Integer value, final Surface region) {
111112
buttonLayout.add(button).pad(4f);
112113

113114
button.addListener(new ClickListener() {
@@ -162,5 +163,5 @@ public void hide (Action action) {
162163
remove();
163164
}
164165

165-
public abstract void result(Integer value, TextureRegion region);
166+
public abstract void result(Integer value, Surface region);
166167
}

Diff for: core/src/com/interrupt/doomtest/gfx/Art.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ public static Texture getTexture(String filename) {
1717
texture.setWrap(Texture.TextureWrap.Repeat, Texture.TextureWrap.Repeat);
1818
texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest);
1919

20-
loadedTextures.put(filename, texture);
20+
cacheTexture(filename, texture);
2121

2222
return texture;
2323
}
24+
25+
public static void cacheTexture(String key, Texture texture) {
26+
loadedTextures.put(key, texture);
27+
}
2428
}

Diff for: core/src/com/interrupt/doomtest/gfx/tesselators/SectorTesselator.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package com.interrupt.doomtest.gfx.tesselators;
22

3+
import com.badlogic.gdx.graphics.Color;
4+
import com.badlogic.gdx.graphics.g3d.Material;
35
import com.badlogic.gdx.graphics.g3d.Model;
46
import com.badlogic.gdx.graphics.g3d.ModelInstance;
7+
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
8+
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
59
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
610
import com.badlogic.gdx.math.Matrix4;
711
import com.badlogic.gdx.math.Vector2;
812
import com.badlogic.gdx.utils.Array;
13+
import com.interrupt.doomtest.gfx.Art;
914
import com.interrupt.doomtest.levels.Sector;
1015
import org.lwjgl.util.glu.GLUtessellator;
1116

@@ -18,7 +23,7 @@ public class SectorTesselator {
1823

1924
public static Array<ModelInstance> tesselate(Sector sector) {
2025

21-
TessCallback callback = new TessCallback(sector.floorMaterial);
26+
TessCallback callback = new TessCallback();
2227

2328
tesselator.gluTessCallback(GLU_TESS_VERTEX, callback);
2429
tesselator.gluTessCallback(GLU_TESS_BEGIN, callback);
@@ -74,8 +79,11 @@ private static Model makeFloorModel(Sector sector, Array<TessCallback.MeshPiece>
7479
ModelBuilder mb = new ModelBuilder();
7580
mb.begin();
7681
int indx = 0;
82+
83+
Material material = sector.floorMaterial.createMaterial(sector.hashCode() + "_floor");
84+
7785
for(TessCallback.MeshPiece m : meshPieces) {
78-
mb.part((indx++) + "", m.mesh, m.drawType, sector.floorMaterial);
86+
mb.part((indx++) + "", m.mesh, m.drawType, material);
7987
}
8088
return mb.end();
8189
}
@@ -84,9 +92,11 @@ private static Model makeCeilingModel(Sector sector, Array<TessCallback.MeshPiec
8492
ModelBuilder mb = new ModelBuilder();
8593
mb.begin();
8694
int indx = 0;
87-
Matrix4 transform = new Matrix4().translate(0, sector.ceilHeight - sector.floorHeight, 0);
95+
96+
Material material = sector.ceilingMaterial.createMaterial(sector.hashCode() + "_ceiling");
97+
8898
for(TessCallback.MeshPiece m : meshPieces) {
89-
mb.part((indx++) + "", m.mesh, m.drawType, sector.ceilingMaterial);
99+
mb.part((indx++) + "", m.mesh, m.drawType, material);
90100
}
91101
return mb.end();
92102
}

Diff for: core/src/com/interrupt/doomtest/gfx/tesselators/TessCallback.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,8 @@ public class MeshPiece {
3434

3535
public Array<MeshPiece> meshPieces = new Array<MeshPiece>();
3636

37-
public TessCallback(Material material) {
37+
public TessCallback() {
3838
modelBuilder.begin();
39-
this.material = material;
4039
}
4140

4241
public void begin(int type) {

0 commit comments

Comments
 (0)