Fix image index underflow when navigating past boundaries

This commit is contained in:
2026-09-18 09:26:17 -05:00
parent 1f416ea8ae
commit 0eae2fa257

View File

@@ -153,6 +153,18 @@ struct App {
LoadImageList(curFile); LoadImageList(curFile);
} }
void Prev()
{
this->imageIndex--;
if(this->imageIndex >= this->filenames.size())
this->imageIndex=this->filenames.size()-1;
}
void Next()
{
this->imageIndex++;
if(this->imageIndex >= this->filenames.size())
this->imageIndex=0;
}
bool Update() { bool Update() {
if(!window || !renderer) return false; if(!window || !renderer) return false;
SDL_Event evt; SDL_Event evt;
@@ -188,22 +200,28 @@ struct App {
break; break;
case SDL_SCANCODE_PAGEUP: case SDL_SCANCODE_PAGEUP:
{ {
SetNewImage(this->imageIndex-20); for(size_t i = 0; i < 20; i++)
Prev();
SetNewImage(this->imageIndex);
} }
break; break;
case SDL_SCANCODE_PAGEDOWN: case SDL_SCANCODE_PAGEDOWN:
{ {
SetNewImage(this->imageIndex+20); for(size_t i = 0; i < 20; i++)
Next();
SetNewImage(this->imageIndex);
} }
break; break;
case SDL_SCANCODE_LEFT: case SDL_SCANCODE_LEFT:
{ {
SetNewImage(this->imageIndex-1); Prev();
SetNewImage(this->imageIndex);
} }
break; break;
case SDL_SCANCODE_RIGHT: case SDL_SCANCODE_RIGHT:
{ {
SetNewImage(this->imageIndex+1); Next();
SetNewImage(this->imageIndex);
} }
break; break;
case SDL_SCANCODE_F: case SDL_SCANCODE_F:
@@ -218,6 +236,8 @@ struct App {
} }
} }
break; break;
default:
break;
} }
} }
break; break;