Archived

This topic is now archived and is closed to further replies.

Sprites background in array clash on recall

This topic is 5010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is an extract from my main game program. For the matter of simplicity i have created a sample program. There are two sprites namely Sprite_Red and Sprite_Yellow. ''Sprite_Red'' moves over a Yellow Stick and ''Sprite_Yellow'' moves over a Red Stick from top to bottom and vice versa, without disturbing the background. Things are OK as long as I enable only one Sprite lets say ''Sprite_Red'' or `Sprite_Yellow''. On enabling both the sprites, the background image of both the sprites in the array clashes. Both the sprites are using different array for storing their respective backgrounds. i don''t know why the arrays are clashing even though i am using different arrays. in no way i am able to understand what is wrong in my program. I am using Turbo C 3.0 DOS compiler. Pls. compile this and see the sample first. The main functions are: 1. Backg_Image_Capture() - Capture the background for both sprites. 2. DrawSprites() - Draws both sprites. 3. EraseSprites() - Erases both sprites using the background captured. I have commented `Sprite_Yellow'' portions in the above three functions by default so that `Sprite_Red'' works. I am listing below the full source code. pls. help as early as possible because i cannot go ahead unless i fix this bug. #include <stdlib.h> #include <conio.h> #include <alloc.h> #include <memory.h> #include <dos.h> #define SCREEN_WIDTH 320 #define SCREEN_HEIGHT 200 #define SCREEN_SEGMENT 0xA000 #define IMAGE_WIDTH 14 #define IMAGE_HEIGHT 14 #define INPUT_STATUS 0x03da #define VRETRACE 0x08 #define NUM_GHOSTS 1 char Sprite_Red[] = { 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, 12,12,12,12,12,12,12,12,12,12,12,12,12,12, }; char Sprite_Yellow[] = { 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, 14,14,14,14,14,14,14,14,14,14,14,14,14,14, }; //for storing the backgrounds. char Sprite_Red_Backg_Array[196]; char Sprite_Yellow_Backg_Array[196]; typedef unsigned char byte; byte *On_Screen=(byte *)MK_FP(0xA000,0); int Sprite_Red_X=126, Sprite_Red_Y=0, Sprite_Yellow_X=226, Sprite_Yellow_Y=0, ydir= 1; //---------------------------- Screen Codes Begins -------------------------- void EnterMode13H(void) {union REGS regs;regs.x.ax = 0x0013;int86(0x10, ®s, ®s);} void LeaveMode13H(void) {union REGS regs;regs.x.ax = 0x0003;int86(0x10, ®s, ®s);} //------------------------------------------------------------------------------------------ //------------------------- Plot-Read Pixels Begins ------------------------- void PlotPixel (unsigned int x, unsigned int y, unsigned char c) { if ((x < 320) && (y < 200)) { On_Screen[(y << 8) + (y << 6) + x] = c; } } unsigned char ReadPixel(unsigned int x, unsigned int y) { if((x < 320) && (y < 200)) { return (On_Screen[(y << 8) + (y << 6) + x]); } else { return(0); } } //------------------------------------------------------------------------------------------ //------------------------- Vertical Retrace Begins ------------------------- void wait_for_retrace(void) { while ((inp(INPUT_STATUS) & VRETRACE)) {}; while (!(inp(INPUT_STATUS) & VRETRACE)) {}; } //------------------------------------------------------------------------------------------ //--------------------------- DrawLine Begins -------------------------------- void DrawLine(int x, int y, int x1, int y1, unsigned char c) { int i, dx=0, dy=0, length; if (x==x1) { length = (y1-y); dy=1; } else { length = (x1-x); dx=1; } for (i=0; i<=(length); i++) { PlotPixel(x,y,c);x = x+dx; y = y+dy; } } //------------------------------------------------------------------------------------------ //------------------------------- IMAGE CAPTURE BEGINS ---------------------------------- Backg_Image_Capture() { int h, w, c; // sprite X, Y values - you can skip this if (Sprite_Red_Y == 186) {ydir=-1;} if (Sprite_Yellow_Y == 186) {ydir=-1;} if (Sprite_Red_Y == 1) {ydir=1;} if (Sprite_Yellow_Y == 1) {ydir=1;} Sprite_Red_Y = Sprite_Red_Y + ydir; Sprite_Yellow_Y = Sprite_Yellow_Y + ydir; //-------------------------- SPRITE RED BACKGROUND CAPTURE ------------------------------- c=0; for (h=0; h<= IMAGE_HEIGHT; h++) { for (w=0; w<= IMAGE_WIDTH; w++) { Sprite_Red_Backg_Array[c++] = ReadPixel(Sprite_Red_X+w, Sprite_Red_Y+h); } } //------------------------ SPRITE YELLOW BACKGROUND CAPTURE ------------------------------- /* c=0; for (h=0; h<= IMAGE_HEIGHT; h++) { for (w=0; w<= IMAGE_WIDTH; w++) { Sprite_Yellow_Backg_Array[c++] = ReadPixel(Sprite_Yellow_X+w, Sprite_Yellow_Y+h); } } */ } //--------------------------------- CAPTURE ENDS ------------------------------- //------------------------- Draw Sprites Begins ----------------------------- void DrawSprites() { int h, w; char far *Sprite_Red_RowPtr; char far *Sprite_Red_BufferPtr; char far *Sprite_Red_ImagePtr; char far *Sprite_Yellow_RowPtr; char far *Sprite_Yellow_BufferPtr; char far *Sprite_Yellow_ImagePtr; //--------------------------- DRAW SPRITE_RED ------------------------------- Sprite_Red_RowPtr = On_Screen+(Sprite_Red_Y * SCREEN_WIDTH) + Sprite_Red_X; Sprite_Red_ImagePtr = Sprite_Red; for (h = 0; h < IMAGE_HEIGHT; h++) { for (w=0, Sprite_Red_BufferPtr = Sprite_Red_RowPtr; w < IMAGE_WIDTH; w++) { *Sprite_Red_BufferPtr++ = *Sprite_Red_ImagePtr++; } Sprite_Red_RowPtr += SCREEN_WIDTH; } //------------------------------------------------------------------------------------------ //--------------------------- DRAW SPRITE_YELLOW ------------------------------- /* Sprite_Yellow_RowPtr = On_Screen+(Sprite_Yellow_Y * SCREEN_WIDTH) + Sprite_Yellow_X; Sprite_Yellow_ImagePtr = Sprite_Yellow; for (h = 0; h < IMAGE_HEIGHT; h++) { for (w=0, Sprite_Yellow_BufferPtr = Sprite_Yellow_RowPtr; w < IMAGE_WIDTH; w++) { *Sprite_Yellow_BufferPtr++ = *Sprite_Yellow_ImagePtr++; } Sprite_Yellow_RowPtr += SCREEN_WIDTH; } */ //------------------------------------------------------------------------------------------ } //----------------------- Erase Sprites Begins ----------------------------- void EraseSprites() { int h, w, c; char far *Sprite_Red_RowPtr; char far *Sprite_Red_BufferPtr; char far *Sprite_Yellow_RowPtr; char far *Sprite_Yellow_BufferPtr; Sprite_Red_RowPtr = On_Screen+(Sprite_Red_Y * SCREEN_WIDTH) + Sprite_Red_X; Sprite_Yellow_RowPtr = On_Screen+(Sprite_Yellow_Y * SCREEN_WIDTH) + Sprite_Yellow_X; //------------------------- ERASE SPRITE_RED --------------------------------------- c=0; for (h = 0; h <= IMAGE_HEIGHT; h++) { for (w=0, Sprite_Red_BufferPtr = Sprite_Red_RowPtr; w <= IMAGE_WIDTH; w++) { *Sprite_Red_BufferPtr++ = Sprite_Red_Backg_Array[c++]; } Sprite_Red_RowPtr += SCREEN_WIDTH; } //------------------------- ERASE SPRITE_YELLOW --------------------------------------- /* c=0; for (h = 0; h <= IMAGE_HEIGHT; h++) { for (w=0, Sprite_Yellow_BufferPtr = Sprite_Yellow_RowPtr; w <= IMAGE_WIDTH; w++) { *Sprite_Yellow_BufferPtr++ = Sprite_Yellow_Backg_Array[c++]; } Sprite_Yellow_RowPtr += SCREEN_WIDTH; } */ } //------------------------------------------------------------------------------------------ //--------------------------- Draw Red & Yellow Stics -------------------------------- DrawSticks() { int i, rsx=130, ysx=230; for (i = 0; i <= 5; i++) { DrawLine(rsx,1,rsx,199,14); rsx++; } for (i = 0; i <= 5; i++) { DrawLine(ysx,1,ysx,199,12); ysx++; } } //------------------------------------------------------------------------------------------ void main() { EnterMode13H(); DrawSticks(); do { Backg_Image_Capture(); DrawSprites(); wait_for_retrace(); //getch(); EraseSprites(); } while (!kbhit()); getch(); LeaveMode13H(); }

Share this post


Link to post
Share on other sites
Please don''t crosspost. One forum is sufficient.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites

This topic is 5010 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.