messing with assembler

This commit is contained in:
John Bintz 2024-02-21 16:25:00 -05:00
parent cf3cdad788
commit 3e18d9cdb2
7 changed files with 174 additions and 14 deletions

View File

@ -55,8 +55,8 @@ void renderArenaTile(int x, int y) {
break; break;
} }
target->x = x * 20; target->x = x * TILE_SIZE;
target->y = y * 20; target->y = y * TILE_SIZE;
drawSprite(target); drawSprite(target);
} }

View File

@ -22,3 +22,4 @@
#define RAD2DEG (180/3.14159) #define RAD2DEG (180/3.14159)
#define DEG2RAD (3.14159/180) #define DEG2RAD (3.14159/180)
#define ENEMY_MAX_COUNT (20)

149
game.c
View File

@ -1,6 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include <conio.h> #include <conio.h>
#include <stdlib.h>
#include <time.h>
#include "system/vga.h" #include "system/vga.h"
#include "system/keyboard.h" #include "system/keyboard.h"
@ -15,7 +17,7 @@
struct BMPImage spritesheetImage; struct BMPImage spritesheetImage;
struct VGAColor vgaColors[256]; struct VGAColor vgaColors[256];
struct SpriteRender rabbit, mouse, bullet; struct SpriteRender rabbit, mouse, bullet, enemy;
struct RabbitPosition rabbitPosition = { struct RabbitPosition rabbitPosition = {
.rabbitPosition = { 60, 60 }, .rabbitPosition = { 60, 60 },
.rabbitLimits = { { 20, 20 }, { 180, 180 } }, .rabbitLimits = { { 20, 20 }, { 180, 180 } },
@ -24,11 +26,13 @@ struct RabbitPosition rabbitPosition = {
.mouseDotPosition = { 0, 0 } .mouseDotPosition = { 0, 0 }
}; };
struct EnemyPosition enemyPosition[ENEMY_MAX_COUNT];
struct BulletPosition rabbitBulletPosition[RABBIT_BULLET_LIMIT]; struct BulletPosition rabbitBulletPosition[RABBIT_BULLET_LIMIT];
struct RabbitWeaponry rabbitWeaponry; struct RabbitWeaponry rabbitWeaponry;
void setupRabbitBullets() { void setupRabbitBullets() {
char i; int i;
for (i = 0; i < RABBIT_BULLET_LIMIT; ++i) { for (i = 0; i < RABBIT_BULLET_LIMIT; ++i) {
rabbitBulletPosition[i].isActive = 0; rabbitBulletPosition[i].isActive = 0;
@ -38,6 +42,15 @@ void setupRabbitBullets() {
rabbitWeaponry.cooldown = 0; rabbitWeaponry.cooldown = 0;
} }
void setupEnemies() {
int i;
for (i = 0; i < ENEMY_MAX_COUNT; ++i) {
enemyPosition[i].isActive = 0;
enemyPosition[i].willBeInactive = 0;
}
}
void attemptToFireRabbitBullet() { void attemptToFireRabbitBullet() {
char okToFire = 0, availableBullet, i; char okToFire = 0, availableBullet, i;
signed char doubleVelocityX, doubleVelocityY; signed char doubleVelocityX, doubleVelocityY;
@ -99,6 +112,62 @@ void advanceRabbitBullets() {
if (rabbitWeaponry.cooldown > 0) rabbitWeaponry.cooldown--; if (rabbitWeaponry.cooldown > 0) rabbitWeaponry.cooldown--;
} }
void detectPlayerBulletToEnemyCollisions() {
int bulletIdx, enemyIdx;
for (bulletIdx = 0; bulletIdx < RABBIT_BULLET_LIMIT; ++bulletIdx) {
if (!rabbitBulletPosition[bulletIdx].isActive) continue;
for (enemyIdx = 0; enemyIdx < ENEMY_MAX_COUNT; ++enemyIdx) {
if (!enemyPosition[enemyIdx].isActive) continue;
// bullets hit enemies if:
// * bulletY - bullet_height_start is within 3px of enemy base
// *
}
}
}
struct SpawnPointRange {
int left, width;
int top, height;
};
struct SpawnPointRange spawnPointRanges[1] = {
{ .left = 50, .width = 30, .top = 50, .height = 30 }
};
void maybeSpawnEnemy() {
char canSpawn;
int i, availableEnemy;
int spawnX, spawnY;
for (i = 0; i < ENEMY_MAX_COUNT; ++i) {
if (!enemyPosition[i].isActive) {
availableEnemy = i;
canSpawn = 1;
break;
}
}
if (!canSpawn) return;
spawnX = spawnPointRanges[0].left + rand() % spawnPointRanges[0].width;
spawnY = spawnPointRanges[0].top + rand() % spawnPointRanges[0].height;
enemyPosition[availableEnemy].isActive = 1;
enemyPosition[availableEnemy].enemyPosition[0] = spawnX;
enemyPosition[availableEnemy].enemyPosition[1] = spawnY;
}
void setupEnemySprites() {
buildSpriteFromSpritesheet(
&spritesheetImage,
&enemy,
0, 20, 16, 16
);
}
void setupRabbitSprites() { void setupRabbitSprites() {
buildSpriteFromSpritesheet( buildSpriteFromSpritesheet(
&spritesheetImage, &spritesheetImage,
@ -145,20 +214,28 @@ void renderRabbit() {
drawSprite(&rabbit); drawSprite(&rabbit);
} }
void renderEnemies() {
int i;
for (i = 0; i < ENEMY_MAX_COUNT; ++i) {
if (!enemyPosition[i].isActive) continue;
enemy.x = enemyPosition[i].enemyPosition[0];
enemy.y = enemyPosition[i].enemyPosition[1];
drawSprite(&enemy);
}
}
void renderRabbitBullets() { void renderRabbitBullets() {
char i; char i;
char buffer[20];
for (i = 0; i < RABBIT_BULLET_LIMIT; ++i) { for (i = 0; i < RABBIT_BULLET_LIMIT; ++i) {
if (!rabbitBulletPosition[i].isActive) continue; if (!rabbitBulletPosition[i].isActive) continue;
bullet.x = rabbitBulletPosition[i].x; bullet.x = rabbitBulletPosition[i].x;
bullet.y = rabbitBulletPosition[i].y; bullet.y = rabbitBulletPosition[i].y;
sprintf(buffer, "%d %d", bullet.x, bullet.y);
writeString(buffer, 0, 10);
drawSprite(&bullet); drawSprite(&bullet);
} }
} }
@ -185,6 +262,23 @@ void drawOnlyRabbitArena() {
drawOnlyArena(&bounds); drawOnlyArena(&bounds);
} }
void drawOnlyEnemiesArena() {
int i;
for (i = 0; i < ENEMY_MAX_COUNT; ++i) {
if (!enemyPosition[i].isActive) continue;
enemy.x = enemyPosition[i].oldEnemyPosition[0];
enemy.y = enemyPosition[i].oldEnemyPosition[1];
getSpriteBounds(&enemy, &bounds);
drawOnlyArena(&bounds);
if (enemyPosition[i].willBeInactive) {
enemyPosition[i].willBeInactive = 0;
}
}
}
void drawOnlyRabbitBulletArena() { void drawOnlyRabbitBulletArena() {
int i; int i;
for (i = 0; i < RABBIT_BULLET_LIMIT; ++i) { for (i = 0; i < RABBIT_BULLET_LIMIT; ++i) {
@ -201,9 +295,8 @@ void drawOnlyRabbitBulletArena() {
} }
} }
int main(void) { int setupGame() {
FILE *fh; FILE *fh;
int keepRunning = 1;
installKeyboardHandler(); installKeyboardHandler();
initializeDrawBuffer(); initializeDrawBuffer();
@ -217,6 +310,7 @@ int main(void) {
setupWallSprites(&spritesheetImage); setupWallSprites(&spritesheetImage);
setupRabbitSprites(); setupRabbitSprites();
setupRabbitBullets(); setupRabbitBullets();
setupEnemySprites();
setVideoMode(VIDEO_MODE_VGA_256); setVideoMode(VIDEO_MODE_VGA_256);
bmp256ColorPaletteToVGAColorPalette(&spritesheetImage, vgaColors); bmp256ColorPaletteToVGAColorPalette(&spritesheetImage, vgaColors);
@ -227,6 +321,30 @@ int main(void) {
buildArena(); buildArena();
clearArenaRedrawRequests(); clearArenaRedrawRequests();
srand(time(NULL));
return 0;
}
double speedCalcs[200];
int currentSpeedCalc = 0;
double averageSpeedCalc;
clock_t startTime;
extern int doAThing(int);
#pragma aux (__cdecl) doAThing "doAThing_"
int main(void) {
int keepRunning = 1;
fprintf(stderr, "%d\n", doAThing(1000));
return 0;
if (setupGame()) return 1;
maybeSpawnEnemy();
while (keepRunning) { while (keepRunning) {
readMouse(&mouseStatus); readMouse(&mouseStatus);
populateKeyboardKeydownState(); populateKeyboardKeydownState();
@ -248,10 +366,14 @@ int main(void) {
advanceRabbitBullets(); advanceRabbitBullets();
drawOnlyRabbitArena(); drawOnlyRabbitArena();
drawOnlyEnemiesArena();
drawOnlyMouseArena(); drawOnlyMouseArena();
drawOnlyRabbitBulletArena(); drawOnlyRabbitBulletArena();
redrawArena(); redrawArena();
renderRabbit(); renderRabbit();
renderEnemies();
renderMouse(); renderMouse();
renderRabbitBullets(); renderRabbitBullets();
@ -269,7 +391,14 @@ int main(void) {
setVideoMode(VIDEO_MODE_80x25_TEXT); setVideoMode(VIDEO_MODE_80x25_TEXT);
uninstallKeyboardHandler(); uninstallKeyboardHandler();
fprintf(stderr, "meow\n"); averageSpeedCalc = 0;
for (currentSpeedCalc = 0; currentSpeedCalc < 200; ++currentSpeedCalc) {
averageSpeedCalc += speedCalcs[currentSpeedCalc];
}
averageSpeedCalc /= 200;
fprintf(stderr, "average: %f\n", averageSpeedCalc);
return 0; return 0;
} }

View File

@ -1,4 +1,4 @@
obj = game.o bmpload.o arena.o movement.o obj = game.o bmpload.o arena.o movement.o test.o
all: system/system.lib game.exe .SYMBOLIC all: system/system.lib game.exe .SYMBOLIC

View File

@ -1,4 +1,5 @@
#include "system/keyboard.h" #include "system/keyboard.h"
struct RabbitPosition { struct RabbitPosition {
int rabbitPosition[2]; int rabbitPosition[2];
int oldRabbitPosition[2]; int oldRabbitPosition[2];
@ -30,6 +31,15 @@ struct BulletPosition {
char velocityStep; char velocityStep;
}; };
struct EnemyPosition {
char isActive;
char willBeInactive;
int enemyPosition[2];
int oldEnemyPosition[2];
};
void calculateTargetAngle(struct RabbitPosition*, struct MouseStatus*); void calculateTargetAngle(struct RabbitPosition*, struct MouseStatus*);
void captureAndLimitMousePosition(struct RabbitPosition*, struct MouseStatus*); void captureAndLimitMousePosition(struct RabbitPosition*, struct MouseStatus*);
void handleRabbitMovement(struct RabbitPosition*, struct KeyboardKeydownState*); void handleRabbitMovement(struct RabbitPosition*, struct KeyboardKeydownState*);

View File

@ -57,6 +57,7 @@ void drawPixel(int x, int y, int color) {
void drawSprite(struct SpriteRender* sprite) { void drawSprite(struct SpriteRender* sprite) {
int x, y; int x, y;
byte pixel; byte pixel;
int modulo = VGA_DISPLAY_WIDTH - sprite->width;
byte* spriteData = sprite->data; byte* spriteData = sprite->data;
byte* drawBufferPos = byte* drawBufferPos =
@ -77,7 +78,7 @@ void drawSprite(struct SpriteRender* sprite) {
drawBufferPos++; drawBufferPos++;
} }
drawBufferPos += (VGA_DISPLAY_WIDTH - sprite->width); drawBufferPos += modulo;
spriteData += sprite->modulo; spriteData += sprite->modulo;
} }
} }

19
test.asm Normal file
View File

@ -0,0 +1,19 @@
PUBLIC doAThing_
.386
.model flat,c
_TEXT segment byte public 'CODE'
doAThing_:
push ebp
mov ebp, esp
mov eax, [ebp + 8]
pop ebp
ret
_TEXT ends
end