libgoatvr

annotate src/opt.c @ 24:d659cbedde1d

works on linux with 0.4.4
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 14 Jan 2015 08:03:27 +0200
parents 437fe32ac633
children
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@0 3 #include <string.h>
nuclear@0 4 #include <errno.h>
nuclear@0 5 #include "opt.h"
nuclear@0 6 #include "rbtree.h"
nuclear@0 7
nuclear@0 8 static void opt_del_func(struct rbnode *opt, void *cls)
nuclear@0 9 {
nuclear@0 10 free(opt);
nuclear@0 11 }
nuclear@0 12
nuclear@0 13 void *create_options(void)
nuclear@0 14 {
nuclear@0 15 struct rbtree *db = rb_create(RB_KEY_STRING);
nuclear@0 16 rb_set_delete_func(db, opt_del_func, 0);
nuclear@0 17 return db;
nuclear@0 18 }
nuclear@0 19
nuclear@0 20 void destroy_options(void *optdb)
nuclear@0 21 {
nuclear@0 22 rb_destroy(optdb);
nuclear@0 23 }
nuclear@0 24
nuclear@0 25 void set_option_int(void *optdb, const char *key, int val)
nuclear@0 26 {
nuclear@19 27 int i;
nuclear@0 28 struct option *opt = malloc(sizeof *opt);
nuclear@0 29 if(!opt) {
nuclear@0 30 fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno));
nuclear@0 31 return;
nuclear@0 32 }
nuclear@0 33 opt->type = OTYPE_INT;
nuclear@0 34 opt->ival = val;
nuclear@0 35 opt->fval = (float)val;
nuclear@19 36 for(i=0; i<4; i++) {
nuclear@19 37 opt->vval[i] = opt->fval;
nuclear@19 38 }
nuclear@0 39
nuclear@0 40 if(rb_insert(optdb, (void*)key, opt) == -1) {
nuclear@0 41 fprintf(stderr, "failed to set option: %s\n", key);
nuclear@0 42 }
nuclear@0 43 }
nuclear@0 44
nuclear@0 45 void set_option_float(void *optdb, const char *key, float val)
nuclear@0 46 {
nuclear@19 47 int i;
nuclear@0 48 struct option *opt = malloc(sizeof *opt);
nuclear@0 49 if(!opt) {
nuclear@0 50 fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno));
nuclear@0 51 return;
nuclear@0 52 }
nuclear@0 53 opt->type = OTYPE_FLOAT;
nuclear@0 54 opt->fval = val;
nuclear@0 55 opt->ival = (int)val;
nuclear@19 56 for(i=0; i<4; i++) {
nuclear@19 57 opt->vval[i] = val;
nuclear@19 58 }
nuclear@0 59
nuclear@0 60 if(rb_insert(optdb, (void*)key, opt) == -1) {
nuclear@0 61 fprintf(stderr, "failed to set option: %s\n", key);
nuclear@0 62 }
nuclear@0 63 }
nuclear@0 64
nuclear@19 65 void set_option_vec(void *optdb, const char *key, float *val)
nuclear@19 66 {
nuclear@19 67 int i;
nuclear@19 68 struct option *opt = malloc(sizeof *opt);
nuclear@19 69 if(!opt) {
nuclear@19 70 fprintf(stderr, "failed to set option: %s: %s\n", key, strerror(errno));
nuclear@19 71 return;
nuclear@19 72 }
nuclear@19 73 opt->type = OTYPE_VEC;
nuclear@19 74 for(i=0; i<4; i++) {
nuclear@19 75 opt->vval[i] = val[i];
nuclear@19 76 }
nuclear@19 77 opt->fval = val[0];
nuclear@19 78 opt->ival = (int)val[0];
nuclear@19 79
nuclear@19 80 if(rb_insert(optdb, (void*)key, opt) == -1) {
nuclear@19 81 fprintf(stderr, "failed to set option: %s\n", key);
nuclear@19 82 }
nuclear@19 83 }
nuclear@19 84
nuclear@19 85 void set_option_vec3f(void *optdb, const char *key, float x, float y, float z)
nuclear@19 86 {
nuclear@19 87 float vec[4];
nuclear@19 88 vec[0] = x;
nuclear@19 89 vec[1] = y;
nuclear@19 90 vec[2] = z;
nuclear@19 91 vec[3] = 1.0f;
nuclear@19 92 set_option_vec(optdb, key, vec);
nuclear@19 93 }
nuclear@19 94
nuclear@19 95 void set_option_vec4f(void *optdb, const char *key, float x, float y, float z, float w)
nuclear@19 96 {
nuclear@19 97 float vec[4];
nuclear@19 98 vec[0] = x;
nuclear@19 99 vec[1] = y;
nuclear@19 100 vec[2] = z;
nuclear@19 101 vec[3] = w;
nuclear@19 102 set_option_vec(optdb, key, vec);
nuclear@19 103 }
nuclear@19 104
nuclear@0 105 int get_option_int(void *optdb, const char *key, int *val)
nuclear@0 106 {
nuclear@0 107 struct option *opt = rb_find(optdb, (void*)key);
nuclear@0 108 if(!opt) {
nuclear@0 109 *val = 0;
nuclear@0 110 return -1;
nuclear@0 111 }
nuclear@0 112 *val = opt->ival;
nuclear@0 113 return 0;
nuclear@0 114 }
nuclear@0 115
nuclear@0 116 int get_option_float(void *optdb, const char *key, float *val)
nuclear@0 117 {
nuclear@0 118 struct option *opt = rb_find(optdb, (void*)key);
nuclear@0 119 if(!opt) {
nuclear@0 120 *val = 0.0f;
nuclear@0 121 return -1;
nuclear@0 122 }
nuclear@0 123 *val = opt->fval;
nuclear@0 124 return 0;
nuclear@0 125 }
nuclear@19 126
nuclear@19 127 int get_option_vec(void *optdb, const char *key, float *val)
nuclear@19 128 {
nuclear@19 129 int i;
nuclear@19 130 struct option *opt = rb_find(optdb, (void*)key);
nuclear@19 131 if(!opt) {
nuclear@19 132 val[0] = val[1] = val[2] = val[3] = 0.0f;
nuclear@19 133 return -1;
nuclear@19 134 }
nuclear@19 135
nuclear@19 136 for(i=0; i<4; i++) {
nuclear@19 137 val[i] = opt->vval[i];
nuclear@19 138 }
nuclear@19 139 return 0;
nuclear@22 140 }