-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNC_Platform.mm
65 lines (50 loc) · 1.62 KB
/
CNC_Platform.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "CNC_Types.h"
#include "CNC_Platform.h"
#include <stdlib.h>
#include <assert.h>
#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG
#include "libs/stb_image.h"
struct MemoryPool* CreateMemoryPool( u32 sizeInBytes )
{
void* data = malloc( sizeInBytes + sizeof( struct MemoryPool) );
struct MemoryPool* pool = ( struct MemoryPool* )data;
pool->m_size = sizeInBytes;
pool->m_usedBytes = 0;
pool->m_freeBytes = sizeInBytes;
pool->m_memory = (u8*)data + sizeof( struct MemoryPool );
return pool;
}
void InitPlatform( struct Platform* platform, void* renderer )
{
platform->loadImage = &LoadImageFile;
platform->uploadToGpu = &UploadToGpu;
platform->freeImageFile = &FreeImageFile;
platform->submitDrawCalls = &SubmitDrawCalls;
platform->m_renderer = renderer;
}
void* AllocateStruct( u32 sizeInBytes, struct MemoryPool* pool )
{
assert( pool->m_freeBytes >= sizeInBytes );
void* data = (u8*)pool->m_memory + pool->m_usedBytes;
pool->m_usedBytes += sizeInBytes;
pool->m_freeBytes -= sizeInBytes;
return data;
}
void ClearMemoryPool( struct MemoryPool* pool )
{
pool->m_freeBytes = pool->m_size;
pool->m_usedBytes = 0;
}
struct ImageFile* LoadImageFile( const char* filename, struct MemoryPool* pool )
{
struct ImageFile* image = AllocStruct( struct ImageFile, pool );
s32 channels = 0;
image->m_data = stbi_load( filename, &image->m_width, &image->m_height, &channels, 4 );
assert( image->m_data != NULL );
return image;
}
void FreeImageFile( struct ImageFile* image )
{
stbi_image_free( image->m_data );
}