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

Author Topic: Nothing is being drawn in my window, may be due to classes  (Read 2102 times)

0 Members and 1 Guest are viewing this topic.

Treena

  • Newbie
  • *
  • Posts: 7
    • View Profile
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);
}

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
Nothing is being drawn in my window, may be due to classes
« Reply #1 on: October 02, 2010, 01:25:23 pm »
You draw everything once your game has been closed. You have to put
Code: [Select]

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

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

   App.Display();

Inside your while(App.IsOpened()) loop.

Treena

  • Newbie
  • *
  • Posts: 7
    • View Profile
Nothing is being drawn in my window, may be due to classes
« Reply #2 on: October 02, 2010, 04:50:28 pm »
Oh wow... didn't even notice that. Thanks for your help!