@SuppressWarnings("unchecked")
    public void TextWrite() {
        T src;
        //V dest;
        int w;
        int count;
        char[] ch;
        int c;
        int cx;
        int cy;

        // erase the entire screen to a tiled background
        src = (T) ((flat_t) W.CacheLumpName(finaleflat, PU_CACHE, flat_t.class)).data;
        //dest = V.getScreen(DoomVideoRenderer.SCREEN_FG);

        for (int y = 0; y < SCREENHEIGHT; y += 64) {

            int y_maxdraw = Math.min(SCREENHEIGHT - y, 64);

            // Draw whole blocks.
            for (int x = 0; x < SCREENWIDTH; x += 64) {
                int x_maxdraw = Math.min(SCREENWIDTH - x, 64);
                V.DrawBlock(x, y, DoomVideoRenderer.SCREEN_FG, x_maxdraw, y_maxdraw,
                        src);
            }
        }

        V.MarkRect(0, 0, SCREENWIDTH, SCREENHEIGHT);

        // draw some of the text onto the screen
        cx = 10;
        cy = 10;
        ch = finaletext.toCharArray();

        count = (finalecount - 10) / TEXTSPEED;
        if (count < 0)
            count = 0;

        // _D_: added min between count and ch.length, so that the text is not
        // written all at once
        for (int i = 0; i < Math.min(ch.length, count); i++)

        {
            c = ch[i];
            if (c == 0)
                break;
            if (c == '\n') {
                cx = 10;
                cy += 11;
                continue;
            }

            c = Character.toUpperCase(c) - HU_FONTSTART;
            if (c < 0 || c > HU_FONTSIZE) {
                cx += 4;
                continue;
            }

            w = hu_font[c].width;
            if (cx + w > SCREENWIDTH)
                break;
            V.DrawScaledPatch(cx, cy, 0, vs, hu_font[c]);
            cx += w;
        }

    }