Slight refactoring
This commit is contained in:
@@ -23,7 +23,7 @@
|
|||||||
|
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
SDL_Surface* TileSheet::LoadSurface(std::string fname, Uint16 w, Uint16 h) {
|
void TileSheet::Load(std::string fname, Uint16 w, Uint16 h) {
|
||||||
//setup the image
|
//setup the image
|
||||||
image.LoadSurface(fname);
|
image.LoadSurface(fname);
|
||||||
image.SetClipW(w);
|
image.SetClipW(w);
|
||||||
@@ -39,11 +39,7 @@ SDL_Surface* TileSheet::LoadSurface(std::string fname, Uint16 w, Uint16 h) {
|
|||||||
end = totalCount;
|
end = totalCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Surface* TileSheet::GetSurface() {
|
void TileSheet::Unload() {
|
||||||
return image.GetSurface();
|
|
||||||
}
|
|
||||||
|
|
||||||
void TileSheet::FreeSurface() {
|
|
||||||
image.FreeSurface();
|
image.FreeSurface();
|
||||||
totalCount = xCount = yCount = 0;
|
totalCount = xCount = yCount = 0;
|
||||||
begin = end = -1;
|
begin = end = -1;
|
||||||
|
|||||||
@@ -37,9 +37,8 @@ public:
|
|||||||
//these load/set functions need to be followed by bookkeeping code
|
//these load/set functions need to be followed by bookkeeping code
|
||||||
//w & h are the width & height of individual tiles
|
//w & h are the width & height of individual tiles
|
||||||
//TODO: rename these
|
//TODO: rename these
|
||||||
SDL_Surface* LoadSurface(std::string fname, Uint16 w, Uint16 h);
|
void Load(std::string fname, Uint16 w, Uint16 h);
|
||||||
SDL_Surface* GetSurface();
|
void Unload();
|
||||||
void FreeSurface();
|
|
||||||
|
|
||||||
void DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex);
|
void DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex);
|
||||||
|
|
||||||
@@ -48,6 +47,9 @@ public:
|
|||||||
//accessors and mutators
|
//accessors and mutators
|
||||||
Image* GetImage() { return ℑ }
|
Image* GetImage() { return ℑ }
|
||||||
|
|
||||||
|
int GetTileW() const { return image.GetClipW(); }
|
||||||
|
int GetTileH() const { return image.GetClipH(); }
|
||||||
|
|
||||||
int GetTotalCount() const { return totalCount; }
|
int GetTotalCount() const { return totalCount; }
|
||||||
int GetXCount() const { return xCount; }
|
int GetXCount() const { return xCount; }
|
||||||
int GetYCount() const { return yCount; }
|
int GetYCount() const { return yCount; }
|
||||||
|
|||||||
@@ -35,10 +35,30 @@ TileSheet* TileSheetManager::LoadSheet(std::string fname, Uint16 w, Uint16 h) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//load & setup the sheet object
|
//load & setup the sheet object
|
||||||
sheetMap[key].LoadSurface(fname, w, h);
|
sheetMap[key].Load(fname, w, h);
|
||||||
sheetMap[key].SetBegin(rangeEnd);
|
sheetMap[key].SetBegin(rangeEnd);
|
||||||
rangeEnd += sheetMap[key].GetTotalCount();
|
rangeEnd += sheetMap[key].GetTotalCount();
|
||||||
sheetMap[key].SetEnd(rangeEnd);
|
sheetMap[key].SetEnd(rangeEnd);
|
||||||
|
|
||||||
|
//you can modify the object, say, during the save & load routines...
|
||||||
|
return &sheetMap[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
TileSheet* TileSheetManager::GetSheet(std::string name) {
|
||||||
|
return &sheetMap.at(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
TileSheet* TileSheetManager::GetSheetByIndex(int tileIndex) {
|
||||||
|
for (auto& it : sheetMap) {
|
||||||
|
if (it.second.InRange(tileIndex)) {
|
||||||
|
return &it.second;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void TileSheetManager::UnloadSheet(std::string name) {
|
||||||
|
sheetMap.erase(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void TileSheetManager::DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex) {
|
void TileSheetManager::DrawTo(SDL_Surface* const dest, int x, int y, int tileIndex) {
|
||||||
|
|||||||
@@ -33,12 +33,9 @@ public:
|
|||||||
~TileSheetManager() = default;
|
~TileSheetManager() = default;
|
||||||
|
|
||||||
TileSheet* LoadSheet(std::string fname, Uint16 w, Uint16 h);
|
TileSheet* LoadSheet(std::string fname, Uint16 w, Uint16 h);
|
||||||
TileSheet* GetSheet(std::string name) {
|
TileSheet* GetSheet(std::string name);
|
||||||
return &sheetMap.at(name);
|
TileSheet* GetSheetByIndex(int tileIndex);
|
||||||
}
|
void UnloadSheet(std::string name);
|
||||||
void UnloadSheet(std::string name) {
|
|
||||||
sheetMap.erase(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
void DrawTo(SDL_Surface* const, int x, int y, int tileIndex);
|
void DrawTo(SDL_Surface* const, int x, int y, int tileIndex);
|
||||||
|
|
||||||
|
|||||||
+10
-9
@@ -63,7 +63,7 @@ EditorScene::EditorScene() {
|
|||||||
pager.SetHeight(32*4);
|
pager.SetHeight(32*4);
|
||||||
|
|
||||||
sheetMgr.LoadSheet("rsc\\graphics\\tilesets\\grass.bmp", 32, 32);
|
sheetMgr.LoadSheet("rsc\\graphics\\tilesets\\grass.bmp", 32, 32);
|
||||||
sheetMgr.LoadSheet("rsc\\graphics\\tilesets\\longgrass.bmp", 32, 32);
|
sheetMgr.LoadSheet("rsc\\graphics\\tilesets\\longgrass.bmp", 16, 16);
|
||||||
|
|
||||||
// loadGameMap("rsc\\maps\\mappy", &pager, &sheetList);
|
// loadGameMap("rsc\\maps\\mappy", &pager, &sheetList);
|
||||||
// saveGameMap("rsc\\maps\\foo", &pager, &sheetList);
|
// saveGameMap("rsc\\maps\\foo", &pager, &sheetList);
|
||||||
@@ -145,19 +145,20 @@ void EditorScene::MouseButtonDown(SDL_MouseButtonEvent const& button) {
|
|||||||
menuBar.MouseButtonDown(button);
|
menuBar.MouseButtonDown(button);
|
||||||
|
|
||||||
if (button.button == SDL_BUTTON_LEFT && button.y >= buttonImage.GetClipH()) {
|
if (button.button == SDL_BUTTON_LEFT && button.y >= buttonImage.GetClipH()) {
|
||||||
Region* ptr = pager.GetRegion(
|
Region* regionPtr = pager.GetRegion(
|
||||||
snapToBase(pager.GetWidth(), button.x + camera.x),
|
snapToBase(pager.GetWidth(), button.x + camera.x),
|
||||||
snapToBase(pager.GetHeight(), button.y + camera.y)
|
snapToBase(pager.GetHeight(), button.y + camera.y)
|
||||||
);
|
);
|
||||||
|
|
||||||
//TODO: find the tileset matching this value, and then use it's width & height for param 4 & 5
|
TileSheet* sheetPtr = sheetMgr.GetSheetByIndex(tileCounter);
|
||||||
ptr->NewTileA({
|
|
||||||
snapToBase(32, button.x + camera.x), //x
|
regionPtr->NewTileA({
|
||||||
snapToBase(32, button.y + camera.y), //y
|
snapToBase(sheetPtr->GetTileW(), button.x + camera.x), //x
|
||||||
|
snapToBase(sheetPtr->GetTileH(), button.y + camera.y), //y
|
||||||
0, //depth
|
0, //depth
|
||||||
32, //width (from tileset)
|
sheetPtr->GetTileW(), //width
|
||||||
32, //height (from tileset)
|
sheetPtr->GetTileH(), //height
|
||||||
0 //value
|
tileCounter++ //value
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -71,6 +71,8 @@ protected:
|
|||||||
struct {
|
struct {
|
||||||
int x = 0, y = 0;
|
int x = 0, y = 0;
|
||||||
} camera;
|
} camera;
|
||||||
|
|
||||||
|
int tileCounter = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user