⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mem.cpp

📁 小型的3D游戏引擎
💻 CPP
字号:
#include "mem.h"


/////////////////////////////////////////////////////////////////////////////////

bool GcMem::Init(uint size, bool vidMem)
{
	// Save the size of the pool
	poolSize = size;
	usedSize = 0;
	g_Debug->Memory(poolSize);

	if(vidMem)
	{
		// Allocate memory on the graphic card
		memPool = (byte*)wglAllocateMemoryNV(poolSize, 0.0f, 0.0f, 1.0f);

		if(memPool)
		{
			// Activate the use of the NV_VERTEX_ARRAY_RANGE
			glVertexArrayRangeNV(poolSize, memPool);
			glEnableClientState(GL_VERTEX_ARRAY_RANGE_NV);

			// Using video memory
			videoMem = true;

			g_Debug->Log("Using video ram\n");

			return true;
		}
	}

	/* Video memory can either not be used or is wanted to be used */

	// Allocate memory of the system ram
	memPool = (byte*)malloc(poolSize);

	// Using system memory
	videoMem = false;

	g_Debug->Log("Using system ram\n");

	return true;
}

/////////////////////////////////////////////////////////////////////////////////

void *GcMem::Allocate(uint size)
{
	// Check to see that there's enought mem left
	if((poolSize - usedSize) >= size)
	{
		void *address = (void*) ((uint)memPool + usedSize);

		usedSize += size;

		return address;
	}
	else
	{
		// Not enought memory left in the pool
		return NULL;
	}
}

/////////////////////////////////////////////////////////////////////////////////

void GcMem::ShutDown()
{
	// Free the memory pool
	if(videoMem) {
		wglFreeMemoryNV(memPool);
		memPool = NULL;
	}
	else {
		free(memPool);
		memPool = NULL;
	}
}

/////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -