I've been working in a platformer and until now everything is working fine.I wanted the game to be something new so i added a feature where you can resize the window to resize the camera, here are some screenshots:
http://imgur.com/gallery/DAHRV/new
but the problem is that when i resize the window deltaTime changes drastically, and i don't want that because it changes the gameplay(like the jumpheight, speed, etc), i don't know how to fix this and here is the source code:
#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <Windows.h>
int main()
{
sf::RenderWindow window(sf::VideoMode(600, 600), "Platformer !");
sf::Clock deltaClock;
sf::View view(window.getDefaultView());
float deltaTime;
// Start
float Gravity = 0;
float NormalGravity = 2.6;
float SlowGravity = 0.87;
float DownGravity = 25;
float JumpHeight = -1.0;
float Acceleration = 1;
bool IsGrounded = false;
bool Walltouching = false;
bool WalltouchingRight = false;
bool WalltouchingLeft = false;
sf::Vector2f Velocity = sf::Vector2f(0, 0);
sf::Vector2f Position = sf::Vector2f(0, 0);
sf::Vector2f Size = sf::Vector2f(50, 50);
float Speed = 800;
float Horizontal = 0;
float Vertical = 0;
sf::RectangleShape Player;
Player.setSize(Size);
Player.setFillColor(sf::Color(150, 40, 70, 200));
sf::RectangleShape BLOK;
BLOK.setSize(Size + Size);
BLOK.setFillColor(sf::Color(250, 130, 40, 150));
BLOK.setPosition(sf::Vector2f(500, 900));
// Game Loop
while (window.isOpen())
{
sf::Event event;
sf::Time dt = deltaClock.restart();
deltaTime = dt.asSeconds();
while (window.pollEvent(event)) // Events
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::Resized:
window.setView(view = sf::View(sf::FloatRect(0.f, 0.f,
static_cast<float>(window.getSize().x),
static_cast<float>(window.getSize().y))));
break;
/*case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Space){
//if (IsGrounded){
Velocity.y = JumpHeight;
printf("JUMPIN");
//}
}*/
//break;
}
}
// Update
// Horizontal Input
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)){
Horizontal = -1;
if (Acceleration <= 2)
Acceleration += 0.4*deltaTime;
}
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)){
Horizontal = 1;
if (Acceleration <= 2)
Acceleration += 0.*deltaTime;
}
else {
if (Acceleration >= 0.01)
Acceleration = 1;
if (Horizontal > 0)
Horizontal -= 2*deltaTime;
else if (Horizontal < 0)
Horizontal += 2*deltaTime;
else
Horizontal = 0;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)){
Gravity = DownGravity;
}
else
Gravity = NormalGravity;
// Wall Collision
if (Position.x <= 0){
//IsGrounded = true;
Position.x = 0;
Walltouching = true;
WalltouchingLeft = true;
Gravity = SlowGravity;
}
else if (Position.x >= window.getSize().x-50){
//IsGrounded = true;
Position.x = window.getSize().x - 50;
Walltouching = true;
WalltouchingRight = true;
Gravity = SlowGravity;
}
else {
Walltouching = false;
WalltouchingRight = false;
WalltouchingLeft = false;
}
// COLLISION
if (Position.x < BLOK.getPosition().x + 100 &&
Position.x + 50 > BLOK.getPosition().x &&
Position.y < BLOK.getPosition().y + 100 &&
50 + Position.y > BLOK.getPosition().y){
printf("\nTOUCHING !");
}
else{
//system("cls");
}
// Gravity and down collision
if (Position.y >= window.getSize().y - 50){
IsGrounded = true;
Velocity.y = 0;
}
else if (Walltouching){
IsGrounded = true;
Velocity.y += Gravity*deltaTime;
}
else {
IsGrounded = false;
Velocity.y += Gravity*deltaTime;
}
// Jumping Input
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)){
if (IsGrounded){
Velocity.y = JumpHeight;
}
}
// Movement Calculations
//14JumpHeight = -(window.getSize().x / window.getSize().x / 1.5);
Velocity.x = Horizontal*Acceleration*Speed*deltaTime;
Player.setPosition(Position);
Position += Velocity;
printf("\nPOSITION Y: %f", Position.y);
// Draw
window.clear();
window.draw(Player);
window.draw(BLOK);
window.display();
}
return 0;
}
I wanted the game to be something new so i added a feature where you can resize the window to resize the camera
case sf::Event::Resized:
window.setView(view = sf::View(sf::FloatRect(0.f, 0.f,
static_cast<float>(window.getSize().x),
static_cast<float>(window.getSize().y))));
break;
This is a very naive approach for what you want to achieve. The problem is that if you have a 640x480 window and then resize to a 1920x1080 window you will see x3 more horizontally and x2.25 more vertically. So if your character fits "nicely" in one resolution it will be either way too big or way too small on another.
A common approach is to have a fixed vertical or horizontal size and have the other axis vary depending on the aspect ratio.
For a fixed height and variable width:
float height = 10.0f; //fixed height
float width = height * ((float)window.getSize().x / (float)window.getSize().y); //13.3 on 640x480 & 17.7 on 1920x1080
but the problem is that when i resize the window deltaTime changes drastically, and i don't want that because it changes the gameplay(like the jumpheight, speed, etc)
You are not being very clear as to what the problem is (also, don't post your entire code, narrow it down to the individual piece of code that's causing problems and then if you still can't figure it out ask).
One thing to note is that if you drag the window and your application tries to poll for the event it will freeze the thread until the dragging has stopped, which is turn will give a sudden increase in your delta. Not sure if this is related to the problem you're having, though.