PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Binary data and GameSpeed

Thanks for answering my previous stupid questions.
:) And of course there are some new questions(I'm absolutely new at eVC).
In my new tile-based game I want to load binary coded data of the level(the level is too big to use a *.txt file) into the game.

What is the best way to store binary data? (I think you suggest STL vector), but I can't get at the meaning of example I found on http://www.cs.rpi.edu/projects/STL( and of course _bvector.h for me is useless).
So I tryied to look into eVC help and found something like 'CLongBinary' and 'CByteArray'(How to use it?).
But the main problem is: I can't even load a .bin file from main memory.
Please give me a simple example if you can. (Just like adde wrote about STL vector and strings).

How to provide gamespeed independent from the fps. I'm using now simple for me method:
every N-th frame I move sprite to new position and do some animation(I've already seen a topic about limiting fps).
Is there any better way :?:
When you say "binary data", that's a somewhat ambiguous term. However, I think one answer that would cover any meaning would be to simply have a chunk of memory that you move through via a pointer.

I actually do that in my current game... I have a scripting engine for cut scenes, and the data is stored simply as a series of char's. I actually write it out from an Excel spreadsheet (I wrote a script editor there, temporarily, it was the quickest and easiest way) and it writes out a data file that's just a series of bytes.

I then import that as a resource, then I just get a handle to it and use a simple pointer to move through it. If your not going to be changing the data, and from what you wrote I don't think you would, this approach might be the best, but it's at least one method to consider. If your going to be making changes, or need truly random data access (as opposed to just moving through linearly or with a known fomula you can apply to get where you want), then you might consider vectors or something else, but that sounds to me too heavyweight for what you described.

The question about game speed I don't quite understand... are you talking about how to limit the speed of a sprite to less than your actual FPS? If so, there are two answers I can think of: (1) Do what you do, move the sprites every x number of frames, or (2) move the sprite x number of pixels per frame, where x increases as the speed of your sprite increases (limited to your FPS as the slowest speed). Not sure which might be appropriate, or if there is a better approach, because I wasn't 100% clear on what you were asking...
fzammetti wrote:
The question about game speed I don't quite understand... are you talking about how to limit the speed of a sprite to less than your actual FPS? If so, there are two answers I can think of: (1) Do what you do, move the sprites every x number of frames, or (2) move the sprite x number of pixels per frame, where x increases as the speed of your sprite increases (limited to your FPS as the slowest speed). Not sure which might be appropriate, or if there is a better approach, because I wasn't 100% clear on what you were asking...


Yes this is what I meant and this method I also use, but some episodes of my game is a little bit slower (in fps) than others, so using this method affects on real game speed.
Programming on Visual Basic I just made two loops: one was a game loop and the other was a Timer.Timer event for "rendering".
Strictly speaking, all data is binary data (as FZammetti pointed out). However, the data of an ascii file (or .txt file if you may) is encoded according to the ASCII standard where 1 (one) is 49 and not 1, or in binary code (hexadecimal) 0x31 and not 0x01.

So, the question is not how to represent binary data but how to represent the data. A good representation can save you a lot of work, and it all depends on the situation.

But, if you want my advice, STAY AWAY FROM EVERYTHING THAT IS MFC!! because it requires your application to link to the MFC library and you don't want that because its large and slow (generally speaking). How can you tell something is MFC? The classes begin with a C (like CByteArray that you mentioned).

Once you've figured out a nice structure for your program data you can begin to worry about how to save and load it from file. There are a lot of different ways to do this, depending on the application. But say for instance that you want to save a bunch of numbers, indicating the tiles of your map where the first number is the tile ID of the first tile of the map (upper-left or something). If you want several maps in the same file you can either enforce that each map has the same size (which makes it easy to just move the filepointer) or that they begin (and possibly end) with a special marker.

Ok.. So much for file structure. Like I said, this is just one way of doing it. But how do I read (and write) the file in binary mode you ask? Simple, you supply a 'b' when you open the file, like this:
Code:
//Open file for reading only, text mode
FILE* fptr = fopen("filename.txt","rt");

// Open file for writing, binary mode
FILE* fptr = fopen("filename.bin","wb");

// Open file for writing at the end of the file (append), binary mode
FILE* fptr = fopen("filename.bin","ab");

And so on... See the reference for fopen() for more info. The file needs to be present or the open-for-reading method will fail, returning a NULL pointer (you should check what fopen returns even if I didn't do it in this tiny example). The file will be created if not present by the open-for-writing and open-for-append methods.

Once you have opened a file, you just read/write the data from/to the file to/from your data structure using some of the file reading/writing functions. For example:
Code:
//Assuming you have the data stored as an array
int tiles[50][50];

//read in the data from file (assuming you are at the right position, or only have one map per file))
for (int i=0; i<50; i++)
fread(tiles,sizeof(int),50,fptr);

//write the data to file (same assumption as above)
for (int i=0; i<50; i++)
fwrite(tiles,sizeof(int),50,fptr);

Simple, isn't it. You could also have done it like this:
Code:
for (int i=0; i<50; i++)
fread(tiles,sizeof(tiles),1,fptr);

and you can even read a whole class this way
Code:

MyClass* myClass;
fread(myClass,sizeof(MyClass),1,fptr);

but remember that it will only work for local types (char, int, struct, etc) and NOT for pointers as you could imagine.

Ok.. this should get you started. I havn't tested the code so I won't guarantee that it will work. Take a look at stdio.h reference to learn more about file IO. The correct way of doing this in C++ (stdio is C by the way) is to use iostreams, but since they are not available for WinCE you don't have much choise. (Slight correction, you can use MFC classes but like I said before, you don't want to do this).

And finally, limiting game speed should (could/would) work just like FZammetti said. First limit the framerate, then limit the speed of your sprites according to the framerate. All good games allow the user to adjust the game speed so it's not that big a deal if it's to fast or to slow on some devices as long as it's not TO SLOW and there is nothing one can do about it.

You could work with a timer for the rendering also if you like. But, you could also try to lower the FPS limit to 24 (like most movies). The following quote is taken from the link below that explains the difference and importance of FPS.

Do we need 30+ fps for enjoyable gameplay ... no... most people are happy with much lower framerates but since the huge hyping of 30+ fps a lot of people think or are influenced by the counter to think that 30 is a minimum... the only minimum is what you feel is unplayable... keep in mind that software engines are often running at between 10 and 20 fps and they are playable too... so framerate numbers are nice for benchmarking but not for judging playability.

http://www.ping.be/powervr/fps_discus.htm
Very good point about frame rate... it's always a contentious issue, but I've been very happy targeting 30FPS on an E-125. I've been able to do everything I want to thus far at that rate and it remains solid (the only thing I've observed slowdowns with is when I'm making use of alpha blending over a large area, but that's to be expected).

I admit I'm not writing a 3-D engine for a first-person shooter, but if I was I probably would be writing my own library anyway!

Again, my own opinion which you are of course welcome to disagree with, is that using PF with sound extensions, if your set as your goal a solid 30FPS on an older MIPS device, you will find that you have plenty of overhead for relatively complex games on any modern device.

Further, I would limit the frame rate as per the various posts floating around here on the topic and then use multi-pixel moves for variable speed. Naturally with that you have to keep in mind that a sprite moving more than a few pixels a frame will appear jumpy... I think I've observed that five or less pixels per frame looks OK, anything more starts to look jumpy.

One last point, again more a matter of opinion than anything else... when your dealing with large chunks of data that is unchanging, like map data, the coding required to deal with it out of a resource tends to be a little less in terms of lines of code, and I think somewhat less error prone (no need to worry about missing files for instance). To me, unless you have real reasons for wanting it in a file, resources are the way to go. I suspect that will be a little better performing as well, but I've done no testing to prove or disprove that, so I could be entirely wrong. Either way, you can deal with it as a stream of byte (or larger types) data, as I do with my cut scene scripts, or as structs, as adde pointed out, which is very powerful when your data is more complexly structured.
FZammetti wrote:To me, unless you have real reasons for wanting it in a file, resources are the way to go. I suspect that will be a little better performing as well, but I've done no testing to prove or disprove that, so I could be entirely wrong.


We've had the argument resource vs. file before so I'm not trying to start that up again. FZammetti likes resources, I like files. They both have their pros and cons. Just saying that a map for instance, should be loaded once before each level and then it doesn't matter if it takes a few more milliseconds. And, what happens if you want to save the state of your game. Then you have to use a file, so why not use one to begin with.

Also, if you have the maps (or other resources) in a file you can supply a map-editor for the fans or something. Correctly used, an external file has more flexability IMO. For instance, supplying more levels by distributing a map file instead of a whole new executable (sort of like the Doom .wad files). You could always encode/encrypt the binary files if you don't want the user to be able to change the aspects of the game (and having data as a resource does not prevent this either since the .exe file can be modified by a hexeditor).
Your right, no need to start that argument again. In any case, your of course correct, and I've said it myself as well... use what's appropriate to the task, that's always true.

It's just that resources ARE always the appropriate choice! (JUST KIDDING)
It would be interesting to see a comparison of speed with resources. I think the cost of resources is that it will occupy memory and will take more time to load initially. That is assuming you load files when needed and not just all of them at the beginning (a lot of games do that however).

Unless the resource loading does some odd things I would expect them to be largely equal since there is no cost difference in terms of loading. That is except the inefficiency I mentioned above.

So I use both, one thing that is extremely helpful to me is to let artist created files be loaded as files during development, its so much easier to let an artist drop in new creations and then run it to test view his work, than to have to rebuild the app just to see it. Faster too.
FZammetti wrote:It's just that resources ARE always the appropriate choice!

=)
Thanks for answering. :lol:
I will use files because they are necessary in the game: my maps are huge (about 350 kb in Ascii .txt). My project started only about one week ago and I don't know would be a "save" option available.
One more question about Game Speed: I have an old E-125 :wink: and I don't know a real fps speed on other devices, so where can I find results of Gapi testing on different devices or something. :?:
Under General Pocket PC Discussion, there was recently a thread on speed where a lot of devices were listed. =)
Thanks Presto! :P