Compare commits
7 Commits
a69dc434a9
...
f57d7a8d8a
| Author | SHA1 | Date | |
|---|---|---|---|
| f57d7a8d8a | |||
| 0767278d7c | |||
| 44b806333a | |||
| 6eee029a0a | |||
| 30d808bc80 | |||
| c58a8c2dc7 | |||
| e38bb6e5d0 |
@@ -11,7 +11,12 @@ add_executable(LotoStatek main.cpp
|
|||||||
sources/Player.cpp
|
sources/Player.cpp
|
||||||
headers/Player.h
|
headers/Player.h
|
||||||
headers/Bullet.h
|
headers/Bullet.h
|
||||||
sources/Bullet.cpp)
|
sources/Bullet.cpp
|
||||||
|
headers/Background.h
|
||||||
|
sources/Background.cpp
|
||||||
|
headers/AudioManager.h
|
||||||
|
sources/AudioManager.cpp
|
||||||
|
)
|
||||||
|
|
||||||
if(WIN32)
|
if(WIN32)
|
||||||
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML")
|
set(SFML_ROOT "${CMAKE_SOURCE_DIR}/SFML")
|
||||||
@@ -20,15 +25,15 @@ if(WIN32)
|
|||||||
file(COPY ${BINARY_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
|
file(COPY ${BINARY_DEP_DLLS} DESTINATION ${CMAKE_BINARY_DIR})
|
||||||
include_directories("${CMAKE_SOURCE_DIR}/SFML/bin" "${CMAKE_SOURCE_DIR}/SFML/include" "${CMAKE_SOURCE_DIR}/SFML/lib/cmake/SFML") # dodane
|
include_directories("${CMAKE_SOURCE_DIR}/SFML/bin" "${CMAKE_SOURCE_DIR}/SFML/include" "${CMAKE_SOURCE_DIR}/SFML/lib/cmake/SFML") # dodane
|
||||||
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules") # dodane
|
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake_modules") # dodane
|
||||||
find_package(SFML 2.5.1 COMPONENTS graphics window system REQUIRED)
|
find_package(SFML 2.5.1 COMPONENTS graphics window system audio REQUIRED)
|
||||||
if(SFML_FOUND)
|
if(SFML_FOUND)
|
||||||
include_directories(${SFML_INCLUDE_DIR})
|
include_directories(${SFML_INCLUDE_DIR})
|
||||||
target_link_libraries(LotoStatek ${SFML_LIBRARIES})
|
target_link_libraries(LotoStatek ${SFML_LIBRARIES})
|
||||||
endif()
|
endif()
|
||||||
elseif(APPLE)
|
elseif(APPLE)
|
||||||
find_package(SFML 2.5.1 COMPONENTS graphics window system REQUIRED)
|
find_package(SFML 2.5.1 COMPONENTS graphics window system REQUIRED)
|
||||||
target_link_libraries(LotoStatek sfml-graphics)
|
target_link_libraries(LotoStatek sfml-graphics sfml-window sfml-audio sfml-system)
|
||||||
elseif(LINUX)
|
elseif(LINUX)
|
||||||
find_package(SFML 2.5.1 COMPONENTS graphics window system REQUIRED)
|
find_package(SFML 2.5.1 COMPONENTS graphics window system REQUIRED)
|
||||||
target_link_libraries(LotoStatek sfml-graphics)
|
target_link_libraries(LotoStatek sfml-graphics sfml-window sfml-audio sfml-system)
|
||||||
endif()
|
endif()
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 169 KiB |
BIN
assets/img/space.png
Normal file
BIN
assets/img/space.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.1 MiB |
BIN
assets/music/background.ogg
Normal file
BIN
assets/music/background.ogg
Normal file
Binary file not shown.
BIN
assets/sounds/shoot.ogg
Normal file
BIN
assets/sounds/shoot.ogg
Normal file
Binary file not shown.
BIN
assets/sounds/shoot_alt.ogg
Normal file
BIN
assets/sounds/shoot_alt.ogg
Normal file
Binary file not shown.
23
headers/AudioManager.h
Normal file
23
headers/AudioManager.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
#ifndef AUDIOMANAGER_H
|
||||||
|
#define AUDIOMANAGER_H
|
||||||
|
|
||||||
|
#include <SFML/Audio.hpp>
|
||||||
|
#include <unordered_map>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
class AudioManager {
|
||||||
|
public:
|
||||||
|
bool loadBackgroundMusic(const std::string& filePath);
|
||||||
|
void playBackgroundMusic(float volume = 50.f, bool loop = true);
|
||||||
|
void stopBackgroundMusic();
|
||||||
|
|
||||||
|
bool loadSoundEffect(const std::string& name, const std::string& filePath);
|
||||||
|
void playSoundEffect(const std::string& name, float volume = 50.f);
|
||||||
|
|
||||||
|
private:
|
||||||
|
sf::Music backgroundMusic;
|
||||||
|
std::unordered_map<std::string, sf::SoundBuffer> soundBuffers;
|
||||||
|
std::unordered_map<std::string, sf::Sound> sounds;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // AUDIOMANAGER_H
|
||||||
20
headers/Background.h
Normal file
20
headers/Background.h
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef BACKGROUND_H
|
||||||
|
#define BACKGROUND_H
|
||||||
|
|
||||||
|
#include "SFML/Graphics.hpp"
|
||||||
|
|
||||||
|
class Background {
|
||||||
|
private:
|
||||||
|
sf::Texture texture;
|
||||||
|
sf::Sprite sprite1;
|
||||||
|
sf::Sprite sprite2;
|
||||||
|
float speed;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Background(const std::string& texturePath, float speed);
|
||||||
|
|
||||||
|
void update();
|
||||||
|
void draw(sf::RenderWindow& window);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // BACKGROUND_H
|
||||||
37
main.cpp
37
main.cpp
@@ -4,6 +4,8 @@
|
|||||||
#include "SFML/Graphics.hpp"
|
#include "SFML/Graphics.hpp"
|
||||||
#include "headers/Player.h"
|
#include "headers/Player.h"
|
||||||
#include "headers/Bullet.h"
|
#include "headers/Bullet.h"
|
||||||
|
#include "headers/Background.h"
|
||||||
|
#include "headers/AudioManager.h"
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
@@ -12,9 +14,18 @@ int main()
|
|||||||
window.setVerticalSyncEnabled(true);
|
window.setVerticalSyncEnabled(true);
|
||||||
window.setFramerateLimit(60);
|
window.setFramerateLimit(60);
|
||||||
|
|
||||||
sf::Texture backgroundTexture;
|
Background background("../assets/img/space.png", 2.0f); //tutaj predkosc tla, mozna zwiekszyc jak za wolno
|
||||||
backgroundTexture.loadFromFile("../assets/img/space.jpg"); // wczytywanie tła
|
|
||||||
sf::Sprite backgroundSprite(backgroundTexture); // tworzenie tła
|
|
||||||
|
AudioManager audioManager;
|
||||||
|
if (!audioManager.loadBackgroundMusic("../assets/music/background.ogg")) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
audioManager.playBackgroundMusic();
|
||||||
|
|
||||||
|
audioManager.loadSoundEffect("shoot", "../assets/sounds/shoot.ogg");
|
||||||
|
audioManager.loadSoundEffect("shoot_alt", "../assets/sounds/shoot_alt.ogg");
|
||||||
|
|
||||||
|
|
||||||
Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
|
Player ship(240,650, "../assets/ship/Dreadnought-Base.png"); // tworzenie statku
|
||||||
ship.setMovingSpeed(8);
|
ship.setMovingSpeed(8);
|
||||||
@@ -23,8 +34,6 @@ int main()
|
|||||||
while (window.isOpen()) {
|
while (window.isOpen()) {
|
||||||
window.clear();
|
window.clear();
|
||||||
|
|
||||||
window.draw(backgroundSprite); // narysuj tło
|
|
||||||
|
|
||||||
// Tu są handlowane eventy
|
// Tu są handlowane eventy
|
||||||
sf::Event event{};
|
sf::Event event{};
|
||||||
while (window.pollEvent(event)) {
|
while (window.pollEvent(event)) {
|
||||||
@@ -35,15 +44,20 @@ int main()
|
|||||||
}
|
}
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
|
||||||
ship.shoot();
|
ship.shoot();
|
||||||
|
audioManager.playSoundEffect("shoot");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(event.type == sf::Event::MouseButtonPressed) {
|
if(event.type == sf::Event::MouseButtonPressed) {
|
||||||
if(event.mouseButton.button == sf::Mouse::Left)
|
if(event.mouseButton.button == sf::Mouse::Left) {
|
||||||
ship.shoot();
|
ship.shoot();
|
||||||
else
|
audioManager.playSoundEffect("shoot", 70.f); // Odtworzenie dźwięku wystrzału
|
||||||
|
} else {
|
||||||
ship.alternate_shoot();
|
ship.alternate_shoot();
|
||||||
|
audioManager.playSoundEffect("shoot_alt", 70.f); // Odtworzenie dźwięku dla alternatywnego strzału
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
|
||||||
if(ship.getPosition().x > -10) {
|
if(ship.getPosition().x > -10) {
|
||||||
@@ -71,6 +85,15 @@ int main()
|
|||||||
window.draw(bullet.getSprite());
|
window.draw(bullet.getSprite());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// tło
|
||||||
|
background.update();
|
||||||
|
background.draw(window);
|
||||||
|
|
||||||
|
for (auto& bullet : ship.getBullets()) {
|
||||||
|
bullet.update();
|
||||||
|
window.draw(bullet.getSprite());
|
||||||
|
}
|
||||||
|
|
||||||
ship.updateBullets();
|
ship.updateBullets();
|
||||||
window.draw(ship.getSprite());
|
window.draw(ship.getSprite());
|
||||||
window.display();
|
window.display();
|
||||||
|
|||||||
44
sources/AudioManager.cpp
Normal file
44
sources/AudioManager.cpp
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include "../headers/AudioManager.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
//Dodalem dla zabawy audio tez jako potencjalny dodatek, jbc mozna wywalic
|
||||||
|
|
||||||
|
bool AudioManager::loadBackgroundMusic(const std::string& filePath) {
|
||||||
|
if (!backgroundMusic.openFromFile(filePath)) {
|
||||||
|
std::cerr << "Muzyka tla sie nie zaladowala!!! " << filePath << "\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioManager::playBackgroundMusic(float volume, bool loop) {
|
||||||
|
backgroundMusic.setVolume(volume);
|
||||||
|
backgroundMusic.setLoop(loop);
|
||||||
|
backgroundMusic.play();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioManager::stopBackgroundMusic() {
|
||||||
|
backgroundMusic.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool AudioManager::loadSoundEffect(const std::string& name, const std::string& filePath) {
|
||||||
|
sf::SoundBuffer buffer;
|
||||||
|
if (!buffer.loadFromFile(filePath)) {
|
||||||
|
std::cerr << "Plik z efektem sie nie zaladowal " << filePath << "\n";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
// std::cout << "Zaladowano " << name << " z " << filePath << "\n";
|
||||||
|
soundBuffers[name] = buffer;
|
||||||
|
sounds[name].setBuffer(soundBuffers[name]);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioManager::playSoundEffect(const std::string& name, float volume) {
|
||||||
|
if (sounds.find(name) != sounds.end()) {
|
||||||
|
sounds[name].setVolume(volume);
|
||||||
|
sounds[name].play();
|
||||||
|
} else {
|
||||||
|
std::cerr << "Pliku z efektem " << name << " nie znaleziono!\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
37
sources/Background.cpp
Normal file
37
sources/Background.cpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#include "../headers/Background.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
//logika robimy dwa spraity i sie przesuwaja po sobie i resetuja jak wyjda poza ekran
|
||||||
|
|
||||||
|
Background::Background(const std::string& texturePath, float speed)
|
||||||
|
: speed(speed)
|
||||||
|
{
|
||||||
|
if (!texture.loadFromFile(texturePath)) {
|
||||||
|
std::cerr << "Tlo sie nie zaladowalo!!! " << texturePath << "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
sprite1.setTexture(texture);
|
||||||
|
sprite2.setTexture(texture);
|
||||||
|
|
||||||
|
sprite1.setPosition(0, 0);
|
||||||
|
sprite2.setPosition(0, -texture.getSize().y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Background::update() {
|
||||||
|
// Przesuwanie tła
|
||||||
|
sprite1.move(0, speed);
|
||||||
|
sprite2.move(0, speed);
|
||||||
|
|
||||||
|
// Resetowanie pozycji tła
|
||||||
|
if (sprite1.getPosition().y >= sprite1.getTexture()->getSize().y) {
|
||||||
|
sprite1.setPosition(0, sprite2.getPosition().y - sprite2.getTexture()->getSize().y);
|
||||||
|
}
|
||||||
|
if (sprite2.getPosition().y >= sprite2.getTexture()->getSize().y) {
|
||||||
|
sprite2.setPosition(0, sprite1.getPosition().y - sprite1.getTexture()->getSize().y);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Background::draw(sf::RenderWindow& window) {
|
||||||
|
window.draw(sprite1);
|
||||||
|
window.draw(sprite2);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user