Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Platform-dependent files?

Credits go to Peter Hull for helping out!
This thread is locked; no one can reply to it. rss feed Print
Platform-dependent files?
Ron Ofir
Member #2,357
May 2002
avatar

Hi all,

I have tried making a small Allegro app for my Java game that takes a bitmap and writes out all of the coordinates of non-white pixels to a file. It writes the pixels in pairs of two integers, no spaces, no tabs, no newlines, just integers. The app runs fine (after some debugging of course), so I went on to try and read the file in my Java game, however the results seem very wrong. When I should get a pair of 4 4 I get 67108864 67108864, and so on. I tried reading the file using the Allegro app and the numbers come out fine (eexcept for the last pair that comes twice for some reason, but I think I'm doing wrong EOF checks). The application is running on an Ubuntu Linux and the Java game is running on Windows XP, so I thought it might be some endian problems, and here comes my problem: How can you choose different byte order in Java for reading files? I would also be glad for any other endian-issues feedback/random info.

(I'm using simple fwrite-fread in order to write and check the file, and DataInputStream in order to read it, oh, and both applications read/write the same number of pairs)

Cookies promised.

Fladimir da Gorf
Member #1,565
October 2001
avatar

I wouldn't rely on any specific format when using DataInputStream. You should use your own stream handler if you make any assumptions.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Peter Hull
Member #1,136
March 2001

I agree with Fladimir; DataInputStream should probably only be used with files created by DataOutputStream.

However you should be able to find the format used with Google. Assuming it's simply a big endian format, you can use the relevant Allegro functions, for example

#include <allegro.h>

int main(int argc, char* argv[])
{
  PACKFILE* f = pack_fopen(argv[1], F_WRITE);
  int i = 4;
  pack_mputl(i, f);
  pack_fclose(f);
  return 0;
} END_OF_MAIN()

produces a file with a 4 in it, that can be read by this program

1import java.io.*;
2 
3class Fr
4{
5 public static void main(String[] args)
6 {
7 System.out.println(args[0]);
8 try {
9 FileInputStream is = new FileInputStream(args[0]);
10 DataInputStream ds = new DataInputStream(is);
11 int i = ds.readInt();
12 System.out.println("Answer " + i);
13 } catch(Exception xep) {System.err.println(xep);}
14 }
15}

Random info: I had leeks for dinner today

Pete

Fladimir da Gorf
Member #1,565
October 2001
avatar

For general communication between programs made with different languages, I'd use XML.

OpenLayer has reached a random SVN version number ;) | Online manual | Installation video!| MSVC projects now possible with cmake | Now alvailable as a Dev-C++ Devpack! (Thanks to Kotori)

Ron Ofir
Member #2,357
May 2002
avatar

I'll check the pack_mputl solution. I don't want to use XML as it will make the files about 20 times bigger and complicate the writing (not so much, but still).

BTW, don't you have to initialize allegro (in console mode, SYSTEM_NONE) in order to use it's routines?

GullRaDriel
Member #3,861
September 2003
avatar

For general communication between programs made with different languages, I'd use A SIMPLE TEXT FILE.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Ron Ofir
Member #2,357
May 2002
avatar

Java uses big-endian so Peter's solution works. I've just tried it with a couple of things and it looks great (I'll post screenshots later when I add some more effects).

James Stanley
Member #7,275
May 2006
avatar

Quote:

I had leeks for dinner today

Leeks are horrible!
Coincidentally, my Grandads vicar steals leeks from the fields.

Go to: