Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

I'd use a simple struct or similar device (depending on your language) to store all game state in a central place. If you want the protection of setters/getters, you can wrap the struct in a class.

If you feel up to it, make use of bitfieldsbitfields or simply do bit manipulation yourself using bitwise operators.

Be aware that in some languages, the rules for struct padding and packing may be a bit complicated -- but it may also not matter much for your case if you have a byte or two of padding.

You may also be able to use a #pragma (such as #pragma pack(1)) or an __attribute__ to closely pack the struct, eliminating padding. This may or may not work depending on your compiler and target architecture.

Note that use of bitfields and pack pragmas or attributes can reduce portability. Across hardware architectures, the struct field endianness (byte order) may also change. So you may want to avoid this if you're trying for portability.

(For e.g. Pac-Man, this struct might naively contain a map id or map seed, a Pac-Man x and y position, four ghost x and y positions, and a large bitfield for the presence or absence of 32-64 pellets, whatever the maximum is.)

Once you have your struct, pass it to something like an xxencode function:

encode_save( char * outStringBuf, size_t outStringBufSize,
             const SaveStruct * inSaveData, size_t inSaveDataSize )

Writing this function is a bit error-prone; you need to shift and combine bytes as necessary to get e.g. 6 bits at a time, then translate into an appropriate character. I'd personally try to hunt down someone else's code, unless I was doing this for "fun" (and I'd probably want a test suite for it).

Never underestimate the power of the old-school struct in the right places. We've used it a ton for GBA and DS games here.

I'd use a simple struct or similar device (depending on your language) to store all game state in a central place. If you want the protection of setters/getters, you can wrap the struct in a class.

If you feel up to it, make use of bitfields or simply do bit manipulation yourself using bitwise operators.

Be aware that in some languages, the rules for struct padding and packing may be a bit complicated -- but it may also not matter much for your case if you have a byte or two of padding.

You may also be able to use a #pragma (such as #pragma pack(1)) or an __attribute__ to closely pack the struct, eliminating padding. This may or may not work depending on your compiler and target architecture.

Note that use of bitfields and pack pragmas or attributes can reduce portability. Across hardware architectures, the struct field endianness (byte order) may also change. So you may want to avoid this if you're trying for portability.

(For e.g. Pac-Man, this struct might naively contain a map id or map seed, a Pac-Man x and y position, four ghost x and y positions, and a large bitfield for the presence or absence of 32-64 pellets, whatever the maximum is.)

Once you have your struct, pass it to something like an xxencode function:

encode_save( char * outStringBuf, size_t outStringBufSize,
             const SaveStruct * inSaveData, size_t inSaveDataSize )

Writing this function is a bit error-prone; you need to shift and combine bytes as necessary to get e.g. 6 bits at a time, then translate into an appropriate character. I'd personally try to hunt down someone else's code, unless I was doing this for "fun" (and I'd probably want a test suite for it).

Never underestimate the power of the old-school struct in the right places. We've used it a ton for GBA and DS games here.

I'd use a simple struct or similar device (depending on your language) to store all game state in a central place. If you want the protection of setters/getters, you can wrap the struct in a class.

If you feel up to it, make use of bitfields or simply do bit manipulation yourself using bitwise operators.

Be aware that in some languages, the rules for struct padding and packing may be a bit complicated -- but it may also not matter much for your case if you have a byte or two of padding.

You may also be able to use a #pragma (such as #pragma pack(1)) or an __attribute__ to closely pack the struct, eliminating padding. This may or may not work depending on your compiler and target architecture.

Note that use of bitfields and pack pragmas or attributes can reduce portability. Across hardware architectures, the struct field endianness (byte order) may also change. So you may want to avoid this if you're trying for portability.

(For e.g. Pac-Man, this struct might naively contain a map id or map seed, a Pac-Man x and y position, four ghost x and y positions, and a large bitfield for the presence or absence of 32-64 pellets, whatever the maximum is.)

Once you have your struct, pass it to something like an xxencode function:

encode_save( char * outStringBuf, size_t outStringBufSize,
             const SaveStruct * inSaveData, size_t inSaveDataSize )

Writing this function is a bit error-prone; you need to shift and combine bytes as necessary to get e.g. 6 bits at a time, then translate into an appropriate character. I'd personally try to hunt down someone else's code, unless I was doing this for "fun" (and I'd probably want a test suite for it).

Never underestimate the power of the old-school struct in the right places. We've used it a ton for GBA and DS games here.

note re endianness and portability
Source Link
leander
  • 2.7k
  • 14
  • 20

I'd use a simple struct or similar device (depending on your language) to store all game state in a central place. If you want the protection of setters/getters, you can wrap the struct in a class.

If you feel up to it, make use of bitfields or simply do bit manipulation yourself using bitwise operators.

Be aware that in some languages, the rules for struct padding and packing may be a bit complicated -- but it may also not matter much for your case if you have a byte or two of padding.

You may also be able to use a #pragma (such as #pragma pack(1)) or an __attribute__ to closely pack the struct, eliminating padding. This may or may not work depending on your compiler and target architecture.

Note that use of bitfields and pack pragmas or attributes can reduce portability. Across hardware architectures, the struct field endianness (byte order) may also change. So you may want to avoid this if you're trying for portability.

(For e.g. Pac-Man, this struct might naively contain a map id or map seed, a Pac-Man x and y position, four ghost x and y positions, and a large bitfield for the presence or absence of 32-64 pellets, whatever the maximum is.)

Once you have your struct, pass it to something like an xxencode function:

encode_save( char * outStringBuf, size_t outStringBufSize,
             const SaveStruct * inSaveData, size_t inSaveDataSize )

Writing this function is a bit error-prone; you need to shift and combine bytes as necessary to get e.g. 6 bits at a time, then translate into an appropriate character. I'd personally try to hunt down someone else's code, unless I was doing this for "fun" (and I'd probably want a test suite for it).

Never underestimate the power of the old-school struct in the right places. We've used it a ton for GBA and DS games here.

I'd use a simple struct or similar device (depending on your language) to store all game state in a central place. If you want the protection of setters/getters, you can wrap the struct in a class.

If you feel up to it, make use of bitfields or simply do bit manipulation yourself using bitwise operators.

Be aware that in some languages, the rules for struct padding and packing may be a bit complicated -- but it may also not matter much for your case if you have a byte or two of padding.

You may also be able to use a #pragma (such as #pragma pack(1)) or an __attribute__ to closely pack the struct, eliminating padding. This may or may not work depending on your compiler and target architecture.

Note that use of bitfields and pack pragmas or attributes can reduce portability.

(For e.g. Pac-Man, this struct might naively contain a map id or map seed, a Pac-Man x and y position, four ghost x and y positions, and a large bitfield for the presence or absence of 32-64 pellets, whatever the maximum is.)

Once you have your struct, pass it to something like an xxencode function:

encode_save( char * outStringBuf, size_t outStringBufSize,
             const SaveStruct * inSaveData, size_t inSaveDataSize )

Writing this function is a bit error-prone; you need to shift and combine bytes as necessary to get e.g. 6 bits at a time, then translate into an appropriate character. I'd personally try to hunt down someone else's code, unless I was doing this for "fun" (and I'd probably want a test suite for it).

Never underestimate the power of the old-school struct in the right places. We've used it a ton for GBA and DS games here.

I'd use a simple struct or similar device (depending on your language) to store all game state in a central place. If you want the protection of setters/getters, you can wrap the struct in a class.

If you feel up to it, make use of bitfields or simply do bit manipulation yourself using bitwise operators.

Be aware that in some languages, the rules for struct padding and packing may be a bit complicated -- but it may also not matter much for your case if you have a byte or two of padding.

You may also be able to use a #pragma (such as #pragma pack(1)) or an __attribute__ to closely pack the struct, eliminating padding. This may or may not work depending on your compiler and target architecture.

Note that use of bitfields and pack pragmas or attributes can reduce portability. Across hardware architectures, the struct field endianness (byte order) may also change. So you may want to avoid this if you're trying for portability.

(For e.g. Pac-Man, this struct might naively contain a map id or map seed, a Pac-Man x and y position, four ghost x and y positions, and a large bitfield for the presence or absence of 32-64 pellets, whatever the maximum is.)

Once you have your struct, pass it to something like an xxencode function:

encode_save( char * outStringBuf, size_t outStringBufSize,
             const SaveStruct * inSaveData, size_t inSaveDataSize )

Writing this function is a bit error-prone; you need to shift and combine bytes as necessary to get e.g. 6 bits at a time, then translate into an appropriate character. I'd personally try to hunt down someone else's code, unless I was doing this for "fun" (and I'd probably want a test suite for it).

Never underestimate the power of the old-school struct in the right places. We've used it a ton for GBA and DS games here.

Source Link
leander
  • 2.7k
  • 14
  • 20

I'd use a simple struct or similar device (depending on your language) to store all game state in a central place. If you want the protection of setters/getters, you can wrap the struct in a class.

If you feel up to it, make use of bitfields or simply do bit manipulation yourself using bitwise operators.

Be aware that in some languages, the rules for struct padding and packing may be a bit complicated -- but it may also not matter much for your case if you have a byte or two of padding.

You may also be able to use a #pragma (such as #pragma pack(1)) or an __attribute__ to closely pack the struct, eliminating padding. This may or may not work depending on your compiler and target architecture.

Note that use of bitfields and pack pragmas or attributes can reduce portability.

(For e.g. Pac-Man, this struct might naively contain a map id or map seed, a Pac-Man x and y position, four ghost x and y positions, and a large bitfield for the presence or absence of 32-64 pellets, whatever the maximum is.)

Once you have your struct, pass it to something like an xxencode function:

encode_save( char * outStringBuf, size_t outStringBufSize,
             const SaveStruct * inSaveData, size_t inSaveDataSize )

Writing this function is a bit error-prone; you need to shift and combine bytes as necessary to get e.g. 6 bits at a time, then translate into an appropriate character. I'd personally try to hunt down someone else's code, unless I was doing this for "fun" (and I'd probably want a test suite for it).

Never underestimate the power of the old-school struct in the right places. We've used it a ton for GBA and DS games here.