2
\$\begingroup\$

So I am trying to draw hexagons with [SDL_RenderDrawLines][]. It looks to be working fairly well, however, it fails to draw some of the lines.

hex map with missing sides

So each of the hexagons in the screenshot is a hex object. They each have a center x/y , and an array of SDL_Points for the six corners of the hex.

Here is the hex object draw call:

SDL_RenderDrawLines(
    TheGame::Instance()->getRenderer(),
    corners,
    HEX_NUMBER_OF_CORNERS);

There is some reoccurring condition that causes a vertical line to not be rendered, and I can't seem to figure out why.

The two obvious things that stand out don't explain why there is no line:

  1. The draw order of the objects shouldn't matter since all of the borders for each hex are being drawn.
  2. The horizontal distance between adjacent hexes' centers can be +-1 because of rounding would lead to a double thick border and not a missing border.

If anyone has any insight, I welcome the input.

EDIT:

I have been over suggestions you have mentioned prior to this question, increasing the array of points to 7 is correct, I tested it on a triangle before I tried with hexagons. When you don't close of the shape, you typically get drawn lines that shoot off the edge of the screen.

The part on three lines is also something else I discovered while messing around. I included some of my code to show the two ways that I preformed the very thing you mentioned:

// render the hexagon to on screen
void Hex::draw()
{/*
 draw order of points
 rotate ccw 90 degrees for actual postion
  [2] ____ [3]
     /    \
[1] /      \
    \      / [4]
     \____/
  [0]    [5]
 */

    // draw the hex with a fill color
    // 0XAABBGGRR is the bit order for the color param
    filledPolygonColor(
        TheGame::Instance()->getRenderer(),
        vx, vy,
        HEX_NUMBER_OF_CORNERS, r_order(CLEAR));

    // set outline color to black
    SDL_SetRenderDrawColor(
        TheGame::Instance()->getRenderer(), 
        0, 0, 0, 255);


    // draw the hex outline

    //SDL_RenderDrawLines(
    //  TheGame::Instance()->getRenderer(),
    //  corners + 2,
    //  HEX_NUMBER_OF_CORNERS - 2);

    // or
    SDL_RenderDrawLine(
        TheGame::Instance()->getRenderer(),
        corners[2].x, corners[2].y,
        corners[3].x, corners[3].y);
    SDL_RenderDrawLine(
        TheGame::Instance()->getRenderer(),
        corners[3].x, corners[3].y,
        corners[4].x, corners[4].y);
    SDL_RenderDrawLine(
        TheGame::Instance()->getRenderer(),
        corners[4].x, corners[4].y,
        corners[5].x, corners[5].y);
}

Just for clarity, it still occurs in spots where there should be a drawn line. This is a shot with the above code:

enter image description here

\$\endgroup\$
1
\$\begingroup\$

According to the documentation for SDL_RenderDrawLines:

Function parameters

count the number of points, drawing count-1 lines

SDL_RenderDrawLines() doesn't presume that you have a closed shape; it's just drawing lines between the points you've provided. So with 6 points (as per your HEX_NUMBER_OF_CORNERS constant), you get just five lines, drawn between the set of points you've specified in order

If you wish to draw all six lines around each hexagon, then you'll need to provide an array of seven points, duplicating the first corner also as the last point in the array, and then pass a count of 7 to SDL_RenderDrawLines().

\$\endgroup\$
  • \$\begingroup\$ Side-note: For bonus points, you actually only need to draw three lines per hexagon, to make this type of grid. If you (for example) draw only the left and top lines for each hexagon, the adjacent hexagons will draw the other edges. Note that you'd still need to draw the other edges for any hexagons which don't have neighbor hexagons across those edges. This isn't necessary, but it slightly reduces the amount of drawing required. :) \$\endgroup\$ – Trevor Powell Jan 3 '17 at 1:37
  • \$\begingroup\$ Thanks for your suggestions, I have gone over both of those area prior to this post. It is strange because, it worked fine in a test project that I used just to work on the headers and classes for this, but once I ported it over practically verbatim it started acting like this. \$\endgroup\$ – chewbapoclypse Jan 3 '17 at 2:08
1
\$\begingroup\$

The reason why I was able to do this in the stand alone project but not have similar results is because I am using SDL_RenderSetLogicalSize. I am not using some crazy size though, which I find kinda odd that it is having issues.

SDL_RenderSetLogicalSize(renderer, 1920, 1080);

As soon as I change the resolution to something smaller than this it works fine.

1536 x 864 was the highest 16:9 resolution that worked

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.