]> www.hackdaworld.org Git - physik/nlsop.git/blob - dfbapi.c
changes in ballistic term & checking for p before getting random value
[physik/nlsop.git] / dfbapi.c
1 /*
2  * scientific visualization api for direct framebuffer
3  *
4  * author: hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #include <stdio.h>
9 #include <directfb.h>
10 #include "dfbapi.h"
11
12 /* two dimensional lattice */
13
14 int d2_lattice_init(int *argc,char **argv,d2_lattice *d2_l)
15 {
16  DFBSurfaceDescription surface_dsc;
17  DFBFontDescription font_dsc;
18  
19  DirectFBInit(argc,&argv);
20  DirectFBCreate(&(d2_l->dfb));
21  d2_l->dfb->SetCooperativeLevel(d2_l->dfb,DFSCL_FULLSCREEN);
22
23  surface_dsc.flags=DSDESC_CAPS;
24  surface_dsc.caps=DSCAPS_PRIMARY|DSCAPS_FLIPPING;
25  d2_l->dfb->CreateSurface(d2_l->dfb,&surface_dsc,&(d2_l->p_surface));
26  d2_l->p_surface->GetSize(d2_l->p_surface,&(d2_l->s_width),&(d2_l->s_height));
27  
28  font_dsc.flags=DFDESC_HEIGHT;
29  font_dsc.height=d2_l->s_height/60; /* 60 ? */
30  d2_l->font_h=font_dsc.height;
31  d2_l->dfb->CreateFont(d2_l->dfb,FONT,&font_dsc,&(d2_l->font));
32  d2_l->p_surface->SetFont(d2_l->p_surface,d2_l->font);
33
34  d2_l->fakt_y=(d2_l->s_height-(2*Y_GAP))/(d2_l->max_y);
35  d2_l->fakt_x=(d2_l->s_height-X_GAP)/(d2_l->max_x); /* bullshit, i can't imagine atm */
36  d2_l->info_x=d2_l->fakt_x*d2_l->max_x+2*X_GAP;
37  d2_l->info_y=Y_GAP;
38  d2_l->info_w=d2_l->s_width-d2_l->info_x-2*X_GAP;
39  d2_l->info_h=d2_l->max_y*d2_l->fakt_y;
40
41  if((d2_l->s_height<(d2_l->max_y+2*Y_GAP)) || (d2_l->s_height<(d2_l->max_x+X_GAP)))
42  {
43   puts("resolution too low!");
44   return -1;
45  } else return 1;
46 }
47
48 int d3_lattice_init(int *argc,char **argv,d3_lattice *d3_l)
49 {
50  DFBSurfaceDescription surface_dsc;
51  DFBFontDescription font_dsc;
52  
53  DirectFBInit(argc,&argv);
54  DirectFBCreate(&(d3_l->dfb));
55  d3_l->dfb->SetCooperativeLevel(d3_l->dfb,DFSCL_FULLSCREEN);
56
57  surface_dsc.flags=DSDESC_CAPS;
58  surface_dsc.caps=DSCAPS_PRIMARY|DSCAPS_FLIPPING;
59  d3_l->dfb->CreateSurface(d3_l->dfb,&surface_dsc,&(d3_l->p_surface));
60  d3_l->p_surface->GetSize(d3_l->p_surface,&(d3_l->s_width),&(d3_l->s_height));
61  
62  font_dsc.flags=DFDESC_HEIGHT;
63  font_dsc.height=d3_l->s_height/40; /* 40 ? */
64  d3_l->font_h=font_dsc.height;
65  d3_l->dfb->CreateFont(d3_l->dfb,FONT,&font_dsc,&(d3_l->font));
66  d3_l->p_surface->SetFont(d3_l->p_surface,d3_l->font);
67
68  d3_l->fakt_y=(d3_l->s_height-(2*Y_GAP))/d3_l->max_z;
69  d3_l->fakt_x=(d3_l->s_width-(4*X_GAP))/(d3_l->max_x+d3_l->max_y+d3_l->max_x);
70  d3_l->info_x=d3_l->fakt_x*(d3_l->max_x+d3_l->max_y)+(3*X_GAP);
71  d3_l->info_y=d3_l->fakt_y*d3_l->max_y+(2*Y_GAP);
72  d3_l->o_x=d3_l->fakt_x*d3_l->max_x+(2*X_GAP);
73  d3_l->info_w=d3_l->fakt_x*d3_l->max_x;
74  d3_l->info_h=d3_l->fakt_y*(d3_l->max_z-d3_l->max_y)-Y_GAP;
75
76  return 1;
77 }
78
79 int d2_lattice_release(d2_lattice *d2_l)
80 {
81  d2_l->font->Release(d2_l->font);
82  d2_l->p_surface->Release(d2_l->p_surface);
83  d2_l->dfb->Release(d2_l->dfb);
84
85  return 1;
86 }
87
88 int d3_lattice_release(d3_lattice *d3_l)
89 {
90  d3_l->font->Release(d3_l->font);
91  d3_l->p_surface->Release(d3_l->p_surface);
92  d3_l->dfb->Release(d3_l->dfb);
93
94  return 1;
95 }
96
97 int dx_lattice_get_color(unsigned char *status,unsigned char *r,unsigned char *g,unsigned char *b,unsigned char mode)
98 {
99  *g=0;
100  if((!mode)||mode==3)
101  {
102   if((*status)&RED)
103   {
104    *r=0xff;
105    *b=0;
106   } else
107   {
108    *r=0;
109    *b=0xff;
110   }
111  } else if(mode==1)
112  {
113   *r=*status;
114   *g=*status;
115   *b=0xff;
116  } else if(mode==2)
117  {
118   *r=*status;
119   *b=0xff;
120  }
121  
122  return 1;
123 }
124
125 int d2_lattice_draw(d2_lattice *d2_l,int x,int y,int arg_c,char **arg_v,unsigned char mode,int max_extra)
126 {
127  int x_c,y_c;
128  int i;
129  unsigned char r,g,b,a;
130  unsigned char foobar;
131
132  a=0xff; /* no alpha blending */
133  
134  for(x_c=0;x_c<d2_l->max_x;x_c++)
135  {
136   for(y_c=0;y_c<d2_l->max_y;y_c++)
137   {
138    if(!mode) dx_lattice_get_color((*d2_l).status+x_c+y_c*d2_l->max_x,&r,&g,&b,mode);
139    if(mode==1)
140    {
141     foobar=(unsigned char)(*(d2_l->extra+x_c+y_c*d2_l->max_x)*255/max_extra);
142     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
143    }
144    if(mode==2)
145    {
146     foobar=(*(unsigned char *)(d2_l->v_ptr+x_c+y_c*d2_l->max_x));
147     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
148    }
149    if(x_c==x && y_c==y)
150    {
151     r=0xff;
152     g=0xff;
153     b=0;
154    }
155    d2_l->p_surface->SetColor(d2_l->p_surface,r,g,b,a);
156    d2_l->p_surface->DrawRectangle(d2_l->p_surface,
157                                   x_c*d2_l->fakt_x+X_GAP,
158                                   y_c*d2_l->fakt_y+Y_GAP,
159                                   d2_l->fakt_x,d2_l->fakt_y);
160   }
161  }
162  r=0xff;
163  g=0xff;
164  b=0;
165  /* clear info box */
166  d2_l->p_surface->SetColor(d2_l->p_surface,0,0,0,0);
167  d2_l->p_surface->FillRectangle(d2_l->p_surface,
168                                 d2_l->info_x,d2_l->info_y,
169                                 d2_l->info_w,d2_l->info_h);
170  d2_l->p_surface->SetColor(d2_l->p_surface,r,g,b,a);
171  d2_l->p_surface->DrawRectangle(d2_l->p_surface,
172                                 d2_l->info_x,d2_l->info_y,
173                                 d2_l->info_w,d2_l->info_h);
174  d2_l->p_surface->SetColor(d2_l->p_surface,0x80,0x80,0xff,0xff);
175  for(i=1;i<=arg_c;i++)
176  {
177   d2_l->p_surface->DrawString(d2_l->p_surface,arg_v[i],-1,
178                               d2_l->info_x+d2_l->fakt_x,
179                               d2_l->info_y+d2_l->fakt_y+d2_l->font_h+(i-1)*2*d2_l->font_h,
180                               DSTF_LEFT);
181  }
182  /* now we flip all to surface */
183  d2_l->p_surface->Flip(d2_l->p_surface,NULL,0);
184  
185  return 1;
186 }
187
188 int d3_lattice_draw(d3_lattice *d3_l,int x,int y,int z,int arg_c,char **arg_v,unsigned char mode,int max_extra,u32 *p1,u32 max1,u32 *p2,u32 max2)
189 {
190  int x_c,y_c,z_c;
191  int i;
192  unsigned char foobar;
193  unsigned char r,g,b,a;
194
195  a=0xff; /* no alpha blending */
196
197  y_c=y;
198  for(x_c=0;x_c<d3_l->max_x;x_c++)
199  {
200   for(z_c=0;z_c<d3_l->max_z;z_c++)
201   {
202    if((!mode)||mode==3) dx_lattice_get_color((*d3_l).status+x_c+y_c*d3_l->max_x+z_c*d3_l->max_x*d3_l->max_y,&r,&g,&b,mode);
203    if(mode==3)
204    {
205     if(d3_l->max_x*p1[z_c]/max1==x_c)
206     {
207      r=0xff;
208      g=0xff;
209      b=0xff;
210     }
211    }
212    if(mode==1)
213    {
214     foobar=(unsigned char)(*(d3_l->extra+x_c+y_c*d3_l->max_x+z_c*d3_l->max_x*d3_l->max_y)*255/max_extra);
215     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
216    }
217    if(mode==2)
218    {
219     foobar=(*(unsigned char *)(d3_l->v_ptr+x_c+y_c*d3_l->max_x+z_c*d3_l->max_x*d3_l->max_y));
220     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
221    }
222    if(x_c==x && z_c==z)
223    {
224     r=0xff;
225     g=0xff;
226     b=0;
227    }
228    d3_l->p_surface->SetColor(d3_l->p_surface,r,g,b,a);
229    d3_l->p_surface->DrawRectangle(d3_l->p_surface,
230                                   x_c*d3_l->fakt_x+X_GAP,
231                                   z_c*d3_l->fakt_y+Y_GAP,
232                                   d3_l->fakt_x,d3_l->fakt_y);
233   }
234  }
235
236  x_c=x;
237  for(y_c=0;y_c<d3_l->max_y;y_c++) 
238  {
239   for(z_c=0;z_c<d3_l->max_z;z_c++)
240   {
241    if((!mode)||mode==3) dx_lattice_get_color((*d3_l).status+x_c+y_c*d3_l->max_x+z_c*d3_l->max_x*d3_l->max_y,&r,&g,&b,mode);
242    if(mode==3)
243    {
244     if((int)(d3_l->max_y*(1.0*p2[z_c]/max2))==y_c)
245     {
246      r=0xff;
247      g=0xff;
248      b=0xff;
249     }
250    }
251    if(mode==1)
252    {
253     foobar=(unsigned char)(*(d3_l->extra+x_c+y_c*d3_l->max_x+z_c*d3_l->max_x*d3_l->max_y)*255/max_extra);
254     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
255    }
256    if(mode==2)
257    {
258     foobar=(*(unsigned char *)(d3_l->v_ptr+x_c+y_c*d3_l->max_x+z_c*d3_l->max_x*d3_l->max_y));
259     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
260    }
261    if(y_c==y && z_c==z)
262    {
263     r=0xff;
264     g=0xff;
265     b=0;
266    }
267    d3_l->p_surface->SetColor(d3_l->p_surface,r,g,b,a);
268    d3_l->p_surface->DrawRectangle(d3_l->p_surface,
269                                   y_c*d3_l->fakt_x+d3_l->o_x,
270                                   z_c*d3_l->fakt_y+Y_GAP,
271                                   d3_l->fakt_x,d3_l->fakt_y);
272   }
273  }
274
275  z_c=z;
276  for(x_c=0;x_c<d3_l->max_x;x_c++)
277  {
278   for(y_c=0;y_c<d3_l->max_y;y_c++)
279   {
280    if((!mode)||mode==3) dx_lattice_get_color((*d3_l).status+x_c+y_c*d3_l->max_x+z_c*d3_l->max_x*d3_l->max_y,&r,&g,&b,mode);
281    if(mode==1)
282    {
283     foobar=(unsigned char)(*(d3_l->extra+x_c+y_c*d3_l->max_x+z_c*d3_l->max_x*d3_l->max_y)*255/max_extra);
284     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
285    }
286    if(mode==2)
287    {
288     foobar=(*(unsigned char *)(d3_l->v_ptr+x_c+y_c*d3_l->max_x+z_c*d3_l->max_x*d3_l->max_y));
289     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
290    }
291    if(x_c==x && y_c==y)
292    {
293     r=0xff;
294     g=0xff;
295     b=0;
296    }
297    d3_l->p_surface->SetColor(d3_l->p_surface,r,g,b,a);
298    d3_l->p_surface->DrawRectangle(d3_l->p_surface,
299                                   x_c*d3_l->fakt_x+d3_l->info_x,
300                                   y_c*d3_l->fakt_y+Y_GAP,
301                                   d3_l->fakt_x,d3_l->fakt_y);
302   }
303  }
304
305  r=0xff;
306  g=0xff;
307  b=0;
308  /* clear info box */
309  d3_l->p_surface->SetColor(d3_l->p_surface,0,0,0,0);
310  d3_l->p_surface->FillRectangle(d3_l->p_surface,
311                                 d3_l->info_x,d3_l->info_y,
312                                 d3_l->info_w,d3_l->info_h);
313  d3_l->p_surface->SetColor(d3_l->p_surface,r,g,b,a);
314  d3_l->p_surface->DrawRectangle(d3_l->p_surface,
315                                 d3_l->info_x,d3_l->info_y,
316                                 d3_l->info_w,d3_l->info_h);
317  d3_l->p_surface->SetColor(d3_l->p_surface,0x80,0x80,0xff,0xff);
318  for(i=1;i<=arg_c;i++)
319    d3_l->p_surface->DrawString(d3_l->p_surface,arg_v[i],-1,
320                                d3_l->info_x+d3_l->fakt_x,
321                                d3_l->info_y+2*d3_l->fakt_y+d3_l->font_h+(i-1)*d3_l->font_h,
322                                DSTF_LEFT);
323
324  /* now we flip all to surface */
325  d3_l->p_surface->Flip(d3_l->p_surface,NULL,0);
326
327  return 1;
328 }
329
330 int d2_event_init(d2_lattice *d2_l)
331 {
332  d2_l->dfb->GetInputDevice(d2_l->dfb,DIDID_KEYBOARD,&(d2_l->keyboard));
333  d2_l->keyboard->CreateEventBuffer(d2_l->keyboard,&(d2_l->k_buffer));
334  
335  return 1;
336 }
337
338 int d3_event_init(d3_lattice *d3_l)                                           
339 {                                                                             
340  d3_l->dfb->GetInputDevice(d3_l->dfb,DIDID_KEYBOARD,&(d3_l->keyboard));
341  d3_l->keyboard->CreateEventBuffer(d3_l->keyboard,&(d3_l->k_buffer));  
342
343  return 1;                                                           
344 }         
345
346 int scan_event(d3_lattice *d3_l,int *x,int *y,int *z,int *q,int *esc,int *switchmode,int *bmp,int *ac_distr)
347 {
348  DFBInputEvent ke;
349
350  d3_l->k_buffer->WaitForEvent(d3_l->k_buffer);
351  d3_l->k_buffer->GetEvent(d3_l->k_buffer,DFB_EVENT(&ke));
352  if(ke.type==DIET_KEYPRESS)
353  {
354   if(ke.key_id==DIKI_LEFT && *x!=0) *x-=1;
355   if(ke.key_id==DIKI_RIGHT && *x!=d3_l->max_x-1) *x+=1;
356   if(ke.key_id==DIKI_UP && *y!=0) *y-=1;
357   if(ke.key_id==DIKI_DOWN && *y!=d3_l->max_y-1) *y+=1;
358   if(ke.key_id==DIKI_PAGE_UP && *z!=0) *z-=1;
359   if(ke.key_id==DIKI_PAGE_DOWN && *z!=d3_l->max_z-1) *z+=1;
360   if(ke.key_id==DIKI_END && *z<=d3_l->max_z-10) *z+=10;
361   if(ke.key_id==DIKI_HOME && *z>=10) *z-=10;
362   if(ke.key_id==DIKI_Q) *q=1;
363   if(ke.key_id==DIKI_ESCAPE) *esc=1;
364   if(ke.key_id==DIKI_M) *switchmode=(*switchmode+1)%4;
365   if(ke.key_id==DIKI_1) *bmp=1;
366   if(ke.key_id==DIKI_2) *bmp=2;
367   if(ke.key_id==DIKI_3) *bmp=3;
368   if(ke.key_id==DIKI_4) *bmp=4;
369   if(ke.key_id==DIKI_5) *bmp=5;
370   if(ke.key_id==DIKI_6) *bmp=6;
371   if(ke.key_id==DIKI_7) *bmp=7;
372   if(ke.key_id==DIKI_8) *bmp=8;
373   if(ke.key_id==DIKI_9) *bmp=9;
374   if(ke.key_id==DIKI_W) *bmp=10;
375   if(ke.key_id==DIKI_E) *bmp=11;
376   if(ke.key_id==DIKI_R) *bmp=12;
377   if(ke.key_id==DIKI_A) *ac_distr=1;
378   if(ke.key_id==DIKI_C) *ac_distr=2;
379   if(ke.key_id==DIKI_B) *ac_distr=3;
380   if(ke.key_id==DIKI_X) *ac_distr=4;
381  }
382
383  return 1;
384 }
385