Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

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.


Topics - tiago221

Pages: [1]
1
Graphics / Bullets in SFML.
« on: September 11, 2009, 01:08:00 am »
This is what I currently have:

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.

2
Graphics / Rotating sprite towards mouse.
« on: September 07, 2009, 06:38:00 pm »
I was using the following code to do it (found the code here, I take no credit for it), but now, after I successfully got the camera to follow the sprite, the code only works when I start the program, if I move the sprite, it's like the center is out of position, re-setting the center didn't work.

Code: [Select]

#include <SFML/Graphics.hpp>
#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);

sf::View Camera(sf::Vector2f(400.f, 300.f), sf::Vector2f(400.f, 300.f));  
sf::Vector2f PPos = Player.GetPosition();


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();
}

float ElapsedTime = Window.GetFrameTime();


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);  

Player.SetCenter(Player.GetSize() / 2.f);


if (Window.GetInput().GetMouseX() <= Player.GetPosition().x) {
Player.SetRotation(((-1* 360 / PI * (atan2(static_cast<double>(Player.GetPosition().y - Window.GetInput().GetMouseY()), static_cast<double>(Player.GetPosition().x - Window.GetInput().GetMouseX()))))/2)+90);
} else {
Player.SetRotation(((-1* 360 / PI *(atan2(static_cast<double>(Player.GetPosition().y - Window.GetInput().GetMouseY()), static_cast<double>(Player.GetPosition().x - Window.GetInput().GetMouseX()))))/2)+90);
}


Window.Clear();
Camera.SetCenter(follow->GetPosition());
Window.SetView(Camera);
Window.Draw(BackgroundS);
Window.Draw(Player);

Window.Display();
}
}

3
Graphics / Getting the camera to follow a sprite.
« on: September 05, 2009, 11:18:59 pm »
This is what I have so far:

Code: [Select]

#include <SFML/Graphics.hpp>

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);

sf::View Camera(sf::Vector2f(400.f, 300.f), sf::Vector2f(400.f, 300.f));  
sf::Vector2f PPos = Player.GetPosition();




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();
}

float ElapsedTime = Window.GetFrameTime();


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);  
 

Player.SetCenter(Player.GetSize() / 2.f);
if (Window.GetInput().GetMouseX() <= Player.GetPosition().x) {
Player.SetRotation(((-1* 360 / 3.1415926535 * (atan2(static_cast<double>(Player.GetPosition().y - Window.GetInput().GetMouseY()), static_cast<double>(Player.GetPosition().x - Window.GetInput().GetMouseX()))))/2)+90);
} else {
Player.SetRotation(((-1* 360 / 3.1415926535 *(atan2(static_cast<double>(Player.GetPosition().y - Window.GetInput().GetMouseY()), static_cast<double>(Player.GetPosition().x - Window.GetInput().GetMouseX()))))/2)+90);
}

Camera.SetCenter(follow->GetCenter());

Window.Draw(BackgroundS);
Window.Draw(Player);
Window.SetView(Camera);
Window.Display();
}
}


And it doesn't work, the window's contents are flickering a lot.

Pages: [1]
anything