fixes in dfbapi, buggy ising.c added
[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))/40; /* 40 ? */
32  d2_l->font_h=font_dsc.height;
33  d2_l->dfb->CreateFont(d2_l->dfb,FONT,&font_dsc,&(d2_l->font));
34  d2_l->p_surface->SetFont(d2_l->p_surface,d2_l->font);
35
36  d2_l->fakt_y=(d2_l->s_height)/(d2_l->max_y+(2*Y_GAP));
37  d2_l->fakt_x=(d2_l->s_height)/(d2_l->max_x+X_GAP); /* bullshit, i can't imagine atm */
38  d2_l->info_x=d2_l->fakt_x*d2_l->max_x+2*X_GAP;
39  d2_l->info_y=Y_GAP;
40  d2_l->info_w=d2_l->s_width-d2_l->info_x-2*X_GAP;
41  d2_l->info_h=d2_l->max_y*d2_l->fakt_y;
42
43  if((d2_l->s_height<(d2_l->max_y+2*Y_GAP)) || (d2_l->s_height<(d2_l->max_x+X_GAP)))
44  {
45   puts("resolution too low!");
46   return -1;
47  } else return 1;
48 }
49
50 int d2_lattice_release(d2_lattice *d2_l)
51 {
52  d2_l->font->Release(d2_l->font);
53  d2_l->p_surface->Release(d2_l->p_surface);
54  d2_l->dfb->Release(d2_l->dfb);
55
56  return 1;
57 }
58
59 int d2_lattice_get_color(unsigned char *status,unsigned char *r,unsigned char *g,unsigned char *b)
60 {
61  if((*status)&RED)
62  {
63   *r=0xff;
64   *g=0;
65   *b=0;
66  } else
67  {
68   *r=0;
69   *g=0;
70   *b=0xff;
71  }
72  
73  return 1;
74 }
75
76 int d2_lattice_draw(d2_lattice *d2_l,int x,int y,int arg_c,char **arg_v)
77 {
78  int x_c,y_c;
79  int i;
80  unsigned char r,g,b,a;
81
82  a=0xff; /* no alpha blending */
83  
84  for(x_c=0;x_c<d2_l->max_x;x_c++)
85  {
86   for(y_c=0;y_c<d2_l->max_y;y_c++)
87   {
88    d2_lattice_get_color((*d2_l).status+x_c+y_c*d2_l->max_x,&r,&g,&b);
89    if(x_c==x && y_c==y)
90    {
91     r=0xff;
92     g=0xff;
93     b=0;
94    }
95    d2_l->p_surface->SetColor(d2_l->p_surface,r,g,b,a);
96    d2_l->p_surface->DrawRectangle(d2_l->p_surface,
97                                   x_c*d2_l->fakt_x+X_GAP,
98                                   y_c*d2_l->fakt_y+Y_GAP,
99                                   d2_l->fakt_x,d2_l->fakt_y);
100   }
101  }
102  r=0xff;
103  g=0xff;
104  b=0;
105  /* clear info box */
106  d2_l->p_surface->SetColor(d2_l->p_surface,0,0,0,0);
107  d2_l->p_surface->FillRectangle(d2_l->p_surface,
108                                 d2_l->info_x,d2_l->info_y,
109                                 d2_l->info_w,d2_l->info_h);
110  d2_l->p_surface->SetColor(d2_l->p_surface,r,g,b,a);
111  d2_l->p_surface->DrawRectangle(d2_l->p_surface,
112                                 d2_l->info_x,d2_l->info_y,
113                                 d2_l->info_w,d2_l->info_h);
114  d2_l->p_surface->SetColor(d2_l->p_surface,0x80,0x80,0xff,0xff);
115  for(i=1;i<=arg_c;i++)
116  {
117   d2_l->p_surface->DrawString(d2_l->p_surface,arg_v[i],-1,
118                               d2_l->info_x+d2_l->fakt_x,
119                               d2_l->info_y+d2_l->fakt_y+d2_l->font_h+(i-1)*2*d2_l->font_h,
120                               DSTF_LEFT);
121  }
122  /* now we flip all to surface */
123  d2_l->p_surface->Flip(d2_l->p_surface,NULL,0);
124  
125  return 1;
126 }
127