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.


Messages - tiago221

Pages: [1] 2
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. :D

2
Graphics / Bullets in SFML.
« on: September 13, 2009, 08:15:38 pm »
Initialize the bullet sprite and image. Why doesn't it make sense?

3
Graphics / Bullets in SFML.
« on: September 13, 2009, 07:49:39 pm »
He asked the most details possible. :D

I can post relevant code. And I did before, but it seems like it's never enough.


Projectile.h (Bullet Class)

Code: [Select]

#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


Projectile.cpp

Code: [Select]

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


main.cpp Game Loop

Code: [Select]

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

4
Graphics / Bullets in SFML.
« on: September 13, 2009, 07:33:45 pm »

5
Graphics / Bullets in SFML.
« on: September 13, 2009, 07:14:12 pm »
Right now I have a sprite as the player that can walk around the screen and rotate towards the mouse, I have a camera that follows that sprite and I want to make it shoot bullets with Mouse1 or Left Mouse Button. Controls for walking are WASD.
It's a relatively simple top-down zombie survival game where there are some other sprites as zombies going at you (not implemented yet) and you have to kill them.

As simple as that.

I'm uploading the complete source code, including .pngs and everything.

6
Graphics / Bullets in SFML.
« on: September 13, 2009, 06:37:54 pm »
I have a problem with the calculate function.

Code: [Select]

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


It said Move is an undeclared identifier so I added sf::Sprite bullet to it so it would move the bullet. Now, GetRotation I think it's wrong, because I don't know what the function is supposed to get the rotation from, if it is the bullet and I did it right, it still doesn't draw the bullets.

7
Graphics / Bullets in SFML.
« on: September 13, 2009, 01:01:34 pm »
The lag stopped, but I can't see any bullets, are they too fast to see or is the image incorrectly set up?

8
Graphics / Bullets in SFML.
« on: September 13, 2009, 06:46:21 am »
Projectile.h

Code: [Select]

#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


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

void Projectile::Destroy()
{
BulletState = true;
}

bool Projectile::IsDestroyed(const Projectile& b)
{
if(BulletState)
{
return true;
}
else
{
return false;
}
}



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;

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


Whenever I click mouse1 the game gets really laggy but nothing appears.

And how would I create some text that tells me how many bullets were fired? Since only sf::string is drawable.

9
Graphics / Bullets in SFML.
« on: September 13, 2009, 03:29:27 am »
Thanks, if I have any problems I'll post. :D

10
Graphics / Bullets in SFML.
« on: September 13, 2009, 03:10:47 am »
http://www.mediafire.com/download.php?yx5muy1ywxm

The whole source code I have currently.


Error:

1>main.obj : error LNK2019: unresolved external symbol "bool __cdecl IsDestroyed(class Projectile const &)" (?IsDestroyed@@YA_NABVProjectile@@@Z) referenced in function _WinMain@16
1>C:\Users\DarkSpirit\Documents\Visual Studio 2008\Projects\zombie\Debug\zombie.exe : fatal error LNK1120: 1 unresolved externals

11
Graphics / Bullets in SFML.
« on: September 13, 2009, 03:00:35 am »
Nexus, the class is Projectile, Bullet is a Projectile. :P

Also, Bullet.Bullet is a sf::sprite (about the Drawable problem) and if I get Bullet.Bullet in Window.Draw(); I get an unresolved external error from the linker regarding the IsDestroyed boolean variable., and I'm pretty sure I've got all the includes and everything set up.

12
Graphics / Bullets in SFML.
« on: September 12, 2009, 08:36:27 pm »
yeah, I also, edited my post so please check the edit.

Also, I know if I get the bullet system working another problem will come soon, and when it comes, I'll need to fix it, when it's fixed I'll have learned new stuff, I'll have learned how to fix it, like how I have learned about vectors, creating classes like they should be created, etc, with this bullet system. I'm only afraid of reading a book because my attention span is not high enough for it. I always drop off after I get bored, I never got this far into a programming language, and that's because I'm actually working on something while I'm doing it. I know it's not going to be the best game ever, not even close to it, but I'll be happy when I finish it, it'll be my first game, it'll be my work, I had learned and fixed most of the problems that appeared. I'll have a feeling of accomplishment, that will boost me up so I can make better stuff, work better on it.

