First Experiment

This commit is contained in:
Asrın Doğan 2020-01-04 14:20:07 +03:00
parent ef5e4b28e0
commit 1db08172ee
4 changed files with 202 additions and 0 deletions

20
.vscode/c_cpp_properties.json vendored Normal file
View File

@ -0,0 +1,20 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:/MinGW/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.16.27023/bin/Hostx64/x64/cl.exe",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}

85
FirstTest/Player.hpp Normal file
View File

@ -0,0 +1,85 @@
#include <SFML/Graphics.hpp>
class Player
{
private:
sf::Vertex vertex;
sf::Vector2f position;
float movementSpeed;
sf::Vector2f velocity;
public:
Player();
void MovePlayer();
void ApplyForce(sf::Vector2f, float);
void ApplyGravity(float);
void ApplyVelocity(float);
void SetSpeed(float);
void SetPosition(float, float);
sf::Vertex &GetVertex();
sf::Vector2f GetPosition();
};
Player::Player()
{
velocity.x = velocity.y = 0.0f;
movementSpeed = 0.0f;
position.x = position.y = 0.0f;
vertex.color = sf::Color::White;
}
void Player::ApplyForce(sf::Vector2f force, float deltaTime)
{
velocity += force * deltaTime;
}
void Player::MovePlayer()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
velocity.x = movementSpeed;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
velocity.x = -movementSpeed;
else
velocity.x = 0;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
velocity.y = movementSpeed;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
velocity.y = -movementSpeed;
else
velocity.y = 0;
}
void Player::ApplyGravity(float deltaTime)
{
velocity.y -= 10 * deltaTime;
}
void Player::ApplyVelocity(float deltaTime)
{
position += velocity * deltaTime;
}
sf::Vector2f Player::GetPosition()
{
sf::Vector2f result = position;
result.y *= -1;
return result;
}
sf::Vertex &Player::GetVertex()
{
vertex.position = GetPosition();
return vertex;
}
void Player::SetSpeed(float speed)
{
movementSpeed = speed;
}
void Player::SetPosition(float x, float y)
{
position.x = x;
position.y = y;
}

95
FirstTest/main.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <SFML/Graphics.hpp>
#include <sstream> // std::ostringstream
#include <vector>
#include <math.h>
#include "Player.hpp"
#define WindowSize sf::VideoMode(960, 540)
#define WindowStyle sf::Style::Titlebar | sf::Style::Close
#define WindowTitle "Test Window Syntriax"
sf::Vertex CreateVertex(double, double);
int main()
{
std::ostringstream textToDisplay;
sf::RenderWindow window(WindowSize, WindowTitle, WindowStyle);
sf::Event event;
sf::Clock clock;
sf::Text text;
sf::Font font;
float sinValue;
float cosValue;
float timePassed;
float deltaTime;
Player player;
player.SetSpeed(1);
player.SetPosition(500, -200);
std::vector<sf::Vertex> vertices;
if (!font.loadFromFile("../OpenSans-Bold.ttf"))
{
return -1;
}
text.setFont(font);
// window.setVerticalSyncEnabled(true);
window.setFramerateLimit(60);
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if (event.type == sf::Event::LostFocus)
continue;
}
deltaTime = clock.getElapsedTime().asSeconds() - timePassed;
timePassed = clock.getElapsedTime().asSeconds();
sinValue = sin(timePassed);
cosValue = cos(timePassed);
// player.MovePlayer();
player.ApplyGravity(deltaTime);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
player.ApplyForce(sf::Vector2f(0, 20), deltaTime);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
player.ApplyForce(sf::Vector2f(5, 0), deltaTime);
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
player.ApplyForce(sf::Vector2f(-5, 0), deltaTime);
player.ApplyVelocity(deltaTime);
textToDisplay.str("");
textToDisplay.clear();
textToDisplay << "Sin: " << sinValue <<
"\nCos: " << cosValue <<
"\nFPS: " << (1.0 / deltaTime) <<
"\nx, y: " << player.GetPosition().x << ", " << player.GetPosition().y;
text.setString(textToDisplay.str());
vertices.clear();
vertices.push_back(CreateVertex(sinValue * 100 + 200, cosValue * 100 + 200));
vertices.push_back(CreateVertex(cosValue * 100 + 300, cosValue * 100 + 300));
vertices.push_back(CreateVertex(cosValue * 100 + 600, sinValue * 100 + 150));
vertices.push_back(CreateVertex(sinValue * 100 + 200, cosValue * 100 + 200));
vertices[0].color = vertices[3].color = sf::Color::Blue;
vertices[1].color = sf::Color::Green;
vertices[2].color = sf::Color::Red;
window.clear();
window.draw(&vertices[0], vertices.size(), sf::PrimitiveType::LinesStrip);
window.draw(text);
window.draw(&player.GetVertex(), 1, sf::PrimitiveType::Points);
window.display();
}
return 0;
}
sf::Vertex CreateVertex(double x, double y)
{
return sf::Vertex(sf::Vector2f(x, y));
}

2
FirstTest/makefile Normal file
View File

@ -0,0 +1,2 @@
main.exe : *.cpp
g++ -o main.exe main.cpp *.hpp ../SFML/*.a