【问题标题】:C++ Wrong margin of the rackets in a window窗口中球拍的 C++ 错误边距
【发布时间】:2015-06-27 01:25:33
【问题描述】:
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;

完整代码(不含球类):http://pastebin.com/TA9NkV5c

右侧球拍到窗口右侧的边距小于左侧。这些变量的计算是正确的,但仍然不相等。

http://i.imgur.com/2PA0pGz.png图片链接

【问题讨论】:

  • 正确书写。我建议您不要使用幻数,而是使用 #defies 。例如。 #define RACKET_WIDTH 10 。然后对两个球拍使用相同的 RACKET_WIDTH 。这样定义高度和初始位置。对于您的问题,请检查“宽度”是否实际上是您的窗口宽度。最好使用 api 来获取宽度等窗口属性
  • 甚至没有可执行代码。告诉我们MCVE。 C++ 没有“球拍”或“坐标”的概念。你是怎么画的?你在使用什么库?只需阅读您的问题并问自己:“有足够的信息来回答吗?”双星号是什么?
  • @MAG 开启,请no 更多macro
  • 我添加了完整的代码。
  • 我们不想要完整的代码(即为您调试它)。在 MCVE 中隔离问题(见上面的链接)

标签: c++


【解决方案1】:

您已将 窗口 设置为 800 像素的宽度,但其他所有内容都相对于客户区定位,该客户区比窗口宽度窄边框的厚度。

使用AdjustWindowRect 计算您必须制作窗口的大小,以使客户区达到所需大小。

// 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, ...);

【讨论】:

  • 只是好奇,你是怎么推断它是winapi的?
  • 屏幕截图是第一条线索。
  • 显然是windows,但也可以是任何渲染框架/API。让我们看看你的猜测是否正确;)
  • 它可以是任何基于 WinAPI 构建的框架。该错误与不考虑客户端矩形和窗口矩形之间的差异完全一致。当然,它可能是 MFC 或 ATL/WTL 甚至 Qt。 PieperLever 可以使用 GDI、GDI+、DirectDraw、Direct2D 或 Direct3D。无论如何,该修复程序适用于任何这些框架。窗口和客户端的区别在于 Windows API 概念。
  • 内容的绘制方式无关紧要。该错误与窗口区域和客户区域之间的差异有关,这是一个Windows API概念。 GLUT 在系统 API 之上工作。
猜你喜欢
  • 1970-01-01
  • 2013-04-19
  • 2018-03-02
  • 1970-01-01
  • 2016-05-14
  • 2020-09-07
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
相关资源
最近更新 更多