Skip to content

Commit

Permalink
Use unsigned indices
Browse files Browse the repository at this point in the history
  • Loading branch information
Flamefire committed Nov 13, 2017
1 parent eab566e commit c59e9e9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 92 deletions.
50 changes: 26 additions & 24 deletions CMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ void CMap::setMouseData(const SDL_MouseMotionEvent& motion)
static bool warping = false;
// SDL_Event TempEvent;
// is right mouse button pressed?
if(SDL_GetMouseState(NULL, NULL) & SDL_BUTTON(3))
if(motion.state & SDL_BUTTON(3))
{
// this whole "warping-thing" is to prevent cursor-moving WITHIN the window while user moves over the map
if(warping == false)
Expand Down Expand Up @@ -1029,23 +1029,24 @@ void CMap::saveVertex(Uint16 MouseX, Uint16 MouseY, Uint8 MouseState)
// if ( (MouseState == SDL_PRESSED) && (mode == EDITOR_MODE_HEIGHT_RAISE || mode == EDITOR_MODE_HEIGHT_REDUCE) )
// return;

int X = 0, Xeven = 0, Xuneven = 0;
int X = 0, Xeven = 0, Xodd = 0;
int Y = 0, MousePosY = 0;

// get X
// following out commented lines are the correct ones, but for tolerance (to prevent to early jumps of the cursor) we substract
// following out commented lines are the correct ones, but for tolerance (to prevent to early jumps of the cursor) we subtract
// "TRIANGLE_WIDTH/2" Xeven = (MouseX + displayRect.x) / TRIANGLE_WIDTH;
Xeven = (MouseX + displayRect.x - TRIANGLE_WIDTH / 2) / TRIANGLE_WIDTH;
if(Xeven < 0)
Xeven += (map->width);
else if(Xeven > map->width - 1)
Xeven -= (map->width - 1);
Xuneven = (MouseX + displayRect.x + TRIANGLE_WIDTH / 2) / TRIANGLE_WIDTH;
// Xuneven = (MouseX + displayRect.x) / TRIANGLE_WIDTH;
if(Xuneven < 0)
Xuneven += (map->width - 1);
else if(Xuneven > map->width - 1)
Xuneven -= (map->width);
// Add rows are already shifted by TRIANGLE_WIDTH / 2
Xodd = (MouseX + displayRect.x) / TRIANGLE_WIDTH;
// Xodd = (MouseX + displayRect.x) / TRIANGLE_WIDTH;
if(Xodd < 0)
Xodd += (map->width - 1);
else if(Xodd > map->width - 1)
Xodd -= (map->width);

MousePosY = MouseY + displayRect.y;
// correct mouse position Y if displayRect is outside map edges
Expand All @@ -1059,17 +1060,17 @@ void CMap::saveVertex(Uint16 MouseX, Uint16 MouseY, Uint8 MouseState)
{
if(j % 2 == 0)
{
// substract "TRIANGLE_HEIGHT/2" is for tolerance, we did the same for X
// subtract "TRIANGLE_HEIGHT/2" is for tolerance, we did the same for X
if((MousePosY - TRIANGLE_HEIGHT / 2) > map->getVertex(Xeven, j).y)
Y++;
else
{
X = Xuneven;
X = Xodd;
break;
}
} else
{
if((MousePosY - TRIANGLE_HEIGHT / 2) > map->getVertex(Xuneven, j).y)
if((MousePosY - TRIANGLE_HEIGHT / 2) > map->getVertex(Xodd, j).y)
Y++;
else
{
Expand All @@ -1078,10 +1079,11 @@ void CMap::saveVertex(Uint16 MouseX, Uint16 MouseY, Uint8 MouseState)
}
}
}
if(Y < 0)
Y += (map->height - 1);
else if(Y > map->height - 1)
Y -= (map->height - 1);
if(Y >= map->height)
{
Y -= map->height;
X = Y % 2 == 0 ? Xeven : Xodd;
}

VertexX_ = X;
VertexY_ = Y;
Expand Down Expand Up @@ -1200,7 +1202,7 @@ void CMap::render()
CFont::writeText(Surf_Map, textBuffer, 20, 40, 14, FONT_ORANGE);
} else if(VerticalMovementLocked)
{
sprintf(textBuffer, "Vertikal mvement locked (F10 to unlock)");
sprintf(textBuffer, "Vertikal movement locked (F10 to unlock)");
CFont::writeText(Surf_Map, textBuffer, 20, 40, 14, FONT_ORANGE);
}

