home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
World of Shareware - Software Farm 2
/
wosw_2.zip
/
wosw_2
/
CPROG
/
DDJ0190.ZIP
/
RAHNER.EXE
/
SPRITE.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-09-21
|
6KB
|
239 lines
/******************************************************************************
TITLE: SPRITES.C
Displays a sprite file on an EGA screen
Written by: Rahner James, CS
of Ryu Consulting, Inc.
******************************************************************************/
#include <stdio.h>
#include <dos.h>
#include <fcntl.h>
/******************************************************************************
VARIOUS DEFINITIONS
******************************************************************************/
#pragma pack(1)
typedef unsigned char uc;
typedef unsigned int ui;
typedef unsigned long ul;
/******************************************************************************
EXTERNAL DECLARATIONS
******************************************************************************/
extern void ega_convert();
extern ui ega_calculate( uc far * );
extern void ega_install();
extern void ega_clear_area( ui, ui, ui, ui );
/******************************************************************************
GLOBAL DATA
******************************************************************************/
ui min_x=0, min_y=0, max_x=639, max_y=199;
/******************************************************************************
long READ_ALL_FILE( uc *FILENAME, uc huge *BUFFER, ul BUFFER_SIZE )
Opens and reads an entire sprite file
Given:
FILENAME -> name of sprite file to read
BUFFER -> buffer to read the sprite file into
BUFFER_SIZE = number of bytes the buffer can hold
Returns:
File is opened, read and closed
Number of bytes read, if all went well
If error, returns -1
******************************************************************************/
long read_all_file( uc *filename, uc huge *buffer, ul buffer_size )
{
long rv = 0;
ui handle, dos_return, amount_read;
ui amount_to_read;
if ( _dos_open( filename, O_RDONLY, &handle ) )
return -1;
while ( buffer_size )
{
amount_to_read = buffer_size<60000L ? buffer_size : 60000L;
if ( _dos_read( handle, buffer+rv, amount_to_read, &amount_read ) )
{
rv = -1;
break;
}
rv += amount_read;
if ( amount_read < 60000 )
break;
buffer_size -= amount_read;
}
_dos_close( handle );
return rv;
}
/******************************************************************************
void DO_BACKGROUND( void )
Sets up the background for the sprite visual screen
Given:
nothing
Returns:
visual sprite screen erased
******************************************************************************/
void do_background( void )
{
ega_clear_area( min_x, min_y, max_x, max_y );
}
/******************************************************************************
uc SET_MODE( uc MODE_NUMBER )
Sets the video mode
Given:
Mode number to set video to
Returns:
Present video mode number
******************************************************************************/
uc set_mode( uc mode_number )
{
uc rv;
union REGS regs;
regs.h.ah = 15;
int86( 0x10, ®s, ®s );
rv = regs.h.al;
regs.h.ah = 0;
regs.h.al = mode_number;
int86( 0x10, ®s, ®s );
return rv;
}
/******************************************************************************
MAIN( int ARGC, uc *ARGV[] )
Allocates memory, reads in a sprite file, displays the sprites
until a key is pressed, frees up memory and interrupt vectors
Given:
ARGC = number of command line values, must be > 1
ARGV[1] -> file name of the sprite file to display
Returns:
0 if all went well, otherwise numbered according to error
******************************************************************************/
main( int argc, uc *argv[] )
{
ui i, x, y;
uc far *file_ptr, huge *sprite_start, huge *buffer_start;
uc huge *sprite_ptr[20];
uc old_mode;
ui memory_segment;
ul memory_size=0, file_size;
/* Check initial values and allocate memory for buffers */
if ( argc<2 )
{
printf( "\nNo file name has been given\n" );
exit( 1 );
}
if ( _dos_allocmem( -1, (ui *)&memory_size ) )
{
if ( _dos_allocmem( memory_size, &memory_segment ) )
{
printf( "\nMemory allocation error\n" );
exit( 2 );
}
}
else
{
memory_segment = memory_size;
memory_size = 0xffff;
}
memory_size <<= 4;
buffer_start = (uc huge *)((ul)memory_segment << 16L);
/* Read in the sprite file and then convert it to our intenal structure */
file_ptr = buffer_start;
if ( (file_size=read_all_file(argv[1],file_ptr,memory_size)) == -1 )
{
_dos_freemem( memory_segment );
printf( "\nGot error reading %s. Aborting.\n", argv[1] );
exit( 3 );
}
clear_sprite_list();
sprite_start = file_ptr + file_size;
for ( i=0 ; i<20 && file_size ; ++i )
{
ega_convert( sprite_ptr[i]=sprite_start, file_ptr );
x = ega_calculate( file_ptr );
sprite_start += x;
x = (ui)*file_ptr * (ui)*(file_ptr+2) + 4;
file_ptr += x;
if ( file_size > (ul)x )
file_size -= (ul)x;
else
file_size = 0;
}
/* Create linked list of sprite circles */
insert_sprite( 100,100, 100,100, 0,0, 7, sprite_ptr[0] );
for ( x=1 ; x<i ; ++x )
add_sprite( sprite_ptr[0], sprite_ptr[x] );
/* Setup the EGA screen mode and interrupt vector */
old_mode = set_mode( 0x10 );
ega_install();
/* Process the sprite list until someone taps a key */
while ( !kbhit() )
do_sprite_list();
/* Restore screen and allocated memory to original state */
ega_rip_out();
set_mode( old_mode );
_dos_freemem( memory_segment );
exit( 0 );
}