The first thing we'll need is a way to change mouse coordinates into what cell they correspond to:
void MouseToRowCol(int32 x, int32 y, uint32& r, uint32& c)
{
if (x < 0 || x > WINDOW_WIDTH || y < 0 || y > WINDOW_HEIGHT)
{
x = 0;
y = 0;
}
r = std::floor(float(y) / float(IMAGE_HEIGHT));
c = std::floor(float(x) / float(IMAGE_WIDTH));
}
Cell* MouseToCell(int32 x, int32 y)
{
uint32 r;
uint32 c;
MouseToRowCol(x, y, r, c);
return &g_cells[r][c];
}
Next, we can deal with SDL_Event's like so:
if (e.type == SDL_MOUSEBUTTONDOWN || e.type == SDL_MOUSEBUTTONUP)
{
if (lost && e.type == SDL_MOUSEBUTTONUP)
{
InitCells();
lost = false;
while (SDL_PollEvent(&e) != 0) {}
break;
}
uint32 r;
uint32 c;
MouseToRowCol(e.button.x, e.button.y, r, c);
Cell* cell = MouseToCell(e.button.x, e.button.y);
if (e.type == SDL_MOUSEBUTTONUP)
{
if (e.button.button == SDL_BUTTON_MIDDLE)
{
// Reveal cells
}
else if (e.button.button == SDL_BUTTON_LEFT)
{
// Click a cell
}
}
else if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_RIGHT)
{
// Flag or guess
}
}