void Context::initContext() { // Here we go. From the top. Where is glResetState? oglDisable(GL_ALPHA_TEST); oglDisable(GL_AUTO_NORMAL); oglEnable(GL_BLEND); // Skip clip planes, there are thousands of them. oglDisable(GL_COLOR_LOGIC_OP); oglDisable(GL_COLOR_MATERIAL); oglDisable(GL_COLOR_TABLE); oglDisable(GL_CONVOLUTION_1D); oglDisable(GL_CONVOLUTION_2D); oglDisable(GL_CULL_FACE); oglDisable(GL_DEPTH_TEST); oglDisable(GL_DITHER); oglDisable(GL_FOG); oglDisable(GL_HISTOGRAM); oglDisable(GL_INDEX_LOGIC_OP); oglDisable(GL_LIGHTING); // Skip line smmooth // Skip map oglDisable(GL_MINMAX); // Skip polygon offset oglDisable(GL_SEPARABLE_2D); oglDisable(GL_SCISSOR_TEST); oglDisable(GL_STENCIL_TEST); oglEnable(GL_TEXTURE_2D); oglDisable(GL_TEXTURE_GEN_Q); oglDisable(GL_TEXTURE_GEN_R); oglDisable(GL_TEXTURE_GEN_S); oglDisable(GL_TEXTURE_GEN_T); oglBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); }
Context::Context(HDC hdc) { timeT = clock(); frameCount = 0; ctx = owglCreateContext(hdc); owglMakeCurrent(hdc, ctx); // Here we go. From the top. Where is glResetState? oglDisable(GL_ALPHA_TEST); oglDisable(GL_AUTO_NORMAL); oglEnable(GL_BLEND); // Skip clip planes, there are thousands of them. oglDisable(GL_COLOR_LOGIC_OP); oglDisable(GL_COLOR_MATERIAL); oglDisable(GL_COLOR_TABLE); oglDisable(GL_CONVOLUTION_1D); oglDisable(GL_CONVOLUTION_2D); oglDisable(GL_CULL_FACE); oglDisable(GL_DEPTH_TEST); oglDisable(GL_DITHER); oglDisable(GL_FOG); oglDisable(GL_HISTOGRAM); oglDisable(GL_INDEX_LOGIC_OP); oglDisable(GL_LIGHTING); // Skip line smmooth // Skip map oglDisable(GL_MINMAX); // Skip polygon offset oglDisable(GL_SEPARABLE_2D); oglDisable(GL_SCISSOR_TEST); oglDisable(GL_STENCIL_TEST); oglEnable(GL_TEXTURE_2D); oglDisable(GL_TEXTURE_GEN_Q); oglDisable(GL_TEXTURE_GEN_R); oglDisable(GL_TEXTURE_GEN_S); oglDisable(GL_TEXTURE_GEN_T); oglBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); texture = ~0; }
void GraphicsBase::draw() { if (indices.empty()) return; if (isWhite_ == false) { glPushColor(); glMultColor(r_, g_, b_, a_); } if (data) { oglEnable(GL_TEXTURE_2D); oglBindTexture(GL_TEXTURE_2D, data->id()); oglEnableClientState(GL_VERTEX_ARRAY); oglEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(2, GL_FLOAT, 0, &vertices[0]); glTexCoordPointer(2, GL_FLOAT, 0, &texcoords[0]); oglDrawElements(mode, indices.size(), GL_UNSIGNED_SHORT, &indices[0]); oglDisableClientState(GL_VERTEX_ARRAY); oglDisableClientState(GL_TEXTURE_COORD_ARRAY); } else { oglDisable(GL_TEXTURE_2D); oglEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_FLOAT, 0, &vertices[0]); oglDrawElements(mode, indices.size(), GL_UNSIGNED_SHORT, &indices[0]); oglDisableClientState(GL_VERTEX_ARRAY); } if (isWhite_ == false) { glPopColor(); } }
void TileMap::doDraw(const CurrentTransform& transform, float hsx, float hsy, float hex, float hey) { int sx, sy, ex, ey; { // inverse transformed hardware start/end x/y float x1, y1, x2, y2, x3, y3, x4, y4; Matrix inverse = transform.inverse(); inverse.transformPoint(hsx, hsy, &x1, &y1); inverse.transformPoint(hex, hsy, &x2, &y2); inverse.transformPoint(hsx, hey, &x3, &y3); inverse.transformPoint(hex, hey, &x4, &y4); float thsx = std::min(std::min(x1, x2), std::min(x3, x4)); float thsy = std::min(std::min(y1, y2), std::min(y3, y4)); float thex = std::max(std::max(x1, x2), std::max(x3, x4)); float they = std::max(std::max(y1, y2), std::max(y3, y4)); // cell size width, cell size height float csw = std::max(std::max(tilewidth_, tileheight_), displaywidth_); float csh = std::max(std::max(tilewidth_, tileheight_), displayheight_); sx = floor((thsx - csw) / displaywidth_) + 1; sy = floor(thsy / displayheight_); ex = floor(thex / displaywidth_) + 1; ey = floor((they + csh) / displayheight_); // extra 1 block around sx--; sy--; ex++; ey++; sx = std::max(sx, 0); sy = std::max(sy, 0); ex = std::min(ex, width_); ey = std::min(ey, height_); } int tileCount = 0; for (int y = sy; y < ey; ++y) for (int x = sx; x < ex; ++x) { int index = x + y * width_; int tx = tileids_[index].x; int ty = tileids_[index].y; if (!isEmpty(tx, ty)) tileCount++; } if (tileCount == 0) return; vertices.resize(tileCount * 12); texcoords.resize(tileCount * 12); int pos = 0; for (int y = sy; y < ey; ++y) for (int x = sx; x < ex; ++x) { int index = x + y * width_; int tx = tileids_[index].x; int ty = tileids_[index].y; int flip = tileids_[index].flip; if (!isEmpty(tx, ty)) { bool flip_horizontal = (flip & FLIP_HORIZONTAL); bool flip_vertical = (flip & FLIP_VERTICAL); bool flip_diagonal = (flip & FLIP_DIAGONAL); float x0, y0, x1, y1; if (!flip_diagonal) { x0 = x * displaywidth_; y0 = y * displayheight_ - (tileheight_ - displayheight_); x1 = x0 + tilewidth_; y1 = y0 + tileheight_; } else { x0 = x * displaywidth_; y0 = y * displayheight_ - (tilewidth_ - displayheight_); x1 = x0 + tileheight_; y1 = y0 + tilewidth_; } vertices[pos + 0] = x0; vertices[pos + 1] = y0; vertices[pos + 2] = x1; vertices[pos + 3] = y0; vertices[pos + 4] = x0; vertices[pos + 5] = y1; vertices[pos + 6] = x1; vertices[pos + 7] = y0; vertices[pos + 8] = x1; vertices[pos + 9] = y1; vertices[pos + 10] = x0; vertices[pos + 11] = y1; int u0 = marginx_ + tx * (tilewidth_ + spacingx_); int v0 = marginy_ + ty * (tileheight_ + spacingy_); int u1 = marginx_ + tx * (tilewidth_ + spacingx_) + tilewidth_; int v1 = marginy_ + ty * (tileheight_ + spacingy_) + tileheight_; float fu0 = (float)u0 / (float)texture_->data->exwidth; float fv0 = (float)v0 / (float)texture_->data->exheight; float fu1 = (float)u1 / (float)texture_->data->exwidth; float fv1 = (float)v1 / (float)texture_->data->exheight; fu0 *= texture_->uvscalex; fv0 *= texture_->uvscaley; fu1 *= texture_->uvscalex; fv1 *= texture_->uvscaley; if (flip_horizontal) std::swap(fu0, fu1); if (flip_vertical) std::swap(fv0, fv1); if (flip_diagonal && (flip_vertical != flip_horizontal)) { std::swap(fu0, fu1); std::swap(fv0, fv1); } fu0 += 0.0001f; fv0 += 0.0001f; fu1 -= 0.0001f; fv1 -= 0.0001f; if (!flip_diagonal) { texcoords[pos + 0] = fu0; texcoords[pos + 1] = fv0; texcoords[pos + 2] = fu1; texcoords[pos + 3] = fv0; texcoords[pos + 4] = fu0; texcoords[pos + 5] = fv1; texcoords[pos + 6] = fu1; texcoords[pos + 7] = fv0; texcoords[pos + 8] = fu1; texcoords[pos + 9] = fv1; texcoords[pos + 10] = fu0; texcoords[pos + 11] = fv1; } else { texcoords[pos + 0] = fu0; texcoords[pos + 1] = fv0; texcoords[pos + 2] = fu0; texcoords[pos + 3] = fv1; texcoords[pos + 4] = fu1; texcoords[pos + 5] = fv0; texcoords[pos + 6] = fu0; texcoords[pos + 7] = fv1; texcoords[pos + 8] = fu1; texcoords[pos + 9] = fv1; texcoords[pos + 10] = fu1; texcoords[pos + 11] = fv0; } pos += 12; } } oglEnable(GL_TEXTURE_2D); oglBindTexture(GL_TEXTURE_2D, texture_->data->id()); oglArrayPointer(VertexArray,2, GL_FLOAT, &vertices[0]); oglEnableClientState(VertexArray); oglArrayPointer(TextureArray,2, GL_FLOAT, &texcoords[0]); oglEnableClientState(TextureArray); oglDrawArrays(GL_TRIANGLES, 0, tileCount * 6); oglDisableClientState(VertexArray); oglDisableClientState(TextureArray); }