C++ Player out of tile map

Recommended Posts

Hi! I made a tile map reading from a file. Almost everything works good, but when a player go out of map, program runs into an error and says that "vector subscript out of range". My question is how to make check for it

Drawing map : 

void GameplayScreen::DrawMap(SDL_Renderer *renderer)
{
	for (int y = map.size() - 1; y >= 0; --y)
	{
		for (int x = getStartBlockX(), xEnd = getEndBlockX(); x < xEnd && x < map[y].size(); ++x)
		{
			if (map[y][x] != "0,0")
			{
				int tempX = atoi(map[y][x].substr(0, map[y][x].find(',')).c_str());
				int tempY = atoi(map[y][x].substr(map[y][x].find(',') + 1).c_str());

				srcRect.x = tempX * 32;
				srcRect.y = tempY * 32;
				srcRect.w = 32;
				srcRect.h = 32;

				destRect.x = x * 32 + posX;
				destRect.y = (y * 32 + posY);
				destRect.w = 32;
				destRect.h = 32;
              
				vBlock[Earth]->Draw(renderer, srcRect, destRect);
            }
		}
	}
}

getStartBlockX returns first map block, getEndBlockX returns last, so it's like render on screen only a piece of map, not all blocks.
tempX returns x coordinate of tile image,  tempY y coordinate. So, for example, if map is like :

0,0 0,0 0,0 0,0 0,0

0,0 0,0 0,0 0,0 0,0

1,0 2,0 0,3 1,0 1,0

0,0 is first block image, 1,0 is one next to the first, 0,3 is 2 under the first block etc.

 

 

Edited by DKdowneR

Share this post


Link to post
Share on other sites
1 hour ago, DKdowneR said:

Hi! I made a tile map reading from a file. Almost everything works good, but when a player go out of map, program runs into an error and says that "vector subscript out of range". My question is how to make check for it

Drawing map : 


void GameplayScreen::DrawMap(SDL_Renderer *renderer)
{
	for (int y = map.size() - 1; y >= 0; --y)
	{
		for (int x = getStartBlockX(), xEnd = getEndBlockX(); x < xEnd && x < map[y].size(); ++x)
		{
			if (map[y][x] != "0,0")
			{
				int tempX = atoi(map[y][x].substr(0, map[y][x].find(',')).c_str());
				int tempY = atoi(map[y][x].substr(map[y][x].find(',') + 1).c_str());

				srcRect.x = tempX * 32;
				srcRect.y = tempY * 32;
				srcRect.w = 32;
				srcRect.h = 32;

				destRect.x = x * 32 + posX;
				destRect.y = (y * 32 + posY);
				destRect.w = 32;
				destRect.h = 32;
              
				vBlock[Earth]->Draw(renderer, srcRect, destRect);
            }
		}
	}
}

getStartBlockX returns first map block, getEndBlockX returns last, so it's like render on screen only a piece of map, not all blocks.
tempX returns x coordinate of tile image,  tempY y coordinate. So, for example, if map is like :

0,0 0,0 0,0 0,0 0,0

0,0 0,0 0,0 0,0 0,0

1,0 2,0 0,3 1,0 1,0

0,0 is first block image, 1,0 is one next to the first, 0,3 is 2 under the first block etc.



 

 

Well you dont check x to being negative. Also when x and and xEnd are equal, you are standing on the same x-line - but from your condition you exit the loop, is this indented? Shouldnt this be x <= xEnd ? Or does getEndBlockX returns the range instead of the last index? From the name of the function i would expect, the last index.

 

Also why you dont have a getStartBlockY() and getEndBlockY() or are your player limited to X-direction only ?

 

Edited by Finalspace

Share this post


Link to post
Share on other sites

I'm planning to do with Y direction. It doesnt matter that it's x < xEnd or x <= xEnd, because I add 1 right in the method. They looks like this: 

int GameplayScreen::getStartBlockX()
{
	return (int)(-posX + (int)posX % 32) / 32;
}
int GameplayScreen::getEndBlockX()
{
	return (int)(-posX + (int)posX % 32 + System::getFM()->getW()) / 32 + 1;
}

Also I don't think that I should check for x is negative, because it's always positive
 

 

getW() returns window width.

Edited by DKdowneR

Share this post


Link to post
Share on other sites

