9a8519b7a49e0c87e0ac8eeb72b26fe8cc35638b
[physik/ising.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 "dfbapi.h"
10
11 /* two dimensional lattice */
12
13 int d2_lattice_init(int *argc,char **argv,d2_lattice *d2_l,int x,int y)
14 {
15  DFBSurfaceDescription surface_dsc;
16  DFBFontDescription font_dsc;
17  
18  d2_l->max_x=x;
19  d2_l->max_y=y;
20
21  DirectFBInit(argc,&argv);
22  DirectFBCreate(&(d2_l->dfb));
23  d2_l->dfb->SetCooperativeLevel(d2_l->dfb,DFSCL_FULLSCREEN);
24
25  surface_dsc.flags=DSDESC_CAPS;
26  surface_dsc.caps=DSCAPS_PRIMARY|DSCAPS_FLIPPING;
27  d2_l->dfb->CreateSurface(d2_l->dfb,&surface_dsc,&(d2_l->p_surface));
28  d2_l->p_surface->GetSize(d2_l->p_surface,&(d2_l->s_width),&(d2_l->s_height));
29  
30  font_dsc.flags=DFDESC_HEIGHT;
31  font_dsc.height=d2_l->max_y*((d2_l->s_height)/(d2_l->max_y))/20; /* 20 ? */
32  d2_l->dfb->CreateFont(d2_l->dfb,FONT,&font_dsc,&(d2_l->font));
33  d2_l->p_surface->SetFont(d2_l->p_surface,d2_l->font);
34
35  d2_l->fakt_y=(d2_l->s_height)/(d2_l->max_y+(2*Y_GAP));
36  d2_l->fakt_x=(d2_l->s_height)/(d2_l->max_x+X_GAP); /* bullshit, i can't imagine atm */
37  d2_l->info_x=d2_l->fakt_x*d2_l->max_x+2*X_GAP;
38  d2_l->info_y=Y_GAP;
39  d2_l->info_w=d2_l->s_width-d2_l->info_x-2*X_GAP;
40  d2_l->info_h=d2_l->max_y*d2_l->fakt_y;
41
42  if((d2_l->s_height<(d2_l->max_y+2*Y_GAP)) || (d2_l->s_height<(d2_l->max_x+X_GAP)))
43  {
44   puts("resolution too low!");
45   return -1;
46  } else return 1;
47 }
48
49 int d2_lattice_release(d2_lattice *d2_l)
50 {
51  d2_l->font->Release(d2_l->font);
52  d2_l->p_surface->Release(d2_l->p_surface);
53  d2_l->dfb->Release(d2_l->dfb);
54
55  return 1;
56 }
57
58 int d2_lattice_get_color(unsigned char *status,unsigned char *r,unsigned char *g,unsigned char *b)
59 {
60  if((*status)&RED)
61  {
62   *r=0xff;
63   *g=0;
64   *b=0;
65  } else
66  {
67   *r=0;
68   *g=0;
69   *b=0xff;
70  }
71  
72  return 1;
73 }
74
75 int d2_lattice_draw(d2_lattice *d2_l,int x,int y,int arg_c,char **arg_v)
76 {
77  int x_c,y_c;
78  int i;
79  unsigned char r,g,b,a;
80
81  a=0xff; /* no alpha blending */
82  
83  for(x_c=0;x_c<d2_l->max_x;x_c++)
84  {
85   for(y_c=0;y_c<d2_l->max_y;y_c++)
86   {
87    d2_lattice_get_color((*d2_l).status+x_c+y_c*d2_l->max_x,&r,&g,&b);
88    if(x_c==x && y_c==y)
89    {
90     r=0xff;
91     g=0xff;
92     b=0;
93    }
94    d2_l->p_surface->SetColor(d2_l->p_surface,r,g,b,a);
95    d2_l->p_surface->DrawRectangle(d2_l->p_surface,
96                                   x_c*d2_l->fakt_x+X_GAP,
97                                   y_c*d2_l->fakt_y+Y_GAP,
98                                   d2_l->fakt_x,d2_l->fakt_y);
99   }
100  }
101  r=0xff;
102  g=0xff;
103  b=0;
104  /* clear info box */
105  d2_l->p_surface->SetColor(d2_l->p_surface,0,0,0,0);
106  d2_l->p_surface->FillRectangle(d2_l->p_surface,
107                                 d2_l->info_x,d2_l->info_y,
108                                 d2_l->info_w,d2_l->info_h);
109  d2_l->p_surface->SetColor(d2_l->p_surface,r,g,b,a);
110  d2_l->p_surface->DrawRectangle(d2_l->p_surface,
111                                 d2_l->info_x,d2_l->info_y,
112                                 d2_l->info_w,d2_l->info_h);
113  d2_l->p_surface->SetColor(d2_l->p_surface,0x80,0x80,0xff,0xff);
114  for(i=1;i<=arg_c;i++)
115  {
116   d2_l->p_surface->DrawString(d2_l->p_surface,arg_v[1],-1,
117                               d2_l->info_x+d2_l->fakt_x,
118                               d2_l->info_y+d2_l->fakt_x+(i-1)*d2_l->fakt_x,
119                               DSTF_LEFT);
120  }
121  /* now we flip all to surface */
122  d2_l->p_surface->Flip(d2_l->p_surface,NULL,0);
123  
124  return 1;
125 }
126