• 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

Int Size, Endianness and Serialisation

4 posts in this topic

I'm trying to write a function that takes an std::vector of game objects and saves them all as an image.


The game I'm making revolves around placing game objects in a specific way and I wanted users to be able to share different placements easily. Since there won't be much data, I decided that for every game object, each of it's applicable data members would be converted to a colour, and saved in the image as a pixel. To load game states my game would open the image and read each pixel from left to right, top to bottom and re-construct each game object.


The bit that I'm a little stuck on is how to convert a number to a colour, and back again. For example the x coordinate of a specific game object could be 42, I need a way to take 42, and convert it to 4 individual bytes (r,g,b,a).


I've realised that this, by itself, isn't that difficult. But I was wondering If I need to take the platform's integer size and endianness into consideration when I'm building this? And If so what would be a good way to deal with all that?




Share this post

Link to post
Share on other sites

Thanks for your speedy reply Samith, lots of good info.

Typically, when you need to deal with unknown endianness you would write a byte order marking at the top of the file, which when read back by another machine can indicate if the content of the file needs to be endian swapped. A common byte order marking is the 16 bit integer 0xfeff. If you read a byte order mark from a file and it comes out as 0xfffe, then you have to reverse the endianness of the contents of that file. If it gets read in as 0xfeff, then you won't need to perform any endian swapping.

This in particular was very enlightening. I'll use a byte order marking to check weather to swap all other numbers read.


You don't need to worry about endian-ness unless you are switching machine families or using Java.

It's nice to know that pretty much all conventional PCs use little endain. But I did a bit of searching and found that vc++ has some built in functions for swapping byte order (I think they're quite well optimised as well). With all this info I found that it really wasn't much effort to support big and little endian (unless I've overlooked something) even if it's not likely to be necessary. Good practice I guess.


This is what I managed to come up with so far:

It still needs to be cleaned up, but I think I've not overlooked anything important (other than a load function..)

bool save_as_image( std::string argFilename, std::vector<ggCell>& argCells )
    // saves a vector of cells as an image.

    // could pack more into each pixel but it's not necessary

    // calculate the image size needed
    unsigned int dimWidth = 128;
    unsigned int hlpBytesNeeded = GG_BYTES_PER_CELL*argCells.size();
    unsigned int dimHeight = 17 + (hlpBytesNeeded/4)/128;
    out("height is " + uint_to_string(dimHeight));

    // load template header
    sf::Image imgHeader;
    if ( !imgHeader.loadFromFile("data/pattern_header.png") ) {
        return false;

    // create new image with header
    sf::Image imgPattern;
    imgPattern.create( dimWidth, dimHeight, sf::Color(0,0,0));
    imgPattern.copy( imgHeader, 0, 0 );

    // write all pixels
    unsigned int x = 0;
    unsigned int y = 16;
    for ( unsigned int index=0; index<argCells.size(); ++index ) {
        // gen colours
        sf::Color colX, colY, colType;
        colX = int_to_pixel( argCells[index].x );
        colY = int_to_pixel( argCells[index].y );
        colType = int_to_pixel( argCells[index].type );
        // write to image
        imgPattern.setPixel( x, y, colX );
        imgPattern.setPixel( x, y, colY );
        imgPattern.setPixel( x, y, colType );
        // output colours used
        out("[" + std::to_string(index) + "] ");
        out("X: ( " + std::to_string(colX.r) + ", " + std::to_string(colX.g) + ", " + std::to_string(colX.b) + ", " + std::to_string(colX.a) + " )\n");
        out("[" + std::to_string(index) + "] ");
        out("X: ( " + std::to_string(colY.r) + ", " + std::to_string(colY.g) + ", " + std::to_string(colY.b) + ", " + std::to_string(colY.a) + " )\n");
        out("[" + std::to_string(index) + "] ");
        out("X: ( " + std::to_string(colType.r) + ", " + std::to_string(colType.g) + ", " + std::to_string(colType.b) + ", " + std::to_string(colType.a) + " )\n");

    // save image
    imgPattern.saveToFile( argFilename );
    return true;


sf::Color int_to_pixel( int number ) {
    // code number to look better as an image
    number += GG_INT_CODE;
    // convert to pixel
    int r_single = number & 0xff000000;
    int g_single = number & 0x00ff0000;
    int b_single = number & 0x0000ff00;
    int a_single = number & 0x000000ff;
    int r = r_single >> 24;
    int g = g_single >> 16;
    int b = b_single >> 8;
    int a = a_single;
    sf::Color colour((unsigned char)r,(unsigned char)g,(unsigned char)b,(unsigned char)a);
    return colour;

int pixel_to_int( sf::Color colour, bool swap ) {
    int r = (int) colour.r;
    int g = (int) colour.g;
    int b = (int) colour.b;
    int a = (int) colour.a;
    int number = r << 24;
    number = number | g << 16;
    number = number | b << 8;
    number = number | a;
    if ( swap ) {
        number = (int)_byteswap_ulong( number );
    // decode
    number -= GG_INT_CODE;
    return number;

void increment( unsigned int& x, unsigned int& y ) {
    if ( x >= 128 ) {
        x = 0;

Share this post

Link to post
Share on other sites

it will be different; the PPC family uses big endian.

Be (somewhat) careful: not *all* PowerPCs use big endian.


The chips themselves can flip between big/little endian mode, in some cases even on a per-page basis. And, at least in theory, can run little endian programs while the OS itself is big endian.


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