]> www.hackdaworld.org Git - physik/nlsop.git/blob - dfbapi.c
new runs
[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)
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)
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) 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==1)
204    {
205     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);
206     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
207    }
208    if(mode==2)
209    {
210     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));
211     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
212    }
213    if(x_c==x && z_c==z)
214    {
215     r=0xff;
216     g=0xff;
217     b=0;
218    }
219    d3_l->p_surface->SetColor(d3_l->p_surface,r,g,b,a);
220    d3_l->p_surface->DrawRectangle(d3_l->p_surface,
221                                   x_c*d3_l->fakt_x+X_GAP,
222                                   z_c*d3_l->fakt_y+Y_GAP,
223                                   d3_l->fakt_x,d3_l->fakt_y);
224   }
225  }
226
227  x_c=x;
228  for(y_c=0;y_c<d3_l->max_y;y_c++) 
229  {
230   for(z_c=0;z_c<d3_l->max_z;z_c++)
231   {
232    if(!mode) 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);
233    if(mode==1)
234    {
235     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);
236     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
237    }
238    if(mode==2)
239    {
240     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));
241     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
242    }
243    if(y_c==y && z_c==z)
244    {
245     r=0xff;
246     g=0xff;
247     b=0;
248    }
249    d3_l->p_surface->SetColor(d3_l->p_surface,r,g,b,a);
250    d3_l->p_surface->DrawRectangle(d3_l->p_surface,
251                                   y_c*d3_l->fakt_x+d3_l->o_x,
252                                   z_c*d3_l->fakt_y+Y_GAP,
253                                   d3_l->fakt_x,d3_l->fakt_y);
254   }
255  }
256
257  z_c=z;
258  for(x_c=0;x_c<d3_l->max_x;x_c++)
259  {
260   for(y_c=0;y_c<d3_l->max_y;y_c++)
261   {
262    if(!mode) 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);
263    if(mode==1)
264    {
265     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);
266     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
267    }
268    if(mode==2)
269    {
270     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));
271     dx_lattice_get_color(&foobar,&r,&g,&b,mode);
272    }
273    if(x_c==x && y_c==y)
274    {
275     r=0xff;
276     g=0xff;
277     b=0;
278    }
279    d3_l->p_surface->SetColor(d3_l->p_surface,r,g,b,a);
280    d3_l->p_surface->DrawRectangle(d3_l->p_surface,
281                                   x_c*d3_l->fakt_x+d3_l->info_x,
282                                   y_c*d3_l->fakt_y+Y_GAP,
283                                   d3_l->fakt_x,d3_l->fakt_y);
284   }
285  }
286
287  r=0xff;
288  g=0xff;
289  b=0;
290  /* clear info box */
291  d3_l->p_surface->SetColor(d3_l->p_surface,0,0,0,0);
292  d3_l->p_surface->FillRectangle(d3_l->p_surface,
293                                 d3_l->info_x,d3_l->info_y,
294                                 d3_l->info_w,d3_l->info_h);
295  d3_l->p_surface->SetColor(d3_l->p_surface,r,g,b,a);
296  d3_l->p_surface->DrawRectangle(d3_l->p_surface,
297                                 d3_l->info_x,d3_l->info_y,
298                                 d3_l->info_w,d3_l->info_h);
299  /* old style
300  d3_l->p_surface->DrawLine(d3_l->p_surface,
301                            d3_l->info_x+d3_l->info_w/2,
302                            d3_l->info_y,
303                            d3_l->info_x+d3_l->info_w/2,
304                            d3_l->info_y+d3_l->info_h);
305  */
306  d3_l->p_surface->SetColor(d3_l->p_surface,0x80,0x80,0xff,0xff);
307  for(i=1;i<=arg_c;i++)
308  {
309   // if(i<13)
310   // {
311    d3_l->p_surface->DrawString(d3_l->p_surface,arg_v[i],-1,
312                                d3_l->info_x+d3_l->fakt_x,
313                                d3_l->info_y+2*d3_l->fakt_y+d3_l->font_h+(i-1)*d3_l->font_h,
314                                DSTF_LEFT);
315   // } else
316   // {
317   /* old style
318     d3_l->p_surface->DrawString(d3_l->p_surface,arg_v[i],-1,
319                                 d3_l->info_x+d3_l->info_w/2+d3_l->fakt_x,
320                                 d3_l->info_y+2*d3_l->fakt_y+d3_l->font_h+(i%13-1)*d3_l->font_h,
321                                 DSTF_LEFT);
322   */
323   // }
324  }
325
326  /* now we flip all to surface */
327  d3_l->p_surface->Flip(d3_l->p_surface,NULL,0);
328
329  return 1;
330 }
331
332 int d2_event_init(d2_lattice *d2_l)
333 {
334  d2_l->dfb->GetInputDevice(d2_l->dfb,DIDID_KEYBOARD,&(d2_l->keyboard));
335  d2_l->keyboard->CreateEventBuffer(d2_l->keyboard,&(d2_l->k_buffer));
336  
337  return 1;
338 }
339
340 int d3_event_init(d3_lattice *d3_l)                                           
341 {                                                                             
342  d3_l->dfb->GetInputDevice(d3_l->dfb,DIDID_KEYBOARD,&(d3_l->keyboard));
343  d3_l->keyboard->CreateEventBuffer(d3_l->keyboard,&(d3_l->k_buffer));  
344
345  return 1;                                                           
346 }         
347
348 int scan_event(d3_lattice *d3_l,int *x,int *y,int *z,int *q,int *esc,int *switchmode,int *bmp,int *ac_distr)
349 {
350  DFBInputEvent ke;
351
352  d3_l->k_buffer->WaitForEvent(d3_l->k_buffer);
353  d3_l->k_buffer->GetEvent(d3_l->k_buffer,DFB_EVENT(&ke));
354  if(ke.type==DIET_KEYPRESS)
355  {
356   if(ke.key_id==DIKI_LEFT && *x!=0) *x-=1;
357   if(ke.key_id==DIKI_RIGHT && *x!=d3_l->max_x-1) *x+=1;
358   if(ke.key_id==DIKI_UP && *y!=0) *y-=1;
359   if(ke.key_id==DIKI_DOWN && *y!=d3_l->max_y-1) *y+=1;
360   if(ke.key_id==DIKI_PAGE_UP && *z!=0) *z-=1;
361   if(ke.key_id==DIKI_PAGE_DOWN && *z!=d3_l->max_z-1) *z+=1;
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)%3;
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_A) *ac_distr=1;
372   if(ke.key_id==DIKI_C) *ac_distr=2;
373   if(ke.key_id==DIKI_B) *ac_distr=3;
374  }
375
376  return 1;
377 }
378