Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / Resources and readers
- - By danikaze Date 2011-11-12 23:56
Maybe it's a silly question but... how resources and readers are used?

Gosu::Reader constructor needs a Resource or no parameters, and Resource needs no parameters. So how it's used?
Can you create a reader from a file, FILE*, fstream, or something?
Parent - - By jlnr (dev) Date 2011-11-13 01:45
Resource is the base class, Buffer and File are the implementations. Typically, you open a File and then call .frontReader() to read or .backWriter() to append data.

I'd have loved to use ANSI functions for loading files, but they don't support Unicode and that's a big no-go on Windows. If you need to, you can easily implement the Resource interface for either FILE or fstream.
Parent - By danikaze Date 2011-11-13 15:42
oh I see...
lol, so knowing that, I think I didn't need to use libpng to load an image from a file since you can do it with readers hahahaha... well, now I know how to use libPNG :P

Thanks!
Parent - - By danikaze Date 2011-11-13 16:47 Edited 2011-11-13 17:10
Anyways I have a question since my code doesn't work reading data from packed files.

I know you can do this:
  Gosu::File f(L"song.ogg");
  Gosu::Reader reader* = new Gosu::Reader(f.frontReader());
  _song.reset(DNEW Gosu::Song(*reader));


And even this:
  Gosu::File f(L"music.snd");
  Gosu::Reader* reader = new Gosu::Reader(f.frontReader());
  reader->setPosition(0);
  _song.reset(DNEW Gosu::Song(*reader));


But, if you know you have a .ogg starting at 116th byte of the file "music.snd" can't you do this?:
  Gosu::File f(L"music.snd");
  Gosu::Reader* reader = new Gosu::Reader(f.frontReader());
  reader->setPosition(116);
  _song.reset(DNEW Gosu::Song(*reader));


because I get an error loading the Gosu::Song from (*reader)
(Unhandled exception at 0x.... in ...exe Microsoft C++ exception: std::reuntime_error at memory location 0x...)

EDIT:
I was thinking, and maybe the error is because in music.snd there are more than 1 song, and Gosu::Song doesn't know where to stop reading?
Let's say the content of the file is something like this:

From byte 0 to 115: metadata
From byte 116 to 2000000: first song
From byte 2000001: second song
and so on.

Is there any way to read data like this?
I read PNG from files like this using libPNG, but I hope there's a way to avoid using libOGG xDDDD
Parent - - By jlnr (dev) Date 2011-11-13 22:40
Yeah, Gosu was supposed to allow reading from arbitrary readers, but the underlying libraries seldom know where to stop. I guess with some streaming formats, it impossible to know anyway. You are better off to read the section into a Buffer and then read that instead. (Or write a Resource wrapper that will return a truncated size.)

BTW, this is a memory leak:
Gosu::Reader* reader = new Gosu::Reader(f.frontReader());
It's way easier :)
Gosu::Reader reader = f.frontReader();
Parent - By danikaze Date 2011-11-14 00:40
ok, I'll think about it :)

And yes, the pointer to reader needs to be freed, but that's how works a class I have over there ;)
Don't worry, I was freeing that pointer, but forgot to include it in the example.

So, there's 3 options:
1 - make a copy to a buffer and then read it
2 - load them from their source files (ogg, m4a, etc.) rather than buffers
3- play with ogg.lib or something like that as I did with PNGs ;)
Up Topic Gosu / Gosu Exchange / Resources and readers

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill