Mail Archives: djgpp/1998/01/12/23:19:46
From: | "Matt Kris" <jdkris AT NOPOOPalpha DOT delta DOT edu>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | Sprite help...
|
Date: | Mon, 12 Jan 1998 22:49:05 -0500
|
Organization: | Delta College
|
Lines: | 130
|
Message-ID: | <69eoci$6gk@alpha.delta.edu>
|
NNTP-Posting-Host: | pm090-01.dialip.mich.net
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Howdy....
Could someone out there help me out and convert this code so that it uses
Allegro and load a 10x10 bitmap from a file. Right now it defines all of
its own functions and doesn't load the bitmap from a file. Thanks alot.
#include <sys/nearptr.h>
#include <go32.h> /* for _dos_ds */
#include <dos.h>
#include <stdlib.h>
#define GRAPHICS 0x013
#define TEXT 0x03
char *video_buffer = (char *)0xa0000;
typedef struct sprite_type {
unsigned x, y; /* location */
unsigned x_vel, y_vel; /* velocity */
char width, height; /* dimensions */
char num_frames; /* number of frames */
char cur_frame; /* current frame */
char *background, **frame;
} sprite;
void Set_Video_Mode(int mode)
{
union REGS regs;
regs.x.ax = mode;
int86(0x10, ®s, ®s);
}
void plot_pixel(short int x, short int y, char color)
{
video_buffer[(y << 8) + (y << 6) + x] = color;
}
void bit_blt(int x, int y, char *bitmap)
{
int yindex, offset = (y << 8) + (y << 6) + x;
char width = bitmap[0], height = bitmap[1];
bitmap += 2; /* skip the first two bytes */
for (yindex = 0; yindex < height; yindex++) {
memcpy((char *)video_buffer+offset,bitmap,width);
offset += 320; /* next line of video buffer */
bitmap += width; /* next line of bitmap */
}
}
void reverse_bit_blt(int x, int y, char *bitmap)
{
int yindex, offset = (y << 8) + (y << 6) + x;
char width = bitmap[0], height = bitmap[1];
bitmap += 2; /* skip the first two bytes */
for (yindex = 0; yindex < height; yindex++) {
memcpy(bitmap,(char *)video_buffer+offset,width);
offset += 320; /* next line of video buffer */
bitmap += width; /* next line of bitmap */
}
}
void transparent_bit_blt(int x, int y, char *buffer)
{
int xindex, yindex, offset = (y << 8) + (y << 6) + x;
char width = buffer[0], height = buffer[1];
buffer += 2; /* skip the first two bytes */
for (yindex = 0; yindex < height; yindex++) {
for (xindex = 0; xindex < width; xindex++) {
if (buffer[xindex]) {
video_buffer[offset+xindex] = buffer[xindex];
}
}
offset += 320; /* next line of video buffer */
buffer += width; /* next line of bitmap */
}
}
void draw_sprite(sprite *sprite)
{
sprite->x += sprite->x_vel; sprite->y += sprite->y_vel;
reverse_bit_blt(sprite->x,sprite->y,sprite->background);
transparent_bit_blt(sprite->x,sprite->y,sprite->frame[sprite->cur_frame]);
}
void erase_sprite(sprite *sprite)
{
bit_blt(sprite->x,sprite->y,sprite->background);
}
char bitmap[102] = {
10,10, /* width and height of block */
15,15,15,15,15,15,15,15,15,15, /* strip 1 */
15,15,00,00,00,00,00,00,15,15,
15,00,15,00,00,00,00,15,00,15,
15,00,00,15,00,00,15,00,00,15,
15,00,00,00,15,15,00,00,00,15,
15,00,00,00,15,15,00,00,00,15,
15,00,00,15,00,00,15,00,00,15,
15,00,15,00,00,00,00,15,00,15,
15,15,00,00,00,00,00,00,15,15,
15,15,15,15,15,15,15,15,15,15 /* strip 10 */
};
int main(void)
{
unsigned count;
sprite sprite;
__djgpp_nearptr_enable();
video_buffer += __djgpp_conventional_base;
Set_Video_Mode(GRAPHICS);
sprite.x = 160;
sprite.y = 100;
sprite.x_vel = 1;
sprite.y_vel = 1;
sprite.width = 10;
sprite.height = 10;
sprite.num_frames = 1;
sprite.cur_frame = 0;
sprite.background = (char *)malloc(sprite.width*sprite.height+2);
sprite.background[0] = 10; sprite.background[1] = 10;
sprite.frame = (char **)malloc(sizeof(char *)*sprite.num_frames);
sprite.frame[0] = (char *)&bitmap;
for(count = 0; count < 32000; count++) {
plot_pixel(rand() % 320, rand() % 200, rand() % 256);
}
while(!kbhit())
{
if((sprite.x > 309) || (sprite.x < 1))
sprite.x_vel = -sprite.x_vel;
if((sprite.y > 189) || (sprite.y < 1))
sprite.y_vel = -sprite.y_vel;
draw_sprite(&sprite);
delay(10);
erase_sprite(&sprite);
}
Set_Video_Mode(TEXT);
__djgpp_nearptr_disable();
exit(0);
}
- Raw text -