hyperkaos/TextBox.c

168 lines
No EOL
3.8 KiB
C

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include "Engine.h"
#include "Timer.h"
#include "Player.h"
#include "TextBox.h"
extern int quit;
extern int playing;
extern int actionbutton;
extern SDL_Event event;
extern SDL_Surface* screen;
extern Timer fps;
extern SDL_Rect* textScroller;
extern SDL_Surface* textBoxBG;
extern SDL_Surface* nextArrow;
extern TTF_Font* font;
extern SDL_Color textColor;
extern Player* hero;
TextBox* newTextBox()
{
TextBox* self = malloc(sizeof(TextBox));
self->portrait = NULL;
self->message = malloc(4*sizeof(SDL_Surface*));
self->lines = 0;
self->cursor = 0;
self->scroll = 0;
self->scrollFrom = 0;
return self;
}
TextBox* newGTextBox(SDL_Surface* image)
{
TextBox* self = malloc(sizeof(TextBox));
self->portrait = image;
self->message = malloc(4*sizeof(SDL_Surface*));
self->lines = 0;
self->cursor = 0;
self->scroll = 0;
self->scrollFrom = 0;
return self;
}
void deleteTextBox(TextBox* target)
{
int i;
if (target->portrait) SDL_FreeSurface(target->portrait);
for (i = 0; i < target->lines; i++)
{
SDL_FreeSurface(target->message[i]);
}
free(target->message);
}
void addText(TextBox* self, char* text)
{
int i;
if ( self->lines != 0 && self->lines%4 == 0)
{
SDL_Surface** templines = (SDL_Surface**)malloc((self->lines+4)*sizeof(SDL_Surface*));
for (i = 0; i < self->lines; i++)
templines[i] = self->message[i];
free(self->message);
self->message = templines;
templines = NULL;
}
self->message[self->lines] = TTF_RenderText_Solid(font, text, textColor);
self->lines++;
}
void displayTextBox(TextBox* self)
{
int textIsRelevent = 1;
int offset = -32;
int i;
SDL_Rect textScroller = {0, 0, 0, 14};
actionbutton = 0;
while (textIsRelevent)
{
timeStart(fps);
renderBackground();
renderForeground();
applySurface(22, 51, textBoxBG, screen, NULL);
if (self->portrait)
{
applySurface(32, 58, self->portrait, screen, NULL);
offset = 0;
}
for (i = self->scrollFrom; i < self->scroll; i++)
{
applySurface(106 + offset, 64+(14*(i%4)), self->message[i], screen, NULL);
}
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;
}
}
}
}
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();
}
self->scroll = 0;
self->cursor = 0;
self->scrollFrom = 0;
}