Page 1 of 2

Vector arrays? ( luigi is a square lolz )

Posted: Thu Nov 11, 2010 2:50 pm
by N64vSNES
So been working on the basic backbone of the NPC system for the eternal quest.

I'm using a vector array since static would be plain stupid but when I staticly add a NPC from a file everything works

-Coordinance gets set
-Spritesheet successfully loads
etc...

Everything is correctly loaded but luigi seems to be a square.......

Image

I'm probably overlooking a very simple problem but I'm quite stuck :|

Here is my NpcManager.h

Code: Select all

#ifndef NPC_MANAGER_H
#define NPC_MANAGER_H

#include "NPCs.h"

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

namespace Eternal {

	class NpcManager {

	private:

			NpcManager();
			static NpcManager *MyManager;

		public:

			static NpcManager *GetManager();
			~NpcManager();

			int AddNpc(float x, float y, std::string file);
			void RemoveNpc(int npc);

			void UpdateManager();

		private:

			int Top;
			std::vector <Eternal::NPC> Npcs;

	};

}

#endif
And my .cpp

Code: Select all

#include "NPCManager.h"

#include "Primitives.h"
#include "DebugLog.h"
#include "Camera.h"

using namespace Eternal;

NpcManager *NpcManager::MyManager = 0;

NpcManager *NpcManager::GetManager() {
	if ( MyManager == 0 ) {
		MyManager = new NpcManager;
	}
	return MyManager;
}

NpcManager::NpcManager() {
	Top = 0;
}

NpcManager::~NpcManager() {

}

int NpcManager::AddNpc(float x, float y, std::string file) {

	std::ifstream ifile;

	NPC npc;

	ifile.open( file.c_str() );

		std::string texture;
		ifile >> texture;
		ifile >> npc.Name;
		ifile >> npc.SpriteSheet.Crop.w;
		ifile >> npc.SpriteSheet.Crop.h;

	ifile.close();

	npc.SpriteSheet.Load(texture.c_str()); // .......
	npc.SpriteSheet.Pos.x = x;
	npc.SpriteSheet.Pos.y = y;	
	npc.SpriteSheet.Pos.w = npc.SpriteSheet.Crop.h;
	npc.SpriteSheet.Pos.w = npc.SpriteSheet.Crop.h;
	npc.SpriteSheet.SetColor(255,255,255); // openGL ( original texture colors )

	Npcs.push_back(npc);

	Top++;

	return Top - 1;
}

void NpcManager::RemoveNpc(int npc) {
	Npcs.erase(Npcs.begin() + npc);	
Top--;
}

void NpcManager::UpdateManager() {
	Camera *CameraSys = Camera::GetInstance();
	for ( int i = 0;i < Top;i++ ) {
		CameraSys->RenderSprite( Npcs[i].SpriteSheet );
	}
}
Sorry for the lack of comments.

If this helps any here is the NPCs file content I allocated it from

Code: Select all

Data/mario.png --SpriteSheet
Luigi --Name
32 -- Width
64 -- Height
1 -- Nothing past this point is getting loaded so I won't go into details
1
1
Data/Scripts/TestScript1.lua
~c999Hello, I've been staticly allocated from a file.
I bet this is a really simple fix but anyone?

Re: Vector arrays? ( luigi is a square lolz )

Posted: Thu Nov 11, 2010 3:33 pm
by k1net1k
i dont know about the code, but i assume its nothing to do with the actual spritesheet ? eg luigi doesnt exist on it ?

Re: Vector arrays? ( luigi is a square lolz )

Posted: Thu Nov 11, 2010 11:37 pm
by gm3dgames
im wondering if this:

Code: Select all

   
npc.SpriteSheet.Pos.w = npc.SpriteSheet.Crop.h;
npc.SpriteSheet.Pos.w = npc.SpriteSheet.Crop.h;
(located in you .cpp) has anything to do with it, unless this is intentional, the crop is going to set the width to the height of the crop.
and its there twice without the setup of the height.

should it be this?

Code: Select all

   
npc.SpriteSheet.Pos.w = npc.SpriteSheet.Crop.w;
npc.SpriteSheet.Pos.h = npc.SpriteSheet.Crop.h;

Re: Vector arrays? ( luigi is a square lolz )

