trop de trucs oscours
This commit is contained in:
@@ -1,138 +1,116 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include "../Core/Menu.h"
|
||||
#include "GraphApp.h"
|
||||
#include "../Pieces/PiecesFiles.h"
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
void setToDefaultConfig();
|
||||
|
||||
int main() {
|
||||
std::srand(std::time(NULL));
|
||||
|
||||
sf::RenderWindow window(sf::VideoMode({800, 640}), "My window", sf::Style::Titlebar | sf::Style::Close);
|
||||
window.setPosition(sf::Vector2i(sf::VideoMode::getDesktopMode().size.x / 2 - 400, sf::VideoMode::getDesktopMode().size.y / 2 - 320));
|
||||
|
||||
// dev version
|
||||
PiecesFiles pf;
|
||||
for (int i = 1; i <= 10; i++) {
|
||||
pf.savePieces(i);
|
||||
for (int i = 1; i <= MAXIMUM_PIECES_SIZE; i++) {
|
||||
if (!std::filesystem::exists("data/pieces/" + std::to_string(i) + "minos.bin")) {
|
||||
std::cout << "pieces files for size " << i << " not found, generating..." << std::endl;
|
||||
pf.savePieces(i);
|
||||
}
|
||||
}
|
||||
if (!std::filesystem::exists("data/config/settings.bin")) {
|
||||
resetSettingsFile();
|
||||
}
|
||||
for (int i = 0; i < 5; i++) {
|
||||
if (!std::filesystem::exists("data/config/keybinds/layout" + std::to_string(i) + ".bin")) {
|
||||
resetKeybindFile(i);
|
||||
}
|
||||
}
|
||||
|
||||
Menu m;
|
||||
m.getPiecesList().loadPieces(10);
|
||||
m.getPiecesList().selectAllPieces(4);
|
||||
m.setBoardWidth(10);
|
||||
m.getPlayerControls().setDAS(6);
|
||||
m.getPlayerControls().setARR(0);
|
||||
m.getPlayerControls().setSDR(0);
|
||||
Game game = m.startGame(SPRINT);
|
||||
game.start();
|
||||
// release version
|
||||
//resetConfigFiles();
|
||||
|
||||
sf::Clock clock;
|
||||
GraphApp UI;
|
||||
UI.run();
|
||||
|
||||
sf::Font font;
|
||||
if (!font.openFromFile("data/fonts/arial.ttf")) {
|
||||
std::cout << "aaaaaaaaaaaaaa";
|
||||
}
|
||||
sf::Text text(font);
|
||||
text.setCharacterSize(20);
|
||||
text.setFillColor(sf::Color::White);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (window.isOpen()) {
|
||||
while (const std::optional event = window.pollEvent()) {
|
||||
if (event->is<sf::Event::Closed>())
|
||||
window.close();
|
||||
}
|
||||
|
||||
if (clock.getElapsedTime().asMilliseconds() > 16) {
|
||||
clock.restart();
|
||||
|
||||
window.clear(sf::Color::Black);
|
||||
|
||||
std::set<Action> actions;
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left)) {
|
||||
actions.insert(MOVE_LEFT);
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Right)) {
|
||||
actions.insert(MOVE_RIGHT);
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Up)) {
|
||||
actions.insert(HARD_DROP);
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Down)) {
|
||||
actions.insert(SOFT_DROP);
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::A)) {
|
||||
actions.insert(ROTATE_CCW);
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::E)) {
|
||||
actions.insert(ROTATE_CW);
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Z)) {
|
||||
actions.insert(HOLD);
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Tab)) {
|
||||
actions.insert(ROTATE_0);
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Num2)) {
|
||||
actions.insert(ROTATE_180);
|
||||
}
|
||||
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::R)) {
|
||||
game.reset();
|
||||
game.start();
|
||||
}
|
||||
game.nextFrame(actions);
|
||||
|
||||
for (int y = game.getBoard().getBaseHeight() + 5; y >= 0; y--) {
|
||||
for (int x = 0; x < game.getBoard().getWidth(); x++) {
|
||||
bool isActivePieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.getActivePiecePosition()));
|
||||
bool isGhostPieceHere = (game.getActivePiece() != nullptr) && (game.getActivePiece()->getPositions().contains(Position{x, y} - game.ghostPiecePosition()));
|
||||
Block block = (isActivePieceHere || isGhostPieceHere) ? game.getActivePiece()->getBlockType() : game.getBoard().getBlock(Position{x, y});
|
||||
|
||||
sf::RectangleShape cell(sf::Vector2f(20.f, 20.f));
|
||||
cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue, (isGhostPieceHere && !isActivePieceHere) ? 150 : 255));
|
||||
cell.setPosition(sf::Vector2f(x*20, (game.getBoard().getBaseHeight() + 10 - y)*20));
|
||||
window.draw(cell);
|
||||
}
|
||||
}
|
||||
|
||||
if (game.getNextPieces().size() > 0) {
|
||||
for (int y = 10; y >= 0; y--) {
|
||||
for (int x = 0; x <= 10; x++) {
|
||||
Block block = game.getNextPieces().at(0).getBlockType();
|
||||
sf::RectangleShape cell(sf::Vector2f(20.f, 20.f));
|
||||
cell.setPosition(sf::Vector2f((x + 2 + game.getBoard().getWidth())*20, (game.getBoard().getBaseHeight() - y)*20));
|
||||
if (game.getNextPieces().at(0).getPositions().contains(Position({x, y}))) {
|
||||
cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue));
|
||||
}
|
||||
else {
|
||||
cell.setFillColor(sf::Color(0, 0, 0));
|
||||
}
|
||||
window.draw(cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (game.getHeldPiece() != nullptr) {
|
||||
for (int y = 10; y >= 0; y--) {
|
||||
for (int x = 0; x <= 10; x++) {
|
||||
Block block = game.getHeldPiece()->getBlockType();
|
||||
sf::RectangleShape cell(sf::Vector2f(20.f, 20.f));
|
||||
cell.setPosition(sf::Vector2f((x + 12 + game.getBoard().getWidth())*20, (game.getBoard().getBaseHeight() - y)*20));
|
||||
if (game.getHeldPiece()->getPositions().contains(Position({x, y}))) {
|
||||
cell.setFillColor(sf::Color(BLOCKS_COLOR[block].red, BLOCKS_COLOR[block].green, BLOCKS_COLOR[block].blue));
|
||||
}
|
||||
else {
|
||||
cell.setFillColor(sf::Color(0, 0, 0));
|
||||
}
|
||||
window.draw(cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
text.setPosition(sf::Vector2f(12*20, (game.getBoard().getBaseHeight() - 5)*20));
|
||||
text.setString(sf::String(std::to_string(game.getClearedLines())));
|
||||
window.draw(text);
|
||||
|
||||
window.display();
|
||||
}
|
||||
void resetConfigFiles() {
|
||||
resetSettingsFile;
|
||||
for (int i = 0; i < 5; i++) {
|
||||
resetKeybindFile(i);
|
||||
}
|
||||
}
|
||||
|
||||
void resetSettingsFile() {
|
||||
|
||||
}
|
||||
|
||||
void resetKeybindFile(int layout) {
|
||||
if (layout < 0 || layout > 4) return;
|
||||
|
||||
std::ofstream layoutFile("data/config/keybinds/layout" + std::to_string(layout) + ".bin", std::ios::trunc | std::ios::binary);
|
||||
std::map<Action, sfKey> keybinds;
|
||||
|
||||
if (layout != 4) {
|
||||
keybinds.insert({PAUSE, sfKey::P});
|
||||
keybinds.insert({RETRY, sfKey::R});
|
||||
}
|
||||
|
||||
if (layout == 0) {
|
||||
keybinds.insert({MOVE_LEFT, sfKey::Left});
|
||||
keybinds.insert({MOVE_RIGHT, sfKey::Right});
|
||||
keybinds.insert({SOFT_DROP, sfKey::Down});
|
||||
keybinds.insert({HARD_DROP, sfKey::Space});
|
||||
keybinds.insert({ROTATE_CW, sfKey::Up});
|
||||
keybinds.insert({ROTATE_CCW, sfKey::Z});
|
||||
keybinds.insert({ROTATE_180, sfKey::X});
|
||||
keybinds.insert({ROTATE_0, sfKey::LShift});
|
||||
keybinds.insert({HOLD, sfKey::C});
|
||||
}
|
||||
if (layout == 1) {
|
||||
keybinds.insert({MOVE_LEFT, sfKey::Z});
|
||||
keybinds.insert({MOVE_RIGHT, sfKey::C});
|
||||
keybinds.insert({SOFT_DROP, sfKey::X});
|
||||
keybinds.insert({HARD_DROP, sfKey::S});
|
||||
keybinds.insert({ROTATE_CW, sfKey::M});
|
||||
keybinds.insert({ROTATE_CCW, sfKey::Comma});
|
||||
keybinds.insert({ROTATE_180, sfKey::J});
|
||||
keybinds.insert({ROTATE_0, sfKey::K});
|
||||
keybinds.insert({HOLD, sfKey::LShift});
|
||||
}
|
||||
if (layout == 2) {
|
||||
keybinds.insert({MOVE_LEFT, sfKey::A});
|
||||
keybinds.insert({MOVE_RIGHT, sfKey::D});
|
||||
keybinds.insert({SOFT_DROP, sfKey::W});
|
||||
keybinds.insert({HARD_DROP, sfKey::S});
|
||||
keybinds.insert({ROTATE_CW, sfKey::Left});
|
||||
keybinds.insert({ROTATE_CCW, sfKey::Right});
|
||||
keybinds.insert({ROTATE_180, sfKey::Up});
|
||||
keybinds.insert({ROTATE_0, sfKey::Down});
|
||||
keybinds.insert({HOLD, sfKey::RShift});
|
||||
}
|
||||
if (layout == 3) {
|
||||
keybinds.insert({MOVE_LEFT, sfKey::Left});
|
||||
keybinds.insert({MOVE_RIGHT, sfKey::Right});
|
||||
keybinds.insert({SOFT_DROP, sfKey::Down});
|
||||
keybinds.insert({HARD_DROP, sfKey::Up});
|
||||
keybinds.insert({ROTATE_CW, sfKey::E});
|
||||
keybinds.insert({ROTATE_CCW, sfKey::A});
|
||||
keybinds.insert({ROTATE_180, sfKey::Num2});
|
||||
keybinds.insert({ROTATE_0, sfKey::Tab});
|
||||
keybinds.insert({HOLD, sfKey::Z});
|
||||
}
|
||||
|
||||
char contentChar;
|
||||
for (Action action : ACTION_LIST_IN_ORDER) {
|
||||
contentChar = action;
|
||||
layoutFile.write(&contentChar, 1);
|
||||
|
||||
if (keybinds.contains(action)) {
|
||||
contentChar = (int) keybinds.at(action);
|
||||
layoutFile.write(&contentChar, 1);
|
||||
}
|
||||
|
||||
contentChar = 0xFF;
|
||||
layoutFile.write(&contentChar, 1);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user