Archived
1
0
Disbranĉigi 0

Improve menu-test, add lib/button abstraction

This commit is contained in:
Jaidyn Ann 2020-06-02 22:56:43 -05:00
parent f117b2c5c8
commit 74966f7877
10 changed files with 160 additions and 82 deletions

View File

@ -3,8 +3,8 @@
# multiple-targets like sparrow3D! # multiple-targets like sparrow3D!
# whoops. # whoops.
SRC = lib/font.c lib/sound.c lib/menu.c SRC = lib/button.c lib/font.c lib/sound.c lib/menu.c
OBJ = font.o sound.o menu.o OBJ = button.o font.o sound.o menu.o
DEP = $(SRC:.c=.d) DEP = $(SRC:.c=.d)
DYNAMIC = -lSDL_ttf -lSDL_mixer -lSDL_image -lSDL -lm DYNAMIC = -lSDL_ttf -lSDL_mixer -lSDL_image -lSDL -lm

53
lib/button.c Normal file
View File

@ -0,0 +1,53 @@
#include <sparrow3d.h>
#include "button.h"
int buttonSteps[20] = { 0 };
// incrementing each spGetInput()->button[] would be sick but they overflow
// too quick
// will return the amount of steps a button has been held down consecutively
// if unpressed, returns 0.
int
getButton ( int button, Uint32 steps )
{
if ( spGetInput()->button[button] ) {
if ( buttonSteps[button] == 0 )
buttonSteps[button]++;
else
buttonSteps[button] += steps;
return buttonSteps[button];
}
else
buttonSteps[button] = 0;
return 0;
}
// acts like getButton, except it returns 0 (unpressed) if the consective steps
// the button has been held down for isn't within the given range.
// can also reset the button's step counter after it meets the range, or
// completely disable the current button-press after it meets the range.
// pass -1 as min_steps or max_steps to disable that part of the range-check.
// pass 0 as disable/reset to disable resetting/disabling, 1 to enable.
// I recommend 1,0,1 for init_no_min,disable,reset
int
getButtonLim
( int button, Uint32 steps, Uint32 min_steps, Uint32 max_steps, int init_no_min,
int disable, int reset )
{
int b_steps = getButton( button, steps );
int range_pass = 0;
if ( (min_steps < b_steps && (b_steps < max_steps || max_steps == -1)) )
range_pass = 1;
if ( init_no_min && b_steps ==1 )
range_pass = 1;
if ( disable && range_pass )
spGetInput()->button[button] = 0;
if ( (reset || disable) && range_pass )
buttonSteps[button] = 1;
if ( range_pass )
return b_steps;
return 0;
}

6
lib/button.h Normal file
View File

@ -0,0 +1,6 @@
#include <sparrow3d.h>
int buttonSteps[20];
int getButton ( int, Uint32 );
int getButtonLim ( int, Uint32, Uint32, Uint32, int, int, int );

View File

