function to determine all the const arrays in a data file

Share unrelated electronics stuff, ideas, rants, etc!
User avatar
nicksen782
Posts: 714
Joined: Wed Feb 01, 2012 8:23 pm
Location: Detroit, United States
Contact:

function to determine all the const arrays in a data file

Post by nicksen782 »

I would like to write a program that takes a .inc file from gconvert as input. I would like to use those arrays and parse them with the final result being a picture of the tiles represented by the arrays in the .inc file.

Does this already exist?
CunningFellow
Posts: 1445
Joined: Mon Feb 11, 2013 8:08 am
Location: Brisbane, Australia

Re: function to determine all the const arrays in a data file

Post by CunningFellow »

If given some hints I might use my spare time next week to do a Qt program that can do that.
User avatar
nicksen782
Posts: 714
Joined: Wed Feb 01, 2012 8:23 pm
Location: Detroit, United States
Contact:

Re: function to determine all the const arrays in a data file

Post by nicksen782 »

That would be fantastic and I would appreciate any help you could give.

Still, I'm curious to know how it could actually be done. Here's my thinking: So, you have this gconvert'ed tiles.inc file and you #include it in your code and call upon the arrays stored in that file. In this case you already know the array names because you named them yourself.

My question specifically is what kind of C code could be used to read through one of these .inc files and get the names of the arrays that are stored? Something like: Program start, Scanning, Okay, I found these 6 arrays in file xzy.inc. At this point the program has the array names and works with them individually.... wait... Perhaps the convert.xml file from gconvert could be used to get the array names? I mean, the array names are in that file. Wow, solved my own problem.

Would be neat if a program could read through a xyz.inc file and return the names of the maps and tileset. Any idea on that? Seems like just curiosity now.

I think that my whole goal here is to be able to open up the gconvert generated .inc file and view it as the actual graphics that it represents and provides to the Uzebox. It would be like a whole new file format, well, more of an interpretation of an existing one. Sound pretty cool? Ideas?
User avatar
uze6666
Site Admin
Posts: 4801
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: function to determine all the const arrays in a data file

Post by uze6666 »

I think that my whole goal here is to be able to open up the gconvert generated .inc file and view it as the actual graphics that it represents and provides to the Uzebox. It would be like a whole new file format, well, more of an interpretation of an existing one. Sound pretty cool? Ideas?
Hmmm, if you already have the source image that generated the .inc file, why can't you look at the original image then? :?: Unless you want to do some sort of tile/map editor that read and writes .inc files directly. Interestingly, it does work and pretty nicely should I say. I used that technique for a tool I started for my Castlevania game. I added a bit of metadata as comments at the beginning of the file to make loading a bit simpler. Though in retrospect I don't like the overall design and it's in C# (and the metadata uses a weird format instead of xml). It should be redone in QT, since it seems *the* framework to use. If anyone interested, here's the sources. You will need Visual C# Express 2010 free from MS. Check the file CProjectFileHandler.cs, it's the one who read/writes the .inc files.

Here's the core of what you are looking for I think:

Code: Select all

  //parse the data in the C array
	    private static byte[] getArrayBytes(String src,String arrayName){
		    String tileData=getTokens(src,"const char "+arrayName+"[] PROGMEM = {","};");
		    tileData=tileData.Replace("\r", "");
		    tileData=tileData.Replace("\n", "");
		
		    if(tileData == null)
			    throw new Exception("Can not find tileset data named "+arrayName);
		
		    String[] arrayBytes=tileData.Split(new Char[] {','});
		    byte[] outb=new byte[arrayBytes.Length];
		
		    for(int j=0;j<outb.Length;j++){
                String hex=arrayBytes[j].Trim().Substring(2);
			    outb[j]=Byte.Parse(hex,NumberStyles.HexNumber);			
		    }
		
		    return outb;
	    }
	
	 
	    private static String getTokens(String data, String startToken, String endToken) {
		    int s,e;
		    String sout=null;
		    s=data.IndexOf(startToken);
		    e=data.IndexOf(endToken,s);
		    if(s>-1 && e>-1){
                sout = data.Substring(s + startToken.Length, (e - s - startToken.Length));
		    }
		    return sout;
	    }
Attachments
UzeMapper.zip
(698.51 KiB) Downloaded 457 times
User avatar
nicksen782
Posts: 714
Joined: Wed Feb 01, 2012 8:23 pm
Location: Detroit, United States
Contact:

Re: function to determine all the const arrays in a data file

Post by nicksen782 »

I'm still not entirely sure how to get all array names from a file without parsing the whole thing. I was looking for a solution where I could just bring in those arrays to a program. Seems that #include is the way to go.

I'm using C/C++ on Windows with the MinGW compiler. Everything occurs via a command prompt box until I can figure out how to do actual simple graphics. I found the library EasyBMP (http://easybmp.sourceforge.net/). I am using it to write a real .bmp file. I'm using the tileset array to figure out what each 8x8 section of the .bmp will be and then write them out.

I'm writing a 24-bit bitmap. The code is more simple that way. A 53 kb file is enough for 256 tiles. I had to strip 'PROGMEM' from the original arrays. At first I did this in a batch file with a hex editor. Now, I've found a more simple method.

