Skip to content

Commit 4664ad3

Browse files
committed
Tilemaps implemented
1 parent 3a0771e commit 4664ad3

6 files changed

Lines changed: 130 additions & 2 deletions

File tree

Chapter02/AnimSpriteComponent.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010
#include "SpriteComponent.h"
1111
#include <vector>
1212

13-
class AnimSpriteInfo
13+
struct AnimSpriteInfo
1414
{
15-
public:
1615
AnimSpriteInfo(int start, int end, bool looping)
1716
:start(start)
1817
,end(end)

Chapter02/Game.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "SpriteComponent.h"
1414
#include "Ship.h"
1515
#include "BGSpriteComponent.h"
16+
#include "TileMapComponent.h"
1617

1718
Game::Game()
1819
:mWindow(nullptr)
@@ -179,6 +180,13 @@ void Game::LoadData()
179180
};
180181
bg->SetBGTextures(bgtexs);
181182
bg->SetScrollSpeed(-200.0f);
183+
// Create Tilemap
184+
TileMapComponent* tm = new TileMapComponent(temp);
185+
//tm->LoadTilemap("Assets/MapLayer3.csv", GetTexture("Assets/Tiles.png"), 32);
186+
//tm = new TileMapComponent(temp);
187+
tm->LoadTilemap("Assets/MapLayer2.csv", GetTexture("Assets/Tiles.png"), 32);
188+
tm = new TileMapComponent(temp);
189+
tm->LoadTilemap("Assets/MapLayer1.csv", GetTexture("Assets/Tiles.png"), 32);
182190
}
183191

184192
void Game::UnloadData()

Chapter02/Game.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<ClCompile Include="Math.cpp" />
2121
<ClCompile Include="Ship.cpp" />
2222
<ClCompile Include="SpriteComponent.cpp" />
23+
<ClCompile Include="TileMapComponent.cpp" />
2324
</ItemGroup>
2425
<ItemGroup>
2526
<ClInclude Include="Actor.h" />
@@ -30,6 +31,7 @@
3031
<ClInclude Include="Math.h" />
3132
<ClInclude Include="Ship.h" />
3233
<ClInclude Include="SpriteComponent.h" />
34+
<ClInclude Include="TileMapComponent.h" />
3335
</ItemGroup>
3436
<PropertyGroup Label="Globals">
3537
<ProjectGuid>{BC508D87-495F-4554-932D-DD68388B63CC}</ProjectGuid>

Chapter02/Game.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
<ClCompile Include="BGSpriteComponent.cpp">
3535
<Filter>Source Files</Filter>
3636
</ClCompile>
37+
<ClCompile Include="TileMapComponent.cpp">
38+
<Filter>Source Files</Filter>
39+
</ClCompile>
3740
</ItemGroup>
3841
<ItemGroup>
3942
<ClInclude Include="Actor.h">
@@ -60,5 +63,8 @@
6063
<ClInclude Include="BGSpriteComponent.h">
6164
<Filter>Source Files</Filter>
6265
</ClInclude>
66+
<ClInclude Include="TileMapComponent.h">
67+
<Filter>Source Files</Filter>
68+
</ClInclude>
6369
</ItemGroup>
6470
</Project>

Chapter02/TileMapComponent.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include "TileMapComponent.h"
2+
#include "Actor.h"
3+
#include <fstream>
4+
#include <sstream>
5+
#include <iostream>
6+
7+
TileMapComponent::TileMapComponent(Actor* owner, int drawOrder)
8+
:SpriteComponent(owner, drawOrder)
9+
{
10+
}
11+
12+
void TileMapComponent::Draw(SDL_Renderer* renderer)
13+
{
14+
if (mTexture)
15+
{
16+
SDL_Rect dsrect;
17+
dsrect.w = mTileSize;
18+
dsrect.h = mTileSize;
19+
for (int i = 0; i < mLoadedCSV.size(); i++)
20+
{
21+
dsrect.y = i * mTileSize;
22+
for (int j = 0; j < mLoadedCSV[i].size(); j++)
23+
{
24+
if (mLoadedCSV[i][j] > -1)
25+
{
26+
dsrect.x = j * mTileSize;
27+
28+
// Draw (have to convert angle from radians to degrees, and clockwise to counter)
29+
SDL_RenderCopyEx(renderer,
30+
mTexture,
31+
&mTileRects[mLoadedCSV[i][j]],
32+
&dsrect,
33+
-Math::ToDegrees(mOwner->GetRotation()),
34+
nullptr,
35+
SDL_FLIP_NONE);
36+
}
37+
}
38+
}
39+
}
40+
}
41+
42+
void TileMapComponent::LoadTilemap(std::string&& CSVPath, SDL_Texture* tilemapTexture, int tileSize)
43+
{
44+
mTexture = tilemapTexture;
45+
mTileSize = tileSize;
46+
47+
SDL_QueryTexture(tilemapTexture, nullptr, nullptr, &mTexWidth, &mTexHeight);
48+
49+
// Split the texture in to tiles to use with a csv tilemap.
50+
int tilesW = mTexWidth / tileSize;
51+
int tilesH = mTexHeight / tileSize;
52+
for (int i = 0; i < tilesH; i++)
53+
{
54+
for (int j = 0; j < tilesW; j++)
55+
{
56+
SDL_Rect tilerect;
57+
tilerect.x = j * tileSize;
58+
tilerect.y = i * tileSize;
59+
tilerect.w = tileSize;
60+
tilerect.h = tileSize;
61+
62+
mTileRects.emplace_back(std::move(tilerect));
63+
}
64+
}
65+
66+
std::cout << "Tiles loaded: " << mTileRects.size() << std::endl;
67+
68+
// Load desired tiles from csv file.
69+
std::ifstream fin(std::move(CSVPath));
70+
std::string s;
71+
char cstring[4];
72+
while (std::getline(fin, s))
73+
{
74+
std::istringstream ss;
75+
ss.str(s);
76+
std::vector<int> CSVLine;
77+
while (ss.getline(cstring, 4, ','))
78+
{
79+
CSVLine.emplace_back(stoi(std::string(cstring)));
80+
}
81+
mLoadedCSV.emplace_back(std::move(CSVLine));
82+
}
83+
84+
// Print loaded tilemap to console.
85+
for (auto& intvec : mLoadedCSV)
86+
{
87+
for (int i : intvec)
88+
{
89+
std::cout << i;
90+
}
91+
std::cout << std::endl;
92+
}
93+
}

Chapter02/TileMapComponent.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
#include "SpriteComponent.h"
3+
#include <vector>
4+
#include <string>
5+
#include "Math.h"
6+
7+
8+
9+
class TileMapComponent : public SpriteComponent
10+
{
11+
public:
12+
TileMapComponent(Actor* owner, int drawOrder = 50);
13+
void LoadTilemap(std::string&& CSVPath, SDL_Texture* tilemapTexture, int tileSize);
14+
virtual void Draw(SDL_Renderer* renderer);
15+
private:
16+
std::vector<std::vector<int>> mLoadedCSV;
17+
std::vector<SDL_Rect> mTileRects;
18+
int mTileSize;
19+
};
20+

0 commit comments

Comments
 (0)