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.


Messages - 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 / Re: angle calculated but sprite doesn't rotate
« on: January 29, 2020, 12:06:38 pm »
For what I know, read it somewhere some time ago, atan2 return the angle in radians, isn't so? Anyway, even if I don't set it in radians, a kind of rotation should be seen since the angle changes. Also I know that sfml method setRotation() must receive an angle in degrees.

EDIT: reading my own words i found the solution. The angle passed to set rotation was in radians lol.
so after I update dx and dy I run
angle /= RADIANS;
and it works well now... I pass to much time with these codes..

3
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?

4
Window / Re: Sprite not in the expected position
« on: January 07, 2020, 01:16:46 pm »
somebody has any idea?

5
Window / Re: Sprite not in the expected position
« on: January 06, 2020, 06:16:39 pm »
The following isn't the solution since there is clearly something happening that shouldn't be that you need to know about but...

If you set the sprite's position to the view's centre, does that place it in the correct place?:
sprite.setPosition(renderWindow.getView().getCenter());

the fact is that the sprite I have to center doesn't know about the render window..

edit: I tried as you said and it works fine, but the code now is a bit messy, and i haven't touched anything since last edit report
edit2: Now, all ok, just the same problematic spaceship is shifted in the bottom, but has the correct axis

6
Window / Re: Sprite not in the expected position
« on: January 06, 2020, 04:46:15 pm »
Thanks Laurent, I did so:
sprite.setOrigin(sprite.getLocalBounds().width/2,sprite.getLocalBounds().height/2);
    xPos = (float)WINDOW_WIDTH/2;
    yPos = (float)WINDOW_HEIGHT/2;
    sprite.setPosition(xPos, yPos);
 

and this is not resolving anything but the fact that now 3 sprites are positioned well and 1 is still not positioned well  :o :-\

7
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)...

8
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?

9
Window / Re: [SOLVED]sf::Keyboard::isKeyPressed is not taking inputs.
« on: November 27, 2019, 07:48:37 pm »
i found the solution by myself, this is really easy and nothing related to sfml, but if someone is a new apple products user as me maybe he is not really used to think about all the permission the mac os ask (and honestly i think it's something very good).

I'm italian so i don't know how english version of macOS calls the various things, but i will translate by meaning hoping this can help.

just open Preferences via apple logo in the top left corner, security and privacy, inputs and be sure Clion or any other ide you use has the permission.

voilĂ  :D


again, this can be silly for most of people, but many others still need this kind of help.

10
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]