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