Skip to content

Commit

Permalink
Added support for integrating icon of the chess application
Browse files Browse the repository at this point in the history
  • Loading branch information
GhostVaibhav committed Mar 28, 2021
1 parent 90f7c1c commit 4e5302f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Binary file added icon.res
Binary file not shown.
23 changes: 20 additions & 3 deletions main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <bits/stdc++.h>
using namespace sf;
// For spacing the board and the pieces
Expand All @@ -21,6 +22,7 @@ std::string paths[] {"images\\white-rook.png","images\\white-knight.png","images
// {0,0,0,0,0,0,0,0},
// {6,6,6,6,6,6,6,6},
// {1,2,3,4,5,3,2,1}};
// Updated the layout of the board
const int board[8][8] = {{0,1,2,3,4,5,6,7},
{8,9,10,11,12,13,14,15},
{0,0,0,0,0,0,0,0},
Expand All @@ -45,14 +47,21 @@ void set_position() {
}
int main()
{
// Arp sound used in the background (will probably change in the future)
SoundBuffer buffer;
if(!buffer.loadFromFile("Audio/Arp.wav"))
std::cout << "Error" << std::endl;
Sound sound;
sound.setLoop(true);
sound.setVolume(10.0f);
sound.setBuffer(buffer);
// Font used in the game
Font game_font;
game_font.loadFromFile("Fonts/Montserrat-Regular.ttf");
// Creating a window for containing all the elements
RenderWindow window(VideoMode(1200, 600), "Chess");
// Limiting the framerate to 60 to decrease the load from GPU
window.setFramerateLimit(60);

// Creating a texture and assigning an image to it
Texture text;
text.loadFromFile(board_path);
Expand Down Expand Up @@ -96,19 +105,22 @@ int main()
pieces[i].setTexture(pieces_text[4]);
}

// Some declarations
bool move = false;
int n = 0;
Vector2f oldPos,newPos;
Vector2f d;
set_position();

sound.play();
// Main game loop
while (window.isOpen())
{
// Creating an event which will look on all the activities related to the game window
sf::Event event;
while (window.pollEvent(event))
{
// If we cross thw window then it should close
if (event.type == Event::Closed) {
window.close();
}
Expand Down Expand Up @@ -144,14 +156,15 @@ int main()
if(pieces[i].getGlobalBounds().contains(Mouse::getPosition(window).x,Mouse::getPosition (window).y)) {
move = true;
n = i;
d.x = Mouse::getPosition(window).x - pieces[i].getPosition().x;
d.y = Mouse::getPosition(window).y - pieces[i].getPosition().y;
// d.x = Mouse::getPosition(window).x - pieces[i].getPosition().x;
// d.y = Mouse::getPosition(window).y - pieces[i].getPosition().y;
oldPos = pieces[n].getPosition();
}
}
}
else {
move = false;
// Capturing the new position for movement
for(int i = 0 ; i < 8 ; i++) {
for(int j = 0 ; j < 8 ; j++) {
if((Mouse::getPosition(window).x > X+j*spacing && Mouse::getPosition(window).x < X+(j+1)*spacing) && (Mouse::getPosition(window).y > Y+i*spacing && Mouse::getPosition(window).y < Y+(i+1)*spacing)) {
Expand All @@ -160,12 +173,15 @@ int main()
}
}
}
// Have to check here if the move is valid or not
// Making the position of the previous piece as {-100,-100} so that it disappears
for(int i = 0 ; i < 32 ; i++) {
if(pieces[i].getPosition() == newPos) {
pieces[i].setPosition({-100,-100});
break;
}
}
// Animation of moving the piece
auto dist = newPos - oldPos;
for(int i = 0 ; i < 40 ; i++) {
window.clear(boardColor);
Expand All @@ -175,6 +191,7 @@ int main()
window.draw(pieces[j]);
window.display();
}
// Setting the new position of the piece
pieces[n].setPosition(newPos);
}
}
Expand Down

0 comments on commit 4e5302f

Please sign in to comment.