0
int width = 800;
int height = 600;
int interval = 1000 / 60;
int score_player1 = 0;
int score_player2 = 0;
int racket_width = 10;
int racket_height = 80;
int racket_speed = 8;
int racket_left_x = 10;
int racket_left_y = 50;
int racket_right_x = width - racket_width - 10;
int racket_right_y = 50;

Full code (without the class for the ball): http://pastebin.com/TA9NkV5c

The margin from the right racket to right side of the window is smaller than the left side. The variables for those are right calculated, but still, it is not equally.

https://i.stack.imgur.com/OjqtZ.png Link to the image

  • Write properly . I would recommend you not to use the magic numbers but #defies . eg . #define RACKET_WIDTH 10 . and then use the same RACKET_WIDTH for both the rackets . That way define for height and initial location . For your problem check if "width" is actually the width of your window . Better to use api to get window properties like width etc – MAG Jun 26 '15 at 16:07
  • There is no even executable code. Show us [MCVE](http://stackoverflow.com/help/mcve). C++ has no notion of a "racket" nor "coordinates". How do you draw it? What libraries are you using? Just read your question and ask yourself: "Is there enough information to answer?" What are the double asterisks? – Ivan Aksamentov - Drop Jun 26 '15 at 16:09
  • 1
    @MAG On, please, [no](http://stackoverflow.com/questions/4715831/why-is-define-bad-and-what-is-the-proper-substitute) more [macro](http://stackoverflow.com/questions/14041453/why-are-preprocessor-macros-evil-and-what-are-the-alternatives) – Ivan Aksamentov - Drop Jun 26 '15 at 16:14
  • I added the full code. – PieperLever Jun 26 '15 at 16:28
  • We don't want full code (i.e. to debug it for you). Isolate the problem in a MCVE (see link above) – Ivan Aksamentov - Drop Jun 26 '15 at 17:12

1 Answers1

0

You've set the window to a width of 800 pixels, but everything else is positioned relative to the client region, which is narrower than the window width by the thickness of the border.

Use AdjustWindowRect to compute the size you have to make the window in order get the client area to be the desired size.

// Initialize a RECT with the size you want the client area to be.
RECT rc = { 0, 0, width, height };
// Now adjust the rectangle to the size the window would need to be.
AdjustWindowRect(&rc, my_style_flags, FALSE);

// Now create the window using the sizes in rc.  Make sure you use
// consistent style flags or the adjustment may not be correct.
const int window_width = rc.right - rc.left;
const int window_height = rc.bottom - rc.top;
my_hwnd = CreateWindow(..., my_style_flags, x, y, window_width, window_height, ...);
Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • Just curious, how do you deduced that it's winapi? – Ivan Aksamentov - Drop Jun 26 '15 at 16:09
  • The screen shot was the first clue. – Adrian McCarthy Jun 26 '15 at 16:10
  • It's obviously windows, but it could be any rendering framework/API. Let's see if your guess is correct ;) – Ivan Aksamentov - Drop Jun 26 '15 at 16:12
  • It could be any framework that's built on WinAPI. The bug is completely consistent with not accounting for the difference between the client rect and the window rect. Sure, it could be MFC or ATL/WTL or even Qt. PieperLever could be using GDI, GDI+, DirectDraw, Direct2D, or Direct3D. Regardless, the fix would work with any of those frameworks. The difference between window and client is a Windows API concept. – Adrian McCarthy Jun 26 '15 at 16:21
  • How the content is being drawn is irrelevant. The bug has to do with the difference between the window area and the client area, which is a Windows API concept. GLUT works on top of the system API. – Adrian McCarthy Jun 26 '15 at 16:54
  • I've got your point. It's just GLUT does `AdjustWindowRect()` by default... So it could be any other thing. – Ivan Aksamentov - Drop Jun 26 '15 at 17:08
  • What makes you think GLUT does AdjustWindowRect()? The documentation doesn't make that claim, and the code's not open source. – Adrian McCarthy Jun 26 '15 at 20:15
  • [GLUT](https://www.opengl.org/resources/libraries/glut/glut_downloads.php#2) is open source, and I looked it up (win32_util.c, line 97) – Ivan Aksamentov - Drop Jun 26 '15 at 20:20
  • When I set it to fullscreen, the margins are perfect, but when I resize the window, the right border seems to go disappear. – PieperLever Jun 27 '15 at 16:17
  • When I went looking for the code, I found the, "GLUT is not open source," on https://www.opengl.org/resources/libraries/glut/, so I stopped looking. – Adrian McCarthy Jun 29 '15 at 17:30