]> hackdaworld.org Git - physik/morpheus.git/blob - display.c
9509276522b0074e36e6386261d1682ccfe5fecc
[physik/morpheus.git] / display.c
1 /*
2  * display.c - visualization api
3  *
4  */
5
6 #include <stdio.h>
7 #include <stdlib.h>
8
9 #include "defines.h"
10
11 int display_init(u32 x,u32 y,u32 z,display *display,cell *cell,int *argc,char **argv)
12 {
13 #ifdef USE_DFB_API
14  DFBSurfaceDescription surface_dsc;
15 #endif
16
17  /* store dimensions */
18  display->max_x=x;
19  display->max_y=y;
20  display->max_z=z;
21  /* store pointer to cells */
22  display->cell_p=cell;
23
24  /* the api(s) */
25 #ifdef USE_DFB_API
26  DirectFBInit(argc,&argv);
27  DirectFBCreate(&(display->dfb));
28  display->dfb->SetCooperativeLevel(display->dfb,DFSCL_FULLSCREEN);
29  surface_dsc.flags=DSDESC_CAPS;
30  surface_dsc.caps=DSCAPS_PRIMARY|DSCAPS_FLIPPING;
31  display->dfb->CreateSurface(display->dfb,&surface_dsc,&(display->primary_surface));
32  display->primary_surface->GetSize(display->primary_surface,&(display->screen_width),&(display->screen_height));
33
34 #endif
35  return 23;
36 }
37
38 int display_release(display *display)
39 {
40 #ifdef USE_DFB_API
41  display->font->Release(display->font);
42  display->primary_surface->Release(display->primary_surface);
43  display->dfb->Release(display->dfb);
44 #endif
45
46  return 23;
47 }
48
49 #ifdef USE_DFB_API
50 int dfb_choose_color(cell *cell_p,unsigned char *r,unsigned char *g,unsigned char *b)
51 {
52  if(cell_p->status&AMORPH)
53  {
54   *r=0xff;
55   *g=0x00;
56   *b=0x00;
57  } else
58  {
59   *r=0x00;
60   *g=0x00;
61   *b=0xff;
62  }
63  return 23;
64 }
65 #endif
66
67 int display_draw(display *display,u32 x,u32 y,u32 z)
68 {
69 #ifdef USE_DFB_API
70  int menu_x,menu_y,menu_w,menu_h; /* for the yellow box */
71  DFBFontDescription font_dsc;
72  char text[MAX_TXT];
73  u32 x_c,y_c,z_c;
74  unsigned char r,g,b,a;
75  int display_faktor_x,display_faktor_y;
76  a=0xff; /* no alpha blending */
77  if(((display->max_x+display->max_y+DISPLAY_X_FREE)>=display->screen_width)|
78     ((display->max_y+display->max_z+DISPLAY_Y_FREE)>=display->screen_height))
79  {
80   puts("resolution too low!");
81   return -23;
82  }
83  display_faktor_x=display->screen_width/(display->max_x+display->max_y+DISPLAY_X_FREE);
84  display_faktor_y=display->screen_height/(display->max_z+display->max_y+DISPLAY_Y_FREE);
85  // if(display_faktor_x<display_faktor_y) display_faktor_y=display_faktor_x;
86  // if(display_faktor_y<display_faktor_x) display_faktor_x=display_faktor_y;
87
88  /* draw x-z surface */
89  y_c=y;
90  for(x_c=0;x_c<display->max_x;x_c++)
91  {
92   for(z_c=0;z_c<display->max_z;z_c++)
93   {
94    dfb_choose_color(display->cell_p+x_c+y_c*display->max_x+z_c*display->max_x*display->max_y,&r,&g,&b);
95    if(x_c==x && z_c==z)
96     display->primary_surface->SetColor(display->primary_surface,0xff,0xff,0,0);
97    else
98     display->primary_surface->SetColor(display->primary_surface,r,g,b,a);
99    display->primary_surface->DrawRectangle(display->primary_surface,x_c*display_faktor_x+DISPLAY_X_FREE/3,z_c*display_faktor_y+DISPLAY_Y_FREE/3,display_faktor_x,display_faktor_y);
100   }
101  }
102  /* draw y-z surface */
103  x_c=x;
104  for(y_c=0;y_c<display->max_y;y_c++)
105  {
106   for(z_c=0;z_c<display->max_z;z_c++)
107   {
108    dfb_choose_color(display->cell_p+x_c+y_c*display->max_x+z_c*display->max_x*display->max_y,&r,&g,&b);
109    if(y_c==y && z_c==z)
110     display->primary_surface->SetColor(display->primary_surface,0xff,0xff,0,0);
111    else
112     display->primary_surface->SetColor(display->primary_surface,r,g,b,a);
113    display->primary_surface->DrawRectangle(display->primary_surface,(y_c+display->max_x)*display_faktor_x+2*DISPLAY_X_FREE/3,z_c*display_faktor_y+DISPLAY_Y_FREE/3,display_faktor_x,display_faktor_y);
114   }
115  }
116  /* draw x-y surface */
117  z_c=z;
118  for(x_c=0;x_c<display->max_x;x_c++)
119  {
120   for(y_c=0;y_c<display->max_y;y_c++)
121   {
122    dfb_choose_color(display->cell_p+x_c+y_c*display->max_x+z_c*display->max_x*display->max_y,&r,&g,&b);
123    if(x_c==x && y_c==y)
124     display->primary_surface->SetColor(display->primary_surface,0xff,0xff,0,0);
125    else
126     display->primary_surface->SetColor(display->primary_surface,r,g,b,a);
127    display->primary_surface->DrawRectangle(display->primary_surface,x_c*display_faktor_x+DISPLAY_X_FREE/3,(y_c+display->max_z)*display_faktor_y+2*DISPLAY_Y_FREE/3,display_faktor_x,display_faktor_y);
128   }
129  }
130  /* display info window */
131  r=0xff;
132  g=0xff;
133  b=0x00;
134  a=0xff;
135  menu_x=2*DISPLAY_X_FREE/3+display->max_x*display_faktor_x;
136  menu_y=2*DISPLAY_Y_FREE/3+display->max_z*display_faktor_y;
137  menu_w=display->max_y*display_faktor_x;
138  menu_h=display->max_y*display_faktor_y;
139  display->primary_surface->SetColor(display->primary_surface,0,0,0,0);
140  display->primary_surface->FillRectangle(display->primary_surface,menu_x,menu_y,menu_w,menu_h);
141  display->primary_surface->SetColor(display->primary_surface,r,g,b,a);
142  display->primary_surface->DrawRectangle(display->primary_surface,menu_x,menu_y,menu_w,menu_h);
143  font_dsc.flags=DFDESC_HEIGHT;
144  font_dsc.height=menu_h/10;
145  display->dfb->CreateFont(display->dfb,DISPLAY_FONT,&font_dsc,&(display->font));
146  display->primary_surface->SetFont(display->primary_surface,display->font);
147  display->primary_surface->SetColor(display->primary_surface,0x80,0x80,0xff,0xff);
148  sprintf(text,"x: %u",x);
149  display->primary_surface->DrawString(display->primary_surface,text,-1,menu_x+display_faktor_x,menu_y+display_faktor_y+menu_h/10,DSTF_LEFT);
150  sprintf(text,"y: %u",y);
151  display->primary_surface->DrawString(display->primary_surface,text,-1,menu_x+display_faktor_x,menu_y+display_faktor_y*2+menu_h/10*2,DSTF_LEFT);
152  sprintf(text,"z: %u",z);
153  display->primary_surface->DrawString(display->primary_surface,text,-1,menu_x+display_faktor_x,menu_y+display_faktor_y*3+menu_h/10*3,DSTF_LEFT);
154  sprintf(text,"status: %c",((display->cell_p+x+y*display->max_x+z*display->max_x*display->max_y))->status&AMORPH?'a':'c');
155  display->primary_surface->DrawString(display->primary_surface,text,-1,menu_x+display_faktor_x,menu_y+display_faktor_y*5+menu_h/10*5,DSTF_LEFT);
156  sprintf(text,"c conc.: %u",(display->cell_p+x+y*display->max_x+z*display->max_x*display->max_y)->conc);
157  display->primary_surface->DrawString(display->primary_surface,text,-1,menu_x+display_faktor_x,menu_y+display_faktor_y*6+menu_h/10*6,DSTF_LEFT);
158  /* flip all to surface */
159  display->primary_surface->Flip(display->primary_surface,NULL,0);
160 #endif
161  return 23;
162 }
163
164 int display_event_init(display *display)
165 {
166 #ifdef USE_DFB_API
167  display->dfb->GetInputDevice(display->dfb,DIDID_KEYBOARD,&(display->keyboard));
168  display->keyboard->CreateEventBuffer(display->keyboard,&(display->keyboard_buffer));
169 #endif
170  return 23;
171 }
172
173 int display_scan_event(display *display,u32 *x,u32 *y,u32 *z,int *quit)
174 {
175  DFBInputEvent keyboard_event;
176  display->keyboard_buffer->WaitForEvent(display->keyboard_buffer);
177  display->keyboard_buffer->GetEvent(display->keyboard_buffer,DFB_EVENT(&keyboard_event));
178  if(keyboard_event.type==DIET_KEYPRESS)
179  {
180   if(keyboard_event.key_id==DIKI_LEFT && *x!=0) *x-=1;
181   if(keyboard_event.key_id==DIKI_RIGHT && *x!=display->max_x-1) *x+=1;
182   if(keyboard_event.key_id==DIKI_DOWN && *y!=0) *y-=1;
183   if(keyboard_event.key_id==DIKI_UP && *y!=display->max_y-1) *y+=1;
184   if(keyboard_event.key_id==DIKI_PAGE_UP && *z!=0) *z-=1;
185   if(keyboard_event.key_id==DIKI_PAGE_DOWN && *z!=display->max_z-1) *z+=1;
186   if(keyboard_event.key_id==DIKI_Q) *quit=1;
187  }
188  return 23;
189 }