commit stuff

This commit is contained in:
John Bintz 2024-02-20 06:48:12 -05:00
parent 5dd033eeb9
commit 5ce88d42cf
9 changed files with 156 additions and 19 deletions

118
game.c
View File

@ -1,4 +1,6 @@
#include <stdio.h>
#include <math.h>
#include <conio.h>
#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 drawOnlyArena() {
int leftTileX, rightTileX,
topTileY, bottomTileY;
leftTileX = bounds.left / 20;
rightTileX = bounds.right / 20;
topTileY = bounds.top / 20;
bottomTileY = bounds.bottom / 20;
renderArenaTile(leftTileX, topTileY);
if (leftTileX != rightTileX) {
renderArenaTile(rightTileX, topTileY);
if (topTileY != bottomTileY) {
renderArenaTile(rightTileX, bottomTileY);
}
}
if (topTileY != bottomTileY) {
renderArenaTile(leftTileX, bottomTileY);
}
}
void drawOnlyMouseArena() {
getSpriteBounds(&mouse, &bounds);
drawOnlyArena();
}
void drawOnlyRabbitArena() {
int rabbitLeftTileX, rabbitRightTileX,
rabbitTopTileY, rabbitBottomTileY;
getSpriteBounds(&rabbit, &bounds);
rabbitLeftTileX = rabbitPosition[0] / 20;
rabbitRightTileX = (rabbitPosition[0] + 16) / 20;
rabbitTopTileY = rabbitPosition[1] / 20;
rabbitBottomTileY = (rabbitPosition[1] + 16) / 20;
drawOnlyArena();
}
renderArenaTile(rabbitLeftTileX, rabbitTopTileY);
if (rabbitLeftTileX != rabbitRightTileX) {
renderArenaTile(rabbitRightTileX, rabbitTopTileY);
if (rabbitTopTileY != rabbitBottomTileY) {
renderArenaTile(rabbitRightTileX, rabbitBottomTileY);
}
}
if (rabbitTopTileY != rabbitBottomTileY) {
renderArenaTile(rabbitLeftTileX, rabbitBottomTileY);
}
#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();

View File

@ -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;

View File

@ -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, &regs, &regs);
regs.h.ah = 0x09;
regs.h.al = c;
regs.h.bh = 0x00;
regs.h.bl = 1;
regs.w.cx = 1;
int386(BIOS_VIDEO_INTERRUPT, &regs, &regs);
currentX++;
}
}

View File

@ -23,3 +23,4 @@ extern byte *VGA;
void setVideoMode(byte);
void waitStartVbl();
void waitEndVbl();
void writeString(char *, int x, int y);

View File

@ -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

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 39 KiB

After

Width:  |  Height:  |  Size: 39 KiB

16
vga.c
View File

@ -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;

11
vga.h
View File

@ -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();