libpsys

annotate src/pattr.c @ 6:d774738f50f6

almost finished with the reorg (2)
author John Tsiombikas <nuclear@mutantstargoat.com>
date Tue, 27 Sep 2011 07:42:54 +0300
parents
children a10f19674147
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@6 8 static void *tex_cls;
nuclear@6 9 static unsigned int (*load_texture)(const char*, void*) = psys_gl_load_texture;
nuclear@6 10 static void (*unload_texture)(unsigned int, void*) = psys_gl_unload_texture;
nuclear@6 11
nuclear@6 12 void psys_texture_loader(unsigned int (*load)(const char*, void*), void (*unload)(unsigned int, void*), void *cls)
nuclear@6 13 {
nuclear@6 14 load_texture = load;
nuclear@6 15 unload_texture = unload;
nuclear@6 16 tex_cls = cls;
nuclear@6 17 }
nuclear@6 18
nuclear@6 19 int psys_init_attr(struct psys_attributes *attr)
nuclear@6 20 {
nuclear@6 21 memset(attr, 0, sizeof *attr);
nuclear@6 22
nuclear@6 23 if(psys_init_track3(&attr->spawn_range) == -1)
nuclear@6 24 goto err;
nuclear@6 25 if(psys_init_track(&attr->rate) == -1)
nuclear@6 26 goto err;
nuclear@6 27 if(psys_init_anm_rnd(&attr->life) == -1)
nuclear@6 28 goto err;
nuclear@6 29 if(psys_init_anm_rnd(&attr->size) == -1)
nuclear@6 30 goto err;
nuclear@6 31 if(psys_init_anm_rnd3(&attr->dir) == -1)
nuclear@6 32 goto err;
nuclear@6 33 if(psys_init_track3(&attr->grav) == -1)
nuclear@6 34 goto err;
nuclear@6 35
nuclear@6 36 attr->max_particles = -1;
nuclear@6 37 return 0;
nuclear@6 38
nuclear@6 39 err:
nuclear@6 40 psys_destroy_attr(attr);
nuclear@6 41 return -1;
nuclear@6 42 }
nuclear@6 43
nuclear@6 44 void psys_destroy_attr(struct psys_attributes *attr)
nuclear@6 45 {
nuclear@6 46 psys_destroy_track3(&attr->spawn_range);
nuclear@6 47 psys_destroy_track(&attr->rate);
nuclear@6 48 psys_destroy_anm_rnd(&attr->life);
nuclear@6 49 psys_destroy_anm_rnd(&attr->size);
nuclear@6 50 psys_destroy_anm_rnd3(&attr->dir);
nuclear@6 51 psys_destroy_track3(&attr->grav);
nuclear@6 52
nuclear@6 53 if(attr->tex && unload_texture) {
nuclear@6 54 unload_texture(attr->tex, tex_cls);
nuclear@6 55 }
nuclear@6 56 }
nuclear@6 57
nuclear@6 58 void psys_eval_attr(struct psys_attributes *attr, anm_time_t tm)
nuclear@6 59 {
nuclear@6 60 psys_eval_track3(&attr->spawn_range, tm);
nuclear@6 61 psys_eval_track(&attr->rate, tm);
nuclear@6 62 psys_eval_anm_rnd(&attr->life, tm);
nuclear@6 63 psys_eval_anm_rnd(&attr->size, tm);
nuclear@6 64 psys_eval_anm_rnd3(&attr->dir, tm);
nuclear@6 65 psys_eval_track3(&attr->grav, tm);
nuclear@6 66 }
nuclear@6 67
nuclear@6 68 int psys_load_attr(struct psys_attributes *attr, const char *fname)
nuclear@6 69 {
nuclear@6 70 FILE *fp;
nuclear@6 71 int res;
nuclear@6 72
nuclear@6 73 if(!(fp = fopen(fname, "r"))) {
nuclear@6 74 fprintf(stderr, "%s: failed to read file: %s: %s\n", __func__, fname, strerror(errno));
nuclear@6 75 return -1;
nuclear@6 76 }
nuclear@6 77 res = psys_load_attr_stream(attr, fp);
nuclear@6 78 fclose(fp);
nuclear@6 79 return res;
nuclear@6 80 }
nuclear@6 81
nuclear@6 82 int psys_load_attr_stream(struct psys_attributes *attr, FILE *fp)
nuclear@6 83 {
nuclear@6 84 return -1; /* TODO */
nuclear@6 85 }
nuclear@6 86
nuclear@6 87
nuclear@6 88 int psys_save_attr(struct psys_attributes *attr, const char *fname)
nuclear@6 89 {
nuclear@6 90 FILE *fp;
nuclear@6 91 int res;
nuclear@6 92
nuclear@6 93 if(!(fp = fopen(fname, "w"))) {
nuclear@6 94 fprintf(stderr, "%s: failed to write file: %s: %s\n", __func__, fname, strerror(errno));
nuclear@6 95 return -1;
nuclear@6 96 }
nuclear@6 97 res = psys_save_attr_stream(attr, fp);
nuclear@6 98 fclose(fp);
nuclear@6 99 return res;
nuclear@6 100 }
nuclear@6 101
nuclear@6 102 int psys_save_attr_stream(struct psys_attributes *attr, FILE *fp)
nuclear@6 103 {
nuclear@6 104 return -1; /* TODO */
nuclear@6 105 }