/*
  Copyright (C) 2010-2011  Charles Pence <charles@charlespence.net>
  Copyright (C) 1996-1997  Id Software, Inc.

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "pch.h"
#include "quakedef.h"

viddef_t    vid;                // global video state

void ClearAllStates (void);
void GL_Init (void);

//====================================

qboolean windowed_mouse = false;

qboolean VID_SetWindowedMode (int modenum)
{
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_SetVideoMode(800, 600, 0, SDL_OPENGL);

    vid.width = 800;
    vid.height = 600;
    return true;
}


qboolean VID_SetFullDIBMode (int modenum)
{
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_SetVideoMode(800, 600, 0, SDL_OPENGL|SDL_FULLSCREEN);

    vid.width = 800;
    vid.height = 600;

    return true;
}


void    VID_SetPalette (unsigned char *palette)
{
    int i;

    for (i = 0 ; i < 256 ; i++)
    {
        vid.palette[i][0] = palette[i*3 + 0];
        vid.palette[i][1] = palette[i*3 + 1];
        vid.palette[i][2] = palette[i*3 + 2];
        vid.palette[i][3] = 255;
    }
    vid.palette[255][3] = 0;
}


int VID_SetMode (int modenum, unsigned char *palette)
{
    qboolean stat;

    // Set either the fullscreen or windowed mode
    if (1) // FIXME windowed
    {
        stat = VID_SetWindowedMode(modenum);

        if (Cvar_VariableValue("in_windowed_mouse") && key_dest == key_game)
            IN_HideMouse ();
        else
            IN_ShowMouse ();
    }
    else
    {
        stat = VID_SetFullDIBMode(modenum);
        IN_HideMouse ();
    }

    if (!stat)
        Sys_Error ("Couldn't set video mode");

    VID_SetPalette (palette);

    ClearAllStates ();

    vid.recalc_refdef = 1;

    return true;
}


/*
================
VID_UpdateWindowStatus
================
*/
void VID_UpdateWindowStatus (void)
{
}


/*
===============
GL_Init
===============
*/
void GL_Init (void)
{
    R_InitExtensions();

    glClearColor (1,0,0,0);
    glCullFace(GL_FRONT);
    glEnable(GL_TEXTURE_2D);

    glAlphaFunc(GL_GREATER, 0.666);

    glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}


void GL_EndRendering (void)
{
    SDL_GL_SwapBuffers();

// handle the mouse state when windowed if that's changed
    if (1)
    {
        if (!Cvar_VariableValue("in_windowed_mouse"))
        {
            if (windowed_mouse)
            {
                IN_ShowMouse ();
                windowed_mouse = false;
            }
        }
        else
        {
            windowed_mouse = true;
            if (key_dest == key_game)
                IN_HideMouse ();
            else if (key_dest != key_game)
                IN_ShowMouse ();
        }
    }
}


void VID_Shutdown (void)
{
}


/*
================
ClearAllStates
================
*/
void ClearAllStates (void)
{
    int        i;

// send an up event for each key, to make sure the server clears them all
    for (i=0 ; i<256 ; i++)
    {
        Key_Event (i, false);
    }

    Key_ClearStates ();
    IN_ClearStates ();
}

/*
=================
VID_NumModes
=================
*/
int VID_NumModes (void)
{
    return 1;
}

/*
===================
VID_Init
===================
*/
void    VID_Init (unsigned char *palette)
{
    Cvar_RegisterVariable("in_windowed_mouse", "1", true);
    VID_SetMode (0, palette);
    GL_Init ();
}