Posted: Fri Nov 12, 2010 4:59 am
by N64vSNES
k1net1k wrote:i dont know about the code, but i assume its nothing to do with the actual spritesheet ? eg luigi doesnt exist on it ?
Nope debug log confirms its been loaded correctly and when I don't set the Pos.w and Pos.h the rectangle is default 128x128 which is the same dimensions are luigi's spritesheet so its loading correctly :(

gm3dgames wrote:im wondering if this:

Code: Select all

   
npc.SpriteSheet.Pos.w = npc.SpriteSheet.Crop.h;
npc.SpriteSheet.Pos.w = npc.SpriteSheet.Crop.h;
(located in you .cpp) has anything to do with it, unless this is intentional, the crop is going to set the width to the height of the crop.
and its there twice without the setup of the height.

should it be this?

Code: Select all

   
npc.SpriteSheet.Pos.w = npc.SpriteSheet.Crop.w;
npc.SpriteSheet.Pos.h = npc.SpriteSheet.Crop.h;
Yeah my bad, I corrected this mistake just after I posted this, but this dosen't solve the problem :(

Re: Vector arrays? ( luigi is a square lolz )

Posted: Fri Nov 12, 2010 6:02 am
by Sour-Lemon
I'm not sure that I can help you, but I was wondering, what is the SetColor(); function doing?

It's just that, Luigi is appearing as a white block, and the last thing you are doing before you call Npcs.push_back() in NpcManager::AddNpc() is:
npc.SpriteSheet.SetColor(255,255,255);

Is it just setting a colour to use for transparency?

Also is Mario being loaded using the same functions?
If he is being loaded seperately for whatever reason are there any differences in the code that is doing it?

Re: Vector arrays? ( luigi is a square lolz )

Posted: Fri Nov 12, 2010 6:54 am
by N64vSNES
Sour-Lemon wrote:I'm not sure that I can help you, but I was wondering, what is the SetColor(); function doing?

It's just that, Luigi is appearing as a white block, and the last thing you are doing before you call Npcs.push_back() in NpcManager::AddNpc() is:
npc.SpriteSheet.SetColor(255,255,255);

Is it just setting a colour to use for transparency?

Also is Mario being loaded using the same functions?
If he is being loaded seperately for whatever reason are there any differences in the code that is doing it?
Set color isn't really nessacary as the Image() constructor sets that by default but I thought this may fix this but no

the 255,255,255 is because I'm using openGL and when I call render it calls glColor3f( r / 255, g / 255, b / 255 ); if you don't know much about openGL this basicly just leaves the colors as they should be.

Thanks.

Re: Vector arrays? ( luigi is a square lolz )

Posted: Fri Nov 12, 2010 7:30 am
by Bakkon
Check to make sure the texture you're binding for Luigi is created properly. That looks pretty similar to when you try to draw with an invalid texture.

Re: Vector arrays? ( luigi is a square lolz )

Posted: Fri Nov 12, 2010 10:11 am
by N64vSNES
Bakkon wrote:Check to make sure the texture you're binding for Luigi is created properly. That looks pretty similar to when you try to draw with an invalid texture.
I've checked through the rendering and I can defnitly say that the is no issue with the texture as I've tried switching textures with the player and thats working fine.

EDIT:

Also I should point out for rendering the is two methods

1 ) Image class
2 ) SpriteSheet class

But this is irrelevant since tiles, party members and npc's are all using the Image class

EDIT:
Still open for suggestions? :(

Re: Vector arrays? ( luigi is a square lolz )

Posted: Mon Nov 15, 2010 7:01 am
by N64vSNES
Okay dudes I found the problem ( Thanks to WSP_SNIPER )

I think because its actully a dynamic array you have to pass a pointer when you push a element onto the array.

Thanks.

Re: Vector arrays? ( luigi is a square lolz )

Posted: Tue Nov 23, 2010 4:56 pm
by Falco Girgis
N64vSNES wrote:Okay dudes I found the problem ( Thanks to WSP_SNIPER )

I think because its actully a dynamic array you have to pass a pointer when you push a element onto the array.

Thanks.
Nope. That's not true. If that were the case it wouldn't even have compiled at all to begin with when you tried to push an NPC back into an NPC * vector.

So did you change

Code: Select all

         std::vector <Eternal::NPC> Npcs;
To

Code: Select all

std::vector <Eternal::NPC *> Npcs
and begin pushing back pointers to dynamically allocated NPCs?

If that's the case (and now works), your copy constructor must be fucked.

Re: Vector arrays? ( luigi is a square lolz )

Posted: Tue Nov 23, 2010 5:43 pm
by dandymcgee
GyroVorbis wrote:If that's the case (and now works), your copy constructor must be fucked.
And assignment operator. :|

Re: Vector arrays? ( luigi is a square lolz )

Posted: Tue Nov 23, 2010 8:51 pm
by Falco Girgis
Not to sound like a prick either, but why do you compare your pointers to 0 rather than NULL? I think that's annoying as hell and makes it unclear that the comparison is between a pointer and a memory address rather than an integer variable and a constant... :)

Re: Vector arrays? ( luigi is a square lolz )

Posted: Wed Nov 24, 2010 12:04 am
by Khearts
I thought NULL == 0...?

Re: Vector arrays? ( luigi is a square lolz )

Posted: Wed Nov 24, 2010 12:18 am
by Falco Girgis
Khearts wrote:I thought NULL == 0...?
It does. That's not the point, though.

If comparing a pointer to 0 were a good practice, why the hell would the keyword/macro NULL even exist? It's not a good practice.

Re: Vector arrays? ( luigi is a square lolz )

Posted: Wed Nov 24, 2010 4:00 am
by N64vSNES
GyroVorbis wrote:
Khearts wrote:I thought NULL == 0...?
It does. That's not the point, though.

If comparing a pointer to 0 were a good practice, why the hell would the keyword/macro NULL even exist? It's not a good practice.
I didn't think that assigning pointers to 0 was that big of a deal since the NULL marco is defined as 0 to begin with.
I suppose its better for readability so I'll start using NULL.


And yes I changed it from
std::Vector <Eternal::Npc> Npcs;
to
std::Vector <Eternal::Npc> Npcs;

Then dynamicly allocate a NPC and pass that onto the array and deallocate it when the NPC gets removed, this wasn't the issue?