Weird problem with for loop

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 1224 Views - Last Post: 24 October 2011 - 12:28 PM Rate Topic: -----

#1 DarkGlitch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 24-July 11

Weird problem with for loop

Posted 21 October 2011 - 12:45 PM

Hey forum.

I'm improving upon a program I was making that outputs pixel color values to a text file. Long story short it will check the image and record the info as a tile based on input received from the user. Here's the code: Use it freely if you like it after the problem gets solved.

#include "main.h"

Uint32 CheckPixel( SDL_Surface* Surf, int x, int y )
{
	if(SDL_MUSTLOCK( Surf ))
		SDL_LockSurface( Surf );

	int bpp = Surf->format->BytesPerPixel;
	/*here p is the address to the pixel we want to retrieve*/
	Uint8 *p = (Uint8 *)Surf->pixels + y * Surf->pitch + x * bpp;

	Uint32 pixelcolor = 0;

	switch(bpp)
	{
		case(1):
			pixelcolor = *p;
		break;

		case(2):
			pixelcolor = *(Uint16 *)p;
		break;

		case(3):
			if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
				pixelcolor = p[0] << 16 | p[1] << 8 | p[2];
			else
				pixelcolor = p[0] | p[1] << 8 | p[2] << 16;
		break;

		case(4):
			pixelcolor = *(Uint32 *)p;
		break;
	}

	if( SDL_MUSTLOCK( Surf ) )
		SDL_UnlockSurface( Surf );

	return pixelcolor;
}

int main( int argc, char* argv[] )
{
	Done = false;
	if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
	{
		std::cout << "ALERT! SDL Did not Init properly!" << std::endl;		
		SDL_Delay( 1000 );
		return -1;
	}

	Image = SDL_SetVideoMode( 1024, 1024, 32, SDL_HWSURFACE );

	std::cout << "Opening Image" << std::endl;

	std::string file = "./image/Image.jpg";

	if( ( Image = Surface::Load_Image( file ) ) == NULL )
	{
		std::cout << "Problem loading " << file << std::endl;
		SDL_Delay( 2000 );
		return 1;
	}

	Uint32 WHITE = SDL_MapRGB( Image->format, 0xFF, 0xFF, 0xFF );
	Uint32 BLUE = SDL_MapRGB( Image->format, 0xFF, 0, 0 );
	Uint32 GREEN = SDL_MapRGB( Image->format, 0, 0xFF, 0 );
	Uint32 RED = SDL_MapRGB( Image->format, 0xFF, 0, 0 );
	Uint32 BLACK = SDL_MapRGB( Image->format, 0, 0, 0 );

	SDL_SetColorKey( Image, SDL_SRCCOLORKEY, WHITE );
	
	int tileHeight = 0; 
	int tileWidth = 0;

	std::string filename;

	std::cout << "Please enter unsigned tile height (No characters!)" << std::endl;
	std::cin >> tileHeight;

	std::cout << "Please Enter unsigned tile width (No characters!)" << std::endl;
	std::cin >> tileWidth;

	std::cout << "Please Enter desired map file name. (No Spaces or special characters)" << std::endl;
	std::cin >> filename;

	filename = filename + ".txt";

	fout.open( filename.c_str(), std::ios::beg );
	fout << Image->h << " " << Image->w << std::endl;

	int ArrayX = 0;
	int ArrayY = 0;

	while( !Done )
	{
		for( int y = 0; y < Image->h; y += tileHeight )
		{
			for( int x = 0; x < Image->w; x += tileWidth )
			{
				if( CheckPixel( Image, ( x + 1 ), y ) == WHITE )
				{
					fout << "X: " << ArrayX << " Y: " << ArrayY << " 0" << std::endl;
					++ArrayX;
				}

				else if( CheckPixel( Image, ( x + 1 ), y ) == BLACK )
				{
					fout << "X: " << ArrayX << " Y: " << ArrayY << " 1" << std::endl;
					++ArrayX;
				}

				else if( CheckPixel( Image, ( x + 1 ), y ) == GREEN )
				{
					fout << "X: " << ArrayX << " Y: " << ArrayY << " 2" << std::endl;
					++ArrayX;
				}

				else if( CheckPixel( Image, ( x + 1 ), y ) == RED )
				{
					fout << "X: " << ArrayX << " Y: " << ArrayY << " 3" << std::endl;
					++ArrayX;
				}

				else if( CheckPixel( Image, ( x + 1 ), y ) == BLUE )
				{
					fout << "X: " << ArrayX << " Y: " << ArrayY << " 4" << std::endl;
					++ArrayX;
				}
			}

			ArrayX = 0;
			++ArrayY;
		}

		fout.close();
		SDL_FreeSurface( Image );
		Done = true;
	}

	SDL_Quit();
	return 0;
}



