From bff466bba7627f2f724663e6914951b8684f5b65 Mon Sep 17 00:00:00 2001 From: Derek Stevens Date: Mon, 24 Sep 2018 13:47:46 -0700 Subject: [PATCH] Implemented input handling for scenes (skip, fullscreen, quit), and pulled input handling for textboxes out into its own function --- Scene.c | 36 +++++++++++++++++ Scene.h | 1 + TextBox.c | 114 ++++++++++++++++++++++++++++-------------------------- TextBox.h | 1 + 4 files changed, 98 insertions(+), 54 deletions(-) diff --git a/Scene.c b/Scene.c index 95ed5ed..e3f9fba 100644 --- a/Scene.c +++ b/Scene.c @@ -68,6 +68,41 @@ void buildScene(Scene* self, SLayer* sprite) self->nSprites++; } +void sceneInput(Scene* self, int* counter) +{ + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_KEYDOWN: + switch (event.key.keysym.sym) + { + case SDLK_j: + *counter = self->time - 1; + break; + case SDLK_f: + toggleFullscreen(); + break; + default: break; + } + break; + case SDL_QUIT: + if (playing) + { + playing = 0; + quit = 1; + } + else + { + *counter = self->time - 1; + playing = 0; + quit = 1; + } + break; + } + } +} + void playScene(Scene* self) { int i, j; @@ -88,6 +123,7 @@ void playScene(Scene* self) for (i = 0; i < self->time; i++) { timeStart(fps); + sceneInput(self, &i); for (j = 0; j < self->nSprites; j++) { applySurface(self->sprites[j]->x, self->sprites[j]->y, self->sprites[j]->sprite, screen, NULL); diff --git a/Scene.h b/Scene.h index 865ce4b..ed3f5fd 100644 --- a/Scene.h +++ b/Scene.h @@ -27,4 +27,5 @@ void deleteSLayer(SLayer* target); Scene* newScene(int in, int out, int time, SDL_Color incolor, SDL_Color outcolor); void buildScene(Scene* self, SLayer* sprite); void playScene(Scene* self); +void sceneInput(Scene* self, int* counter); void deleteScene(Scene* target); \ No newline at end of file diff --git a/TextBox.c b/TextBox.c index 284cfef..96ed9be 100644 --- a/TextBox.c +++ b/TextBox.c @@ -64,6 +64,65 @@ void addText(TextBox* self, char* text) self->lines++; } +void textBoxInput(TextBox* self, int* textIsRelevent) +{ + if (self->cursor != 17) + { + self->cursor++; + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: quit=1; playing = 0; *textIsRelevent = 0; break; + case SDL_KEYDOWN: + switch (event.key.keysym.sym) + { + case SDLK_j: + if (self->cursor < 15) self->cursor += 3; + break; + default: break; + } + } + } + } + + + if (self->cursor == 17 && self->scroll+1 != self->lines && (self->scroll+1)%4 != 0) + { + self->cursor = 0; + self->scroll++; + } + if (self->cursor == 17 && ((self->scroll+1)%4 == 0 || self->scroll+1 == self->lines)) + { + applySurface(274, 120, nextArrow, screen, NULL); + while (SDL_PollEvent(&event)) + { + switch (event.type) + { + case SDL_QUIT: quit = 1; playing = 0; *textIsRelevent = 0; break; + case SDL_KEYDOWN: + switch (event.key.keysym.sym) + { + case SDLK_j: + if (self->scroll + 1 != self->lines) + { + self->cursor = 0; + self->scroll++; + self->scrollFrom += 4; + } + else *textIsRelevent = 0; + break; + case SDLK_f: + toggleFullscreen(); + break; + + default: break; + } + } + } + } +} + void displayTextBox(TextBox* self) { int textIsRelevent = 1; @@ -92,61 +151,8 @@ void displayTextBox(TextBox* self) textScroller.w = self->cursor*10; applySurface(106 + offset, 64+(14*(self->scroll%4)), self->message[self->scroll], screen, &textScroller); - if (self->cursor != 17) - { - self->cursor++; - while (SDL_PollEvent(&event)) - { - switch (event.type) - { - case SDL_QUIT: quit=1; playing = 0; textIsRelevent = 0; break; - case SDL_KEYDOWN: - switch (event.key.keysym.sym) - { - case SDLK_j: - if (self->cursor < 15) self->cursor += 3; - break; - default: break; - } - } - } - } + textBoxInput(self, &textIsRelevent); - - if (self->cursor == 17 && self->scroll+1 != self->lines && (self->scroll+1)%4 != 0) - { - self->cursor = 0; - self->scroll++; - } - if (self->cursor == 17 && ((self->scroll+1)%4 == 0 || self->scroll+1 == self->lines)) - { - applySurface(274, 120, nextArrow, screen, NULL); - while (SDL_PollEvent(&event)) - { - switch (event.type) - { - case SDL_QUIT: quit = 1; playing = 0; textIsRelevent = 0; break; - case SDL_KEYDOWN: - switch (event.key.keysym.sym) - { - case SDLK_j: - if (self->scroll + 1 != self->lines) - { - self->cursor = 0; - self->scroll++; - self->scrollFrom += 4; - } - else textIsRelevent = 0; - break; - case SDLK_f: - toggleFullscreen(); - break; - - default: break; - } - } - } - } SDL_Flip(screen); timeDilation(); diff --git a/TextBox.h b/TextBox.h index 1ec44c1..c87e0b9 100644 --- a/TextBox.h +++ b/TextBox.h @@ -15,3 +15,4 @@ void deleteTextBox(TextBox* target); void addText(TextBox* self, char* text); void displayTextBox(TextBox* self); +void textBoxInput(TextBox* self, int* textIsRelevent);