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 - sholmes

Pages: [1]
1
Graphics / Re: A noobie needs help with drawing a bullet sprite
« 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;
}
 

2
Graphics / Re: A noobie needs help with drawing a bullet sprite
« 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 :)




3
Graphics / Re: A noobie needs help with drawing a bullet sprite
« 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]

4
Graphics / 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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Pages: [1]
anything