Expand Down Expand Up @@ -2626,6 +2628,13 @@ void CMap::modifyPlayer(int VertexX, int VertexY)
map->getVertex(VertexX, VertexY).objectType = modeContent;
map->getVertex(VertexX, VertexY).objectInfo = 0x80;

// for compatibility with original settlers 2 we write the headquarters positions to the map header (for the first 7 players)
if(modeContent >= 0 && modeContent < 7)
{
map->HQx[modeContent] = VertexX;
map->HQy[modeContent] = VertexY;
}

// save old position if exists
if(PlayerHQx[modeContent] != 0xFFFF && PlayerHQy[modeContent] != 0xFFFF)
{
Expand All @@ -2638,13 +2647,6 @@ void CMap::modifyPlayer(int VertexX, int VertexY)
PlayerHQx[modeContent] = VertexX;
PlayerHQy[modeContent] = VertexY;

// for compatibility with original settlers 2 we write the headquarters positions to the map header (for the first 7 players)
if(modeContent >= 0 && modeContent < 7)
{
map->HQx[modeContent] = VertexX;
map->HQy[modeContent] = VertexY;
}

// setup number of players in map header
if(!PlayerRePositioned)
map->player++;
Expand Down
111 changes: 43 additions & 68 deletions CSurface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,7 @@ void CSurface::DrawTriangleField(SDL_Surface* display, DisplayRectangle displayR
// NOTE: WE DO THIS TWICE, AT FIRST ONLY TRIANGLE-TEXTURES, AT SECOND THE TEXTURE-BORDERS AND OBJECTS
for(int i = 0; i < 2; i++)
{
if(i == 0)
drawTextures = true;
else /*if (i == 1)*/
drawTextures = false;
drawTextures = (i == 0);

for(int k = 0; k < 4; k++)
{
Expand Down Expand Up @@ -385,106 +382,84 @@ void CSurface::DrawTriangleField(SDL_Surface* display, DisplayRectangle displayR
assert(col_start <= col_end);
assert(row_start <= row_end);

for(int j = row_start; j < height - 1 && j <= row_end; j++)
for(unsigned y = row_start; y < height - 1u && y <= static_cast<unsigned>(row_end); y++)
{
if(j % 2 == 0)
if(y % 2 == 0)
{
// first RightSideUp
tempP2 = myMap->getVertex(width - 1, y + 1);
tempP2.x = 0;
tempP2.y = myMap->getVertex(width - 1, j + 1).y;
tempP2.z = myMap->getVertex(width - 1, j + 1).z;
tempP2.i = myMap->getVertex(width - 1, j + 1).i;
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(0, j), tempP2, myMap->getVertex(0, j + 1));
for(int i = std::max(col_start, 1); i < width && i <= col_end; i++)
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(0, y), tempP2, myMap->getVertex(0, y + 1));
for(unsigned x = std::max(col_start, 1); x < width && x <= static_cast<unsigned>(col_end); x++)
{
// RightSideUp
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(i, j), myMap->getVertex(i - 1, j + 1),
myMap->getVertex(i, j + 1));
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(x, y), myMap->getVertex(x - 1, y + 1),
myMap->getVertex(x, y + 1));
// UpSideDown
if(i < width)
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(i - 1, j + 1), myMap->getVertex(i - 1, j),
myMap->getVertex(i, j));
if(x < width)
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(x - 1, y + 1), myMap->getVertex(x - 1, y),
myMap->getVertex(x, y));
}
// last UpSideDown
tempP3.x = myMap->getVertex(width - 1, j).x + TRIANGLE_WIDTH;
tempP3.y = myMap->getVertex(0, j).y;
tempP3.z = myMap->getVertex(0, j).z;
tempP3.i = myMap->getVertex(0, j).i;
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(width - 1, j + 1), myMap->getVertex(width - 1, j),
tempP3 = myMap->getVertex(0, y);
tempP3.x = myMap->getVertex(width - 1, y).x + TRIANGLE_WIDTH;
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(width - 1, y + 1), myMap->getVertex(width - 1, y),
tempP3);
} else
{
for(int i = col_start; i < width - 1 && i <= col_end; i++)
for(unsigned x = col_start; x < width - 1u && x <= static_cast<unsigned>(col_end); x++)
{
// RightSideUp
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(i, j), myMap->getVertex(i, j + 1),
myMap->getVertex(i + 1, j + 1));
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(x, y), myMap->getVertex(x, y + 1),
myMap->getVertex(x + 1, y + 1));
// UpSideDown
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(i + 1, j + 1), myMap->getVertex(i, j),
myMap->getVertex(i + 1, j));
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(x + 1, y + 1), myMap->getVertex(x, y),
myMap->getVertex(x + 1, y));
}
// last RightSideUp
tempP3.x = myMap->getVertex(width - 1, j + 1).x + TRIANGLE_WIDTH;
tempP3.y = myMap->getVertex(0, j + 1).y;
tempP3.z = myMap->getVertex(0, j + 1).z;
tempP3.i = myMap->getVertex(0, j + 1).i;
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(width - 1, j), myMap->getVertex(width - 1, j + 1),
tempP3 = myMap->getVertex(0, y + 1);
tempP3.x = myMap->getVertex(width - 1, y + 1).x + TRIANGLE_WIDTH;
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(width - 1, y), myMap->getVertex(width - 1, y + 1),
tempP3);
// last UpSideDown
tempP1.x = myMap->getVertex(width - 1, j + 1).x + TRIANGLE_WIDTH;
tempP1.y = myMap->getVertex(0, j + 1).y;
tempP1.z = myMap->getVertex(0, j + 1).z;
tempP1.i = myMap->getVertex(0, j + 1).i;
tempP3.x = myMap->getVertex(width - 1, j).x + TRIANGLE_WIDTH;
tempP3.y = myMap->getVertex(0, j).y;
tempP3.z = myMap->getVertex(0, j).z;
tempP3.i = myMap->getVertex(0, j).i;
DrawTriangle(display, displayRect, myMap, type, tempP1, myMap->getVertex(width - 1, j), tempP3);
tempP1 = myMap->getVertex(0, y + 1);
tempP1.x = myMap->getVertex(width - 1, y + 1).x + TRIANGLE_WIDTH;
tempP3 = myMap->getVertex(0, y);
tempP3.x = myMap->getVertex(width - 1, y).x + TRIANGLE_WIDTH;
DrawTriangle(display, displayRect, myMap, type, tempP1, myMap->getVertex(width - 1, y), tempP3);
}
}

