vrmodel

annotate src/main.cc @ 3:a1784a4290c2

client-side networking
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 01 Sep 2014 05:59:31 +0300
parents affaad5fcd30
children a32b151fb3c6
rev   line source
nuclear@1 1 #include <stdio.h>
nuclear@1 2 #include <stdlib.h>
nuclear@1 3 #include <SDL2/SDL.h>
nuclear@1 4 #include "opengl.h"
nuclear@1 5 #include "vport.h"
nuclear@1 6
nuclear@1 7 #define ANYPOS SDL_WINDOWPOS_UNDEFINED
nuclear@1 8
nuclear@1 9 void display();
nuclear@1 10 bool handle_event(SDL_Event *ev);
nuclear@1 11 bool handle_key(int key, bool pressed);
nuclear@1 12 void reshape(int x, int y);
nuclear@1 13
nuclear@1 14 static SDL_Window *win;
nuclear@1 15 static SDL_GLContext ctx;
nuclear@1 16
nuclear@1 17 int main(int argc, char **argv)
nuclear@1 18 {
nuclear@1 19 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
nuclear@1 20 fprintf(stderr, "failed to initialize SDL\n");
nuclear@1 21 return 1;
nuclear@1 22 }
nuclear@1 23
nuclear@1 24 unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
nuclear@1 25 if(!(win = SDL_CreateWindow("vrmodel", ANYPOS, ANYPOS, 1280, 800, flags))) {
nuclear@1 26 fprintf(stderr, "failed to create window\n");
nuclear@1 27 return 1;
nuclear@1 28 }
nuclear@1 29 if(!(ctx = SDL_GL_CreateContext(win))) {
nuclear@1 30 fprintf(stderr, "failed to create OpenGL context\n");
nuclear@1 31 SDL_Quit();
nuclear@1 32 return 1;
nuclear@1 33 }
nuclear@1 34
nuclear@1 35 for(;;) {
nuclear@1 36 SDL_Event ev;
nuclear@1 37 SDL_WaitEvent(&ev);
nuclear@1 38
nuclear@1 39 do {
nuclear@1 40 if(!handle_event(&ev)) {
nuclear@1 41 goto done;
nuclear@1 42 }
nuclear@1 43 } while(SDL_PollEvent(&ev));
nuclear@1 44
nuclear@1 45 display();
nuclear@1 46 }
nuclear@1 47
nuclear@1 48 done:
nuclear@1 49 SDL_Quit();
nuclear@1 50 return 0;
nuclear@1 51 }
nuclear@1 52
nuclear@1 53 void display()
nuclear@1 54 {
nuclear@1 55 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@1 56
nuclear@1 57 int width, height;
nuclear@1 58 SDL_GetWindowSize(win, &width, &height);
nuclear@1 59 glViewport(0, 0, width, height);
nuclear@1 60
nuclear@1 61 glMatrixMode(GL_PROJECTION);
nuclear@1 62 glLoadIdentity();
nuclear@1 63 gluPerspective(50.0, (float)width / (float)height, 0.5, 1000.0);
nuclear@1 64
nuclear@1 65 glMatrixMode(GL_MODELVIEW);
nuclear@1 66 glLoadIdentity();
nuclear@1 67 glTranslatef(0, 0, -10);
nuclear@1 68 glRotatef(25, 1, 0, 0);
nuclear@1 69
nuclear@1 70 draw_vport();
nuclear@1 71
nuclear@1 72 SDL_GL_SwapWindow(win);
nuclear@1 73 }
nuclear@1 74
nuclear@1 75 bool handle_event(SDL_Event *ev)
nuclear@1 76 {
nuclear@1 77 switch(ev->type) {
nuclear@1 78 case SDL_KEYDOWN:
nuclear@1 79 case SDL_KEYUP:
nuclear@1 80 return handle_key(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
nuclear@1 81
nuclear@1 82 default:
nuclear@1 83 break;
nuclear@1 84 }
nuclear@1 85
nuclear@1 86 return true;
nuclear@1 87 }
nuclear@1 88
nuclear@1 89 bool handle_key(int key, bool pressed)
nuclear@1 90 {
nuclear@1 91 if(pressed) {
nuclear@1 92 switch(key) {
nuclear@1 93 case 27:
nuclear@1 94 return false;
nuclear@1 95
nuclear@1 96 default:
nuclear@1 97 break;
nuclear@1 98 }
nuclear@1 99 }
nuclear@1 100 return true;
nuclear@1 101 }