I think I'm getting nuts. Maybe I just need a break
Before optimizing the code I wanted to implement some more features, like making the ship shoot. But when I press the button to trigger the Shoot() function the Bullet's sprite isn't drawn on the screen, even if the function is called. Here's some code:
Ship Class:
struct Ship {
sf::Sprite sprite;
Bullet b = (Bullet)false;
int speed = 5;
enum Side {LEFT, RIGHT};
// Keys
bool isLeftPressed = false;
bool isRightPressed = false;
void setUp() {
sprite.setTexture(tship);
sprite.setScale(0.5f,0.5);
sprite.setPosition((float)WIDTH / 2.0f - (float)sprite.getGlobalBounds().width / 2.0f, HEIGHT * 5.0f / 6.0f);
std::cout << "Ship Builded\n";
}
void move(Side side) {
switch (side)
{
case Ship::LEFT:
sprite.move(-speed, 0);
break;
case Ship::RIGHT:
sprite.move(speed, 0);
break;
default:
break;
}
}
bool keepShipInScreen() {
if (sprite.getPosition().x < 0) {
sprite.setPosition(0, sprite.getPosition().y);
return true;
}
else if (sprite.getPosition().x > WIDTH - sprite.getGlobalBounds().width) {
sprite.setPosition(WIDTH - sprite.getGlobalBounds().width, sprite.getPosition().y);
return true;
}
else return false;
}
void Shoot() {
b.bExist = true;
b.sprite.setPosition(sprite.getPosition().x + sprite.getGlobalBounds().width / 2 - b.sprite.getGlobalBounds().height / 2, sprite.getPosition().y);
}
void handler() {
if (keepShipInScreen() == false) {
if (isLeftPressed == true) {
move(LEFT);
}
if (isRightPressed == true) {
move(RIGHT);
}
}
b.handler();
}
}ship;
Player's Thread:
void ShipThread() {
// Thread Settings
auto ssTimer = std::chrono::steady_clock::now();
while (1) {
auto seTimer = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(seTimer - ssTimer).count() >= 16.67) { // Set Looprate to 60
// Thread
// Player Input
// Move Left
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A) || sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
ship.isLeftPressed = true;
}
else ship.isLeftPressed = false;
// Move Right
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D) || sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
ship.isRightPressed = true;
}
else ship.isRightPressed = false;
// Shoot
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W) || sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
ship.Shoot();
}
// Player Handler
ship.handler();
ssTimer = std::chrono::steady_clock::now(); // Restart Timer
}
}
}
Texture Loading Function:
sf::Texture tship, tenemy, tbullet;
bool textureLoad() {
if (tship.loadFromFile("../Textures/ship.png")) {
std::cout << "Ship Texture Succesfully Loaded\n";
if (tenemy.loadFromFile("../Textures/enemy.png")) {
std::cout << "Enemy Texture Succesfully Loaded\n";
if (tbullet.loadFromFile("../textures/bullet.png")) {
std::cout << "Bullet Texture Succesfully Loaded\n";
return true;
}
else return false;
}
else return false;
}
else return false;
}
main.cpp
#include <SFML/Graphics.hpp>
#include <iostream>
#include <thread>
#include <vector>
#include <stdlib.h>
#include "TextureLoad.h"
#include "Entity.h"
#include "Threads.h"
short WIDTH = 600, HEIGHT = 800;
sf::RenderWindow window;
std::vector<Enemy> e;
int main()
{
srand(time(NULL));
// Setup Game Window
window.create(sf::VideoMode(WIDTH, HEIGHT), "Space Invaders");
window.setFramerateLimit(60);
// Load Textures
textureLoad();
ship.setUp();
// SetUp Level
for (int i = 0; i < 5; i++) {
e.push_back(enemy = (Enemy)i);
}
// Threads
// Enemy
std::thread th_enemy(EnemyThread);
// Ship
std::thread th_ship(ShipThread);
// Game Loop
while (window.isOpen())
{
// Event Handler
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
// Bullet Handler
for (unsigned int i = 0; i < e.size(); i++) {
if (e[i].b.bExist == true) {
e[i].b.handler();
}
}
// Draw
window.clear();
// Draw Enemy
for (unsigned int i = 0; i < e.size(); i++) {
window.draw(e[i].sprite);
}
// Draw Enemy Bullet
for (unsigned int i = 0; i < e.size(); i++) {
if (e[i].b.bExist == true) {
window.draw(e[i].b.sprite);
}
}
// Draw Ship
window.draw(ship.sprite);
// Draw Ship Bullets
if (ship.b.bExist == true) {
window.draw(ship.b.sprite);
}
window.display();
}
th_enemy.join();
th_ship.join();
return 0;
}
I looked if the sprite was drawn out of the windows but it wans't. I checked if the texture was loaded correctly and it was, also because the bullets of the enemies work fine. I have no idea
PLS HELP ME!!