FFS Pixmap visualization client / pixmap_viz_client.cpp

#include <cstdlib>
#include <ctime>
#include <cmath>
#include <string>
#include <iostream>
#include <fstream>
#include <limits>
#include <vector>
#include <map>
#include <sstream>
#include <algorithm>
#include<cassert>

using namespace std;

#ifndef __APPLE__
#include <GL/glut.h>
#else
#include <GLUT/glut.h>
#endif

///Displays the rendered scene (handles MSAA resolve, buffer swap, etc).
void display();

///Primary initialization routine (initializes OpenGL, Cg and the Wiimote).
void initialize();

///Keyboard handler.
///\param Key Code of the pressed key.
///\param X X component of the cursor position.
///\param Y Y component of the cursor position.
void keyboard(unsigned char Key, int X, int Y);

///A namespace to contain the application specific shared data.
namespace application
{
    ///Display fullscreen
    extern bool Fullscreen;
    ///Display screen width.
    extern int ScreenWidth;
    ///Display screen height.
    extern int ScreenHeight;
}

void reshape(int Width, int Height);

struct client_info
{
    int width;
    int height;
};

unsigned char* PixmapData;

void client_initialize();
client_info handshake(const char* const IP);
//writes received data to next image into, Data can hold width*height*4 elements (RGBA)
//returns 1 on success, 0 on failure (failure, say, server closed.)
int next_image(unsigned char* Data);
void close_connection();
void client_deinitialize();

int main(int argc, char* argv[])
{
    cout << "Use the right mouse button to bring up the options menu.\n";
    cout << "Press Q to quit.\n";
    //
    client_initialize();
    client_info Info = handshake("127.0.0.1");
    application::ScreenWidth = Info.width;
    application::ScreenHeight = Info.height;
    PixmapData = new unsigned char [Info.width * Info.height * 4];
    //
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowSize(application::ScreenWidth, application::ScreenHeight);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Window");
    glutKeyboardFunc(keyboard);
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    initialize();
    glutMainLoop();
    return 0;
}

namespace application
{
    bool Fullscreen = false;
    int ScreenWidth = 1280;
    int ScreenHeight = 720;
}

void initialize_gl()
{
    //glewInit();
    glClearColor(0.7f, 0.7f, 0.7f, 1.0f);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0f, float(application::ScreenWidth) / float(application::ScreenHeight), 0.1f, 100.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void initialize()
{
    initialize_gl();
    srand(time(0));
}

int CurrentWidth;
int CurrentHeight;

void reshape(int Width, int Height)
{
    CurrentWidth = Width;
    CurrentHeight = Height;
    if(Width == 0)
    {
        Width = 1;
    }
    if(Height == 0)
    {
        Height = 1;
    }
    glViewport(0, 0, Width, Height);
    //glMatrixMode(GL_PROJECTION);
}

void keyboard(unsigned char Key, int X, int Y)
{
    if(Key == 'q')
    {
        exit(0);
    }
}

//This function is from http://www.lighthouse3d.com/opengl/glut/index.php?bmpfont
//(slightly modified for style)
void render_bitmap_string(
                          float x, 
                          float y, 
                          float z, 
                          void *font, 
                          char *string)
{  
    char *c;
    glRasterPos3f(x, y,z);
    for (c=string; *c != '\0'; c++)
    {
        glutBitmapCharacter(font, *c);
    }
}

void display()
{
    if(next_image(PixmapData))
    {
        using namespace application;
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glMatrixMode(GL_PROJECTION);
        glPushMatrix();
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
        glPushMatrix();
        glLoadIdentity();
        gluOrtho2D(0,1, 0, 1);
        //gluOrtho2D(-LeftMargin, MaxX + LeftMargin, -BottomMargin, MaxY + BottomMargin);
        //render_graph();
        GLubyte Data[] = { 255, 0, 0, 1, 0, 255, 0, 255, 0, 0, 255, 255, 0, 255, 255, 255 };
        glDisable(GL_DEPTH_TEST);
        glRasterPos3f(0, 0, 0.0);
        for(unsigned int i = 0; i < 100; ++i)
        {
            glDrawPixels(ScreenWidth, ScreenHeight, GL_RGBA, GL_UNSIGNED_BYTE, PixmapData);
        }
        assert(glGetError() == GL_NO_ERROR);
        glutSwapBuffers();
        glMatrixMode(GL_PROJECTION);
        glPopMatrix();
        glMatrixMode(GL_MODELVIEW);
        glPopMatrix();
    }
    else
    {
        close_connection();
        client_deinitialize();
        exit(0);
    }
}

void client_initialize() {}
client_info handshake(const char* const IP)
{
    client_info Info;
    Info.width = 512;
    Info.height = 512;
    return Info;
}
//writes received data to next image into, Data can hold width*height*4 elements (RGBA)
//returns 1 on success, 0 on failure (failure, say, server closed.)
int next_image(unsigned char* Data)
{
    using namespace application;
    int Index = 0;
    for(unsigned int i = 0; i < ScreenWidth * ScreenHeight; ++i)
    {
        Data[Index] = 128;
        Data[Index + 1] = 192;
        Data[Index + 2] = 64;
        Data[Index + 3] = 255;
        Index += 4;
    }
    return 1;
}
void close_connection() {}
void client_deinitialize() {}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.