69 lines
1.5 KiB
C++
69 lines
1.5 KiB
C++
// =============================================
|
|
// Scratch: graphics.h
|
|
// Copyright (c) 2020-2024 Anish Bhobe
|
|
// =============================================
|
|
|
|
#pragma once
|
|
|
|
#include "global.h"
|
|
|
|
#include "context.h"
|
|
#include "device.h"
|
|
#include "surface.h"
|
|
|
|
struct Window;
|
|
|
|
struct Memory
|
|
{
|
|
u8 *m_Data;
|
|
|
|
u8 *m_TransientMemory;
|
|
usize m_TransientMemorySize;
|
|
|
|
u8 *m_PermanentMemory;
|
|
usize m_PermanentMemoryRemaining;
|
|
|
|
void Init(usize transientSize, usize permanentSize)
|
|
{
|
|
m_Data = new u8[permanentSize + transientSize];
|
|
m_TransientMemory = m_Data + permanentSize;
|
|
m_TransientMemorySize = transientSize;
|
|
m_PermanentMemory = m_Data;
|
|
m_PermanentMemoryRemaining = permanentSize;
|
|
}
|
|
|
|
void
|
|
Destroy()
|
|
{
|
|
delete[] Take(m_Data);
|
|
}
|
|
|
|
template <typename T>
|
|
void MakePermanent(T* &val) requires std::is_default_constructible_v<T>
|
|
{
|
|
const usize alignment = alignof(T);
|
|
const usize size = sizeof(T);
|
|
|
|
const usize offset = (alignment - Recast<uptr>(val) % alignment) % alignment;
|
|
const usize totalSize = size + offset;
|
|
|
|
assert(totalSize <= m_PermanentMemoryRemaining);
|
|
|
|
u8 *block = m_PermanentMemory;
|
|
block += offset;
|
|
m_PermanentMemory += totalSize;
|
|
m_PermanentMemoryRemaining -= totalSize;
|
|
|
|
val = Recast<T *>(block);
|
|
}
|
|
};
|
|
|
|
struct Graphics
|
|
{
|
|
Context m_Context;
|
|
Surface m_Surface;
|
|
Device m_Device;
|
|
|
|
void Init(const Window* window, const cstr appName, const Version version);
|
|
void Destroy();
|
|
}; |