Scene::Scene(Game* game, std::vector<char*> &levelsData, int dataStart, ObjectConstructor* &constructor) { this->game = game; gravity = 4.0f; Sprite tmp = Sprite(game, glm::vec2(0, 0), 1.0f, constructor->GetValidTiles()[0]); tileCount = glm::vec2(0, 0); tileSize = glm::vec2(tmp.GetRect().w, tmp.GetRect().h); objectiveCount = 0; for (unsigned int i = dataStart; i < levelsData.size(); i++) { if (StrLib::str_contains(levelsData[i], "#")) { tileCount.y = (float)(i - dataStart); i = InstantiateDynObjects(levelsData, i + 1, constructor); } if (StrLib::str_contains(levelsData[i], "};")) { std::cout << "Scene loaded successfully: TILES:" << tiles.size() << "." << std::endl; tileCount.x++; break; } for (unsigned int j = 1; j < strlen(levelsData[i]); j++) { int val = levelsData[i][j] - '0' - 1; tileMap.push_back(val); constructor->CreateObject(tiles, game, glm::vec2(j * tileSize.x, (i - dataStart) * tileSize.y), val); if (val == 2) objectiveCount++; if (j > tileCount.x) tileCount.x = (float)j; } } if (tiles.size() == 0) { std::cout << std::endl << "Scene loaded incorrectly." << std::endl; } }
Gdiplus::RectF Sprite::GetAbsRect() { Sprite *sp = m_parent; Gdiplus::RectF rcSelf = GetRect(); Gdiplus::RectF rcParent; while(sp) { rcParent = sp->GetRect(); rcSelf.Offset(rcParent.X, rcParent.Y); sp = sp->m_parent; } return rcSelf; }
void Sprite::OnDraw( Gdiplus::Graphics &g, const Gdiplus::RectF &rcDirty ) { if (!m_bVisible) { return; // 子节点也不会被绘制 } // 前序遍历 让父节点先绘制 //Gdiplus::RectF rc = GetRect(); //if (10 == rc.Width && 10 == rc.Height) //{ // LOGW(<<L"Orignal Size 10 10"); // 检查下有没有多余的重绘 //} if (m_bClipChildren) { Gdiplus::RectF rcClip = GetRect(); rcClip.X = 0.0f; rcClip.Y = 0.0f; g.SetClip(rcClip); } PaintEvent ev; ev.graphics = &g; ev.rcDirty = rcDirty; SendNotify(ePaint, &ev); //this->ClientDraw(g, rcDirty); Sprite *sp = m_firstChild; while(sp) { // 如果需要重绘部分矩形和sp相交则重画它 否则不重画 // 这里还有个问题就是 父矩形必须比子矩形要大 否则可能父的相交不到 而子的相交的到 // 可能要强制这一原理 类似于浏览器 会撑大 Gdiplus::RectF rc2 = sp->GetRect(); Gdiplus::RectF rcAbs = sp->GetAbsRect(); rcAbs.X -= 0.5f; // FIXME 有时无法得到重画导致边界1像素消失 rcAbs.Y -= 0.5f; rcAbs.Width += 1.0f; rcAbs.Height += 1.0f; if (rcDirty.IntersectsWith(rcAbs)) { g.TranslateTransform(rc2.X, rc2.Y); sp->OnDraw(g, rcDirty); g.TranslateTransform(-rc2.X, -rc2.Y); } sp = sp->m_nextSibling; } if (m_bClipChildren) { g.ResetClip(); } }