vrmodel

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