Scaling and rotation working.
This commit is contained in:
parent
a7f96f25b4
commit
881fb4de3f
1 changed files with 84 additions and 63 deletions
109
src/singe.c
109
src/singe.c
|
@ -382,9 +382,9 @@ int32_t apiSpriteDraw(lua_State *L);
|
||||||
int32_t apiSpriteGetHeight(lua_State *L);
|
int32_t apiSpriteGetHeight(lua_State *L);
|
||||||
int32_t apiSpriteGetWidth(lua_State *L);
|
int32_t apiSpriteGetWidth(lua_State *L);
|
||||||
int32_t apiSpriteLoad(lua_State *L);
|
int32_t apiSpriteLoad(lua_State *L);
|
||||||
|
int32_t apiSpriteQuality(lua_State *L);
|
||||||
int32_t apiSpriteRotate(lua_State *L);
|
int32_t apiSpriteRotate(lua_State *L);
|
||||||
int32_t apiSpriteScale(lua_State *L);
|
int32_t apiSpriteScale(lua_State *L);
|
||||||
int32_t apiSpriteSmooth(lua_State *L);
|
|
||||||
int32_t apiSpriteUnload(lua_State *L);
|
int32_t apiSpriteUnload(lua_State *L);
|
||||||
|
|
||||||
int32_t apiVideoDraw(lua_State *L);
|
int32_t apiVideoDraw(lua_State *L);
|
||||||
|
@ -422,6 +422,7 @@ void putPixel(int32_t x, int32_t y, SDL_Color *c);
|
||||||
void startControllers(void);
|
void startControllers(void);
|
||||||
void startLuaContext(lua_State *L);
|
void startLuaContext(lua_State *L);
|
||||||
void stopControllers(void);
|
void stopControllers(void);
|
||||||
|
SDL_Surface *surfaceCopy(SDL_Surface *source);
|
||||||
void takeScreenshot(void);
|
void takeScreenshot(void);
|
||||||
void updatePauseState(void);
|
void updatePauseState(void);
|
||||||
|
|
||||||
|
@ -1080,15 +1081,15 @@ int32_t apiFontToSprite(lua_State *L) {
|
||||||
message = lua_tostring(L, 1);
|
message = lua_tostring(L, 1);
|
||||||
switch (_global.fontQuality) {
|
switch (_global.fontQuality) {
|
||||||
case 1:
|
case 1:
|
||||||
sprite->surface = TTF_RenderText_Solid(_global.fontCurrent->font, message, _global.colorForeground);
|
sprite->originalSurface = TTF_RenderText_Solid(_global.fontCurrent->font, message, _global.colorForeground);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 2:
|
case 2:
|
||||||
sprite->surface = TTF_RenderText_Shaded(_global.fontCurrent->font, message, _global.colorForeground, _global.colorBackground);
|
sprite->originalSurface = TTF_RenderText_Shaded(_global.fontCurrent->font, message, _global.colorForeground, _global.colorBackground);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 3:
|
case 3:
|
||||||
sprite->surface = TTF_RenderText_Blended(_global.fontCurrent->font, message, _global.colorForeground);
|
sprite->originalSurface = TTF_RenderText_Blended(_global.fontCurrent->font, message, _global.colorForeground);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -1096,11 +1097,11 @@ int32_t apiFontToSprite(lua_State *L) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!sprite->surface) {
|
if (!sprite->originalSurface) {
|
||||||
luaDie(L, "fontToSprite", "Font surface is null!");
|
luaDie(L, "fontToSprite", "Font surface is null!");
|
||||||
} else {
|
} else {
|
||||||
SDL_SetColorKey(sprite->surface, true, 0);
|
SDL_SetColorKey(sprite->originalSurface, true, 0);
|
||||||
SDL_BlitSurface(sprite->surface, NULL, sprite->originalSurface, NULL);
|
sprite->surface = surfaceCopy(sprite->originalSurface);
|
||||||
sprite->scaleX = 1.0;
|
sprite->scaleX = 1.0;
|
||||||
sprite->scaleY = 1.0;
|
sprite->scaleY = 1.0;
|
||||||
sprite->id = _global.nextSpriteId;
|
sprite->id = _global.nextSpriteId;
|
||||||
|
@ -1888,6 +1889,7 @@ int32_t apiSpriteDraw(lua_State *L) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//utilSay("spriteDraw: x=%d y=%d c=%d id=%d", dest.x, dest.y, center, id);
|
||||||
HASH_FIND_INT(_global.spriteList, &id, sprite);
|
HASH_FIND_INT(_global.spriteList, &id, sprite);
|
||||||
if (!sprite) luaDie(L, "spriteDraw", "No sprite at index %d in apiSpriteDraw.", id);
|
if (!sprite) luaDie(L, "spriteDraw", "No sprite at index %d in apiSpriteDraw.", id);
|
||||||
if ((n == 3) || (n == 4)) {
|
if ((n == 3) || (n == 4)) {
|
||||||
|
@ -1989,10 +1991,10 @@ int32_t apiSpriteLoad(lua_State *L) {
|
||||||
sprite = (SpriteT *)calloc(1, sizeof(SpriteT));
|
sprite = (SpriteT *)calloc(1, sizeof(SpriteT));
|
||||||
if (!sprite) luaDie(L, "spriteLoad", "Unable to allocate new sprite.");
|
if (!sprite) luaDie(L, "spriteLoad", "Unable to allocate new sprite.");
|
||||||
name = lua_tostring(L, 1);
|
name = lua_tostring(L, 1);
|
||||||
sprite->surface = IMG_Load(name);
|
sprite->originalSurface = IMG_Load(name);
|
||||||
if (!sprite->surface) luaDie(L, "spriteLoad", "%s", IMG_GetError());
|
if (!sprite->originalSurface) luaDie(L, "spriteLoad", "%s", IMG_GetError());
|
||||||
SDL_SetColorKey(sprite->surface, true, 0);
|
SDL_SetColorKey(sprite->originalSurface, true, 0);
|
||||||
SDL_BlitSurface(sprite->surface, NULL, sprite->originalSurface, NULL);
|
sprite->surface = surfaceCopy(sprite->originalSurface);
|
||||||
sprite->scaleX = 1.0;
|
sprite->scaleX = 1.0;
|
||||||
sprite->scaleY = 1.0;
|
sprite->scaleY = 1.0;
|
||||||
sprite->id = _global.nextSpriteId;
|
sprite->id = _global.nextSpriteId;
|
||||||
|
@ -2012,6 +2014,37 @@ int32_t apiSpriteLoad(lua_State *L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int32_t apiSpriteQuality(lua_State *L) {
|
||||||
|
int32_t n = lua_gettop(L);
|
||||||
|
int32_t id = -1;
|
||||||
|
int32_t s;
|
||||||
|
double d;
|
||||||
|
SpriteT *sprite = NULL;
|
||||||
|
|
||||||
|
if (n == 2) {
|
||||||
|
if (lua_isnumber(L, 1)) {
|
||||||
|
if (lua_isnumber(L, 2)) {
|
||||||
|
d = lua_tonumber(L, 1); s = (int32_t)d;
|
||||||
|
d = lua_tonumber(L, 2); id = (int32_t)d;
|
||||||
|
HASH_FIND_INT(_global.spriteList, &id, sprite);
|
||||||
|
if (!sprite) luaDie(L, "spriteQuality", "No sprite at index %d in apiSpriteQuality.", id);
|
||||||
|
sprite->smooth = s;
|
||||||
|
SDL_FreeSurface(sprite->surface);
|
||||||
|
sprite->surface = rotozoomSurfaceXY(sprite->originalSurface, 360 - sprite->angle, sprite->scaleX, sprite->scaleY, sprite->smooth);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (id >= 0) {
|
||||||
|
luaTrace(L, "spriteQuality", "%d %d", id, sprite->smooth);
|
||||||
|
} else {
|
||||||
|
luaDie(L, "spriteQuality", "Failed!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int32_t apiSpriteRotate(lua_State *L) {
|
int32_t apiSpriteRotate(lua_State *L) {
|
||||||
int32_t n = lua_gettop(L);
|
int32_t n = lua_gettop(L);
|
||||||
int32_t id = -1;
|
int32_t id = -1;
|
||||||
|
@ -2028,7 +2061,7 @@ int32_t apiSpriteRotate(lua_State *L) {
|
||||||
if (!sprite) luaDie(L, "spriteRotate", "No sprite at index %d in apiSpriteRotate.", id);
|
if (!sprite) luaDie(L, "spriteRotate", "No sprite at index %d in apiSpriteRotate.", id);
|
||||||
sprite->angle = a;
|
sprite->angle = a;
|
||||||
SDL_FreeSurface(sprite->surface);
|
SDL_FreeSurface(sprite->surface);
|
||||||
sprite->surface = rotozoomSurfaceXY(sprite->originalSurface, sprite->angle, sprite->scaleX, sprite->scaleY, sprite->smooth);
|
sprite->surface = rotozoomSurfaceXY(sprite->originalSurface, 360 - sprite->angle, sprite->scaleX, sprite->scaleY, sprite->smooth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2067,7 +2100,7 @@ int32_t apiSpriteScale(lua_State *L) {
|
||||||
sprite->scaleX = x;
|
sprite->scaleX = x;
|
||||||
sprite->scaleY = y;
|
sprite->scaleY = y;
|
||||||
SDL_FreeSurface(sprite->surface);
|
SDL_FreeSurface(sprite->surface);
|
||||||
sprite->surface = rotozoomSurfaceXY(sprite->originalSurface, sprite->angle, sprite->scaleX, sprite->scaleY, sprite->smooth);
|
sprite->surface = rotozoomSurfaceXY(sprite->originalSurface, 360 - sprite->angle, sprite->scaleX, sprite->scaleY, sprite->smooth);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2082,37 +2115,6 @@ int32_t apiSpriteScale(lua_State *L) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int32_t apiSpriteSmooth(lua_State *L) {
|
|
||||||
int32_t n = lua_gettop(L);
|
|
||||||
int32_t id = -1;
|
|
||||||
int32_t s;
|
|
||||||
double d;
|
|
||||||
SpriteT *sprite = NULL;
|
|
||||||
|
|
||||||
if (n == 2) {
|
|
||||||
if (lua_isnumber(L, 1)) {
|
|
||||||
if (lua_isnumber(L, 2)) {
|
|
||||||
d = lua_tonumber(L, 1); s = (int32_t)d;
|
|
||||||
d = lua_tonumber(L, 2); id = (int32_t)d;
|
|
||||||
HASH_FIND_INT(_global.spriteList, &id, sprite);
|
|
||||||
if (!sprite) luaDie(L, "spriteSmooth", "No sprite at index %d in apiSpriteSmooth.", id);
|
|
||||||
sprite->smooth = s;
|
|
||||||
SDL_FreeSurface(sprite->surface);
|
|
||||||
sprite->surface = rotozoomSurfaceXY(sprite->originalSurface, sprite->angle, sprite->scaleX, sprite->scaleY, sprite->smooth);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (id >= 0) {
|
|
||||||
luaTrace(L, "spriteSmooth", "%d %d", id, sprite->smooth);
|
|
||||||
} else {
|
|
||||||
luaDie(L, "spriteSmooth", "Failed!");
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int32_t apiSpriteUnload(lua_State *L) {
|
int32_t apiSpriteUnload(lua_State *L) {
|
||||||
int32_t n = lua_gettop(L);
|
int32_t n = lua_gettop(L);
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
@ -3976,6 +3978,9 @@ void singe(SDL_Window *window, SDL_Renderer *renderer, ConfigT *conf) {
|
||||||
lua_register(_global.luaContext, "spriteGetHeight", apiSpriteGetHeight);
|
lua_register(_global.luaContext, "spriteGetHeight", apiSpriteGetHeight);
|
||||||
lua_register(_global.luaContext, "spriteGetWidth", apiSpriteGetWidth);
|
lua_register(_global.luaContext, "spriteGetWidth", apiSpriteGetWidth);
|
||||||
lua_register(_global.luaContext, "spriteLoad", apiSpriteLoad);
|
lua_register(_global.luaContext, "spriteLoad", apiSpriteLoad);
|
||||||
|
lua_register(_global.luaContext, "spriteQuality", apiSpriteQuality);
|
||||||
|
lua_register(_global.luaContext, "spriteRotate", apiSpriteRotate);
|
||||||
|
lua_register(_global.luaContext, "spriteScale", apiSpriteScale);
|
||||||
lua_register(_global.luaContext, "spriteUnload", apiSpriteUnload);
|
lua_register(_global.luaContext, "spriteUnload", apiSpriteUnload);
|
||||||
|
|
||||||
lua_register(_global.luaContext, "videoDraw", apiVideoDraw);
|
lua_register(_global.luaContext, "videoDraw", apiVideoDraw);
|
||||||
|
@ -4664,6 +4669,22 @@ void stopControllers(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
SDL_Surface *surfaceCopy(SDL_Surface *source) {
|
||||||
|
SDL_Surface *destination = NULL;
|
||||||
|
destination = SDL_CreateRGBSurface(
|
||||||
|
source->flags,
|
||||||
|
source->w,
|
||||||
|
source->h,
|
||||||
|
source->format->BitsPerPixel,
|
||||||
|
source->format->Rmask,
|
||||||
|
source->format->Gmask,
|
||||||
|
source->format->Bmask,
|
||||||
|
source->format->Amask);
|
||||||
|
if (destination != NULL) SDL_BlitSurface(source, NULL, destination, NULL);
|
||||||
|
return destination;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void takeScreenshot(void) {
|
void takeScreenshot(void) {
|
||||||
int32_t x = 0;
|
int32_t x = 0;
|
||||||
char filename[1024];
|
char filename[1024];
|
||||||
|
|
Loading…
Add table
Reference in a new issue