// draw last line
for(int i = col_start; i < width - 1 && i <= col_end; i++)
for(unsigned x = col_start; x < width - 1u && x <= static_cast<unsigned>(col_end); x++)
{
// RightSideUp
tempP2.x = myMap->getVertex(i, 0).x;
tempP2.y = height * TRIANGLE_HEIGHT + myMap->getVertex(i, 0).y;
tempP2.z = myMap->getVertex(i, 0).z;
tempP2.i = myMap->getVertex(i, 0).i;
tempP3.x = myMap->getVertex(i + 1, 0).x;
tempP3.y = height * TRIANGLE_HEIGHT + myMap->getVertex(i + 1, 0).y;
tempP3.z = myMap->getVertex(i + 1, 0).z;
tempP3.i = myMap->getVertex(i + 1, 0).i;
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(i, height - 1), tempP2, tempP3);
tempP2 = myMap->getVertex(x, 0);
tempP2.y = height * TRIANGLE_HEIGHT + myMap->getVertex(x, 0).y;
tempP3 = myMap->getVertex(x + 1, 0);
tempP3.y = height * TRIANGLE_HEIGHT + myMap->getVertex(x + 1, 0).y;
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(x, height - 1), tempP2, tempP3);
// UpSideDown
tempP1.x = myMap->getVertex(i + 1, 0).x;
tempP1.y = height * TRIANGLE_HEIGHT + myMap->getVertex(i + 1, 0).y;
tempP1.z = myMap->getVertex(i + 1, 0).z;
tempP1.i = myMap->getVertex(i + 1, 0).i;
DrawTriangle(display, displayRect, myMap, type, tempP1, myMap->getVertex(i, height - 1),
myMap->getVertex(i + 1, height - 1));
tempP1 = myMap->getVertex(x + 1, 0);
tempP1.y = height * TRIANGLE_HEIGHT + myMap->getVertex(x + 1, 0).y;
DrawTriangle(display, displayRect, myMap, type, tempP1, myMap->getVertex(x, height - 1),
myMap->getVertex(x + 1, height - 1));
}
}

