Skip to content

Commit

Permalink
improved movement, camera follows focus, and more
Browse files Browse the repository at this point in the history
  • Loading branch information
natearn committed Feb 23, 2012
1 parent 2d4772a commit 02a9fe2
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 46 deletions.
3 changes: 1 addition & 2 deletions TODO
@@ -1,7 +1,6 @@
in order of importance: in order of importance:


- improve sprite action routines and animation table/attributes - decide how affects with durations will be handled
- decide how affects with durations will be handled
- decide if sprite updating and sprite rendering need to be separated - decide if sprite updating and sprite rendering need to be separated
- create a keyboard input state machine - create a keyboard input state machine
- create a field (sensor shape) which applies a force or velocity to the sprite body; - create a field (sensor shape) which applies a force or velocity to the sprite body;
Expand Down
4 changes: 3 additions & 1 deletion gamestate.c
Expand Up @@ -49,11 +49,13 @@ int AddSprite( struct GameState *game, Sprite *sprite, cpVect posn ) {
int RenderGameState( struct GameState *game, SDL_Surface *screen, Uint32 delta ) { int RenderGameState( struct GameState *game, SDL_Surface *screen, Uint32 delta ) {
assert( game && screen ); assert( game && screen );
struct SpriteList *list = NULL; struct SpriteList *list = NULL;
/* TODO: replace hard-coded offset */
cpVect screen_posn = cpvadd( cpBodyGetPos(game->focus->body), cpv(-320.0, -240.0) );
/* render the map */ /* render the map */
SDL_FillRect( screen, NULL, 0x0 ); /* temporary: fill screen black */ SDL_FillRect( screen, NULL, 0x0 ); /* temporary: fill screen black */
/* render the sprites */ /* render the sprites */
for(list = game->sprites; list; list = list->next) { for(list = game->sprites; list; list = list->next) {
if( DrawSprite( list->sprite, screen, delta )) { if( DrawSprite( list->sprite, screen, screen_posn, delta )) {
return -1; return -1;
} }
} }
Expand Down
67 changes: 44 additions & 23 deletions newgame.c
Expand Up @@ -102,30 +102,51 @@ Uint32 CalcWaitTime( Uint32 target, Uint32 delay, Uint32 min ) {
void HandleInput( unsigned int keys[], struct GameState *game, SDL_Event *event ) { void HandleInput( unsigned int keys[], struct GameState *game, SDL_Event *event ) {
assert( event->type == SDL_KEYDOWN || event->type == SDL_KEYUP ); assert( event->type == SDL_KEYDOWN || event->type == SDL_KEYUP );
int x, y; int x, y;
switch( event->key.keysym.sym ) { if( event->type == SDL_KEYDOWN ) {
case SDLK_LEFT: switch( event->key.keysym.sym ) {
keys[2] = (event->type == SDL_KEYDOWN); case SDLK_a:
y = keys[1] - keys[0]; return;
x = keys[3] - keys[2]; case SDLK_s:
break; return;
case SDLK_RIGHT: case SDLK_d:
keys[3] = (event->type == SDL_KEYDOWN); return;
y = keys[1] - keys[0]; case SDLK_f:
x = keys[3] - keys[2]; return;
break; case SDLK_LEFT:
case SDLK_UP: keys[2] = 1;
keys[0] = (event->type == SDL_KEYDOWN); break;
y = keys[1] - keys[0]; case SDLK_RIGHT:
x = keys[3] - keys[2]; keys[3] = 1;
break; break;
case SDLK_DOWN: case SDLK_UP:
keys[1] = (event->type == SDL_KEYDOWN); keys[0] = 1;
y = keys[1] - keys[0]; break;
x = keys[3] - keys[2]; case SDLK_DOWN:
break; keys[1] = 1;
default: break;
break; default:
return;
}
} else {
switch( event->key.keysym.sym ) {
case SDLK_LEFT:
keys[2] = 0;
break;
case SDLK_RIGHT:
keys[3] = 0;
break;
case SDLK_UP:
keys[0] = 0;
break;
case SDLK_DOWN:
keys[1] = 0;
break;
default:
return;
}
} }
y = keys[1] - keys[0];
x = keys[3] - keys[2];
switch(x) { switch(x) {
case -1: case -1:
switch(y) { switch(y) {
Expand Down
16 changes: 9 additions & 7 deletions sprite.c
Expand Up @@ -26,7 +26,7 @@ Sprite *InitSprite( Sprite *sprite, struct Resource *resource ) {
/* TODO: replace hard-coded constants with parameters */ /* TODO: replace hard-coded constants with parameters */
sprite->control = cpBodyNew( INFINITY, INFINITY ); sprite->control = cpBodyNew( INFINITY, INFINITY );
sprite->body = cpBodyNew( 10, INFINITY ); sprite->body = cpBodyNew( 10, INFINITY );
sprite->shape = cpCircleShapeNew( sprite->body, 12, cpvzero ); sprite->shape = cpCircleShapeNew( sprite->body, 10, cpvzero );
//cpShapeSetElasticity(sprite->shape, 0.0f); //cpShapeSetElasticity(sprite->shape, 0.0f);
sprite->shape->e = 0.0f; sprite->shape->e = 0.0f;
//cpShapeSetFriction(sprite->shape, 0.7f); //cpShapeSetFriction(sprite->shape, 0.7f);
Expand All @@ -36,9 +36,12 @@ Sprite *InitSprite( Sprite *sprite, struct Resource *resource ) {
sprite->pivot->maxBias = 0; sprite->pivot->maxBias = 0;
//cpConstraintSetMaxForce(sprite->pivot, 10000.0f); // emulate linear friction //cpConstraintSetMaxForce(sprite->pivot, 10000.0f); // emulate linear friction
sprite->pivot->maxForce = 1000000.0; sprite->pivot->maxForce = 1000000.0;

for( size_t i=0; i < NUM_ATTR; i++ ) { for( size_t i=0; i < NUM_ATTR; i++ ) {
sprite->attributes[i] = 0; sprite->attributes[i] = 0;
} }
sprite->facing = NONE;
sprite->moving = NONE;
return sprite; return sprite;
} }


Expand Down Expand Up @@ -83,11 +86,10 @@ Uint32 UpdateSprite( Sprite *sprite, Uint32 time ) {
} }


/* this needs to be separated */ /* this needs to be separated */
int DrawSprite( Sprite *sprite, SDL_Surface *surface, Uint32 delta ) { int DrawSprite( Sprite *sprite, SDL_Surface *surface, cpVect screen_posn, Uint32 delta ) {
assert(sprite && surface); assert(sprite && surface);
SDL_Rect *frame; SDL_Rect *frame;
SDL_Rect posn = {0,0,0,0}; SDL_Rect posn = {0,0,0,0};
cpVect offset;


/* determine current animation */ /* determine current animation */
if( sprite->animation != sprite->table[sprite->attributes[ATTR_FACE]][sprite->attributes[ATTR_MOVE]] ) { if( sprite->animation != sprite->table[sprite->attributes[ATTR_FACE]][sprite->attributes[ATTR_MOVE]] ) {
Expand All @@ -100,10 +102,10 @@ int DrawSprite( Sprite *sprite, SDL_Surface *surface, Uint32 delta ) {
sprite->time += delta; sprite->time += delta;
frame = GetUpdatedFrame( sprite->animation, &sprite->index, &sprite->time ); frame = GetUpdatedFrame( sprite->animation, &sprite->index, &sprite->time );


/* get the sprite position */ /* get the sprite screen position */
/* TODO: calculated offset to center of grav */ posn = Vect2Rect( cpvsub( cpBodyGetPos(sprite->body), screen_posn ));
offset = cpvzero; posn.y -= frame->h;
if( sprite->body ) posn = Vect2Rect( cpvadd( offset, cpBodyGetPos( sprite->body ))); posn.x -= frame->w/2;


/* blit */ /* blit */
if( SDL_BlitSurface( sprite->resource->surface, frame, surface, &posn )) { if( SDL_BlitSurface( sprite->resource->surface, frame, surface, &posn )) {
Expand Down
27 changes: 14 additions & 13 deletions sprite.h
Expand Up @@ -6,18 +6,17 @@
#include "animation.h" #include "animation.h"
#include "resource.h" #include "resource.h"


typedef enum { #define NONE 0
NONE = 0, #define UP 1
UP, #define DOWN 2
DOWN, #define LEFT 3
LEFT, #define RIGHT 4
RIGHT, #define UP_RIGHT 5
UP_RIGHT, #define UP_LEFT 6
UP_LEFT, #define DOWN_RIGHT 7
DOWN_RIGHT, #define DOWN_LEFT 8
DOWN_LEFT, #define NUM_DIRECTION 9
NUM_DIRECTION = 8 typedef unsigned int Direction;
} Direction;


#define FACE_LEFT 0 #define FACE_LEFT 0
#define FACE_RIGHT 1 #define FACE_RIGHT 1
Expand All @@ -43,6 +42,8 @@ typedef struct {


/* game data */ /* game data */
unsigned int attributes[NUM_ATTR]; /* collection of state enums used by the animation table */ unsigned int attributes[NUM_ATTR]; /* collection of state enums used by the animation table */
Direction facing;
Direction moving;


/* animation data */ /* animation data */
Animation *table[NUM_FACE][NUM_MOVE]; /* look-up table */ Animation *table[NUM_FACE][NUM_MOVE]; /* look-up table */
Expand Down Expand Up @@ -73,7 +74,7 @@ void FreeSprite( Sprite *sprite );
Uint32 UpdateSprite( Sprite *sprite, Uint32 time ); Uint32 UpdateSprite( Sprite *sprite, Uint32 time );


/* draw the sprite on the surface */ /* draw the sprite on the surface */
int DrawSprite( Sprite *sprite, SDL_Surface *surface, Uint32 delta ); int DrawSprite( Sprite *sprite, SDL_Surface *surface, cpVect screen_posn, Uint32 delta );


/* get methods (hiding the implementation) */ /* get methods (hiding the implementation) */
cpBody *GetSpriteBody( Sprite *sprite ); cpBody *GetSpriteBody( Sprite *sprite );
Expand Down

0 comments on commit 02a9fe2

Please sign in to comment.