Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Efficient parsing?  (Read 1032 times)
0 Members and 1 Guest are viewing this topic.
Offline Tyecon

Senior Newbie

« Posted 2013-04-02 17:28:40 »
This is my Compatiblity.shader file, and I'm combining the .vert and .frag shaders into one file. My current code for parsing is:
String shader=AssetHandler.readFile(path+".shader");
            String version=shader.substring(
                    (shader.indexOf("<VERSION>")==-1) ? 0 : shader.indexOf("<VERSION>")+9,
                    (shader.indexOf("</VERSION>")==-1) ? 0 : shader.indexOf("</VERSION>")).trim();
                    ((version.isEmpty()) ? "" : ("#version "+version))
                    + shader.substring(shader.indexOf("<VERT>")+6, shader.indexOf("</VERT>")), GL_VERTEX_SHADER);
                    ((version.isEmpty()) ? "" : ("#version "+version))
                    + shader.substring(shader.indexOf("<FRAG>")+6, shader.indexOf("</FRAG>")), GL_FRAGMENT_SHADER);

Is this ok? I feel like it could be a lot cleaner and nicer. I'm going to add a <varying> tag next.

Please have a baggy ready before looking at my code.
Offline quew8

JGO Knight

Medals: 53

« Reply #1 - Posted 2013-04-03 15:12:53 »

If your going to be doing some serious parsing, might I suggest using JavaCC. I find it to be a wonderful tool that is as simple as it needs to be. If your unaware of it, you write a syntax for it and it will generate the source code for a java parser that will parse your input and perform any actions you add in.
Offline StrideColossus
« Reply #2 - Posted 2013-04-03 15:32:41 »

Is this ok? I feel like it could be a lot cleaner and nicer. I'm going to add a <varying> tag next.

Couple of suggestions you may want to consider:

1. Encapsulate the code that extracts the text from between the XML tags into a helper method:
- you can then write a unit-test to make sure that chunk works
- the code that's doing the actual parsing will then be a lot cleaner (and therefore less buggy / easier to maintain)
- and you won't be cut-and-pasting and hard-coding the end index when you add further functionality, further reducing chances of cock-ups Smiley

2. Avoid duplicating the indexOf() call - OK the performance of doing it twice is probably hardly game-breaking but it's good practice.

3. Is there a reason you're not just throwing exceptions for the cases where the file is not valid (no <version> tag)?  You'll have to check elsewhere that the vertex/fragment shader is potentially empty which (presumably) is a pretty fatal scenario, so you might as well handle that explicitly with an exception.

Pages: [1]
  ignore  |  Print  

EgonOlsen (74 views)
2018-06-10 19:43:48

EgonOlsen (55 views)
2018-06-10 19:43:44

EgonOlsen (74 views)
2018-06-10 19:43:20

DesertCoockie (254 views)
2018-05-13 18:23:11

nelsongames (154 views)
2018-04-24 18:15:36

nelsongames (154 views)
2018-04-24 18:14:32

ivj94 (895 views)
2018-03-24 14:47:39

ivj94 (156 views)
2018-03-24 14:46:31

ivj94 (808 views)
2018-03-24 14:43:53

Solater (172 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!