1
Graphics / Bullets in SFML.
« on: September 11, 2009, 01:08:00 am »
This is what I currently have:
Projectile.h
Projectile.cpp
main.cpp
Now, when I press the left mouse button, nothing works, what should I do?
Oh, and if someone has any suggestions to improve the coding, please say them.
Thanks in advance.
Projectile.h
Code: [Select]
#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>
class Projectile
{
public:
sf::Image BulletImage;
static sf::Sprite Bullet;
public:
void SetDirection(sf::Sprite);
void SetSpeed(float);
};
#endif
Projectile.cpp
Code: [Select]
#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);
}
main.cpp
Code: [Select]
#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;
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");
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);
float ElapsedTime = Window.GetFrameTime();
sf::Vector2f MousePos = Window.ConvertCoords(Window.GetInput().GetMouseX(), Window.GetInput().GetMouseY());
Window.Clear();
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)) Window.Draw(Projectile::Bullet);
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);
}
}
Now, when I press the left mouse button, nothing works, what should I do?
Oh, and if someone has any suggestions to improve the coding, please say them.
Thanks in advance.