Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Mapview.cpp
- void MapView::draw(const Rect& rect)
- {
- // update visible tiles cache when needed
- if(m_mustUpdateVisibleTilesCache || m_updateTilesPos > 0)
- updateVisibleTilesCache(m_mustUpdateVisibleTilesCache ? 0 : m_updateTilesPos);
- float scaleFactor = m_tileSize/(float)Otc::TILE_PIXELS;
- Position cameraPosition = getCameraPosition();
- int drawFlags = 0;
- // First branch:
- // This is unlikely to be false because a lot of us
- // don't wanna hear their GPU fan while playing a
- // 2D game.
- //
- // Second & Third branch:
- // This is likely to be true since not many people have
- // low-end graphics cards.
- if(unlikely(g_map.isForcingAnimations()) || (likely(g_map.isShowingAnimations()) && m_viewMode == NEAR_VIEW))
- drawFlags = Otc::DrawAnimations;
- if(m_viewMode == NEAR_VIEW)
- drawFlags |= Otc::DrawGround | Otc::DrawGroundBorders | Otc::DrawWalls |
- Otc::DrawItems | Otc::DrawCreatures | Otc::DrawEffects | Otc::DrawMissiles;
- else
- drawFlags |= Otc::DrawGround | Otc::DrawGroundBorders | Otc::DrawWalls | Otc::DrawItems;
- if(m_mustDrawVisibleTilesCache || (drawFlags & Otc::DrawAnimations)) {
- m_framebuffer->bind();
- if(m_mustCleanFramebuffer) {
- Rect clearRect = Rect(0, 0, m_drawDimension * m_tileSize);
- g_painter->setColor(Color::black);
- g_painter->drawFilledRect(clearRect);
- if(m_drawLights) {
- m_lightView->reset();
- m_lightView->resize(m_framebuffer->getSize());
- Light ambientLight;
- if(cameraPosition.z <= Otc::SEA_FLOOR) {
- ambientLight = g_map.getLight();
- } else {
- ambientLight.color = 215;
- ambientLight.intensity = 0;
- }
- ambientLight.intensity = std::max<int>(m_minimumAmbientLight*255, ambientLight.intensity);
- m_lightView->setGlobalLight(ambientLight);
- }
- }
- g_painter->setColor(Color::white);
- auto it = m_cachedVisibleTiles.begin();
- auto end = m_cachedVisibleTiles.end();
- Position hoverPos = g_map.getHoveredPosition();
- Point hoverDest;
- bool isHovered = false;
- for(int z=m_cachedLastVisibleFloor;z>=m_cachedFirstVisibleFloor;--z) {
- clearFloorEffect();
- while(it != end) {
- const TilePtr& tile = *it;
- Position tilePos = tile->getPosition();
- if(tilePos.z != z)
- break;
- else
- ++it;
- Point dest = transformPositionTo2D(tilePos, cameraPosition);
- if(tilePos.x == hoverPos.x && tilePos.y == hoverPos.y && tilePos.z == hoverPos.z){
- isHovered = true;
- hoverDest = dest;
- }
- if (g_map.isCovered(tilePos, m_cachedFirstVisibleFloor))
- tile->draw(dest, scaleFactor, drawFlags, this);
- else
- tile->draw(dest, scaleFactor, drawFlags, this, m_lightView.get());
- }
- //draw spells
- Position centralPosition = g_map.getCentralPosition();
- bool animate = drawFlags & Otc::DrawAnimations;
- if(drawFlags & Otc::DrawEffects) {
- for(const TilePtr& tile : m_tileWithEffect){
- Point dest = transformPositionTo2D(tile->getPosition(), cameraPosition);
- tile->drawTileEffect(dest, scaleFactor, drawFlags, centralPosition, animate, m_lightView.get());
- }
- }
- if(drawFlags & Otc::DrawMissiles) {
- for(const MissilePtr& missile : g_map.getFloorMissiles(z)) {
- missile->draw(transformPositionTo2D(missile->getPosition(), cameraPosition), scaleFactor, drawFlags & Otc::DrawAnimations, m_lightView.get());
- }
- }
- }
- TexturePtr hoverTexture = g_map.getHoverTerxutre();
- if(isHovered && hoverTexture && g_map.canDrawTileAnimation()){
- int frames = 21;
- int ticksPerFrame = 400 / frames;
- int animationPhase = ((g_clock.millis() % (ticksPerFrame * frames)) / ticksPerFrame);
- Rect destRect(hoverDest, 32, 32);
- Rect textureRect(0, animationPhase*32, 32, 32);
- g_painter->drawTexturedRect(destRect, hoverTexture, textureRect);
- }
- m_framebuffer->release();
- // generating mipmaps each frame can be slow in older cards
- //m_framebuffer->getTexture()->buildHardwareMipmaps();
- m_mustDrawVisibleTilesCache = false;
- }
- float fadeOpacity = 1.0f;
- if(!m_shaderSwitchDone && m_fadeOutTime > 0) {
- fadeOpacity = 1.0f - (m_fadeTimer.timeElapsed() / m_fadeOutTime);
- if(fadeOpacity < 0.0f) {
- m_shader = m_nextShader;
- m_nextShader = nullptr;
- m_shaderSwitchDone = true;
- m_fadeTimer.restart();
- }
- }
- if(m_shaderSwitchDone && m_shader && m_fadeInTime > 0)
- fadeOpacity = std::min<float>(m_fadeTimer.timeElapsed() / m_fadeInTime, 1.0f);
- Rect srcRect = calcFramebufferSource(rect.size());
- Point drawOffset = srcRect.topLeft();
- if(m_shader && g_painter->hasShaders() && g_graphics.shouldUseShaders() && m_viewMode == NEAR_VIEW) {
- Rect framebufferRect = Rect(0,0, m_drawDimension * m_tileSize);
- Point center = srcRect.center();
- Point globalCoord = Point(cameraPosition.x - m_drawDimension.width()/2, -(cameraPosition.y - m_drawDimension.height()/2)) * m_tileSize;
- m_shader->bind();
- m_shader->setUniformValue(ShaderManager::MAP_CENTER_COORD, center.x / (float)framebufferRect.width(), 1.0f - center.y / (float)framebufferRect.height());
- m_shader->setUniformValue(ShaderManager::MAP_GLOBAL_COORD, globalCoord.x / (float)framebufferRect.height(), globalCoord.y / (float)framebufferRect.height());
- m_shader->setUniformValue(ShaderManager::MAP_ZOOM, scaleFactor);
- g_painter->setShaderProgram(m_shader);
- }
- g_painter->setColor(Color::white);
- g_painter->setOpacity(fadeOpacity);
- glDisable(GL_BLEND);
- #if 0
- // debug source area
- g_painter->saveAndResetState();
- m_framebuffer->bind();
- g_painter->setColor(Color::green);
- g_painter->drawBoundingRect(srcRect, 2);
- m_framebuffer->release();
- g_painter->restoreSavedState();
- m_framebuffer->draw(rect);
- #else
- m_framebuffer->draw(rect, srcRect);
- #endif
- g_painter->resetShaderProgram();
- g_painter->resetOpacity();
- glEnable(GL_BLEND);
- // this could happen if the player position is not known yet
- if(!cameraPosition.isValid())
- return;
- float horizontalStretchFactor = rect.width() / (float)srcRect.width();
- float verticalStretchFactor = rect.height() / (float)srcRect.height();
- // avoid drawing texts on map in far zoom outs
- if(m_viewMode == NEAR_VIEW) {
- for(const CreaturePtr& creature : m_cachedFloorVisibleCreatures) {
- if(!creature->canBeSeen())
- continue;
- PointF jumpOffset = creature->getJumpOffset() * scaleFactor;
- Point creatureOffset = Point(16 - creature->getDisplacementX(), - creature->getDisplacementY() - 2);
- Position pos = creature->getPosition();
- Point p = transformPositionTo2D(pos, cameraPosition) - drawOffset;
- p += (creature->getDrawOffset() + creatureOffset) * scaleFactor - Point(stdext::round(jumpOffset.x), stdext::round(jumpOffset.y));
- p.x = p.x * horizontalStretchFactor;
- p.y = p.y * verticalStretchFactor;
- p += rect.topLeft();
- int flags = 0;
- if(m_drawNames){ flags = Otc::DrawNames; }
- if(m_drawHealthBars) { flags |= Otc::DrawBars; }
- creature->drawInformation(p, g_map.isCovered(pos, m_cachedFirstVisibleFloor), rect, flags);
- }
- }
- // lights are drawn after names and before texts
- if(m_drawLights)
- m_lightView->draw(rect, srcRect);
- if(m_viewMode == NEAR_VIEW && m_drawTexts) {
- for(const StaticTextPtr& staticText : g_map.getStaticTexts()) {
- Position pos = staticText->getPosition();
- // ony draw static texts from current camera floor, unless yells
- //if(pos.z != cameraPosition.z && !staticText->isYell())
- // continue;
- if(pos.z != cameraPosition.z && staticText->getMessageMode() == Otc::MessageNone)
- continue;
- Point p = transformPositionTo2D(pos, cameraPosition) - drawOffset;
- p.x = p.x * horizontalStretchFactor;
- p.y = p.y * verticalStretchFactor;
- p += rect.topLeft();
- staticText->drawText(p, rect);
- }
- for(const AnimatedTextPtr& animatedText : g_map.getAnimatedTexts()) {
- Position pos = animatedText->getPosition();
- /*
- // only draw animated texts from visible floors
- if(pos.z < m_cachedFirstVisibleFloor || pos.z > m_cachedLastVisibleFloor)
- continue;
- // dont draw animated texts from covered tiles
- if(pos.z != cameraPosition.z && g_map.isCovered(pos, m_cachedFirstVisibleFloor))
- continue;
- */
- if(pos.z != cameraPosition.z)
- continue;
- Point p = transformPositionTo2D(pos, cameraPosition) - drawOffset;
- p.x = p.x * horizontalStretchFactor;
- p.y = p.y * verticalStretchFactor;
- p += rect.topLeft();
- animatedText->drawText(p, rect);
- }
- }
- }
- //map.h
- Position getHoveredPosition();
- bool installSqmTexture(const std::string &path){
- m_hoverTexture = g_textures.getTexture(path);
- return m_hoverTexture == nullptr ? false : true;
- }
- TexturePtr getHoverTerxutre() { return m_hoverTexture;}
- void registerUiMapPtr(const UIMapPtr& ptr){m_uiMapPtr = ptr;};
- void unregisterUiMapPtr(){m_uiMapPtr = nullptr;}
- bool canDrawTileAnimation(){ return m_drawTileAnimation;}
- void drawTileAnimation(bool v){ m_drawTileAnimation = v;}
- //map.cpp
- Position Map::getHoveredPosition(){
- Position hoveredPosition;
- if (!m_uiMapPtr){
- return hoveredPosition;
- }
- TilePtr tile = m_uiMapPtr->getTile(g_window.getMousePosition());
- if (!tile){
- return hoveredPosition;
- }
- hoveredPosition = tile->getPosition();
- return hoveredPosition;
- }
- //modules/game_interface/uigamemap.lua
- function UIGameMap.create()
- local gameMap = UIGameMap.internalCreate()
- gameMap:setKeepAspectRatio(true)
- gameMap:setVisibleDimension({width = 15, height = 11})
- gameMap:setDrawLights(true)
- g_map.registerUiMapPtr(gameMap)
- return gameMap
- end
- //gameinterface.lua
- function terminate()
- hide()
- hookedMenuOptions = {}
- stopSmartWalk()
- disconnect(g_game, {
- onGameStart = onGameStart,
- onGameEnd = onGameEnd,
- onLoginAdvice = onLoginAdvice
- })
- disconnect(gameLeftPanel, { onVisibilityChange = onLeftPanelVisibilityChange })
- logoutButton:destroy()
- gameRootPanel:destroy()
- g_map.unregisterUiMapPtr()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement