Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Jycerian

Pages: [1]
1
General / Jerky gameplay
« on: November 19, 2014, 03:23:47 pm »
Hi guys, made a small sample program. That will eventually become my second game. But I got this weird jerky game-play when moving around etc... also notice some screen-tearing and I dont get what causes this weird phenomenon.

Executable: http://puu.sh/cX70G/9dc21552d5.zip
Project Files: http://puu.sh/cX7j3/fd2d7c3238.zip

Anyone got any clue's? Is it also jerky on your computer?

const sf::Time Game::kTimePerFrame = sf::seconds(1.f / 60.f);

Game::Game()
    : window_(sf::VideoMode(720, 540), "Prototype"),
      player_("assets/textures/player.png") {
  window_.setVerticalSyncEnabled(true); // Without this there is less stuttering but also takes like 45% cpu
  camera_.AdaptSize(window_); // Simply gets window size, and set view to same size.
  camera_.Attach(player_); // Set view/camera center to player

  if (!background_texture.loadFromFile("assets/textures/background.png"))
    std::cout << "Failed to load background.png" << std::endl;

  background_.setTexture(background_texture);
}

// Logic loop is fixed at 60, render loop unlimited but limited with v-sync.
void Game::Run() {
  sf::Clock clock;
  sf::Time timeSinceLastUpdate = sf::Time::Zero;
  while (window_.isOpen()) {
    timeSinceLastUpdate += clock.restart();
    while (timeSinceLastUpdate > kTimePerFrame) {
      timeSinceLastUpdate -= kTimePerFrame;

      ProcessEvents();
      Update(kTimePerFrame);
    }
    Render();
  }
}

void Game::ProcessEvents() {
  sf::Event event;
  while (window_.pollEvent(event)) {
    if (event.type == sf::Event::Closed)
      window_.close();

    player_.ProcessEvents(event); // Player events like walking...
  }
}

void Game::Update(const sf::Time& dt) {
  player_.Update(dt);
  camera_.Update(dt);
}

void Game::Render() {
  window_.setView(camera_.GetView()); // Get sf::view from camera class.
  window_.clear(sf::Color(15, 15, 15, 255));
  window_.draw(background_);
  window_.draw(player_);
  window_.display();
}
 

The camera class and player class are very simple so I didn't include them. Could the problem be in this code? I also have an old Asus k52n laptop.