Code: Select all

#define PROGMEM /*PROGMEM*/ 	// Strips 'PROGMEM' from the included file.
#include "alltiles.inc"     	// Provides array size and the tileset array. Batch file strips 'PROGMEM' from it.
It works! This post (http://uzebox.org/forums/viewtopic.php?f=6&t=396#p5727) helped with getting the colors correct.

I would like to release this tool but it's not ready yet. Perhaps I could ask a couple questions here?
  • I would like to pass the gconvert .inc file as a command-line argument. I see how to do that, however, then I would need to parse the file for values and create an array in RAM. I really like the simplicity of just using #include but then I'm using a hard-coded value and you have to edit and compile the code for different file names. Do I have to parse the file then? Everybody's file isn't going to be the same format. I could probably just search for the last '#define' and array name.

    I would really like to display the graphic in code instead of just passing the output .bmp file to MSPAINT. I would like to avoid the need to learn a new framework to do this. Suggestions?
This program will probably evolve into a tile editor with it's first goal being to be able to rearrange tiles in the tileset (and of course the maps that use it.)
User avatar
nicksen782
Posts: 714
Joined: Wed Feb 01, 2012 8:23 pm
Location: Detroit, United States
Contact:

Re: function to determine all the const arrays in a data file

Post by nicksen782 »

* bump * lol.

Okay, so I'm back in the game and I've written a utility that will parse a .inc file and create a .bmp file out of the tilesheet. It accepts a .inc file and tilesheet name as input. The output will be tilesheet.bmp where tilesheet equals what is passed to the program. It searches for "const char <tilesheetname>" then skips the rest of the line and reads up until the "};" at the end of the array.

It works. I just have to make sure that it works with more than just the test spritesheet I've been using.

I just wanted to know what the converted spritesheet would look like without having to write a function into my game to view it. This utility does that.

I'd like to release it to the community here. Should I open up a new thread or rename this one? It'll be available on the wiki too. I wonder if this is a good excuse to figure out Bitbucket now. A link to it would be better than multiple separate copies on the Uzebox website.

... parsing strings/text in C isn't nearly as easy as I thought it would be. In PHP, and even Javascript it was easier. I guess that's what you get with a full language. That and more power - and more ways to break stuff.

I'll release this soon. I just wanted to write something up about it real quick.
User avatar
D3thAdd3r
Posts: 3221
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: function to determine all the const arrays in a data file

Post by D3thAdd3r »

Thats a handy tool to have, especially since there are at least a couple games where the original unconverted artwork seems lost to the bitbucket.
User avatar
uze6666
Site Admin
Posts: 4801
Joined: Tue Aug 12, 2008 9:13 pm
Location: Montreal, Canada
Contact:

Re: function to determine all the const arrays in a data file

Post by uze6666 »

I'd like to release it to the community here. Should I open up a new thread or rename this one? It'll be available on the wiki too. I wonder if this is a good excuse to figure out Bitbucket now. A link to it would be better than multiple separate copies on the Uzebox website.
I'd create a new topic...
User avatar
nicksen782
Posts: 714
Joined: Wed Feb 01, 2012 8:23 pm
Location: Detroit, United States
Contact:

Re: function to determine all the const arrays in a data file

Post by nicksen782 »

D3thAdd3r wrote:Thats a handy tool to have, especially since there are at least a couple games where the original unconverted artwork seems lost to the bitbucket.
Lee, can you name these games? I'd like to run some tests.

The tool has evolved a bit (read: re-written twice.) I've tested it with some of the font table .inc files, my zelda game, my Flappy Bird game (unfinished), and Arkanoid. With the exception of Arkanoid my tests have been successful. Actually the problem with Arkanoid was an extra blank line under the #define for the tileset size. Gconvert doesn't do that but the .inc file looks like gconvert output. Perhaps an older version. I removed the extra blank line and my program worked fine.

It's C/C++ and I've been using with Ubuntu Linux since that is what Cloud9 IDE provides. (Check out Cloud9 IDE at c9.io).

I would be very interested in some pointers (no pun intended) concerning the code. It uses the EasyBMP library and the standard libraries.
User avatar
D3thAdd3r
Posts: 3221
Joined: Wed Apr 29, 2009 10:00 am
Location: Minneapolis, United States

Re: function to determine all the const arrays in a data file

Post by D3thAdd3r »

I know for sure the original bitmaps for UzeSweeper and Sokoban World are lost. I'd be interested if the tool would be able to recover them as they are the format from TileStudio and likely manually edited by myself at some point. Source for those is here for now until I eventually get all my source organized in 1 place(SVN):
http://216.189.148.140/UZESWEEPsrc.zip
http://216.189.148.140/SOKOWRLDsrc.zip

Looking at uzebox_src_3.3.zip and quickly through all the individual source .zip files I can't seem to find the graphics for these ones:
Atomix,Dr.Mario, Maze,SDCardDemo,SpriteDemo,tutorial,VectorDemo,Fireman Rescue,Pong,G-Force,zombienator

I didn't list any for games that I couldn't find source for so I could have missed 1 or 2. There are a lot of existing games that the source isn't available on, I am going to message and bother these people next year to fix it :lol:
Post Reply