1
Graphics / Bullets in SFML.
« on: September 13, 2009, 08:21:37 pm »
Of course I'll take them, and I'll start working on it right away, thanks.
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.
#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>
class Projectile
{
public:
sf::Image BulletImage;
static sf::Sprite Bullet;
bool BulletState;
float BulletSpeed;
float BulletDirection;
public:
void SetDirection(sf::Sprite);
void SetSpeed(float);
void Destroy(void);
bool IsDestroyed(const Projectile& b);
void Calculate(sf::RenderWindow &app);
};
#endif
#include "Projectile.h"
#include <SFML/Graphics.hpp>
#define PI 3.1415926535
sf::Sprite Projectile::Bullet;
void Projectile::SetDirection(sf::Sprite Reference)
{
float TargetRotation = Reference.GetRotation();
Bullet.SetRotation(static_cast<float>(fmod(TargetRotation, 360)));
float BulletRotation = Bullet.GetRotation();
if (BulletRotation < 0)
Bullet.SetRotation(BulletRotation + 360.f);
}
void Projectile::SetSpeed(float NewSpeed)
{
BulletSpeed = NewSpeed;
}
void Projectile::Destroy()
{
BulletState = true;
}
bool Projectile::IsDestroyed(const Projectile& b)
{
if(BulletState)
{
return true;
}
else
{
return false;
}
}
void Projectile::Calculate(sf::RenderWindow &app)
{
sf::Vector2f offset;
offset.x = static_cast<float>(cos(PI * Bullet.GetRotation() / 180) * BulletSpeed * app.GetFrameTime());
offset.y = static_cast<float>(sin(PI * Bullet.GetRotation() / 180) * BulletSpeed * app.GetFrameTime());
Bullet.Move(offset);
}
while(Window.IsOpened())
{
sf::Event Event;
while (Window.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Window.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Window.Close();
}
Bullet.SetDirection(Player);
Bullet.SetSpeed(2);
Bullet.Calculate(Window);
Window.Clear();
float ElapsedTime = Window.GetFrameTime();
sf::Vector2f MousePos = Window.ConvertCoords(Window.GetInput().GetMouseX(), Window.GetInput().GetMouseY());
if (Window.GetInput().IsKeyDown(sf::Key::W)) Player.Move(0, -425 * ElapsedTime);
if (Window.GetInput().IsKeyDown(sf::Key::S)) Player.Move(0, 425 * ElapsedTime);
if (Window.GetInput().IsKeyDown(sf::Key::A)) Player.Move(-425 * ElapsedTime, 0);
if (Window.GetInput().IsKeyDown(sf::Key::D)) Player.Move(425 * ElapsedTime, 0);
if (Window.GetInput().IsKeyDown(sf::Key::N)) InGameMusic.Play();
if (Window.GetInput().IsKeyDown(sf::Key::M)) InGameMusic.Stop();
if (Window.GetInput().IsMouseButtonDown(sf::Mouse::Left)) shotFired = true;
if (shotFired)
{
BulletClip.push_back(Bullet);
Bullet.BulletState = false;
}
for (std::vector<Projectile>::iterator Itr = BulletClip.begin(), End = BulletClip.end(); Itr != End; )
{
if (Bullet.IsDestroyed(*Itr))
{
Itr = BulletClip.erase(Itr);
}
else
{
Window.Draw(Bullet.Bullet);
++Itr;
}
}
shotFired = false;
Player.SetCenter(Player.GetSize() / 2.f);
Player.SetRotation(((-1* 360 / PI *(atan2(static_cast<double>(Player.GetPosition().y - MousePos.y), static_cast<double>(Player.GetPosition().x - MousePos.x))))/2)+90);
Camera.SetCenter(follow->GetPosition());
Window.SetView(Camera);
Window.Draw(BackgroundS);
Window.Draw(Player);
Window.Display();
InGameMusic.SetLoop(true);
}
void Projectile::Calculate(sf::RenderWindow &app)
{
sf::Vector2f offset;
offset.x = static_cast<float>(cos(PI * Bullet.GetRotation() / 180) * BulletSpeed * app.GetFrameTime());
offset.y = static_cast<float>(sin(PI * Bullet.GetRotation() / 180) * BulletSpeed * app.GetFrameTime());
Bullet.Move(offset);
}
#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>
class Projectile
{
public:
sf::Image BulletImage;
static sf::Sprite Bullet;
bool BulletState;
public:
void SetDirection(sf::Sprite);
void SetSpeed(float);
void Destroy(void);
bool IsDestroyed(const Projectile& b);
};
#endif
#include "Projectile.h"
#include <SFML/Graphics.hpp>
sf::Sprite Projectile::Bullet;
void Projectile::SetDirection(sf::Sprite Reference)
{
float TargetRotation = Reference.GetRotation();
Bullet.SetRotation(static_cast<float>(fmod(TargetRotation, 360)));
float BulletRotation = Bullet.GetRotation();
if (BulletRotation < 0)
Bullet.SetRotation(BulletRotation + 360.f);
}
void Projectile::SetSpeed(float SpeedMultiplier)
{
BulletImage.LoadFromFile("bullet.png");
Bullet.SetImage(BulletImage);
float BulletX = Bullet.GetPosition().x;
float BulletY = Bullet.GetPosition().y;
Bullet.SetX(BulletX * SpeedMultiplier);
Bullet.SetY(BulletY * SpeedMultiplier);
}
void Projectile::Destroy()
{
BulletState = true;
}
bool Projectile::IsDestroyed(const Projectile& b)
{
if(BulletState)
{
return true;
}
else
{
return false;
}
}
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include "Projectile.h"
#define PI 3.1415926535
int WINAPI WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Zombie Game Alpha");
sf::Image PlayerImage;
sf::Image Background;
Background.LoadFromFile("background.png");
PlayerImage.LoadFromFile("player.png");
sf::Sprite Player(PlayerImage);
sf::Sprite BackgroundS(Background);
sf::Sprite* follow = &Player;
Player.SetPosition(400,300);
Projectile Bullet;
Bullet.BulletImage.LoadFromFile("bullet.png");
Bullet.Bullet.SetImage(Bullet.BulletImage);
std::vector <Projectile> BulletClip;
sf::View Camera(sf::Vector2f(400.f, 300.f), sf::Vector2f(400.f, 300.f));
sf::Vector2f PPos = Player.GetPosition();
sf::Music InGameMusic;
InGameMusic.OpenFromFile("in_game.ogg");
bool shotFired = false;
std::vector<Projectile*> AllBullets;
while(Window.IsOpened())
{
sf::Event Event;
while (Window.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
Window.Close();
if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
Window.Close();
}
Bullet.SetDirection(Player);
Bullet.SetSpeed(2);
Window.Clear();
float ElapsedTime = Window.GetFrameTime();
sf::Vector2f MousePos = Window.ConvertCoords(Window.GetInput().GetMouseX(), Window.GetInput().GetMouseY());
if (Window.GetInput().IsKeyDown(sf::Key::W)) Player.Move(0, -425 * ElapsedTime);
if (Window.GetInput().IsKeyDown(sf::Key::S)) Player.Move(0, 425 * ElapsedTime);
if (Window.GetInput().IsKeyDown(sf::Key::A)) Player.Move(-425 * ElapsedTime, 0);
if (Window.GetInput().IsKeyDown(sf::Key::D)) Player.Move(425 * ElapsedTime, 0);
if (Window.GetInput().IsKeyDown(sf::Key::N)) InGameMusic.Play();
if (Window.GetInput().IsKeyDown(sf::Key::M)) InGameMusic.Stop();
if (Window.GetInput().IsMouseButtonDown(sf::Mouse::Left)) shotFired = true;
if (shotFired)
{
BulletClip.push_back(Bullet);
Bullet.BulletState = false;
}
for (std::vector<Projectile>::iterator Itr = BulletClip.begin(), End = BulletClip.end(); Itr != End; )
{
if (Bullet.IsDestroyed(*Itr))
{
Itr = BulletClip.erase(Itr);
}
else
{
Window.Draw(Bullet.Bullet);
++Itr;
}
}
Player.SetCenter(Player.GetSize() / 2.f);
Player.SetRotation(((-1* 360 / PI *(atan2(static_cast<double>(Player.GetPosition().y - MousePos.y), static_cast<double>(Player.GetPosition().x - MousePos.x))))/2)+90);
Camera.SetCenter(follow->GetPosition());
Window.SetView(Camera);
Window.Draw(BackgroundS);
Window.Draw(Player);
Window.Display();
InGameMusic.SetLoop(true);
}
}
if (shotFired == true)
{
BulletClip.push_back(BulletP);
}
for (int i = 0; i < BulletClip.size(); i++)
{
if ( i = 0 )
{
BulletClip.pop_back();
}
else
{
Window.Draw(BulletP);
}
}