# 分层架构

> CrossFly 的思维导图 —— app → ui → core → gl → libGDX —— 以及为什么存在这种分层。

Source: https://crossfly.dev/zh/docs/concepts/architecture — CrossFly documentation. Full docs as one file: https://crossfly.dev/llms-full.txt

# 分层架构

要理解 CrossFly 的任何部分，先掌握整体地图会很有帮助。本质上，**CrossFly 就是一个 libGDX 的
`ApplicationListener`** —— libGDX 通过这个单一入口来创建、调整大小并渲染应用。围绕这一点，框架被
组织成若干层。

<div class="cf-diagram">
<svg role="img" id="my-svg" width="525" height="587" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" class="flowchart" style="max-width: 524.828px; background-color: transparent;" viewBox="0 0 524.828125 587" role="graphics-document document" aria-roledescription="flowchart-v2"><style>#my-svg{font-family:Manrope,system-ui,sans-serif;font-size:14px;fill:var(--text);}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#my-svg .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#my-svg .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#my-svg .error-icon{fill:var(--bg-elev);}#my-svg .error-text{fill:var(--text-dim);stroke:var(--text-dim);}#my-svg .edge-thickness-normal{stroke-width:1px;}#my-svg .edge-thickness-thick{stroke-width:3.5px;}#my-svg .edge-pattern-solid{stroke-dasharray:0;}#my-svg .edge-thickness-invisible{stroke-width:0;fill:none;}#my-svg .edge-pattern-dashed{stroke-dasharray:3;}#my-svg .edge-pattern-dotted{stroke-dasharray:2;}#my-svg .marker{fill:var(--text-dim);stroke:var(--text-dim);}#my-svg .marker.cross{stroke:var(--text-dim);}#my-svg svg{font-family:Manrope,system-ui,sans-serif;font-size:14px;}#my-svg p{margin:0;}#my-svg .label{font-family:Manrope,system-ui,sans-serif;color:var(--text);}#my-svg .cluster-label text{fill:var(--text-dim);}#my-svg .cluster-label span{color:var(--text-dim);}#my-svg .cluster-label span p{background-color:transparent;}#my-svg .label text,#my-svg span{fill:var(--text);color:var(--text);}#my-svg .node rect,#my-svg .node circle,#my-svg .node ellipse,#my-svg .node polygon,#my-svg .node path{fill:var(--bg-soft);stroke:var(--border-strong);stroke-width:1px;}#my-svg .rough-node .label text,#my-svg .node .label text,#my-svg .image-shape .label,#my-svg .icon-shape .label{text-anchor:middle;}#my-svg .node .katex path{fill:var(--text-dim);stroke:var(--text-dim);stroke-width:1px;}#my-svg .rough-node .label,#my-svg .node .label,#my-svg .image-shape .label,#my-svg .icon-shape .label{text-align:center;}#my-svg .node.clickable{cursor:pointer;}#my-svg .root .anchor path{fill:var(--text-dim)!important;stroke-width:0;stroke:var(--text-dim);}#my-svg .arrowheadPath{fill:rgba(255, 255, 255, 0);}#my-svg .edgePath .path{stroke:var(--text-dim);stroke-width:1px;}#my-svg .flowchart-link{stroke:var(--text-dim);fill:none;}#my-svg .edgeLabel{background-color:var(--bg-elev);text-align:center;}#my-svg .edgeLabel p{background-color:var(--bg-elev);}#my-svg .edgeLabel rect{opacity:0.5;background-color:var(--bg-elev);fill:var(--bg-elev);}#my-svg .labelBkg{background-color:rgba(255, 255, 255, 0.5);}#my-svg .cluster rect{fill:var(--bg-elev);stroke:hsl(0, 0%, 90%);stroke-width:1px;}#my-svg .cluster text{fill:var(--text-dim);}#my-svg .cluster span{color:var(--text-dim);}#my-svg div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:Manrope,system-ui,sans-serif;font-size:12px;background:var(--bg-elev);border:1px solid hsl(0, 0%, 90%);border-radius:2px;pointer-events:none;z-index:100;}#my-svg .flowchartTitleText{text-anchor:middle;font-size:18px;fill:var(--text);}#my-svg rect.text{fill:none;stroke-width:0;}#my-svg .icon-shape,#my-svg .image-shape{background-color:var(--bg-elev);text-align:center;}#my-svg .icon-shape p,#my-svg .image-shape p{background-color:var(--bg-elev);padding:2px;}#my-svg .icon-shape .label rect,#my-svg .image-shape .label rect{opacity:0.5;background-color:var(--bg-elev);fill:var(--bg-elev);}#my-svg .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#my-svg .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#my-svg .node .neo-node{stroke:var(--border-strong);}#my-svg [data-look="neo"].node rect,#my-svg [data-look="neo"].cluster rect,#my-svg [data-look="neo"].node polygon{stroke:url(#my-svg-gradient);filter:drop-shadow( 1px 2px 2px rgba(185,185,185,1));}#my-svg [data-look="neo"].swimlane.cluster rect{filter:none;}#my-svg [data-look="neo"].node path{stroke:url(#my-svg-gradient);stroke-width:1px;}#my-svg [data-look="neo"].node .outer-path{filter:drop-shadow( 1px 2px 2px rgba(185,185,185,1));}#my-svg [data-look="neo"].node .neo-line path{stroke:var(--border-strong);filter:none;}#my-svg [data-look="neo"].node circle{stroke:url(#my-svg-gradient);filter:drop-shadow( 1px 2px 2px rgba(185,185,185,1));}#my-svg [data-look="neo"].node circle .state-start{fill:var(--text-dim);}#my-svg [data-look="neo"].icon-shape .icon{fill:url(#my-svg-gradient);filter:drop-shadow( 1px 2px 2px rgba(185,185,185,1));}#my-svg [data-look="neo"].icon-shape .icon-neo path{stroke:url(#my-svg-gradient);filter:drop-shadow( 1px 2px 2px rgba(185,185,185,1));}#my-svg :root{--mermaid-font-family:Manrope,system-ui,sans-serif;}</style><g><marker id="my-svg_flowchart-v2-pointEnd" class="marker flowchart-v2" viewBox="0 0 10 10" refX="5" refY="5" markerUnits="userSpaceOnUse" markerWidth="8" markerHeight="8" orient="auto"><path d="M 0 0 L 10 5 L 0 10 z" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-pointStart" class="marker flowchart-v2" viewBox="0 0 10 10" refX="4.5" refY="5" markerUnits="userSpaceOnUse" markerWidth="8" markerHeight="8" orient="auto"><path d="M 0 5 L 10 10 L 10 0 z" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-pointEnd-margin" class="marker flowchart-v2" viewBox="0 0 11.5 14" refX="11.5" refY="7" markerUnits="userSpaceOnUse" markerWidth="10.5" markerHeight="14" orient="auto"><path d="M 0 0 L 11.5 7 L 0 14 z" class="arrowMarkerPath" style="stroke-width: 0; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-pointStart-margin" class="marker flowchart-v2" viewBox="0 0 11.5 14" refX="1" refY="7" markerUnits="userSpaceOnUse" markerWidth="11.5" markerHeight="14" orient="auto"><polygon points="0,7 11.5,14 11.5,0" class="arrowMarkerPath" style="stroke-width: 0; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-circleEnd" class="marker flowchart-v2" viewBox="0 0 10 10" refX="11" refY="5" markerUnits="userSpaceOnUse" markerWidth="11" markerHeight="11" orient="auto"><circle cx="5" cy="5" r="5" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-circleStart" class="marker flowchart-v2" viewBox="0 0 10 10" refX="-1" refY="5" markerUnits="userSpaceOnUse" markerWidth="11" markerHeight="11" orient="auto"><circle cx="5" cy="5" r="5" class="arrowMarkerPath" style="stroke-width: 1; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-circleEnd-margin" class="marker flowchart-v2" viewBox="0 0 10 10" refY="5" refX="12.25" markerUnits="userSpaceOnUse" markerWidth="14" markerHeight="14" orient="auto"><circle cx="5" cy="5" r="5" class="arrowMarkerPath" style="stroke-width: 0; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-circleStart-margin" class="marker flowchart-v2" viewBox="0 0 10 10" refX="-2" refY="5" markerUnits="userSpaceOnUse" markerWidth="14" markerHeight="14" orient="auto"><circle cx="5" cy="5" r="5" class="arrowMarkerPath" style="stroke-width: 0; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-crossEnd" class="marker cross flowchart-v2" viewBox="0 0 11 11" refX="12" refY="5.2" markerUnits="userSpaceOnUse" markerWidth="11" markerHeight="11" orient="auto"><path d="M 1,1 l 9,9 M 10,1 l -9,9" class="arrowMarkerPath" style="stroke-width: 2; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-crossStart" class="marker cross flowchart-v2" viewBox="0 0 11 11" refX="-1" refY="5.2" markerUnits="userSpaceOnUse" markerWidth="11" markerHeight="11" orient="auto"><path d="M 1,1 l 9,9 M 10,1 l -9,9" class="arrowMarkerPath" style="stroke-width: 2; stroke-dasharray: 1, 0;"/></marker><marker id="my-svg_flowchart-v2-crossEnd-margin" class="marker cross flowchart-v2" viewBox="0 0 15 15" refX="17.7" refY="7.5" markerUnits="userSpaceOnUse" markerWidth="12" markerHeight="12" orient="auto"><path d="M 1,1 L 14,14 M 1,14 L 14,1" class="arrowMarkerPath" style="stroke-width: 2.5;"/></marker><marker id="my-svg_flowchart-v2-crossStart-margin" class="marker cross flowchart-v2" viewBox="0 0 15 15" refX="-3.5" refY="7.5" markerUnits="userSpaceOnUse" markerWidth="12" markerHeight="12" orient="auto"><path d="M 1,1 L 14,14 M 1,14 L 14,1" class="arrowMarkerPath" style="stroke-width: 2.5; stroke-dasharray: 1, 0;"/></marker><g class="root"><g class="clusters"/><g class="edgePaths"><path d="M264.91,78L264.91,82.167C264.91,86.333,264.91,94.667,264.91,102.333C264.91,110,264.91,117,264.91,120.5L264.91,124" id="my-svg-L_App_UI_0" class="edge-thickness-normal edge-pattern-solid edge-thickness-normal edge-pattern-solid flowchart-link" style=";" data-edge="true" data-et="edge" data-id="L_App_UI_0" data-points="W3sieCI6MjY0LjkxMDE1NjI1LCJ5Ijo3OH0seyJ4IjoyNjQuOTEwMTU2MjUsInkiOjEwM30seyJ4IjoyNjQuOTEwMTU2MjUsInkiOjEyOH1d" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/><path d="M264.91,198L264.91,202.167C264.91,206.333,264.91,214.667,264.91,222.333C264.91,230,264.91,237,264.91,240.5L264.91,244" id="my-svg-L_UI_Core_0" class="edge-thickness-normal edge-pattern-solid edge-thickness-normal edge-pattern-solid flowchart-link" style=";" data-edge="true" data-et="edge" data-id="L_UI_Core_0" data-points="W3sieCI6MjY0LjkxMDE1NjI1LCJ5IjoxOTh9LHsieCI6MjY0LjkxMDE1NjI1LCJ5IjoyMjN9LHsieCI6MjY0LjkxMDE1NjI1LCJ5IjoyNDh9XQ==" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/><path d="M186.433,339L176.228,344.917C166.023,350.833,145.613,362.667,135.408,373.833C125.203,385,125.203,395.5,125.203,400.75L125.203,406" id="my-svg-L_Core_GL_0" class="edge-thickness-normal edge-pattern-solid edge-thickness-normal edge-pattern-solid flowchart-link" style=";" data-edge="true" data-et="edge" data-id="L_Core_GL_0" data-points="W3sieCI6MTg2LjQzMjc0OTgwNzA5ODc2LCJ5IjozMzl9LHsieCI6MTI1LjIwMzEyNSwieSI6Mzc0LjV9LHsieCI6MTI1LjIwMzEyNSwieSI6NDEwfV0=" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/><path d="M125.203,480L125.203,484.167C125.203,488.333,125.203,496.667,125.203,504.333C125.203,512,125.203,519,125.203,522.5L125.203,526" id="my-svg-L_GL_LibGDX_0" class="edge-thickness-normal edge-pattern-solid edge-thickness-normal edge-pattern-solid flowchart-link" style=";" data-edge="true" data-et="edge" data-id="L_GL_LibGDX_0" data-points="W3sieCI6MTI1LjIwMzEyNSwieSI6NDgwfSx7IngiOjEyNS4yMDMxMjUsInkiOjUwNX0seyJ4IjoxMjUuMjAzMTI1LCJ5Ijo1MzB9XQ==" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/><path d="M343.388,339L353.593,344.917C363.797,350.833,384.207,362.667,394.412,373.833C404.617,385,404.617,395.5,404.617,400.75L404.617,406" id="my-svg-L_Core_Serv_0" class="edge-thickness-normal edge-pattern-dotted edge-thickness-normal edge-pattern-solid flowchart-link" style=";" data-edge="true" data-et="edge" data-id="L_Core_Serv_0" data-points="W3sieCI6MzQzLjM4NzU2MjY5MjkwMTI3LCJ5IjozMzl9LHsieCI6NDA0LjYxNzE4NzUsInkiOjM3NC41fSx7IngiOjQwNC42MTcxODc1LCJ5Ijo0MTB9XQ==" data-look="classic" marker-end="url(#my-svg_flowchart-v2-pointEnd)"/></g><g class="edgeLabels"><g class="edgeLabel"><g class="label" data-id="L_App_UI_0" transform="translate(0, 0)"><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml" class="labelBkg" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="edgeLabel"></span></div></foreignObject></g></g><g class="edgeLabel"><g class="label" data-id="L_UI_Core_0" transform="translate(0, 0)"><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml" class="labelBkg" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="edgeLabel"></span></div></foreignObject></g></g><g class="edgeLabel"><g class="label" data-id="L_Core_GL_0" transform="translate(0, 0)"><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml" class="labelBkg" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="edgeLabel"></span></div></foreignObject></g></g><g class="edgeLabel"><g class="label" data-id="L_GL_LibGDX_0" transform="translate(0, 0)"><foreignObject width="0" height="0"><div xmlns="http://www.w3.org/1999/xhtml" class="labelBkg" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="edgeLabel"></span></div></foreignObject></g></g><g class="edgeLabel" transform="translate(404.6171875, 374.5)"><g class="label" data-id="L_Core_Serv_0" transform="translate(-13.8984375, -10.5)"><foreignObject width="27.796875" height="21"><div xmlns="http://www.w3.org/1999/xhtml" class="labelBkg" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="edgeLabel"><p>使用</p></span></div></foreignObject></g></g></g><g class="nodes"><g class="node default" id="my-svg-flowchart-App-0" data-look="classic" transform="translate(264.91015625, 43)"><rect class="basic label-container" style="" x="-98.1171875" y="-35" width="196.234375" height="70"/><g class="label" style="" transform="translate(-70.1171875, -21)"><rect/><foreignObject width="140.234375" height="42"><div xmlns="http://www.w3.org/1999/xhtml" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="nodeLabel"><p><b>app</b> — 你的代码<br />(Windows, 组件, 逻辑)</p></span></div></foreignObject></g></g><g class="node default" id="my-svg-flowchart-UI-1" data-look="classic" transform="translate(264.91015625, 163)"><rect class="basic label-container" style="" x="-104.5" y="-35" width="209" height="70"/><g class="label" style="" transform="translate(-76.5, -21)"><rect/><foreignObject width="153" height="42"><div xmlns="http://www.w3.org/1999/xhtml" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="nodeLabel"><p><b>ui</b> — 一切皆 Component<br />(widgets, 布局, 样式)</p></span></div></foreignObject></g></g><g class="node default" id="my-svg-flowchart-Core-3" data-look="classic" transform="translate(264.91015625, 293.5)"><rect class="basic label-container" style="" x="-128" y="-45.5" width="256" height="91"/><g class="label" style="" transform="translate(-100, -31.5)"><rect/><foreignObject width="200" height="63"><div xmlns="http://www.w3.org/1999/xhtml" style="display: table; white-space: break-spaces; line-height: 1.5; max-width: 200px; text-align: center; width: 200px;"><span class="nodeLabel"><p><b>core / kernel</b><br />(bootstrap, input, 生命周期, config)</p></span></div></foreignObject></g></g><g class="node default" id="my-svg-flowchart-GL-5" data-look="classic" transform="translate(125.203125, 445)"><rect class="basic label-container" style="" x="-117.203125" y="-35" width="234.40625" height="70"/><g class="label" style="" transform="translate(-89.203125, -21)"><rect/><foreignObject width="178.40625" height="42"><div xmlns="http://www.w3.org/1999/xhtml" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="nodeLabel"><p><b>gl</b> — 按图元的渲染器<br />(将绘制转换为 → draw calls)</p></span></div></foreignObject></g></g><g class="node default" id="my-svg-flowchart-LibGDX-7" data-look="classic" transform="translate(125.203125, 554.5)"><rect class="basic label-container" style="" x="-87.390625" y="-24.5" width="174.78125" height="49"/><g class="label" style="" transform="translate(-59.390625, -10.5)"><rect/><foreignObject width="118.78125" height="21"><div xmlns="http://www.w3.org/1999/xhtml" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="nodeLabel"><p><b>libGDX + OpenGL</b></p></span></div></foreignObject></g></g><g class="node default" id="my-svg-flowchart-Serv-9" data-look="classic" transform="translate(404.6171875, 445)"><rect class="basic label-container" style="" x="-112.2109375" y="-35" width="224.421875" height="70"/><g class="label" style="" transform="translate(-84.2109375, -21)"><rect/><foreignObject width="168.421875" height="42"><div xmlns="http://www.w3.org/1999/xhtml" style="display: table-cell; white-space: nowrap; line-height: 1.5; max-width: 200px; text-align: center;"><span class="nodeLabel"><p><b>服务</b> — sqlite, device, map<br />(独立于 UI 的模块)</p></span></div></foreignObject></g></g></g></g></g><defs><filter id="my-svg-drop-shadow" height="130%" width="130%"><feDropShadow dx="4" dy="4" stdDeviation="0" flood-opacity="0.06" flood-color="var(--text-dim)"/></filter></defs><defs><filter id="my-svg-drop-shadow-small" height="150%" width="150%"><feDropShadow dx="2" dy="2" stdDeviation="0" flood-opacity="0.06" flood-color="var(--text-dim)"/></filter></defs><linearGradient id="my-svg-gradient" gradientUnits="objectBoundingBox" x1="0%" y1="0%" x2="100%" y2="0%"><stop offset="0%" stop-color="var(--border-strong)" stop-opacity="1"/><stop offset="100%" stop-color="hsl(0, 0%, 90%)" stop-opacity="1"/></linearGradient></svg>
</div>