The problem is at X:5, Y:6 the next one should be X:6, but it skips this and goes to X:7. I'm not sure why and if anyone cares to look at this I'd appreciate it. It seems like it's a simple problem but I can't figure out what would make it jump numbers. Image is attached.
Attached Image

Is This A Good Question/Topic? 0
  • +

Replies To: Weird problem with for loop

#2 DarkGlitch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 24-July 11

Re: Weird problem with for loop

Posted 21 October 2011 - 12:56 PM

Ok its not just skipping X6 its skipping randomnly. Sometimes it skips X9 Y6.
Was This Post Helpful? 0
  • +
  • -

#3 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 91
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Weird problem with for loop

Posted 21 October 2011 - 02:10 PM

What are you trying to do with the code? I'm gathering it loads an image, and the user can say how wide/tall he wants to get a subimage from it?

I might simplify the double for loops.
for(int y = 0; y < Image->h; y++)
{
  for(int x= 0; x < Image->; x++)
  {
    std::string pixelColor = "";
    Uint32 pixel = CheckPixel(Image,x,y);
    if(pixel == White)
    {
       pixelColor = " 0";
    }
    else if(pixel == BLACK)
    {
       pixelColor = " 1";
    }
    //etc

    fout << "X: " << x << " Y: " << y << pixelColor << std::endl;
  }
}


Was This Post Helpful? 0
  • +
  • -

#4 DarkGlitch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 24-July 11

Re: Weird problem with for loop

Posted 21 October 2011 - 02:58 PM

Sorry I should've been more descriptive.

I'm trying to make a way for me to create a collision array for a tile engine. The program should check the first pixel of each tile. I have the program check each x value first before moving down to the next y value. Each time a loop is done, it should increase x or Y by the tile width or height respectively.

