• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Khatharr

STL Streams for files and buffers

9 posts in this topic

So I'm reworking my Input Manager and it's coming along nicely. I've written a few functions that pass around basic_ofstream / basic_ifstream for saving and loading the key mappings. Those are working great but I'd like to be able to save/load to/from a memory buffer as well. For some reason all my STL documentation is either too referential to be of use for finding out what to use in a specific case or else too 'for dummies' style to mention cases where something unusual is being done.

I'm using unsigned char as the element type here since it's the most appropriate for the data I'm saving, so stringstream doesn't seem right, but the more I look at this gibberish the more confused I become, so I thought I'd ask here.

Is there a way (and if so, how) to set up an input stream that will write unsigned char data into a buffer which will expand as data is added?

Is it possible that I could use a single STL type in the underlying system and then have the pair of top-level functions feed the system either an ofstream or a buffer-stream? (polymorphism)

I mean I guess if it comes down to it I can create a new helper type that will figure it out one way or the other, but I get the feeling that underneath all this "thing&<derp>::moocow<wtf>&&&" madness this is something that STL can do easily.

Any advice would be greatly appreciated. Thank you. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Edit - Also, I wonder if I just have a really bad collection of docs here. Does anyone have any suggestions for somewhere I can get some good STL docs that explain what parts are actually intended to be used and what parts are just implementation or alien conspiracy? Edited by Khatharr
0

Share this post


Link to post
Share on other sites
Chances are a std::stringstream will work just fine. Just change the type you are passing around from ifstream/ofstream to istream/ostream. If you're feeling adventurous you can use a std::strstream, which allows you to manually manage the memory the stream writes to, but it's somewhat inconvenient from my point of view.

As for documentation, a good reference for the standard library is Nicolai Josuttis' "The C++ Standard Library" which covers how the library is meant to be used in addition to what the different bits do. I've only used the first edition myself, but there is a second edition out now that covers C++11. I personally mostly consult the C++ standard itself if I need to look things up.
0

Share this post


Link to post
Share on other sites

Thanks Boss. I've been using a downloaded version of http://en.cppreference.com/w/ along with VS integrated MSDN and a really old book in the vein of "C++ for people who like picture books".

 

With stringstream my concern is the signedness of the members. It's more or less what I want except for that one issue. The first thing I tried was just digging into the STL headers and trying to find out how to make a stringstream based on unsigned int, but it's buried in such an long and inheritance chain that I gave up after my third "flub<char>diggle::bigarb&". When I get home I'll mess with it a bit and see if it mangles my output.

 

Ah, so ostream is a parent of both. That should work nicely. Thanks.

 

As for the book, it looks like what I'm after, but sheesh - $50.

 

Guess I'll look into it next time I get paid or see if I can get a second-hand copy somewhere. Thanks again. biggrin.png

 

Edit - Now that I'm looking at cppreference it would appear that my offline version is criminally out-of-date. wacko.png

Edited by Khatharr
0

Share this post


Link to post
Share on other sites
Like a lot of the standard library, std::stringstream is actually a template class. It's a typedef for std::basic_stringstream<char>. You could try using a std::basic_stringstream<unsigned char> instead. However, chances are pretty good that won't be necessary and using a regular std::stringstream will just work.
0

Share this post


Link to post
Share on other sites

Reviewing the current version of cppref made everything much more clear.

 

You're saying that feeding unsigned chars into char based streams won't cause trouble? If the incoming value is greater than 128 won't it get clipped or rotated by the signed type?

Edited by Khatharr
0

Share this post


Link to post
Share on other sites
Well first, char isn't necessarily signed or unsigned. It can be either depending on the compiler. There are three char types: signed char, unsigned char and plain char, with plain char being a separate type but otherwise identical to one of the other two. Most compilers I know of allow you to specify if char is signed or unsigned. For example gcc has the -funsigned-char and -fsigned-char options and MSVC has the /J switch. If you're feeling very paranoid about dumping unsigned chars into a regular char container, you can probably use the set char to unsigned switch for the compiler you are using.

That said, when you dump binary data into a stream, it really doesn't care very much about signed/unsigned. It just sees one byte units of storage. In practice using regular char streams for serialization just works. If you aren't confident, the write some unit tests for the cases that seem like it might cause troubles.
0

Share this post


Link to post
Share on other sites

I'm confident that it will work, or can be made to work when I'm compiling locally. I'm just wondering about what it may get up to on other compilers or if I post the source for other people to compile, since I'm aiming for ease of portability. Is there a standard in terms of how STL will behave in this case? It's really no trouble to use a basic_stringstream<unsigned char> if it guarantees behavior. I'm just curious, since you mentioned it. Thanks again for the advice, btw.

Edited by Khatharr
0

Share this post


Link to post
Share on other sites

You're going to have to say what case(s) you're looking at before anyone would be able to say what guarantees the standard gives you.

0

Share this post


Link to post
Share on other sites
There are two possibilities here: char is signed or char is unsigned. If the char is unsigned, then everything is guaranteed to work without a hitch. If char is signed then what happens depends on the platform. The standard is written such that machines that use two's complement representation for signed negative numbers shouldn't change the binary representation when casting from signed to unsigned and back. However, if you've got a machine using sign magnitude for negatives, then there's no telling what value will come out.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0