Day09_OpenTTD的Viewport介绍

返回目录

OpenTTD的视觉系统2.5D形式展现,素材都是图片,通过屏幕贴图的方式生成界面元素,因此不能像真3D游戏那样在游戏过程中旋转视角,看到3D模型侧面或者背面的情况。这种2.5D的展示方式对显卡要求较低,大量使用在手机游戏里面,比如 王者荣耀、部落冲突、三国志策略版的界面,我们都能看到这种显示模式的身影。Openttd.cpp 代码量不大(行数 3000+),函数数量多(50+),其中对外服务函数26个,私有服务 27个。上层调用者主要包括 gui类、cmd类以及游戏初始化阶段的各种类,向下调用 gfx.cpp set dirty(把屏幕部分或者全部设置刷新) , Draw开头的绘制等操作,下面是整理出来的主要函数调用关系及说明。
viewport是游戏进行中的主界面,我们以铺设铁轨为例,修建铁路相关的界面在rail_gui.cpp定义,操作流程:
1.点开toolbar的铁路菜单栏;
2.选择修建铁路图标;
3.鼠标左键拖动修建铁路;
4.鼠标拖动过程中,界面对应Tile对应显示铁轨;
5.鼠标左键抬起,完成修建动作;
请添加图片描述

以上3-5步骤分别由 viewport.cpp的三个函数实现

VpStartPlaceSizing() 开始拖动
<- rail_gui.cpp OnPlaceObject()
<- viewport.cpp PlaceObject()
<- viewport.cpp VpStartDragging() 拖动中
VpHandlePlaceSizingDrag() 拖动完成
<- rail_gui.cpp OnPlaceMouseUp()

通过上面的分析,我们可以看到,实现拖动架设铁路的操作是由 viewport.cpp 与 rail_gui.cpp 几个函数之间互相调用实现

铺设铁路调用链
说明:
a<-b 表示函数a被函数b调用
<= 后面是这个函数的说明
格式:<-(第一行没有) 源码文件名 函数名 <= 函数说明

rail_gui.cpp DoRailroadTrack() <= 调用DoCommandP()
<- HandleAutodirPlacement() <= select_proc=DDSP_PLACE_RAIL
<- OnPlaceMouseUp() <= select_proc=DDSP_PLACE_RAIL
<- PlaceRail_Bridge() <= Start placing a rail bridge.
<- OnPlaceObject() <= this->last_user_action=WID_RAT_BUILD_BRIDGE
<- viewport.cpp <= PlaceObject()
<- HandleViewportClicked()
<- window.cpp MouseLoop() <= 每一个tick
<- HandleMouseEvents()
<- sdl2_v.cpp VideoDriver_SDL_Base::PollEvent()
<- video_driver.cpp VideoDriver::Tick()

最后,是viewport.cpp的函数清单
说明:
a() <- b() 表示函数a被 函数b调用
: 后面是这个函数的中英文说明
*3 表示这是需要重要关注的函数

----public----

  1. StartSpriteCombine() <- *_cmd.cpp 一般是draw操作前调用
    : Starts a block of sprites, which are “combined” into a single bounding box.
  2. EndSpriteCombine() <- *_cmd.cpp 与上面成对出现
    : Terminates a block of sprites started by #StartSpriteCombine.
    *3. ViewportDoDraw() <- screenshot.cpp LargeWorldCallback() & this.ViewportDraw()
    : 调用 ViewportDraw 开头的几个私有函数完成ViewPort的绘制
  3. DrawViewport() <- widget.cpp NWidgetViewport::Draw()
    : Draw the viewport of this window.
    : 在窗口的小组件widget绘制的时候,直接调用 ViewportDraw() 间接调用 ViewportDoDraw()
  4. ViewportAddString() <- texteff.cpp DrawTextEffects() & this.ViewportAddKdtreeSigns()
    : Add a string to draw in the viewport
    : 界面上增加字符串
    ------六个Make开头的函数 被 cmd类程序发起
  5. MakeStation() <- station_map.h 创建车站、码头、机场等Tile & this.RebuildViewportKdtree()
  6. MakeWaypoint() <- waypoint_cmd.cpp Waypoint::UpdateVirtCoord() & this.RebuildViewportKdtree()
  7. MakeTown() <- town_cmd.cpp Town::UpdateVirtCoord() & this.RebuildViewportKdtree()
  8. MakeSign() <- signs.cpp Sign::UpdateVirtCoord() & this.RebuildViewportKdtree()
    ------
  9. RebuildViewportKdtree() <- afterload.cpp AfterLoadGame() UpdateAllVirtCoords() & misc.cpp InitializeGame()
  10. ScrollWindowTo() <- main_gui.cpp ZoomInOrOutToCursorWindow() & smallmap_gui.cpp ScrollMainWindowTo()
    : Scrolls the viewport in a window to a given location.
  11. ScrollWindowToTile() <- *_gui.cpp OnResize() 包括 industry/town/waypoint
    : Scrolls the viewport in a window to a given location.
  12. ScrollMainWindowToTile() <- *_gui.cpp 大量调用这个方法 跳转视角
    : Scrolls the viewport of the main window to a given location.
  13. UpdateTileSelection() <- window.cpp MouseLoop() & station_gui.cpp StationJoinerNeeded()
    : Updates tile highlighting for all cases.
  14. VpSelectTilesWithMethod() <- *_gui.cpp OnPlaceDrag()
    : Selects tiles while dragging
  15. VpHandlePlaceSizingDrag() <- window.cpp MouseLoop()
    : Handle the mouse while dragging for placement/resizing.
  16. SetObjectToPlaceWnd() <- *_gui.cpp OnClick()
    : 调用SetObjectToPlace()
  17. SetObjectToPlace() <- genworld.cpp GenerateWorld() & *_gui.cpp OnClick()
    : Change the cursor and mouse click/drag handling
  18. ResetObjectToPlace()
    : Reset the cursor and mouse mode handling back to default
    : 调用SetObjectToPlace()
  19. GetViewportStationMiddle() <- linkgraph_gui.cpp GetStationMiddle()
  20. CmdScrollViewport(): Scroll players main viewport.
  21. SetViewportCatchmentStation():
  22. SetViewportCatchmentTown(): Town’s coverage set dirty

