vrmodel

diff src/main.cc @ 4:a32b151fb3c6

moving along slowly
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 11 Sep 2014 00:08:23 +0300
parents 76e75cbcb758
children
line diff
     1.1 --- a/src/main.cc	Mon Sep 01 05:59:31 2014 +0300
     1.2 +++ b/src/main.cc	Thu Sep 11 00:08:23 2014 +0300
     1.3 @@ -1,8 +1,16 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6 +#include <errno.h>
     1.7 +#include <thread>
     1.8 +#include <mutex>
     1.9  #include <SDL2/SDL.h>
    1.10 +#include <unistd.h>
    1.11 +#include <sys/select.h>
    1.12 +#include <sys/types.h>
    1.13 +#include <sys/time.h>
    1.14  #include "opengl.h"
    1.15  #include "vport.h"
    1.16 +#include "inpclient.h"
    1.17  
    1.18  #define ANYPOS	SDL_WINDOWPOS_UNDEFINED
    1.19  
    1.20 @@ -10,10 +18,17 @@
    1.21  bool handle_event(SDL_Event *ev);
    1.22  bool handle_key(int key, bool pressed);
    1.23  void reshape(int x, int y);
    1.24 +void inp_thread_func();
    1.25  
    1.26  static SDL_Window *win;
    1.27  static SDL_GLContext ctx;
    1.28  
    1.29 +// network input stuff
    1.30 +static std::thread inp_thread;
    1.31 +static std::mutex inp_mutex;
    1.32 +static float inp_pos[3];
    1.33 +static unsigned int inp_bn;
    1.34 +
    1.35  int main(int argc, char **argv)
    1.36  {
    1.37  	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
    1.38 @@ -32,6 +47,8 @@
    1.39  		return 1;
    1.40  	}
    1.41  
    1.42 +	inp_thread = std::thread(inp_thread_func);
    1.43 +
    1.44  	for(;;) {
    1.45  		SDL_Event ev;
    1.46  		SDL_WaitEvent(&ev);
    1.47 @@ -46,6 +63,7 @@
    1.48  	}
    1.49  
    1.50  done:
    1.51 +	inp_thread.join();
    1.52  	SDL_Quit();
    1.53  	return 0;
    1.54  }
    1.55 @@ -99,3 +117,33 @@
    1.56  	}
    1.57  	return true;
    1.58  }
    1.59 +
    1.60 +void inp_thread_func()
    1.61 +{
    1.62 +	int s = netinp_start();
    1.63 +	if(s == -1) {
    1.64 +		return;
    1.65 +	}
    1.66 +
    1.67 +	for(;;) {
    1.68 +		fd_set rdset;
    1.69 +
    1.70 +		FD_ZERO(&rdset);
    1.71 +		FD_SET(s, &rdset);
    1.72 +
    1.73 +		struct timeval tv;
    1.74 +		tv.tv_sec = 5;
    1.75 +		tv.tv_usec = 0;
    1.76 +
    1.77 +		while(select(s + 1, &rdset, 0, 0, &tv) == -1 && errno == EINTR);
    1.78 +
    1.79 +		if(FD_ISSET(s, &rdset)) {
    1.80 +			inp_mutex.lock();
    1.81 +			netinp_read(inp_pos, &inp_bn);
    1.82 +			inp_mutex.unlock();
    1.83 +		}
    1.84 +	}
    1.85 +
    1.86 +	netinp_stop();
    1.87 +	close(s);
    1.88 +}