/*
        Windows Textmode OpenGL proof of concept
        (c) 2008 Jari Komppa
        http://iki.fi/sol/

        This software is provided 'as-is', without any express or implied
        warranty.  In no event will the authors be held liable for any damages
        arising from the use of this software.

        Permission is granted to anyone to use this software for any purpose,
        including commercial applications, and to alter it and redistribute it
        freely, subject to the following restrictions:

        1. The origin of this software must not be misrepresented; you must not
            claim that you wrote the original software. If you use this software
            in a product, an acknowledgment in the product documentation would be
            appreciated but is not required.
        2. Altered source versions must be plainly marked as such, and must not be
            misrepresented as being the original software.
        3. This notice may not be removed or altered from any source distribution.

        Requirement libs: opengl32.lib glu32.lib libcaca

        Feel free to use parts of this source as you see fit.
*/

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

#include <math.h>
#include <string.h>
#include <stdio.h>

#include "caca.h"

#define FB_WIDTH 160
#define FB_HEIGHT 100
#define STFU(x) warning(disable: x)
#pragma STFU(4244)

int * fb;

void run_effect(int frame)
{
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);

    glPushMatrix();
    glTranslatef(0,0,-5);
    glRotatef(frame, 0.27,0.25,0.75);

    // This bit borrowed from
    // http://www.morrowland.com/apron/tutorials/gl/gl_rotating_cube.php
    glBegin(GL_QUADS);
    glColor3f(0.0f,1.0f,0.0f);
    glVertex3f( 1.0f, 1.0f,-1.0f);
    glVertex3f(-1.0f, 1.0f,-1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f( 1.0f, 1.0f, 1.0f);
    glColor3f(1.0f,0.5f,0.0f);
    glVertex3f( 1.0f,-1.0f, 1.0f);
    glVertex3f(-1.0f,-1.0f, 1.0f);
    glVertex3f(-1.0f,-1.0f,-1.0f);
    glVertex3f( 1.0f,-1.0f,-1.0f);
    glColor3f(1.0f,0.0f,0.0f);
    glVertex3f( 1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f,-1.0f, 1.0f);
    glVertex3f( 1.0f,-1.0f, 1.0f);
    glColor3f(1.0f,1.0f,0.0f);
    glVertex3f( 1.0f,-1.0f,-1.0f);
    glVertex3f(-1.0f,-1.0f,-1.0f);
    glVertex3f(-1.0f, 1.0f,-1.0f);
    glVertex3f( 1.0f, 1.0f,-1.0f);
    glColor3f(0.0f,0.0f,1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f,-1.0f);
    glVertex3f(-1.0f,-1.0f,-1.0f);
    glVertex3f(-1.0f,-1.0f, 1.0f);
    glColor3f(1.0f,0.0f,1.0f);
    glVertex3f( 1.0f, 1.0f,-1.0f);
    glVertex3f( 1.0f, 1.0f, 1.0f);
    glVertex3f( 1.0f,-1.0f, 1.0f);
    glVertex3f( 1.0f,-1.0f,-1.0f);
    glEnd();    

    glPopMatrix();
}

void opengl_startup()
{
    PIXELFORMATDESCRIPTOR pfd;
    int  pixelFormat;
    void *glframebuf;

    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL | PFD_SUPPORT_GDI;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 16;

    HDC hDC, pDC;
    BITMAPINFO bitmapinfo;
    hDC = CreateCompatibleDC(NULL);
    bitmapinfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bitmapinfo.bmiHeader.biWidth = 160;
    bitmapinfo.bmiHeader.biHeight = 100; 
    bitmapinfo.bmiHeader.biPlanes = 1;
    bitmapinfo.bmiHeader.biBitCount = 32;
    bitmapinfo.bmiHeader.biCompression = BI_RGB;
    bitmapinfo.bmiHeader.biSizeImage = 0;
    bitmapinfo.bmiHeader.biClrUsed = 256;
    bitmapinfo.bmiHeader.biClrImportant = 256;
    HBITMAP ourbitmap=CreateDIBSection(hDC, &bitmapinfo, DIB_PAL_COLORS, &glframebuf, 0, 0);
    pDC=CreateCompatibleDC(NULL);
    SelectObject(pDC, ourbitmap);
    DeleteDC(hDC);

    pixelFormat = ChoosePixelFormat(pDC, &pfd);
    SetPixelFormat(pDC, pixelFormat, &pfd);
    wglMakeCurrent(pDC, wglCreateContext(pDC)); 

    glMatrixMode(GL_PROJECTION);
    gluPerspective(60,1,1,1000);
    glMatrixMode(GL_MODELVIEW);
}


void main()
{
    unsigned int event;
    opengl_startup();
    fb = new int[FB_WIDTH * FB_HEIGHT];
    struct caca_bitmap *bitmap;

    if(caca_init()) return;

    caca_set_window_title("textmode opengl proof of concept - alt-enter for fullscreen");
    caca_set_delay(0);

    bitmap = caca_create_bitmap(32, FB_WIDTH, FB_HEIGHT, 4 * FB_WIDTH, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);

    int termination = 0;
    while (!termination)
    {
        while((event = caca_get_event(CACA_EVENT_ANY)))
        {
            if (event & CACA_EVENT_KEY_PRESS)
            {
                termination = 1;
            }
        }

        caca_clear();
        run_effect(GetTickCount() / (1000/25));
        glReadPixels(0, 0, 160, 100, GL_RGBA, GL_UNSIGNED_BYTE, fb);
        caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1, bitmap, fb);//framebuffer);
        caca_refresh();
        Sleep(1);
    }

    caca_free_bitmap(bitmap);
    caca_end();
}
