void DrawRect(int x, int y, int width, int height, uint8 r, uint8 g, uint8 b)
{
    int right = x + width;
    int bottom = y + height;
    if (x < 0)
        x = 0;
    if (y < 0)
        y = 0;
    if (right > (int32)s3eSurfaceGetInt(S3E_SURFACE_WIDTH))
        right = s3eSurfaceGetInt(S3E_SURFACE_WIDTH);
    if (bottom > (int32)s3eSurfaceGetInt(S3E_SURFACE_HEIGHT))
        bottom = s3eSurfaceGetInt(S3E_SURFACE_HEIGHT);

    // Draw the text
    IwGxSetScreenSpaceSlot(0);

    CIwMaterial *fadeMat = IW_GX_ALLOC_MATERIAL();
    fadeMat->SetAlphaMode(CIwMaterial::NONE);
    fadeMat->SetShadeMode(CIwMaterial::SHADE_FLAT);
    IwGxSetMaterial(fadeMat);

    CIwColour* cols = IW_GX_ALLOC(CIwColour, 4);
    for (int i = 0; i < 4; i++)
        cols[i].Set(r, g, b);

    CIwSVec2 xy(x, y);
    CIwSVec2 wh(width, height);
    IwGxDrawRectScreenSpace(&xy, &wh, cols);
}
// This is the main rendering function that you have to implement and provide to ImGui (via setting up 'RenderDrawListsFn' in the ImGuiIO structure)
void ImGui_Marmalade_RenderDrawLists(ImDrawData* draw_data)
{
    // Handle cases of screen coordinates != from framebuffer coordinates (e.g. retina displays)
    ImGuiIO& io = ImGui::GetIO();
    draw_data->ScaleClipRects(io.DisplayFramebufferScale);

    // Render command lists
    for(int n = 0; n < draw_data->CmdListsCount; n++)
    {
        const ImDrawList* cmd_list = draw_data->CmdLists[n];
        const unsigned char* vtx_buffer = (const unsigned char*)&cmd_list->VtxBuffer.front();
        const ImDrawIdx* idx_buffer = &cmd_list->IdxBuffer.front();
        int nVert = cmd_list->VtxBuffer.size();
        CIwFVec2* pVertStream = IW_GX_ALLOC(CIwFVec2, nVert);
        CIwFVec2* pUVStream = IW_GX_ALLOC(CIwFVec2, nVert);
        CIwColour* pColStream = IW_GX_ALLOC(CIwColour, nVert);

        for( int i=0; i < nVert; i++ )
        {
            // TODO: optimize multiplication on gpu using vertex shader
            pVertStream[i].x = cmd_list->VtxBuffer[i].pos.x * g_scale.x;
            pVertStream[i].y = cmd_list->VtxBuffer[i].pos.y * g_scale.y;
            pUVStream[i].x = cmd_list->VtxBuffer[i].uv.x;
            pUVStream[i].y = cmd_list->VtxBuffer[i].uv.y;
            pColStream[i] = cmd_list->VtxBuffer[i].col;
        }

        IwGxSetVertStreamScreenSpace(pVertStream, nVert);
        IwGxSetUVStream(pUVStream);
        IwGxSetColStream(pColStream, nVert);
        IwGxSetNormStream(0);

        for (int cmd_i = 0; cmd_i < cmd_list->CmdBuffer.size(); cmd_i++)
        {
            const ImDrawCmd* pcmd = &cmd_list->CmdBuffer[cmd_i];
            if (pcmd->UserCallback)
            {
                pcmd->UserCallback(cmd_list,pcmd);
            }
            else
            {
                CIwMaterial* pCurrentMaterial = IW_GX_ALLOC_MATERIAL();
                pCurrentMaterial->SetShadeMode(CIwMaterial::SHADE_FLAT);
                pCurrentMaterial->SetCullMode(CIwMaterial::CULL_NONE);
                pCurrentMaterial->SetFiltering(false);
                pCurrentMaterial->SetAlphaMode(CIwMaterial::ALPHA_BLEND);
                pCurrentMaterial->SetDepthWriteMode(CIwMaterial::DEPTH_WRITE_NORMAL);
                pCurrentMaterial->SetAlphaTestMode(CIwMaterial::ALPHATEST_DISABLED);
                pCurrentMaterial->SetTexture((CIwTexture*)pcmd->TextureId);
                IwGxSetMaterial(pCurrentMaterial);
                IwGxDrawPrims(IW_GX_TRI_LIST, (uint16*)idx_buffer, pcmd->ElemCount);
            }
            idx_buffer += pcmd->ElemCount;
        }
        IwGxFlush();
    }

    // TODO: restore modified state (i.e. mvp matrix)
}