From 35d7ba95b9ed24adfa0a96d52fa9d02ce07a7b59 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Sat, 24 Feb 2024 10:38:46 -0500 Subject: [PATCH] finish spritesheet handling, hook up to makefile --- arena.c | 15 +++++++-- bin/build_spritesheet_asm.rb | 16 +++++++-- game.c | 55 +++++++++++++++--------------- makefile | 3 ++ sprites.asm | 16 ++++----- sprites.h | 63 ++++++++++++++++++++++++++++++++++- spritesheet.xcf | Bin 1251 -> 1257 bytes sprtsht.bmp | Bin 40206 -> 40206 bytes system/vga.c | 8 +++-- system/vga.h | 11 ++++-- 10 files changed, 143 insertions(+), 44 deletions(-) diff --git a/arena.c b/arena.c index 9616cc5..90a45bd 100644 --- a/arena.c +++ b/arena.c @@ -25,19 +25,28 @@ void setupWallSprites() { buildCompiledSprite( &sprite_arenaWallTop, &arenaWallTop, - 20, 20 + SPRITE_ARENAWALLTOP_WIDTH, + SPRITE_ARENAWALLTOP_HEIGHT, + SPRITE_ARENAWALLTOP_OFFSET_X, + SPRITE_ARENAWALLTOP_OFFSET_Y ); buildCompiledSprite( &sprite_arenaWallSide, &arenaWallSide, - 20, 20 + SPRITE_ARENAWALLSIDE_WIDTH, + SPRITE_ARENAWALLSIDE_HEIGHT, + SPRITE_ARENAWALLSIDE_OFFSET_X, + SPRITE_ARENAWALLSIDE_OFFSET_Y ); buildCompiledSprite( &sprite_arenaFloor, &arenaFloor, - 20, 20 + SPRITE_ARENAFLOOR_WIDTH, + SPRITE_ARENAFLOOR_HEIGHT, + SPRITE_ARENAFLOOR_OFFSET_X, + SPRITE_ARENAFLOOR_OFFSET_Y ); } diff --git a/bin/build_spritesheet_asm.rb b/bin/build_spritesheet_asm.rb index b80f06b..3d6fdab 100755 --- a/bin/build_spritesheet_asm.rb +++ b/bin/build_spritesheet_asm.rb @@ -68,9 +68,10 @@ C_TEMPLATE = <<~C <% sprites.each do |sprite| %> extern void <%= sprite.function_name %>(byte *); + <% sprite.constants.each do |constant, value| %> + #define <%= constant %> (<%= value %>) + <% end %> <% end %> - - // TODO: something with bounds checking here #endif C @@ -102,6 +103,17 @@ class AssemblerSprite "#{@prefix}_#{@name}" end + def constants + [ + ['WIDTH', @pixel_data[0].length], + ['HEIGHT', @pixel_data.length], + ['OFFSET_X', @offset_x], + ['OFFSET_Y', @offset_y] + ].map do |suffix, value| + ["#{function_name.upcase}_#{suffix}", value] + end + end + def bytes return @bytes if defined?(@bytes) diff --git a/game.c b/game.c index cbd3f83..09bd0c0 100644 --- a/game.c +++ b/game.c @@ -18,7 +18,7 @@ struct BMPImage spritesheetImage; struct VGAColor vgaColors[256]; -struct SpriteRender rabbit, mouse, bullet, enemy; +struct CompiledSpriteRender rabbit, mouse, bullet, enemy; struct RabbitPosition rabbitPosition = { .rabbitPosition = { 60, 60 }, .rabbitLimits = { { 20, 20 }, { 180, 180 } }, @@ -162,40 +162,43 @@ void maybeSpawnEnemy() { } void setupEnemySprites() { - buildSpriteFromSpritesheet( - &spritesheetImage, + buildCompiledSprite( + &sprite_enemy, &enemy, - 0, 20, 16, 16 + SPRITE_ENEMY_WIDTH, + SPRITE_ENEMY_HEIGHT, + SPRITE_ENEMY_OFFSET_X, + SPRITE_ENEMY_OFFSET_Y ); } void setupRabbitSprites() { - buildSpriteFromSpritesheet( - &spritesheetImage, + buildCompiledSprite( + &sprite_rabbit, &rabbit, - 0, 20, 16, 16 + SPRITE_RABBIT_WIDTH, + SPRITE_RABBIT_HEIGHT, + SPRITE_RABBIT_OFFSET_X, + SPRITE_RABBIT_OFFSET_Y ); - rabbit.offsetX = 8; - rabbit.offsetY = 15; - - buildSpriteFromSpritesheet( - &spritesheetImage, + buildCompiledSprite( + &sprite_mouse, &mouse, - 16, 20, 8, 8 + SPRITE_MOUSE_WIDTH, + SPRITE_MOUSE_HEIGHT, + SPRITE_MOUSE_OFFSET_X, + SPRITE_MOUSE_OFFSET_Y ); - mouse.offsetX = 4; - mouse.offsetY = 4; - - buildSpriteFromSpritesheet( - &spritesheetImage, + buildCompiledSprite( + &sprite_bullet, &bullet, - 16, 28, 4, 4 + SPRITE_BULLET_WIDTH, + SPRITE_BULLET_HEIGHT, + SPRITE_BULLET_OFFSET_X, + SPRITE_BULLET_OFFSET_Y ); - - bullet.offsetX = 1; - bullet.offsetY = 1; } struct MouseStatus mouseStatus; @@ -204,7 +207,7 @@ void renderMouse() { mouse.x = rabbitPosition.mousePosition[0]; mouse.y = rabbitPosition.mousePosition[1]; - drawSprite(&mouse); + drawCompiledSprite(&mouse); drawPixel(rabbitPosition.mouseDotPosition[0], rabbitPosition.mouseDotPosition[1], 2); } @@ -212,7 +215,7 @@ void renderRabbit() { rabbit.x = rabbitPosition.rabbitPosition[0]; rabbit.y = rabbitPosition.rabbitPosition[1]; - drawSprite(&rabbit); + drawCompiledSprite(&rabbit); } void renderEnemies() { @@ -224,7 +227,7 @@ void renderEnemies() { enemy.x = enemyPosition[i].enemyPosition[0]; enemy.y = enemyPosition[i].enemyPosition[1]; - drawSprite(&enemy); + drawCompiledSprite(&enemy); } } @@ -237,7 +240,7 @@ void renderRabbitBullets() { bullet.x = rabbitBulletPosition[i].x; bullet.y = rabbitBulletPosition[i].y; - drawSprite(&bullet); + drawCompiledSprite(&bullet); } } diff --git a/makefile b/makefile index 32aab2c..41c5bac 100644 --- a/makefile +++ b/makefile @@ -13,6 +13,9 @@ system/system.lib: .SYMBOLIC .asm.o: wasm $< +sprites.asm: sprtsht.bmp spritesheet.yml + bin/build_spritesheet_asm.rb + game.exe: $(obj) system/system.lib wcl386 -q -bt=dos -l=dos4g $(obj) system/system.lib diff --git a/sprites.asm b/sprites.asm index 79140ea..6c271c5 100644 --- a/sprites.asm +++ b/sprites.asm @@ -1251,12 +1251,12 @@ sprite_rabbit_: mov BYTE PTR [eax + -1921], 1 mov BYTE PTR [eax + -1920], 1 mov BYTE PTR [eax + -1602], 1 - mov BYTE PTR [eax + -1601], 1 - mov BYTE PTR [eax + -1600], 1 + mov BYTE PTR [eax + -1601], 2 + mov BYTE PTR [eax + -1600], 2 mov BYTE PTR [eax + -1599], 1 mov BYTE PTR [eax + -1282], 1 - mov BYTE PTR [eax + -1281], 1 - mov BYTE PTR [eax + -1280], 1 + mov BYTE PTR [eax + -1281], 2 + mov BYTE PTR [eax + -1280], 2 mov BYTE PTR [eax + -1279], 1 mov BYTE PTR [eax + -961], 1 mov BYTE PTR [eax + -960], 1 @@ -1351,12 +1351,12 @@ sprite_enemy_: mov BYTE PTR [eax + -1921], 1 mov BYTE PTR [eax + -1920], 1 mov BYTE PTR [eax + -1602], 1 - mov BYTE PTR [eax + -1601], 1 - mov BYTE PTR [eax + -1600], 1 + mov BYTE PTR [eax + -1601], 2 + mov BYTE PTR [eax + -1600], 2 mov BYTE PTR [eax + -1599], 1 mov BYTE PTR [eax + -1282], 1 - mov BYTE PTR [eax + -1281], 1 - mov BYTE PTR [eax + -1280], 1 + mov BYTE PTR [eax + -1281], 2 + mov BYTE PTR [eax + -1280], 2 mov BYTE PTR [eax + -1279], 1 mov BYTE PTR [eax + -961], 1 mov BYTE PTR [eax + -960], 1 diff --git a/sprites.h b/sprites.h index 4beed32..b88bef1 100644 --- a/sprites.h +++ b/sprites.h @@ -6,19 +6,80 @@ extern void sprite_arenaWallTop(byte *); +#define SPRITE_ARENAWALLTOP_WIDTH (20) + +#define SPRITE_ARENAWALLTOP_HEIGHT (20) + +#define SPRITE_ARENAWALLTOP_OFFSET_X (0) + +#define SPRITE_ARENAWALLTOP_OFFSET_Y (0) + + extern void sprite_arenaWallSide(byte *); +#define SPRITE_ARENAWALLSIDE_WIDTH (20) + +#define SPRITE_ARENAWALLSIDE_HEIGHT (20) + +#define SPRITE_ARENAWALLSIDE_OFFSET_X (0) + +#define SPRITE_ARENAWALLSIDE_OFFSET_Y (0) + + extern void sprite_arenaFloor(byte *); +#define SPRITE_ARENAFLOOR_WIDTH (20) + +#define SPRITE_ARENAFLOOR_HEIGHT (20) + +#define SPRITE_ARENAFLOOR_OFFSET_X (0) + +#define SPRITE_ARENAFLOOR_OFFSET_Y (0) + + extern void sprite_rabbit(byte *); +#define SPRITE_RABBIT_WIDTH (16) + +#define SPRITE_RABBIT_HEIGHT (16) + +#define SPRITE_RABBIT_OFFSET_X (8) + +#define SPRITE_RABBIT_OFFSET_Y (8) + + extern void sprite_mouse(byte *); +#define SPRITE_MOUSE_WIDTH (8) + +#define SPRITE_MOUSE_HEIGHT (8) + +#define SPRITE_MOUSE_OFFSET_X (4) + +#define SPRITE_MOUSE_OFFSET_Y (4) + + extern void sprite_bullet(byte *); +#define SPRITE_BULLET_WIDTH (4) + +#define SPRITE_BULLET_HEIGHT (4) + +#define SPRITE_BULLET_OFFSET_X (1) + +#define SPRITE_BULLET_OFFSET_Y (1) + + extern void sprite_enemy(byte *); +#define SPRITE_ENEMY_WIDTH (16) + +#define SPRITE_ENEMY_HEIGHT (16) + +#define SPRITE_ENEMY_OFFSET_X (8) + +#define SPRITE_ENEMY_OFFSET_Y (8) + -// TODO: something with bounds checking here #endif diff --git a/spritesheet.xcf b/spritesheet.xcf index 36ea740775653cca6a028df013cfe0fe65a2d585..c2b3fb2fd274f3636593e0135e2e0b6e9f8c376f 100644 GIT binary patch delta 182 zcmaFN`I2+O3`XXSGkuvjFEW4u%iYQEndIxILwU2I^n5727)mdP(yO8LdMLdaN^ghK zyP@=cD18`8ABWPXq4aqO&A=ecB+M+#GP##|neZP*CMHH!21Zt9Al797aVI;lECv7u C&>0c{ delta 176 zcmaFK`IvLU3`VAnGkuvjPcwi4%k|0cndIvyLU~i6^h_u{7fLUL(o3QAN+`V+N^gYH yTcPw$D7_a-AB56Jq4Y@z&A=ecB+M+#GP##|880&gZ;f?oWaMrKby} delta 28 fcmeC%#niWpX+v)(6C>l~{?20%>gZ;f?oWaMrEd$N diff --git a/system/vga.c b/system/vga.c index 1ff251c..c34e54b 100644 --- a/system/vga.c +++ b/system/vga.c @@ -54,11 +54,15 @@ void buildCompiledSprite( void (*code)(byte *), struct CompiledSpriteRender *compiledSpriteRender, int width, - int height + int height, + int offsetX, + int offsetY ) { compiledSpriteRender->code = code; compiledSpriteRender->width = width; compiledSpriteRender->height = height; + compiledSpriteRender->offsetX = offsetX; + compiledSpriteRender->offsetY = offsetY; } void drawPixel(int x, int y, int color) { @@ -101,7 +105,7 @@ void drawCompiledSprite(struct CompiledSpriteRender* compiledSprite) { compiledSprite->code(drawBufferPos); } -void getSpriteBounds(struct SpriteRender *sprite, struct SpriteBounds *bounds) { +void getSpriteBounds(struct CompiledSpriteRender *sprite, struct SpriteBounds *bounds) { bounds->top = sprite->y - sprite->offsetY; bounds->bottom = bounds->top + sprite->height; diff --git a/system/vga.h b/system/vga.h index 0619c4e..e2bb1ab 100644 --- a/system/vga.h +++ b/system/vga.h @@ -34,6 +34,8 @@ struct CompiledSpriteRender { int y; unsigned int width; unsigned int height; + int offsetX; + int offsetY; }; struct SpriteBounds { @@ -48,8 +50,13 @@ void drawPixel(int x, int y, int color); void drawSprite(struct SpriteRender *sprite); void drawCompiledSprite(struct CompiledSpriteRender *compiledSprite); void buildSpriteFromSpritesheet(struct BMPImage*, struct SpriteRender*, int, int, int, int); -void buildCompiledSprite(void (*)(byte *), struct CompiledSpriteRender*, int width, int height); -void getSpriteBounds(struct SpriteRender *sprite, struct SpriteBounds *bounds); +void buildCompiledSprite( + void (*)(byte *), + struct CompiledSpriteRender*, + int width, int height, + int offsetX, int offsetY +); +void getSpriteBounds(struct CompiledSpriteRender *sprite, struct SpriteBounds *bounds); byte *initializeDrawBuffer(); byte *getDrawBuffer();