www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [Dgame] Sprite loading and setting position in another class

reply "Jack" <Jackoz530 gmail.com> writes:
So I've been using Dgame to learn about game development and I'm 
having an error when I'm trying to set a sprite's position 
initialized and stored in another class.

Basically my code is:
//Texman.d//////////////////////////////////////

Class Texman
{
   //variables

	void addSprite(string sprite_file, string name)
	{
		Surface wiki_img = Surface(sprite_file);
		Texture wiki_tex = Texture(wiki_img);
		sprite_list[name] = new Sprite(wiki_tex);
                 sprite_list[name].setPosition(1,1); //tried to 
remedy by this		
	}
}

//GameObject.d////////////////////////////

class GameObject
{
Sprite sprite;
float location_x;
float location_y;
//other variables

	void setLocation(float x, float y)
	{
		sprite.setPosition(x,y);
		location_x = x;
		location_y = y;
	}
}

//main.d/////////////////////////////////


	void create()
	{
		wnd = Window(1024, 720, "Dgame Test");
		sample_object = new GameObject();
		texman = new TextureManager();
		texman.addSprite("images.png", "kirino");
		sample_object.setSprite(texman.getSprite("kirino"));
		sample_object.setLocation(500,500);
	}
//////////////////////////////////////////////////////

And I'm having an error namely
"First-chance exception: core.exception.AssertError null"
pointing at the setLocation on GameObject.d.

I tried to remove the "setLocation" method and tried to set 
Location on TextureManager itself and it worked.

So is setting the position of a sprite outside of the class it 
initialized into forbidden, or am I doing something wrong here?
Aug 25 2015
parent reply "Jack" <Jackoz530 gmail.com> writes:
On Tuesday, 25 August 2015 at 12:50:50 UTC, Jack wrote:
 So I've been using Dgame to learn about game development and 
 I'm having an error when I'm trying to set a sprite's position 
 initialized and stored in another class.

 Basically my code is:
 //Texman.d//////////////////////////////////////

 Class Texman
 {
   //variables

 	void addSprite(string sprite_file, string name)
 	{
 		Surface wiki_img = Surface(sprite_file);
 		Texture wiki_tex = Texture(wiki_img);
 		sprite_list[name] = new Sprite(wiki_tex);
                 sprite_list[name].setPosition(1,1); //tried to 
 remedy by this		
 	}
 }

 //GameObject.d////////////////////////////

 class GameObject
 {
 Sprite sprite;
 float location_x;
 float location_y;
 //other variables

 	void setLocation(float x, float y)
 	{
 		sprite.setPosition(x,y);
 		location_x = x;
 		location_y = y;
 	}
 }

 //main.d/////////////////////////////////


 	void create()
 	{
 		wnd = Window(1024, 720, "Dgame Test");
 		sample_object = new GameObject();
 		texman = new TextureManager();
 		texman.addSprite("images.png", "kirino");
 		sample_object.setSprite(texman.getSprite("kirino"));
 		sample_object.setLocation(500,500);
 	}
 //////////////////////////////////////////////////////

 And I'm having an error namely
 "First-chance exception: core.exception.AssertError null"
 pointing at the setLocation on GameObject.d.

 I tried to remove the "setLocation" method and tried to set 
 Location on TextureManager itself and it worked.

 So is setting the position of a sprite outside of the class it 
 initialized into forbidden, or am I doing something wrong here?
Edit: Basically my code is: //Texman.d////////////////////////////////////// Class TextureManager { //variables void addSprite(string sprite_file, string name) { Surface wiki_img = Surface(sprite_file); Texture wiki_tex = Texture(wiki_img); sprite_list[name] = new Sprite(wiki_tex); sprite_list[name].setPosition(1,1); //tried to remedy by this } }
Aug 25 2015
parent reply "Namespace" <rswhite4 gmail.com> writes:
 Edit:

  Basically my code is:
  //Texman.d//////////////////////////////////////

  Class TextureManager
  {
   //variables

 	void addSprite(string sprite_file, string name)
 	{ 		
                 Surface wiki_img = Surface(sprite_file);
  		Texture wiki_tex = Texture(wiki_img);
 		sprite_list[name] = new Sprite(wiki_tex);
                 sprite_list[name].setPosition(1,1); //tried to
  remedy by this		
 	}
  }
You have to store your Texture. See "My Sprite is only a white Rectangle" on http://dgame-dev.de/index.php?controller=faq I'll change that with v0.7, so that Sprite will manage the Texture by himself.
Aug 25 2015
next sibling parent "Namespace" <rswhite4 gmail.com> writes:
Note that Texture is (in constrast to Sprite) a struct and not a 
class, so it is a value type. Dgame tries to use as many value 
types as possible to reduce the amount of garbage.
Aug 25 2015
prev sibling parent reply "Jack" <Jackoz530 gmail.com> writes:
On Tuesday, 25 August 2015 at 13:22:43 UTC, Namespace wrote:
 Edit:

  Basically my code is:
  //Texman.d//////////////////////////////////////

  Class TextureManager
  {
   //variables

 	void addSprite(string sprite_file, string name)
 	{ 		
                 Surface wiki_img = Surface(sprite_file);
  		Texture wiki_tex = Texture(wiki_img);
 		sprite_list[name] = new Sprite(wiki_tex);
                 sprite_list[name].setPosition(1,1); //tried to
  remedy by this		
 	}
  }
You have to store your Texture. See "My Sprite is only a white Rectangle" on http://dgame-dev.de/index.php?controller=faq I'll change that with v0.7, so that Sprite will manage the Texture by himself.
Thank you for answering so quickly. If you don't mind me asking when will v0.7 be out?
Aug 25 2015
parent "Namespace" <rswhite4 gmail.com> writes:
 Thank you for answering so quickly. If you don't mind me asking 
 when will v0.7 be out?
Not so soon. But maybe I'll release v0.6.5 with this feature at the end of september. For now you need to store your Texture in e.g. a Texture manager.
Aug 25 2015