erebus

diff src/main.cc @ 4:93894c232d65

more changes across the board
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 29 Apr 2014 07:38:40 +0300
parents 474a0244f57d
children 9621beb22694
line diff
     1.1 --- a/src/main.cc	Mon Apr 28 15:44:59 2014 +0300
     1.2 +++ b/src/main.cc	Tue Apr 29 07:38:40 2014 +0300
     1.3 @@ -2,11 +2,13 @@
     1.4  #include <stdlib.h>
     1.5  #include <assert.h>
     1.6  #include "opengl.h"
     1.7 +#include "erebus.h"
     1.8  
     1.9  static bool init();
    1.10  static void cleanup();
    1.11  static void resize_rtarget(int xsz, int ysz);
    1.12  static void update_rect(int x, int y, int xsz, int ysz, float *pixels);
    1.13 +static void idle();
    1.14  static void display();
    1.15  static void reshape(int x, int y);
    1.16  static void keyb(unsigned char key, int x, int y);
    1.17 @@ -16,6 +18,10 @@
    1.18  static int width, height, rtex_width, rtex_height;
    1.19  static unsigned int rtex;
    1.20  
    1.21 +static erebus *erb;
    1.22 +static bool render_pending;
    1.23 +
    1.24 +
    1.25  int main(int argc, char **argv)
    1.26  {
    1.27  	glutInitWindowSize(1024, 600);
    1.28 @@ -38,11 +44,27 @@
    1.29  
    1.30  static bool init()
    1.31  {
    1.32 +	if(!(erb = erb_init())) {
    1.33 +		return false;
    1.34 +	}
    1.35 +	erb_setopti(erb, ERB_OPT_WIDTH, width);
    1.36 +	erb_setopti(erb, ERB_OPT_HEIGHT, height);
    1.37 +
    1.38 +	if(erb_load_scene(erb, "scene") == -1) {
    1.39 +		return false;
    1.40 +	}
    1.41 +
    1.42 +	printf("begin rendering\n");
    1.43 +	render_pending = true;
    1.44 +	glutIdleFunc(idle);
    1.45 +	erb_begin_frame(erb, 0);
    1.46 +
    1.47  	return true;
    1.48  }
    1.49  
    1.50  static void cleanup()
    1.51  {
    1.52 +	erb_destroy(erb);
    1.53  }
    1.54  
    1.55  static void resize_rtarget(int xsz, int ysz)
    1.56 @@ -92,8 +114,21 @@
    1.57  	glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, xsz, ysz, GL_RGBA, GL_FLOAT, pixels);
    1.58  }
    1.59  
    1.60 +static void idle()
    1.61 +{
    1.62 +	glutPostRedisplay();
    1.63 +}
    1.64 +
    1.65  static void display()
    1.66  {
    1.67 +	if(render_pending) {
    1.68 +		if(erb_render(erb, 128) == 0) {
    1.69 +			render_pending = false;
    1.70 +			glutIdleFunc(0);
    1.71 +		}
    1.72 +		update_rect(0, 0, width, height, erb_get_framebuffer(erb));
    1.73 +	}
    1.74 +
    1.75  	glBindTexture(GL_TEXTURE_2D, rtex);
    1.76  	glEnable(GL_TEXTURE_2D);
    1.77  
    1.78 @@ -117,6 +152,9 @@
    1.79  {
    1.80  	glViewport(0, 0, x, y);
    1.81  	resize_rtarget(x, y);
    1.82 +
    1.83 +	erb_setopti(erb, ERB_OPT_WIDTH, width);
    1.84 +	erb_setopti(erb, ERB_OPT_HEIGHT, height);
    1.85  }
    1.86  
    1.87  static void keyb(unsigned char key, int x, int y)
    1.88 @@ -124,6 +162,13 @@
    1.89  	switch(key) {
    1.90  	case 27:
    1.91  		exit(0);
    1.92 +
    1.93 +	case ' ':
    1.94 +		printf("begin rendering\n");
    1.95 +		render_pending = true;
    1.96 +		glutIdleFunc(idle);
    1.97 +		erb_begin_frame(erb, 0);
    1.98 +		break;
    1.99  	}
   1.100  }
   1.101