Commits

Anonymous committed 6d4b240

Converted line endings to unix.

Comments (0)

Files changed (4)

-#!/bin/sh
-g++ ./pixmap_viz_client.cpp ./client.cpp -o ./viz -lglut
-echo "use ./viz to run."
+#!/bin/sh
+g++ ./pixmap_viz_client.cpp ./client.cpp -o ./viz -lglut
+echo "use ./viz to run."
-#ifdef _WIN32
-#define WINDOWS
-#endif
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#ifdef WINDOWS
-#include <Ws2tcpip.h>
-#include <winsock.h>
-#else
-#include <arpa/inet.h>
-#include <netdb.h>
-#include <sys/socket.h>
-#include <sys/types.h>
-#include <unistd.h>
-#endif
-
-#include "client.hpp"
-
-#define PORT "57483" // Port to connect to
-
-int connect_socket;
-client_info c_info;
-struct addrinfo hints, *server_info, *p;
-
-void client_initialize() {
-
-#ifdef WINDOWS
-    WSADATA wsaData;   // if this doesn't work
-    //WSAData wsaData; // then try this instead
-
-    // MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
-    if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
-        fprintf(stderr, "WSAStartup failed.\n");
-        return;
-    }
-#endif
-
-    memset(&hints, 0, sizeof(hints));
-    hints.ai_family = AF_INET;
-    hints.ai_socktype = SOCK_STREAM;
-}
-
-client_info handshake(const char* const IP) {
-    c_info.width = c_info.height = 0;
-    int status;
-    while(1) {
-      status = getaddrinfo(IP, PORT, &hints, &server_info);
-      if (status != 0) {
-        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
-        continue;
-      }
-
-      /* Loop through all results and connect to the first one we can */
-      for (p= server_info; p != NULL; p = p->ai_next) {
-        if ((connect_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
-          perror("Client: socket");
-          continue;
-        }
-
-        if (connect(connect_socket, p->ai_addr, p->ai_addrlen) == -1) {
-#ifdef WINDOWS
-          closesocket(connect_socket);
-#else
-            close(connect_socket);
-#endif
-          perror("Client: connect");
-          continue;
-        }
-        break;
-      }
-
-      if (p == NULL) { /* Failed to connect */
-        fprintf(stderr, "Client: failed to connect\n");
-        continue; /* Keep trying */
-      } else {
-        break;
-      }
-    }
-
-    freeaddrinfo(server_info);
-
-    int buf[2];
-    int num = recv(connect_socket, (char*)buf, 2*sizeof(int), 0);
-    if (num == -1) {
-        perror("Client: recv");
-        c_info.width = c_info.height = 0; // Failure
-        return c_info;
-    }
-    if ((unsigned int)num < sizeof(client_info)) {
-        fprintf(stderr, "Client: only received %d out of %d for info struct\n", num, (int) sizeof(client_info));
-        c_info.width = c_info.height = 0; // Failure
-        return c_info;
-    }
-    c_info.width = buf[0];
-    c_info.height = buf[1];
-
-    return c_info;
-}
-
-int next_image(unsigned char* Data) {
-    int len = c_info.width*c_info.height*4;
-    int chars_recv;
-
-    /* Receive from server */
-    chars_recv = recv(connect_socket, (char*) Data, len, 0);
-    if (chars_recv == -1 || chars_recv == 0) {
-        perror("Client: recv data");
-        return 0;
-    }
-    if (chars_recv < len) {
-        fprintf(stderr, "Client: only received %d out of %d chars\n", chars_recv, len);
-        return 0;
-    }
-    return 1;
-}
-
-void close_connection() {
-#ifdef WINDOWS
-    closesocket(connect_socket);
-#else
-    close(connect_socket);
-#endif
-}
-
-void client_deinitialize() {
-#ifdef WINDOWS
-    WSACleanup();
-#endif
-}
-#pragma comment(lib, "ws2_32")
+#ifdef _WIN32
+#define WINDOWS
+#endif
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#ifdef WINDOWS
+#include <Ws2tcpip.h>
+#include <winsock.h>
+#else
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
+#endif
+
+#include "client.hpp"
+
+#define PORT "57483" // Port to connect to
+
+int connect_socket;
+client_info c_info;
+struct addrinfo hints, *server_info, *p;
+
+void client_initialize() {
+
+#ifdef WINDOWS
+    WSADATA wsaData;   // if this doesn't work
+    //WSAData wsaData; // then try this instead
+
+    // MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:
+    if (WSAStartup(MAKEWORD(2,0), &wsaData) != 0) {
+        fprintf(stderr, "WSAStartup failed.\n");
+        return;
+    }
+#endif
+
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_family = AF_INET;
+    hints.ai_socktype = SOCK_STREAM;
+}
+
+client_info handshake(const char* const IP) {
+    c_info.width = c_info.height = 0;
+    int status;
+    while(1) {
+      status = getaddrinfo(IP, PORT, &hints, &server_info);
+      if (status != 0) {
+        fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(status));
+        continue;
+      }
+
+      /* Loop through all results and connect to the first one we can */
+      for (p= server_info; p != NULL; p = p->ai_next) {
+        if ((connect_socket = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
+          perror("Client: socket");
+          continue;
+        }
+
+        if (connect(connect_socket, p->ai_addr, p->ai_addrlen) == -1) {
+#ifdef WINDOWS
+          closesocket(connect_socket);
+#else
+            close(connect_socket);
+#endif
+          perror("Client: connect");
+          continue;
+        }
+        break;
+      }
+
+      if (p == NULL) { /* Failed to connect */
+        fprintf(stderr, "Client: failed to connect\n");
+        continue; /* Keep trying */
+      } else {
+        break;
+      }
+    }
+
+    freeaddrinfo(server_info);
+
+    int buf[2];
+    int num = recv(connect_socket, (char*)buf, 2*sizeof(int), 0);
+    if (num == -1) {
+        perror("Client: recv");
+        c_info.width = c_info.height = 0; // Failure
+        return c_info;
+    }
+    if ((unsigned int)num < sizeof(client_info)) {
+        fprintf(stderr, "Client: only received %d out of %d for info struct\n", num, (int) sizeof(client_info));
+        c_info.width = c_info.height = 0; // Failure
+        return c_info;
+    }
+    c_info.width = buf[0];
+    c_info.height = buf[1];
+
+    return c_info;
+}
+
+int next_image(unsigned char* Data) {
+    int len = c_info.width*c_info.height*4;
+    int chars_recv;
+
+    /* Receive from server */
+    chars_recv = recv(connect_socket, (char*) Data, len, 0);
+    if (chars_recv == -1 || chars_recv == 0) {
+        perror("Client: recv data");
+        return 0;
+    }
+    if (chars_recv < len) {
+        fprintf(stderr, "Client: only received %d out of %d chars\n", chars_recv, len);
+        return 0;
+    }
+    return 1;
+}
+
+void close_connection() {
+#ifdef WINDOWS
+    closesocket(connect_socket);
+#else
+    close(connect_socket);
+#endif
+}
+
+void client_deinitialize() {
+#ifdef WINDOWS
+    WSACleanup();
+#endif
+}
+#pragma comment(lib, "ws2_32")
-#ifndef HG_CLIENT_HPP
-#define HG_CLIENT_HPP
-
-struct client_info
-{
-    int width;
-    int height;
-};
-
-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();
-
-#endif
+#ifndef HG_CLIENT_HPP
+#define HG_CLIENT_HPP
+
+struct client_info
+{
+    int width;
+    int height;
+};
+
+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();
+
+#endif

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
-
-#include "client.hpp"
-
-///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);
-
-void idle();
-
-unsigned char* PixmapData;
-
-int main(int argc, char* argv[])
-{
-    if(argc != 2)
-    {
-        cout << "Usage: " << argv[0] << " <target ip>\n";
-        return 1;
-    }
-    //
-    cout << "Initializing networking...\n";
-    client_initialize();
-    cout << "Doing handshake...\n";
-    client_info Info = handshake(argv[1]);
-    cout << "Handshake done.\n";
-    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);
-    glutIdleFunc(idle);
-    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);
-    }
-}
-
-void idle()
-{
-    glutPostRedisplay();
-}
-
-//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);
-    }
-}
-
-#if 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] = rand() % 256;
-        Data[Index + 1] = rand() % 256;
-        Data[Index + 2] = rand() % 256;
-        Data[Index + 3] = 255;
-        Index += 4;
-    }
-    return 1;
-}
-void close_connection() {}
-void client_deinitialize() {}
-#endif
+#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
+
+#include "client.hpp"
+
+///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);
+
+void idle();
+
+unsigned char* PixmapData;
+
+int main(int argc, char* argv[])
+{
+    if(argc != 2)
+    {
+        cout << "Usage: " << argv[0] << " <target ip>\n";
+        return 1;
+    }
+    //
+    cout << "Initializing networking...\n";
+    client_initialize();
+    cout << "Doing handshake...\n";
+    client_info Info = handshake(argv[1]);
+    cout << "Handshake done.\n";
+    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);
+    glutIdleFunc(idle);
+    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);
+    }
+}
+
+void idle()
+{
+    glutPostRedisplay();
+}
+
+//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);
+    }
+}
+
+#if 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] = rand() % 256;
+        Data[Index + 1] = rand() % 256;
+        Data[Index + 2] = rand() % 256;
+        Data[Index + 3] = 255;
+        Index += 4;
+    }
+    return 1;
+}
+void close_connection() {}
+void client_deinitialize() {}
+#endif