Before converting overlay to a Surface.

This commit is contained in:
Scott Duensing 2019-12-11 19:01:07 -06:00
parent 1ccc25d337
commit 794177b078
2 changed files with 308 additions and 186 deletions

View file

@ -44,6 +44,7 @@
#define SCROLLWHEEL_DISPLAY_TICKS 100
#define NOMOUSE -1
#define KEYBD_ARRAY_SIZE 15
#define MOUSE_ARRAY_SIZE 6
typedef struct MouseS {
@ -160,7 +161,7 @@ int _confYResolution = -1;
static MouseT _mice[MAX_MICE];
static lua_State *_luaContext = NULL;
static SDL_Color _colorForeground = { 255, 255, 255, 255 };
static SDL_Color _colorBackground = { 0, 0, 0, 255 };
static SDL_Color _colorBackground = { 0, 0, 0, 0 };
static SDL_Texture *_overlay = NULL;
static SDL_Window *_window = NULL;
static SDL_Renderer *_renderer = NULL;
@ -185,6 +186,8 @@ static SpriteT *_spriteList = NULL;
static SoundT *_soundList = NULL;
static FontT *_fontList = NULL;
static FontT *_fontCurrent = NULL;
static void *_pixelBuffer = NULL;
static int _keyDefs[SWITCH_COUNT][2] = {
{ SDLK_UP, SDLK_KP_8 }, // Up
@ -295,50 +298,30 @@ int apiSingeSetGameName(lua_State *L);
int apiSingeGetScriptPath(lua_State *L);
void callLua(const char *func, const char *sig, ...);
void channelFinished(int channel);
void luaDie(lua_State *L, char *method, char *fmt, ...);
int luaError(lua_State *L);
void luaTrace(lua_State *L, char *method, char *fmt, ...);
void makeZeroTransparent(SDL_Texture *texture);
void processKey(bool down, int keysym);
void luaTrace(lua_State *L, char *method, char *fmt, ...) {
va_list args;
lua_Debug ar;
char *string1 = NULL;
char *string2 = NULL;
if (utilTraceFile) {
lua_getstack(L, 1, &ar);
lua_getinfo(L, "nSl", &ar);
string1 = utilCreateString("%d:%s: ", ar.currentline, method);
if (!string1) utilDie("Unable to allocate first trace string.");
va_start(args, fmt);
string2 = utilCreateStringVArgs(fmt, args);
if (!string2) utilDie("Unable to allocate second trace string.");
va_end(args);
utilSay("%s%s", string1, string2);
utilTrace("%s%s", string1, string2);
free(string2);
free(string1);
}
}
int apiColorBackground(lua_State *L) {
int n = lua_gettop(L);
bool result = false;
int n = lua_gettop(L);
double d = 0;
bool result = false;
if ((n == 3) || (n == 4)) {
if (lua_isinteger(L, 1)) {
if (lua_isinteger(L, 2)) {
if (lua_isinteger(L, 3)) {
_colorBackground.r = (byte)lua_tointeger(L, 1);
_colorBackground.g = (byte)lua_tointeger(L, 2);
_colorBackground.b = (byte)lua_tointeger(L, 3);
if (lua_isnumber(L, 1)) {
if (lua_isnumber(L, 2)) {
if (lua_isnumber(L, 3)) {
d = lua_tonumber(L, 1); _colorBackground.r = (byte)d;
d = lua_tonumber(L, 2); _colorBackground.g = (byte)d;
d = lua_tonumber(L, 3); _colorBackground.b = (byte)d;
if (n == 3) {
_colorBackground.a = (byte)255;
} else {
if (lua_isinteger(L, 4)) {
_colorBackground.a = (byte)lua_tointeger(L, 4);
if (lua_isnumber(L, 4)) {
d = lua_tonumber(L, 4); _colorBackground.a = (byte)d;
} else {
_colorBackground.a = (byte)255;
}
@ -360,21 +343,22 @@ int apiColorBackground(lua_State *L) {
int apiColorForeground(lua_State *L) {
int n = lua_gettop(L);
bool result = false;
int n = lua_gettop(L);
bool result = false;
double d = 0;
if ((n == 3) || (n == 4)) {
if (lua_isinteger(L, 1)) {
if (lua_isinteger(L, 2)) {
if (lua_isinteger(L, 3)) {
_colorForeground.r = (byte)lua_tointeger(L, 1);
_colorForeground.g = (byte)lua_tointeger(L, 2);
_colorForeground.b = (byte)lua_tointeger(L, 3);
if (lua_isnumber(L, 1)) {
if (lua_isnumber(L, 2)) {
if (lua_isnumber(L, 3)) {
d = lua_tonumber(L, 1); _colorForeground.r = (byte)d;
d = lua_tonumber(L, 2); _colorForeground.g = (byte)d;
d = lua_tonumber(L, 3); _colorForeground.b = (byte)d;
if (n == 3) {
_colorForeground.a = (byte)255;
} else {
if (lua_isinteger(L, 4)) {
_colorForeground.a = (byte)lua_tointeger(L, 4);
if (lua_isnumber(L, 4)) {
d = lua_tonumber(L, 4); _colorForeground.a = (byte)d;
} else {
_colorForeground.a = (byte)255;
}
@ -398,7 +382,7 @@ int apiColorForeground(lua_State *L) {
int apiDaphneGetHeight(lua_State *L) {
int y;
SDL_GetWindowSize(_window, NULL, &y);
luaTrace(L, "DaphneGetHeight", "%d", y);
luaTrace(L, "daphneGetHeight", "%d", y);
lua_pushinteger(L, y);
return 1;
}
@ -407,7 +391,7 @@ int apiDaphneGetHeight(lua_State *L) {
int apiDaphneGetWidth(lua_State *L) {
int x;
SDL_GetWindowSize(_window, &x, NULL);
luaTrace(L, "DaphneGetWidth", "%d", x);
luaTrace(L, "daphneGetWidth", "%d", x);
lua_pushinteger(L, x);
return 1;
}
@ -429,19 +413,19 @@ int apiDaphneScreenshot(lua_State *L) {
if (!utilFileExists(filename)) break;
x++;
}
if (x > 999) utilDie("Seriously? You have 1000 screenshots in this folder? Remove some.");
if (x > 999) luaDie(L, "daphneScreenshot", "Seriously? You have 1000 screenshots in this folder? Remove some.");
luaTrace(L, "DaphneScreenshot", "%s", filename);
luaTrace(L, "daphneScreenshot", "%s", filename);
SDL_SetRenderTarget(_renderer, NULL);
texture = SDL_GetRenderTarget(_renderer);
SDL_QueryTexture(texture, &format, NULL, &x, &y);
if (SDL_QueryTexture(texture, &format, NULL, &x, &y) < 0) luaDie(L, "daphneScreenshot", "%s", SDL_GetError());
pixels = malloc((size_t)x * (size_t)y * SDL_BYTESPERPIXEL(format));
if (!pixels) utilDie("Unable to allocate screenshot.");
if (SDL_RenderReadPixels(_renderer, NULL, format, pixels, (Uint16)x * SDL_BYTESPERPIXEL(format)) < 0) utilDie("%s", SDL_GetError());
if (!pixels) luaDie(L, "daphneScreenshot", "Unable to allocate screenshot.");
if (SDL_RenderReadPixels(_renderer, NULL, format, pixels, (Uint16)x * SDL_BYTESPERPIXEL(format)) < 0) luaDie(L, "daphneScreenshot", "%s", SDL_GetError());
surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, x, y, SDL_BITSPERPIXEL(format), (Uint16)x * SDL_BYTESPERPIXEL(format), format);
if (!surface) utilDie("%s", SDL_GetError());
if (IMG_SavePNG(surface, filename) < 0) utilDie("%s", IMG_GetError());
if (!surface) luaDie(L, "daphneScreenshot", "%s", SDL_GetError());
if (IMG_SavePNG(surface, filename) < 0) luaDie(L, "daphneScreenshot", "%s", IMG_GetError());
SDL_FreeSurface(surface);
free(pixels);
SDL_SetRenderTarget(_renderer, _overlay);
@ -465,18 +449,19 @@ int apiDebugPrint(lua_State *L) {
int apiDiscAudio(lua_State *L) {
int n = lua_gettop(L);
int channel = 0;
int left = 0;
int right = 0;
bool onOff = false;
bool result = false;
int n = lua_gettop(L);
int channel = 0;
int left = 0;
int right = 0;
bool onOff = false;
bool result = false;
double d = 0;
if (n == 2) {
if (lua_isinteger(L, 1)) {
if (lua_isnumber(L, 1)) {
if (lua_isboolean(L, 2)) {
channel = (int)lua_tointeger(L, 1);
onOff = (bool)lua_toboolean(L, 2);
d = lua_tonumber(L, 1); channel = (int)d;
d = lua_tonumber(L, 2); onOff = (bool)d;
if ((channel == 1) && onOff) left = _confVolumeVldp;
if ((channel == 2) && onOff) right = _confVolumeVldp;
videoSetVolume(_videoHandle, left, right);
@ -530,14 +515,15 @@ int apiDiscPause(lua_State *L) {
int apiDiscPauseAtFrame(lua_State *L) {
int n = lua_gettop(L);
int frame = 0;
bool result = false;
int n = lua_gettop(L);
int frame = 0;
bool result = false;
double d = 0;
if (!_discStopped) {
if (n == 1) {
if (lua_isinteger(L, 1)) {
frame = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); frame = (int)d;
videoSeek(_videoHandle, frame);
videoPause(_videoHandle);
result = true;
@ -572,14 +558,15 @@ int apiDiscPlay(lua_State *L) {
int apiDiscSearch(lua_State *L) {
int n = lua_gettop(L);
int frame = 0;
bool result = false;
int n = lua_gettop(L);
int frame = 0;
bool result = false;
double d = 0;
if (!_discStopped) {
if (n == 1) {
if (lua_isinteger(L, 1)) {
frame = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); frame = (int)d;
videoSeek(_videoHandle, frame);
result = true;
}
@ -616,14 +603,15 @@ int apiDiscSetFps(lua_State *L) {
int apiDiscSkipBackward(lua_State *L) {
int n = lua_gettop(L);
int frame = 0;
bool result = false;
int n = lua_gettop(L);
int frame = 0;
bool result = false;
double d = 0;
if (!_discStopped) {
if (n == 1) {
if (lua_isinteger(L, 1)) {
frame = videoGetFrame(_videoHandle) - (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); frame = videoGetFrame(_videoHandle) - (int)d;
videoSeek(_videoHandle, frame);
result = true;
}
@ -652,14 +640,15 @@ int apiDiscSkipBlanking(lua_State *L) {
int apiDiscSkipForward(lua_State *L) {
int n = lua_gettop(L);
int frame = 0;
bool result = false;
int n = lua_gettop(L);
int frame = 0;
bool result = false;
double d = 0;
if (!_discStopped) {
if (n == 1) {
if (lua_isinteger(L, 1)) {
frame = videoGetFrame(_videoHandle) + (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); frame = videoGetFrame(_videoHandle) + (int)d;
videoSeek(_videoHandle, frame);
result = true;
}
@ -737,18 +726,19 @@ int apiFontLoad(lua_State *L) {
int result = -1;
int points = 0;
const char *name = NULL;
double d = 0;
FontT *font = NULL;
if (n == 2) {
if (lua_isstring(L, 1)) {
if (lua_isinteger(L, 2)) {
if (lua_isnumber(L, 2)) {
name = lua_tostring(L, 1);
points = (int)lua_tointeger(L, 2);
d = lua_tonumber(L, 2); points = (int)d;
font = (FontT *)calloc(1, sizeof(FontT));
if (!font) utilDie("Unable to allocate new font.");
if (!font) luaDie(L, "fontLoad", "Unable to allocate new font.");
// Load this font.
font->font = TTF_OpenFont(name, points);
if (!font->font) utilDie("%s", TTF_GetError());
if (!font->font) luaDie(L, "fontLoad", "%s", TTF_GetError());
// Make it the current font and mark it as loaded.
font->id = _nextFontId;
result = _nextFontId++;
@ -772,17 +762,18 @@ int apiFontLoad(lua_State *L) {
int apiFontPrint(lua_State *L) {
int n = lua_gettop(L);
const char *message = NULL;
double d = 0;
SDL_Surface *textSurface = NULL;
SDL_Texture *texture = NULL;
SDL_Rect dest;
if (n == 3) {
if (lua_isinteger(L, 1)) {
if (lua_isinteger(L, 2)) {
if (lua_isnumber(L, 1)) {
if (lua_isnumber(L, 2)) {
if (lua_isstring(L, 3)) {
if (_fontCurrent) {
dest.x = (int)lua_tointeger(L, 1);
dest.y = (int)lua_tointeger(L, 2);
d = lua_tonumber(L, 1); dest.x = (int)d;
d = lua_tonumber(L, 2); dest.y = (int)d;
textSurface = NULL;
message = lua_tostring(L, 3);
switch (_fontQuality) {
@ -799,15 +790,15 @@ int apiFontPrint(lua_State *L) {
break;
}
if (!textSurface) {
utilDie("Font surface is null!");
luaDie(L, "fontPrint", "Font surface is null!");
} else {
SDL_SetSurfaceBlendMode(textSurface, SDL_BLENDMODE_BLEND);
// Transparent index is 0
SDL_SetColorKey(textSurface, true, 0);
texture = SDL_CreateTextureFromSurface(_renderer, textSurface);
SDL_FreeSurface(textSurface);
if (!texture) utilDie("%s", SDL_GetError());
if (SDL_QueryTexture(texture, NULL, NULL, &dest.w, &dest.h) < 0) utilDie("%s", SDL_GetError());
if (!texture) luaDie(L, "fontPrint", "%s", SDL_GetError());
if (SDL_QueryTexture(texture, NULL, NULL, &dest.w, &dest.h) < 0) luaDie(L, "fontPrint", "%s", SDL_GetError());
SDL_RenderCopy(_renderer, texture, NULL, &dest);
}
}
@ -827,12 +818,13 @@ int apiFontPrint(lua_State *L) {
int apiFontQuality(lua_State *L) {
int n = lua_gettop(L);
bool result = false;
int n = lua_gettop(L);
bool result = false;
double d = 0;
if (n == 1) {
if (lua_isinteger(L, 1)) {
_fontQuality = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); _fontQuality = (int)d;
result = true;
}
}
@ -850,21 +842,23 @@ int apiFontQuality(lua_State *L) {
int apiFontSelect(lua_State *L) {
int n = lua_gettop(L);
int id = -1;
double d = 0;
bool result = false;
FontT *font = NULL;
if (n == 1) {
if (lua_isinteger(L, 1)) {
id = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1);
id = (int)d;
HASH_FIND_INT(_fontList, &id, font);
if (!font) utilDie("No font at index %d in apiSpriteGetWidth.", id);
if (!font) luaDie(L, "fontSelect", "No font at index %d in apiSpriteGetWidth.", id);
_fontCurrent = font;
result = true;
}
}
if (result) {
luaTrace(L, "fontSelect", "%d", _fontCurrent);
luaTrace(L, "fontSelect", "%d", _fontCurrent->id);
} else {
luaTrace(L, "fontSelect", "Failed!");
}
@ -900,17 +894,17 @@ int apiFontToSprite(lua_State *L) {
}
if (!textSurface) {
utilDie("Font surface is null!");
luaDie(L, "fontToSprite", "Font surface is null!");
} else {
SDL_SetSurfaceBlendMode(textSurface, SDL_BLENDMODE_BLEND);
// Transparent index is 0
SDL_SetColorKey(textSurface, true, 0);
// Create spirte
sprite = (SpriteT *)calloc(1, sizeof(SpriteT));
if (!sprite) utilDie("Unable to allocate new text sprite.");
if (!sprite) luaDie(L, "fontToSprite", "Unable to allocate new text sprite.");
sprite->texture = SDL_CreateTextureFromSurface(_renderer, textSurface);
SDL_FreeSurface(textSurface);
if (!sprite->texture) utilDie("%s", SDL_GetError());
if (!sprite->texture) luaDie(L, "fontToSprite", "%s", SDL_GetError());
sprite->id = _nextSpriteId;
result = _nextSpriteId++;
HASH_ADD_INT(_spriteList, id, sprite);
@ -945,7 +939,7 @@ int apiOverlayGetHeight(lua_State *L) {
int x;
int y;
if (SDL_QueryTexture(_overlay, &format, &access, &x, &y) < 0) utilDie("%s", SDL_GetError());
if (SDL_QueryTexture(_overlay, &format, &access, &x, &y) < 0) luaDie(L, "overlayGetHeight", "%s", SDL_GetError());
luaTrace(L, "overlayGetHeight", "%d", y);
@ -960,7 +954,7 @@ int apiOverlayGetWidth(lua_State *L) {
int x;
int y;
if (SDL_QueryTexture(_overlay, &format, &access, &x, &y) < 0) utilDie("%s", SDL_GetError());
if (SDL_QueryTexture(_overlay, &format, &access, &x, &y) < 0) luaDie(L, "overlayGetWidth", "%s", SDL_GetError());
luaTrace(L, "overlayGetWidth", "%d", x);
@ -973,9 +967,9 @@ int apiOverlayPrint(lua_State *L) {
int n = lua_gettop(L);
if (n == 3) {
if (lua_isinteger(L, 1)) {
if (lua_isinteger(L, 2)) {
if (lua_isinteger(L, 3)) {
if (lua_isnumber(L, 1)) {
if (lua_isnumber(L, 2)) {
if (lua_isnumber(L, 3)) {
//***TODO*** g_pSingeIn->draw_string((char *)lua_tostring(L, 3), lua_tonumber(L, 1), lua_tonumber(L, 2), g_se_surface);
}
}
@ -995,10 +989,10 @@ int apiSoundLoad(lua_State *L) {
if (n == 1) {
if (lua_isstring(L, 1)) {
sound = (SoundT *)calloc(1, sizeof(SoundT));
if (!sound) utilDie("Unable to allocate new sound.");
if (!sound) luaDie(L, "soundLoad", "Unable to allocate new sound.");
name = lua_tostring(L, 1);
sound->chunk = Mix_LoadWAV(name);
if (!sound->chunk) utilDie("%s", Mix_GetError());
if (!sound->chunk) luaDie(L, "soundLoad", "%s", Mix_GetError());
sound->id = _nextSoundId;
result = _nextSoundId++;
HASH_ADD_INT(_soundList, id, sound);
@ -1020,14 +1014,15 @@ int apiSoundPlay(lua_State *L) {
int n = lua_gettop(L);
int result = -1;
int id = -1;
double d = 0;
SoundT *sound = NULL;
if (n == 1) {
if (lua_isinteger(L, 1)) {
id = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); id = (int)d;
// Get our sound structure
HASH_FIND_INT(_soundList, &id, sound);
if (!sound) utilDie("No sound at index %d in apiSoundPlay.", id);
if (!sound) luaDie(L, "soundPlay", "No sound at index %d in apiSoundPlay.", id);
// Play it
result = Mix_PlayChannel(-1, sound->chunk, 0);
Mix_Volume(result, _effectsVolume * 2);
@ -1058,12 +1053,13 @@ int apiSoundPause(lua_State *L) {
// --rdg
int n = lua_gettop(L);
bool result = false;
int channel = -1;
double d = 0;
bool result = false;
if (n == 1) {
if (lua_isinteger(L, 1)) {
channel = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); channel = (int)d;
Mix_Pause(channel);
result = true;
}
@ -1094,12 +1090,13 @@ int apiSoundResume(lua_State *L) {
// --rdg
int n = lua_gettop(L);
bool result = false;
int channel = -1;
double d = 0;
bool result = false;
if (n == 1) {
if (lua_isinteger(L, 1)) {
channel = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); channel = (int)d;
Mix_Resume(channel);
result = true;
}
@ -1129,12 +1126,13 @@ int apiSoundIsPlaying(lua_State *L) {
// --rdg
int n = lua_gettop(L);
bool result = false;
int channel = -1;
double d = 0;
bool result = false;
if (n == 1) {
if (lua_isinteger(L, 1)) {
channel = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); channel = (int)d;
result = (bool)Mix_Playing(channel);
}
}
@ -1166,12 +1164,13 @@ int apiSoundStop(lua_State *L) {
// --rdg
int n = lua_gettop(L);
bool result = false;
int channel = -1;
double d = 0;
bool result = false;
if (n == 1) {
if (lua_isinteger(L, 1)) {
channel = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); channel = (int)d;
Mix_HaltChannel(channel);
result = true;
}
@ -1199,18 +1198,19 @@ int apiSoundSetVolume(lua_State *L) {
//
// --rdg
int n = lua_gettop(L);
bool result = false;
int thisValue = 0;
int n = lua_gettop(L);
int thisValue = 0;
double d = 0;
bool result = false;
if (n == 1) {
if (lua_isinteger(L, 1)) {
thisValue = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); thisValue = (int)d;
if (thisValue >= 0 && thisValue <= AUDIO_MAX_VOLUME) {
_effectsVolume = thisValue;
Mix_Volume(-1, _effectsVolume * 2);
} else {
utilDie("Invalid sound volume value.");
luaDie(L, "soundSetVolume", "Invalid sound volume value.");
}
}
}
@ -1262,19 +1262,20 @@ int apiSoundFullStop(lua_State *L) {
int apiSpriteDraw(lua_State *L) {
int n = lua_gettop(L);
int id = -1;
double d = 0;
SpriteT *sprite = NULL;
SDL_Rect dest;
if (n == 3) {
if (lua_isinteger(L, 1)) {
if (lua_isinteger(L, 2)) {
if (lua_isinteger(L, 3)) {
id = (int)lua_tointeger(L, 1);
dest.x = (int)lua_tointeger(L, 2);
dest.y = (int)lua_tointeger(L, 3);
if (lua_isnumber(L, 1)) {
if (lua_isnumber(L, 2)) {
if (lua_isnumber(L, 3)) {
d = lua_tonumber(L, 1); dest.x = (int)d;
d = lua_tonumber(L, 2); dest.y = (int)d;
d = lua_tonumber(L, 3); id = (int)d;
HASH_FIND_INT(_spriteList, &id, sprite);
if (!sprite) utilDie("No sprite at index %d in apiSpriteGetWidth.", id);
if (SDL_QueryTexture(sprite->texture, NULL, NULL, &dest.w, &dest.h) < 0) utilDie("%s", SDL_GetError());
if (!sprite) luaDie(L, "spriteDraw", "No sprite at index %d in apiSpriteGetWidth.", id);
if (SDL_QueryTexture(sprite->texture, NULL, NULL, &dest.w, &dest.h) < 0) luaDie(L, "spriteDraw", "%s", SDL_GetError());
SDL_RenderCopy(_renderer, sprite->texture, NULL, &dest);
}
}
@ -1298,16 +1299,17 @@ int apiSpriteGetHeight(lua_State *L) {
int access;
int x;
int y;
double d;
Uint32 format;
SpriteT *sprite = NULL;
if (n == 1) {
if (lua_isstring(L, 1)) {
id = (int)lua_tointeger(L, 1);
d = lua_tonumber(L, 1); id = (int)d;
// Get our sprite structure
HASH_FIND_INT(_spriteList, &id, sprite);
if (!sprite) utilDie("No sprite at index %d in apiSpriteGetWidth.", id);
if (SDL_QueryTexture(sprite->texture, &format, &access, &x, &y) < 0) utilDie("%s", SDL_GetError());
if (!sprite) luaDie(L, "spriteGetHeight", "No sprite at index %d in apiSpriteGetWidth.", id);
if (SDL_QueryTexture(sprite->texture, &format, &access, &x, &y) < 0) luaDie(L, "spriteGetHeight", "%s", SDL_GetError());
result = y;
}
}
@ -1330,16 +1332,21 @@ int apiSpriteGetWidth(lua_State *L) {
int access;
int x;
int y;
double d;
Uint32 format;
SpriteT *sprite = NULL;
lua_Debug ar;
lua_getstack(L, 1, &ar);
lua_getinfo(L, "nSl", &ar);
if (n == 1) {
if (lua_isstring(L, 1)) {
id = (int)lua_tointeger(L, 1);
d = lua_tonumber(L, 1); id = (int)d;
// Get our sprite structure
HASH_FIND_INT(_spriteList, &id, sprite);
if (!sprite) utilDie("No sprite at index %d in apiSpriteGetWidth.", id);
if (SDL_QueryTexture(sprite->texture, &format, &access, &x, &y) < 0) utilDie("%s", SDL_GetError());
if (!sprite) luaDie(L, "spriteGetWidth", "No sprite at index %d in apiSpriteGetWidth.", id);
if (SDL_QueryTexture(sprite->texture, &format, &access, &x, &y) < 0) luaDie(L, "spriteGetWidth", "%s", SDL_GetError());
result = x;
}
}
@ -1366,12 +1373,12 @@ int apiSpriteLoad(lua_State *L) {
if (lua_isstring(L, 1)) {
name = lua_tostring(L, 1);
image = IMG_Load(name);
if (!image) utilDie("%s", IMG_GetError());
if (!image) luaDie(L, "spriteLoad", "%s", IMG_GetError());
sprite = (SpriteT *)calloc(1, sizeof(SpriteT));
if (!sprite) utilDie("Unable to allocate new sprite.");
if (!sprite) luaDie(L, "spriteLoad", "Unable to allocate new sprite.");
sprite->texture = SDL_CreateTextureFromSurface(_renderer, image);
SDL_FreeSurface(image);
if (!sprite->texture) utilDie("%s", SDL_GetError());
if (!sprite->texture) luaDie(L, "spriteLoad", "%s", SDL_GetError());
sprite->id = _nextSpriteId;
result = _nextSpriteId++;
HASH_ADD_INT(_spriteList, id, sprite);
@ -1399,19 +1406,20 @@ int apiVldpGetHeight(lua_State *L) {
int apiVldpGetPixel(lua_State *L) {
int n = lua_gettop(L);
double d = 0;
bool result = false;
byte pixel[SDL_BYTESPERPIXEL(SDL_PIXELFORMAT_BGRA32)];
SDL_Rect rect;
if (n == 2) {
if (lua_isinteger(L, 1)) {
if (lua_isinteger(L, 2)) {
if (lua_isnumber(L, 1)) {
if (lua_isnumber(L, 2)) {
rect.h = 1;
rect.w = 1;
rect.x = (int)lua_tointeger(L, 1);
rect.y = (int)lua_tointeger(L, 2);
d = lua_tonumber(L, 1); rect.x = (int)d;
d = lua_tonumber(L, 2); rect.y = (int)d;
SDL_SetRenderTarget(_renderer, NULL);
if (SDL_RenderReadPixels(_renderer, &rect, SDL_PIXELFORMAT_BGRA32, pixel, SDL_BYTESPERPIXEL(SDL_PIXELFORMAT_BGRA32)) < 0) utilDie("%s", SDL_GetError());
if (SDL_RenderReadPixels(_renderer, &rect, SDL_PIXELFORMAT_BGRA32, pixel, SDL_BYTESPERPIXEL(SDL_PIXELFORMAT_BGRA32)) < 0) luaDie(L, "vldpGetPixel", "%s", SDL_GetError());
SDL_SetRenderTarget(_renderer, _overlay);
result = true;
}
@ -1477,11 +1485,12 @@ int apiKeyboardSetMode(lua_State *L) {
*
*/
int n = lua_gettop(L);
int n = lua_gettop(L);
double d = 0;
if (n == 1) {
if (lua_isinteger(L, 1)) {
_keyboardMode = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); _keyboardMode = (int)d;
result = true;
}
}
@ -1532,12 +1541,13 @@ int apiMouseSetMode(lua_State *L) {
//
// --rdg
int n = lua_gettop(L);
bool result = false;
int n = lua_gettop(L);
double d = 0;
bool result = false;
if (n == 1) {
if (lua_isinteger(L, 1)) {
_mouseMode = (int)lua_tointeger(L, 1);
if (lua_isnumber(L, 1)) {
d = lua_tonumber(L, 1); _mouseMode = (int)d;
result = true;
}
}
@ -1708,6 +1718,7 @@ void callLua(const char *func, const char *sig, ...) {
int narg;
int nres;
int popCount;
double d;
const int top = lua_gettop(_luaContext);
va_start(vl, sig);
@ -1771,10 +1782,10 @@ void callLua(const char *func, const char *sig, ...) {
break;
case 'i': // Int
if (!lua_isinteger(_luaContext, nres)) {
if (!lua_isnumber(_luaContext, nres)) {
utilDie("Wrong result type");
}
*va_arg(vl, int *) = (int)lua_tointeger(_luaContext, nres);
d = lua_tonumber(_luaContext, nres); *va_arg(vl, int *) = (int)d;
break;
case 's': // String
@ -1802,6 +1813,26 @@ void channelFinished(int channel) {
}
void luaDie(lua_State *L, char *method, char *fmt, ...) {
va_list args;
lua_Debug ar;
char *string1 = NULL;
char *string2 = NULL;
lua_getstack(L, 1, &ar);
lua_getinfo(L, "nSl", &ar);
string1 = utilCreateString("%d:%s: ", ar.currentline, method);
if (!string1) utilDie("Unable to allocate first trace string.");
va_start(args, fmt);
string2 = utilCreateStringVArgs(fmt, args);
if (!string2) utilDie("Unable to allocate second trace string.");
va_end(args);
utilTrace("%s%s", string1, string2);
utilDie("%s%s", string1, string2);
// Can't free strings - we never get here.
}
int luaError(lua_State *L) {
lua_Debug ar;
int level = 0;
@ -1821,6 +1852,52 @@ int luaError(lua_State *L) {
}
void luaTrace(lua_State *L, char *method, char *fmt, ...) {
va_list args;
lua_Debug ar;
char *string1 = NULL;
char *string2 = NULL;
if (utilTraceFile) {
lua_getstack(L, 1, &ar);
lua_getinfo(L, "nSl", &ar);
string1 = utilCreateString("%d:%s: ", ar.currentline, method);
if (!string1) utilDie("Unable to allocate first trace string.");
va_start(args, fmt);
string2 = utilCreateStringVArgs(fmt, args);
if (!string2) utilDie("Unable to allocate second trace string.");
va_end(args);
utilSay("%s%s", string1, string2);
utilTrace("%s%s", string1, string2);
free(string2);
free(string1);
}
}
void makeZeroTransparent(SDL_Texture *texture) {
int x;
int y;
int w;
int h;
int access;
Uint32 format;
Uint32 *pixel = NULL; // Four bytes in BGRA order
size_t pixelSize = 0;
static size_t lastPixelsSize = 0;
if (SDL_QueryTexture(texture, &format, &access, &w, &h) < 0) utilDie("%s", SDL_GetError());
pixelSize = (size_t)(w * h * 4);
if (pixelSize != lastPixelsSize) {
// Reallocate buffer
if (_pixelBuffer) free(_pixelBuffer);
lastPixelsSize = pixelSize;
_pixelBuffer = malloc(lastPixelsSize);
if (!_pixelBuffer) utilDie("Unable to allocate pixels for transparency replacement.");
}
}
void processKey(bool down, int keysym) {
int move;
@ -2003,6 +2080,21 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) {
if (_videoHandle < 0) utilDie("Unable to load video file: %s", _confVideoFile);
videoSetVolume(_videoHandle, _confVolumeVldp, _confVolumeVldp);
// Should we resize the window?
//***TODO***
// Create overlay texture
_overlayScaleX = 0.5;
_overlayScaleY = 0.5;
x = (int)(videoGetWidth(_videoHandle) * _overlayScaleX);
y = (int)(videoGetHeight(_videoHandle) * _overlayScaleY);
_overlay = SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, x, y);
if (_overlay == NULL) utilDie("%s", SDL_GetError());
SDL_SetTextureBlendMode(_overlay, SDL_BLENDMODE_BLEND);
if (_confStretchVideo) {
SDL_RenderSetLogicalSize(_renderer, x, y);
}
// Mouse setup
_mouseCount = ManyMouse_Init();
if (_mouseCount < 1) utilDie("No mice detected.");
@ -2014,20 +2106,12 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) {
for (x=0; x<_mouseCount; x++) {
strncpy(_mice[x].name, ManyMouse_DeviceName((unsigned)x), sizeof(_mice[x].name));
_mice[x].name[sizeof(_mice[x].name) - 1] = 0;
_mice[x].x = videoGetWidth(_videoHandle) / 2;
_mice[x].y = videoGetHeight(_videoHandle) / 2;
_mice[x].x = (int)(videoGetWidth(_videoHandle) * _overlayScaleX);
_mice[x].y = (int)(videoGetHeight(_videoHandle) * _overlayScaleY);
}
SDL_SetWindowGrab(_window, SDL_TRUE);
SDL_ShowCursor(SDL_DISABLE);
// Create overlay texture
_overlay = SDL_CreateTexture(_renderer, SDL_PIXELFORMAT_BGRA32, SDL_TEXTUREACCESS_TARGET, videoGetWidth(_videoHandle), videoGetHeight(_videoHandle));
if (_overlay == NULL) utilDie("%s", SDL_GetError());
SDL_SetTextureBlendMode(_overlay, SDL_BLENDMODE_BLEND);
if (_confStretchVideo) {
SDL_RenderSetLogicalSize(_renderer, videoGetWidth(_videoHandle), videoGetHeight(_videoHandle));
}
// Set volume
_effectsVolume = (int)((float)AUDIO_MAX_VOLUME * (float)_confVolumeNonVldp * (float)0.01);
Mix_Volume(-1, _effectsVolume * 2);
@ -2058,6 +2142,34 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) {
processKey(false, event.key.keysym.sym);
break;
case SDL_MOUSEMOTION:
if ((_mouseEnabled) && (_mouseMode == MOUSE_SINGLE)) {
x = (int)(event.motion.x * _overlayScaleX);
y = (int)(event.motion.y * _overlayScaleY);
xr = (int)(event.motion.xrel * _overlayScaleX);
yr = (int)(event.motion.yrel * _overlayScaleY);
callLua("onMouseMoved", "iiiii", x, y, xr, yr, NOMOUSE);
}
break;
case SDL_MOUSEBUTTONDOWN:
for (x=0; x<MOUSE_ARRAY_SIZE; x++) {
if (event.button.button == x) {
callLua("onInputPressed", "ii", mouseButtonMap[x], NOMOUSE);
break;
}
}
break;
case SDL_MOUSEBUTTONUP:
for (x=0; x<MOUSE_ARRAY_SIZE; x++) {
if (event.button.button == x) {
callLua("onInputReleased", "ii", mouseButtonMap[x], NOMOUSE);
break;
}
}
break;
case SDL_QUIT:
_running = 0;
break;
@ -2066,6 +2178,10 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) {
// Mouse Event Loop
while (ManyMouse_PollEvent(&mouseEvent)) {
// Just run out the event queue if we're not using ManyMouse
if ((!_mouseEnabled) || (_mouseMode == MOUSE_SINGLE)) continue;
// Has this one been unplugged?
if (mouseEvent.device >= (unsigned)_mouseCount) continue;
mouse = &_mice[mouseEvent.device];
@ -2081,14 +2197,16 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) {
break;
}
// Clamp to video size
if (mouse->relx < 0) mouse->relx = 0;
if (mouse->relx > videoGetWidth(_videoHandle)) mouse->relx = videoGetWidth(_videoHandle) - 1;
if (mouse->rely < 0) mouse->rely = 0;
if (mouse->rely > videoGetHeight(_videoHandle)) mouse->rely = videoGetHeight(_videoHandle) - 1;
x *= _overlayScaleX;
y *= _overlayScaleY;
xr *= _overlayScaleX;
yr *= _overlayScaleY;
x = videoGetWidth(_videoHandle);
y = videoGetHeight(_videoHandle);
if (mouse->relx < 0) mouse->relx = 0;
if (mouse->relx >= x) mouse->relx = x - 1;
if (mouse->rely < 0) mouse->rely = 0;
if (mouse->rely >= y) mouse->rely = y - 1;
x = (int)(mouse->x * _overlayScaleX);
y = (int)(mouse->y * _overlayScaleY);
xr = (int)(mouse->relx * _overlayScaleX);
yr = (int)(mouse->relx * _overlayScaleY);
callLua("onMouseMoved", "iiiii", x, y, xr, yr, mouseEvent.device);
break;
@ -2102,10 +2220,11 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) {
mouse->y += mouseEvent.value;
break;
}
x *= _overlayScaleX;
y *= _overlayScaleY;
xr *= _overlayScaleX;
yr *= _overlayScaleY;
x = (int)(mouse->x * _overlayScaleX);
y = (int)(mouse->y * _overlayScaleY);
xr = (int)(mouse->relx * _overlayScaleX);
yr = (int)(mouse->relx * _overlayScaleY);
utilSay("Mouse %d: Absolute %d, %d", mouseEvent.device, x, y);
callLua("onMouseMoved", "iiiii", x, y, xr, yr, mouseEvent.device);
break;
@ -2169,7 +2288,8 @@ void singe(SDL_Window *window, SDL_Renderer *renderer) {
//***TODO*** Handle overlay and blank disk frames
SDL_SetRenderTarget(_renderer, NULL);
SDL_RenderCopy(_renderer, videoTexture, NULL, NULL);
//SDL_RenderCopy(_renderer, _overlay, NULL, NULL);
//makeZeroTransparent(_overlay);
SDL_RenderCopy(_renderer, _overlay, NULL, NULL);
SDL_RenderPresent(_renderer);
_refreshDisplay = false;
}

View file

@ -253,9 +253,11 @@ int videoLoad(char *filename, char *indexPath, bool stretchVideo, SDL_Renderer *
if (FFMS_IndexBelongsToFile(index, filename, &v->errInfo)) {
FFMS_DestroyIndex(index);
index = NULL;
/*
utilSay("Cached index is invalid.");
} else {
utilSay("Loaded cached index.");
*/
}
}
if (!index) {