[ create a new paste ] login | about

Link: http://codepad.org/Q6zfTV44    [ raw code | output | fork ]

C, pasted on Sep 12:
#include "stdio.h"

typedef unsigned char uchar;

uchar pack_tile
(uchar ID, uchar passable)
{
    if (ID < 128 && (passable == 0 || passable == 1))
        return (ID << 1) + passable;
    else
        exit(1); /* There's no real error checking in C, so we're just gonna fail
                  * the program if the values aren't good. */
}

void read_tile
(uchar packedID)
{
    printf("Tile %d:\n", packedID);
    printf("    ID: %d\n", packedID >> 1);
    if (packedID & 1)
        printf("    passable\n");
    else
        printf("    non-passable\n");
}

int main
(int argc, char *argv[])
{
    uchar t1 = 36;  uchar t1_p = 1; // t1 is tile 1 ID valude t1_p determines whether it should be passable or not
    uchar t2 = 47;  uchar t2_p = 0;
    uchar t3 = 127; uchar t3_p = 1;
    
    /* So, we'll just read back the packed data and it should print the exact values
     * that we declared and initialized. */
    read_tile(pack_tile(t1, t1_p));
    read_tile(pack_tile(t2, t2_p));
    read_tile(pack_tile(t3, t3_p));
    return 0;
}


Output:
1
2
3
4
5
6
7
8
9
Tile 73:
    ID: 36
    passable
Tile 94:
    ID: 47
    non-passable
Tile 255:
    ID: 127
    passable


Create a new paste based on this one


Comments: