nuclear@1: #include nuclear@1: #include nuclear@1: #include nuclear@1: #include "opengl.h" nuclear@1: #include "vport.h" nuclear@1: nuclear@1: #define ANYPOS SDL_WINDOWPOS_UNDEFINED nuclear@1: nuclear@1: void display(); nuclear@1: bool handle_event(SDL_Event *ev); nuclear@1: bool handle_key(int key, bool pressed); nuclear@1: void reshape(int x, int y); nuclear@1: nuclear@1: static SDL_Window *win; nuclear@1: static SDL_GLContext ctx; nuclear@1: nuclear@1: int main(int argc, char **argv) nuclear@1: { nuclear@1: if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { nuclear@1: fprintf(stderr, "failed to initialize SDL\n"); nuclear@1: return 1; nuclear@1: } nuclear@1: nuclear@1: unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE; nuclear@1: if(!(win = SDL_CreateWindow("vrmodel", ANYPOS, ANYPOS, 1280, 800, flags))) { nuclear@1: fprintf(stderr, "failed to create window\n"); nuclear@1: return 1; nuclear@1: } nuclear@1: if(!(ctx = SDL_GL_CreateContext(win))) { nuclear@1: fprintf(stderr, "failed to create OpenGL context\n"); nuclear@1: SDL_Quit(); nuclear@1: return 1; nuclear@1: } nuclear@1: nuclear@1: for(;;) { nuclear@1: SDL_Event ev; nuclear@1: SDL_WaitEvent(&ev); nuclear@1: nuclear@1: do { nuclear@1: if(!handle_event(&ev)) { nuclear@1: goto done; nuclear@1: } nuclear@1: } while(SDL_PollEvent(&ev)); nuclear@1: nuclear@1: display(); nuclear@1: } nuclear@1: nuclear@1: done: nuclear@1: SDL_Quit(); nuclear@1: return 0; nuclear@1: } nuclear@1: nuclear@1: void display() nuclear@1: { nuclear@1: glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); nuclear@1: nuclear@1: int width, height; nuclear@1: SDL_GetWindowSize(win, &width, &height); nuclear@1: glViewport(0, 0, width, height); nuclear@1: nuclear@1: glMatrixMode(GL_PROJECTION); nuclear@1: glLoadIdentity(); nuclear@1: gluPerspective(50.0, (float)width / (float)height, 0.5, 1000.0); nuclear@1: nuclear@1: glMatrixMode(GL_MODELVIEW); nuclear@1: glLoadIdentity(); nuclear@1: glTranslatef(0, 0, -10); nuclear@1: glRotatef(25, 1, 0, 0); nuclear@1: nuclear@1: draw_vport(); nuclear@1: nuclear@1: SDL_GL_SwapWindow(win); nuclear@1: } nuclear@1: nuclear@1: bool handle_event(SDL_Event *ev) nuclear@1: { nuclear@1: switch(ev->type) { nuclear@1: case SDL_KEYDOWN: nuclear@1: case SDL_KEYUP: nuclear@1: return handle_key(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); nuclear@1: nuclear@1: default: nuclear@1: break; nuclear@1: } nuclear@1: nuclear@1: return true; nuclear@1: } nuclear@1: nuclear@1: bool handle_key(int key, bool pressed) nuclear@1: { nuclear@1: if(pressed) { nuclear@1: switch(key) { nuclear@1: case 27: nuclear@1: return false; nuclear@1: nuclear@1: default: nuclear@1: break; nuclear@1: } nuclear@1: } nuclear@1: return true; nuclear@1: }