Make perspective scale work more explicitly from properties

This commit is contained in:
2026-05-13 23:36:49 -04:00
parent aad196d84e
commit 73b1a4cab0
3 changed files with 31 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
#ifndef _SDL_GAMECONTROLLERDB_H_ #ifndef _SDL_GAMECONTROLLERDB_H_
#define _SDL_GAMECONTROLLERDB_H_ #define _SDL_GAMECONTROLLERDB_H_
// Taken from https://raw.githubusercontent.com/mdqinc/SDL_GameControllerDB/refs/heads/master/gamecontrollerdb.txt on Wed May 13 10:44:28 PM EDT 2026 // Taken from https://raw.githubusercontent.com/mdqinc/SDL_GameControllerDB/refs/heads/master/gamecontrollerdb.txt on Wed May 13 11:34:15 PM EDT 2026
#define AKGL_SDL_GAMECONTROLLER_DB_LEN 2228 #define AKGL_SDL_GAMECONTROLLER_DB_LEN 2228

View File

@@ -91,6 +91,8 @@ typedef struct {
int p_vanishing_y; int p_vanishing_y;
int p_foreground_h; int p_foreground_h;
int p_vanishing_h; int p_vanishing_h;
float p_foreground_scale;
float p_vanishing_scale;
float p_scale; float p_scale;
float p_rate; float p_rate;
akgl_Tileset tilesets[AKGL_TILEMAP_MAX_TILESETS]; akgl_Tileset tilesets[AKGL_TILEMAP_MAX_TILESETS];

View File

@@ -35,7 +35,7 @@ akerr_ErrorContext *akgl_get_json_tilemap_property(json_t *obj, char *key, char
} }
CATCH(errctx, akgl_get_json_string_value(property, "type", &tmpstr)); CATCH(errctx, akgl_get_json_string_value(property, "type", &tmpstr));
if ( strcmp(tmpstr->data, type) != 0 ) { if ( strcmp(tmpstr->data, type) != 0 ) {
FAIL_BREAK(errctx, AKERR_TYPE, "Character property is present but is incorrect type"); FAIL_BREAK(errctx, AKERR_TYPE, "Object property is present but is incorrect type");
} }
*dest = property; *dest = property;
SUCCEED_RETURN(errctx); SUCCEED_RETURN(errctx);
@@ -82,6 +82,20 @@ akerr_ErrorContext *akgl_get_json_properties_integer(json_t *obj, char *key, int
SUCCEED_RETURN(errctx); SUCCEED_RETURN(errctx);
} }
akerr_ErrorContext *akgl_get_json_properties_number(json_t *obj, char *key, float *dest)
{
PREPARE_ERROR(errctx);
json_t *property = NULL;
ATTEMPT {
CATCH(errctx, akgl_get_json_tilemap_property(obj, key, "number", &property));
CATCH(errctx, akgl_get_json_number_value(property, "value", dest));
} CLEANUP {
} PROCESS(errctx) {
} FINISH(errctx, true);
SUCCEED_RETURN(errctx);
}
akerr_ErrorContext *akgl_tilemap_load_tilesets_each(json_t *tileset, akgl_Tilemap *dest, int tsidx) akerr_ErrorContext *akgl_tilemap_load_tilesets_each(json_t *tileset, akgl_Tilemap *dest, int tsidx)
{ {
PREPARE_ERROR(errctx); PREPARE_ERROR(errctx);
@@ -282,9 +296,11 @@ akerr_ErrorContext *akgl_tilemap_load_layer_objects(akgl_Tilemap *dest, json_t *
if ( strcmp((char *)curobj->name, "p_foreground") == 0 ) { if ( strcmp((char *)curobj->name, "p_foreground") == 0 ) {
dest->p_foreground_y = curobj->y; dest->p_foreground_y = curobj->y;
CATCH(errctx, akgl_get_json_integer_value((json_t *)layerdatavalue, "height", &dest->p_foreground_h)); CATCH(errctx, akgl_get_json_integer_value((json_t *)layerdatavalue, "height", &dest->p_foreground_h));
CATCH(errctx, akgl_get_json_properties_number((json_t *)layerdatavalue, "scale", &dest->p_foreground_scale));
} else if ( strcmp((char *)curobj->name, "p_vanishing") == 0 ) { } else if ( strcmp((char *)curobj->name, "p_vanishing") == 0 ) {
dest->p_vanishing_y = curobj->y; dest->p_vanishing_y = curobj->y;
CATCH(errctx, akgl_get_json_integer_value((json_t *)layerdatavalue, "height", &dest->p_vanishing_h)); CATCH(errctx, akgl_get_json_integer_value((json_t *)layerdatavalue, "height", &dest->p_vanishing_h));
CATCH(errctx, akgl_get_json_properties_number((json_t *)layerdatavalue, "scale", &dest->p_vanishing_scale));
} }
} }
@@ -418,7 +434,8 @@ akerr_ErrorContext *akgl_tilemap_load(char *fname, akgl_Tilemap *dest)
FAIL_ZERO_RETURN(errctx, dest, AKERR_NULLPOINTER, "load_tilemap received null tilemap"); FAIL_ZERO_RETURN(errctx, dest, AKERR_NULLPOINTER, "load_tilemap received null tilemap");
memset(dest, 0x00, sizeof(akgl_Tilemap)); memset(dest, 0x00, sizeof(akgl_Tilemap));
dest->p_foreground_scale = 1.0;
dest->p_vanishing_scale = 1.0;
ATTEMPT { ATTEMPT {
//CATCH(errctx, akgl_heap_next_string(&tmpstr)); //CATCH(errctx, akgl_heap_next_string(&tmpstr));
@@ -450,15 +467,14 @@ akerr_ErrorContext *akgl_tilemap_load(char *fname, akgl_Tilemap *dest)
if ( dest->p_foreground_y && dest->p_vanishing_y ) { if ( dest->p_foreground_y && dest->p_vanishing_y ) {
// How much bigger is the foreground vs the vanishing point? // How much bigger is the foreground vs the vanishing point?
// if vanishing is height 16, and foreground is height 32, that is a 2x scale difference // if vanishing is height 16, and foreground is height 32, that is a 2x scale difference
dest->p_scale = (dest->p_foreground_h / dest->p_vanishing_h); /* dest->p_scale = ((float)dest->p_foreground_h / (float)dest->p_vanishing_h); */
SDL_Log("Map perspective scale is (%d/%d) = %f", dest->p_foreground_h, dest->p_vanishing_h, dest->p_scale); /* SDL_Log("Map perspective scale is (%d/%d) = %f", dest->p_foreground_h, dest->p_vanishing_h, dest->p_scale); */
// Sprites are always size 1.0 at the foreground, so how much do we need to // Sprites are scale N (default 1.0) at the foreground, so how much do we need to
// scale them for every pixel above foreground_y before they reach vanishing_y? // scale them for every pixel above foreground_y before they reach vanishing_y?
// If vanishing is at 320 and foreground is at 640, that is a 320 line difference // If vanishing is at 320 and foreground is at 640, that is a 320 line difference
// If our scaling rate is 2x, then our rate is ((1.0 - (1.0 / 2.0)) / 320.0), or // If our scaling rate is 2x, then our rate is (((N=1.0) / (640 - 320)) / (dest->p_scale = 2)), or
// 0.0015625% scale per pixel. // 0.0066% scale per pixel.
// At position 640, dest->p_rate = ((dest->p_foreground_scale - dest->p_vanishing_scale) / (dest->p_foreground_y - dest->p_vanishing_y));
dest->p_rate = (1.0 / (dest->p_foreground_y - dest->p_vanishing_y)) / dest->p_scale;
SDL_Log("Map perspective rate is %f", dest->p_rate); SDL_Log("Map perspective rate is %f", dest->p_rate);
} }
} CLEANUP { } CLEANUP {
@@ -647,11 +663,11 @@ akerr_ErrorContext AKERR_NOIGNORE *akgl_tilemap_scale_actor(akgl_Tilemap *map, a
FAIL_ZERO_RETURN(e, actor, AKERR_NULLPOINTER, "NULL actor"); FAIL_ZERO_RETURN(e, actor, AKERR_NULLPOINTER, "NULL actor");
if ( actor->y <= map->p_vanishing_y ) { if ( actor->y <= map->p_vanishing_y ) {
actor->scale = 1.0 / map->p_scale; actor->scale = map->p_vanishing_scale;
} else if ( actor->y >= map->p_foreground_y ) { } else if ( actor->y >= map->p_foreground_y ) {
actor->scale = 1.0; actor->scale = map->p_foreground_scale;
} else { } else {
actor->scale = 1.0 - (map->p_rate * (map->p_foreground_y - actor->y)); actor->scale = map->p_foreground_scale - (map->p_rate * (map->p_foreground_y - actor->y));
} }
SUCCEED_RETURN(e); SUCCEED_RETURN(e);
} }