glstuff

experiments with opengl2/ogles2/sdl
git clone http://frotz.net/git/glstuff.git
Log | Files | Refs

sdlglue.cc (4224B)


      1 /* Copyright 2013 Brian Swetland <swetland@frotz.net>
      2  *
      3  * Licensed under the Apache License, Version 2.0 (the "License");
      4  * you may not use this file except in compliance with the License.
      5  * You may obtain a copy of the License at
      6  *
      7  *     http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software
     10  * distributed under the License is distributed on an "AS IS" BASIS,
     11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  * See the License for the specific language governing permissions and
     13  * limitations under the License.
     14  */
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <time.h>
     19 
     20 #ifndef _WIN32
     21 #include <unistd.h>
     22 #endif
     23 
     24 #include <SDL.h>
     25 
     26 #ifdef _WIN32
     27 #define GLUE_DEFINE_EXTENSIONS
     28 #endif
     29 
     30 #include "opengl.h"
     31 #include "util.h"
     32 #include "app.h"
     33 
     34 static void die(const char *fmt, ...) {
     35 	fprintf(stderr,"ERROR: %s\n", fmt);
     36 	exit(-1);
     37 }
     38 
     39 #ifdef _WIN32
     40 #if !WITH_SDL2
     41 static int SDL_GL_ExtensionSupported(const char *name) {
     42 	if (strstr((char*)glGetString(GL_EXTENSIONS), name))
     43 		return 1;
     44 	else
     45 		return 0;
     46 }
     47 #endif
     48 
     49 static void glsl_init(void) {
     50 	int n;
     51 	if (!SDL_GL_ExtensionSupported("GL_ARB_shader_objects") ||
     52 		!SDL_GL_ExtensionSupported("GL_ARB_shading_language_100") ||
     53 		!SDL_GL_ExtensionSupported("GL_ARB_vertex_shader") ||
     54 		!SDL_GL_ExtensionSupported("GL_ARB_fragment_shader"))
     55 		die("missing glsl extensions");
     56 	for (n = 0; n < sizeof(fntb)/sizeof(fntb[0]); n++) {
     57 		*fntb[n].func = SDL_GL_GetProcAddress(fntb[n].name);
     58 		if (!(*fntb[n].func))
     59 			die("cannot find func '%s'", fntb[n].name);
     60 	}
     61 }
     62 #else
     63 void glsl_init(void) {}
     64 #endif
     65 
     66 static void quit(void) {
     67 	SDL_Quit();
     68 	exit(0);
     69 }
     70 
     71 App::App() : _width(640), _height(480), _vsync(1), _fps(0) {
     72 	memset(_keystate, 0, sizeof(_keystate));
     73 }
     74 
     75 App::~App() {
     76 }
     77 
     78 void App::setSize(int w, int h) {
     79 	_width = w;
     80 	_height = h;
     81 }
     82 
     83 void App::handleEvents(void) {
     84 	SDL_Event ev;
     85 
     86 	while (SDL_PollEvent(&ev)) {
     87 		switch (ev.type) {
     88 		case SDL_KEYDOWN:
     89 			_keystate[ev.key.keysym.sym] = 1;
     90 			if (ev.key.keysym.sym == SDLK_ESCAPE)
     91 				quit();
     92 			break;
     93 		case SDL_KEYUP:
     94 			_keystate[ev.key.keysym.sym] = 0;
     95 			key(ev.key.keysym.sym);
     96 			break;
     97 		case SDL_QUIT:
     98 			quit();
     99 		}
    100 	}
    101 }
    102 
    103 void App::setOptions(int argc, char **argv) {
    104 	char *x;
    105 	argc--;
    106 	argv++;
    107 	while (argc--) {
    108 		if (!strcmp("-nosync",argv[0])) {
    109 			_vsync = 0;
    110 		} else if (isdigit(argv[0][0]) && (x = strchr(argv[0],'x'))) {
    111 			_width = atoi(argv[0]);
    112 			_height = atoi(x + 1);
    113 		} else {
    114 			fprintf(stderr,"unknown argument '%s'\n",argv[0]);
    115 		}
    116 		argv++;
    117 	}
    118 }
    119 
    120 int App::run(void) {
    121 	time_t t0, t1;
    122 	int count;
    123 #if WITH_SDL2
    124 	SDL_Window *w;
    125 	SDL_GLContext gc;
    126 #endif
    127 
    128 	if (SDL_Init(SDL_INIT_VIDEO))
    129 		die("sdl video init failed");
    130 
    131 	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    132 	SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    133 	SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    134 	SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    135  
    136 	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    137 	SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
    138 
    139 #if 0 
    140 	SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,    8);
    141 	SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,    8);
    142 	SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,    8);
    143 	SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,    8);
    144  
    145 	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,  1);
    146 	SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,  2);
    147 #endif
    148 
    149 	/* enable vsync */
    150 	SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, _vsync);
    151 	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, _vsync);
    152 
    153 #if WITH_SDL2
    154 	if (!(w = SDL_CreateWindow("Test", 0, 0, _width, _height,
    155 		SDL_WINDOW_OPENGL)))
    156 		die("sdl cannot create window");
    157 
    158 	if (!(gc = SDL_GL_CreateContext(w)))
    159 		die("sdl cannot create gl context");
    160 #else
    161 	if (SDL_SetVideoMode(_width, _height, 32,
    162 		SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL) == NULL) 
    163 		die("sdl cannot set mode");
    164 #endif	
    165 
    166 	glsl_init();
    167 
    168 	if (init())
    169 		return -1;
    170 
    171 	t0 = time(0);
    172 	count = 0;
    173 	for (;;) {
    174 		handleEvents();
    175 
    176 		if (render())
    177 			return -1;
    178 
    179 		if (_vsync) {
    180 #if WITH_SDL2 
    181 			SDL_GL_SwapWindow(w);
    182 #else
    183 			SDL_GL_SwapBuffers();
    184 #endif
    185 		} else {
    186 			glFlush();
    187 		}
    188 
    189 		t1 = time(0);
    190 		count++;
    191 		if (t0 != t1) {
    192 			_fps = count;
    193 			printf("%d fps\n", count);
    194 			count = 0;
    195 			t0 = t1;
    196 		}
    197 	}
    198 	return -1;
    199 }
    200