//add start by Talen
int CTResultReport::makeItemTitle(char *outBuf, char *inPut, int rty)
{
	int i=1;
	char temp[32]="";
	CString srcBuf;
	CString dstBuf(inPut);
//	CString srcBuf(inPut);
	dstBuf+=",";
	while(i <= rty)
	{
		srcBuf=_T(inPut);
		srcBuf+=",";
		memset(temp,0,sizeof(temp));
		sprintf_s(temp,sizeof(temp),"_RTY%d,",i);
		srcBuf.Replace(",",temp);
		i++;
		if(i>rty)
		{
			srcBuf.TrimRight(",");
			//srcBuf.Delete(srcBuf.GetLength()-1,1);
		}
		dstBuf+=srcBuf;
	}
	sprintf(outBuf,"%s",dstBuf);
	return 1;
}
void SurfaceSdlGraphicsManager::clearOverlay() {
    if (!_overlayscreen)
        return;

    if (!_overlayVisible)
        return;

#ifdef USE_OPENGL
    if (_opengl) {
        SDL_Surface *tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, _overlayWidth, _overlayHeight,
                                                _overlayscreen->format->BytesPerPixel * 8,
                                                _overlayscreen->format->Rmask, _overlayscreen->format->Gmask,
                                                _overlayscreen->format->Bmask, _overlayscreen->format->Amask);

        SDL_LockSurface(tmp);
        SDL_LockSurface(_overlayscreen);

        glReadPixels(0, 0, _overlayWidth, _overlayHeight, GL_RGB, _overlayScreenGLFormat, tmp->pixels);

        // Flip pixels vertically
        byte *src = (byte *)tmp->pixels;
        byte *buf = (byte *)_overlayscreen->pixels + (_overlayHeight - 1) * _overlayscreen->pitch;
        int h = _overlayHeight;
        do {
            memcpy(buf, src, _overlayWidth * _overlayscreen->format->BytesPerPixel);
            src += tmp->pitch;
            buf -= _overlayscreen->pitch;
        } while (--h);

        SDL_UnlockSurface(_overlayscreen);
        SDL_UnlockSurface(tmp);

        SDL_FreeSurface(tmp);
    } else