I tried Python, Lua, C, HTML (even HTML, seriously), etc... And I never could do anything with them besides a simple printing program or a calculator of some sort (to which it applies).

So, yeah, pretty much.

13
Graphics / Bullets in SFML.
« on: September 12, 2009, 08:13:10 pm »
Please don't think I'm just posting error messages and waiting for someone to fix them, it's just that I'm still learning and I think the best way to learn is by doing, and if I don't understand the error messages or something my next step is to post them so I can be able to understand them. Still, thanks for the example.

And I think it's really unnecessary to go through this much trouble just to create a working bullet system.


EDIT:


I fixed most of it, only I got to this one and I don't have any idea about it.

1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(84) : error C2664: 'sf::RenderTarget::Draw' : cannot convert parameter 1 from 'Projectile' to 'const sf::Drawable &'
1>        Reason: cannot convert from 'Projectile' to 'const sf::Drawable'

14
Graphics / Bullets in SFML.
« on: September 12, 2009, 06:36:25 pm »
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(26) : error C2061: syntax error : identifier 'Bullet'
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(68) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'Projectile *' to 'const Projectile &'
1>        with
1>        [
1>            _Ty=Projectile
1>        ]
1>        Reason: cannot convert from 'Projectile *' to 'const Projectile'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(72) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(72) : error C2143: syntax error : missing ',' before '&'
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(74) : error C2923: 'std::vector' : 'Bullet' is not a valid template type argument for parameter '_Ty'
1>        c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(24) : see declaration of 'Bullet'
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(77) : error C2923: 'std::vector' : 'Bullet' is not a valid template type argument for parameter '_Ty'
1>        c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(24) : see declaration of 'Bullet'
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(81) : error C2664: 'std::_Vector_iterator<_Ty,_Alloc> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Ty,_Alloc>)' : cannot convert parameter 1 from 'std::_Vector_iterator<_Ty,_Alloc>' to 'std::_Vector_const_iterator<_Ty,_Alloc>'
1>        with
1>        [
1>            _Ty=Projectile,
1>            _Alloc=std::allocator<Projectile>
1>        ]
1>        and
1>        [
1>            _Ty=int,
1>            _Alloc=std::allocator<int>
1>        ]
1>        and
1>        [
1>            _Ty=Projectile,
1>            _Alloc=std::allocator<Projectile>
1>        ]
1>        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(86) : error C2664: 'sf::RenderTarget::Draw' : cannot convert parameter 1 from 'int' to 'const sf::Drawable &'
1>        Reason: cannot convert from 'int' to 'const sf::Drawable'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous

15
Graphics / Bullets in SFML.
« on: September 12, 2009, 06:10:40 pm »
Code: [Select]

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



BulletClip is an std::vector
BulletP is Projectile* BulletP = new Bullet;
Bullet is a Projectile


1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(26) : error C2061: syntax error : identifier 'Bullet'
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(68) : error C2664: 'std::vector<_Ty>::push_back' : cannot convert parameter 1 from 'Projectile *' to 'const Projectile &'
1>        with
1>        [
1>            _Ty=Projectile
1>        ]
1>        Reason: cannot convert from 'Projectile *' to 'const Projectile'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(71) : warning C4018: '<' : signed/unsigned mismatch
1>c:\users\darkspirit\documents\visual studio 2008\projects\zombie\zombie\main.cpp(80) : error C2664: 'sf::RenderTarget::Draw' : cannot convert parameter 1 from 'Projectile *' to 'const sf::Drawable &'
1>        Reason: cannot convert from 'Projectile *' to 'const sf::Drawable'
1>        No constructor could take the source type, or constructor overload resolution was ambiguous

Pages: [1] 2
anything