-
Notifications
You must be signed in to change notification settings - Fork 28
API Documentation
IMPORTANT: The following documentation doesn't reflect current version of Punity and is incomplete. For more detailed information see punity.h
.
Reserves virtual memory of given size
. If pointer ptr
is not 0
, it'll try to allocate the memory at given address. Reserve is protected with ASSERT
. The reserved memory cannot be used, you need to call virtual_commit
to be able to use it.
Marks a portion of reserved memory (see virtual_reserve
) of given size
(lower or equal to reserved size) for use.
Marks a portion of commited memory (see virtual_commit
) of given size
as not used (but still reserved).
Releases memory.
A convenience function to reserve and commit memory.
Memory banks are simple linear pre-allocated pieces of memory with LIFO (last-in first-out) access. Bank
is a simple structure containing pointers to the begin
and end
of the allocated memory. On top of that it also holds a pointer to a push/pop cursor called it
. When the application starts, you allocate the bank with bank_init
and then you call bank_push
and bank_pop
to add and remove blocks to and from the bank's cursor.
If there's not enough memory available in the bank, the bank_push
will quit the application with a message. This behavior forces you to know how much memory you need for your game. The simple solution is usually just to increment the size of the memory and rebuild the game.
You can also use BankState
and it's bank_begin
and bank_end
functions to store and restore the state of the bank. This is useful if you do multiple push
calls and want to reset the bank`s pointer to it's original state when you're done.
Bank bank;
bank_init(&bank, megabytes(16));
BankState state = bank_begin(&bank);
ptr1 = bank_push(&bank, kilobytes(1));
ptr2 = bank_push(&bank, kilobytes(1));
ptr3 = bank_push(&bank, kilobytes(1));
// Pop all of the stuff at once.
bank_end(&state);
Punity defines two default banks:
-
CORE->stack
is a bank for short-term allocations. Thepush
andpop
has to be balanced so when the frame ends it nothing will be allocated in the memory. -
CORE->storage
is a memory that is used for long-term allocations of bitmaps, sounds, maps, state, etc.
Banks can be used in various ways. My favorite is to allocate, process and return big memory blocks from a function through CORE->stack
:
void *generate_naked_woman() {
void *ptr = bank_push(CORE->stack, megabytes(1));
// Generate naked woman bitmap.
return ptr;
}
void *naked_woman = generate_naked_woman();
// Draw naked woman.
bank_pop(naked_woman);
Intializes an memory bank
with given capacity
.
Resets the push pointer to zero, but keeps the memory.
Pushes a block of given size
to current pointer (and return pointer to it) and sets the pointer to the end of that block.
Moves the bank
s pointer back to ptr
.
Stores and returns the state of the bank
.
Restores the state of the state.bank
to the state when the bank_begin
was called.
File I/O provides two convenience functions to read and write binary files.
Reads binary file and adds it to bank
. The data should be freed with bank_pop
.
If bank
is 0
, then it'll use malloc
to allocate the memory and the data should be freed with free
.
Writes memory block at ptr
of given size
to the file at path
.
Color is a struct holding 8-bits per red, green, blue and alpha channels. The ordering of the channels is platform specific and can be customized with PUN_COLOR_CHANNELS
. This ordering is used to skip the step of reshifting each color when blitting to screen. This functionality will probably change when platform support emerge.
Punity uses 8-bit indexed colors with palette stored in CORE->canvas.palette
, each entry in the palette is Color
. In the palette 3 colors indexes are considered special:
-
0
is used for transparent color(r=0, g=0, b=0, a=0)
and is reserved; internal algorithms count with this setup. -
1
is used for black color(r=0, g=0, b=0, a=255)
it's recommended but not used internally. -
2
is used for white color(r=255, g=255, b=255, a=255)
it's recommended but not used internally.
Sets up a color struct.
Returns new color that is in distance t
(0..1) between from
and to
colors.
A struct holding 4 integer coordinates of an axis-aligned rectangle.
Not implemented yet.
Not implemented yet.
Requires stb_image.h
Requires stb_image.h