#endif
    {
        SDL_LockSurface(_screen);
        SDL_LockSurface(_overlayscreen);
        Graphics::PixelBuffer srcBuf(_screenFormat, (byte *)_screen->pixels);
        Graphics::PixelBuffer dstBuf(_overlayFormat, (byte *)_overlayscreen->pixels);
        int h = _overlayHeight;

        do {
            dstBuf.copyBuffer(0, _overlayWidth, srcBuf);

            srcBuf.shiftBy(_overlayWidth);
            dstBuf.shiftBy(_overlayWidth);
        } while (--h);
        SDL_UnlockSurface(_screen);
        SDL_UnlockSurface(_overlayscreen);
    }
    _overlayDirty = true;
}
void SurfaceSdlGraphicsManager::drawOverlay() {
    if (!_overlayscreen)
        return;

    SDL_LockSurface(_screen);
    SDL_LockSurface(_overlayscreen);
    Graphics::PixelBuffer srcBuf(_overlayFormat, (byte *)_overlayscreen->pixels);
    Graphics::PixelBuffer dstBuf(_screenFormat, (byte *)_screen->pixels);
    int h = _overlayHeight;

    do {
        dstBuf.copyBuffer(0, _overlayWidth, srcBuf);

        srcBuf.shiftBy(_overlayWidth);
        dstBuf.shiftBy(_overlayWidth);
    } while (--h);
    SDL_UnlockSurface(_screen);
    SDL_UnlockSurface(_overlayscreen);
}
void SurfaceSdlGraphicsManager::updateScreen() {
#ifdef USE_OPENGL
	if (_opengl) {
		if (_overlayVisible) {
			if (_overlayDirty) {
				// remove if already exist
				if (_overlayNumTex > 0) {
					glDeleteTextures(_overlayNumTex, _overlayTexIds);
					delete[] _overlayTexIds;
					_overlayNumTex = 0;
				}

				_overlayNumTex = ((_overlayWidth + (BITMAP_TEXTURE_SIZE - 1)) / BITMAP_TEXTURE_SIZE) *
								((_overlayHeight + (BITMAP_TEXTURE_SIZE - 1)) / BITMAP_TEXTURE_SIZE);
				_overlayTexIds = new GLuint[_overlayNumTex];
				glGenTextures(_overlayNumTex, _overlayTexIds);
				for (int i = 0; i < _overlayNumTex; i++) {
					glBindTexture(GL_TEXTURE_2D, _overlayTexIds[i]);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
					glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
					glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, BITMAP_TEXTURE_SIZE, BITMAP_TEXTURE_SIZE, 0, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, NULL);
				}

				glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
				glPixelStorei(GL_UNPACK_ROW_LENGTH, _overlayWidth);

				int curTexIdx = 0;
				for (int y = 0; y < _overlayHeight; y += BITMAP_TEXTURE_SIZE) {
					for (int x = 0; x < _overlayWidth; x += BITMAP_TEXTURE_SIZE) {
						int t_width = (x + BITMAP_TEXTURE_SIZE >= _overlayWidth) ? (_overlayWidth - x) : BITMAP_TEXTURE_SIZE;
						int t_height = (y + BITMAP_TEXTURE_SIZE >= _overlayHeight) ? (_overlayHeight - y) : BITMAP_TEXTURE_SIZE;
						glBindTexture(GL_TEXTURE_2D, _overlayTexIds[curTexIdx]);
						glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, t_width, t_height, GL_RGB, GL_UNSIGNED_SHORT_5_6_5, (byte *)_overlayscreen->pixels + (y * 2 * _overlayWidth) + (2 * x));
						curTexIdx++;
					}
				}
				glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
				glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
			}

			// Save current state
			glPushAttrib(GL_TRANSFORM_BIT | GL_ENABLE_BIT | GL_DEPTH_BUFFER_BIT | GL_SCISSOR_BIT);

			// prepare view
			glMatrixMode(GL_PROJECTION);
			glPushMatrix();
			glLoadIdentity();
			glOrtho(0, _overlayWidth, _overlayHeight, 0, 0, 1);

			glMatrixMode(GL_MODELVIEW);
			glPushMatrix();
			glLoadIdentity();

			glMatrixMode(GL_TEXTURE);
			glPushMatrix();
			glLoadIdentity();

			glDisable(GL_LIGHTING);
			glEnable(GL_TEXTURE_2D);
			glDisable(GL_DEPTH_TEST);
			glDepthMask(GL_FALSE);
			glEnable(GL_SCISSOR_TEST);

			glScissor(0, 0, _overlayWidth, _overlayHeight);

			int curTexIdx = 0;
			for (int y = 0; y < _overlayHeight; y += BITMAP_TEXTURE_SIZE) {
				for (int x = 0; x < _overlayWidth; x += BITMAP_TEXTURE_SIZE) {
					glBindTexture(GL_TEXTURE_2D, _overlayTexIds[curTexIdx]);
					glBegin(GL_QUADS);
					glTexCoord2f(0, 0);
					glVertex2i(x, y);
					glTexCoord2f(1.0f, 0.0f);
					glVertex2i(x + BITMAP_TEXTURE_SIZE, y);
					glTexCoord2f(1.0f, 1.0f);
					glVertex2i(x + BITMAP_TEXTURE_SIZE, y + BITMAP_TEXTURE_SIZE);
					glTexCoord2f(0.0f, 1.0f);
					glVertex2i(x, y + BITMAP_TEXTURE_SIZE);
					glEnd();
					curTexIdx++;
				}
			}

			// Restore previous state
			glMatrixMode(GL_PROJECTION);
			glPopMatrix();

			glMatrixMode(GL_MODELVIEW);
			glPopMatrix();

			glMatrixMode(GL_TEXTURE);
			glPopMatrix();

			glPopAttrib();
		}
		SDL_GL_SwapBuffers();
	} else
#endif
	{
		if (_overlayVisible) {
			SDL_LockSurface(_screen);
			SDL_LockSurface(_overlayscreen);
			Graphics::PixelBuffer srcBuf(_overlayFormat, (byte *)_overlayscreen->pixels);
			Graphics::PixelBuffer dstBuf(_screenFormat, (byte *)_screen->pixels);
			int h = _overlayHeight;

			do {
				dstBuf.copyBuffer(0, _overlayWidth, srcBuf);

				srcBuf.shiftBy(_overlayWidth);
				dstBuf.shiftBy(_overlayWidth);
			} while (--h);
			SDL_UnlockSurface(_screen);
			SDL_UnlockSurface(_overlayscreen);
		}
		SDL_Flip(_screen);
	}
}