From 5ce88d42cfd610a34be06d5b76d1c62880541820 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 20 Feb 2024 06:48:12 -0500 Subject: [PATCH] commit stuff --- game.c | 114 ++++++++++++++++++++++++++++++++++++++++-------- mouse_io.c | 3 ++ pc_stuff.c | 29 ++++++++++++ pc_stuff.h | 1 + setup.sh | 1 + spritesheet.xcf | Bin 1213 -> 1243 bytes sprtsht.bmp | Bin 40206 -> 40206 bytes vga.c | 16 ++++++- vga.h | 11 +++++ 9 files changed, 156 insertions(+), 19 deletions(-) diff --git a/game.c b/game.c index 98f61dc..6aa4c5d 100644 --- a/game.c +++ b/game.c @@ -1,4 +1,6 @@ #include +#include +#include #include "vga.h" #include "keyboard.h" @@ -10,7 +12,7 @@ struct BMPImage spritesheetImage; struct VGAColor vgaColors[256]; struct SpriteRender arenaWallTop, arenaWallSide, arenaFloor; -struct SpriteRender rabbit; +struct SpriteRender rabbit, mouse; void setupWallSprites() { buildSpriteFromSpritesheet( @@ -38,6 +40,18 @@ void setupRabbitSprites() { &rabbit, 0, 20, 16, 16 ); + + rabbit.offsetX = 8; + rabbit.offsetY = 15; + + buildSpriteFromSpritesheet( + &spritesheetImage, + &mouse, + 16, 20, 8, 8 + ); + + mouse.offsetX = 4; + mouse.offsetY = 4; } char arenaLayout[10][10] = { @@ -99,10 +113,25 @@ int rabbitLimits[2][2] = { signed char rabbitVelocity[2] = { 0, 0 }; -void renderRabbit() { +struct SpriteBounds bounds; +struct MouseStatus mouseStatus; +char mousePosition[2] = { 0, 0 }; + +void setupRabbitDrawing() { rabbit.x = rabbitPosition[0]; rabbit.y = rabbitPosition[1]; +} +void setupMouseDrawing() { + mouse.x = mousePosition[0]; + mouse.y = mousePosition[1]; +} + +void renderMouse() { + drawSprite(&mouse); +} + +void renderRabbit() { drawSprite(&rabbit); } @@ -155,34 +184,75 @@ void handleRabbitMovement() { } } -void drawOnlyRabbitArena() { - int rabbitLeftTileX, rabbitRightTileX, - rabbitTopTileY, rabbitBottomTileY; +void drawOnlyArena() { + int leftTileX, rightTileX, + topTileY, bottomTileY; - rabbitLeftTileX = rabbitPosition[0] / 20; - rabbitRightTileX = (rabbitPosition[0] + 16) / 20; - rabbitTopTileY = rabbitPosition[1] / 20; - rabbitBottomTileY = (rabbitPosition[1] + 16) / 20; + leftTileX = bounds.left / 20; + rightTileX = bounds.right / 20; + topTileY = bounds.top / 20; + bottomTileY = bounds.bottom / 20; - renderArenaTile(rabbitLeftTileX, rabbitTopTileY); - if (rabbitLeftTileX != rabbitRightTileX) { - renderArenaTile(rabbitRightTileX, rabbitTopTileY); - if (rabbitTopTileY != rabbitBottomTileY) { - renderArenaTile(rabbitRightTileX, rabbitBottomTileY); + renderArenaTile(leftTileX, topTileY); + if (leftTileX != rightTileX) { + renderArenaTile(rightTileX, topTileY); + if (topTileY != bottomTileY) { + renderArenaTile(rightTileX, bottomTileY); } } - if (rabbitTopTileY != rabbitBottomTileY) { - renderArenaTile(rabbitLeftTileX, rabbitBottomTileY); + if (topTileY != bottomTileY) { + renderArenaTile(leftTileX, bottomTileY); } } +void drawOnlyMouseArena() { + getSpriteBounds(&mouse, &bounds); + + drawOnlyArena(); +} + +void drawOnlyRabbitArena() { + getSpriteBounds(&rabbit, &bounds); + + drawOnlyArena(); +} + +#define RAD2DEG (180/3.14159) +#define DEG2RAD (3.14159/180) +#define MOUSE_DISTANCE (32) + +void calculateMouseAngle() { + float distanceX, distanceY; + float angle; + + char buffer[20]; + + distanceX = mouseStatus.xPosition - rabbitPosition[0]; + distanceY = mouseStatus.yPosition - rabbitPosition[1]; + + angle = atan2(distanceY, distanceX) * 180 / 3.14159; + + sprintf(buffer, "%f", angle); + writeString(buffer, 0, 2); + + sprintf(buffer, "%f", distanceX); + writeString(buffer, 0, 3); + + sprintf(buffer, "%d", mouseStatus.xPosition); + writeString(buffer, 0, 4); + + distanceX = cos(angle * DEG2RAD) * MOUSE_DISTANCE; + distanceY = sin(angle * DEG2RAD) * MOUSE_DISTANCE; + + mousePosition[0] = rabbitPosition[0] + distanceX; + mousePosition[1] = rabbitPosition[1] + distanceY; +} + int main(void) { FILE *fh; int keepRunning = 1; - struct MouseStatus mouseStatus; installKeyboardHandler(); - activateMouse(&mouseStatus); initializeDrawBuffer(); fh = fopen("sprtsht.bmp", "rb"); @@ -198,18 +268,26 @@ int main(void) { bmp256ColorPaletteToVGAColorPalette(&spritesheetImage, vgaColors); setVGAColors(vgaColors, 256); + activateMouse(&mouseStatus); + buildArena(); while (keepRunning) { readMouse(&mouseStatus); populateKeyboardKeydownState(); handleRabbitMovement(); + calculateMouseAngle(); waitStartVbl(); + setupRabbitDrawing(); + setupMouseDrawing(); drawOnlyRabbitArena(); + drawOnlyMouseArena(); renderRabbit(); + renderMouse(); copyDrawBufferToDisplay(); + calculateMouseAngle(); waitEndVbl(); diff --git a/mouse_io.c b/mouse_io.c index 3114e66..cd82551 100644 --- a/mouse_io.c +++ b/mouse_io.c @@ -6,6 +6,9 @@ struct MouseStatus *_status; +/** + * Ensure this is called after setting the desired video mode. + */ int activateMouse(struct MouseStatus *status) { union REGS regs; int mouseActivated; diff --git a/pc_stuff.c b/pc_stuff.c index d28eb77..2c10948 100644 --- a/pc_stuff.c +++ b/pc_stuff.c @@ -21,3 +21,32 @@ void waitStartVbl() { void waitEndVbl() { while (!(inp(INPUT_STATUS) & VBLANK)); } + +#define FAILSAFE (80) + +void writeString(char *buffer, int x, int y) { + union REGS regs; + + int i, currentX = x; + char c; + + for (i = 0; i < FAILSAFE; ++i) { + c = buffer[i]; + if (c == 0) break; + + regs.h.ah = 0x02; + regs.h.bh = 0x00; + regs.h.dh = y; + regs.h.dl = currentX; + int386(BIOS_VIDEO_INTERRUPT, ®s, ®s); + + regs.h.ah = 0x09; + regs.h.al = c; + regs.h.bh = 0x00; + regs.h.bl = 1; + regs.w.cx = 1; + int386(BIOS_VIDEO_INTERRUPT, ®s, ®s); + + currentX++; + } +} diff --git a/pc_stuff.h b/pc_stuff.h index b12718b..ee3100d 100644 --- a/pc_stuff.h +++ b/pc_stuff.h @@ -23,3 +23,4 @@ extern byte *VGA; void setVideoMode(byte); void waitStartVbl(); void waitEndVbl(); +void writeString(char *, int x, int y); diff --git a/setup.sh b/setup.sh index 60c78ad..cb4126c 100644 --- a/setup.sh +++ b/setup.sh @@ -1,3 +1,4 @@ export WATCOM=~/Applications/open-watcom-v2/rel export INCLUDE=$WATCOM/h +export LIB=$WATCOM/lib286/dos:$WATCOM/lib286 export PATH=$WATCOM/binl64:$WATCOM/binl:$PATH diff --git a/spritesheet.xcf b/spritesheet.xcf index b1eb2d5fa36128e2e1fb9a49d50a5395dae6586d..b2af885bbf0de5fadf05f3fd722277aa87e93c53 100644 GIT binary patch delta 228 zcmdnXd7E>BCG-FP|0g=iPYht)n9#<=d6)qVSk6y=&m>>p3FY-d>4{K!DwLiHrRPHF zg;07alwJv?*Fx!yPNtjufWpXd`3`;QvMpj!OHe_IAv|(Uo eWCgOBfmj!btp}6?s$t{=i*th2=udWFsRaOwZX7HC delta 194 zcmcc3xtDW-%f`v?ndIxUp}c%3T@0nmp>#Eru7}diP`VvT zcSGrZC_NcUPlwX8q4azxy%<6>FbFdVGYhj!?q!}KUC+?Xz{qIBz|3e3#5O>-Ef8BW Ma59=tj$^3>0BIo?nE(I) diff --git a/sprtsht.bmp b/sprtsht.bmp index 064e850c0de30ef1b091a0dd1111775f91848379..0d947b0e8cf549f036c05beb4537e8b2b1a9ff20 100644 GIT binary patch delta 91 zcmeC%#niWpX~I0_|NsAQT)3c(iIsJ-V#}V%|J%6`%%hWgI=MJlSs8$Um3896ZD{=M WlV!WF0F_T%cnnN#-kjIFfFA&us3Hmg delta 59 zcmeC%#niWpX~MjXi=-MSCa`SY)7Z*7`FYE}$@1ODCd+mopWM^QHF5bq2(=$5a%JO& LTE@+Jy$kpOZbKb? diff --git a/vga.c b/vga.c index bb593fd..a791f65 100644 --- a/vga.c +++ b/vga.c @@ -55,7 +55,13 @@ void drawSprite(struct SpriteRender* sprite) { byte pixel; byte* spriteData = sprite->data; - byte* drawBufferPos = drawBuffer + sprite->x + (sprite->y * VGA_DISPLAY_WIDTH); + byte* drawBufferPos = + drawBuffer + + sprite->x - + sprite->offsetX + + ( + (sprite->y - sprite->offsetY) * VGA_DISPLAY_WIDTH + ); for (y = 0; y < sprite->height; ++y) { for (x = 0; x < sprite->width; ++x) { @@ -72,6 +78,14 @@ void drawSprite(struct SpriteRender* sprite) { } } +void getSpriteBounds(struct SpriteRender *sprite, struct SpriteBounds *bounds) { + bounds->top = sprite->y - sprite->offsetY; + bounds->bottom = bounds->top + sprite->height; + + bounds->left = sprite->x - sprite->offsetX; + bounds->right = bounds->left + sprite->width; +} + void setVGAColors(struct VGAColor colors[], int totalColors) { int i; diff --git a/vga.h b/vga.h index c3a6028..2c353a6 100644 --- a/vga.h +++ b/vga.h @@ -23,10 +23,21 @@ struct SpriteRender { unsigned int height; int transparentColor; unsigned int modulo; + + int offsetX; + int offsetY; +}; + +struct SpriteBounds { + int top; + int right; + int bottom; + int left; }; void drawSprite(struct SpriteRender *sprite); void buildSpriteFromSpritesheet(struct BMPImage*, struct SpriteRender*, int, int, int, int); +void getSpriteBounds(struct SpriteRender *sprite, struct SpriteBounds *bounds); byte *initializeDrawBuffer(); byte *getDrawBuffer();