vrmodel

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