#define I_HAVE_REALLY_COOL_OS
#include
#include
#include
#include
#include
#include
long int R = 200, Sq, offset;
int i,j,x, y, width, height;
bool first_time=true;
long int buff[5];
float z, t;
char c[3];
char color;
FILE* fp;
void draw_damn_thing();
#ifdef I_HAVE_REALLY_COOL_OS
#include
#include
GtkWidget * mywnd;
GdkWindowAttr wattr;
GdkGC* my_gc;
GdkColor * cols[16];
void putpixel(int x,int y,char color)
{
gdk_gc_set_foreground(my_gc,cols[color]);
gdk_gc_set_fill(my_gc,GDK_SOLID);
// gdk_rgb_gc_set_background(my_gc,256*256*color+256*color+color);
// gdk_draw_point(mywnd->window,my_gc,x,y);
/* switch(color/4)
{
case 0:
my_gc=mywnd->style->white_gc;
break;
case 1:
my_gc=mywnd->style->black_gc;
break;
case 3:
my_gc=*mywnd->style->dark_gc;
break;
case 2:
my_gc=*mywnd->style->light_gc;
break;
}*/
if((x<0)||(y<0)||(x>640)||(y>480))
printf("(%d,%d)\n",x,y);
gdk_draw_point(mywnd->window,my_gc,x,y);
// if(first_time)
// printf("%d ",color);
}
void my_exit()
{
if(fp!=NULL)
fclose(fp);
fp=NULL;
exit(0);
}
gint gtk_draw_damn_thing(GtkWidget* W)
{
draw_damn_thing();
first_time=false;
return TRUE;
}
GdkColor *NewColor(char color)
{
GdkColor *c = (GdkColor *) g_malloc (sizeof (GdkColor));
c->red = 0xffff;
c->green = 0xffff;
c->blue = 0xffff;
gdk_color_alloc(gdk_colormap_get_system(),c);
c->pixel=color;
return (c);
}
#define delay(x) sleep(x)
#define getch() getchar()
#else
#include //for exit();
#include // for delay();
#include //for graphics functions
#include //for getch()
#include "c:\borlandc\include\graphlib.h"
#endif
void draw_damn_thing()
{
int vi,vj;
for( i = 0; i < 640; i++ )
for( j = 0; j < 480; j++ )
{
vi=i-320;
vj=j-240;
// Sq = R*R - (i-320)*(i-320) - (j-240)*(j-240);
Sq = R*R - vi*vi - vj*vj;
if( Sq >= 0 )
{
/* z = sqrt(Sq)-R; //place map on top of the sphere.
t = 2*R/(2*R+z);
x=(1-t)*320+t*i;
y=(1-t)*240+t*j;
// x = i + t*(320-i) - 320 + width/2;
// y = j + t*(240-j) - 240 + height/2;
*/
/* z=sqrt(Sq)-R;
t=(2*R)/(2*R+z);
x=rintf((1-t)*320 + t*i);
y=rintf((1-t)*240 + t*j);
*/
z=sqrt(Sq);
x=-2*R*vi/(R+z);
y=-2*R*vj/(R+z);
x+=320;y+=240;
// x=2*R*(320-i)/(z+2*R)+320;
// y=2*R*(240-j)/(z+2*R)+240;
if(fseek( fp, (y*width + x)/2 + buff[2]+buff[3], SEEK_SET )==-1)
printf("x=%d,y=%d,i=%d,j=%d,vi=%d,vj=%d\n",x,y,i,j,vi,vj);
fread( &color, sizeof(color), 1, fp );
if((y*width + x)%2)
color=color&0x0f;
else
color=(color&0xf0)/16;
putpixel(i,j,color);
} // end if
} // end for
// end for
}
int main(int argc,char ** argv)
{
if(( fp = fopen( "sz22b.bmp", "rb")) == NULL)
{ printf("Cannot open file\n");
return 0;
}
fread( c, sizeof(char), 2, fp);
c[2] = '\0';
fread( &buff[0], sizeof(long int), 1, fp );
fread( &buff[1], sizeof(long int), 1, fp );
fread( &buff[2], sizeof(long int), 1, fp );
fread( &buff[3], sizeof(long int), 1, fp );
fread( &width, sizeof(long int), 1, fp );
fread( &height, sizeof(long int), 1, fp );
cout << "File Type = " << c << endl //OK
<< " Size of file = " << buff[0] << endl //ok
<< " Reserved ( must be 0 ) = " << buff[1] << endl
<< " Byte offset to bitmap data " << buff[2] << endl
<< "number of bytes in header = " << buff[3] << endl //ok
<< "width = " << width << endl //ok
<< "height = " << height << endl; //ok
cout << "Press a key to continue" << endl << endl;
getch();
#ifdef I_HAVE_REALLY_COOL_OS
gtk_init(&argc,&argv);
gdk_init(&argc,&argv);
gdk_rgb_init();
printf("GTK initialized\n");
mywnd = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
gtk_signal_connect(GTK_OBJECT(mywnd), "destroy",GTK_SIGNAL_FUNC(my_exit), NULL);
gtk_widget_set_usize(mywnd,640,480);
// gtk_widget_show(mywnd);
wattr.window_type = GDK_WINDOW_CHILD;
wattr.wclass = GDK_INPUT_OUTPUT;
wattr.width = 640;
wattr.height = 480;
mywnd->window = gdk_window_new(NULL, &wattr, 0);
gtk_widget_show(mywnd);
gdk_window_set_title(mywnd->window,"Comp Graphics Homework 11");
my_gc=gdk_gc_new(mywnd->window);
gdk_window_show(mywnd->window);
gtk_timeout_add( 100,gtk_draw_damn_thing, NULL );
for(i=0;i<16;i++)
// {
cols[i]=NewColor(i);
// cols[i]->pixel=i*256+i;
// }
gtk_main();
sleep(500);
exit(0);
#else
int driver = DETECT, mode, errorcode;
/* initialize graphics and local variables */
initgraph(&driver, &mode, "c:\\borlandc\\bgi");
/* read result of initialization */
errorcode = graphresult();
if (errorcode != grOk) /* an error occurred */
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
printf("Press any key to halt:");
getch();
exit(1); /* terminate with an error code */
}
#endif
draw_damn_thing();
getch();
fclose(fp);
#ifndef I_HAVE_REALLY_COOL_OS
closegraph();
#endif
return 0;
}
               (
geocities.com/siliconvalley/park)                   (
geocities.com/siliconvalley)