ckb-next  v0.2.8 at branch master
ckb-next driver for corsair devices
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
main.c
Go to the documentation of this file.
1 #include "../ckb/ckb-anim.h"
2 #include <math.h>
3 #include <stdlib.h>
4 #include <time.h>
5 
6 void ckb_info(){
7  // Plugin info
8  CKB_NAME("Raindrop");
9  CKB_VERSION("0.10");
10  CKB_COPYRIGHT("2014-2016", "MSC");
11  CKB_LICENSE("GPLv2");
12  CKB_GUID("{5D6695AF-0496-41E2-BEE7-F7D0ABAA49E9}");
13  CKB_DESCRIPTION("A rain-like effect.");
14 
15  // Effect parameters
16  CKB_PARAM_RGB("color", "Drop color:", "", 255, 255, 255);
17  CKB_PARAM_DOUBLE("intensity", "Intensity:", "%", 100, 0, 100);
18  CKB_PARAM_DOUBLE("size", "Size:", "%", 25, 1, 100);
19  CKB_PARAM_LONG("frequency", "Frequency:", "per second", 10, 1, 100);
20 
21  // Timing/input parameters
26 
27  // Presets
28  CKB_PRESET_START("Default");
29  CKB_PRESET_PARAM("trigger", "1");
30  CKB_PRESET_PARAM("kptrigger", "1");
32 }
33 
34 #define DROP_MAX 1000
35 
36 int spawn = 0;
37 double period = 0.1;
38 double maxsize = 100.;
39 double speed = 100.;
40 unsigned char ar = 255, ag = 255, ab = 255;
41 float aa = 1.;
42 struct {
43  int active;
44  float x, y;
45  float size;
46  float msize;
47 } drop[DROP_MAX];
48 
49 void drop_add(float x, float y, int slow){
50  for(int i = 0; i < DROP_MAX; i++){
51  if(drop[i].active)
52  continue;
53  drop[i].active = 1;
54  drop[i].x = x;
55  drop[i].y = y;
56  float msize = maxsize * (0.9 + (rand() / (double)RAND_MAX * 0.2));
57  drop[i].size = -msize / 2. * slow;
58  drop[i].msize = msize;
59  return;
60  }
61 }
62 
63 void ckb_init(ckb_runctx* context){
64  srand(time(NULL));
65 }
66 
67 void ckb_parameter(ckb_runctx* context, const char* name, const char* value){
68  CKB_PARSE_RGB("color", &ar, &ag, &ab){}
69  double intensity, size;
70  long frequency;
71  CKB_PARSE_DOUBLE("intensity", &intensity){
72  aa = intensity / 100.;
73  }
74  CKB_PARSE_DOUBLE("size", &size){
75  double sizex = context->width / 2.;
76  double sizey = context->height / 2.;
77  maxsize = size / 100. * sqrt(sizex * sizex + sizey * sizey);
78  speed = maxsize;
79  }
80  CKB_PARSE_LONG("frequency", &frequency){
81  period = 1. / frequency;
82  }
83  CKB_PARSE_BOOL("trigger", &spawn){}
84 }
85 
86 void ckb_keypress(ckb_runctx* context, ckb_key* key, int x, int y, int state){
87  // Add a drop on keypress
88  if(state)
89  drop_add(x, y, 0);
90 }
91 
92 double tick = -1.;
93 
94 void ckb_start(ckb_runctx* context, int state){
95  // Start or stop random spawning
96  tick = state ? 0. : -1.;
97 }
98 
99 void ckb_time(ckb_runctx* context, double delta){
100  if(delta <= 0.)
101  return;
102  // Process existing drops
103  for(unsigned i = 0; i < DROP_MAX; i++){
104  if(drop[i].active){
105  drop[i].size += delta * speed;
106  if(drop[i].size > drop[i].msize)
107  drop[i].active = 0;
108  }
109  }
110  if(tick >= 0.){
111  // Spawn a new randomly-placed drop, if the spawn time has passed
112  tick += delta;
113  if(tick > period && spawn){
114  drop_add(rand() / (double)RAND_MAX * context->width, rand() / (double)RAND_MAX * context->height, 1);
115  tick -= period;
116  }
117  }
118 }
119 
120 int ckb_frame(ckb_runctx* context){
121  CKB_KEYCLEAR(context);
122  // Draw drops
123  for(unsigned i = 0; i < DROP_MAX; i++){
124  if(drop[i].active){
125  unsigned count = context->keycount;
126  ckb_key* keys = context->keys;
127  for(ckb_key* key = keys; key < keys + count; key++){
128  // Calculate distance between key and drop, relative to the current drop size
129  float distance = drop[i].size - sqrt(pow(key->x - drop[i].x, 2.f) + pow(key->y - drop[i].y, 2.f));
130  // On the outside, cut the distance in half
131  if(distance < 0.)
132  distance = -distance / 2.;
133  // Scale according to drop size (fade-out radius expands as the drop becomes larger)
134  float scale = drop[i].size / 4.f;
135  if(scale < 10.)
136  scale = 10.;
137  distance /= scale;
138  if(distance <= 1.){
139  // Scale alpha according to size divided by maximum size (drops fade out as they expand)
140  float ascale = (1.f - drop[i].size / drop[i].msize);
141  if(ascale > 1.f)
142  ascale = 1.f;
143  // Apply color
144  ckb_alpha_blend(key, (1.f - distance) * ascale * aa * 255., ar, ag, ab);
145  }
146  }
147  }
148  }
149  return 0;
150 }
#define CKB_PRESET_END
Definition: ckb-anim.h:87
#define FALSE
Definition: ckb-anim.h:49
void ckb_keypress(ckb_runctx *context, ckb_key *key, int x, int y, int state)
Definition: main.c:75
void ckb_parameter(ckb_runctx *context, const char *name, const char *value)
Definition: main.c:68
#define CKB_PARAM_DOUBLE(name, prefix, postfix, default, min, max)
Definition: ckb-anim.h:75
#define CKB_VERSION(version)
Definition: ckb-anim.h:64
#define CKB_TIMEMODE(mode)
Definition: ckb-anim.h:97
float y
Definition: main.c:66
#define TRUE
Definition: ckb-anim.h:46
double maxsize
Definition: main.c:38
#define CKB_GUID(guid)
Definition: ckb-anim.h:60
unsigned char ag
Definition: main.c:40
void drop_add(float x, float y, int slow)
Definition: main.c:49
#define CKB_NAME(name)
Definition: ckb-anim.h:62
#define DROP_MAX
Definition: main.c:34
#define CKB_PARAM_LONG(name, prefix, postfix, default, min, max)
Definition: ckb-anim.h:74
float x
Definition: main.c:66
#define CKB_TIME_ABSOLUTE
Definition: ckb-anim.h:96
#define CKB_LICENSE(license)
Definition: ckb-anim.h:68
#define CKB_KP_POSITION
Definition: ckb-anim.h:92
#define CKB_COPYRIGHT(year, author)
Definition: ckb-anim.h:66
Definition: keymap.h:49
unsigned height
Definition: ckb-anim.h:143
#define CKB_DESCRIPTION(description)
Definition: ckb-anim.h:70
int ckb_frame(ckb_runctx *context)
Definition: main.c:137
#define CKB_PARSE_RGB(param_name, r_ptr, g_ptr, b_ptr)
Definition: ckb-anim.h:118
unsigned keycount
Definition: ckb-anim.h:141
#define CKB_PRESET_START(name)
Definition: ckb-anim.h:85
double period
Definition: main.c:37
void ckb_time(ckb_runctx *context, double delta)
Definition: main.c:126
#define CKB_KPMODE(mode)
Definition: ckb-anim.h:93
#define CKB_LIVEPARAMS(enable)
Definition: ckb-anim.h:104
int spawn
Definition: main.c:36
void ckb_info()
Definition: main.c:5
#define CKB_PARSE_LONG(param_name, value_ptr)
Definition: ckb-anim.h:115
#define CKB_PARSE_BOOL(param_name, value_ptr)
Definition: ckb-anim.h:117
void ckb_init(ckb_runctx *context)
Definition: main.c:57
#define CKB_REPEAT(enable)
Definition: ckb-anim.h:99
#define CKB_KEYCLEAR(context)
Definition: ckb-anim.h:148
void ckb_start(ckb_runctx *context, int state)
Definition: main.c:114
#define CKB_PRESET_PARAM(name, value)
Definition: ckb-anim.h:86
double tick
Definition: main.c:92
ckb_key * keys
Definition: ckb-anim.h:140
float aa
Definition: main.c:41
#define CKB_PARSE_DOUBLE(param_name, value_ptr)
Definition: ckb-anim.h:116
unsigned char ar
Definition: main.c:40
#define CKB_PARAM_RGB(name, prefix, postfix, r, g, b)
Definition: ckb-anim.h:77
double speed
Definition: main.c:39
struct @7 drop[1000]
void ckb_alpha_blend(ckb_key *key, float a, float r, float g, float b)
Definition: ckb-anim.h:283
unsigned width
Definition: ckb-anim.h:143
unsigned char ab
Definition: main.c:40