vrmodel
view src/main.cc @ 5:9e260c091f75
wrote some server code in the test input tool
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 15 Sep 2014 10:57:31 +0300 |
parents | 76e75cbcb758 |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <errno.h>
4 #include <thread>
5 #include <mutex>
6 #include <SDL2/SDL.h>
7 #include <unistd.h>
8 #include <sys/select.h>
9 #include <sys/types.h>
10 #include <sys/time.h>
11 #include "opengl.h"
12 #include "vport.h"
13 #include "inpclient.h"
15 #define ANYPOS SDL_WINDOWPOS_UNDEFINED
17 void display();
18 bool handle_event(SDL_Event *ev);
19 bool handle_key(int key, bool pressed);
20 void reshape(int x, int y);
21 void inp_thread_func();
23 static SDL_Window *win;
24 static SDL_GLContext ctx;
26 // network input stuff
27 static std::thread inp_thread;
28 static std::mutex inp_mutex;
29 static float inp_pos[3];
30 static unsigned int inp_bn;
32 int main(int argc, char **argv)
33 {
34 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
35 fprintf(stderr, "failed to initialize SDL\n");
36 return 1;
37 }
39 unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
40 if(!(win = SDL_CreateWindow("vrmodel", ANYPOS, ANYPOS, 1280, 800, flags))) {
41 fprintf(stderr, "failed to create window\n");
42 return 1;
43 }
44 if(!(ctx = SDL_GL_CreateContext(win))) {
45 fprintf(stderr, "failed to create OpenGL context\n");
46 SDL_Quit();
47 return 1;
48 }
50 inp_thread = std::thread(inp_thread_func);
52 for(;;) {
53 SDL_Event ev;
54 SDL_WaitEvent(&ev);
56 do {
57 if(!handle_event(&ev)) {
58 goto done;
59 }
60 } while(SDL_PollEvent(&ev));
62 display();
63 }
65 done:
66 inp_thread.join();
67 SDL_Quit();
68 return 0;
69 }
71 void display()
72 {
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
75 int width, height;
76 SDL_GetWindowSize(win, &width, &height);
77 glViewport(0, 0, width, height);
79 glMatrixMode(GL_PROJECTION);
80 glLoadIdentity();
81 gluPerspective(50.0, (float)width / (float)height, 0.5, 1000.0);
83 glMatrixMode(GL_MODELVIEW);
84 glLoadIdentity();
85 glTranslatef(0, 0, -10);
86 glRotatef(25, 1, 0, 0);
88 draw_vport();
90 SDL_GL_SwapWindow(win);
91 }
93 bool handle_event(SDL_Event *ev)
94 {
95 switch(ev->type) {
96 case SDL_KEYDOWN:
97 case SDL_KEYUP:
98 return handle_key(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
100 default:
101 break;
102 }
104 return true;
105 }
107 bool handle_key(int key, bool pressed)
108 {
109 if(pressed) {
110 switch(key) {
111 case 27:
112 return false;
114 default:
115 break;
116 }
117 }
118 return true;
119 }
121 void inp_thread_func()
122 {
123 int s = netinp_start();
124 if(s == -1) {
125 return;
126 }
128 for(;;) {
129 fd_set rdset;
131 FD_ZERO(&rdset);
132 FD_SET(s, &rdset);
134 struct timeval tv;
135 tv.tv_sec = 5;
136 tv.tv_usec = 0;
138 while(select(s + 1, &rdset, 0, 0, &tv) == -1 && errno == EINTR);
140 if(FD_ISSET(s, &rdset)) {
141 inp_mutex.lock();
142 netinp_read(inp_pos, &inp_bn);
143 inp_mutex.unlock();
144 }
145 }
147 netinp_stop();
148 close(s);
149 }