libpsys

annotate src/pattr.c @ 9:9c24273f211b

first test is done
author John Tsiombikas <nuclear@mutantstargoat.com>
date Wed, 28 Sep 2011 03:42:01 +0300
parents a10f19674147
children 1b0db53d5b5b
rev   line source
nuclear@6 1 #include <stdio.h>
nuclear@6 2 #include <stdlib.h>
nuclear@6 3 #include <string.h>
nuclear@6 4 #include <errno.h>
nuclear@6 5 #include "pattr.h"
nuclear@6 6 #include "psys_gl.h"
nuclear@6 7
nuclear@9 8 static int init_particle_attr(struct psys_particle_attributes *pattr);
nuclear@9 9 static void destroy_particle_attr(struct psys_particle_attributes *pattr);
nuclear@9 10
nuclear@6 11 static void *tex_cls;
nuclear@6 12 static unsigned int (*load_texture)(const char*, void*) = psys_gl_load_texture;
nuclear@6 13 static void (*unload_texture)(unsigned int, void*) = psys_gl_unload_texture;
nuclear@6 14
nuclear@6 15 void psys_texture_loader(unsigned int (*load)(const char*, void*), void (*unload)(unsigned int, void*), void *cls)
nuclear@6 16 {
nuclear@6 17 load_texture = load;
nuclear@6 18 unload_texture = unload;
nuclear@6 19 tex_cls = cls;
nuclear@6 20 }
nuclear@6 21
nuclear@6 22 int psys_init_attr(struct psys_attributes *attr)
nuclear@6 23 {
nuclear@6 24 memset(attr, 0, sizeof *attr);
nuclear@6 25
nuclear@6 26 if(psys_init_track3(&attr->spawn_range) == -1)
nuclear@6 27 goto err;
nuclear@6 28 if(psys_init_track(&attr->rate) == -1)
nuclear@6 29 goto err;
nuclear@6 30 if(psys_init_anm_rnd(&attr->life) == -1)
nuclear@6 31 goto err;
nuclear@6 32 if(psys_init_anm_rnd(&attr->size) == -1)
nuclear@6 33 goto err;
nuclear@6 34 if(psys_init_anm_rnd3(&attr->dir) == -1)
nuclear@6 35 goto err;
nuclear@6 36 if(psys_init_track3(&attr->grav) == -1)
nuclear@6 37 goto err;
nuclear@6 38
nuclear@9 39 if(init_particle_attr(&attr->part_attr) == -1)
nuclear@9 40 goto err;
nuclear@9 41
nuclear@6 42 attr->max_particles = -1;
nuclear@8 43
nuclear@8 44 anm_set_track_default(&attr->size.value.trk, 1.0);
nuclear@8 45 anm_set_track_default(&attr->life.value.trk, 1.0);
nuclear@8 46
nuclear@6 47 return 0;
nuclear@6 48
nuclear@6 49 err:
nuclear@6 50 psys_destroy_attr(attr);
nuclear@6 51 return -1;
nuclear@6 52 }
nuclear@6 53
nuclear@9 54
nuclear@9 55 static int init_particle_attr(struct psys_particle_attributes *pattr)
nuclear@9 56 {
nuclear@9 57 if(psys_init_track3(&pattr->color) == -1) {
nuclear@9 58 return -1;
nuclear@9 59 }
nuclear@9 60 if(psys_init_track(&pattr->alpha) == -1) {
nuclear@9 61 psys_destroy_track3(&pattr->color);
nuclear@9 62 return -1;
nuclear@9 63 }
nuclear@9 64 if(psys_init_track(&pattr->size) == -1) {
nuclear@9 65 psys_destroy_track3(&pattr->color);
nuclear@9 66 psys_destroy_track(&pattr->alpha);
nuclear@9 67 return -1;
nuclear@9 68 }
nuclear@9 69
nuclear@9 70 anm_set_track_default(&pattr->color.x, 1.0);
nuclear@9 71 anm_set_track_default(&pattr->color.y, 1.0);
nuclear@9 72 anm_set_track_default(&pattr->color.z, 1.0);
nuclear@9 73 anm_set_track_default(&pattr->alpha.trk, 1.0);
nuclear@9 74 anm_set_track_default(&pattr->size.trk, 1.0);
nuclear@9 75 return 0;
nuclear@9 76 }
nuclear@9 77
nuclear@9 78
nuclear@6 79 void psys_destroy_attr(struct psys_attributes *attr)
nuclear@6 80 {
nuclear@6 81 psys_destroy_track3(&attr->spawn_range);
nuclear@6 82 psys_destroy_track(&attr->rate);
nuclear@6 83 psys_destroy_anm_rnd(&attr->life);
nuclear@6 84 psys_destroy_anm_rnd(&attr->size);
nuclear@6 85 psys_destroy_anm_rnd3(&attr->dir);
nuclear@6 86 psys_destroy_track3(&attr->grav);
nuclear@6 87
nuclear@9 88 destroy_particle_attr(&attr->part_attr);
nuclear@9 89
nuclear@6 90 if(attr->tex && unload_texture) {
nuclear@6 91 unload_texture(attr->tex, tex_cls);
nuclear@6 92 }
nuclear@6 93 }
nuclear@6 94
nuclear@9 95 static void destroy_particle_attr(struct psys_particle_attributes *pattr)
nuclear@9 96 {
nuclear@9 97 psys_destroy_track3(&pattr->color);
nuclear@9 98 psys_destroy_track(&pattr->alpha);
nuclear@9 99 psys_destroy_track(&pattr->size);
nuclear@9 100 }
nuclear@9 101
nuclear@6 102 void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm)
nuclear@6 103 {
nuclear@6 104 psys_eval_track3(&attr->spawn_range, tm);
nuclear@6 105 psys_eval_track(&attr->rate, tm);
nuclear@6 106 psys_eval_anm_rnd(&attr->life, tm);
nuclear@6 107 psys_eval_anm_rnd(&attr->size, tm);
nuclear@6 108 psys_eval_anm_rnd3(&attr->dir, tm);
nuclear@6 109 psys_eval_track3(&attr->grav, tm);
nuclear@6 110 }
nuclear@6 111
nuclear@6 112 int psys_load_attr(struct psys_attributes *attr, const char *fname)
nuclear@6 113 {
nuclear@6 114 FILE *fp;
nuclear@6 115 int res;
nuclear@6 116
nuclear@6 117 if(!(fp = fopen(fname, "r"))) {
nuclear@6 118 fprintf(stderr, "%s: failed to read file: %s: %s\n", __func__, fname, strerror(errno));
nuclear@6 119 return -1;
nuclear@6 120 }
nuclear@6 121 res = psys_load_attr_stream(attr, fp);
nuclear@6 122 fclose(fp);
nuclear@6 123 return res;
nuclear@6 124 }
nuclear@6 125
nuclear@6 126 int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp)
nuclear@6 127 {
nuclear@6 128 return -1; /* TODO */
nuclear@6 129 }
nuclear@6 130
nuclear@6 131
nuclear@6 132 int psys_save_attr(struct psys_attributes *attr, const char *fname)
nuclear@6 133 {
nuclear@6 134 FILE *fp;
nuclear@6 135 int res;
nuclear@6 136
nuclear@6 137 if(!(fp = fopen(fname, "w"))) {
nuclear@6 138 fprintf(stderr, "%s: failed to write file: %s: %s\n", __func__, fname, strerror(errno));
nuclear@6 139 return -1;
nuclear@6 140 }
nuclear@6 141 res = psys_save_attr_stream(attr, fp);
nuclear@6 142 fclose(fp);
nuclear@6 143 return res;
nuclear@6 144 }
nuclear@6 145
nuclear@6 146 int psys_save_attr_stream(struct psys_attributes *attr, FILE *fp)
nuclear@6 147 {
nuclear@6 148 return -1; /* TODO */
nuclear@6 149 }