beginners C++ tut5

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Posts: 1
Joined: May 2nd, 2019, 10:28 am

beginners C++ tut5

Post by thanak13 » May 2nd, 2019, 5:14 pm

Hey! Sorry for my bad english. Im watching your tutorials about beginner c++ game programming and im stuck on tutorial 5 at the point of screenwidth and screenheight. There is no problem when i move the cursor to the right and hit the window, but when i go left it pass the line and stopped at the point that it's a half from the left line and the other half from the right line. I try to set x - 5 < 10 and it fixes the problem for the x axis. But there is a bigger problem with the y axis. I have set y + 5 >= gfx.ScreenHeight but the programm still crashes, if i set y + 5 >= 590 the programm works only if the cursor hit the under line with low speed, if it hits with high speed it will crash again. Im so confused at this point and hope u have the solution about that problem.
p.s. Your tutorial rocks!!!!

Posts: 3721
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: beginners C++ tut5

Post by albinopapa » May 3rd, 2019, 1:12 am

The drawing buffer is one continuous chunk of memory, even through you use X and Y to represent position, they are actually a 2D index into a 1D array. When you go to X = 800, Y = 0, you are actually 800 elements in on the 1D array. Now, say you are at coordinates X = 400, Y = 600, in the 1D array you are actually 480,400 elements into the array when the array was only allocated ( 800 x 600 = ) 480,000 elements.

Basically, going off the top or bottom of the screen, you end up trying to access outside the array bounds. Going off the left or right, just wraps around ( or would if it wasn't for the asserts in PutPixel ).

Back to the X = 800, Y = 0 scenario, you can convert from 2D index to 1D index using:
x + ( y * width ) -> 800 + ( 0 * 800 ) -> 800 + 0 -> 1d_index = 800

you can convert from 1D to 2D like this,
int x = 1d_index % width; // 800 % 800 = 0
int y = 1d_index / width; // 800 / 800 = 1

So, coordinates ( 0, 1 ) are the same as coordinates ( 800, 0 ) when the screen width is equal to 800.

Hopefully that helps.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. -

Post Reply