vrmodel

diff src/main.cc @ 1:76e75cbcb758

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 29 Aug 2014 23:07:59 +0300
parents affaad5fcd30
children a32b151fb3c6
line diff
     1.1 --- a/src/main.cc	Fri Aug 29 18:56:54 2014 +0300
     1.2 +++ b/src/main.cc	Fri Aug 29 23:07:59 2014 +0300
     1.3 @@ -0,0 +1,101 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <SDL2/SDL.h>
     1.7 +#include "opengl.h"
     1.8 +#include "vport.h"
     1.9 +
    1.10 +#define ANYPOS	SDL_WINDOWPOS_UNDEFINED
    1.11 +
    1.12 +void display();
    1.13 +bool handle_event(SDL_Event *ev);
    1.14 +bool handle_key(int key, bool pressed);
    1.15 +void reshape(int x, int y);
    1.16 +
    1.17 +static SDL_Window *win;
    1.18 +static SDL_GLContext ctx;
    1.19 +
    1.20 +int main(int argc, char **argv)
    1.21 +{
    1.22 +	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
    1.23 +		fprintf(stderr, "failed to initialize SDL\n");
    1.24 +		return 1;
    1.25 +	}
    1.26 +
    1.27 +	unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
    1.28 +	if(!(win = SDL_CreateWindow("vrmodel", ANYPOS, ANYPOS, 1280, 800, flags))) {
    1.29 +		fprintf(stderr, "failed to create window\n");
    1.30 +		return 1;
    1.31 +	}
    1.32 +	if(!(ctx = SDL_GL_CreateContext(win))) {
    1.33 +		fprintf(stderr, "failed to create OpenGL context\n");
    1.34 +		SDL_Quit();
    1.35 +		return 1;
    1.36 +	}
    1.37 +
    1.38 +	for(;;) {
    1.39 +		SDL_Event ev;
    1.40 +		SDL_WaitEvent(&ev);
    1.41 +
    1.42 +		do {
    1.43 +			if(!handle_event(&ev)) {
    1.44 +				goto done;
    1.45 +			}
    1.46 +		} while(SDL_PollEvent(&ev));
    1.47 +
    1.48 +		display();
    1.49 +	}
    1.50 +
    1.51 +done:
    1.52 +	SDL_Quit();
    1.53 +	return 0;
    1.54 +}
    1.55 +
    1.56 +void display()
    1.57 +{
    1.58 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    1.59 +
    1.60 +	int width, height;
    1.61 +	SDL_GetWindowSize(win, &width, &height);
    1.62 +	glViewport(0, 0, width, height);
    1.63 +
    1.64 +	glMatrixMode(GL_PROJECTION);
    1.65 +	glLoadIdentity();
    1.66 +	gluPerspective(50.0, (float)width / (float)height, 0.5, 1000.0);
    1.67 +
    1.68 +	glMatrixMode(GL_MODELVIEW);
    1.69 +	glLoadIdentity();
    1.70 +	glTranslatef(0, 0, -10);
    1.71 +	glRotatef(25, 1, 0, 0);
    1.72 +
    1.73 +	draw_vport();
    1.74 +
    1.75 +	SDL_GL_SwapWindow(win);
    1.76 +}
    1.77 +
    1.78 +bool handle_event(SDL_Event *ev)
    1.79 +{
    1.80 +	switch(ev->type) {
    1.81 +	case SDL_KEYDOWN:
    1.82 +	case SDL_KEYUP:
    1.83 +		return handle_key(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
    1.84 +
    1.85 +	default:
    1.86 +		break;
    1.87 +	}
    1.88 +
    1.89 +	return true;
    1.90 +}
    1.91 +
    1.92 +bool handle_key(int key, bool pressed)
    1.93 +{
    1.94 +	if(pressed) {
    1.95 +		switch(key) {
    1.96 +		case 27:
    1.97 +			return false;
    1.98 +
    1.99 +		default:
   1.100 +			break;
   1.101 +		}
   1.102 +	}
   1.103 +	return true;
   1.104 +}