erebus

diff liberebus/src/erebus.cc @ 2:474a0244f57d

fixed specialization mistake fixed line endings added makefiles
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 28 Apr 2014 06:31:10 +0300
parents 4abdce1361b9
children 93894c232d65
line diff
     1.1 --- a/liberebus/src/erebus.cc	Mon Apr 28 05:58:22 2014 +0300
     1.2 +++ b/liberebus/src/erebus.cc	Mon Apr 28 06:31:10 2014 +0300
     1.3 @@ -1,159 +1,159 @@
     1.4 -#include <string.h>
     1.5 -#include <limits.h>
     1.6 -#include <chrono>
     1.7 -#include <random>
     1.8 -#include "erebus.h"
     1.9 -#include "vmath/vector.h"
    1.10 -#include "image.h"
    1.11 -
    1.12 -using namespace std::chrono;
    1.13 -
    1.14 -struct Rect {
    1.15 -	int x, y, width, height;
    1.16 -
    1.17 -	bool operator ==(const Rect &r) { return memcmp(this, &r, sizeof r) == 0; }
    1.18 -	bool operator !=(const Rect &r) { return memcmp(this, &r, sizeof r) != 0; }
    1.19 -};
    1.20 -
    1.21 -#define INVALID_RECT	Rect{0, 0, 0, 0}
    1.22 -
    1.23 -struct erebus {
    1.24 -	Image<float> fbimg;
    1.25 -	Vector4 options[ERB_NUM_OPTIONS];
    1.26 -
    1.27 -	// render state
    1.28 -	long cur_time;
    1.29 -	int cur_pixel_x, cur_pixel_y;
    1.30 -	Rect cur_rect;
    1.31 -};
    1.32 -
    1.33 -static void render_pixel(struct erebus *ctx, int x, int y);
    1.34 -
    1.35 -static std::mt19937 rnd_gen;
    1.36 -
    1.37 -extern "C" {
    1.38 -
    1.39 -struct erebus *erb_init(void)
    1.40 -{
    1.41 -	struct erebus *ctx;
    1.42 -	try {
    1.43 -		ctx = new struct erebus;
    1.44 -	}
    1.45 -	catch(...) {
    1.46 -		return 0;
    1.47 -	}
    1.48 -
    1.49 -	ctx->cur_time = 0;
    1.50 -	ctx->cur_rect = INVALID_RECT;
    1.51 -	return ctx;
    1.52 -}
    1.53 -
    1.54 -void erb_destroy(struct erebus *ctx)
    1.55 -{
    1.56 -	delete ctx;
    1.57 -}
    1.58 -
    1.59 -void erb_setopti(struct erebus *ctx, enum erb_option opt, int val)
    1.60 -{
    1.61 -	ctx->options[opt].x;
    1.62 -}
    1.63 -void erb_setoptf(struct erebus *ctx, enum erb_option opt, float val)
    1.64 -{
    1.65 -	ctx->options[opt].x = val;
    1.66 -}
    1.67 -void erb_setoptfv(struct erebus *ctx, enum erb_option opt, float *vec)
    1.68 -{
    1.69 -	for(int i=0; i<4; i++) {
    1.70 -		ctx->options[opt][i] = vec[i];
    1.71 -	}
    1.72 -}
    1.73 -
    1.74 -int erb_getopti(struct erebus *ctx, enum erb_option opt)
    1.75 -{
    1.76 -	return ctx->options[opt].x;
    1.77 -}
    1.78 -float erb_getoptf(struct erebus *ctx, enum erb_option opt)
    1.79 -{
    1.80 -	return ctx->options[opt].x;
    1.81 -}
    1.82 -float *erb_getoptfv(struct erebus *ctx, enum erb_option opt)
    1.83 -{
    1.84 -	return &ctx->options[opt].x;
    1.85 -}
    1.86 -
    1.87 -float *erb_get_framebuffer(struct erebus *ctx)
    1.88 -{
    1.89 -	return ctx->fbimg.get_pixels();
    1.90 -}
    1.91 -
    1.92 -void erb_begin_frame(struct erebus *ctx, long ms)
    1.93 -{
    1.94 -	ctx->cur_time = ms;
    1.95 -}
    1.96 -
    1.97 -int erb_render(struct erebus *ctx, long timeout)
    1.98 -{
    1.99 -	return erb_render_rect(ctx, 0, 0, ctx->fbimg.get_width(), ctx->fbimg.get_height(), timeout);
   1.100 -}
   1.101 -
   1.102 -int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
   1.103 -{
   1.104 -	if(!width || !height) return -1;
   1.105 -
   1.106 -	Rect rect{x, y, width, height};
   1.107 -	if(ctx->cur_rect != rect) {
   1.108 -		ctx->cur_rect = rect;
   1.109 -		ctx->cur_pixel_x = x;
   1.110 -		ctx->cur_pixel_y = y;
   1.111 -	}
   1.112 -
   1.113 -	if(timeout > 0) {
   1.114 -		auto start_time = monotonic_clock::now();
   1.115 -		while(duration_cast<milliseconds>(monotonic_clock::now() - start_time).count() < timeout) {
   1.116 -			render_pixel(ctx, ctx->cur_pixel_x, ctx->cur_pixel_y);
   1.117 -
   1.118 -			if(++ctx->cur_pixel_x >= ctx->cur_rect.width) {
   1.119 -				if(++ctx->cur_pixel_y >= ctx->cur_rect.height) {
   1.120 -					ctx->cur_rect = INVALID_RECT;
   1.121 -					return 0;
   1.122 -				}
   1.123 -			}
   1.124 -		}
   1.125 -		return 1;
   1.126 -	}
   1.127 -
   1.128 -	for(int i=0; i<height; i++) {
   1.129 -		for(int j=0; j<width; j++) {
   1.130 -			render_pixel(ctx, j, i);
   1.131 -		}
   1.132 -	}
   1.133 -	return 0;
   1.134 -}
   1.135 -
   1.136 -int erb_get_progress(struct erebus *ctx)
   1.137 -{
   1.138 -	return 0;	// TODO
   1.139 -}
   1.140 -
   1.141 -int erb_load_scene(struct erebus *ctx, const char *fname)
   1.142 -{
   1.143 -	//delete ctx->scene;
   1.144 -	//ctx->scene = new Scene;
   1.145 -
   1.146 -	return false;	// TODO
   1.147 -}
   1.148 -
   1.149 -}	// extern "C"
   1.150 -
   1.151 -float randf(float low, float high)
   1.152 -{
   1.153 -	std::uniform_real_distribution<float> unirnd(low, high);
   1.154 -	return unirnd(rnd_gen);
   1.155 -}
   1.156 -
   1.157 -static void render_pixel(struct erebus *ctx, int x, int y)
   1.158 -{
   1.159 -	float *pix = ctx->fbimg.get_pixels() + (y * ctx->fbimg.get_width() + x) * 4;
   1.160 -	pix[0] = pix[1] = pix[2] = 0.0f;
   1.161 -	pix[3] = 1.0f;
   1.162 -}
   1.163 \ No newline at end of file
   1.164 +#include <string.h>
   1.165 +#include <limits.h>
   1.166 +#include <chrono>
   1.167 +#include <random>
   1.168 +#include "erebus.h"
   1.169 +#include "vmath/vector.h"
   1.170 +#include "image.h"
   1.171 +
   1.172 +using namespace std::chrono;
   1.173 +
   1.174 +struct Rect {
   1.175 +	int x, y, width, height;
   1.176 +
   1.177 +	bool operator ==(const Rect &r) { return memcmp(this, &r, sizeof r) == 0; }
   1.178 +	bool operator !=(const Rect &r) { return memcmp(this, &r, sizeof r) != 0; }
   1.179 +};
   1.180 +
   1.181 +#define INVALID_RECT	Rect{0, 0, 0, 0}
   1.182 +
   1.183 +struct erebus {
   1.184 +	Image<float> fbimg;
   1.185 +	Vector4 options[ERB_NUM_OPTIONS];
   1.186 +
   1.187 +	// render state
   1.188 +	long cur_time;
   1.189 +	int cur_pixel_x, cur_pixel_y;
   1.190 +	Rect cur_rect;
   1.191 +};
   1.192 +
   1.193 +static void render_pixel(struct erebus *ctx, int x, int y);
   1.194 +
   1.195 +static std::mt19937 rnd_gen;
   1.196 +
   1.197 +extern "C" {
   1.198 +
   1.199 +struct erebus *erb_init(void)
   1.200 +{
   1.201 +	struct erebus *ctx;
   1.202 +	try {
   1.203 +		ctx = new struct erebus;
   1.204 +	}
   1.205 +	catch(...) {
   1.206 +		return 0;
   1.207 +	}
   1.208 +
   1.209 +	ctx->cur_time = 0;
   1.210 +	ctx->cur_rect = INVALID_RECT;
   1.211 +	return ctx;
   1.212 +}
   1.213 +
   1.214 +void erb_destroy(struct erebus *ctx)
   1.215 +{
   1.216 +	delete ctx;
   1.217 +}
   1.218 +
   1.219 +void erb_setopti(struct erebus *ctx, enum erb_option opt, int val)
   1.220 +{
   1.221 +	ctx->options[opt].x = val;
   1.222 +}
   1.223 +void erb_setoptf(struct erebus *ctx, enum erb_option opt, float val)
   1.224 +{
   1.225 +	ctx->options[opt].x = val;
   1.226 +}
   1.227 +void erb_setoptfv(struct erebus *ctx, enum erb_option opt, float *vec)
   1.228 +{
   1.229 +	for(int i=0; i<4; i++) {
   1.230 +		ctx->options[opt][i] = vec[i];
   1.231 +	}
   1.232 +}
   1.233 +
   1.234 +int erb_getopti(struct erebus *ctx, enum erb_option opt)
   1.235 +{
   1.236 +	return ctx->options[opt].x;
   1.237 +}
   1.238 +float erb_getoptf(struct erebus *ctx, enum erb_option opt)
   1.239 +{
   1.240 +	return ctx->options[opt].x;
   1.241 +}
   1.242 +float *erb_getoptfv(struct erebus *ctx, enum erb_option opt)
   1.243 +{
   1.244 +	return &ctx->options[opt].x;
   1.245 +}
   1.246 +
   1.247 +float *erb_get_framebuffer(struct erebus *ctx)
   1.248 +{
   1.249 +	return ctx->fbimg.get_pixels();
   1.250 +}
   1.251 +
   1.252 +void erb_begin_frame(struct erebus *ctx, long ms)
   1.253 +{
   1.254 +	ctx->cur_time = ms;
   1.255 +}
   1.256 +
   1.257 +int erb_render(struct erebus *ctx, long timeout)
   1.258 +{
   1.259 +	return erb_render_rect(ctx, 0, 0, ctx->fbimg.get_width(), ctx->fbimg.get_height(), timeout);
   1.260 +}
   1.261 +
   1.262 +int erb_render_rect(struct erebus *ctx, int x, int y, int width, int height, long timeout)
   1.263 +{
   1.264 +	if(!width || !height) return -1;
   1.265 +
   1.266 +	Rect rect{x, y, width, height};
   1.267 +	if(ctx->cur_rect != rect) {
   1.268 +		ctx->cur_rect = rect;
   1.269 +		ctx->cur_pixel_x = x;
   1.270 +		ctx->cur_pixel_y = y;
   1.271 +	}
   1.272 +
   1.273 +	if(timeout > 0) {
   1.274 +		auto start_time = steady_clock::now();
   1.275 +		while(duration_cast<milliseconds>(steady_clock::now() - start_time).count() < timeout) {
   1.276 +			render_pixel(ctx, ctx->cur_pixel_x, ctx->cur_pixel_y);
   1.277 +
   1.278 +			if(++ctx->cur_pixel_x >= ctx->cur_rect.width) {
   1.279 +				if(++ctx->cur_pixel_y >= ctx->cur_rect.height) {
   1.280 +					ctx->cur_rect = INVALID_RECT;
   1.281 +					return 0;
   1.282 +				}
   1.283 +			}
   1.284 +		}
   1.285 +		return 1;
   1.286 +	}
   1.287 +
   1.288 +	for(int i=0; i<height; i++) {
   1.289 +		for(int j=0; j<width; j++) {
   1.290 +			render_pixel(ctx, j, i);
   1.291 +		}
   1.292 +	}
   1.293 +	return 0;
   1.294 +}
   1.295 +
   1.296 +int erb_get_progress(struct erebus *ctx)
   1.297 +{
   1.298 +	return 0;	// TODO
   1.299 +}
   1.300 +
   1.301 +int erb_load_scene(struct erebus *ctx, const char *fname)
   1.302 +{
   1.303 +	//delete ctx->scene;
   1.304 +	//ctx->scene = new Scene;
   1.305 +
   1.306 +	return false;	// TODO
   1.307 +}
   1.308 +
   1.309 +}	// extern "C"
   1.310 +
   1.311 +float randf(float low, float high)
   1.312 +{
   1.313 +	std::uniform_real_distribution<float> unirnd(low, high);
   1.314 +	return unirnd(rnd_gen);
   1.315 +}
   1.316 +
   1.317 +static void render_pixel(struct erebus *ctx, int x, int y)
   1.318 +{
   1.319 +	float *pix = ctx->fbimg.get_pixels() + (y * ctx->fbimg.get_width() + x) * 4;
   1.320 +	pix[0] = pix[1] = pix[2] = 0.0f;
   1.321 +	pix[3] = 1.0f;
   1.322 +}