// last RightSideUp
tempP2.x = myMap->getVertex(width - 1, 0).x;
tempP2 = myMap->getVertex(width - 1, 0);
tempP2.y = height * TRIANGLE_HEIGHT + myMap->getVertex(width - 1, 0).y;
tempP2.z = myMap->getVertex(width - 1, 0).z;
tempP2.i = myMap->getVertex(width - 1, 0).i;
tempP3 = myMap->getVertex(0, 0);
tempP3.x = myMap->getVertex(width - 1, 0).x + TRIANGLE_WIDTH;
tempP3.y = height * TRIANGLE_HEIGHT + myMap->getVertex(0, 0).y;
tempP3.z = myMap->getVertex(0, 0).z;
tempP3.i = myMap->getVertex(0, 0).i;
tempP3.y += height * TRIANGLE_HEIGHT;
DrawTriangle(display, displayRect, myMap, type, myMap->getVertex(width - 1, height - 1), tempP2, tempP3);
// last UpSideDown
tempP1 = myMap->getVertex(0, 0);
tempP1.x = myMap->getVertex(width - 1, 0).x + TRIANGLE_WIDTH;
tempP1.y = height * TRIANGLE_HEIGHT + myMap->getVertex(0, 0).y;
tempP1.z = myMap->getVertex(0, 0).z;
tempP1.i = myMap->getVertex(0, 0).i;
tempP1.y += height * TRIANGLE_HEIGHT;
tempP3 = myMap->getVertex(0, height - 1);
tempP3.x = myMap->getVertex(width - 1, height - 1).x + TRIANGLE_WIDTH;
tempP3.y = myMap->getVertex(0, height - 1).y;
tempP3.z = myMap->getVertex(0, height - 1).z;
tempP3.i = myMap->getVertex(0, height - 1).i;
DrawTriangle(display, displayRect, myMap, type, tempP1, myMap->getVertex(width - 1, height - 1), tempP3);
}
}
Expand Down
2 changes: 2 additions & 0 deletions SGE/sge_textpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class DECLSPEC sge_text : public sge_TextEditor
sge_text()
{
tt_font = NULL;
color.r = color.b = color.g = 0;
background.r = background.b = background.g = 0;
bm_font = NULL;
alpha_level = SDL_ALPHA_OPAQUE;
text_surface = NULL;
Expand Down

0 comments on commit c59e9e9

Please sign in to comment.