From dc39771307d5d8a191f05a66a4783d4504ec9a6e Mon Sep 17 00:00:00 2001 From: Jaidyn Ann Date: Wed, 3 Jun 2020 01:05:18 -0500 Subject: [PATCH] Add sprite-rendering test --- Makefile | 6 +- data/sprite/scientist.png | Bin 0 -> 6062 bytes data/sprite/scientist.ssc | 89 +++++++++++++++++++++++++++++ lib/font.c | 2 + tests/test-sprites.c | 117 ++++++++++++++++++++++++++++++++++++++ tests/test-sprites.h | 26 +++++++++ 6 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 data/sprite/scientist.png create mode 100644 data/sprite/scientist.ssc create mode 100644 tests/test-sprites.c create mode 100644 tests/test-sprites.h diff --git a/Makefile b/Makefile index aa470a2..23cb860 100644 --- a/Makefile +++ b/Makefile @@ -54,10 +54,13 @@ all: tests targets: @echo "The targets are the same like for sparrow3d. :P" +test-sprites: tests/test-sprites.c $(OBJ) makeBuildDir + $(CC) $(CFLAGS) $(LINK_FLAGS) $< $(OBJ) $(SDL) $(INCLUDE) $(LIB) $(EFEMMERA_STATIC) $(DYNAMIC) -o $(BUILD)/tests/$@$(SUFFIX) + test-menu: tests/test-menu.c $(OBJ) makeBuildDir $(CC) $(CFLAGS) $(LINK_FLAGS) $< $(OBJ) $(SDL) $(INCLUDE) $(LIB) $(EFEMMERA_STATIC) $(DYNAMIC) -o $(BUILD)/tests/$@$(SUFFIX) -tests: test-menu +tests: test-menu test-sprites @echo "=== Tests built ===" #efemmera: main.c $(OBJ) makeBuildDir #$(CC) $(CFLAGS) $(LINK_FLAGS) $< $(OBJ) $(SDL) $(INCLUDE) $(LIB) $(EFEMMERA_STATIC) $(DYNAMIC) -o $(BUILD)/$@$(SUFFIX) @@ -76,6 +79,7 @@ clean: rm -f *.o *.d rm -f lib/*.d lib/*.o rm -f $(BUILD)/tests/test-menu$(SUFFIX) + rm -f $(BUILD)/tests/test-sprites$(SUFFIX) rm -f $(BUILD)/efemmera$(SUFFIX) oclean: diff --git a/data/sprite/scientist.png b/data/sprite/scientist.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^R}y^C zbh0jT=(VO(M*bnpbmar0ZcN`9F2F3Ca;g1jV0OOT%xjtE^o|WflOTuu_W}$d)*`X< zKi8g(3!!BiA)koqud4kHKgV^)o>h7)tP-6%8S}@A*C*)8YwJsJX8r=+9`GuusnAS~ zxRbn0W-za%B@$BWC2e9_2lu+>SfOcE55#c{+{Js`kK@2JMDMAbiAF{c)T}wmMmgI` zekv!mBWuDDR$-711BlrAL3!~oESlz(tR4G`SX#Ye3u5+|iIx*H=26S|pn;&3v@uQfuP ze>85%VkT%m+glV~$EE53_Y1TXPfM018K6@!Wk}PjnA>vlQ~9#sX4F9S-V$7qowK!+ z^Hl!pcc)POoto-BR-`R!uFr^ZorhgW3KMt}%lAuwGB;R>dbB^4>mO@y(>(*M>65KK z75!M^>cJ_tm6Umdb`*8)C3A5>Kyjl|G;J~X(vnq2PlL3Y>W<>CFCL&oU8PjDhblTQL)1WV zEm|pFz92MKfPu?!PQAG;9lB7*Umhy*WFJ|gge%9!en5T(Z_f?;u_u>nbW&IKwOBsF zUJgkeU4ZgSQ&!K=W!1tmB!0tK6L=5{T0)TP0vW5xuc1dg2%<e*{tj A&Hw-a literal 0 HcmV?d00001 diff --git a/data/sprite/scientist.ssc b/data/sprite/scientist.ssc new file mode 100644 index 0000000..7876203 --- /dev/null +++ b/data/sprite/scientist.ssc @@ -0,0 +1,89 @@ +# ssc files (sparrow sprite collection) are like other ini-files. +# The file format is line based (every line one command). +# comments begin with # - like this one - or with ; +# every section (started with [ and ended with ]) is one sprite. The +# name of the section is between [ and ] +# Every key before the first section is for global keys. Every key +# after the first section is a local key for the last defined sprite. +# A key looks like this "keyname = keyvalue". The count of space between +# doesn't matter. Only the name, the = and the value of the key are +# important. + +# first some global defines. + +# the default sprite is the first defined or this one defined here: + +author = Ziz +license = CC-BY-SA + +default = run right + +# if no image in the section is defined, this shall be the default. The +# image is, where the parts are used from + +image = ./data/sprite/scientist.png + +# if no fps in the section is defined, this shall be the default. +# The fps say, how long a sprite-frame is seeable. E.g. with fps = 24 +# every frame is 1000/24 ~ 42 ms seeable (internal it is always rounded +# down, so it would be 41 ms per frame). + +fps = 10 + +# if no framesize in the section is defined, this shall be the default. +# The framesize is, how much you see, when you draw the sprite. + +framesize = 22, 46 + +# if no bordersize in the section is defined, this shall be the default. +# The bordersize is, how much is jumped, if you define multiply frames +# at once (sprite sheet). Furthermore the sparrow3d rotozoom function is +# a bit inaccurate. That means: a colorless border (pink is alpha color) +# may prevent you from dirty artifacts. +# The frame (with it framesize) is centered in the border! + +bordersize = 24, 48 + +# Defining a new sprite: + +[stand right] + +# frame defines the frame(s) of the sprite. It can be just one frame +# like here, multiply calls of frame or a third parameter as count of +# the frames, which are loaded at once (a whole spritesheet at once). +# If there is just one frame, obviously the fps doesn't matter. +# The first both parameters are the x and y position of the border (!) +# of the frame in the image. + +frame = 0, 0 + +[stand left] +frame = 0, 96 + +[run right] + +# the default fps is used + +frame = 0, 0, 8 + +[run left] +frame = 0, 96, 8 + +[push right] + +# the default fps (10) is to "fast" for the push animation, so we define +# a own fps + +fps = 8 +frame = 24, 48, 6 + +[push left] +fps = 8 +frame = 24, 144, 6 + +[jump right] +frame = 0, 48 + +[jump left] +frame = 0, 144, 1 + diff --git a/lib/font.c b/lib/font.c index c027f1b..29dcc3f 100644 --- a/lib/font.c +++ b/lib/font.c @@ -45,6 +45,8 @@ resizeFont ( spFontPointer* font, char ttf_path[512] ) spFontAddBorder( *(font), 0 ); //Have a look, how [S] in line 21 is drawn as a Button image. + spFontAddButton( *(font), 'A', SP_BUTTON_A_NAME, 65535, + spGetRGB( 64, 64, 64 ) ); spFontAddButton( *(font), 'E', SP_BUTTON_SELECT_NAME, 65535, spGetRGB( 64, 64, 64 ) ); spFontAddButton( *(font), 'S', SP_BUTTON_START_NAME, 65535, diff --git a/tests/test-sprites.c b/tests/test-sprites.c new file mode 100644 index 0000000..2ddf51e --- /dev/null +++ b/tests/test-sprites.c @@ -0,0 +1,117 @@ +#include +#include +#include "../lib/font.h" +#include "test-sprites.h" + +SDL_Surface* screen = NULL; +SDL_Surface* sprites_texture = NULL; + +spSpritePointer sprite; +spSpriteCollectionPointer collection; + +// ============================================================================ + +int +main ( int argc, char **argv ) +{ + init(); + spLoop( draw_test, calc_test, 10, resize, NULL ); + + cleanup(); + return 0; +} + +// ============================================================================ +// TEST +// ============================================================================ +// draw this whole sprite test (aka, simple background, text, and the sprites.) +void +draw_test ( void ) +{ + spClearTarget( spGetRGB(64,64,64) ); + spSelectRenderTarget(spGetWindowSurface()); + + screen = spGetWindowSurface(); + sprite->zoomX *= -1; + sprite->zoomX = sprite->zoomY = spGetSizeFactor() ; + spDrawSprite( 4 * screen->w / 5, 5 * screen->h / 8, 0, sprite ); + + draw_hud( ); + + spFlip(); +} + +void +draw_hud ( ) +{ + spFontDrawMiddle( spGetWindowSurface()->w/2, + spGetWindowSurface()->h + - fonts[FN_COZETTE]->maxheight -2, 0, + "Press [E] to exit", fonts[FN_COZETTE] ); +} + +// input for the sprite test +int +calc_test ( Uint32 steps ) +{ + spUpdateSprite( sprite, steps ); + if ( spGetInput()->button[SP_BUTTON_SELECT] ) + return -1; + return 0; +} + +void +resize ( Uint16 w, Uint16 h ) +{ + spSelectRenderTarget( spGetWindowSurface()); + resizeFonts(); +} + + +// ============================================================================ +// INIT +// ============================================================================ +int +init ( void ) +{ + init_sparrow3d(); + init_fonts(); + init_sprites(); + resize( spGetWindowSurface()->w, spGetWindowSurface()->h ); +} + +void +init_fonts ( void ) +{ + addFont("data/font/cozette-vector.ttf"); + addFont("data/font/love_me_chain.ttf"); +} + +void +init_sprites ( void ) +{ + sprites_texture = spLoadSurface( "data/sprite/scientist.png" ); + collection = spLoadSpriteCollection("data/sprite/scientist.ssc",sprites_texture); + sprite = spActiveSprite(collection); +} + +void +init_sparrow3d ( void ) +{ + spSetDefaultWindowSize( 320, 240 ); + spInitCore(); + spCreateDefaultWindow(); + spSetZSet(0); + spSetZTest(0); +} + + +// ============================================================================ +// CLEANUP +// ============================================================================ +int +cleanup ( void ) +{ + cleanupFonts(); + spQuitCore(); +} diff --git a/tests/test-sprites.h b/tests/test-sprites.h new file mode 100644 index 0000000..6b51b94 --- /dev/null +++ b/tests/test-sprites.h @@ -0,0 +1,26 @@ +#include + +#define FN_COZETTE 0 +#define FN_LOVEMECHAIN 1 +#define SF_TAMBORINE 0 +#define SF_EEUH 1 + +SDL_Surface* screen; +SDL_Surface* sprites; + +spSpritePointer sprite; +spSpriteCollectionPointer collection; + +int calc_test ( Uint32 ); +void draw_test ( void ); +void draw_hud ( void ); +void resize ( Uint16, Uint16 ); + +int init ( void ); + +void init_sprites ( void ); +void init_fonts ( void ); +void init_sounds ( void ); +void init_sparrow3d ( void ); + +int cleanup ( void );