void GuiTilemap::updatePreviewTiles() { Tile* tile = getTileXY( _d->lastCursorPos.getX(), _d->lastCursorPos.getY() ); // tile under the cursor (or NULL) discardPreview(); if( tile ) { int i = tile->getI(); int j = tile->getJ(); checkPreviewBuild(i, j); checkPreviewRemove(i, j); } };
void GuiTilemap::handleEvent(SDL_Event &event) { if (event.type == SDL_MOUSEMOTION) { _d->lastCursorPos = Point( event.motion.x, event.motion.y ); updatePreviewTiles(); } if (event.type == SDL_MOUSEBUTTONDOWN) { int button = event.button.button; int x = event.button.x; int y = event.button.y; Tile* tile = getTileXY(x, y); // tile under the cursor (or NULL) if (tile == NULL) { // std::cout << "No Tile pressed at ("<<x<<","<<y<<")" << std::endl; } else { int i = tile->getI(); int j = tile->getJ(); std::cout << "Tile ("<<i<<","<<j<<") pressed at ("<<x<<","<<y<<") with button:" << button << std::endl; if (button == 1) { // left button if (_removeTool) { _city->clearLand(i, j); } else if (_buildInstance != NULL) { Construction& overlay = *_buildInstance; if (overlay.canBuild(i, j)) { _city->build(overlay, i, j); } } else { getMapArea().setCenterIJ(i, j); } } else if (button == 3) { if (_removeTool) { // discard removeTool _removeTool = false; discardPreview(); } else if (_buildInstance != NULL) { // discard buildTool _buildInstance = NULL; discardPreview(); } else if (tile->get_terrain().getOverlay() != NULL) { // tile has an overlay LandOverlay &overlay = *tile->get_terrain().getOverlay(); GuiInfoBox* infoBox = overlay.makeInfoBox(); if (infoBox != NULL) { infoBox->setPosition((GfxEngine::instance().getScreenWidth()-infoBox->getWidth())/2, 10); _screenGame->setInfoBox(infoBox); } } } } } }
void render(SDL_Renderer* renderer) { //draw draw space // if (input_draw_space) { // SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); // // SDL_Rect rect; // rect.x = 0; // rect.y = 0; // mapToOrtho(worldMap.GetWidth(), worldMap.GetHeight(), rect.w, rect.h); // zoom(rect); // worldToScreen(rect.x, rect.y); // // SDL_RenderFillRect(renderer, &rect); // SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // } //draw map for (int y = 0; y < worldMap.GetHeight(); y++) { for (int x = 0; x < worldMap.GetWidth(); x++) { SDL_Rect rect; //in world coords rect.w = tileSizeX; rect.h = tileSizeY; mapToIso(x, y, rect.x, rect.y); zoom(rect); rect.y -= (tileSizeY*input_scale - tileWidth*input_scale); worldToScreen(rect.x, rect.y); SDL_Rect tileRect; //in tileset coords tileRect.w = tileSizeX; tileRect.h = tileSizeY; //layer terrain const int tileId = worldMap.Get(x,y).terrainId; if (tileId != -1) { int tileX; int tileY; getTileXY(tileId, &tileX, &tileY, TILESET_WIDTH); tileRect.x = tileX * tileSizeX; tileRect.y = tileY * tileSizeY; SDL_RenderCopy(renderer, tileset, &tileRect, &rect); } //layer object const int tileId2 = worldMap.Get(x, y).objectId; if (tileId2 != -1) { int tileObjX; int tileObjY; getTileXY(tileId2, &tileObjX, &tileObjY, TILESET_WIDTH); tileRect.x = tileObjX * tileSizeX; tileRect.y = tileObjY * tileSizeY; SDL_RenderCopy(renderer, tileset, &tileRect, &rect); } } } // //draw debug rect // if (input_show_debug_rect) { // SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); // for (int y = 0; y < worldMap.GetHeight(); y++) { // for (int x = 0; x < worldMap.GetWidth(); x++) { // SDL_Rect rect; // rect.w = tileSizeX; // rect.h = tileWidth; // // mapToOrtho(x,y,rect.x, rect.y); // zoom(rect); // worldToScreen(rect.x, rect.y); // // SDL_RenderDrawRect(renderer, &rect); // } // } // SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); // } //show editor bar for (int i = 0; i < NB_TILES_EDITOR; i++) { SDL_Rect tileRect; //in tileset coords tileRect.w = tileSizeX; tileRect.h = tileSizeY; const int tileId = TILES_EDITOR[i]; int tileX; int tileY; getTileXY(tileId, &tileX, &tileY, TILESET_WIDTH); tileRect.x = tileX * tileSizeX; tileRect.y = tileY * tileSizeY; SDL_Rect rect; //in world coords rect.w = tileSizeX; rect.h = tileSizeY; rect.x = i * tileSizeX; rect.y = WIN_HEIGHT - tileSizeY; SDL_RenderCopy(renderer, tileset, &tileRect, &rect); } if (input_show_debug_rect) { SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); for (int x = 0; x < worldMap.GetWidth() + 1; x++) { int x1 = x; int y1 = 0; int x2 = x; int y2 = worldMap.GetHeight(); int tx1; int tx2; int ty1; int ty2; mapToIso(x1, y1, tx1, ty1); mapToIso(x2, y2, tx2, ty2); x1 = tx1; x2 = tx2; y1 = ty1; y2 = ty2; x1 += tileSizeX / 2; x2 += tileSizeX / 2; x1 *= input_scale; x2 *= input_scale; y1 *= input_scale; y2 *= input_scale; worldToScreen(x1, y1); worldToScreen(x2, y2); SDL_RenderDrawLine(renderer, x1, y1, x2, y2); } for (int y = 0; y < worldMap.GetHeight() + 1; y++) { int x1 = 0; int y1 = y; int x2 = worldMap.GetWidth(); int y2 = y; int tx1; int tx2; int ty1; int ty2; mapToIso(x1, y1, tx1, ty1); mapToIso(x2, y2, tx2, ty2); x1 = tx1; x2 = tx2; y1 = ty1; y2 = ty2; x1 += tileSizeX / 2; x2 += tileSizeX / 2; x1 *= input_scale; x2 *= input_scale; y1 *= input_scale; y2 *= input_scale; worldToScreen(x1, y1); worldToScreen(x2, y2); SDL_RenderDrawLine(renderer, x1, y1, x2, y2); } SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); } }