@ -10,26 +10,26 @@ char fontPaths[10][512];
// ============================================================================ // ============================================================================
// add a TTF font at given path to the global `font` array // add a TTF font at given path to the global `font` array
void void
add_font ( char ttf_path[512] ) addFont ( char ttf_path[512] )
{ {
strcpy(fontPaths[fontCount], ttf_path); strcpy(fontPaths[fontCount], ttf_path);
fontCount++; fontCount++;
resize_fonts(); resizeFonts();
} }
// ============================================================================ // ============================================================================
// resizes/updates all fonts; should be run whenever window resizes // resizes/updates all fonts; should be run whenever window resizes
void void
resize_fonts ( void ) resizeFonts ( void )
{ {
for ( int i = 0; i < fontCount; i++ ) { for ( int i = 0; i < fontCount; i++ ) {
resize_font(&fonts[i], fontPaths[i]); resizeFont(&fonts[i], fontPaths[i]);
} }
} }
// resizes/updates a given font // resizes/updates a given font
void void
resize_font ( spFontPointer* font, char ttf_path[512] ) resizeFont ( spFontPointer* font, char ttf_path[512] )
{ {
spFontShadeButtons(1); spFontShadeButtons(1);
@ -54,7 +54,7 @@ resize_font ( spFontPointer* font, char ttf_path[512] )
// ============================================================================ // ============================================================================
// cleans up all fonts in global array `fonts` // cleans up all fonts in global array `fonts`
void void
cleanup_fonts ( void ) cleanupFonts ( void )
{ {
for ( int i = 0; i < fontCount; i++ ) { for ( int i = 0; i < fontCount; i++ ) {
spFontDelete( fonts[i] ); spFontDelete( fonts[i] );

View File

@ -5,9 +5,9 @@ spFontPointer fonts[10];
char fontPaths[10][512]; char fontPaths[10][512];
void add_font ( char ttf_path[512] ); void addFont ( char ttf_path[512] );
void resize_fonts ( void ); void resizeFonts ( void );
void resize_font ( spFontPointer*, char[512] ); void resizeFont ( spFontPointer*, char[512] );
void cleanup_fonts ( void ); void cleanupFonts ( void );

View File

@ -1,5 +1,6 @@
#include <sparrow3d.h> #include <sparrow3d.h>
#include <string.h> #include <string.h>
#include "button.h"
#include "menu.h" #include "menu.h"
// ============================================================================ // ============================================================================
@ -7,7 +8,7 @@
// ============================================================================ // ============================================================================
// init a menu with the given title text. // init a menu with the given title text.
void void
init_menu initMenu
( struct eMenu* menu, char title[512], ( struct eMenu* menu, char title[512],
spFontPointer* titleFont, spFontPointer* elementFont, spFontPointer* titleFont, spFontPointer* elementFont,
int menuBackgroundColor, int selectBackgroundColor ) int menuBackgroundColor, int selectBackgroundColor )
@ -26,6 +27,12 @@ init_menu
menu->selection = 0; menu->selection = 0;
menu->elementCount = 0; menu->elementCount = 0;
menu->inputDelayStep = 100;
menu->inputCutoffStep = -1;
menu->inputImmediate = 1;
menu->inputDisable = 0;
menu->inputReset = 1;
menu->menuBgColor = menuBackgroundColor; menu->menuBgColor = menuBackgroundColor;
menu->selectBgColor = selectBackgroundColor; menu->selectBgColor = selectBackgroundColor;
@ -36,7 +43,7 @@ init_menu
// init the given eMenuElement with the given text in an eMenu. // init the given eMenuElement with the given text in an eMenu.
void void
init_menu_element initMenuElement
( struct eMenu* menu, struct eMenuElement* element, char text[512] ) ( struct eMenu* menu, struct eMenuElement* element, char text[512] )
{ {
int count = menu->elementCount; int count = menu->elementCount;
@ -50,22 +57,22 @@ init_menu_element
// create a new eMenuElement with given text, and append to eMenu. // create a new eMenuElement with given text, and append to eMenu.
void void
add_menu_element addMenuElement
( struct eMenu* menu, char text[512] ) ( struct eMenu* menu, char text[512] )
{ {
struct eMenuElement* element = malloc(sizeof(struct eMenuElement)); struct eMenuElement* element = malloc(sizeof(struct eMenuElement));
init_menu_element( menu, element, text ); initMenuElement( menu, element, text );
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// resize a menu (executed by resize loop) // resize a menu (executed by resize loop)
void void
resize_menu ( struct eMenu* menu ) resizeMenu ( struct eMenu* menu )
{ {
if ( menu->height == 0 ) if ( menu->height == 0 )
menu->height = ideal_menu_height(menu); menu->height = idealMenuHeight(menu);
if ( menu->width == 0 ) if ( menu->width == 0 )
menu->width = ideal_menu_width(menu); menu->width = idealMenuWidth(menu);
menu->scaleFactor = spFixedToInt(spGetSizeFactor()); menu->scaleFactor = spFixedToInt(spGetSizeFactor());
int height = menu->height * menu->scaleFactor; int height = menu->height * menu->scaleFactor;
@ -84,19 +91,20 @@ resize_menu ( struct eMenu* menu )
// ============================================================================ // ============================================================================
// draw an eMenu on it's surface. // draw an eMenu on it's surface.
void void
draw_menu ( SDL_Surface* surface, struct eMenu* menu ) drawMenu ( SDL_Surface* surface, struct eMenu* menu )
{ {
SDL_Surface* old_surface = spGetRenderTarget( ); SDL_Surface* old_surface = spGetRenderTarget( );
spSelectRenderTarget( menu->surface ); spSelectRenderTarget( menu->surface );
spClearTarget( menu->menuBgColor ); spClearTarget( menu->menuBgColor );
if ( menu->textCentered == 0 ) if ( menu->textCentered == 0 )
spFontDrawMiddle ( menu->width / 2 * menu->scaleFactor,0,0, menu->title, *(menu->font) ); spFontDrawMiddle( menu->width / 2 * menu->scaleFactor,0,0,
menu->title, *(menu->font) );
else else
spFontDraw( 0,0,0, menu->title, *(menu->font) ); spFontDraw( 0,0,0, menu->title, *(menu->font) );
for ( int i=0; i < menu->elementCount; i++ ) { for ( int i=0; i < menu->elementCount; i++ ) {
draw_menu_element( menu->elements[i]); drawMenuElement( menu->elements[i]);
} }
spSelectRenderTarget( old_surface ); spSelectRenderTarget( old_surface );
@ -104,7 +112,7 @@ draw_menu ( SDL_Surface* surface, struct eMenu* menu )
// draw an eMenuElement on current target surface // draw an eMenuElement on current target surface
void void
draw_menu_element ( struct eMenuElement* element ) drawMenuElement ( struct eMenuElement* element )
{ {
struct eMenu* parent = element->parentMenu; struct eMenu* parent = element->parentMenu;
int index = element->index; int index = element->index;
@ -123,7 +131,8 @@ draw_menu_element ( struct eMenuElement* element )
else index++; else index++;
if ( parent->textCentered == 0 ) if ( parent->textCentered == 0 )
spFontDrawMiddle(parent->width / 2 * parent->scaleFactor,index * fontHeight,0, spFontDrawMiddle( parent->width / 2 * parent->scaleFactor,
index * fontHeight,0,
element->text, *(parent->elementFont)); element->text, *(parent->elementFont));
else else
spFontDraw( 20,index * fontHeight,0, spFontDraw( 20,index * fontHeight,0,
@ -139,24 +148,28 @@ draw_menu_element ( struct eMenuElement* element )
// 0 if not buttons were pressed, // 0 if not buttons were pressed,
// otherwise return the item selected + 1. // otherwise return the item selected + 1.
int int
calc_menu ( struct eMenu* menu, int sp_select_button ) calcMenu ( struct eMenu* menu, int sp_select_button, Uint32 steps )
{ {
spSleep(50000); if ( getButton( sp_select_button, steps ) )
if ( spGetInput()->button[sp_select_button] ) return menuSelect( menu, steps );
return menu_select( menu ); else if ( getButtonLim( SP_BUTTON_UP, steps,
else if ( spGetInput()->button[SP_BUTTON_DOWN] ) menu->inputDelayStep,menu->inputCutoffStep, menu->inputImmediate,
menu_select_down( menu ); menu->inputDisable, menu->inputReset ) )
else if ( spGetInput()->button[SP_BUTTON_UP] ) menuSelectUp( menu, steps );
menu_select_up( menu ); else if ( getButtonLim( SP_BUTTON_DOWN, steps,
menu->inputDelayStep,menu->inputCutoffStep, menu->inputImmediate,
menu->inputDisable, menu->inputReset ) )
menuSelectDown( menu, steps );
else else
return CALCMENU_NOINPUT; return CALCMENU_NOINPUT;
return CALCMENU_MOVED; return CALCMENU_MOVED;
} }
// ------------------------------------------------------------------- // -------------------------------------------------------------------
// returns selected menu item, and plays proper sound // returns selected menu item, and plays proper sound
int int
menu_select ( struct eMenu* menu ) menuSelect ( struct eMenu* menu, Uint32 steps )
{ {
if ( menu->moveSound != NULL ) if ( menu->moveSound != NULL )
spSoundPlay( menu->selectSound, menu->soundChannel,0,0,-1 ); spSoundPlay( menu->selectSound, menu->soundChannel,0,0,-1 );
@ -165,7 +178,7 @@ menu_select ( struct eMenu* menu )
// move an eMenu's selected element up by one (if possible) // move an eMenu's selected element up by one (if possible)
void void
menu_select_up ( struct eMenu* menu ) menuSelectUp ( struct eMenu* menu, Uint32 steps )
{ {
if ( 0 < menu->selection ) { if ( 0 < menu->selection ) {
menu->selection -= 1; menu->selection -= 1;
@ -176,7 +189,7 @@ menu_select_up ( struct eMenu* menu )
// move an eMenu's selected element down by one (if possible) // move an eMenu's selected element down by one (if possible)
void void
menu_select_down ( struct eMenu* menu ) menuSelectDown ( struct eMenu* menu, Uint32 steps )
{ {
if ( (menu->elementCount - 1) > menu->selection ) { if ( (menu->elementCount - 1) > menu->selection ) {
menu->selection += 1; menu->selection += 1;
@ -191,7 +204,7 @@ menu_select_down ( struct eMenu* menu )
// ============================================================================ // ============================================================================
// return menu's ideal (smallest while displaying everything) calculated height // return menu's ideal (smallest while displaying everything) calculated height
int int
ideal_menu_height ( struct eMenu* menu ) idealMenuHeight ( struct eMenu* menu )
{ {
spFontPointer font = *(menu->font); spFontPointer font = *(menu->font);
spFontPointer elementFont = *(menu->elementFont); spFontPointer elementFont = *(menu->elementFont);
@ -203,7 +216,7 @@ ideal_menu_height ( struct eMenu* menu )
// return menu's ideal (smallest while displaying everything) calculated width // return menu's ideal (smallest while displaying everything) calculated width
int int
ideal_menu_width ( struct eMenu* menu ) idealMenuWidth ( struct eMenu* menu )
{ {
struct eMenuElement* element; struct eMenuElement* element;
int top_len = spFontWidth( menu->title, *(menu->font) ); int top_len = spFontWidth( menu->title, *(menu->font) );

View File

@ -28,6 +28,12 @@ struct eMenu
int titleCentered; int titleCentered;
int textCentered; int textCentered;
int inputDelayStep;
int inputCutoffStep;
int inputDisable;
int inputReset;
int inputImmediate;
int menuBgColor; int menuBgColor;
int selectBgColor; int selectBgColor;
@ -42,21 +48,21 @@ struct eMenu
}; };
void init_menu ( struct eMenu*, char title[512], void initMenu ( struct eMenu*, char title[512],
spFontPointer*, spFontPointer*, int, int ); spFontPointer*, spFontPointer*, int, int );
void init_menu_element ( struct eMenu*, struct eMenuElement*, char[512] ); void initMenuElement ( struct eMenu*, struct eMenuElement*, char[512] );
void add_menu_element ( struct eMenu*, char[512] ); void addMenuElement ( struct eMenu*, char[512] );
void draw_menu ( SDL_Surface*, struct eMenu* ); void drawMenu ( SDL_Surface*, struct eMenu* );
void draw_menu_element ( struct eMenuElement* ); void drawMenuElement ( struct eMenuElement* );
int calc_menu ( struct eMenu*, int ); int calcMenu ( struct eMenu*, int, Uint32 );
void resize_menu ( struct eMenu* ); void resizeMenu ( struct eMenu* );
int menu_select ( struct eMenu* ); int menuSelect ( struct eMenu*, Uint32 );
void menu_select_up ( struct eMenu* ); void menuSelectUp ( struct eMenu*, Uint32 );
void menu_select_down ( struct eMenu* ); void menuSelectDown ( struct eMenu*, Uint32 );
int ideal_menu_height ( struct eMenu* ); int idealMenuHeight ( struct eMenu* );
int ideal_menu_width ( struct eMenu* ); int idealMenuWidth ( struct eMenu* );

View File

@ -7,7 +7,7 @@ spSound* sounds[10] = { NULL };
// add a sound to the global sounds array // add a sound to the global sounds array
void void
add_sound ( char soundpath[512] ) addSound ( char soundpath[512] )
{ {
sounds[soundCount] = spSoundLoad(soundpath); sounds[soundCount] = spSoundLoad(soundpath);
soundCount++; soundCount++;
@ -15,7 +15,7 @@ add_sound ( char soundpath[512] )
// cleanup all sounds // cleanup all sounds
void void
cleanup_sounds ( void ) cleanupSounds ( void )
{ {
for ( int i = 0; i < soundCount; i++ ) { for ( int i = 0; i < soundCount; i++ ) {
spSoundDelete( sounds[i] ); spSoundDelete( sounds[i] );

View File

@ -3,5 +3,5 @@
int soundCount; int soundCount;
spSound* sounds[10]; spSound* sounds[10];
void add_sound ( char[512] ); void addSound ( char[512] );
void cleanup_sounds ( void ); void cleanupSounds ( void );

View File

@ -41,7 +41,7 @@ draw_test ( void )
spClearTarget( spGetRGB(64,64,64) ); spClearTarget( spGetRGB(64,64,64) );
spSelectRenderTarget(spGetWindowSurface()); spSelectRenderTarget(spGetWindowSurface());
draw_menu ( testMenu->surface, testMenu ); drawMenu ( testMenu->surface, testMenu );
spBlitSurface( spGetWindowSurface()->w/2, spGetWindowSurface()->h/2, 0, spBlitSurface( spGetWindowSurface()->w/2, spGetWindowSurface()->h/2, 0,
testMenu->surface ); testMenu->surface );
@ -57,18 +57,18 @@ draw_test ( void )
} }
// input for the menu test // input for the menu test
// if non-zero return int, it's the index of the selected menu item + 1.
int int
calc_test ( Uint32 steps ) calc_test ( Uint32 steps )
{ {
int menucode = calc_menu ( testMenu, SP_BUTTON_START ); int menucode = calcMenu( testMenu, SP_BUTTON_START, steps );
// no input? interpret keypress as our own // no input? interpret keypress as our own
if ( menucode == CALCMENU_NOINPUT ) if ( menucode == CALCMENU_NOINPUT ) {
if ( spGetInput()->button[SP_BUTTON_SELECT] ) if ( spGetInput()->button[SP_BUTTON_SELECT] )
return -1; return -1;
else
return 0; return 0;
}
// greater than _moved means it returned the inc'd menu selection index // greater than _MOVED means it returned the inc'd menu selection index
if ( menucode > CALCMENU_MOVED ) if ( menucode > CALCMENU_MOVED )
spSleep(700000); spSleep(700000);
// sleep long enough after selection to play selection sound lol // sleep long enough after selection to play selection sound lol
@ -79,8 +79,8 @@ void
resize ( Uint16 w, Uint16 h ) resize ( Uint16 w, Uint16 h )
{ {
spSelectRenderTarget( spGetWindowSurface()); spSelectRenderTarget( spGetWindowSurface());
resize_fonts(); resizeFonts();
resize_menu(testMenu); resizeMenu( testMenu );
} }
@ -100,26 +100,26 @@ init ( void )
void void
init_test_menu ( struct eMenu* menu ) init_test_menu ( struct eMenu* menu )
{ {
init_menu( menu, "what do you do? <3", initMenu( menu, "what do you do? <3",
&fonts[FN_LOVEMECHAIN], &fonts[FN_COZETTE], &fonts[FN_LOVEMECHAIN], &fonts[FN_COZETTE],
spGetRGB(80,80,80), spGetRGB(35,71,107) ); spGetRGB(80,80,80), spGetRGB(35,71,107) );
menu->titleCentered = 0; menu->titleCentered = 0;
menu->textCentered = 0; menu->textCentered = 0;
menu->moveSound = sounds[SF_TAMBORINE]; menu->moveSound = sounds[SF_TAMBORINE];
menu->selectSound = sounds[SF_EEUH]; menu->selectSound = sounds[SF_EEUH];
add_menu_element(menu, "give up"); addMenuElement(menu, "give up");
add_menu_element(menu, "run away"); addMenuElement(menu, "run away");
add_menu_element(menu, "eat a baby"); addMenuElement(menu, "eat a baby");
add_menu_element(menu, "wave hello"); addMenuElement(menu, "wave hello");
add_menu_element(menu, "hug a stranger until they blush"); addMenuElement(menu, "hug a stranger until they blush");
add_menu_element(menu, "oh well"); addMenuElement(menu, "oh well");
} }
void void
init_fonts ( void ) init_fonts ( void )
{ {
add_font("data/font/cozette-vector.ttf"); addFont("data/font/cozette-vector.ttf");
add_font("data/font/love_me_chain.ttf"); addFont("data/font/love_me_chain.ttf");
} }
void void
@ -136,8 +136,8 @@ void
init_sounds ( void ) init_sounds ( void )
{ {
spSoundInit(); spSoundInit();
add_sound("data/sound/tamborine.ogg"); addSound("data/sound/tamborine.ogg");
add_sound("data/sound/eeuh.ogg"); addSound("data/sound/eeuh.ogg");
} }
@ -147,8 +147,8 @@ init_sounds ( void )
int int
cleanup ( void ) cleanup ( void )
{ {
cleanup_fonts(); cleanupFonts();
cleanup_sounds(); cleanupSounds();
spDeleteSurface( menuSurface ); spDeleteSurface( menuSurface );
spQuitCore(); spQuitCore();
} }