delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/03/05/02:15:38

From: Drizzle <drysle AT rocketmail DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Blit Sprite w/ out destroying background..
Date: Wed, 04 Mar 1998 21:25:56 -0600
Organization: AT&T WorldNet Services
Lines: 204
Message-ID: <6dl5vh$9nc@bgtnsc03.worldnet.att.net>
NNTP-Posting-Host: 12.66.101.245
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

    Using allegro I'm trying to make/use a Sprite_Under, And
Erase_Sprite to display a sprite without having to redraw the
background.. I've spent hours trying different source code from
different sources trying to get them to work with DJGPP, and Allegro..
If anyone can help, please do..
    The following code is just from an example program i'm making to try
to get these working, sprite.h is just a header file for a spare
datafile with a few rle sprites in it.. using allegro for graphics
functions(draw sprite, set screen resolution)..

Thanks in Advance..


#include <stdio.h>
#include <stdlib.h>
#include <allegro.h>
#include <string.h>

#include "sprite.h"

#define sprite_width 32
#define sprite_height 32

BITMAP *buffer;
BITMAP *backdrop;
DATAFILE *datafile=NULL;

typedef struct sprite_type {
        int state;
  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, *sprite_ptr;

/****************************************************************/

void Sprite_Erase(sprite_ptr sprite,unsigned char *buffer)
{
// replace the background that was behind the sprite
// this function replaces the background that was saved from where a
sprite
// was going to be placed

unsigned char *back_buffer; // background buffer for sprite

int y,                          // current line being scanned
    width,                      // size of sprite
    height;

// alias a pointer to sprite background for ease of access

back_buffer = sprite->background;

// alias width and height

width  = sprite->width;
height = sprite->height;

// compute offset in destination buffer

buffer = buffer + (sprite->y << 8) + (sprite->y << 6) + sprite->x;

for (y=0; y<height; y++)
    {
    // copy the next from sprite background buffer to destination buffer

    memcpy((char *)buffer,
             (char *)back_buffer,
             width);

    // move to next line in destination buffer and in sprite background
buffer

    buffer      += SCREEN_W;
    back_buffer += width;

    } // end for y

} // end Sprite_Erase


void Sprite_Under(sprite_ptr sprite, unsigned char *buffer)
{

// this function scans the background under a sprite so that when the
sprite
// is drawn the background isn't obliterated

unsigned char *back_buffer; // background buffer for sprite

int y,                          // current line being scanned
    width,                      // size of sprite
    height;

// alias a pointer to sprite background for ease of access

back_buffer = sprite->background;

// alias width and height

width  = sprite->width;
height = sprite->height;

// compute offset of background in source buffer

buffer = buffer + (sprite->y << 8) + (sprite->y << 6) + sprite->x;

for (y=0; y<height; y++)
    {
    // copy the next row out off image buffer into sprite background
buffer

    memcpy((char *)back_buffer,
           (char *)buffer,
           width);

    // move to next line in source buffer and in sprite background
buffer

    buffer      += SCREEN_W;
    back_buffer += width;

    } // end for y

} // end Sprite_Under
/****************************************************************/
sprite ptank;

int main(void)
{
 allegro_init();
 install_timer();
 install_keyboard();
 initialise_joystick();
 install_mouse();
 if((datafile=load_datafile("sprite.dat"))==NULL)
 {
         /*Shutdown Allegro*/
         allegro_exit();
         /*Print error message*/
         printf("Error loading \"sprite.dat\"\n%s\n\n", allegro_error);
         exit(1);
 }
 printf("Setting up the Sound.");
 if(install_sound(DIGI_AUTODETECT,MIDI_AUTODETECT,NULL) != 0)
 {
         /*Shutdown Allegro*/
         allegro_exit();
         /*Print error message*/
         printf("Error setting up Sound\n%s\n\n", allegro_error);
         exit(1);
 }
 /*Set the graphics mode*/
 printf("Setting up graphics mode 640x480.\n");
 if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0)
 {
         /*Shutdown Allegro*/
         allegro_exit();
         /*Print error message*/
         printf("Error setting graphics mode\n%s\n\n", allegro_error);
         exit(1);
 }
 set_pallete(datafile[pal].dat);
 backdrop=create_bitmap(640,455);
 blit(datafile[back].dat, backdrop, 0, 0, 0, 0, 640, 455);
 buffer=create_bitmap(640,480);
 clear(buffer);
 blit(backdrop,buffer,0,0,10,30,640,455);

 Sprite_Under(&ptank,buffer);
/*********** Init Sprite ************/
 ptank.x = 320; ptank.y = 240;
        ptank.x_vel = 1; ptank.y_vel = 0;
        ptank.width = 32; ptank.height = 32;
        ptank.num_frames = 1;
        ptank.cur_frame = 0;
        ptank.background = (char *)malloc(ptank.width*ptank.height+2);
        ptank.background[0] = 32; ptank.background[1] = 32;
        ptank.frame = (char **)malloc(sizeof(char *)*ptank.num_frames);
        ptank.frame[0] = (char *)&ptank;
/************************************/
while(!key[KEY_ESC])
{

 Sprite_Erase(&ptank,buffer);

 if(key[KEY_DOWN])
       ptank.y+=6;
if(key[KEY_UP])
       ptank.y-=6;

 Sprite_Under(&ptank,buffer);

 draw_rle_sprite(buffer,datafile[tank].dat,ptank.x,ptank.y);
 blit(buffer,screen,0,0,0,0,640,480);
}
allegro_exit();
 return 0;
}


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019