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

Author Topic: A noobie needs help with drawing a bullet sprite  (Read 5848 times)

0 Members and 2 Guests are viewing this topic.

sholmes

  • Newbie
  • *
  • Posts: 4
    • View Profile
A noobie needs help with drawing a bullet sprite
« on: April 07, 2013, 12:07:15 pm »
Hello!

I'm trying to create a simple  western style shooting game with the help of this tutorial:

http://www.sfml-dev.org/tutorials/1.6/graphics-sprite.php

I have managed to make a game character that can be moved around.
However I haven't been able to implement the bullets or the ability to shoot.

I'm using Code::Blocks svn 8928 with a Debian Linux 2.6.32-5-amd64.

So far I have created a class called Projectile from which I create a bullet instance.
The problem is that I  cant get this bullet (bulletInst) drawn to the screen.

Could someone please help me with this?

At below are the three files of the game, which are graphics-sprite.cpp, Projectile.h and Projectile.cpp

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>  graphics-sprite.cpp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#include <SFML/Graphics.hpp>
#include "Projectile.h"

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    // Create a sprite for the background
    sf::Image BackgroundImage;
    if (!BackgroundImage.LoadFromFile("images/background.jpg"))
        return EXIT_FAILURE;
    sf::Sprite Background(BackgroundImage);

    // Load the sprite image from a file
    sf::Image Image;
    if (!Image.LoadFromFile("images/sprite.tga"))
        return EXIT_FAILURE;

    // Create the sprite
    sf::Sprite Sprite(Image);

    // *** Create a bullet instance from the Projectile class ***
    Projectile bulletInst = Projectile(210.f, 110.f);
   
    Sprite.Scale(0.3f, 0.3f);
    Sprite.SetPosition(200.f, 100.f);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Get elapsed time
        float ElapsedTime = App.GetFrameTime();

        // Move the sprite
        if (App.GetInput().IsKeyDown(sf::Key::Left))  Sprite.Move(-100 * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(sf::Key::Right)) Sprite.Move( 100 * ElapsedTime, 0);
        if (App.GetInput().IsKeyDown(sf::Key::Up))    Sprite.Move(0, -100 * ElapsedTime);
        if (App.GetInput().IsKeyDown(sf::Key::Down))  Sprite.Move(0,  100 * ElapsedTime);

        // Clear screen
        App.Clear();

        // Draw background
        App.Draw(Background);

        // Display sprite in our window
        App.Draw(Sprite);

        // *** Trying to display bullet but it does not show up! ***
        App.Draw(bulletInst.bullet);

        // Display window contents on screen
        App.Display();
    }
    return EXIT_SUCCESS;
}   


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Projectile.h >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#ifndef PROJECTILE_H
#define PROJECTILE_H

#include <SFML/Graphics.hpp>

class Projectile
{
    public:
        sf::Image bulletImage;
        sf::Sprite bullet;

        Projectile();
        Projectile(float, float);
        virtual ~Projectile();
   void setPosition(float, float);
    private:
        float x_;
        float y_;
};


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Projectile.cpp >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#include <iostream>
using std::cout;
using std::endl;

#include "Projectile.h"
#include <SFML/Graphics.hpp>

Projectile::Projectile()
{
    cout << "The bullet is here" << endl;
}

