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