/
TileTest.cpp
112 lines (94 loc) · 2.79 KB
/
TileTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <iostream>
#include <vector>
#include <ctime>
int main()
{
//Create the rendering window
sf::RenderWindow TileTest(sf::VideoMode(800, 600, 32), "Tile Test");
// Load the images from files
std::vector<sf::Image> images(2); // Preset the vector to 2 images
if (!images[0].LoadFromFile("TestTile.png"))
return EXIT_FAILURE;
if (!images[1].LoadFromFile("TestTile2.png"))
return EXIT_FAILURE;
sf::Image TestPlayerImage;
if (!TestPlayerImage.LoadFromFile("circle.png"))
return EXIT_FAILURE;
// Create the sprites
std::vector<sf::Sprite> tiles(2); // Preset the vector to 2 sprites
tiles[0] = sf::Sprite(images[0]); // green
tiles[1] = sf::Sprite(images[1]); // blue
sf::Sprite TestPlayer(TestPlayerImage);
TestPlayer.SetPosition(90.f, 40.f);
//set up the map
std::vector< std::vector<int> > map(11, std::vector<int>(35));
//random seed
srand(time(0));
//randomly generate blue tiles
for (int count = 0; count < 10; count++)
{
int RandomTileX = rand() %11;
int RandomTileY = rand() %35;
map[RandomTileX][RandomTileY] = 1;
}
//running loop
while (TileTest.IsOpened())
{
sf::Event Event;
while (TileTest.GetEvent(Event))
{
//Close window : exit
if (Event.Type == sf::Event::Closed)
TileTest.Close();
//Escape key : exit
if ((Event.Type == sf::Event::KeyPressed)
&& (Event.Key.Code == sf::Key::Escape))
TileTest.Close();
}
//Get elapsed time
float ElapsedTime = TileTest.GetFrameTime();
//Move the player
if (TileTest.GetInput().IsKeyDown(sf::Key::W))
if (TestPlayer.GetPosition().y > 16)
TestPlayer.Move(0, -16);
if (TileTest.GetInput().IsKeyDown(sf::Key::S))
if (TestPlayer.GetPosition().y < 550)
TestPlayer.Move(0, 16);
if (TileTest.GetInput().IsKeyDown(sf::Key::A))
if (TestPlayer.GetPosition().x > 64)
TestPlayer.Move(-32, 0);
if (TileTest.GetInput().IsKeyDown(sf::Key::D))
if (TestPlayer.GetPosition().x < 730)
TestPlayer.Move(32, 0);
//clear the screen
TileTest.Clear();
//draw the map
for (int x = 0; x < map.size (); x++)
{
for (int y = 0; y < map[x].size (); y++)
{
int tileId = map[x][y];
// Get the tile's image
const sf::Image* image = tiles[tileId].GetImage();
// Get the width and height of the image
int width = image->GetWidth();
int height = image->GetHeight();
if ((y % 2) == 1)
{
// Adjust the offset by using the width
tiles[tileId].SetPosition(x * width + width / 2 + 32, y * height / 2);
}
else
{
// Adjust the offset by using the width
tiles[tileId].SetPosition(x * width + 32, y * height / 2);
}
TileTest.Draw(tiles[tileId]);
}
}
TileTest.Draw(TestPlayer);
TileTest.Display();
}
}