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

122 lines
2.7 KiB
C
Raw Normal View History

2024-02-15 01:15:55 +00:00
#include "vga.h"
#include "pc_stuff.h"
#include <malloc.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
int activePage = 0;
2024-02-15 13:34:50 +00:00
byte *drawBuffer;
2024-02-15 01:15:55 +00:00
2024-02-15 13:34:50 +00:00
byte *initializeDrawBuffer() {
2024-02-15 01:15:55 +00:00
int i;
drawBuffer = (byte *)malloc(VGA_DISPLAY_WIDTH * VGA_DISPLAY_HEIGHT);
for (i = 0; i < VGA_DISPLAY_WIDTH * VGA_DISPLAY_HEIGHT; ++i) {
drawBuffer[i] = 0;
}
2024-02-15 13:34:50 +00:00
return drawBuffer;
2024-02-15 01:15:55 +00:00
}
2024-02-15 13:34:50 +00:00
void copyDrawBufferToDisplay() {
2024-02-24 02:29:36 +00:00
memcpy(VGA, drawBuffer, VGA_DISPLAY_WIDTH * VGA_DISPLAY_HEIGHT);
2024-02-15 01:15:55 +00:00
}
2024-02-15 13:34:50 +00:00
void freeDrawBuffer() {
free(drawBuffer);
2024-02-15 01:15:55 +00:00
}
byte *getDrawBuffer() {
return drawBuffer;
}
clock_t startTime, endTime;
2024-02-15 13:34:50 +00:00
void buildSpriteFromSpritesheet(
struct BMPImage *spritesheetImage,
struct SpriteRender *spriteRender,
int positionX,
int positionY,
int width,
int height
) {
spriteRender->data = spritesheetImage->memoryStart + positionY * spritesheetImage->width + positionX;
spriteRender->modulo = spritesheetImage->width - width;
spriteRender->width = width;
spriteRender->height = height;
spriteRender->transparentColor = spritesheetImage->transparentColor;
2024-02-15 01:15:55 +00:00
}
2024-02-24 12:36:18 +00:00
void buildCompiledSprite(
void (*code)(byte *),
struct CompiledSpriteRender *compiledSpriteRender,
int width,
int height
) {
compiledSpriteRender->code = code;
compiledSpriteRender->width = width;
compiledSpriteRender->height = height;
}
2024-02-20 17:39:28 +00:00
void drawPixel(int x, int y, int color) {
drawBuffer[y * VGA_DISPLAY_WIDTH + x] = color;
}
2024-02-15 01:15:55 +00:00
void drawSprite(struct SpriteRender* sprite) {
int x, y;
byte pixel;
2024-02-21 21:25:00 +00:00
int modulo = VGA_DISPLAY_WIDTH - sprite->width;
2024-02-15 01:15:55 +00:00
byte* spriteData = sprite->data;
2024-02-20 11:48:12 +00:00
byte* drawBufferPos =
drawBuffer +
sprite->x -
sprite->offsetX +
(
(sprite->y - sprite->offsetY) * VGA_DISPLAY_WIDTH
);
2024-02-15 01:15:55 +00:00
for (y = 0; y < sprite->height; ++y) {
for (x = 0; x < sprite->width; ++x) {
pixel = *(spriteData++);
if (pixel != sprite->transparentColor) {
*(drawBufferPos) = pixel;
}
drawBufferPos++;
}
2024-02-21 21:25:00 +00:00
drawBufferPos += modulo;
2024-02-15 01:15:55 +00:00
spriteData += sprite->modulo;
}
}
2024-02-24 12:36:18 +00:00
void drawCompiledSprite(struct CompiledSpriteRender* compiledSprite) {
byte *drawBufferPos = drawBuffer +
compiledSprite->x + compiledSprite->y * VGA_DISPLAY_WIDTH;
compiledSprite->code(drawBufferPos);
}
2024-02-20 11:48:12 +00:00
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;
}
2024-02-15 01:15:55 +00:00
void setVGAColors(struct VGAColor colors[], int totalColors) {
int i;
outp(0x3c8,0);
for (i = 0; i < totalColors; ++i) {
outp(0x3c9, colors[i].red);
outp(0x3c9, colors[i].green);
outp(0x3c9, colors[i].blue);
}
}