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

Pages: [1]
1
General / Share SFML Project with others
« on: December 19, 2021, 02:17:38 pm »
Hey,
i want to share my sfml project and obviously there are some errors with .dlls ... i googled a bit and found this thread https://stackoverflow.com/questions/56602222/how-can-i-share-my-sfml-game-with-others-without-errors .
I tried using Dependencies, but it shows that i need some dlls from the SysWOW64 folder ... and even when i add those into my project folder the errors for these two still appear (see attachment).

2
General / Creating an input field
« on: December 14, 2021, 05:33:43 pm »
Hey,
i want to make at the end of my game to let the player input his name to save it to the Leaderboard ... Dont really know how to let the player input only Letters and numbers, do i need to make an Array with all chars that are allowed? and how do i get the button the "player" has pressed? 
hope you can help me

3
General / Use value from another Class
« on: December 10, 2021, 03:16:04 pm »
Hey,
i cant use a value from another class ... in this example videoMode.height, and videoMode.width are 0 even tho i give the value of the window size in void starship::windowValues(int width, int height)
Im quite a beginner in c++ ... so im lost here :(

My plan was to give the size of the window in a function defined in starship.h, and use it in game.cpp to give the value of width and height, and then give these values to sf::VideoMode  videoMode; in starship.cpp.

starship.cpp
#include "game.h"
#include "starship.h"
#include <math.h>

//Constructor/Destructor

starship::starship()
{
    //Init
    initVariables();
    initShip();
    initBullet();
    void windowValues(int , int );
}

starship::~starship()
{
   
}

void starship::initVariables()
{
    // Dont change
    spawnBulletBool = false;
    speedCur = 0.f;

    //Parameters
    acceleration = 0.00001f; //Speed the Ship Accelerates -- Normal 0.000025f - Fast 0.00005f - Slow 0.00001f
    speedMax = 0.1f; //Max Speed the Ship travels -- Normal 0.1f

   
}


//Init Stuff

void starship::initShip()
{
    //Load Texture
    texture.loadFromFile("assets/graphics/starship.png");
    ship.setTexture(texture);

    //Change Origin
    ship.setOrigin(ship.getGlobalBounds().width / 2, ship.getGlobalBounds().height / 2);
    ship.setPosition(500 , 500);

    std::cout << videoMode.height; // <------------------
    std::cout << videoMode.width;  // <------------------
}

void starship::initBullet()
{
    //Load Texture
    bulletTexture.loadFromFile("assets/graphics/bullet.png");
    bullet.setTexture(bulletTexture);

    //Change Origin
    bullet.setOrigin(bullet.getGlobalBounds().width / 2, bullet.getGlobalBounds().height / 2);
}

//Functions

void starship::controlShip()
{
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
    {
        ship.rotate(-1 * 0.06);
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
    {
        ship.rotate(1 * 0.06);
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
    {
        if(speedCur < speedMax)
            speedCur += acceleration;

        ship.move(sin((ship.getRotation() / 180) * 3.14) * speedCur, -1 * cos((ship.getRotation() / 180) * 3.14) * speedCur);
    }    

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    {
        if(speedCur > speedMax * -1)
            speedCur -= acceleration;

        ship.move(sin((ship.getRotation() / 180) * 3.14) * speedCur, -1 * cos((ship.getRotation() / 180) * 3.14) * speedCur);
    }

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && spawnBulletBool == false)
    {
       
        prevTimeBullet = time.asSeconds();

        spawnBulletBool = true;

        spawnBullet();
    } else if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) spawnBulletBool = false;
   
    //Breaking
    if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && !sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
    {
        if(speedCur > 0)
            speedCur -= acceleration;
        if(speedCur < 0)
            speedCur += acceleration;

        ship.move(sin((ship.getRotation() / 180) * 3.14) * speedCur, -1 * cos((ship.getRotation() / 180) * 3.14) * speedCur);
    }
}

void starship::windowValues(int width, int height)
{
    starship::videoMode.width = width;
    starship::videoMode.height = height;
}

void starship::spawnBullet()
{
    bullet.setRotation(ship.getRotation());
    bullet.setPosition(ship.getPosition().x, ship.getPosition().y);

    bullets.push_back(bullet);
}


//Update Stuff

void starship::updateShip()
{
    controlShip();
    updateBullet();
}

void starship::updateBullet()
{
    for(int i = 0; i < bullets.size(); i++)
        bullets[i].move(sin((bullets[i].getRotation() / 180) * 3.14) / 5, -1 * cos((bullets[i].getRotation() / 180) * 3.14) / 5);
}


// Render Stuff

void starship::renderShip(sf::RenderTarget& target)
{
    //Render Bullets
    for(auto &e : this->bullets)
    {
        target.draw(e);
    }

    //Render Ship
    target.draw(ship);
}
 

starship.h
#ifndef __STARSHIP_H
#define __STARSHIP_H

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>

#include <iostream>


class starship
{
public:
    sf::VideoMode videoMode;

    //Functions
    void windowValues(int, int);
    void spawnBullet();

    //Render
    void renderShip(sf::RenderTarget& target);
    void renderBullet(sf::RenderTarget& target);

    //Update
    void updateShip();
    void updateBullet();
    void controlShip();

    //Constructor/Destructor
    starship();
    ~starship();

private:
    //Ship
    sf::Sprite ship;
    sf::Texture texture;

    //Bullets
    sf::Sprite bullet;
    sf::Texture bulletTexture;
    std::vector<sf::Sprite> bullets;
    sf::Time time;

    //Variables
    int prevTimeBullet;
    int attackSpeed;
    bool spawnBulletBool;
    float speedMax;
    float acceleration;
    float speedCur;

    //Init
    void initVariables();
    void initShip();
    void initBullet();
};
#endif

game.cpp
#include "game.h"
#include "starship.h"


//Private functions
void Game::initVariables()
{
    this->window = nullptr;
}
void Game::initWindow()
{

    this->videoMode.width = 1280;
    this->videoMode.height = 720;
   
    starship::windowValues(videoMode.width, videoMode.height);

    this->window = new sf::RenderWindow(this->videoMode, "Window", sf::Style::Titlebar | sf::Style::Close);

    //this->window->setFramerateLimit(140);
}


//Constructors / Destructors
Game::Game()
{
    this->initVariables();
    this->initWindow();

}

Game::~Game()
{
delete this->window;

}

//Accessors
const bool Game::getWindowIsOpen() const
{
    return this->window->isOpen();
}


//Functions
void Game::pollEvent()
{
while(this->window->pollEvent(this->ev))
    {
    switch(this->ev.type)
    {
        case sf::Event::Closed:
        this->window->close();
        break;
        case sf::Event::KeyPressed:
        if (this->ev.key.code == sf::Keyboard::Escape)
            this->window->close();
        break;
        }
    }
}

void Game::update()
{
    //Event polling
    this->pollEvent();
    starship::updateShip();
    enemy::updateAsteroids();

}

void Game::render()
{
    /*
        @return void

        * clear old frame
        * render objects
        * display frame in window
       
        Renders the game objects.
    */


    this->window->clear(sf::Color(190, 63, 63, 255));

    //Draw game objects
    starship::renderShip(*window);
    enemy::renderAsteroids(*window);
    this->window->display();
   
}
 

game.h
#ifndef __GAME_H
#define __GAME_H

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Network.hpp>
#include <SFML/System.hpp>
#include <SFML/Audio.hpp>

#include <iostream>
#include "starship.h"
#include "enemy.h"

/*
    Class that acts as the game engine.
    Wrapper class.
*/


class Game : public starship, public enemy
{
private:


    //Private function
    void initVariables();
    void initWindow();

public:
    //Window
    sf::RenderWindow* window;
    sf::VideoMode videoMode;
    sf::Event ev;


    //Constructors / Destructors
    Game();
    virtual ~Game();

    //Accessors
    const bool getWindowIsOpen() const;

    //Functions
    void pollEvent();
    void update();
    void render();
};

#endif

4
General / Re: undefined reference to Error
« on: December 04, 2021, 02:30:18 pm »
I usually avoid makefiles at all costs, so this is my memory from about 20 years ago, but iirc...
In your original makefile, the line:
g++ -I src/include -c main.cpp
compiles the file main.cpp, which generates the main.o file.
Then
g++ main.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system
says to take main.o and the sfml libraries and link them all together to output the final executable called main.
But game.cpp is never compiled into game.o, and game.o isn't linked with main.o.

CPP files are compiled independently. Then they need to be linked together into a single program.

So you'd need something like:
all: compile link

compile:
        g++ -I src/include -c main.cpp
        g++ -I src/include -c game.cpp


link:
        g++ main.o game.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system
(Or maybe game.o main.o. I can't remember if the order there matters in that situation)

Worked thanks :)

5
General / Re: undefined reference to Error
« on: December 02, 2021, 07:07:51 pm »
It looks like the g++ command line is only linking the main.o. It would also need game.o, which is where the do_something() function is.

Makefile looked like this
all: compile link

compile:
        g++ -I src/include -c main.cpp


link:
        g++ main.o -o main -L src/lib -l sfml-graphics -l sfml-window -l sfml-system

i added to link:
g++ game.o -o game -L src/lib -l sfml-graphics -l sfml-window -l sfml-system

still doesnt work

6
General / undefined reference to Error
« on: December 02, 2021, 06:03:54 pm »
Hello,
started out with SFML and need help with connecting the .cpps and .h

main.cpp
#include "game.h"
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>


int main()
{
    do_something();

    return 0;
}

game.h
    #ifndef __GAME_H
    #define __GAME_H

    void do_something();

    #endif

game.cpp
#include "game.h"
#include "main.cpp"


void do_something()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

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

        window.clear();
        window.draw(shape);
        window.display();
    }
}

hope you can help me
thanks

7
General / Re: Makefile doesnt create main.exe
« on: December 02, 2021, 02:15:04 pm »
I think there shouldn't be a space between -l and sfml-graphics etc.
Nor for -L or -I
tried it ... didnt work

8
General / Makefile doesnt create main.exe
« on: December 02, 2021, 11:52:35 am »
Hey,
still trying to set up my SFML Project.
When i try to execute my Makefile i get an error (screenshot in attachments) ... it creates the main.o but not the .exe . You can see the project here https://github.com/ZonsaC/ReposMT

9
General / Re: Cant find the include Path
« on: December 02, 2021, 11:47:05 am »
Fixed it,
i deleted the Extensions folder in C:\Users\user\.vscode and installed C/C++ again

10
General / Cant find the include Path
« on: December 01, 2021, 11:29:45 pm »
Hey,
just tried to set up the SMFL Library, but my vscode cant find the include Path ...
I added ${workspaceFolder}/** do the include path, but it still cant find the lib
hope you can help me

Pages: [1]