conworlds

annotate src/main.cc @ 17:c814f77d177e

moved to SDL2
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 25 Aug 2014 22:02:08 +0300
parents 283cdfa7dda2
children
rev   line source
nuclear@2 1 #include <stdio.h>
nuclear@2 2 #include <stdlib.h>
nuclear@17 3 #include <SDL2/SDL.h>
nuclear@2 4 #include "opengl.h"
nuclear@2 5 #include "game.h"
nuclear@13 6 #include "gameopt.h"
nuclear@6 7 #include "vr/vr.h"
nuclear@2 8
nuclear@17 9 #define ANYPOS SDL_WINDOWPOS_UNDEFINED
nuclear@17 10
nuclear@2 11 static bool init();
nuclear@2 12 static void cleanup();
nuclear@17 13 static void handle_event(SDL_Event *ev);
nuclear@2 14
nuclear@17 15 static SDL_Window *win;
nuclear@17 16 static SDL_GLContext ctx;
nuclear@12 17
nuclear@2 18 int main(int argc, char **argv)
nuclear@2 19 {
nuclear@13 20 if(!parse_args(argc, argv)) {
nuclear@13 21 return 1;
nuclear@13 22 }
nuclear@13 23
nuclear@17 24 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
nuclear@2 25
nuclear@17 26 unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
nuclear@17 27 if(!(win = SDL_CreateWindow("oculus test", ANYPOS, ANYPOS, 1024, 600, flags))) {
nuclear@17 28 fprintf(stderr, "failed to create window\n");
nuclear@17 29 return 1;
nuclear@17 30 }
nuclear@17 31
nuclear@17 32 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
nuclear@17 33 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
nuclear@17 34
nuclear@17 35 if(!(ctx = SDL_GL_CreateContext(win))) {
nuclear@17 36 fprintf(stderr, "failed to create OpenGL context\n");
nuclear@17 37 return 1;
nuclear@17 38 }
nuclear@2 39
nuclear@2 40 if(!init()) {
nuclear@2 41 return 1;
nuclear@2 42 }
nuclear@2 43 atexit(cleanup);
nuclear@2 44
nuclear@17 45 int xsz, ysz;
nuclear@17 46 SDL_GetWindowSize(win, &xsz, &ysz);
nuclear@17 47 game_reshape(xsz, ysz);
nuclear@17 48
nuclear@17 49 for(;;) {
nuclear@17 50 SDL_Event ev;
nuclear@17 51
nuclear@17 52 while(SDL_PollEvent(&ev)) {
nuclear@17 53 handle_event(&ev);
nuclear@17 54 }
nuclear@17 55
nuclear@17 56 unsigned int msec = SDL_GetTicks();
nuclear@17 57
nuclear@17 58 game_update(msec);
nuclear@17 59 game_render();
nuclear@17 60 }
nuclear@17 61
nuclear@2 62 return 0;
nuclear@2 63 }
nuclear@2 64
nuclear@2 65 static bool init()
nuclear@2 66 {
nuclear@2 67 glewInit();
nuclear@2 68
nuclear@2 69 if(!game_init()) {
nuclear@2 70 return false;
nuclear@2 71 }
nuclear@7 72
nuclear@13 73 if(opt.vr) {
nuclear@13 74 int win_xsz = vr_get_opti(VR_OPT_DISPLAY_WIDTH);
nuclear@13 75 int win_ysz = vr_get_opti(VR_OPT_DISPLAY_HEIGHT);
nuclear@13 76 if(win_xsz && win_ysz) {
nuclear@17 77 SDL_SetWindowSize(win, win_xsz, win_ysz);
nuclear@13 78 }
nuclear@7 79 }
nuclear@2 80 return true;
nuclear@2 81 }
nuclear@2 82
nuclear@2 83 static void cleanup()
nuclear@2 84 {
nuclear@2 85 game_cleanup();
nuclear@2 86 }
nuclear@2 87
nuclear@17 88 static void handle_event(SDL_Event *ev)
nuclear@2 89 {
nuclear@11 90 static bool fullscr;
nuclear@12 91 static int prev_xpos, prev_ypos;
nuclear@11 92 static int prev_xsz, prev_ysz;
nuclear@11 93
nuclear@17 94 switch(ev->type) {
nuclear@17 95 case SDL_KEYDOWN:
nuclear@17 96 case SDL_KEYUP:
nuclear@17 97 if(ev->key.keysym.sym == 'f' && ev->key.state == SDL_PRESSED) {
nuclear@17 98 fullscr = !fullscr;
nuclear@17 99 if(fullscr) {
nuclear@17 100 int xoffs, yoffs;
nuclear@11 101
nuclear@17 102 SDL_GetWindowPosition(win, &prev_xpos, &prev_ypos);
nuclear@17 103 SDL_GetWindowSize(win, &prev_xsz, &prev_ysz);
nuclear@11 104
nuclear@17 105 xoffs = vr_get_opti(VR_OPT_WIN_XOFFS);
nuclear@17 106 yoffs = vr_get_opti(VR_OPT_WIN_YOFFS);
nuclear@17 107 if(xoffs || yoffs) {
nuclear@17 108 printf("repositioning: %d,%d\n", xoffs, yoffs);
nuclear@17 109 SDL_SetWindowPosition(win, xoffs, yoffs);
nuclear@17 110 }
nuclear@17 111 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
nuclear@17 112 } else {
nuclear@17 113 SDL_SetWindowFullscreen(win, 0);
nuclear@17 114 /*SDL_SetWindowPosition(win, prev_xpos, prev_ypos);
nuclear@17 115 SDL_SetWindowSize(win, prev_xsz, prev_ysz);*/
nuclear@11 116 }
nuclear@17 117 break;
nuclear@11 118 }
nuclear@17 119
nuclear@17 120 game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
nuclear@17 121 break;
nuclear@17 122
nuclear@17 123 case SDL_MOUSEBUTTONDOWN:
nuclear@17 124 case SDL_MOUSEBUTTONUP:
nuclear@17 125 game_mouse(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
nuclear@17 126 ev->button.x, ev->button.y);
nuclear@17 127 break;
nuclear@17 128
nuclear@17 129 case SDL_MOUSEWHEEL:
nuclear@17 130 game_mwheel(ev->wheel.y);
nuclear@17 131 break;
nuclear@17 132
nuclear@17 133 case SDL_MOUSEMOTION:
nuclear@17 134 game_motion(ev->motion.x, ev->motion.y);
nuclear@17 135 break;
nuclear@17 136
nuclear@17 137 case SDL_WINDOWEVENT:
nuclear@17 138 switch(ev->window.event) {
nuclear@17 139 case SDL_WINDOWEVENT_RESIZED:
nuclear@17 140 game_reshape(ev->window.data1, ev->window.data2);
nuclear@17 141 break;
nuclear@17 142
nuclear@17 143 default:
nuclear@17 144 break;
nuclear@17 145 }
nuclear@17 146
nuclear@17 147 default:
nuclear@11 148 break;
nuclear@11 149 }
nuclear@2 150 }