gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > seperating array data from source code?

#109391 - dext3r - Fri Nov 17, 2006 11:03 pm

Hello,
I am working on a C program for the DS that keeps track of train station data. I will have about 40 or so stations. This is how I'm storing the data:

Code:

typedef struct
{   
   u32    stationID;   
   char    *name;
   char    *color;
   u32   coordx;
   u32   coordy;
   char   *outbound;
   u32   outbound_sprite;
   char    *inbound;
   u32   inbound_sprite;
   char  *outbound_wkday;
   char    *outbound_sat;
   char   *outbound_sun;
   char    *inbound_wkday;
   char   *inbound_sat;
   char   *inbound_sun;
   char   *comments;
} stationData;


I have an array of stationDatas. In my main code I have this:
Code:

 stations[0].name = "Roosevelt";
   stations[0].stationID = 1;
   stations[0].color= "Red";
   stations[0].outbound = "95/DanRyan";
   stations[0].outbound_sprite = 1;
   stations[0].inbound = "Howard";
   stations[0].inbound_sprite = 4;
   stations[0].coordx = 100;
   stations[0].coordy = 100;
   
   stations[1].name = "Roosevelt";
   stations[1].stationID = 2;
   stations[1].color= "Green";
   stations[1].outbound = "Ashland/63";
   stations[1].outbound_sprite = 0;
   stations[1].inbound = "Harlem";
   stations[1].inbound_sprite = 3;
   stations[1].coordx = 100;
   stations[1].coordy = 100;

and so on and so forth...

Is it possible to put this data into a seperate file I can include and compile and link or something? I'd rather not have it in the main source. If station info changes, I'd rather just update the external file and recompile the application. I'd also rather not store the data in a textfile because then i'd have to use a filesystem and I'd want the app to be compatible with a majority of carts.

Thanks for any help you can provide. I've tried googling for info but I'm not sure what this technique is called. Thanks.
dext3r

#109394 - KoshNi - Fri Nov 17, 2006 11:36 pm

Does

Code:

#include "station_data.h" ;


mean something to you ? If not, have a look at this http://www.gamedev.net/reference/programming/features/orgfiles/page2.asp and/or google "include C header file"
_________________
Sunshine. Beauty. Love. Happiness.

#109398 - dext3r - Sat Nov 18, 2006 12:16 am

Thanks for the response. Perhaps I was overcomplicating this. I'm familiar with header files. I'll try some stuff. Thanks.

#109414 - ScottLininger - Sat Nov 18, 2006 2:15 am

Tepples also has provided a lightweight way to "append" data to a GBA binary file and then access it at run time. It might not be ideal for your structured kind of data, but take a look.

If nothing else, the source code may provide you with a jumping-off point to create your own version. The nice thing about this approach is that you can truly edit your data outside of the compilation workflow, allowing changes without having to recompile anything.

Program available at http://www.pineight.com/gba/#gbfs

-Scott

#109416 - sgeos - Sat Nov 18, 2006 2:44 am

If you plan to have hundred of stations, you'll want to store that info in an easy to edit format (a spreadsheet). You'll then want to write something that converts the designers data table into a code LUT.

-Brendan

#109428 - gauauu - Sat Nov 18, 2006 7:16 am

And if you do put it in a separate C file, do yourself a favor and don't put it in a header (*.h) file, but in an actual .c or assembly or bin file that you compile separately. Do a search on the forums for about five hundred threads explaining why this is a better idea.

#109444 - keldon - Sat Nov 18, 2006 10:27 am

Search for what 'exact terms'?

#109447 - KoshNi - Sat Nov 18, 2006 10:53 am

gauaa wrote:
don't put it in a header (*.h) file, but in an actual .c or assembly or bin file that you compile separately


Why not ?
_________________
Sunshine. Beauty. Love. Happiness.

#109452 - sgeos - Sat Nov 18, 2006 11:31 am

KoshNi wrote:
gauaa wrote:
don't put it in a header (*.h) file, but in an actual .c or assembly or bin file that you compile separately

Why not ?

Because data and code do not belong in header files. mydata.h should look something like this:
Code:
// mydata.h - autogenerated on 2006/11/18
#ifndef MYDATA_H
#define MYDATA_H
#include "whatever_you_need.h"

#define MYDATA_SIZE  1234
extern const type_t mydata[MYDATA_SIZE];

#endif // MYDATA_H


mydata.c should contain the actual data:
Code:
// mydata.c - autogenerated on 2006/11/18
#include "mydata.h"

const type_t mydata[MYDATA_SIZE] =
{
    {"String00", 0x0000, 0x00, 0x00},
    {"String01", 0x0001, 0x01, 0x01},
    {"String02", 0x0002, 0x02, 0x02},
// ...
    END_OF_TABLE_MARKER
};

#endif // MYDATA_H

NOTE: This code is untested.

You can either have multiple data tables for different kinds of data (my_palette_data.*, my_tile_data.*), or throw everything into one big data object. If you don't know which stragegy is better (separate fies), the stragegy you use probably doesn't matter yet.

I don't like to have to edit .c and .h files, so I'd be apt to autogenerate them from something more designer friendly. Clearly you don't need to do that if you don't want to.

-Brendan

#109495 - Peter - Sat Nov 18, 2006 8:37 pm

KoshNi wrote:
gauaa wrote:
don't put it in a header (*.h) file, but in an actual .c or assembly or bin file that you compile separately

Why not ?

Cearn wrote some words about it. It's part of his TONC series, see here.

sgeos wrote:
I don't like to have to edit .c and .h files, so I'd be apt to autogenerate them from something more designer friendly.

[advertising]
I don't like it either, so I wrote a simple program for it. The program is called Katie.

Katie concatenates binary data into one data-object and converts it into sourcecode (.c/.s) or binary again. The files inside get padded to n bytes and a header file with offsets to each file inside the data-object can be automatically generated as well. You can dowload the program here if you are interested.
[/advertising]

#109807 - dext3r - Tue Nov 21, 2006 10:36 am

Wow, I'm retarded. I totally forgot you can initialize arrays with values.

Good. I suck. Thanks to all who pointed this out. :p

#109819 - sgeos - Tue Nov 21, 2006 1:42 pm

dext3r wrote:
Wow, I'm retarded. I totally forgot you can initialize arrays with values.

Now you remember. =)

Good luck!
-Brendan