Advertisement
gustavoblaze

Hover Tile

May 25th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.32 KB | None | 0 0
  1. //Mapview.cpp
  2. void MapView::draw(const Rect& rect)
  3. {
  4.     // update visible tiles cache when needed
  5.     if(m_mustUpdateVisibleTilesCache || m_updateTilesPos > 0)
  6.         updateVisibleTilesCache(m_mustUpdateVisibleTilesCache ? 0 : m_updateTilesPos);
  7.  
  8.     float scaleFactor = m_tileSize/(float)Otc::TILE_PIXELS;
  9.     Position cameraPosition = getCameraPosition();
  10.  
  11.     int drawFlags = 0;
  12.     // First branch:
  13.     // This is unlikely to be false because a lot of us
  14.     // don't wanna hear their GPU fan while playing a
  15.     // 2D game.
  16.     //
  17.     // Second & Third branch:
  18.     // This is likely to be true since not many people have
  19.     // low-end graphics cards.
  20.     if(unlikely(g_map.isForcingAnimations()) || (likely(g_map.isShowingAnimations()) && m_viewMode == NEAR_VIEW))
  21.         drawFlags = Otc::DrawAnimations;
  22.  
  23.     if(m_viewMode == NEAR_VIEW)
  24.         drawFlags |= Otc::DrawGround | Otc::DrawGroundBorders | Otc::DrawWalls |
  25.                     Otc::DrawItems | Otc::DrawCreatures | Otc::DrawEffects | Otc::DrawMissiles;
  26.     else
  27.         drawFlags |= Otc::DrawGround | Otc::DrawGroundBorders | Otc::DrawWalls | Otc::DrawItems;
  28.  
  29.     if(m_mustDrawVisibleTilesCache || (drawFlags & Otc::DrawAnimations)) {
  30.         m_framebuffer->bind();
  31.  
  32.         if(m_mustCleanFramebuffer) {
  33.             Rect clearRect = Rect(0, 0, m_drawDimension * m_tileSize);
  34.             g_painter->setColor(Color::black);
  35.             g_painter->drawFilledRect(clearRect);
  36.  
  37.             if(m_drawLights) {
  38.                 m_lightView->reset();
  39.                 m_lightView->resize(m_framebuffer->getSize());
  40.  
  41.                 Light ambientLight;
  42.                 if(cameraPosition.z <= Otc::SEA_FLOOR) {
  43.                     ambientLight = g_map.getLight();
  44.                 } else {
  45.                     ambientLight.color = 215;
  46.                     ambientLight.intensity = 0;
  47.                 }
  48.                 ambientLight.intensity = std::max<int>(m_minimumAmbientLight*255, ambientLight.intensity);
  49.                 m_lightView->setGlobalLight(ambientLight);
  50.             }
  51.         }
  52.         g_painter->setColor(Color::white);
  53.  
  54.         auto it = m_cachedVisibleTiles.begin();
  55.         auto end = m_cachedVisibleTiles.end();
  56.  
  57.         Position hoverPos = g_map.getHoveredPosition();
  58.         Point hoverDest;
  59.         bool isHovered = false;
  60.  
  61.         for(int z=m_cachedLastVisibleFloor;z>=m_cachedFirstVisibleFloor;--z) {
  62.             clearFloorEffect();
  63.             while(it != end) {
  64.                 const TilePtr& tile = *it;
  65.                 Position tilePos = tile->getPosition();
  66.                 if(tilePos.z != z)
  67.                     break;
  68.                 else
  69.                     ++it;
  70.                 Point dest = transformPositionTo2D(tilePos, cameraPosition);
  71.  
  72.                 if(tilePos.x == hoverPos.x && tilePos.y == hoverPos.y && tilePos.z == hoverPos.z){
  73.                     isHovered = true;
  74.                     hoverDest = dest;
  75.                 }
  76.  
  77.                 if (g_map.isCovered(tilePos, m_cachedFirstVisibleFloor))
  78.                     tile->draw(dest, scaleFactor, drawFlags, this);
  79.                 else
  80.                     tile->draw(dest, scaleFactor, drawFlags, this, m_lightView.get());
  81.             }
  82.  
  83.             //draw spells
  84.             Position centralPosition = g_map.getCentralPosition();
  85.             bool animate = drawFlags & Otc::DrawAnimations;
  86.             if(drawFlags & Otc::DrawEffects) {
  87.                 for(const TilePtr& tile : m_tileWithEffect){
  88.                     Point dest = transformPositionTo2D(tile->getPosition(), cameraPosition);
  89.                     tile->drawTileEffect(dest, scaleFactor, drawFlags, centralPosition, animate, m_lightView.get());
  90.                 }
  91.             }
  92.  
  93.             if(drawFlags & Otc::DrawMissiles) {
  94.                 for(const MissilePtr& missile : g_map.getFloorMissiles(z)) {
  95.                     missile->draw(transformPositionTo2D(missile->getPosition(), cameraPosition), scaleFactor, drawFlags & Otc::DrawAnimations, m_lightView.get());
  96.                 }
  97.             }
  98.         }
  99.  
  100.  
  101.         TexturePtr hoverTexture = g_map.getHoverTerxutre();
  102.  
  103.         if(isHovered && hoverTexture && g_map.canDrawTileAnimation()){
  104.             int frames = 21;
  105.             int ticksPerFrame = 400 / frames;
  106.             int animationPhase = ((g_clock.millis() % (ticksPerFrame * frames)) / ticksPerFrame);
  107.             Rect destRect(hoverDest, 32, 32);
  108.             Rect textureRect(0, animationPhase*32, 32, 32);
  109.             g_painter->drawTexturedRect(destRect, hoverTexture, textureRect);
  110.         }
  111.  
  112.         m_framebuffer->release();
  113.  
  114.         // generating mipmaps each frame can be slow in older cards
  115.         //m_framebuffer->getTexture()->buildHardwareMipmaps();
  116.  
  117.         m_mustDrawVisibleTilesCache = false;
  118.     }
  119.  
  120.  
  121.     float fadeOpacity = 1.0f;
  122.     if(!m_shaderSwitchDone && m_fadeOutTime > 0) {
  123.         fadeOpacity = 1.0f - (m_fadeTimer.timeElapsed() / m_fadeOutTime);
  124.         if(fadeOpacity < 0.0f) {
  125.             m_shader = m_nextShader;
  126.             m_nextShader = nullptr;
  127.             m_shaderSwitchDone = true;
  128.             m_fadeTimer.restart();
  129.         }
  130.     }
  131.  
  132.     if(m_shaderSwitchDone && m_shader && m_fadeInTime > 0)
  133.         fadeOpacity = std::min<float>(m_fadeTimer.timeElapsed() / m_fadeInTime, 1.0f);
  134.  
  135.     Rect srcRect = calcFramebufferSource(rect.size());
  136.     Point drawOffset = srcRect.topLeft();
  137.  
  138.     if(m_shader && g_painter->hasShaders() && g_graphics.shouldUseShaders() && m_viewMode == NEAR_VIEW) {
  139.         Rect framebufferRect = Rect(0,0, m_drawDimension * m_tileSize);
  140.         Point center = srcRect.center();
  141.         Point globalCoord = Point(cameraPosition.x - m_drawDimension.width()/2, -(cameraPosition.y - m_drawDimension.height()/2)) * m_tileSize;
  142.         m_shader->bind();
  143.         m_shader->setUniformValue(ShaderManager::MAP_CENTER_COORD, center.x / (float)framebufferRect.width(), 1.0f - center.y / (float)framebufferRect.height());
  144.         m_shader->setUniformValue(ShaderManager::MAP_GLOBAL_COORD, globalCoord.x / (float)framebufferRect.height(), globalCoord.y / (float)framebufferRect.height());
  145.         m_shader->setUniformValue(ShaderManager::MAP_ZOOM, scaleFactor);
  146.         g_painter->setShaderProgram(m_shader);
  147.     }
  148.  
  149.     g_painter->setColor(Color::white);
  150.     g_painter->setOpacity(fadeOpacity);
  151.     glDisable(GL_BLEND);
  152. #if 0
  153.     // debug source area
  154.     g_painter->saveAndResetState();
  155.     m_framebuffer->bind();
  156.     g_painter->setColor(Color::green);
  157.     g_painter->drawBoundingRect(srcRect, 2);
  158.     m_framebuffer->release();
  159.     g_painter->restoreSavedState();
  160.     m_framebuffer->draw(rect);
  161. #else
  162.     m_framebuffer->draw(rect, srcRect);
  163. #endif
  164.     g_painter->resetShaderProgram();
  165.     g_painter->resetOpacity();
  166.     glEnable(GL_BLEND);
  167.  
  168.  
  169.     // this could happen if the player position is not known yet
  170.     if(!cameraPosition.isValid())
  171.         return;
  172.  
  173.     float horizontalStretchFactor = rect.width() / (float)srcRect.width();
  174.     float verticalStretchFactor = rect.height() / (float)srcRect.height();
  175.  
  176.     // avoid drawing texts on map in far zoom outs
  177.     if(m_viewMode == NEAR_VIEW) {
  178.         for(const CreaturePtr& creature : m_cachedFloorVisibleCreatures) {
  179.             if(!creature->canBeSeen())
  180.                 continue;
  181.  
  182.             PointF jumpOffset = creature->getJumpOffset() * scaleFactor;
  183.             Point creatureOffset = Point(16 - creature->getDisplacementX(), - creature->getDisplacementY() - 2);
  184.             Position pos = creature->getPosition();
  185.             Point p = transformPositionTo2D(pos, cameraPosition) - drawOffset;
  186.             p += (creature->getDrawOffset() + creatureOffset) * scaleFactor - Point(stdext::round(jumpOffset.x), stdext::round(jumpOffset.y));
  187.             p.x = p.x * horizontalStretchFactor;
  188.             p.y = p.y * verticalStretchFactor;
  189.             p += rect.topLeft();
  190.  
  191.             int flags = 0;
  192.             if(m_drawNames){ flags = Otc::DrawNames; }
  193.             if(m_drawHealthBars) { flags |= Otc::DrawBars; }
  194.             creature->drawInformation(p, g_map.isCovered(pos, m_cachedFirstVisibleFloor), rect, flags);
  195.         }
  196.     }
  197.  
  198.     // lights are drawn after names and before texts
  199.     if(m_drawLights)
  200.         m_lightView->draw(rect, srcRect);
  201.  
  202.     if(m_viewMode == NEAR_VIEW && m_drawTexts) {
  203.         for(const StaticTextPtr& staticText : g_map.getStaticTexts()) {
  204.             Position pos = staticText->getPosition();
  205.  
  206.             // ony draw static texts from current camera floor, unless yells
  207.             //if(pos.z != cameraPosition.z && !staticText->isYell())
  208.             //    continue;
  209.  
  210.             if(pos.z != cameraPosition.z && staticText->getMessageMode() == Otc::MessageNone)
  211.                 continue;
  212.  
  213.             Point p = transformPositionTo2D(pos, cameraPosition) - drawOffset;
  214.             p.x = p.x * horizontalStretchFactor;
  215.             p.y = p.y * verticalStretchFactor;
  216.             p += rect.topLeft();
  217.             staticText->drawText(p, rect);
  218.         }
  219.  
  220.         for(const AnimatedTextPtr& animatedText : g_map.getAnimatedTexts()) {
  221.             Position pos = animatedText->getPosition();
  222.  
  223.             /*
  224.             // only draw animated texts from visible floors
  225.             if(pos.z < m_cachedFirstVisibleFloor || pos.z > m_cachedLastVisibleFloor)
  226.                 continue;
  227.  
  228.             // dont draw animated texts from covered tiles
  229.             if(pos.z != cameraPosition.z && g_map.isCovered(pos, m_cachedFirstVisibleFloor))
  230.                 continue;
  231.             */
  232.             if(pos.z != cameraPosition.z)
  233.                 continue;
  234.  
  235.             Point p = transformPositionTo2D(pos, cameraPosition) - drawOffset;
  236.             p.x = p.x * horizontalStretchFactor;
  237.             p.y = p.y * verticalStretchFactor;
  238.             p += rect.topLeft();
  239.             animatedText->drawText(p, rect);
  240.         }
  241.     }
  242. }
  243. //map.h
  244.     Position getHoveredPosition();
  245.     bool installSqmTexture(const std::string &path){
  246.         m_hoverTexture = g_textures.getTexture(path);
  247.         return m_hoverTexture == nullptr ? false : true;
  248.     }
  249.     TexturePtr getHoverTerxutre() { return m_hoverTexture;}
  250.     void registerUiMapPtr(const UIMapPtr& ptr){m_uiMapPtr = ptr;};
  251.     void unregisterUiMapPtr(){m_uiMapPtr = nullptr;}
  252.     bool canDrawTileAnimation(){ return m_drawTileAnimation;}
  253.     void drawTileAnimation(bool v){ m_drawTileAnimation = v;}
  254.  
  255. //map.cpp
  256. Position Map::getHoveredPosition(){
  257.     Position hoveredPosition;
  258.     if (!m_uiMapPtr){
  259.         return hoveredPosition;
  260.     }
  261.  
  262.     TilePtr tile = m_uiMapPtr->getTile(g_window.getMousePosition());
  263.     if (!tile){
  264.         return hoveredPosition;
  265.     }
  266.  
  267.     hoveredPosition = tile->getPosition();
  268.  
  269.     return hoveredPosition;
  270. }
  271.  
  272. //modules/game_interface/uigamemap.lua
  273. function UIGameMap.create()
  274.   local gameMap = UIGameMap.internalCreate()
  275.   gameMap:setKeepAspectRatio(true)
  276.   gameMap:setVisibleDimension({width = 15, height = 11})
  277.   gameMap:setDrawLights(true)
  278.   g_map.registerUiMapPtr(gameMap)
  279.   return gameMap
  280. end
  281.  
  282. //gameinterface.lua
  283. function terminate()
  284.   hide()
  285.  
  286.   hookedMenuOptions = {}
  287.  
  288.   stopSmartWalk()
  289.  
  290.   disconnect(g_game, {
  291.     onGameStart = onGameStart,
  292.     onGameEnd = onGameEnd,
  293.     onLoginAdvice = onLoginAdvice
  294.   })
  295.  
  296.   disconnect(gameLeftPanel, { onVisibilityChange = onLeftPanelVisibilityChange })
  297.  
  298.   logoutButton:destroy()
  299.   gameRootPanel:destroy()
  300.   g_map.unregisterUiMapPtr()
  301. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement