forked from theZiz/sparrow3d
/
testsprite.c
142 lines (124 loc) · 3.86 KB
/
testsprite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
The contents of this file are subject to the "do whatever you like"-license.
That means: Do, whatver you want, this file is under public domain. It is an
example for sparrow3d. Copy it and learn from it for your project and release
it under every license you want. ;-)
For feedback and questions about my Files and Projects please mail me,
Alexander Matthes (Ziz) , zizsdl_at_googlemail.com
*/
#include "../sparrow3d/sparrow3d.h"
#ifdef SDL_INCLUDE_SUBDIR
#include <SDL/SDL_image.h>
#else
#include <SDL_image.h>
#endif
SDL_Surface *screen;
SDL_Surface *tile_map;
spSpritePointer sprite;
Sint32 rotation = 0;
void draw_function( void )
{
//Cleaning the target (in this case the screen surface) with 0 (black).
//In this case I don't clean the zBuffer, because I don't use it. ;-)
//But you ARE able to use it, if you want!
spResetZBuffer();
spClearTarget( 34567 );
//Set the origin:
switch ( ( rotation >> 17 ) % 9 )
{
case 0:
spSetHorizontalOrigin( SP_CENTER );
spSetVerticalOrigin( SP_CENTER );
break;
case 1:
spSetHorizontalOrigin( SP_LEFT );
spSetVerticalOrigin( SP_CENTER );
break;
case 2:
spSetHorizontalOrigin( SP_LEFT );
spSetVerticalOrigin( SP_TOP );
break;
case 3:
spSetHorizontalOrigin( SP_CENTER );
spSetVerticalOrigin( SP_TOP );
break;
case 4:
spSetHorizontalOrigin( SP_RIGHT );
spSetVerticalOrigin( SP_TOP );
break;
case 5:
spSetHorizontalOrigin( SP_RIGHT );
spSetVerticalOrigin( SP_CENTER );
break;
case 6:
spSetHorizontalOrigin( SP_RIGHT );
spSetVerticalOrigin( SP_BOTTOM );
break;
case 7:
spSetHorizontalOrigin( SP_CENTER );
spSetVerticalOrigin( SP_BOTTOM );
break;
case 8:
spSetHorizontalOrigin( SP_LEFT );
spSetVerticalOrigin( SP_BOTTOM );
break;
}
//Drawing the sprite in the middle of the screen
spDrawSprite( screen->w / 2, screen->h / 2, -1, sprite );
spDrawSprite( screen->w - 1, screen->h - 1, -1, sprite );
spDrawSprite( screen->w - 1, 0, -1, sprite );
spDrawSprite( 0, screen->h - 1, -1, sprite );
spDrawSprite( 0, 0, -1, sprite );
//mark the center
//spEllipseBorder( screen->w / 2, screen->h / 2, -100, 60+spSin(rotation/2)/1000, 60+spCos(rotation/3)/1000, 20+spCos(rotation/7)/10000, 20+spSin(rotation/5)/10000, 0 );
spEllipseBorder( screen->w / 2, screen->h / 2, -100, 6, 6, 2, 2, 0 );
//Show it!
spFlip();
}
int calc_function( Uint32 steps )
{
//update rotation counter
rotation += steps * 32;
//if "Start" (or "Menu" for Wiz or "Home" for Caanoo or "Return" at the P) is
//pressed: exit
if ( spGetInput()->button[SP_BUTTON_START] )
return 1;
//Update the sprite (next picture)
spUpdateSprite( sprite, steps );
//rotozoom the sprite (without rotozoom it's faster):
sprite->zoomX = spSin( rotation * 8 ) / 2 + ( 3 << SP_ACCURACY - 1 );
sprite->zoomY = spCos( rotation * 6 ) / 2 + ( 3 << SP_ACCURACY - 1 );
sprite->rotation = rotation * 4;
return 0;
}
int main( int argc, char **argv )
{
//sparrow3D Init
spSetDefaultWindowSize( 640, 480 ); //Creates a 640x480 window at PC instead of 320x240
spInitCore();
//Setup
screen = spCreateDefaultWindow();
spSelectRenderTarget(screen);
//Tile map loading
tile_map = spLoadSurface( "./data/science_guy_frames01.png" );
//Creating an empty sprite
sprite = spNewSprite();
//Filling it with it subsprites.
int i;
for ( i = 0; i < 9; i++ )
spNewSubSpriteWithTiling( sprite, tile_map, i * 24 + 1, 1, 22, 46, 100 );
//We don't want to use the zBuffer in any way
spSetZSet(0);
spSetZTest(0);
//All glory the main loop
//every frame the draw_function is called
//every frame the calc_function is called with the past time as argument
//at least 10 ms have to be between to frames (max 100 fps)
//if the window is resized, the resize feedback-function is called (again)
spLoop( draw_function, calc_function, 10, NULL, NULL );
//Winter Wrap up, Winter Wrap up
spDeleteSprite( sprite );
spDeleteSurface( tile_map );
spQuitCore();
return 0;
}