----init----

  1. InitializeSpriteSorter() <- openttd.cpp main()
  2. InitializeWindowViewport() <- widget.cpp NWidgetViewport::InitializeViewport()
    : 很多 gui 创建新窗口的时候调用

----private----

  1. MarkCatchmentTilesDirty() <- SetViewportCatchmentStation()
  2. CalcRaildirsDrawstyle() <- VpSelectTilesWithMethod()
    : while dragging
  3. DoSetViewportPosition() <- SetViewportPosition()
  4. SetViewportPosition() <- UpdateViewportPosition()
    *5. TranslateXYToTileCoord() <- GetTileFromScreenXY() & CheckClickOnLandscape()
    : Translate screen coordinate in a viewport to underlying tile coordinate.
    : x,y的坐标转换为3D的透视图,调用 landscape.cpp InverseRemapCoords2() 完成转换
  5. GetTileFromScreenXY() <- GetTileBelowCursor() & GetTileZoomCenterWindow()
    : When used for zooming, check area below current coordinates (x,y)
  6. AddTileSpriteToDraw() <- DrawGroundSpriteAt() & DrawSelectionSprite()
    : Schedules a tile sprite for drawing.
  7. AddChildSpriteToFoundation() <- DrawGroundSpriteAt() & DrawSelectionSprite()
    : Adds a child sprite to the active foundation.
  8. AddCombinedSprite() <- AddSortableSpriteToDraw()
    : Draw a (transparent) sprite at given coordinates with a given bounding box.
  9. AddStringToDraw() <- ViewportAddString()
  10. ViewportSortParentSprites() <- InitializeSpriteSorter()
    ------下面这一堆函数都被ViewportDoDraw()调用
  11. ViewportAddLandscape() <- ViewportDoDraw()
  12. ViewportDrawTileSprites() <- ViewportDoDraw()
  13. ViewportDrawParentSprites() <- ViewportDoDraw()
  14. ViewportDrawBoundingBoxes() <- ViewportDoDraw()
  15. ViewportDrawDirtyBlocks() <- ViewportDoDraw()
    : Draw/colour the blocks that have been redrawn.
  16. ViewportDrawStrings() <- ViewportDoDraw()
    ------上面这一堆函数都被ViewportDoDraw()调用
  17. ClampViewportToMap() <- UpdateViewportPosition()
    : Ensure that a given viewport has a valid scroll position.
  18. SetSelectionTilesDirty() <- SetSelectionRed() & UpdateTileSelection() & SetObjectToPlace()
    : Marks the selected tiles as dirty.
  19. UpdateViewportPosition() <- window.cpp UpdateWindows()
    : Update the viewport position being displayed.
  20. CheckClickOnViewportSign() <- HandleViewportClicked()
    : Check whether any viewport sign was clicked, and dispatch the click.
  21. CheckClickOnLandscape() <- HandleViewportClicked()
  22. PlaceObject() <- HandleViewportClicked()
  23. HandleViewportClicked() <- window.cpp MouseLoop()
    : 点击鼠标左键的事件响应
  24. ViewportDraw() <- Window::DrawViewport()
    : Draw the viewport of this window.
  25. RebuildViewportOverlay() <- ScrollWindowTo()
    : 区域 SetDirty()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值