for( int y = 0; y < Image->h; y += tileHeight )
		{
			for( int x = 0; x < Image->w; x += tileWidth )
			{
				if( CheckPixel( Image, ( x + 1 ), y ) == WHITE )
				{
					fout << "X: " << ArrayX << " Y: " << ArrayY << " 0" << std::endl;
					++ArrayX;
				}
                                // etc...



The ArrayX/Y part is where I want the program to put where that tile is in the map Array. This way when I pass the array to my collision detection function it will check that location for a value and determine what to do from there.

One day I can further expand this to make a map editor but I can't have it skipping sections of the image when it isn't supposed to.

Thanks for reading and responding.

Edit: That code you posted above was how it looked before when all it did was check pixels. Now I want it to check the first pixel where the next tile is supposed to appear.

This post has been edited by DarkGlitch: 21 October 2011 - 03:01 PM

Was This Post Helpful? 0
  • +
  • -

#5 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 91
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Weird problem with for loop

Posted 21 October 2011 - 05:19 PM

maybe it's the part where you call checkPixel? you have your x+1, so I believe for each tile you're checking the second pixel to the right instead of the 0,0 of your tile. Also, maybe you're getting a color that isn't one you've defined, in which case you could have a default else statement to end your checkpixel tests. I see that if none of your if/else if statements fire ArrayX doesn't get incremented anyway.
Was This Post Helpful? 0
  • +
  • -

#6 DarkGlitch  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 24-July 11

Re: Weird problem with for loop

Posted 21 October 2011 - 07:28 PM

No no no the color isn't the problem, it's the fact that it is skipping a column. Let me show you an output of the program.

700 700
X: 0 Y: 0 0
X: 1 Y: 0 0
X: 2 Y: 0 0
X: 3 Y: 0 0
X: 4 Y: 0 0
X: 5 Y: 0 0
X: 6 Y: 0 0
X: 7 Y: 0 0
X: 8 Y: 0 0
X: 9 Y: 0 0
X: 0 Y: 1 1
X: 1 Y: 1 0
X: 2 Y: 1 0
X: 3 Y: 1 0
X: 4 Y: 1 0
X: 5 Y: 1 0
X: 6 Y: 1 0
X: 7 Y: 1 0
X: 8 Y: 1 0
X: 9 Y: 1 0
X: 0 Y: 2 1
X: 1 Y: 2 1
X: 2 Y: 2 0
X: 3 Y: 2 0
X: 4 Y: 2 0
X: 5 Y: 2 0
X: 6 Y: 2 0
X: 7 Y: 2 0
X: 8 Y: 2 0
X: 9 Y: 2 0
X: 0 Y: 3 1
X: 1 Y: 3 1
X: 2 Y: 3 1
X: 3 Y: 3 1
X: 4 Y: 3 0
X: 5 Y: 3 0
X: 6 Y: 3 0
X: 7 Y: 3 0
X: 8 Y: 3 0
X: 9 Y: 3 0
X: 0 Y: 4 0
X: 1 Y: 4 0
X: 2 Y: 4 0
X: 3 Y: 4 0
X: 4 Y: 4 1
X: 5 Y: 4 0
X: 6 Y: 4 0
X: 7 Y: 4 0
X: 8 Y: 4 0
X: 9 Y: 4 0
X: 0 Y: 5 0
X: 1 Y: 5 0
X: 2 Y: 5 0
X: 3 Y: 5 0
X: 4 Y: 5 0
X: 5 Y: 5 1
X: 6 Y: 5 0
X: 7 Y: 5 0
X: 8 Y: 5 0
X: 9 Y: 5 0
X: 0 Y: 6 0
X: 1 Y: 6 0
X: 2 Y: 6 0
X: 3 Y: 6 0
X: 4 Y: 6 0
X: 5 Y: 6 0
X: 6 Y: 6 1
X: 7 Y: 6 0
X: 8 Y: 6 0
X: 0 Y: 7 0
X: 1 Y: 7 0
X: 2 Y: 7 0
X: 3 Y: 7 0
X: 4 Y: 7 0
X: 5 Y: 7 0
X: 6 Y: 7 1
X: 7 Y: 7 0
X: 8 Y: 7 0
X: 9 Y: 7 0
X: 0 Y: 8 0
X: 1 Y: 8 0
X: 2 Y: 8 0
X: 3 Y: 8 0
X: 4 Y: 8 0
X: 5 Y: 8 0
X: 6 Y: 8 0
X: 7 Y: 8 1
X: 8 Y: 8 0
X: 9 Y: 8 0
X: 0 Y: 9 0
X: 1 Y: 9 0
X: 2 Y: 9 0
X: 3 Y: 9 0
X: 4 Y: 9 0
X: 5 Y: 9 0
X: 6 Y: 9 0
X: 7 Y: 9 0
X: 8 Y: 9 1
X: 9 Y: 9 0

Notice anything strange where the text turns red? X: 9 is completely skipped and there is no reason why! I'm quite baffled.
I even took out the x + 1 part since I realized it is unnecessary.

This post has been edited by DarkGlitch: 21 October 2011 - 07:33 PM

Was This Post Helpful? 0
  • +
  • -

#7 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 91
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Weird problem with for loop

Posted 22 October 2011 - 05:38 AM

I would put an else statement at the end of your if/else block and do fout << "Error" or something like that. It would make sense that if it doesn't find the pixel, ArrayX wouldn't increase, but then you'd be at the end of the row, and ArrayX would get reset to 0. The problem might even be with an earlier pixel, but your program doesn't handle problem pixels/increase ArrayX if it doesn't find the preset pixel.

Like I said earlier, tack an else statement, and increment ArrayX regardless of pixel found.
for( int y = 0; y < Image->h; y += tileHeight )
{
  for( int x = 0; x < Image->w; x += tileWidth )
  {
    if( CheckPixel( Image, ( x + 1 ), y ) == WHITE )
    {
      fout << "X: " << ArrayX << " Y: " << ArrayY << " 0" << std::endl;
    }

    else if( CheckPixel( Image, ( x + 1 ), y ) == BLACK )
    {
      fout << "X: " << ArrayX << " Y: " << ArrayY << " 1" << std::endl;
    }

    else if( CheckPixel( Image, ( x + 1 ), y ) == GREEN )
    {
      fout << "X: " << ArrayX << " Y: " << ArrayY << " 2" << std::endl;
    }

    else if( CheckPixel( Image, ( x + 1 ), y ) == RED )
    {
      fout << "X: " << ArrayX << " Y: " << ArrayY << " 3" << std::endl;
    }
    else if( CheckPixel( Image, ( x + 1 ), y ) == BLUE )
    {
      fout << "X: " << ArrayX << " Y: " << ArrayY << " 4" << std::endl;
    }
    //Just in case :P/>
    else
    {
      fout << "Error: Pixel Color not Found." << std::endl;
    }
    //no matter what, The tile has changed, must change the Array
    ++ArrayX;
    
  }
ArrayX = 0;
++ArrayY;
}



Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Weird problem with for loop

Posted 22 October 2011 - 07:21 AM

you really should be stepping through your code with the debugger. If you don't know how to debug you will not be able to complete many games.
Was This Post Helpful? 0
  • +
  • -

#9 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 91
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Weird problem with for loop

Posted 22 October 2011 - 07:48 AM

ButchDean is correct.I haven't gone so far as to use step by step debugging using the IDE debugger. But what I do is have System.out.println(I use Java, for c++ the equivalent would cout) statements inside code I'm working on, code that gets data returned from functions I've written, as well as System.err.println(cerr? for c++) connected to a file for try/catch blocks that output the stacktrace of any errors that pop up.

The most important thing,though, is knowing what functionality your code needs, what way to implement it properly/knowing what it will do and why, and any issues that could arise for anything thrown at it. You can spend all day at the debugger if you write code you don't understand, or know your code enough that a debugger is unnecessary.
Was This Post Helpful? 0
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Weird problem with for loop

Posted 22 October 2011 - 07:56 AM

View PostGreltam, on 22 October 2011 - 03:48 PM, said:

... or know your code enough that a debugger is unnecessary.

A debugger is never unnecessary! It is there to point out flaws in your logic that you simply miss. :)
Was This Post Helpful? 0
  • +
  • -

#11 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 91
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Weird problem with for loop

Posted 22 October 2011 - 08:43 AM

I meant that there is a scale for needing a debugger, which is between “Oh god how did this get in here I am not good with computers” copy paster programmer that doesn't know what logic is, and some super genius that writes flawless code to begin with.

As most of us fall between that scale, their will be differing needs for the debugger, and sometimes if you know your code good and an error happens in your program, you will know which line/block of code is producing the error and what needs to be fixed.
Was This Post Helpful? 0
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Weird problem with for loop

Posted 22 October 2011 - 09:59 AM

There is no excuse for not needing the debugger. To be honest you cannot completely understand your code without the use of a debugger - and there is no such thing at all as a super genius coder who writes perfect code! No way!
Was This Post Helpful? 0
  • +
  • -

#13 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 91
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Weird problem with for loop

Posted 22 October 2011 - 10:42 AM

So what you're saying is that a human has no capability of reason/logic/intelligence, and that we ALWAYS need a debugger to tell us what is wrong? Maybe we also need a application that writes our programs, and then codes them for us also. :P
Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Weird problem with for loop

Posted 22 October 2011 - 10:50 AM

No, you're taking this where it doesn't need to go. I'm saying that you cannot judge precisely how complicated a program needs to be before you need the debugger as an aid to understanding what is going on whether code is working or not. When you write a program there is a lot going on that only a debugger has real-time access to. Also, if you are a beginning programmer where you are only able to write very simple programs then a debugger is just as important to you to really see what is going on and move forward to more complicated code later with that more thorough understanding.

Any advice that says that a debugger is not necessary when programming is bad advice - you are encouraging the practice of writing unreliable code that will come back to bite you later, either in the same project or later in another project when the code bombs and you don't know how to debug.
Was This Post Helpful? 1
  • +
  • -

#15 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 91
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Weird problem with for loop

Posted 22 October 2011 - 11:38 AM

I'm not saying
if(Program.complication() < complicated)
  Program.writing().doNotUseDebugger();


If there is no need to use the debugger, then why spend the time?
Example:
for(int i = 0; i < 5; i++)
{
  System.out.println(i);
}


Why didn't it write 5? Oh yes, the conditional needs an = sign.
for(int i = 0; i <= 5; i++)
{
  System.out.println(i);
}


or maybe when you write to a file, not all of it gets written. Logically, if you're using a BufferedWriter, you probably forgot to flush it.

The more reliant on the debugger you are to find where you made mistakes, the less skilled you are. Beginners should use the debugger, because they are unskilled, but as time goes by you should need it less and less.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2