问题描述
我在做瓷砖的游戏在C + +。现在,当游戏加载所有的瓷砖基础上把自己:
I'm making a tile game in c++. Right now when the game loads all the tiles place themselves based on:
tilesize - >它们是正方形所以这是宽度和高度
tilesize -> they are squares so this is width and height
tile_count_x
tile_count_x
tile_count_y
tile_count_y
我有以下变量:
desktop_width
desktop_width
desktop_height
desktop_height
game_window_width
game_window_width
game_window_height
game_window_height
tile_count_x
tile_count_x
tile_count_y
tile_count_y
,我在寻找一种算法,将设置特定的桌面和tile_count限制一个适当的窗口大小。然后在此,我希望我的瓷砖具有偏移,将边界X%左右,这将根本决定tilesize太窗口:
Based on these values, I'm looking for an algorithm that will set an appropriate window size given the desktop and tile_count constraints. Then within this, I want my tiles to have an offset that will border x% around the window which will basically decide the tilesize too:
例:如果我有10 * 3瓦的话:
Example:If I have 10 * 3 of tiles then:
______________________________
Window Title _[]X
------------------------------
| |
| [][][][][][][][][][] |
| [][][][][][][][][][] |
| [][][][][][][][][][] |
| |
------------------------------
我只是不知道所需要的公式来做到这一点。
I'm just not sure the formula required to do this.
编辑(从评论):
- Tilesize变化,tilecountx和y是静态
- 我想,因为它可以给桌面分辨率的gamewindow是一样大,但我也希望它的长宽比尊重tilecoutx和tilecounty
我发现我的意思的例子,在Windows中打开扫雷
I found an example of what I mean, open up Minesweeper in Windows
推荐答案
下面是我如何解决它?
//WINDOW SIZE SETUP
//choose the smaller TILE_SIZE
if (DESKTOP_WIDTH / TILE_COUNT_X > DESKTOP_HEIGHT / TILE_COUNT_Y)
{
TILE_SIZE = DESKTOP_HEIGHT / TILE_COUNT_Y;
}
else
{
TILE_SIZE = DESKTOP_WIDTH / TILE_COUNT_X;
}
//Set screen size and consider a 5 tile border
SCREEN_WIDTH = TILE_SIZE * (TILE_COUNT_X + 5);
SCREEN_HEIGHT = TILE_SIZE * (TILE_COUNT_Y + 5);
//resize window until it satisfies resolution constraints
while( SCREEN_WIDTH > (DESKTOP_WIDTH - (DESKTOP_WIDTH * 0.07)))
{
TILE_SIZE --;
SCREEN_WIDTH = TILE_SIZE * (TILE_COUNT_X + 5);
SCREEN_HEIGHT = TILE_SIZE * (TILE_COUNT_Y + 5);
}
while( SCREEN_HEIGHT > (DESKTOP_HEIGHT - (DESKTOP_HEIGHT * 0.15)))
{
TILE_SIZE -- ;
SCREEN_WIDTH = TILE_SIZE * (TILE_COUNT_X + 5);
SCREEN_HEIGHT = TILE_SIZE * (TILE_COUNT_Y + 5);
}
for(int i = 0; i < 8; ++i) //Ensure resolution is multiple of 8
{
if (SCREEN_WIDTH % 8 != 0) //remainder means not multiple of 8
{
SCREEN_WIDTH += 1;
}
if (SCREEN_HEIGHT % 8 != 0)
{
SCREEN_HEIGHT += 1;
}
}
X_OFFSET = (SCREEN_WIDTH - (TILE_SIZE * TILE_COUNT_X)) / 2;
Y_OFFSET = (SCREEN_HEIGHT - (TILE_SIZE * TILE_COUNT_Y)) / 2;
这篇关于瓷砖尺寸算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!