first commit

This commit is contained in:
cellos51
2025-01-31 17:42:09 -05:00
commit cac321a325
15 changed files with 756 additions and 0 deletions
+2
View File
@@ -0,0 +1,2 @@
/.vscode
/build
+189
View File
@@ -0,0 +1,189 @@
#---------------------------------------------------------------------------------
.SUFFIXES:
#---------------------------------------------------------------------------------
ifeq ($(strip $(DEVKITARM)),)
$(error "Please set DEVKITARM in your environment. export DEVKITARM=<path to>devkitARM")
endif
include $(DEVKITARM)/gba_rules
#---------------------------------------------------------------------------------
# the LIBGBA path is defined in gba_rules, but we have to define LIBTONC ourselves
#---------------------------------------------------------------------------------
LIBTONC := $(DEVKITPRO)/libtonc
#---------------------------------------------------------------------------------
# TARGET is the name of the output
# BUILD is the directory where object files & intermediate files will be placed
# SOURCES is a list of directories containing source code
# INCLUDES is a list of directories containing extra header files
# DATA is a list of directories containing binary data
# GRAPHICS is a list of directories containing files to be processed by grit
#
# All directories are specified relative to the project directory where
# the makefile is found
#
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := source
INCLUDES := include
DATA :=
MUSIC := audio
GRAPHICS := graphics
#---------------------------------------------------------------------------------
# options for code generation
#---------------------------------------------------------------------------------
ARCH := -mthumb -mthumb-interwork
CFLAGS := -g -Wall -O2\
-mcpu=arm7tdmi -mtune=arm7tdmi\
$(ARCH)
CFLAGS += $(INCLUDE)
CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions
ASFLAGS := -g $(ARCH)
LDFLAGS = -g $(ARCH) -Wl,-Map,$(notdir $*.map)
#---------------------------------------------------------------------------------
# any extra libraries we wish to link with the project
#---------------------------------------------------------------------------------
LIBS := -lmm -ltonc
#---------------------------------------------------------------------------------
# list of directories containing libraries, this must be the top level containing
# include and lib.
# the LIBGBA path should remain in this list if you want to use maxmod
#---------------------------------------------------------------------------------
LIBDIRS := $(LIBGBA) $(LIBTONC)
#---------------------------------------------------------------------------------
# no real need to edit anything past this point unless you need to add additional
# rules for different file extensions
#---------------------------------------------------------------------------------
ifneq ($(BUILD),$(notdir $(CURDIR)))
#---------------------------------------------------------------------------------
export OUTPUT := $(CURDIR)/$(BUILD)/$(TARGET)
export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir)) \
$(foreach dir,$(GRAPHICS),$(CURDIR)/$(dir))
export DEPSDIR := $(CURDIR)/$(BUILD)
CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s)))
PNGFILES := $(foreach dir,$(GRAPHICS),$(notdir $(wildcard $(dir)/*.png)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*)))
ifneq ($(strip $(MUSIC)),)
export AUDIOFILES := $(foreach dir,$(notdir $(wildcard $(MUSIC)/*.*)),$(CURDIR)/$(MUSIC)/$(dir))
BINFILES += soundbank.bin
endif
#---------------------------------------------------------------------------------
# use CXX for linking C++ projects, CC for standard C
#---------------------------------------------------------------------------------
ifeq ($(strip $(CPPFILES)),)
#---------------------------------------------------------------------------------
export LD := $(CC)
#---------------------------------------------------------------------------------
else
#---------------------------------------------------------------------------------
export LD := $(CXX)
#---------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------
export OFILES_BIN := $(addsuffix .o,$(BINFILES))
export OFILES_SOURCES := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
export OFILES_GRAPHICS := $(PNGFILES:.png=.o)
export OFILES := $(OFILES_BIN) $(OFILES_SOURCES) $(OFILES_GRAPHICS)
export HFILES := $(addsuffix .h,$(subst .,_,$(BINFILES))) $(PNGFILES:.png=.h)
export INCLUDE := $(foreach dir,$(INCLUDES),-iquote $(CURDIR)/$(dir)) \
$(foreach dir,$(LIBDIRS),-I$(dir)/include) \
-I$(CURDIR)/$(BUILD)
export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
.PHONY: $(BUILD) clean
#---------------------------------------------------------------------------------
$(BUILD):
@[ -d $@ ] || mkdir -p $@
@$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
#---------------------------------------------------------------------------------
clean:
@echo clean ...
@rm -fr $(BUILD) $(TARGET).elf $(TARGET).gba
#---------------------------------------------------------------------------------
all: $(BUILD)
else
#---------------------------------------------------------------------------------
# main targets
#---------------------------------------------------------------------------------
$(OUTPUT).gba : $(OUTPUT).elf
$(OUTPUT).elf : $(OFILES)
$(OFILES_SOURCES) : $(HFILES)
#---------------------------------------------------------------------------------
# The bin2o rule should be copied and modified
# for each extension used in the data directories
#---------------------------------------------------------------------------------
#---------------------------------------------------------------------------------
# rule to build soundbank from music files
#---------------------------------------------------------------------------------
soundbank.bin soundbank.h : $(AUDIOFILES)
#---------------------------------------------------------------------------------
@mmutil $^ -osoundbank.bin -hsoundbank.h
#---------------------------------------------------------------------------------
# This rule links in binary data with the .bin extension
#---------------------------------------------------------------------------------
%.bin.o %_bin.h : %.bin
#---------------------------------------------------------------------------------
@echo $(notdir $<)
@$(bin2o)
#---------------------------------------------------------------------------------
# This rule creates assembly source files using grit
# grit takes an image file and a .grit describing how the file is to be processed
# add additional rules like this for each image extension
# you use in the graphics folders
#---------------------------------------------------------------------------------
%.s %.h: %.png %.grit
#---------------------------------------------------------------------------------
@echo "grit $(notdir $<)"
@grit $< -fts -o$*
# make likes to delete intermediate files. This prevents it from deleting the
# files generated by grit after building the GBA ROM.
.SECONDARY:
-include $(DEPSDIR)/*.d
#---------------------------------------------------------------------------------------
endif
#---------------------------------------------------------------------------------------
BIN
View File
Binary file not shown.
Binary file not shown.
+1
View File
@@ -0,0 +1 @@
-gB8 -mR8 -mLs -mRtf
Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

+1
View File
@@ -0,0 +1 @@
-gB4 -Mw4 -Mh4
Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

+62
View File
@@ -0,0 +1,62 @@
#ifndef CARD_H
#define CARD_H
#include <tonc.h>
#include "sprite.h"
// Card suits
#define HEARTS 0
#define DIAMONDS 1
#define CLUBS 2
#define SPADES 2
// Card ranks
#define TWO 0
#define THREE 1
#define FOUR 2
#define FIVE 3
#define SIX 4
#define SEVEN 5
#define EIGHT 6
#define NINE 7
#define TEN 8
#define JACK 9
#define QUEEN 10
#define KING 11
#define ACE 12
typedef struct
{
u8 suit;
u8 rank;
} Card;
typedef struct
{
Card *card;
Sprite *sprite;
FIXED x, y; // position
FIXED vx, vy; // velocity
bool selected;
} CardObject;
// Card methods
Card *card_new(u8 suit, u8 rank);
void card_destroy(Card **card);
// CardObject methods
CardObject *card_object_new(Card *card);
void card_object_destroy(CardObject **card_object);
// Card functions
void card_init();
void card_update();
void card_draw();
// Hand functions
void hand_index(int index); // This makes the card at the given index the one that's being looked at
void hand_select(); // This lets the player select the card they're looking at for the next action
void hand_change_sort(); // This sorts the hand by suit or rank
#endif // CARD_H
+8
View File
@@ -0,0 +1,8 @@
#ifndef GAME_H
#define GAME_H
// Game functions
void game_init();
void game_update();
#endif // GAME_H
+30
View File
@@ -0,0 +1,30 @@
#ifndef SPRITE_H
#define SPRITE_H
#include <tonc.h>
typedef struct
{
OBJ_ATTR *obj;
OBJ_AFFINE *aff;
u32 tid, pb;
POINT pos;
} Sprite;
// Sprite methods
Sprite *sprite_new(u16 a0, u16 a1, u32 tid, u32 pb, int sprite_index);
Sprite *affine_sprite_new(u16 a0, u16 a1, u32 tid, u32 pb);
void sprite_destroy(Sprite **sprite);
INLINE void sprite_position(Sprite *sprite, int x, int y)
{
sprite->pos.x = x;
sprite->pos.y = y;
obj_set_pos(sprite->obj, x, y);
}
// Sprite functions
void sprite_init();
void sprite_draw();
#endif // SPRITE_H
+254
View File
@@ -0,0 +1,254 @@
#include "card.h"
#include <maxmod.h>
#include <stdlib.h>
// Audio
#include "soundbank.h"
#define MAX_HAND_SIZE 16
#define MAX_DECK_SIZE 52
// Card sprites lookup table. First index is the suit, second index is the rank. The value is the tile index.
const static u16 card_sprite_lut[4][13] = {
{0, 16, 32, 48, 64, 80, 96, 112, 128, 144, 160, 176, 192},
{208, 224, 240, 256, 272, 288, 304, 320, 336, 352, 368, 384, 400},
{416, 432, 448, 464, 480, 496, 512, 528, 544, 560, 576, 592, 608},
{624, 640, 656, 672, 688, 704, 720, 736, 752, 768, 784, 800, 816}
};
static CardObject *hand[MAX_HAND_SIZE] = {NULL};
static Card *deck[MAX_DECK_SIZE] = {NULL};
static int deck_top = -1;
static int hand_top = -1;
static int hand_size = 8;
static int card_focused = 0;
static bool sort_by_suit = false;
// Sorting functions
static inline void sort_cards()
{
if (sort_by_suit)
{
for (int a = 0; a < hand_top; a++)
{
for (int b = a + 1; b <= hand_top; b++)
{
if (hand[a] == NULL || (hand[b] != NULL && (hand[a]->card->suit > hand[b]->card->suit || (hand[a]->card->suit == hand[b]->card->suit && hand[a]->card->rank > hand[b]->card->rank))))
{
CardObject *temp = hand[a];
hand[a] = hand[b];
hand[b] = temp;
}
}
}
}
else
{
for (int a = 0; a < hand_top; a++)
{
for (int b = a + 1; b <= hand_top; b++)
{
if (hand[a] == NULL || (hand[b] != NULL && hand[a]->card->rank > hand[b]->card->rank))
{
CardObject *temp = hand[a];
hand[a] = hand[b];
hand[b] = temp;
}
}
}
}
// Update the sprites in the hand by destroying them and creating new ones in the correct order.
// (This is feels like a diabolical solution but like literally how else would you do this)
for (int i = 0; i <= hand_top; i++)
{
if (hand[i] != NULL)
{
sprite_destroy(&hand[i]->sprite);
}
}
for (int i = 0; i <= hand_top; i++)
{
if (hand[i] != NULL)
{
hand[i]->sprite = sprite_new(ATTR0_SQUARE | ATTR0_4BPP | ATTR0_AFF, ATTR1_SIZE_32, card_sprite_lut[hand[i]->card->suit][hand[i]->card->rank], 0, i);
}
}
}
// Deck private functions
void deck_push(Card *card)
{
if (deck_top >= MAX_DECK_SIZE - 1) return;
deck[++deck_top] = card;
}
Card *deck_pop()
{
if (deck_top < 0) return NULL;
return deck[deck_top--];
}
// Card methods
Card *card_new(u8 suit, u8 rank)
{
Card *card = malloc(sizeof(Card));
card->suit = suit;
card->rank = rank;
return card;
}
void card_destroy(Card **card)
{
if (*card == NULL) return;
free(*card);
*card = NULL;
}
// CardObject methods
CardObject *card_object_new(Card *card)
{
CardObject *card_object = malloc(sizeof(CardObject));
card_object->card = card;
card_object->sprite = NULL;
card_object->x = 0;
card_object->y = 0;
card_object->vx = 0;
card_object->vy = 0;
card_object->selected = false;
return card_object;
}
void card_object_destroy(CardObject **card_object)
{
if (*card_object == NULL) return;
card_destroy(&(*card_object)->card);
sprite_destroy(&(*card_object)->sprite);
free(*card_object);
*card_object = NULL;
}
void card_draw()
{
if (deck_top < 0 || hand_top >= hand_size - 1 || hand_top >= MAX_HAND_SIZE - 1) return;
CardObject *card_object = card_object_new(deck_pop());
static const FIXED deck_x = 208 << FIX_SHIFT;
static const FIXED deck_y = 110 << FIX_SHIFT;
card_object->x = deck_x;
card_object->y = deck_y;
hand[++hand_top] = card_object;
// Sort the hand after drawing a card
sort_cards();
static mm_sound_effect sfx_draw = {{ SFX_DRAW }, (int)(1.0f * (1<<10)), 0, 255, 128,};
mmEffectEx(&sfx_draw);
}
// Card functions
void card_init()
{
// Fill the deck with all the cards
for (int suit = 0; suit < 4; suit++)
{
for (int rank = 0; rank < 13; rank++)
{
Card *card = card_new(suit, rank);
deck_push(card);
}
}
// Shuffle the deck
for (int i = 0; i < MAX_DECK_SIZE; i++)
{
int j = rand() % MAX_DECK_SIZE;
Card *temp = deck[i];
deck[i] = deck[j];
deck[j] = temp;
}
}
void card_update() // This whole function is currently pretty unoptimized due to the fixed point math, but that can be fixed later with a LUT
{
for (int i = 0; i < MAX_HAND_SIZE; i++)
{
if (hand[i] != NULL)
{
static const int spacing_lut[MAX_HAND_SIZE] = {28, 28, 28, 28, 27, 21, 18, 15, 13, 12, 10, 9, 9, 8, 8, 7}; // This is a stupid way to do this but I don't care
static const FIXED hand_x = 120 << FIX_SHIFT;
static const FIXED hand_y = 90 << FIX_SHIFT;
FIXED hand_y_offset = hand_y;
if (i == card_focused && !hand[i]->selected)
{
hand_y_offset -= (10 << FIX_SHIFT);
}
else if (i != card_focused && hand[i]->selected)
{
hand_y_offset -= (15 << FIX_SHIFT);
}
else if (i == card_focused && hand[i]->selected)
{
hand_y_offset -= (20 << FIX_SHIFT);
}
hand[i]->vx += (hand_x + (int2fx(i) - int2fx(hand_top) / 2) * -spacing_lut[hand_top] - hand[i]->x) / 8; // TODO: Change this later to reference a 2D LUT of positions
hand[i]->vy += (hand_y_offset - hand[i]->y) / 8;
// set velocity to 0 if it's close enough to the target
if (hand[i]->vx < float2fx(0.01f) && hand[i]->vx > float2fx(-0.01f) && hand[i]->vy < float2fx(0.01f) && hand[i]->vy > float2fx(-0.01f))
{
hand[i]->vx = 0;
hand[i]->vy = 0;
hand[i]->x = hand_x + (int2fx(i) - int2fx(hand_top) / 2) * -spacing_lut[hand_top];
hand[i]->y = hand_y_offset;
}
else
{
hand[i]->vx = (hand[i]->vx * 7) / 10;
hand[i]->vy = (hand[i]->vy * 7) / 10;
hand[i]->x += hand[i]->vx;
hand[i]->y += hand[i]->vy;
}
obj_aff_rotate(hand[i]->sprite->aff, -hand[i]->vx);
sprite_position(hand[i]->sprite, fx2int(hand[i]->x), fx2int(hand[i]->y));
}
}
}
// Hand functions
void hand_index(int index)
{
if (index < 0 || index > hand_top) return;
card_focused = index;
}
void hand_select()
{
if (hand[card_focused] == NULL) return;
hand[card_focused]->selected = !hand[card_focused]->selected;
}
void hand_change_sort()
{
sort_by_suit = !sort_by_suit;
sort_cards();
}
+46
View File
@@ -0,0 +1,46 @@
#include "game.h"
#include <maxmod.h>
#include <tonc.h>
#include <string.h>
#include "sprite.h"
#include "card.h"
void game_init()
{
}
void game_update()
{
static int timer = 0;
timer++;
if (timer % 12 == 0)
{
card_draw();
}
static int hand_index_selected = 0;
if (key_hit(KEY_LEFT))
{
hand_index_selected++;
hand_index(hand_index_selected);
}
if (key_hit(KEY_RIGHT))
{
hand_index_selected--;
hand_index(hand_index_selected);
}
if (key_hit(KEY_A))
{
hand_select();
}
if (key_hit(KEY_SELECT))
{
hand_change_sort();
}
}
+70
View File
@@ -0,0 +1,70 @@
#include <tonc.h>
#include <maxmod.h>
#include <string.h>
#include "sprite.h"
#include "card.h"
#include "game.h"
// Graphics
#include "deck_gfx.h"
#include "background_gfx.h"
// Audio
#include "soundbank.h"
#include "soundbank_bin.h"
void init()
{
// Initialize Maxmod
irq_init(NULL);
irq_add(II_VBLANK, mmVBlank);
mmInitDefault((mm_addr)soundbank_bin, 8);
mmStart(MOD_MAIN_THEME, MM_PLAY_LOOP);
// Set up the video mode
REG_DISPCNT = DCNT_MODE0 | DCNT_BG0 | DCNT_OBJ_1D | DCNT_OBJ;
REG_BG0CNT = BG_CBB(0) | BG_SBB(30) | BG_8BPP | BG_REG_64x32;
// Load the tiles and palette
memcpy(pal_bg_mem, background_gfxPal, background_gfxPalLen);
memcpy(&tile_mem[0][0], background_gfxTiles, background_gfxTilesLen);
memcpy32(&se_mem[30][0], background_gfxMap, background_gfxMapLen);
memcpy(&tile_mem[4][0], deck_gfxTiles, deck_gfxTilesLen);
memcpy(pal_obj_mem, deck_gfxPal, deck_gfxPalLen);
// Initialize subsystems
sprite_init();
card_init();
game_init();
}
void update()
{
game_update();
card_update();
}
void draw()
{
sprite_draw();
}
int main()
{
init();
while(true)
{
vid_vsync();
mmFrame();
key_poll();
update();
draw();
}
return 0;
}
+93
View File
@@ -0,0 +1,93 @@
#include "sprite.h"
#include <tonc.h>
#include <stdlib.h>
#define MAX_SPRITES 128
#define MAX_AFFINES 32
OBJ_ATTR obj_buffer[MAX_SPRITES];
OBJ_AFFINE *obj_aff_buffer = (OBJ_AFFINE*)obj_buffer;
static Sprite *free_sprites[MAX_SPRITES] = {NULL};
static bool free_affines[MAX_AFFINES] = {false};
// Sprite methods
Sprite *sprite_new(u16 a0, u16 a1, u32 tid, u32 pb, int sprite_index)
{
Sprite *sprite = malloc(sizeof(Sprite));
sprite->obj = NULL;
sprite->aff = NULL;
if(!free_sprites[sprite_index])
{
free_sprites[sprite_index] = sprite;
}
else
{
free(sprite);
return NULL;
}
if (a0 & ATTR0_AFF)
{
int aff_index = MAX_AFFINES;
for(int i = 0; i < MAX_AFFINES; i++)
{
if(!free_affines[i])
{
free_affines[i] = true;
aff_index = i;
break;
}
}
if (aff_index == MAX_AFFINES)
{
free(sprite);
return NULL;
}
a1 = a1 | ATTR1_AFF_ID(aff_index);
sprite->obj = &obj_buffer[sprite_index];
sprite->aff = &obj_aff_buffer[aff_index];
obj_set_attr(sprite->obj, a0, a1, ATTR2_PALBANK(pb) | tid);
obj_aff_identity(&obj_aff_buffer[aff_index]);
return sprite;
}
else
{
sprite->obj = &obj_buffer[sprite_index];
obj_set_attr(sprite->obj, a0, a1, ATTR2_PALBANK(pb) | tid);
return sprite;
}
}
void sprite_destroy(Sprite **sprite)
{
if (*sprite == NULL) return;
obj_hide((*sprite)->obj);
free_sprites[(*sprite)->obj - obj_buffer] = NULL;
if ((*sprite)->aff != NULL)
{
free_affines[(*sprite)->aff - obj_aff_buffer] = false;
}
free(*sprite);
*sprite = NULL;
}
// Sprite functions
void sprite_init()
{
oam_init(obj_buffer, MAX_SPRITES);
}
void sprite_draw()
{
obj_copy(obj_mem, obj_buffer, MAX_SPRITES);
obj_aff_copy(obj_aff_mem, obj_aff_buffer, MAX_AFFINES);
oam_copy(oam_mem, obj_buffer, MAX_SPRITES);
}