Archived
1
0
Disbranĉigi 0

Add sprite-rendering test

This commit is contained in:
Jaidyn Ann 2020-06-03 01:05:18 -05:00
parent 74966f7877
commit dc39771307
6 changed files with 239 additions and 1 deletions

View File

@ -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:

BIN
data/sprite/scientist.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

89
data/sprite/scientist.ssc Normal file
View File

@ -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

View File

@ -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,

117
tests/test-sprites.c Normal file
View File

@ -0,0 +1,117 @@
#include <sparrow3d.h>
#include <string.h>
#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();
}

26
tests/test-sprites.h Normal file
View File

@ -0,0 +1,26 @@
#include <sparrow3d.h>
#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 );