Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
G
Game Library
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Onuralp SEZER
Game Library
Commits
9aa636c6
Commit
9aa636c6
authored
2 years ago
by
Joseph Walton-Rivers
Browse files
Options
Downloads
Patches
Plain Diff
grid rendering code
parent
52342ac7
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
demo/demo/hexboard/board.cpp
+40
-2
40 additions, 2 deletions
demo/demo/hexboard/board.cpp
include/fggl/grid/hexagon.hpp
+4
-2
4 additions, 2 deletions
include/fggl/grid/hexagon.hpp
with
44 additions
and
4 deletions
demo/demo/hexboard/board.cpp
+
40
−
2
View file @
9aa636c6
...
...
@@ -40,15 +40,53 @@ namespace demo::hexboard {
}
}
// see https://www.redblobgames.com/grids/hexagons/#hex-to-pixel
const
fggl
::
math
::
mat2
HEX_BASIS
{
std
::
sqrt
(
3.0
F
),
std
::
sqrt
(
3.0
F
)
/
2.0
F
,
0.0
F
,
3.0
F
/
2.0
F
};
static
inline
fggl
::
math
::
vec2
hexToScreen
(
fggl
::
grid
::
IntHex
hexPos
,
float
size
,
fggl
::
math
::
vec2
offset
=
{
0
,
0
})
{
return
size
*
fggl
::
math
::
vec2
{
hexPos
.
q
(),
hexPos
.
r
()}
*
HEX_BASIS
+
offset
;
}
void
Scene
::
render
(
fggl
::
gfx
::
Graphics
&
gfx
)
{
// if the board is not set, abort
if
(
m_board
==
nullptr
){
/*
if ( m_board == nullptr ){
return;
}
}
*/
// draw the grid
// FIXME don't hard-code the screen size
const
float
hexRadius
=
64.0
F
;
const
auto
gridWidth
=
(
int
)(
(
1920
-
hexRadius
)
/
(
hexRadius
*
std
::
sqrt
(
3.0
F
))
);
const
auto
gridHeight
=
(
int
)(
(
1080
-
hexRadius
)
/
(
hexRadius
*
(
3.0
F
/
2.0
F
)
));
const
fggl
::
math
::
vec2
offset
{
(
1920
-
(
(
float
)
gridWidth
*
hexRadius
*
std
::
sqrt
(
3.0
F
)))
-
(
hexRadius
/
2.0
F
),
(
(
1080
-
hexRadius
/
2.0
F
)
-
(
(
float
)
gridHeight
*
hexRadius
*
(
3.0
F
/
2.0
F
)
)),
};
fggl
::
gfx
::
Paint
paint
;
fggl
::
grid
::
IntHex
hexPos
{
0
,
0
};
auto
rowBasis
=
hexPos
;
for
(
auto
i
=
0
;
i
<
gridHeight
;
++
i
)
{
for
(
auto
j
=
0
;
j
<
gridWidth
;
++
j
)
{
auto
pos
=
hexToScreen
(
hexPos
,
hexRadius
,
offset
);
auto
hex
=
fggl
::
gfx
::
make_shape
(
pos
,
hexRadius
,
6
);
paint
.
stroke
(
hex
);
pos
.
x
+=
hexRadius
;
hexPos
=
hexPos
.
neighbour
(
fggl
::
grid
::
HexDirPointy
::
RIGHT
);
}
rowBasis
=
i
%
2
==
0
?
rowBasis
.
neighbour
(
fggl
::
grid
::
HexDirPointy
::
BOTTOM_RIGHT
)
:
rowBasis
.
neighbour
(
fggl
::
grid
::
HexDirPointy
::
BOTTOM_LEFT
);
hexPos
=
rowBasis
;
}
gfx
.
draw2D
(
paint
);
}
}
// namespace demo::hexboard
\ No newline at end of file
This diff is collapsed.
Click to expand it.
include/fggl/grid/hexagon.hpp
+
4
−
2
View file @
9aa636c6
...
...
@@ -72,11 +72,13 @@ namespace fggl::grid {
}
inline
HexPointT
neighbour
(
HexDirPointy
dir
)
{
return
this
+
HexPointT
<
T
>
(
HEX_DIRECTIONS
.
at
((
int
)
dir
));
auto
&
offset
=
HEX_DIRECTIONS
.
at
(
(
int
)
dir
);
return
{
m_pos
[
0
]
+
offset
[
0
],
m_pos
[
1
]
+
offset
[
1
]
};
}
inline
HexPointT
neighbour
(
HexDirFlat
dir
)
{
return
this
+
HexPointT
<
T
>
(
HEX_DIAGONALS
.
at
((
int
)
dir
));
auto
&
offset
=
HEX_DIAGONALS
.
at
(
(
int
)
dir
);
return
{
m_pos
[
0
]
+
offset
[
0
],
m_pos
[
1
]
+
offset
[
1
]
};
}
HexPointT
operator
+
(
const
HexPointT
<
T
>&
other
)
const
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment