Refactor sprite tween logic out openrct2.c

This commit is contained in:
Ted John 2016-12-11 00:08:37 +00:00
parent e9bafc2973
commit 0019827d6c
6 changed files with 113 additions and 77 deletions

View File

@ -38,6 +38,8 @@
#include "version.h"
#include "world/mapgen.h"
#define UPDATE_TIME_MS 25 // (1000 / 40fps) = 25ms
int gExitCode;
int gOpenRCT2StartupAction = STARTUP_ACTION_TITLE;
@ -62,9 +64,6 @@ EVP_MD_CTX *gHashCTX = NULL;
/** If set, will end the OpenRCT2 game loop. Intentially private to this module so that the flag can not be set back to 0. */
int _finished;
// Used for object movement tweening
static rct_xyz16 _spritelocations1[MAX_SPRITES], _spritelocations2[MAX_SPRITES];
static void openrct2_loop();
void openrct2_write_full_version_info(utf8 *buffer, size_t bufferSize)
@ -343,20 +342,6 @@ void openrct2_dispose()
platform_free();
}
/**
* Determines whether its worth tweening a sprite or not when frame smoothing is on.
*/
static bool sprite_should_tween(rct_sprite *sprite)
{
switch (sprite->unknown.linked_list_type_offset >> 1) {
case SPRITE_LIST_VEHICLE:
case SPRITE_LIST_PEEP:
case SPRITE_LIST_UNKNOWN:
return true;
}
return false;
}
/**
* Run the main game loop until the finished flag is set at 40fps (25ms interval).
*/
@ -377,44 +362,33 @@ static void openrct2_loop()
if (uncapTick == 0) {
// Reset sprite locations
uncapTick = SDL_GetTicks();
openrct2_reset_object_tween_locations();
sprite_position_tween_reset();
}
// Limit number of updates per loop (any long pauses or debugging can make this update for a very long time)
if (currentTick - uncapTick > 25 * 60) {
uncapTick = currentTick - 25 - 1;
if (currentTick - uncapTick > UPDATE_TIME_MS * 60) {
uncapTick = currentTick - UPDATE_TIME_MS - 1;
}
platform_process_messages();
while (uncapTick <= currentTick && currentTick - uncapTick > 25) {
while (uncapTick <= currentTick && currentTick - uncapTick > UPDATE_TIME_MS) {
// Get the original position of each sprite
store_sprite_locations(_spritelocations1);
sprite_position_tween_store_a();
// Update the game so the sprite positions update
rct2_update();
// Get the next position of each sprite
store_sprite_locations(_spritelocations2);
sprite_position_tween_store_b();
uncapTick += 25;
uncapTick += UPDATE_TIME_MS;
}
// Tween the position of each sprite from the last position to the new position based on the time between the last
// tick and the next tick.
float nudge = 1 - ((float)(currentTick - uncapTick) / 25);
for (uint16 i = 0; i < MAX_SPRITES; i++) {
if (!sprite_should_tween(get_sprite(i)))
continue;
sprite_set_coordinates(
_spritelocations2[i].x + (sint16)((_spritelocations1[i].x - _spritelocations2[i].x) * nudge),
_spritelocations2[i].y + (sint16)((_spritelocations1[i].y - _spritelocations2[i].y) * nudge),
_spritelocations2[i].z + (sint16)((_spritelocations1[i].z - _spritelocations2[i].z) * nudge),
get_sprite(i)
);
invalidate_sprite_2(get_sprite(i));
}
float nudge = 1 - ((float)(currentTick - uncapTick) / UPDATE_TIME_MS);
sprite_position_tween_all(nudge);
platform_draw();
@ -424,21 +398,14 @@ static void openrct2_loop()
secondTick = SDL_GetTicks();
}
// Restore the real positions of the sprites so they aren't left at the mid-tween positions
for (uint16 i = 0; i < MAX_SPRITES; i++) {
if (!sprite_should_tween(get_sprite(i)))
continue;
invalidate_sprite_2(get_sprite(i));
sprite_set_coordinates(_spritelocations2[i].x, _spritelocations2[i].y, _spritelocations2[i].z, get_sprite(i));
}
sprite_position_tween_restore();
} else {
uncapTick = 0;
currentTick = SDL_GetTicks();
ticksElapsed = currentTick - lastTick;
if (ticksElapsed < 25) {
SDL_Delay(25 - ticksElapsed);
lastTick += 25;
if (ticksElapsed < UPDATE_TIME_MS) {
SDL_Delay(UPDATE_TIME_MS - ticksElapsed);
lastTick += UPDATE_TIME_MS;
} else {
lastTick = currentTick;
}
@ -461,12 +428,3 @@ void openrct2_finish()
{
_finished = 1;
}
void openrct2_reset_object_tween_locations()
{
for (uint16 i = 0; i < MAX_SPRITES; i++) {
_spritelocations1[i].x = _spritelocations2[i].x = get_sprite(i)->unknown.x;
_spritelocations1[i].y = _spritelocations2[i].y = get_sprite(i)->unknown.y;
_spritelocations1[i].z = _spritelocations2[i].z = get_sprite(i)->unknown.z;
}
}

View File

@ -60,7 +60,6 @@ bool openrct2_initialise();
void openrct2_launch();
void openrct2_dispose();
void openrct2_finish();
void openrct2_reset_object_tween_locations();
int cmdline_run(const char **argv, int argc);

View File

@ -1978,7 +1978,7 @@ bool peep_pickup_place(rct_peep* peep, int x, int y, int z, bool apply)
peep->action_sprite_image_offset = 0;
peep->action_sprite_type = 0;
peep->var_C4 = 0;
openrct2_reset_object_tween_locations();
sprite_position_tween_reset();
if (peep->type == PEEP_TYPE_GUEST) {
peep->action_sprite_type = 0xFF;
@ -12245,7 +12245,7 @@ void peep_update_name_sort(rct_peep *peep)
finish_peep_sort:
// This is required at the moment because this function reorders peeps in the sprite list
openrct2_reset_object_tween_locations();
sprite_position_tween_reset();
}
void peep_sort()

View File

@ -385,7 +385,7 @@ extern "C"
s6Importer->LoadSavedGame(rw);
s6Importer->Import();
openrct2_reset_object_tween_locations();
sprite_position_tween_reset();
result = true;
}
catch (ObjectLoadException)
@ -412,7 +412,7 @@ extern "C"
s6Importer->LoadSavedGame(path);
s6Importer->Import();
openrct2_reset_object_tween_locations();
sprite_position_tween_reset();
result = true;
}
catch (ObjectLoadException)
@ -452,7 +452,7 @@ extern "C"
s6Importer->LoadScenario(path);
s6Importer->Import();
openrct2_reset_object_tween_locations();
sprite_position_tween_reset();
result = true;
}
catch (ObjectLoadException)
@ -486,7 +486,7 @@ extern "C"
s6Importer->LoadSavedGame(rw);
s6Importer->Import();
openrct2_reset_object_tween_locations();
sprite_position_tween_reset();
result = true;
}
catch (ObjectLoadException)

View File

@ -38,24 +38,15 @@ static rct_sprite *_spriteList = RCT2_ADDRESS(RCT2_ADDRESS_SPRITE_LIST, rct_spri
uint16 gSpriteSpatialIndex[0x10001];
static rct_xyz16 _spritelocations1[MAX_SPRITES];
static rct_xyz16 _spritelocations2[MAX_SPRITES];
rct_sprite *get_sprite(size_t sprite_idx)
{
openrct2_assert(sprite_idx < MAX_SPRITES, "Tried getting sprite %u", sprite_idx);
return &_spriteList[sprite_idx];
}
void store_sprite_locations(rct_xyz16 * sprite_locations)
{
for (uint16 i = 0; i < MAX_SPRITES; i++) {
// skip going through `get_sprite` to not get stalled on assert,
// this can get very expensive for busy parks with uncap FPS option on
const rct_sprite *sprite = &_spriteList[i];
sprite_locations[i].x = sprite->unknown.x;
sprite_locations[i].y = sprite->unknown.y;
sprite_locations[i].z = sprite->unknown.z;
}
}
uint16 sprite_get_first_in_quadrant(int x, int y)
{
int offset = ((x & 0x1FE0) << 3) | (y >> 5);
@ -701,3 +692,87 @@ void litter_remove_at(int x, int y, int z)
spriteIndex = nextSpriteIndex;
}
}
/**
* Determines whether its worth tweening a sprite or not when frame smoothing is on.
*/
static bool sprite_should_tween(rct_sprite *sprite)
{
switch (sprite->unknown.linked_list_type_offset >> 1) {
case SPRITE_LIST_VEHICLE:
case SPRITE_LIST_PEEP:
case SPRITE_LIST_UNKNOWN:
return true;
}
return false;
}
static void store_sprite_locations(rct_xyz16 * sprite_locations)
{
for (uint16 i = 0; i < MAX_SPRITES; i++) {
// skip going through `get_sprite` to not get stalled on assert,
// this can get very expensive for busy parks with uncap FPS option on
const rct_sprite *sprite = &_spriteList[i];
sprite_locations[i].x = sprite->unknown.x;
sprite_locations[i].y = sprite->unknown.y;
sprite_locations[i].z = sprite->unknown.z;
}
}
void sprite_position_tween_store_a()
{
store_sprite_locations(_spritelocations1);
}
void sprite_position_tween_store_b()
{
store_sprite_locations(_spritelocations2);
}
void sprite_position_tween_all(float nudge)
{
for (uint16 i = 0; i < MAX_SPRITES; i++) {
rct_sprite * sprite = get_sprite(i);
if (sprite_should_tween(sprite)) {
rct_xyz16 posA = _spritelocations1[i];
rct_xyz16 posB = _spritelocations2[i];
sprite_set_coordinates(
posB.x + (sint16)((posA.x - posB.x) * nudge),
posB.y + (sint16)((posA.y - posB.y) * nudge),
posB.z + (sint16)((posA.z - posB.z) * nudge),
sprite
);
invalidate_sprite_2(sprite);
}
}
}
/**
* Restore the real positions of the sprites so they aren't left at the mid-tween positions
*/
void sprite_position_tween_restore()
{
for (uint16 i = 0; i < MAX_SPRITES; i++) {
rct_sprite * sprite = get_sprite(i);
if (sprite_should_tween(sprite)) {
invalidate_sprite_2(sprite);
rct_xyz16 pos = _spritelocations2[i];
sprite_set_coordinates(pos.x, pos.y, pos.z, sprite);
}
}
}
void sprite_position_tween_reset()
{
for (uint16 i = 0; i < MAX_SPRITES; i++) {
rct_sprite * sprite = get_sprite(i);
_spritelocations1[i].x =
_spritelocations2[i].x = sprite->unknown.x;
_spritelocations1[i].y =
_spritelocations2[i].y = sprite->unknown.y;
_spritelocations1[i].z =
_spritelocations2[i].z = sprite->unknown.z;
}
}

View File

@ -400,7 +400,6 @@ enum {
};
rct_sprite *get_sprite(size_t sprite_idx);
void store_sprite_locations(rct_xyz16 *sprite_locations);
// rct2: 0x00982708
extern rct_sprite_entry g_sprite_entries[48];
@ -432,6 +431,11 @@ void litter_remove_at(int x, int y, int z);
void sprite_misc_explosion_cloud_create(int x, int y, int z);
void sprite_misc_explosion_flare_create(int x, int y, int z);
uint16 sprite_get_first_in_quadrant(int x, int y);
void sprite_position_tween_store_a();
void sprite_position_tween_store_b();
void sprite_position_tween_all(float nudge);
void sprite_position_tween_restore();
void sprite_position_tween_reset();
///////////////////////////////////////////////////////////////
// Balloon