Page 1 of 2

Surface Class Loading a BMP

Posted: October 7th, 2016, 5:57 pm
by egizz983
Hello again need your help to solve a problem , so i am following a chili intermediate tutorials lesson 8 and doing a surface class , not as chili , i do it my way , and face a problem , could not load an image , once i run game its just crash and when i break it its returns me into my BMP.ccp file i have done no major changes there just pass two more parameters to a functions "unsinged int& width, unsinged int& height" and then inside a function just assign a values that was read from a file , basically i am using my whole old code from my last game (load bitmap , draw sprite and so on ) all those functions are same as it was on my last game and everything was working so i assume that there is a problem with my surface class , the framework are empty only a surface class and some functions inside graphic.h

Re: Surface Class Loading a BMP

Posted: October 7th, 2016, 6:49 pm
by freebattie
so i ran it in debug mode and it crashes when making the surface inside the bmpload function, i dont know how vectors work and bit unsure about pointers but i am gessing your problem is there, maybe the vector pointer need to be set to something?

EDIT if that dosent help you, you need to wait for the big boys :p

Re: Surface Class Loading a BMP

Posted: October 7th, 2016, 6:57 pm
by egizz983
freebattie wrote:so i ran it in debug mode and it crashes when making the surface inside the bmpload function, i dont know how vectors work and bit unsure about pointers but i am gessing your problem is there, maybe the vector pointer need to be set to something?

EDIT if that dosent help you, you need to wait for the big boys :p
Yea its works if i make sprite without a pointer and pass like this :

Code: Select all

class Surface {
public:
	Surface(std::string filename) {
		LoadBMP(filename, sprite,width,height);
	}
	~Surface() {
		sprite.clear();
	}
	void Draw(int xoff ,int yoff , int tr,int tg,int tb,Graphics &gfx) {
		gfx.DrawSprite(xoff, yoff, width, height, sprite, tr, tg, tb);
	}
private:
	unsigned int width;
	unsigned int height;
	std::vector<Color> sprite;
};
i am not sure if its bad to do like this or not , but its works , with one issue , i am loading a star as you could see if you use a class like above without pointers . So i load a start.bmp witch is a yellow star and pass 0,0,0 as transparent color witch is black and as mine drawsprite function checks a pixel and if its not black then draw it you can check for definition of drawsprite . but the thing is its not draw me yellow color for some reason if you use class as i posted above you would see what i mean

Re: Surface Class Loading a BMP

Posted: October 7th, 2016, 7:08 pm
by freebattie
yeah thats a problem whit your drawfunction, yellow is 255,255,0 so you hit 1 of the checks for != 0

Re: Surface Class Loading a BMP

Posted: October 7th, 2016, 7:10 pm
by egizz983
freebattie wrote:yeah thats a problem whit your drawfunction, yellow is 255,255,0 so you hit 1 of the checks for != 0
i dont really see a problem in a draw function :

Code: Select all

void Graphics::DrawSprite(int x, int y, int width, int heigth, std::vector<Color>& image, int tr, int tg, int tb)
{
	for (int a = 0; a < heigth; a++) {
		for (int b = 0; b < width; b++) {
			unsigned char rd = image[b + a * width].GetR();
			unsigned char gr = image[b + a * width].GetG();
			unsigned char bl = image[b + a * width].GetB();
			if (rd != (unsigned char)tr &&
				gr != (unsigned char)tg &&
				bl != (unsigned char)tb) {
				PutPixel(x + b, y + a,rd,gr,bl);
			}
		}
	}
}
if transparent color is 0,0,0, witch is black then by this code i would not draw black and will draw everyother colors

Re: Surface Class Loading a BMP

Posted: October 7th, 2016, 7:21 pm
by freebattie
you are saying that rd AND gr AND bl needs to NOT be 0, but when you have yellow, it is 255,255,0 so it will not draw yellow since bl will be 0 and your check will be untrue and skip, need to rethink how you check for 0,

Re: Surface Class Loading a BMP

Posted: October 7th, 2016, 7:26 pm
by egizz983
that if tells if rd != tr(0) and gr != tg(0) and bl != tb(0) then draw
so if its yellow if would look like this
if 255 != 0 and 255 != 0 && 0 != 0
witch is false

edited:
ah okay i got this now, but still now sure about the problem i mentioned before about those pointers should it be a pointer or its good without :/

Re: Surface Class Loading a BMP

Posted: October 7th, 2016, 7:41 pm
by freebattie
ok so it needs to be !( bla == 0 && bla1 == 0 && bla2 ==0)
the way you did it you are saying if rd or gr or bl is 0, skip it, what i posted will check if all 3 is 0 and only skip if that is true

EDIT hope that makes it bit clearer how it works, if you still have some problem understanding it

Re: Surface Class Loading a BMP

Posted: October 7th, 2016, 7:49 pm
by egizz983
yea i did it same way :D was not getting this at first :D

Code: Select all

if (!(rd == tr && gr == tg && bl == tb)) {
				PutPixel(x + b, y + a,rd,gr,bl);
			}

Re: Surface Class Loading a BMP

Posted: October 7th, 2016, 11:50 pm
by LuisR14
cool that you guys solved w/o me having to jump in (was here for a while xD)
just to help you guys in the future

Code: Select all

if( a && b ) // all have to eval to true to pass (you know lol)

so for the yellow check
if( rd(255) != tr(0) && gr(255) != tg(255) && bl(0) != tb(0) ) // if( true && true && false ) == false

so yea, checking that all be true and then negate would be the way to go
if( not ( rd(255) == tr(0) && gr(255) == tg(0) && bl(0) == tb(0) ) )
// if( not( false && false && true ) ) -> if( not( false ) ) == true
so yea