dos-vga-arena-shooter-game/unch1.c

156 lines
3.4 KiB
C
Raw Normal View History

2024-02-15 01:15:55 +00:00
#include <conio.h>
#include <dos.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
#include "types.h"
#include "mouse_io.h"
#include "pc_stuff.h"
#include "bmp_loader.h"
#include "vga.h"
#include "keyboard.h"
byte MOUSE_POINTER[8][8] = {
{ 2, 2, 2, 2, 2, 0, 0, 0 },
{ 2, 1, 1, 1, 2, 0, 0, 0 },
{ 2, 1, 1, 1, 2, 0, 0, 0 },
{ 2, 1, 1, 1, 2, 0, 0, 0 },
{ 2, 2, 2, 255, 1, 2, 0, 0 },
{ 0, 0, 0, 0, 2, 1, 2, 0 },
{ 0, 0, 0, 0, 0, 2, 1, 2 },
{ 0, 0, 0, 0, 0, 0, 2, 0 },
};
byte WALKER[9][8] = {
{ 0, 0, 0, 4, 4, 0, 0, 0 },
{ 0, 0, 4, 5, 5, 4, 0, 0 },
{ 0, 4, 5, 5, 5, 5, 4, 0 },
{ 0, 0, 0, 4, 4, 0, 0, 0 },
{ 0, 0, 4, 5, 255, 4, 0, 0 },
{ 0, 4, 5, 5, 5, 5, 4, 0 },
{ 0, 0, 0, 4, 4, 0, 0, 0 },
{ 0, 0, 4, 5, 5, 4, 0, 0 },
{ 0, 4, 5, 5, 5, 5, 4, 0 }
};
int main(void) {
FILE *fh;
struct BMPImage bmpImage, spriteSheetImage;
struct MouseStatus mouseStatus = { .xPosition = 0, .yPosition = 0, .leftButtonDown = 0 };
struct VGAColor colors[256];
byte *spriteSheet, *chicken, *drawBuffer;
int i, x, y;
int currentMouseSprite = 0;
int mouseSpriteDelayCount = 0;
float measuredTime;
int walkerX = 0, walkerY = 0;
int yOffset;
struct SpriteRender mousePointer = {
.data = (byte *)MOUSE_POINTER,
.transparentColor = 0,
.width = 8,
.height = 8,
.modulo = 48
};
struct SpriteRender walker = {
.data = (byte *)WALKER,
.transparentColor = 0,
.width = 8,
.height = 9,
.modulo = 0
};
installKeyboardHandler();
initializeDrawBuffer();
spriteSheet = malloc(64 * 64);
spriteSheetImage.memoryStart = spriteSheet;
fh = fopen("sprtsht.bmp", "rb");
if (readBMPIntoMemory(fh, &spriteSheetImage)) return 1;
fclose(fh);
setVideoMode(VIDEO_MODE_VGA_256);
chicken = malloc(320 * 256);
bmpImage.memoryStart = chicken;
fh = fopen("chicken.bmp", "rb");
readBMPIntoMemory(fh, &bmpImage);
fclose(fh);
mousePointer.data = spriteSheet;
mousePointer.width = 16;
mousePointer.height = 16;
mousePointer.modulo = 48;
bmp256ColorPaletteToVGAColorPalette(&bmpImage, colors);
setVGAColors(colors);
activateMouse(&mouseStatus);
readMouse(&mouseStatus);
while (!mouseStatus.leftButtonDown) {
readMouse(&mouseStatus);
populateKeyboardKeydownState();
if (keyboardKeydownState.KEY_W) walkerY -= 1;
if (keyboardKeydownState.KEY_S) walkerY += 1;
if (keyboardKeydownState.KEY_A) walkerX -= 1;
if (keyboardKeydownState.KEY_D) walkerX += 1;
drawBuffer = getDrawBuffer();
memcpy(drawBuffer, chicken, 320 * 200);
mousePointer.x = mouseStatus.xPosition;
mousePointer.y = mouseStatus.yPosition;
drawSprite(&mousePointer);
for (i = 0; i < 100; ++i) {
walker.x = walkerX + i;
walker.y = walkerY + i;
drawSprite(&walker);
}
waitStartVbl();
memcpy((byte *)0xa0000, drawBuffer, 320 * 200);
waitEndVbl();
//drawBufferToUnchainedMemory();
//copyUnchainedMemoryToActive(chicken);
//swapPlanes();
mouseSpriteDelayCount += 1;
if (mouseSpriteDelayCount >= 15) {
currentMouseSprite = 1 - currentMouseSprite;
mousePointer.data = spriteSheet + (currentMouseSprite * 16);
mouseSpriteDelayCount = 0;
}
}
uninstallKeyboardHandler();
setVideoMode(VIDEO_MODE_80x25_TEXT);
free(spriteSheet);
free(chicken);
freeDrawBuffer();
fprintf(stderr, "%lu %lu %lu\n", startTime, endTime, (clock_t)(endTime - startTime));
return 0;
}