File: hello.C

package info (click to toggle)
sabre 0.2.4b-6
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,536 kB
  • ctags: 7,207
  • sloc: cpp: 36,926; ansic: 8,272; sh: 2,547; makefile: 208
file content (279 lines) | stat: -rw-r--r-- 5,272 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// -*- mode: C; -*-
// jed does not understand .C

//
// Minefields:
// 
// This code is mine. You may use, modify or redistribute this code 
// in any APPROPRIATE manner you may choose. 
//  
// If you use, modify or redistribute this code, I am NOT responsible
// for ANY issue raised. 
// 
// ( If you modify or remove this note, this code is no longer mine. ) 
// 
// proff@iki.fi 
// 
//

// Wed Dec 17 19:46:56 EET 1997 (proff@alf.melmac)
// created file vp.C

// Current version:
// Last Modified:

#include "hello.h"

// --
// -- main( options ) 
// --
// --

int main( int argc, char ** argv )
{
   int dimx=320, dimy=200;
   int c;

   while( 1 ) {
      int option_index = 0;
      static struct option long_options[] =
      {
        {"dimx", 1, 0, 'X'},
        {"dimy", 1, 0, 'Y'},
        {"help", 1, 0, '?'},
        {0, 0, 0, 0}
      };

      c = getopt_long( argc, argv, "?", long_options, &option_index );
      if( c == -1 )
	break;

      switch( c ) {
        case 'X':
	   dimx=atoi(optarg);
          break;
        case 'Y':
	   dimy=atoi(optarg);
          break;
	 case '?':
	   usage();
          break;  
      } 
   }
   
   if( optind < argc ) {
       usage();  
   }

   fprintf( stderr, "hello %dx%d\n", dimx, dimy );
   if( loadfonts( ) )
       return 1;
   
   assign_gdev_svgalib();
   assign_rgbdev8_svgalib();
   if( G->open(dimx,dimy) ) {
      fprintf( stderr, "error opening gdev in %dx%dx8 resolution!\n", dimx, dimy );
      return 1;
   }

   // full device
   test();
   
   // "1/4 device"
   G->setview( dimx/4, dimy/4, dimx-dimx/4, dimy-dimy/4 );
   test();
   
   G->close();
   return 0;
}

// --
// -- usage( ) 
// --
// --

void usage( void )
{
   fprintf( stderr, "usage: hello [options]\n" 
	            "options:\n"
	            "          --dimx <value>\n"
	            "          --dimy <value>\n"
	            "          --font-file <file>\n"
	            "\n"
	              );
   exit(1);
}

// --
// -- test( ) 
// --
// -- runs sequence of tests in previously specified viewport
// -- no parameters are used nor accepted
// --

static float ffactor[64];
static char mypal[768]; 
static void fade( int out=1 )
{
   int i, j, beg, end, step;
   //(works only for 8bpp vicinity)
   char fadepal[768];

   if( out ) {
      // fade away..
      beg=63;
      end=-1;
      step=-1;
   } else {
      // fade in..
      beg=0;
      end=64;
      step=1;
   }

   for( j=beg; j!=end; j+=step ) { 
      char *p=fadepal, *mp=mypal;
      for( i=0; i<768; i++, p++, mp++ ) {
	 *p=(char)(j*ffactor[*mp]);
      }
      RGB8->set( fadepal );
   }

   if( !out )
     RGB8->set( mypal );   
}

fontdev *FONT=new fontdev;
fontdev *BROADWAYFONT=new fontdev;
fontdev *SMALLERFONT=new fontdev;
fontdev *MEDIEVALFONT=new fontdev;

void test( void )
{
   int i;
   ffactor[0]=0.0;
   for( i=1; i<64; i++ )
     ffactor[i]=(i*1.0)/64.0;
   
   RGB8->get( mypal );

   //
   // Input test
   //
   
   fade();
   G->clear( );
   i=0;
   gprintf( FONT, 0, 0,     1, "Press something..." ); 
   i+=FONT->getdimy()+1;
   gprintf( FONT, 0, i, 1, "(to be continued)" ); 
   i+=BROADWAYFONT->getdimy()+1;
   gprintf( BROADWAYFONT, 0, i, 1, "HELLO WORLD" ); 
   i+=SMALLERFONT->getdimy()+1;
   gprintf( SMALLERFONT, 0, i, 1, "hello world..." ); 
   i+=MEDIEVALFONT->getdimy()+1;
   gprintf( MEDIEVALFONT, 0, i, 1, "Hello World?" ); 
   G->update();
   fade(0);
   wait();
   
   fade();
   G->clear( );
   int dx=G->getdimx(), dy=G->getdimy();
      
   //
   // line test: print a "star"
   //
   int radius=dx>dy?dy/2:dx/2;
   for( i=0; i<32; i++ ) {
      float angle=(i/32.0)*2*3.14159;
      int ddx=(int)(sin(angle)*radius);
      int ddy=(int)(cos(angle)*radius);
      G->line( dx/2, dy/2, dx/2+ddx, dy/2+ddy, 32+i );
   }

   G->update();
   fade(0);
   sleep(1);
   fade();

   //
   // rectangle test
   //
   for( i=15; i>=0; i-- )
     G->rect( 0, 0, (int)(i/15.0*dx), (int)(i/15.0*dy), i );
   G->update();
   fade(0);
   sleep(1);
   fade();
   G->clear();
   G->update();
   fade(0);
   
   //
   // vbuf test: user framebuffer manipulation support
   //
   unsigned char pattern[225];
   for( i=0; i<224; i++ )
     pattern[i]=i+32;
   pattern[i]=0;
   unsigned char *start_pattern=pattern;
   time_t end=clock()+CLOCKS_PER_SEC;
   int count=0;
   while( clock() < end ) {
      count++;
      start_pattern++;
      if( !*pattern )
	start_pattern=pattern;
      unsigned char *dest=(unsigned char *)G->getvbuf(), *src=start_pattern;
      for( i=0; i<dx*dy; i++, dest++, src++ ) {
	 if( !*src )
	   src=pattern;
	 *dest=*src;
      }
      G->update();
   }
   fade();
   G->clear();
   G->update();
   fade(0);
}

// --
// -- loadfont( fn ) 
// --
// --


char *myfontpaths[] = {
   "fonts",
   "../fonts",
   "lib/fonts",
   "../lib/fonts",
   NULL
};

int loadfonts( void )
{
   fontpaths=myfontpaths;
   if( FONT->load( "simple-5x6" ) )
     return 1;
   if( BROADWAYFONT->load( "broadway-16" ) )
     return 1;
   if( SMALLERFONT->load( "alt-8x14" ) )
     return 1;
   if( MEDIEVALFONT->load( "medieval-16" ) )
     return 1;
   return 0;
}

void wait( void )
{
   char looping=1;
   while( looping ) {
      gevent *g= G->event(1);
      if( g )
	if( 0 != (g->type&gevent::pressed) )
	  looping=0;
   }
}