Draw: General image scaling (Entities, Fonts, etc)

This commit is contained in:
2016-09-11 13:00:49 +02:00
committed by Valeriano A.R
parent dfd4a5b77f
commit 6383c53634
7 changed files with 118 additions and 104 deletions

View File

@@ -50,6 +50,8 @@ void player_proc(Entity e, int ft) {
}
Entity_CalcBBox(e);
Entity_SetScale(e, (float[2]){0.5f, 0.5f});
// FIXME: play sound
}
if (Input_GetKey(InputKey_Left)) {
@@ -71,6 +73,8 @@ void player_proc(Entity e, int ft) {
}
if (Input_GetKey(InputKey_Action1) == InputKey_Pressed ||
Input_GetKey(InputKey_Action2) == InputKey_Pressed) {
Entity_SetScale(e, (float[2]){1.0f, 1.0f});
}
// Scroll View

View File

@@ -108,12 +108,12 @@ int Anim_GetFlip(Anim a) {
// Anim_Draw
//
//
void Anim_Draw(Anim a, int time_ms, int x, int y) {
void Anim_Draw(Anim a, int time_ms, int x, int y, float scale[2]) {
Animation *anim = a;
int frame;
frame = (time_ms / anim->ftime) % anim->frames;
Draw_DrawImgPartHoriz(anim->img, x, y, anim->w, frame);
Draw_DrawImgPartHoriz(anim->img, x, y, anim->w, frame, scale);
}
/////////////////////////////
@@ -177,17 +177,17 @@ void AnimPlay_SetImgPart(AnimPlay *ap, DrawImg img, int w, int h, int i,
// AnimPlay_Draw
//
//
void AnimPlay_Draw(AnimPlay *ani, int x, int y) {
void AnimPlay_Draw(AnimPlay *ani, int x, int y, float scale[2]) {
if (ani->anim) {
Anim_Draw(ani->anim, ani->time_ms, x, y);
Anim_Draw(ani->anim, ani->time_ms, x, y, scale);
return;
}
if (ani->img) {
Draw_DrawImg(ani->img, x, y);
Draw_DrawImg(ani->img, x, y, scale);
return;
}
if (ani->imgPart) {
Draw_DrawImgPart(ani->imgPart, x, y, ani->w, ani->h, ani->i, ani->j);
Draw_DrawImgPart(ani->imgPart, x, y, ani->w, ani->h, ani->i, ani->j, scale);
return;
}
}

View File

@@ -49,7 +49,7 @@ int Anim_GetFlip(Anim anim);
// Anim_Draw
//
//
void Anim_Draw(Anim anim, int time_ms, int x, int y);
void Anim_Draw(Anim anim, int time_ms, int x, int y, float scale[2]);
////////////////////////////////////////////////
// AnimPlay //
@@ -87,7 +87,7 @@ void AnimPlay_SetImgPart(AnimPlay *ap, DrawImg img, int w, int h, int i, int j);
// AnimPlay_Draw
//
//
void AnimPlay_Draw(AnimPlay *ani, int x, int y);
void AnimPlay_Draw(AnimPlay *ani, int x, int y, float scale[2]);
/////////////////////////////
// AnimPlay_GetOffset

View File

@@ -733,18 +733,18 @@ int Draw_GetFlip(DrawImg img) {
// Draw_DrawImg
//
// Draws an image.
void Draw_DrawImg(DrawImg img, int x, int y) {
void Draw_DrawImg(DrawImg img, int x, int y, float scale[2]) {
DrawImage image = img;
float x1, x2, y1, y2;
float u1 = 0.0f, u2 = 1.0f;
float v1 = 0.0f, v2 = 1.0f;
// Prepare
x1 = x + image->x;
y1 = y + image->y;
x2 = (x + image->x) + image->w;
y2 = (y + image->y) + image->h;
// Prepare screen coordinates
x1 = x + (image->x * scale[0]);
y1 = y + (image->y * scale[1]);
x2 = x1 + (image->w * scale[0]);
y2 = y1 + (image->h * scale[1]);
// Apply flipping
if (image->flip & 1) {
float t = u1;
@@ -778,8 +778,8 @@ void Draw_DrawImgResized(DrawImg img, int x, int y, float w, float h) {
// Prepare
x1 = x + image->x;
y1 = y + image->y;
x2 = (x + image->x) + w;
y2 = (y + image->y) + h;
x2 = x1 + w;
y2 = y1 + h;
// Apply flipping
if (image->flip & 1) {
@@ -805,17 +805,19 @@ void Draw_DrawImgResized(DrawImg img, int x, int y, float w, float h) {
// Draw_DrawImgPart
//
// Draws an image part.
void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j) {
void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j, float scale[2]) {
DrawImage image = img;
int x1, x2, y1, y2;
float us, u1, u2;
float vs, v1, v2;
// Prepare
x1 = x + image->x;
y1 = y + image->y;
x2 = (x + image->x) + w;
y2 = (y + image->y) + h;
// Prepare screen coordinates
x1 = x + (image->x * scale[0]);
y1 = y + (image->y * scale[1]);
x2 = x1 + (w * scale[0]);
y2 = y1 + (h * scale[1]);
// Prepare image coordinates
us = 1.0f / image->w;
u1 = us * i * w;
u2 = u1 + (us * w);
@@ -847,17 +849,19 @@ void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j) {
// Draw_DrawImgPartHoriz
//
// Draws an image part horizontally.
void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i) {
void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i, float scale[2]) {
DrawImage image = img;
int x1, x2, y1, y2;
float us, u1, u2;
float v1 = 0.0f, v2 = 1.0f;
// Prepare
x1 = x + image->x;
y1 = y + image->y;
x2 = (x + image->x) + w;
y2 = (y + image->y) + image->h;
// Prepare screen coordinates
x1 = x + (image->x * scale[0]);
y1 = y + (image->y * scale[1]);
x2 = x1 + (w * scale[0]);
y2 = y1 + (image->h * scale[1]);
// Prepare image coordinates
us = 1.0f / image->w;
u1 = us * i * w;
u2 = u1 + us * w;
@@ -935,6 +939,7 @@ typedef struct {
DrawImage img;
int w, h;
int min, max;
float scale[2];
} DrawFont;
/////////////////////////////
@@ -1007,6 +1012,16 @@ DrawFnt Draw_LoadFont(char *fichero, int min, int max) {
return ((DrawFnt)font);
}
/////////////////////////////
// Draw_FontScale
//
void Draw_FontScale(DrawFnt f, float scale[2]) {
DrawFont *font = f;
font->scale[0] = scale[0];
font->scale[1] = scale[1];
}
/////////////////////////////
// Draw_DrawText
//
@@ -1019,7 +1034,7 @@ void Draw_DrawText(DrawFnt f, char *text, int x, int y) {
ptr = text;
while (*ptr) {
if ((*ptr) < font->max) {
Draw_DrawImgPartHoriz(font->img, x, y, font->w, (*ptr) - font->min);
Draw_DrawImgPartHoriz(font->img, x, y, font->w, (*ptr) - font->min, font->scale);
}
x += font->w;
ptr++;

View File

@@ -83,7 +83,7 @@ int Draw_GetFlip(DrawImg img);
// Draw_DrawImg
//
// Draws an image.
void Draw_DrawImg(DrawImg img, int x, int y);
void Draw_DrawImg(DrawImg img, int x, int y, float scale[2]);
/////////////////////////////
// Draw_DrawImgResized
@@ -95,13 +95,13 @@ void Draw_DrawImgResized(DrawImg img, int x, int y, float w, float h);
// Draw_DrawImgPart
//
// Draws an image part.
void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j);
void Draw_DrawImgPart(DrawImg img, int x, int y, int w, int h, int i, int j, float scale[2]);
/////////////////////////////
// Draw_DrawImgPartHoriz
//
// Draws an image part horizontally.
void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i);
void Draw_DrawImgPartHoriz(DrawImg img, int x, int y, int w, int i, float scale[2]);
/////////////////////////////
// Draw_ImgParallax
@@ -135,6 +135,11 @@ DrawFnt Draw_DefaultFont(unsigned char r, unsigned char g, unsigned char b,
// Load a font from a file.
DrawFnt Draw_LoadFont(char *fichero, int min, int max);
/////////////////////////////
// Draw_FontScale
//
void Draw_FontScale(DrawFnt f, float scale[2]);
/////////////////////////////
// Draw_DrawText
//

View File

@@ -14,6 +14,7 @@
#define EntityIntFlag_UpdatedPos 2
#define EntityIntFlag_UpdatedColor 4
#define EntityIntFlag_UpdateColor 8
#define EntityIntFlag_UpdatedScale 16
/////////////////////////////
// Entity_New
@@ -23,14 +24,6 @@ Entity _free_entity = NULL;
Entity Entity_New() {
Entity e;
#if 0
if(!_free_entity){
e=malloc(sizeof(TEntity));
}else{
e=_free_entity;
_free_entity=e->next;
}
#else
if (!_free_entity) {
// Allocate a big block of entities
int n = 1024, i;
@@ -44,10 +37,8 @@ Entity Entity_New() {
}
_free_entity = newEnts;
}
e = _free_entity;
_free_entity = e->next;
#endif
e->base = NULL;
e->type = 0;
@@ -81,6 +72,11 @@ Entity Entity_New() {
e->defaultColor[0] = e->defaultColor[1] = e->defaultColor[2] =
e->defaultColor[3] = 1.0f;
e->scale0[0] = 1.0f;
e->scale0[1] = 1.0f;
e->scale[0] = 1.0f;
e->scale[1] = 1.0f;
e->oncopy = NULL;
e->oninit = NULL;
e->ondelete = NULL;
@@ -167,6 +163,11 @@ Entity Entity_Copy(Entity e) {
n->defaultColor[2] = e->defaultColor[2];
n->defaultColor[3] = e->defaultColor[3];
n->scale0[0] = e->scale[0];
n->scale0[1] = e->scale[1];
n->scale[0] = e->scale[0];
n->scale[1] = e->scale[1];
n->oncopy = e->oncopy;
n->oninit = e->oninit;
n->ondelete = e->ondelete;
@@ -234,6 +235,7 @@ int Entity_BBoxIntersect(Entity ent1, Entity ent2) {
//
void Entity_Draw(Entity e, int x, int y, float f) {
vec2 fPos;
float scale[2];
if (e->internalFlags & EntityIntFlag_UpdatedColor) {
Draw_SetColor(e->color0[0] - f * (e->color0[0] - e->color[0]),
e->color0[1] - f * (e->color0[1] - e->color[1]),
@@ -242,11 +244,18 @@ void Entity_Draw(Entity e, int x, int y, float f) {
} else {
Draw_SetColor(e->color[0], e->color[1], e->color[2], e->color[3]);
}
if (e->internalFlags & EntityIntFlag_UpdatedScale) {
scale[0] = e->scale0[0] - f * (e->scale0[0] - e->scale[0]);
scale[1] = e->scale0[1] - f * (e->scale0[1] - e->scale[1]);
} else {
scale[0] = e->scale[0];
scale[1] = e->scale[1];
}
if (e->internalFlags & EntityIntFlag_UpdatedPos) {
vec2_interpol(fPos, e->pos0, e->pos, f);
AnimPlay_Draw(&e->anim, fPos[0] + x, fPos[1] + y);
AnimPlay_Draw(&e->anim, fPos[0] + x, fPos[1] + y, scale);
} else {
AnimPlay_Draw(&e->anim, e->pos[0] + x, e->pos[1] + y);
AnimPlay_Draw(&e->anim, e->pos[0] + x, e->pos[1] + y, scale);
}
}
@@ -277,12 +286,18 @@ int Entity_IsVisible(Entity e, int x, int y, int w, int h) {
// Entity_Process
//
//
void Entity_Process(Entity b, int ft) {
b->internalFlags &= ~EntityIntFlag_UpdatedPos;
void Entity_Process(Entity e, int ft) {
e->internalFlags &= ~EntityIntFlag_UpdatedPos;
if (e->internalFlags & EntityIntFlag_UpdatedScale) {
e->scale0[0] = e->scale[0];
e->scale0[1] = e->scale[1];
e->internalFlags &= ~EntityIntFlag_UpdatedScale;
}
// Launch method
if (b->proc) {
b->proc(b, ft);
if (e->proc) {
e->proc(e, ft);
}
}
@@ -920,6 +935,15 @@ void Entity_SetDefaultColor(Entity e, float r, float g, float b, float a) {
e->defaultColor[3] = a;
}
/////////////////////////////
// Entity_SetScale
//
void Entity_SetScale(Entity e, float scale[2]) {
e->scale[0] = scale[0];
e->scale[1] = scale[1];
e->internalFlags |= EntityIntFlag_UpdatedScale;
}
/////////////////////////////
// Entity_Iluminate
//

View File

@@ -56,6 +56,9 @@ struct TEntity {
float light[4];
float defaultColor[4];
float scale0[2];
float scale[2];
void (*oncopy)(Entity ent);
void (*oninit)(Entity ent);
void (*ondelete)(Entity ent);
@@ -204,87 +207,50 @@ void Entity_Overlaps(Entity b1, Entity b2);
/////////////////////////////
// Entity_GetPos
//
void Entity_GetPos(Entity e, vec2 pos);
/////////////////////////////
// Entity_SetPos
//
//
void Entity_SetPos(Entity e, vec2 pos);
/////////////////////////////
// Entity_AddPos
//
//
void Entity_AddPos(Entity e, vec2 pos);
/////////////////////////////
// Entity_UpdatePos
//
void Entity_GetPos(Entity e, vec2 pos);
void Entity_SetPos(Entity e, vec2 pos);
void Entity_AddPos(Entity e, vec2 pos);
void Entity_UpdatePos(Entity e, vec2 pos);
/////////////////////////////
// Entity_AddVel
// Entity_SetVel
// Entity_SetVelH
// Entity_SetVelV
// Entity_AddVelLimit
// Entity_AddVelLimitH
// Entity_AddVelLimitH
//
void Entity_AddVel(Entity e, vec2 vel);
/////////////////////////////
// Entity_SetVel
//
void Entity_SetVel(Entity e, vec2 vel);
/////////////////////////////
// Entity_SetVelH
//
void Entity_SetVelH(Entity e, float v);
/////////////////////////////
// Entity_SetVelV
//
void Entity_SetVelV(Entity e, float v);
/////////////////////////////
// Entity_AddVelLimit
//
void Entity_AddVelLimit(Entity e, vec2 vel, float limit);
/////////////////////////////
// Entity_AddVelLimitH
//
void Entity_AddVelLimitH(Entity e, float v, float limit);
/////////////////////////////
// Entity_AddVelLimitH
//
void Entity_AddVelLimitV(Entity e, float v, float limit);
/////////////////////////////
// Entity_SetColor
//
void Entity_SetColor(Entity e, float r, float g, float b, float a);
/////////////////////////////
// Entity_AddColor
//
void Entity_AddColor(Entity e, float r, float g, float b, float a);
/////////////////////////////
// Entity_MultColor
//
//
void Entity_MultColor(Entity e, float r, float g, float b, float a);
/////////////////////////////
// Entity_AddColor
//
void Entity_SetLight(Entity e, float r, float g, float b, float rad);
/////////////////////////////
// Entity_SetDefaultColor
//
void Entity_SetColor(Entity e, float r, float g, float b, float a);
void Entity_AddColor(Entity e, float r, float g, float b, float a);
void Entity_MultColor(Entity e, float r, float g, float b, float a);
void Entity_SetLight(Entity e, float r, float g, float b, float rad);
void Entity_SetDefaultColor(Entity e, float r, float g, float b, float a);
/////////////////////////////
// Entity_SetScale
//
void Entity_SetScale(Entity e, float scale[2]);
/////////////////////////////
// Entity_Iluminate
//