-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
215 lines (188 loc) · 7.72 KB
/
main.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <SFML/Graphics.hpp>
#include <iostream>
#include <filesystem>
#include <fstream>
#include <vector>
using namespace sf;
using namespace std;
int HEIGHT = 600, WIDTH = 800; // Default size of the window
float TILES = 10; // Number of tiles (sprites) in a column
int TILE_SIZE = 32; // Tile size in viewport points
int MAP[50][50][4]; // Map
int main() {
vector<Texture> textures;
string command, argument;
bool mapReceived = false;
while (!mapReceived) {
ifstream mapfile;
cout << R"(type "new" to create new empty map, and "load" to load existing map)" << "\n";
cin >> command;
if (command == "new") {
mapReceived = true;
continue;
}
else if (command == "load") {
cout << "name of map" << '\n';
cin >> argument;
mapfile.open(argument, ios::out | ios::binary );
if (!mapfile.fail()) {
mapfile.read((char*)&MAP, sizeof(MAP));
mapfile.close();
mapReceived = true;
continue;
}
else{
cout << "error loading texture" << '\n';
}
}
else {
cout << "unknown command" << '\n';
}
}
bool mouseClicked;
bool shiftPressed;
View mainGrid(Vector2f(0, 0), Vector2f(WIDTH, HEIGHT));
View toolsGrid(Vector2f(0, 0), Vector2f(WIDTH, HEIGHT));
RenderWindow window(VideoMode(WIDTH, HEIGHT), "Alien Maintenance");
for (const auto & entry : filesystem::directory_iterator("../sprites")) {
if (entry.path() != "../sprites/empty.png") {
textures.insert(textures.begin(), Texture());
textures[0].loadFromFile(entry.path());
}
}
textures.insert(textures.begin(), Texture());
textures[0].loadFromFile("../sprites/empty.png");
int texturesCount = textures.size();
int textureIndex = 0;
while (window.isOpen()) {
// Event handling
Event event{};
while (window.pollEvent(event)) {
if (event.type == Event::Closed) {
window.close();
return 0;
}
if (event.type == Event::Resized) {
HEIGHT = event.size.height;
WIDTH = event.size.width;
float zoom = TILE_SIZE / (HEIGHT / TILES);
mainGrid.setSize(WIDTH * 0.75, HEIGHT);
mainGrid.setViewport(FloatRect(0,0,0.75,1));
mainGrid.setCenter(WIDTH / 2 * zoom * 0.75 , HEIGHT / 2 * zoom);
mainGrid.zoom(zoom);
toolsGrid.setSize(WIDTH * 0.25, HEIGHT);
toolsGrid.setViewport(FloatRect(0.75,0,0.25,1));
toolsGrid.setCenter((WIDTH / 2) * zoom * 0.25, HEIGHT / 2 * zoom);
toolsGrid.zoom(zoom);
}
if (event.type == Event::MouseButtonPressed || (event.type == Event::MouseMoved && mouseClicked)) {
mouseClicked = true;
Vector2i pixelPos = Mouse::getPosition(window);
if (pixelPos.x < WIDTH * 0.75) {
window.setView(mainGrid);
Vector2f worldPos = window.mapPixelToCoords(pixelPos);
int blockX = (int) worldPos.x / TILE_SIZE;
int blockY = (int) worldPos.y / TILE_SIZE;
if ( Mouse::isButtonPressed( Mouse::Left)) {
if (shiftPressed && textureIndex != 0) {
for (int i = 0; i < 4; i++) {
if (MAP[blockX][blockY][i] == 0) {
MAP[blockX][blockY][i] = textureIndex;
}
if (MAP[blockX][blockY][i] == textureIndex) {
break;
}
}
} else {
for (int i = 0; i < 4; i++) {
MAP[blockX][blockY][i] = 0;
}
MAP[blockX][blockY][0] = textureIndex;
}
} else if ( Mouse::isButtonPressed( Mouse::Right)) {
for (int i = 0; i < 4; i++) {
MAP[blockX][blockY][i] = 0;
}
} else if (Mouse::isButtonPressed(Mouse::Middle)) {
for (int i = 3; i >= 0; i--) {
if (MAP[blockX][blockY][i] != 0) {
textureIndex = MAP[blockX][blockY][i];
break;
}
}
}
} else {
window.setView(toolsGrid);
Vector2f worldPos = window.mapPixelToCoords(pixelPos);
int blockX = (int) worldPos.x / TILE_SIZE;
int blockY = (int) worldPos.y / TILE_SIZE;
int textureNumber = blockX + blockY * (int) (WIDTH * 0.25 / (HEIGHT / TILES));
if (textureNumber < texturesCount) {
textureIndex = textureNumber;
}
}
}
if (event.type == Event::MouseButtonReleased) {
mouseClicked = false;
}
if (event.type == Event::KeyPressed)
{
if (event.key.code == Keyboard::S) {
ofstream mapfile;
cin >> argument;
mapfile.open(argument, ios::out | ios::binary );
mapfile.write((char*)&MAP, sizeof(MAP));
mapfile.close();
}
if (event.key.code == Keyboard::Escape) {
window.close();
return 0;
}
if (event.key.code == Keyboard::LShift || event.key.code == Keyboard::RShift) {
shiftPressed = true;
}
}
if (event.type == Event::KeyReleased) {
if (event.key.code == Keyboard::LShift || event.key.code == Keyboard::RShift) {
shiftPressed = false;
}
}
}
window.setView(mainGrid);
for(int i = 0; i <= 100; i++) {
RectangleShape line(Vector2f(1, window.getSize().y));
line.setPosition(Vector2f(i * TILE_SIZE, 0));
line.setFillColor(Color::Black);
window.draw(line);
}
for(int i = 0; i <= 100; i++) {
RectangleShape line(Vector2f(window.getSize().x, 1));
line.setPosition(Vector2f(0, i * TILE_SIZE));
line.setFillColor(Color::Black);
window.draw(line);
}
for(int x = 0; x < 50; x++) {
for(int y = 0; y < 40; y++) {
for(int t = 0; t < 4; t++) {
Sprite sprite;
sprite.setTexture(textures[MAP[x][y][t]]);
sprite.setPosition( Vector2f(x * TILE_SIZE, y * TILE_SIZE));
window.draw(sprite);
}
}
}
window.setView(toolsGrid);
RectangleShape back(Vector2f(WIDTH * 0.25, HEIGHT));
back.setFillColor(Color(127,127,127));
window.draw(back);
for(int i = 0; i < textures.size(); i++ ) {
Sprite sprite;
sprite.setTexture(textures[i]);
sprite.setPosition( Vector2f(i % (int) (WIDTH * 0.25 / (HEIGHT / TILES)) * TILE_SIZE,
i / (int) (WIDTH * 0.25 / (HEIGHT / TILES)) * TILE_SIZE));
window.draw(sprite);
}
window.display();
window.clear(Color::White);
}
}