2
General / Drag and Drop - Code Review
« on: October 12, 2014, 07:13:14 pm »
Hey guys, I have basically finished my first game named Gravity Surge (http://en.sfml-dev.org/forums/index.php?topic=15512.0) and now I am busy with my next game and learning more about programming.

My new game will be some sort of level editor/game combo and this is my first attempt for a drag and drop but I have no clue if my code is any good or not because I am not that advanced yet, I would really like to know what your opinions are!

Note : I intentionally made this one file just to learn.



Code :
(click to show/hide)

3
SFML projects / Feedback wanted - Simple Gravity Game.
« on: June 12, 2014, 10:11:27 pm »
SCROLL DOWN


Hi there, yesterday I started working on a new game. Actually my first game I am planning to finish. I would be glad if you guys can give me some feedback.

http://puu.sh/9qHiB/38bc03ce3b.zip



Click with your left mouse button on the spaceship and drag your mouse in the direction you want the spaceship to move then release your left mouse button to move the spaceship. The more you drag your mouse the faster the spaceship will move.

The goal is to reach the wormhole (square), at the moment it will do nothing but in the future it will bring the player to a different section in space (next level).


Note: I only have just started developing, this program is really minimalisitic.

4
General / Inner-Circle Collision / Calculate New Angle.
« on: June 03, 2014, 11:59:18 am »
Hello,

I am having trouble calculating the new angle of the ball because I have not learned this kind of math at school.
How would I get the correct new angle? Can someone explain it to me how it is done? I have a feeling it is simpler than I think but I have no clue on how to make it work. Maby someone can give me some example code.



#include <SFML/Graphics.hpp>
#include <iostream>

#include <cmath>

#define PI 3.14159265359

unsigned const int SCREEN_WIDTH  = 640;
unsigned const int SCREEN_HEIGHT = 480;

int main()
{
    sf::RenderWindow window(sf::VideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32), "C-Long", sf::Style::Close);
    window.setFramerateLimit(60);

    // Loading textures
    sf::Texture ballTexture;
    if (!ballTexture.loadFromFile("Assets/Textures/ball.png")) return 0;

        sf::Texture backgroundTex;
        if (!backgroundTex.loadFromFile("Assets/Textures/background1.png")) return 0;

        sf::Sprite background;
        background.setTexture(backgroundTex);
        background.setOrigin(0,0);
        background.setPosition(0,0);

        // Create ball sprite
    sf::Sprite ball;
    ball.setTexture(ballTexture);
    ball.setOrigin(ball.getGlobalBounds().width / 2, ball.getGlobalBounds().height / 2);
    ball.setPosition(SCREEN_WIDTH / 2 - 50, SCREEN_HEIGHT / 2 + 100);

    // Ball variables
    float ballSpeed = 200.f;
    float ballRadius = ball.getGlobalBounds().width / 2;
    float initBallAngle = 10.f;
    float ballAngle = 0.f;

    ballAngle = initBallAngle * PI / 180.f;

        float xBegin = 0;
        int hitAmmount = 0;
    sf::Clock clock;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        float deltaTime = clock.restart().asSeconds();

        // Move the ball
        float factor = ballSpeed * deltaTime;
                ball.move(cos(ballAngle) * factor, sin(ballAngle) * factor);

                // Distance center / ball
                int ballDistanceFromCenter = (sqrt(pow((SCREEN_WIDTH / 2) - ball.getPosition().x, 2) +
                pow(ball.getPosition().y - (SCREEN_HEIGHT/2), 2)));
               
                if (ballDistanceFromCenter > 230)
                {
                        int difference = (ballDistanceFromCenter - 230);
                       
                        int x,y;
                        // ball out of bounds? set it back x pixels.
                        if (ball.getPosition().x < (SCREEN_WIDTH / 2) && ball.getPosition().y < (SCREEN_HEIGHT / 2))
                        {
                                x = ball.getPosition().x + difference;
                                y = ball.getPosition().y + difference;
                        }
                        else if (ball.getPosition().x > (SCREEN_WIDTH / 2) && ball.getPosition().y > (SCREEN_HEIGHT / 2))
                        {
                                x = ball.getPosition().x - difference;
                                y = ball.getPosition().y - difference;
                        }
                        else if (ball.getPosition().x < (SCREEN_WIDTH / 2) && ball.getPosition().y > (SCREEN_HEIGHT / 2))
                        {
                                x = ball.getPosition().x + difference;
                                y = ball.getPosition().y - difference;
                        }
                        else if (ball.getPosition().x > (SCREEN_WIDTH / 2) && ball.getPosition().y < (SCREEN_HEIGHT / 2))
                        {
                                x = ball.getPosition().x - difference;
                                y = ball.getPosition().y + difference;
                        }
                        ball.setPosition(x, y);
                       
                        //ignore this bad angle calculation.
                        ballAngle = (ballAngle + PI) + ((90 - 30) * PI / 180);
                        if((ballAngle * 180 / PI) > 360)
                                ballAngle =  ((ballAngle * 180 / PI) - 360) * PI / 180;
                        std::cout << ballAngle * 180 / PI << "\n\n";   
                }

                sf::Vertex line1[] =
                {
                        sf::Vertex(sf::Vector2f(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)),
                        sf::Vertex(sf::Vector2f(ball.getPosition().x, ball.getPosition().y))
                };

                sf::Vertex line2[] =
                {
                        sf::Vertex(sf::Vector2f(SCREEN_WIDTH/2, SCREEN_HEIGHT/2)),
                        sf::Vertex(sf::Vector2f(ball.getPosition().x, SCREEN_HEIGHT/2))
                };

                sf::Vertex line3[] =
                {
                        sf::Vertex(sf::Vector2f(ball.getPosition().x, SCREEN_HEIGHT/2)),
                        sf::Vertex(sf::Vector2f(ball.getPosition().x, ball.getPosition().y))
                };

        window.clear();
                window.draw(background);
                window.draw(line1, 2, sf::Lines);
                window.draw(line2, 2, sf::Lines);
                window.draw(line3, 2, sf::Lines);
        window.draw(ball);
        window.display();
    }
    return 0;
}

5
Graphics / Asteroids: Bullets not working as intended.
« on: April 15, 2014, 11:16:52 am »
PROBLEM1: Image not showing, shows white area instead.
PROBLEM2: Game lags really hard when you keep shooting. (probably due to loading the image over and over every new bullet, no clue how to fix)


Can anyone help me with these problems? This is the first time I try to do something with bullets and really have no clue on how to exactly fix this. If you take the time to reply to my topic please keep in mind I am a beginner so please use a language that a beginner will understand, Thank you in advance.



bullet.h
class Bullet: public sf::Drawable {
        static const float lifetime;
        static const float speed;

        public:
                Bullet(sf::Vector2f position, float angle);
                ~Bullet();

                bool isAlive();
                void kill();
                void update(float frametime);
                void draw(sf::RenderTarget& target, sf::RenderStates states) const;
               

        private:
                bool is_alive;
                float remaining_life;
                sf::Vector2f direction;
                sf::Texture bulletTex;
                sf::Sprite bulletSprite;
};

bullet.cpp
const float Bullet::lifetime = 1000.f;
const float Bullet::speed = 0.9f;

