all repos — hyperkaos @ main

lightweight modular puzzle/adventure game engine in C with SDL 1.2

TextBox.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "config.h"

#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"

typedef struct room Room;
typedef struct kaos Kaos;
typedef struct scene Scene;
typedef struct hyperKaos HyperKaos;

#include "extern.h"

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 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 A_BUTTON:
                if (self->cursor < 15) self->cursor += 3;
                break;
              case FS_BUTTON:
                toggleFullscreen();
                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 A_BUTTON:
                if (self->scroll + 1 != self->lines)
                {
                  self->cursor = 0;
                  self->scroll++;
                  self->scrollFrom += 4;
                }
                else *textIsRelevent = 0;
                break;
              case FS_BUTTON:
                  toggleFullscreen();
                break;

            default: break;
          }
        }
      }
    }
}

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);

    textBoxInput(self, &textIsRelevent);

    frameAdvance();
  }
  self->scroll = 0;
  self->cursor = 0;
  self->scrollFrom = 0;
}