Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1051 | chris | 1 | package com.gebauz.bauzoid.graphics.spritex; |
| 2 | |||
| 3 | import com.badlogic.gdx.Gdx; |
||
| 4 | import com.badlogic.gdx.files.FileHandle; |
||
| 5 | import com.gebauz.bauzoid.app.Consts; |
||
| 6 | import com.gebauz.bauzoid.graphics.Graphics; |
||
| 7 | import com.gebauz.bauzoid.parser.Preprocessor; |
||
| 8 | import com.gebauz.bauzoid.parser.ScanException; |
||
| 9 | import com.gebauz.bauzoid.parser.Tokenizer; |
||
| 10 | |||
| 11 | public class SpriteUtil |
||
| 12 | { |
||
| 13 | public static final String LOG_TAG = Consts.LOG_TAG + ":SpriteUtil"; |
||
| 14 | public static boolean verbose = true; |
||
| 15 | |||
| 16 | private SpriteUtil() {} |
||
| 17 | |||
| 18 | public static void log(String tag, String msg) |
||
| 19 | { |
||
| 20 | if (verbose) |
||
| 21 | Gdx.app.log(tag, msg); |
||
| 22 | } |
||
| 23 | |||
| 24 | static public AtlasDefinition readAtlasSpriteInfo(FileHandle file) |
||
| 25 | { |
||
| 26 | String fileContents = Preprocessor.stripComments(file.readString()); |
||
| 27 | |||
| 28 | AtlasDefinition spriteInfo = new AtlasDefinition(); |
||
| 29 | |||
| 30 | try |
||
| 31 | { |
||
| 32 | Tokenizer tokenizer = new Tokenizer(fileContents); |
||
| 33 | tokenizer.setStringDelimiter(new char[] {'\'', '"'} ); |
||
| 34 | while (!tokenizer.checkNoMoreTokens()) |
||
| 35 | { |
||
| 36 | String identifier = tokenizer.readIdentifier(); |
||
| 37 | if (identifier.equalsIgnoreCase("frame")) |
||
| 38 | { |
||
| 39 | spriteInfo.frames.add(readFrameLine(tokenizer)); |
||
| 40 | } |
||
| 41 | else |
||
| 42 | { |
||
| 43 | throw new ScanException(LOG_TAG, "Syntax error at: " + tokenizer.getSurroundings()); |
||
| 44 | } |
||
| 45 | } |
||
| 46 | } |
||
| 47 | catch (ScanException e) |
||
| 48 | { |
||
| 49 | e.log(LOG_TAG); |
||
| 50 | return null; |
||
| 51 | } |
||
| 52 | |||
| 53 | // Check must-have info |
||
| 54 | /*if (spriteInfo.textureFile == null) |
||
| 55 | { |
||
| 56 | log(LOG_TAG, "Atlas definition does not contain any texture file!"); |
||
| 57 | return null; |
||
| 58 | }*/ |
||
| 59 | |||
| 60 | if (spriteInfo.frames.size() == 0) |
||
| 61 | { |
||
| 62 | log(LOG_TAG, "Atlas definition does not contain any frame information!"); |
||
| 63 | return null; |
||
| 64 | } |
||
| 65 | |||
| 66 | return spriteInfo; |
||
| 67 | } |
||
| 68 | |||
| 69 | static public AtlasSprite createAtlasSpriteFromFile(Graphics graphics, String textureFile, FileHandle file) |
||
| 70 | { |
||
| 71 | AtlasDefinition spriteInfo = readAtlasSpriteInfo(file); |
||
| 72 | if (spriteInfo != null) |
||
| 73 | { |
||
| 74 | AtlasSprite sprite = new AtlasSprite(graphics, textureFile); |
||
| 75 | sprite.init(); |
||
| 76 | |||
| 77 | SpriteRegion regions[] = new SpriteRegion[spriteInfo.frames.size()]; |
||
| 78 | for (int i = 0; i < spriteInfo.frames.size(); i++) |
||
| 79 | { |
||
| 80 | regions[i] = new SpriteRegion(sprite.getTexture(), spriteInfo.frames.get(i).x, spriteInfo.frames.get(i).y, spriteInfo.frames.get(i).w, spriteInfo.frames.get(i).h); |
||
| 81 | } |
||
| 82 | sprite.setRegions(regions); |
||
| 83 | |||
| 84 | return sprite; |
||
| 85 | } |
||
| 86 | return null; |
||
| 87 | } |
||
| 88 | |||
| 89 | /*static private String readTextureLine(Tokenizer t) throws ScanException |
||
| 90 | { |
||
| 91 | String result = t.readString(); |
||
| 92 | t.readToken(";"); |
||
| 93 | return result; |
||
| 94 | }*/ |
||
| 95 | |||
| 96 | static private AtlasDefinition.FrameInfo readFrameLine(Tokenizer t) throws ScanException |
||
| 97 | { |
||
| 98 | float x = 0; |
||
| 99 | float y = 0; |
||
| 100 | float w = 0; |
||
| 101 | float h = 0; |
||
| 102 | |||
| 103 | x = t.readNumber(); |
||
| 104 | t.readToken(", "); |
||
| 105 | |||
| 106 | y = t.readNumber(); |
||
| 107 | t.readToken(", "); |
||
| 108 | |||
| 109 | w = t.readNumber(); |
||
| 110 | t.readToken(", "); |
||
| 111 | |||
| 112 | h = t.readNumber(); |
||
| 113 | t.readToken(";"); |
||
| 114 | |||
| 115 | return new AtlasDefinition.FrameInfo(x, y, w, h); |
||
| 116 | } |
||
| 117 | |||
| 118 | // public AnimatedSprite createAnimatedSpriteFromFile(Graphics graphics, FileHandle file) |
||
| 119 | // { |
||
| 120 | // return null; |
||
| 121 | // } |
||
| 122 | |||
| 123 | } |