Projectile::Projectile(float x, float y) : x_(x), y_(y)
{
    if (!bulletImage.LoadFromFile("images/bullet.png"))
       exit(0);
    sf::Sprite bullet(bulletImage);
    cout << "The bullet is here with a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}

Projectile::~Projectile()
{
    cout << "The bullet was deleted in a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}

void Projectile::setPosition(float x, float y)
{
    x_ = x;
    y_ = y;
}

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< The end of code <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: A noobie needs help with drawing a bullet sprite
« Reply #1 on: April 07, 2013, 05:16:55 pm »
Please make use of the [ code = cpp] forum tag! This would really help with readability of your post.

I'm not a C++ dev, but for your problem unless I am missing it there is no where in your code where you actually set the position of the sprite you are trying to draw. There is nothing magical that automatically moves the sprite to the x_ and y_ in your projectile class.
« Last Edit: April 07, 2013, 05:18:26 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: A noobie needs help with drawing a bullet sprite
« Reply #2 on: April 07, 2013, 05:20:03 pm »
Please read this thread and come up with a minimal complete example.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sholmes

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: A noobie needs help with drawing a bullet sprite
« Reply #3 on: April 08, 2013, 06:47:31 pm »
Hello again!

Thank you for the advice!
I made a minimal code example of my problem
of not being able to draw the bullet (bulletInst) to the screen.

The example consists of three files, which are also included
as an attachment "Sprite_question.tar.gz" :


sprite.cpp:

#include <SFML/Graphics.hpp>
#include "Projectile.h"

int main()
{
    // Create the main rendering window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    // Load the sprite image from a file
    sf::Image Image;
    if (!Image.LoadFromFile("images/sprite.tga"))
        return EXIT_FAILURE;

    // Create the sprite for the game character
    sf::Sprite Sprite(Image);

    // Create the sprite for the bullet with a given position
    Projectile bulletInst = Projectile(250.f, 150.f);

    // bulletInst.Scale(2.f, 2.f);

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        // Clear screen
        App.Clear();

        // Display sprite in our window
        App.Draw(Sprite);

        // Display bullet in our window >>>>> The bulletIns is not showing up! <<<<<<<
        App.Draw(bulletInst.bullet);

        // Display window contents on screen
        App.Display();
    }
    return EXIT_SUCCESS;
}
 

projectile.h:

#ifndef PROJECTILE_H
#define PROJECTILE_H

#include <SFML/Graphics.hpp>

class Projectile
{
    public:
        sf::Sprite bullet;
        Projectile();
        Projectile(float, float);
        virtual ~Projectile();
    protected:
    private:
        sf::Image bulletImage;
        float x_;
        float y_;
};
#endif // PROJECTILE_H
 

projectile.cpp:

#include <iostream>
using std::cout;
using std::endl;

#include "Projectile.h"
#include <SFML/Graphics.hpp>

Projectile::Projectile()
{
    cout << "The bullet is here" << endl;
}

Projectile::Projectile(float x, float y) : x_(x), y_(y)
{
    if (!bulletImage.LoadFromFile("images/bullet.png"))
       exit(0);
    sf::Sprite bullet(bulletImage);
    bullet.SetPosition(x_, y_);
    // bullet.SetScale(2.f, 2.f);
    bullet.Scale(2.f, 2.f);
    cout << "The bullet is here with a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}

Projectile::~Projectile()
{
    cout << "The bullet was deleted in a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}
 

[attachment deleted by admin]

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: A noobie needs help with drawing a bullet sprite
« Reply #4 on: April 09, 2013, 04:57:52 pm »
It looks like it should work (mind you I am no C++ Dev). What happens if you try to load the bullet sprite and draw it all in the main function (basically forget about the projectile class), does that work?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: A noobie needs help with drawing a bullet sprite
« Reply #5 on: April 09, 2013, 05:55:24 pm »
Your example is still not minimal. To know what this means, read the link in my last post carefully.

You should adhere to the C++ rule of three (search it). You should also read about local variables, because you are using them wrong. Then I don't think you want a sf::Image per bullet instance -- read again the SFML documentation/tutorials and try to understand why sf::Image and sf::Sprite are separated into 2 classes. And last, I recommend using SFML 2, version 1.6 is terribly outdated. If something doesn't work, you won't get any support for it.
« Last Edit: April 09, 2013, 05:56:56 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sholmes

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: A noobie needs help with drawing a bullet sprite
« Reply #6 on: April 11, 2013, 03:28:31 pm »
Hello!

Ah! "The rule of three" hint is leading me into the understanding of copy - and copy assingment constructors.
I guess that my problem has something to do with splicing.
 
I'll  also do some research on how to update the SFML version from 1.6 to 2.0.
The older one is the default in the Debian depository.

To get rid of unwanted sf::Image member in the Projectile class I have to study more how to use pointers and/or references as function parameters. This will take me a while. I know there are no short cuts :)




sholmes

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: A noobie needs help with drawing a bullet sprite
« Reply #7 on: April 17, 2013, 12:30:15 pm »
I finally managed to draw the bullet sprite  :)

The error was in the Projectile.cpp where I tried to make an instance of the Projectile class:

sf::Sprite bullet(bulletImage);
 

The sprite drew itself allright after I changed that line to this:

bullet = sf::Sprite(*bulletImage);
 

The problem was not slicing as I thought but that I had made an extra local variable "bullet" that shadowed the one I had declared.

The "rule of three" led me to do some research into the  copy and assingment constructors.
However I didn't include them in order to keep this example as minimal as possible.
Thank you all for your help!

Below are the three sourcefiles.

Sprite.cpp:

#include <SFML/Graphics.hpp>
#include "projectile.h"

int main()
{
    sf::Image bulletImage;
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");

    // Load an image for the bullet sprite
    if (!bulletImage.LoadFromFile("images/bullet.png"))
       return EXIT_FAILURE;

    // Create the sprite for the bullet with a given position
    Projectile bulletInst = Projectile(&bulletImage, 250.f, 150.f);

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }
        App.Clear();
        App.Draw(bulletInst.getBullet());
        App.Display();
    }
    return EXIT_SUCCESS;
}
 

Projectile.h

#ifndef PROJECTILE_H
#define PROJECTILE_H
#include <SFML/Graphics.hpp>

class Projectile
{
    public:
        Projectile();
        Projectile(sf::Image const *, float, float);
        sf::Sprite getBullet() const;
        virtual ~Projectile();
    protected:
    private:
        sf::Sprite bullet;
        float x_;
        float y_;
};
#endif // PROJECTILE_H
 


Projectile.cpp:

#include "projectile.h"
#include <SFML/Graphics.hpp>

#include <iostream>
using std::cout;
using std::endl;

Projectile::Projectile()
{
    cout << "The bullet is here" << endl;
}

Projectile::Projectile(sf::Image const * bulletImage, float x, float y) : x_(x), y_(y)
{
    bullet = sf::Sprite(*bulletImage);
    bullet.SetPosition(x_, y_);
    cout << "The bullet is here with a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}

sf::Sprite Projectile::getBullet() const
{
    return bullet;
}

Projectile::~Projectile()
{
    cout << "The bullet was deleted in a position (x, y): (" << x_ << ", " << y_ << ")"<< endl;
}