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

Pages: [1]
1
General / Incrementing a variable when pushing a button
« on: October 05, 2010, 06:33:15 am »
I'm trying to create an asteroids game clone, and I'm stuck on creating the bullets. I'm pretty sure I've isolated the problem to being when I push space and increment the variable, numberofbullets, that's when the program crashes. When I removed incrementing the variable in that if statement, the program ran fine, the only problem was I only had one bullet being drawn over and over again. Here's the code:

Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;

#include "Bullet.h"
#include "Ship.h"

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

//Loading ship image
Ship ship1;
ship1.loadImage("ship.png");
Bullet bullet[10]; //Bullet array, will make it larger when I get bullets working

int numberofbullets = 0; // Index to count array of bullets
int i;

while(App.IsOpened())
{
sf::Event Event;

while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

if(App.GetInput().IsKeyDown(sf::Key::Up))
ship1.moveForward(App);
if(App.GetInput().IsKeyDown(sf::Key::Right))
ship1.rotateRight(App);
if(App.GetInput().IsKeyDown(sf::Key::Left))
ship1.rotateLeft(App);
if(App.GetInput().IsKeyDown(sf::Key::Space))
{
bullet[numberofbullets].SetPosition(ship1); //Set position of bullet  based on ship
numberofbullets++; // Incrementing so next time player presses space a new bullet is made
}
App.Clear(sf::Color(0, 0, 0));

//Drawing of sprites
ship1.drawShip(App);
for(i=0; i <= numberofbullets; i++) // Draws number of bullets that have been made
{
bullet[numberofbullets].draw(App);
}
if (numberofbullets == 10) // Resets array, will make larger when I get it working
{
numberofbullets = 0;
}


App.Display();

}

return EXIT_SUCCESS;
}


2
Graphics / Nothing is being drawn in my window, may be due to classes
« on: October 02, 2010, 05:54:46 am »
I'm trying to put together an asteroid clone, but nothing's being drawn in my window. I was going through the tutorials, and was able to load a sprite image, display it on the screen, and get keyboard input to get the sprite to move. Then I started a new project. The only thing I did different was make a class called Ship for the ship in the asteroids game.

To make sure I installed SFML 1.6 correctly, I copied and pasted the source code from the project I made from the tutorials, and it was able to display the sprite. Here's my source code:

main.cpp
Code: [Select]

/*
 * Headers, will add later
 */
#include <SFML/Graphics.hpp>
#include "Ship.h"

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

//Loading ship image
Ship ship1;
ship1.loadImage("ship.png");

while(App.IsOpened())
{
sf::Event Event;

while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

if(App.GetInput().IsKeyDown(sf::Key::Up))
ship1.moveForward(App);
if(App.GetInput().IsKeyDown(sf::Key::Right))
ship1.rotateRight(App);
if(App.GetInput().IsKeyDown(sf::Key::Left))
ship1.rotateLeft(App);
}

App.Clear(sf::Color(0, 0, 0));

//Drawing of sprites
ship1.drawShip(App);

App.Display();

return EXIT_SUCCESS;
}


Ship.h
Code: [Select]

/*
 * Ship.h
 * Header file for the ship in the asteroids game
 */

#include <SFML/Graphics.hpp>
#include <string>

using namespace std;

class Ship
{
public:

void loadImage(string imagefilename); // Function loads image and turns it into a sprite
void rotateRight(sf::RenderWindow &game);
void rotateLeft(sf::RenderWindow &game);
void moveForward(sf::RenderWindow &game);
void drawShip(sf::RenderWindow &game);

private:

sf::Sprite shipSprite;
float directionfacingX;
float directionfacingY;
};


Ship.cpp
Code: [Select]

/*
 * Ship.cpp
 * Implementation file for the Ship class
 */

#include <SFML/Graphics.hpp>
#include <string>
#include <cmath>
#include <iostream>

using namespace std;

#include "Ship.h"

// This function loads an image, then converts it to a sprite
void Ship::loadImage(string imagefilename)
{
sf::Image Image;
if (!Image.LoadFromFile(imagefilename))
cout << "Error loading file" << endl;
shipSprite.SetImage(Image);

directionfacingX = 0.0;
directionfacingY = 0.0;

shipSprite.SetColor(sf::Color(0,255,255,128));
shipSprite.SetCenter(10,10);

}

// Used when right arrow key is hit
// Rotates the image and sets new X and Y for movement
void Ship::rotateRight(sf::RenderWindow &game)
{
float ElapsedTime = game.GetFrameTime();
shipSprite.Rotate(-100 * ElapsedTime);

directionfacingX = cos((shipSprite.GetRotation()*3.14)/180);
directionfacingY = sin((shipSprite.GetRotation()*3.14)/180);
}

// Used when left arrow key is hit
// Rotates the image and sets new X and Y for movement
void Ship::rotateLeft(sf::RenderWindow &game)
{
float ElapsedTime = game.GetFrameTime();
shipSprite.Rotate(+100 * ElapsedTime);

directionfacingX = cos((shipSprite.GetRotation()*3.14)/180);
directionfacingY = sin((shipSprite.GetRotation()*3.14)/180);
}

void Ship::moveForward(sf::RenderWindow &game)
{
float ElapsedTime = game.GetFrameTime();
shipSprite.Move(directionfacingX, directionfacingY);
}

void Ship::drawShip(sf::RenderWindow &game)
{
game.Draw(shipSprite);
}

3
General / Having trouble loading an image
« on: September 26, 2010, 03:11:00 am »
I'm getting strange errors in Visual Studio 2010 when running the "Display a Sprite" tutorial. I'm able to run the "Using rendering windows" sample program, but the title of the window it produces isn't "SFML Graphics". It has some strange characters before SFML.

But when I try to run the "Display a Sprite" tutorial, the program starts beeping and then it stops responding. The console window also displays a lot of weird characters.

Since I can run the "Using rendering windows" sample program, I don't think this error has to do with installing SFML, but correct me if I'm wrong.

And the sprite I'm trying to load is a triangle I drew in paint and saved it as a .png.

Can anyone help me?

Pages: [1]
anything