I don't see an error with the for loops, maybe I'm overlooking it. Are you sure the error isn't somehow in this call:

1 hour ago, Finalspace said:

vBlock[Earth]->Draw(renderer, srcRect, destRect);

What is the value of Earth?

Share this post


Link to post
Share on other sites

You can just use the keyword continue or break from that loop

1 hour ago, DKdowneR said:

int GameplayScreen::getEndBlockX() { return (int)(-posX + (int)posX % 32 + System::getFM()->getW()) / 32 + 1; }

Also, I don't think this is doing what you think it is. I think you need more parenthesis to control the order of operations.

Share this post


Link to post
Share on other sites

What you think it is? It returns last block, so if window is 1280 width,  it returns 40, that's only what I need it to do and in draw for loop, loop through 0 to 40. So, it renders 40 block instead of ~90. If start block = 5, end = 45, etc. so, always depends on window it returns everything to render only that what need to be on the screen, nothing more, nuting less.

Edited by DKdowneR

Share this post


Link to post
Share on other sites

So, is posX screen coordinates for a tile, or the start block or what?

(int)(-posX + (int)posX % 32 + System::getFM()->getW()) / 32 + 1

if start block = 5

(int)(-5 + (int)5 % 32 + 1280) / 32 + 1

(int)(-5 + 5 + 1280) / 32 + 1

(int)(1280) / 32 + 1

(int)40 + 1 =

41 not 45

 

Edited by Yxjmir
Math error

Share this post


Link to post
Share on other sites
6 hours ago, DKdowneR said:

Also I don't think that I should check for x is negative, because it's always positive

What values can posX have?

If posX can be greater than 32, getStartBlockX will return a negative number.

return (int)(-posX + (int)posX % 32) / 32;

//Assuming posX = 33:
//Ignoring the int-casts to show it simpler.
return (-33 + 33 % 32) / 32;
return (-33 + 1) / 32;
return -32 / 32;
return -1;

 

Share this post


Link to post
Share on other sites

posX is a value that decrease if position X of Player is higher than or equal (1280/2) - playerWidth and if Player moves right. if moves left and position X of Player is lower than or equal (1280/2) - playerWidth, posX increase.

The player and map moving formula are :
 

//moving right
if (posX >= (System::getFM()->getW() / 2) - getHitBoxX() && System::getSM()->GetGame()->getMoveMap())// && System::getSM()->GetGame()->getPosX() >= -1760)
{
	System::getSM()->GetGame()->MoveMap(-moveSpeed, 0);
}
	// player posX
else posX += moveSpeed;

moveAnim = true;


// moving left
if (posX <= (System::getFM()->getW() / 2) - getHitBoxX() && System::getSM()->GetGame()->getMoveMap() && System::getSM()->GetGame()->getPosX() < 0)
{
	System::getSM()->GetGame()->MoveMap(-moveSpeed, 0);
}
else if (posX - System::getSM()->GetGame()->getPosX() + moveSpeed >= 0 && posX >= 0)
{
  // player posX
	posX += moveSpeed;
}
else if (posX >= 0)
{
	UpdatePosX(moveSpeed + 1);
}
moveAnim = true;

Getgame returns GameplayScreen instace, move map is a method that add moveSpeed to posX of map. getMoveMap() returns boolean, if true, map can move, if false, it cannot.

LactoseposX is always negative or 0.

Yxjmir, that's right. I gave a bad example. It's 40 because if posX = 5, still I can see only 40 block and a piece of 41, and then I add 1 to get 1 more blocks. It returns ~41.

Untitled.png

as you can see, on the right site there are pieces of blocks.

Edited by DKdowneR

Share this post


Link to post
Share on other sites

Depending on your compiler, modulo of a negative number can result in undefined behavior.

Regardless, it should be fairly easy to track down where the error is in your case, since you can reproduce it easily:

For every loop, before using the x and y indices anywhere, print/log them.

