9 Replies - 696 Views - Last Post: 29 April 2013 - 01:08 PM Rate Topic: -----

#1 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

sfml sprite not drawing

Posted 29 April 2013 - 08:29 AM

hey guyz i started creating a space shooter type game and ran into an error
i currently have a ship class and a main file cpp .. the problem is that my ship wont draw on the screen plzz take a look at the code
here are the files

ship.h

#pragma once
#include<iostream>
#include<SFML\graphics.hpp>

class ship{
   private:
	   sf::Texture st;
	   sf::Sprite ss;
	   int frame ;
   public:
	   int xvel;
       int yvel;
	   int life;
	   ship(float x,float y);
	   void move(sf::RenderWindow &name);
	   };
ship::ship(float x ,float y)
{
	life=100;
	if(!st.loadFromFile("bul1.png")){std::cout<<"error";}
	ss.setTexture(st);
	ss.setPosition(x,y);
	frame = 2;
}
void ship::move(sf::RenderWindow &name)
{
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){ yvel-=0.8;}	 
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){ yvel+=0.8;}	
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
	 { xvel-=0.8;
	  --frame;}
	if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
	 { xvel+=0.8;
	  ++frame;}
	else if(!sf::Keyboard::isKeyPressed(sf::Keyboard::Left)&& !sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){frame=2;}

	if(frame>4){frame=4;}
	 else if(frame<0){frame=0;}

	if(frame==0){ss.setTextureRect(sf::IntRect(0,0,22,43));}
	else if(frame==1){std::cout<<"f2";
		ss.setTextureRect(sf::IntRect(42,0,33,43));}
	else if(frame==2){ss.setTextureRect(sf::IntRect(86,0,43,43));}
	else if(frame==3){ss.setTextureRect(sf::IntRect(140,0,33,43));}
	else if(frame==4){ss.setTextureRect(sf::IntRect(194,0,22,43));}
	ss.move(xvel,yvel);
	name.draw(ss);
	std::cout<<"draw";
}


and main cpp

// brickbreaker.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <SFML\graphics.hpp>
#include "ship.h"


int main()
{  sf::RenderWindow app(sf::VideoMode(800,600),"test");
sf::Texture bt;
bt.loadFromFile("spw.jpg");
sf::Sprite background(bt);
ship player(400,300);
while(app.isOpen())
{app.clear();
app.draw(background);
  	sf::Event x;
	while(app.pollEvent(x))
		{if(x.type==sf::Event::Closed){app.close();}}
	player.move(app);
	app.display();
}
	return 0;
}




please help guyz

This post has been edited by B0rn2c0de: 29 April 2013 - 08:31 AM
Reason for edit:: fixed botched code tag


Is This A Good Question/Topic? 0
  • +

Replies To: sfml sprite not drawing

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9491
  • View blog
  • Posts: 35,825
  • Joined: 12-June 08

Re: sfml sprite not drawing

Posted 29 April 2013 - 08:36 AM

Is this the same issue as this thread?

http://www.dreaminco...ite-rectangles/
Was This Post Helpful? 0
  • +
  • -

#3 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: sfml sprite not drawing

Posted 29 April 2013 - 08:51 AM

no that was solved that was because i was allocating the image before rendering the window .. here its not even getting drawn on the screen.. the background is getting drawn but not the ship .. first i thought the draw code is not being reached so i put a cout statement there to check but it was working so i dont know why
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9491
  • View blog
  • Posts: 35,825
  • Joined: 12-June 08

Re: sfml sprite not drawing

Posted 29 April 2013 - 08:58 AM

Are you drawing them in the right order? I mean if the ship is being drawn first and then the background on top of it you will never see the ship.
Was This Post Helpful? 0
  • +
  • -

#5 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: sfml sprite not drawing

Posted 29 April 2013 - 09:03 AM

yes i saw to that first i call app.clear() then app.draw(background) and then player.move(app) which contains the draw function for the ship
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9491
  • View blog
  • Posts: 35,825
  • Joined: 12-June 08

Re: sfml sprite not drawing

Posted 29 April 2013 - 09:06 AM

What happens when you don't draw the background?
Was This Post Helpful? 0
  • +
  • -

#7 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: sfml sprite not drawing

Posted 29 April 2013 - 09:07 AM

black screen but the ship still dosent get drawn
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9491
  • View blog
  • Posts: 35,825
  • Joined: 12-June 08

Re: sfml sprite not drawing

Posted 29 April 2013 - 09:15 AM

... and you are getting the responses from "std::cout<<"draw";" ?
Was This Post Helpful? 0
  • +
  • -

#9 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: sfml sprite not drawing

Posted 29 April 2013 - 09:19 AM

yes for each frame
Was This Post Helpful? 0
  • +
  • -

#10 B0rn2c0de  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 40
  • Joined: 11-April 13

Re: sfml sprite not drawing

Posted 29 April 2013 - 01:08 PM

i got it guyz .. i wasnt initialising xvel and yvel before calling the move funtion :P/> .. that did it

This post has been edited by B0rn2c0de: 29 April 2013 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1