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 - Lauro

Pages: [1]
1
SFML projects / My first coding program: Xenon
« on: February 09, 2020, 03:08:49 pm »
Hey there, I finished my university project some days ago. I never programmed before, I started with some C and then I went on C++ for object programming.
I want to share my project with you, maybe you will find it somehow fun :)
If you want you can tell me what do you think about it, I would like to learn more from you opinions.

The project is called Xenon and it is a kind of remake of the Atari Asteroids 1979.
My father was in love with that game, and when I told him that I would have done a videogame for a University Project (I could have avoided doing so, the hard way, but the thing you learn coding are far better than the thing you learn with a lot of mere theory, and also if you code something of course you have to know the theory... you can't guess codes), he asked me to give him an enhanced Asteroids game.

Here it is :)
https://github.com/Lauro98/XenonAsteroids

2
Graphics / angle calculated but sprite doesn't rotate
« on: January 29, 2020, 11:22:25 am »
I have a sprite that has to chase a moving target. I calculate the angle of the vector between target and the sprite.
The chasing object is created with a angle = -90;
In every frame I calculate the angle and I update the position this way
    dx=0;
    dy=0;
    destination = spaceship.getSprite().getPosition();
    angle = (float)(atan2((destination.y-sprite.getPosition().y), (destination.x-sprite.getPosition().x)));
    if(sprite.getPosition().x != destination.x && sprite.getPosition().y != destination.y){
        dx=(float)(cos(angle)*ALIEN_ACCEL);
        dy=(float)(sin(angle)*ALIEN_ACCEL);
    }
    xPos += dx;
    yPos += dy;
 

The draw method is written this way

sprite.setOrigin(sprite.getLocalBounds().width/2,sprite.getLocalBounds().height/2);
    sprite.setPosition(xPos, yPos);
    sprite.setRotation(angle+90);
    gameWindow.draw(sprite);
 

The chasing object chase (sorry) well the target one, but it keeps the same angle, it never rotate, I mean, it follows the target but the rotation remains the same.
The target object, which is the player, rotate this way: if the left key is pressed it call a method that does
angle -= bend_factor;
, while if the right key is pressed the method will do
angle += bend_factor;
then the same draw method is called and the rotation is done well (both the player class and the chasing one extend the same class, so the draw method is the same.
What am I missing?

3
Window / Sprite not in the expected position
« on: January 06, 2020, 11:03:33 am »
I create the render window in a state (I use state manager) in this way:
renderWindow.create(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), "Xenon", sf::Style::Fullscreen);
 

where the two macros are 1280 and 800.

in another state(objects are shared among states well, I'm sure), when I create a object that is supposed to be in the center of the screen, it is shifted to the right.
Specifically I have a spaceship class that has 4 "different" spaceship possible. The difference, other than their stats (fire rate, acceleration and so on.. that I will not report since they are not influent), are of course the dimensions of the sprite, that I use to subtract it to the half of the screen dimensions (both in width and height) to move its top left corner and put the spaceship's center in the center of the window.
Three of the spaceships are centred well, one is shifted in the right (correct y-position, x-position shifted to the right side of the screen).

this is how I manage the positioning, I will cut the stats attribution since are irrelevant:
xPos=((float)WINDOW_WIDTH - sprite.getLocalBounds().width)/2;
    yPos=((float)WINDOW_HEIGHT - sprite.getLocalBounds().height)/2;
    std::cout << xPos << ", " << yPos << std::endl;
    sprite.setPosition(xPos, yPos);
    std::cout << "position is" << sprite.getPosition().x << ", " << sprite.getPosition().y << std::endl;
    sprite.setOrigin(sprite.getLocalBounds().width/2,sprite.getLocalBounds().height/2);
 
I use the same code for all the 4 spaceships, in facts the constructor use a switch case for the texture selection and appropriate stats attribution, than it has a shared part (out of the switch case) with this portion of code.

What's the problem? I added the first cout to see what value is assigned to xPos and yPos but it always print the value it should have, and it is the same printed to the second cout, every time i run the program, for all the spaceships.

EDIT: now I changed a minor thing in the program, the entities life is considered as boolean and not as int and now randomly 2 spaceship are in the correct position and 2 are shifted (once i experienced a shift on the left)...

4
Window / Sprite can't be positioned
« on: November 30, 2019, 10:48:05 am »
Hi everyone, i'm having an issue.
in the constructor of the class Starship i put this:
    sprite.setPosition((float)sf::VideoMode::getDesktopMode().width/2, (float)sf::VideoMode::getDesktopMode().height/2);

 
to have the starship sprite in the center of the screen whatever resolution the window is using.

The problem is that the sprite is always constructed in 0,0 even if i put random integers in the setPosition call.

What am i doing wrong?

5
Window / [SOLVED]sf::Keyboard::isKeyPressed is not taking inputs.
« on: November 27, 2019, 11:00:46 am »
hello everybody, i am new in sfml so sorry if i may ask something silly.

i have this:
void GameLoop::loop() {

    gameWindow.clear();
    sf::Event event{};

    while(gameWindow.isOpen()){
        while (gameWindow.pollEvent(event)) {
            if (event.type == sf::Event::EventType::Closed)             //close window
                gameWindow.close();

            //future events here

        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
            playerUp = true;
        if(!(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)))
            playerUp = false;
        std::cout << playerUp << std::endl;


        gameWindow.display();
    }
}
 

I am trying to modify playerUp that is a bool initialised as false in the constructor.
If i am not wrong, i should have understood that the difference between sf::Keyboard::isKeyPressed (as i am doing) and sf::Event::KeyPressed is that the first consider how long you press a key, while the second is only triggered when i press the key so i should handle with more code the fact that it it not released.
Considering that i want to keep the code clean I opted for isKeyPressed out of eventPoll loop.
when i launch the program this is the output:
0
We got a keyboard without any keys (1)
0
0
0
0
//there are only 0s 'till the end even though i press the key
 

I tried to use another key beside Up but with no result.
So, what does that message means? what am i doing wrong?

edit: Forgot to say i am using macOS, maybe this is something that have to be known for you to help me.

side question:
if i got it right sf:Event and sf::Keyboard should be part of Window.hpp, but i am not sure.
By the way in the header I included Graphics.hpp as seen in the tutorial to check if sfml was working and it still find those elements i mentioned, why? Could this be the problem of my question?
Also, where can i check the right "category" (window, graphics, audio ...) of the various elements of sfml? for example if i want to know which is the right category of sf::Event, the guide tell me i need to include Event.hpp, but where can i read if it belongs to window rather then graphics and so on.

sorry for my silly questions :P

Pages: [1]