When your application crashes, see what the x and y values are. Then, the next step is to find out why they are broken, and how to fix it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Forum Statistics

    • Total Topics
      628707
    • Total Posts
      2984310
  • Similar Content

    • By Benjamin Shefte
      Hey there,  I have this old code im trying to compile using GCC and am running into a few issues..
      im trying to figure out how to convert these functions to gcc
      static __int64 MyQueryPerformanceFrequency() { static __int64 aFreq = 0; if(aFreq!=0) return aFreq; LARGE_INTEGER s1, e1, f1; __int64 s2, e2, f2; QueryPerformanceCounter(&s1); s2 = MyQueryPerformanceCounter(); Sleep(50); e2 = MyQueryPerformanceCounter(); QueryPerformanceCounter(&e1); QueryPerformanceFrequency(&f1); double aTime = (double)(e1.QuadPart - s1.QuadPart)/f1.QuadPart; f2 = (e2 - s2)/aTime; aFreq = f2; return aFreq; } void PerfTimer::GlobalStart(const char *theName) { gPerfTimerStarted = true; gPerfTotalTime = 0; gPerfTimerStartCount = 0; gPerfElapsedTime = 0; LARGE_INTEGER anInt; QueryPerformanceCounter(&anInt); gPerfResetTick = anInt.QuadPart; } /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// void PerfTimer::GlobalStop(const char *theName) { LARGE_INTEGER anInt; QueryPerformanceCounter(&anInt); LARGE_INTEGER aFreq; QueryPerformanceFrequency(&aFreq); gPerfElapsedTime = (double)(anInt.QuadPart - gPerfResetTick)/aFreq.QuadPart*1000.0; gPerfTimerStarted = false; }  
      I also tried converting this function (original function is the first function below and my converted for gcc function is under that) is this correct?:
      #if defined(WIN32) static __int64 MyQueryPerformanceCounter() { // LARGE_INTEGER anInt; // QueryPerformanceCounter(&anInt); // return anInt.QuadPart; #if defined(WIN32) unsigned long x,y; _asm { rdtsc mov x, eax mov y, edx } __int64 result = y; result<<=32; result|=x; return result; } #else static __int64 MyQueryPerformanceCounter() { struct timeval t1, t2; double elapsedTime; // start timer gettimeofday(&t1, NULL); Sleep(50); // stop timer gettimeofday(&t2, NULL); // compute and print the elapsed time in millisec elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0; // sec to ms elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0; // us to ms return elapsedTime; } #endif Any help would be appreciated, Thank you!
    • By mister345
      Hi, I'm building a game engine using DirectX11 in c++.
      I need a basic physics engine to handle collisions and motion, and no time to write my own.
      What is the easiest solution for this? Bullet and PhysX both seem too complicated and would still require writing my own wrapper classes, it seems. 
      I found this thing called PAL - physics abstraction layer that can support bullet, physx, etc, but it's so old and no info on how to download or install it.
      The simpler the better. Please let me know, thanks!
    • By lawnjelly
      It comes that time again when I try and get my PC build working on Android via Android Studio. All was going swimmingly, it ran in the emulator fine, but on my first actual test device (Google Nexus 7 2012 tablet (32 bit ARM Cortex-A9, ARM v7A architecture)) I was getting a 'SIGBUS illegal alignment' crash.
      My little research has indicated that while x86 is fine with loading 16 / 32 / 64 bit values from any byte address in memory, the earlier ARM chips may need data to be aligned to the data size. This isn't a massive problem, and I see the reason for it (probably faster, like SIMD aligned loads, and simpler for the CPU). I probably have quite a few of these, particular in my own byte packed file formats. I can adjust the exporter / formats so that they are using the required alignment.
      Just to confirm, if anyone knows this, is it all 16 / 32 / 64 bit accesses that need to be data size aligned on early android devices? Or e.g. just 64 bit size access? 
      And is there any easy way to get the compiler to spit out some kind of useful information as to the alignment of each member of a struct / class, so I can quickly pin down the culprits?
      The ARM docs (http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15414.html) suggest another alternative is using a __packed qualifier. Anyone used this, is this practical?
    • By composerjones
      Hey guys, I just composed a new piece that is definitely inspired by scores of the Final Fantasy series and the Kingdom Hearts series. I hope you guys enjoy!
       
    • By Josheir
      In the following code:

       
      Point p = a[1]; center of rotation for (int i = 0; I<4; i++) { int x = a[i].x - p.x; int y = a[i].y - p.y; a[i].x = y + p.x; a[i].y = - x + p.y; }  
      I am understanding that a 90 degree shift results in a change like:   
      xNew = -y
      yNew = x
       
      Could someone please explain how the two additions and subtractions of the p.x and p.y works?
       
      Thank you,
      Josheir
  • Popular Now