SDL2 遊戲開發日記 六 紋理快取

2021-10-01 13:41:21 字數 1262 閱讀 1590

功能:名字相同的紋理只載入一次,重複使用。

紋理快取類採用單例模式。支援從檔案或者從sdl_rwops中載入紋理

#pragma once

#include #include using namespace std;

struct textureinfo

~textureinfo()

}};typedef maptexturelist;

#define texcache texturecache::instance()

class texturecache

texturecache(const texturecache &){}

void operator=(const texturecache &){}

public:

static texturecache& instance()

~texturecache()

mtexturelist.clear();

} textureinfo* gettexture(string filename,sdl_rwops * rwops = null);

// bool addtexture(string filename, textureinfo *textureinfo)

mtexturelist.insert(make_pair(filename, textureinfo));

return true;

} //從快取中移除

void removetexture(string filename)

}};#include "stdafx.h"

#include "texturecache.h"

#include "sdlgame.h"

textureinfo* texturecache::gettexture(string filename, sdl_rwops * rwops)

else

else

if (su***ce != null)

sdl_freesu***ce(su***ce);

} }return info;

}

renderable釋放時不再釋放mtexture。由textruecache統一釋放。

bool renderable::loadtexture(string filename,sdl_rwops *rwops)

} return bresult;

}

SDL2 遊戲開發日誌(二)

構建框架 場景,渲染。負責載入和渲染,它將可以新增到指定的 場景 中,當 場景 被 場景管理類 呼叫時,它將每一幀都被呼叫和更新。pragma once include include using namespace std class renderable virtual renderable i...

SDL2函式簡單介紹01

int sdl init uint32 flags 內部呼叫了sdl initsubsystem 功能 初始化sdl庫,呼叫其他sdl函式之前必須先呼叫此函式 引數 falgs 子系統初始化標誌取下列值的or sdl init timer timer subsystem sdl init audio...

SDL2函式簡單介紹04

1.sdl thread sdl createthread sdl threadfunction fn,const char name,void data 功能 建立乙個新的執行緒 引數 fn 新執行緒中呼叫的函式 name 執行緒的名字 data 傳給fn的引數 返回值 成功返回給新執行緒不透明指...