Bullet::Bullet(sf::Vector2f position, float angle):
        is_alive(true),
        remaining_life(lifetime),
        direction(cos(angle * DEG2RAD), sin (angle * DEG2RAD)) {
        bulletTex.loadFromFile("Assets/Images/bullet.png");
        bulletSprite.setTexture(bulletTex);
        bulletSprite.setPosition(position);
}

Bullet::~Bullet() {
}

bool Bullet::isAlive() {
        return is_alive;
}

void Bullet::update(float frametime) {
        if (!is_alive) return;

        remaining_life -= frametime;
        if (remaining_life <= 0) is_alive = false;

        sf::Vector2f distance = direction * speed * frametime;
        bulletSprite.move(distance);
}

void Bullet::draw(sf::RenderTarget& target, sf::RenderStates states) const {
        target.draw(bulletSprite);
}

void Bullet::kill() {
        is_alive = false;
}

level.h
class Level {
        public:
                Level();
                ~Level();
                void onEvent(const sf::Event& event);
                void update(float frametime);
                void draw(sf::RenderTarget& target);
                void start();

        private:
                Background background;
                Spaceship ship;

                std::vector<Bullet> bullets;
};

level.cpp
void Level::onEvent(const sf::Event& event) {
        ship.onEvent(event);

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
                Bullet bullet(ship.spaceshipSprite.getPosition(), ship.spaceshipSprite.getRotation());
                bullets.push_back(bullet);
        }
}

void Level::update(float frametime) {
        ship.update(frametime);

        std::vector<Bullet>::iterator start_bullets = bullets.begin();
        while (start_bullets != bullets.end()) {
                if (start_bullets->isAlive()) {
                        start_bullets->update(frametime);
                        ++start_bullets;
                } else
                        start_bullets = bullets.erase(start_bullets);
        }
}

void Level::draw(sf::RenderTarget& target) {
        target.draw(background);
        target.draw(ship);

        for(std::vector<Bullet>::iterator it = bullets.begin(); it != bullets.end(); ++it)
                target.draw(*it);
}

6
General / C++ SFML, Circular Movement - Help!
« on: September 18, 2013, 07:07:13 pm »
I started with creating my first real C++ SFML game based on Pong. Right now the paddles move from left to right but I would like to have them move like the picture below. How can I accomplish this?



Current Code:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

int main()
{
        const int gameWidth  = 640;
        const int gameHeight = 480;

        const float paddleSpeed = 400.f;

        sf::VideoMode videoMode(gameWidth, gameHeight);
    sf::RenderWindow window(videoMode, "Pong", sf::Style::Close);

        // Loading textures
        sf::Texture bgTexture;
        sf::Texture p1Texture;
        sf::Texture p2Texture;
        sf::Texture ballTexture;

        if (!bgTexture.loadFromFile("Media/Textures/Background.png"))
                return EXIT_FAILURE;
        if (!p1Texture.loadFromFile("Media/Textures/Player1.png"))
                return EXIT_FAILURE;
        if (!p2Texture.loadFromFile("Media/Textures/Player2.png"))
                return EXIT_FAILURE;
        if (!ballTexture.loadFromFile("Media/Textures/Ball.png"))
                return EXIT_FAILURE;

        // Loading Fonts
        sf::Font font;
        if (!font.loadFromFile("Media/Fonts/Unibody 8.ttf"))
                return EXIT_FAILURE;

        // Initializing sprites
        sf::Sprite background;
        sf::Sprite player1;
        sf::Sprite player2;
        sf::Sprite ball;

        background.setTexture(bgTexture);
        player1.setTexture   (p1Texture);
        player2.setTexture   (p2Texture);
        ball.setTexture      (ballTexture);

        // Sprites origin
        background.setOrigin(0,0);
        player1.setOrigin(player1.getLocalBounds().width / 2, player1.getLocalBounds().height / 2);
        player2.setOrigin(player2.getLocalBounds().width / 2, player2.getLocalBounds().height / 2);
        ball.setOrigin   (ball.getLocalBounds().width    / 2, ball.getLocalBounds().height    / 2);

        // Sprites position
        background.setPosition(0,0);
        player1.setPosition   (gameWidth / 2, gameHeight - 30);
        player2.setPosition   (gameWidth / 2, 30);
        ball.setPosition      (gameWidth / 2, gameHeight / 2);

        sf::Clock clock;
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                float deltaTime = clock.restart().asSeconds();

                // Move player1 paddle (bottom)
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) &&
                   (player1.getPosition().x - player1.getLocalBounds().width / 2 > 0.f))
                {
                        player1.move(-paddleSpeed * deltaTime, 0.f);
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) &&
                   (player1.getPosition().x + player1.getLocalBounds().width / 2 < gameWidth))
                {
                        player1.move(paddleSpeed * deltaTime, 0.f);
                }


                window.clear(sf::Color::Black);

                // Drawing sprites
                window.draw(background);
                window.draw(player1);
                window.draw(player2);
                window.draw(ball);

                window.display();
        }
        return EXIT_SUCCESS;
}

Pages: [1]
anything