## 每一层各做什么
- **app** —— 你的代码：`Window` 的子类（屏幕）、组件、业务规则。这是你日常工作的地方。
- **ui** —— UI 库：**一切都派生自 `Component`**（widgets、容器、样式、布局）。
  参见 [一切都是 Component](/docs/concepts/everything-is-a-component)。
- **core / kernel** —— 将应用连接到外部世界：`Main`（即 `ApplicationListener`）负责启动引导，
  分发输入、触发生命周期并加载设备配置。参见 [生命周期](/docs/concepts/lifecycle)。
- **gl** —— 将高层绘制（`Paint`）翻译成 OpenGL 的 *draw calls*，每个图元对应一个
  渲染器。参见 [渲染管线](/docs/concepts/rendering-pipeline)。
- **serviços** —— SQLite、device、map 等：**独立于 UI** 的模块，由 app 按需使用。

## 为什么这种分层很重要
- 你**几乎总是在 `ui` 层编程**（通过组合 `Component`）。下层是实现
  细节 —— 你很少会碰 `gl`，也很少直接与 libGDX 打交道。
- 框架在**paint thread 上是单线程的**（参见 [paint thread](/docs/concepts/the-paint-thread)）：
  `Main` 始终在该线程上调用 `create()`/`render()`/`resize()`，所有 UI 都发生在其中。
- **服务不依赖 UI** —— 你可以不搭建任何界面就使用 SQLite 或 device，而 UI 在你调用这些模块之前
  也"不知道"它们的存在。

## 高层次的控制流
1. libGDX 创建 `Main` 并在 paint thread 上调用 `create()`。
2. `Main` 完成启动引导（平台、运行时、配置），并**通过反射实例化主窗口**，
   调用其 `onShow()`。
3. 每一帧，libGDX 都会调用 `render()`；`EventsProcessor` 绘制**窗口栈**（栈顶
   在最上层）—— 但**只有在收到渲染请求时**（`repaint()`）才绘制，而非持续绘制。
4. 输入和 `resize` 通过 `Main` 的回调到达，始终在 paint thread 上。

> 这是思维导图，而非包目录。关于按层划分的包和类列表，请参见
> 技术参考。

## 参见
- 下一篇：[一切都是 Component](/docs/concepts/everything-is-a-component) · [生命周期](/docs/concepts/lifecycle)
