vrheights

diff src/main.cc @ 0:ccbd444939a1

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 22 Sep 2014 18:36:24 +0300
parents
children b49461618f61
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/main.cc	Mon Sep 22 18:36:24 2014 +0300
     1.3 @@ -0,0 +1,94 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <SDL2/SDL.h>
     1.7 +#include "game.h"
     1.8 +
     1.9 +static bool init();
    1.10 +static void cleanup();
    1.11 +static void handle_event(SDL_Event *ev);
    1.12 +
    1.13 +static bool done;
    1.14 +static SDL_Window *win;
    1.15 +static SDL_GLContext ctx;
    1.16 +
    1.17 +int main(int argc, char **argv)
    1.18 +{
    1.19 +	if(!init()) {
    1.20 +		return 1;
    1.21 +	}
    1.22 +
    1.23 +	for(;;) {
    1.24 +		SDL_Event ev;
    1.25 +		while(SDL_PollEvent(&ev)) {
    1.26 +			handle_event(&ev);
    1.27 +			if(done) break;
    1.28 +		}
    1.29 +
    1.30 +		game_update(SDL_GetTicks());
    1.31 +		game_display();
    1.32 +	}
    1.33 +
    1.34 +	cleanup();
    1.35 +	return 0;
    1.36 +}
    1.37 +
    1.38 +void exit_game()
    1.39 +{
    1.40 +	done = true;
    1.41 +}
    1.42 +
    1.43 +static bool init()
    1.44 +{
    1.45 +	if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
    1.46 +		fprintf(stderr, "failed to initialize SDL\n");
    1.47 +		return false;
    1.48 +	}
    1.49 +
    1.50 +	win = SDL_CreateWindow("vrheights", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
    1.51 +		1280, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
    1.52 +	if(!win) {
    1.53 +		fprintf(stderr, "failed to create window\n");
    1.54 +		return false;
    1.55 +	}
    1.56 +
    1.57 +	if(!(ctx = SDL_GL_CreateContext(win))) {
    1.58 +		fprintf(stderr, "failed to create OpenGL context\n");
    1.59 +		return false;
    1.60 +	}
    1.61 +
    1.62 +	return game_init();
    1.63 +}
    1.64 +
    1.65 +static void cleanup()
    1.66 +{
    1.67 +	game_cleanup();
    1.68 +	SDL_Quit();
    1.69 +}
    1.70 +
    1.71 +static void handle_event(SDL_Event *ev)
    1.72 +{
    1.73 +	switch(ev->type) {
    1.74 +	case SDL_WINDOWEVENT:
    1.75 +		if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
    1.76 +			game_reshape(ev->window.data1, ev->window.data2);
    1.77 +		}
    1.78 +		break;
    1.79 +
    1.80 +	case SDL_KEYDOWN:
    1.81 +	case SDL_KEYUP:
    1.82 +		game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
    1.83 +		break;
    1.84 +
    1.85 +	case SDL_MOUSEBUTTONDOWN:
    1.86 +	case SDL_MOUSEBUTTONUP:
    1.87 +		game_mouse_button(ev->button.button, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y);
    1.88 +		break;
    1.89 +
    1.90 +	case SDL_MOUSEMOTION:
    1.91 +		game_mouse_motion(ev->motion.x, ev->motion.y);
    1.92 +		break;
    1.93 +
    1.94 +	default:
    1.95 +		break;
    1.96 +	}
    1.97 +}
    1.98 \ No newline at end of file