-
Notifications
You must be signed in to change notification settings - Fork 3
/
SDLTWE.c
248 lines (213 loc) · 10.8 KB
/
SDLTWE.c
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/****************************************************************************\
* *
* SDLTWE.c *
* *
* SDL Text Windows Engine *
* *
* (c) 2012-2019 by CH, Copyright 2019-2025 Valerio Messina *
* *
* V 2.10 - 20250106 *
* *
* SDLTWE.c is part of Wilderland - A Hobbit Environment *
* Wilderland is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 2 of the License, or *
* (at your option) any later version. *
* *
* Wilderland is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with Wilderland. If not, see <http://www.gnu.org/licenses/>. *
* *
\****************************************************************************/
/*** INCLUDES ***************************************************************/
#include <stdio.h>
#include "WL.h"
#include "SDLTWE.h"
#include "GlobalVars.h"
/*** GLOBAL VARIABLES *******************************************************/
int delay=22; // with SDL_RENDERER_ACCELERATED min 22 ms to avoid flickering
byte firstEditable = 0; // column by SDLTWE_PrintCharTextWindow
/****************************************************************************\
* SDLTWE_DrawTextWindowFrame *
* *
* Draw a frame around a text window. *
* The caller is responsible for sensible values! *
\****************************************************************************/
void SDLTWE_DrawTextWindowFrame(struct TextWindowStruct* TW, int BorderWidth, color_t Color) {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
SDL_Rect rect;
SDL_GetRenderDrawColor(renPtr, &r, &g, &b, &a); // save prev color
SDL_SetRenderDrawColor(renPtr, components(Color));
for (int i=1; i<=BorderWidth; i++) { // 1,2,3,4
rect.x = TW->rect.x - i; // 7,6,5,4
rect.y = TW->rect.y - i;
rect.w = TW->rect.w + 2*i; // 2,4,6,8
rect.h = TW->rect.h + 2*i;
SDL_RenderDrawRect(renPtr, &rect);
}
SDL_SetRenderDrawColor(renPtr, r, g, b, a); // restore prev color
} // SDLTWE_DrawTextWindowFrame()
/****************************************************************************\
* SDLTWE_Setpixel *
* *
\****************************************************************************/
void SDLTWE_SetPixel(struct TextWindowStruct* TW, int x, int y, color_t color) {
if (x>=TW->rect.w) return;
if (y>=TW->rect.h) return;
TW->framePtr[y*(TW->rect.w) + x] = color;
} // SDLTWE_SetPixel()
/****************************************************************************\
* SDLTWE_VerticalScrollUpOneLine *
* *
\****************************************************************************/
void SDLTWE_VerticalScrollUpOneLine(struct TextWindowStruct* TW, struct CharSetStruct* CS, color_t paper) {
int x, y;
//size_t plines = TW->rect.h - CS->Height; // pixel lines number - 1 char line
//memmove (&TW->framePtr[0], &TW->framePtr[CS->Height * TW->rect.w], plines * TW->rect.w * BYTESPERPIXEL); // move up Frame
int sp, dp;
for (uint8_t l=1; l<TW->rect.h/CS->Height; l++) { // lines 1 to 63
sp= l * CS->Height * TW->rect.w;
dp=(l-1) * CS->Height * TW->rect.w;
//printf("l:%02u sp:%06d, dp:%06d sz:%04u\n", l, sp, dp, CS->Height * TW->pitch);
memcpy (&TW->framePtr[dp], &TW->framePtr[sp], CS->Height * TW->pitch); // move up one char line
}
for (y = 0; y < CS->Height; y++) // fill last char line with paper
for (x = 0; x < TW->rect.w; x++)
SDLTWE_SetPixel(TW, x, TW->rect.h - CS->Height + y, paper);
} // SDLTWE_VerticalScrollUpOneLine()
/****************************************************************************\
* SDLTWE_PrintRawCharTextWindow *
* *
* Prints a raw character to a text window. *
\****************************************************************************/
void SDLTWE_PrintRawCharTextWindow(struct TextWindowStruct* TW, char a, struct CharSetStruct* CS, color_t ink, color_t paper) {
int i, j;
int CharIndex;
byte mask;
uint32_t pixm;
for (i = 0; i <= 7; i++) { // print char from CharSetStruct
mask = 0x80;
CharIndex = (a - CS->CharMin) * CS->Height;
for (j = 0; j <= 7; j++, mask >>= 1) {
if (CS->Bitmap[CharIndex + i] & mask)
pixm = ink;
else
pixm = paper;
TW->framePtr[(TW->CurrentPrintPosY + i) * TW->rect.w + TW->CurrentPrintPosX + j] = pixm;
}
}
} // SDLTWE_PrintRawCharTextWindow()
/****************************************************************************\
* SDLTWE_PrintCharTextWindow *
* *
* Prints a character to a text window. The print coordinates are updated and *
* a vertical scroll is performed if necessary. *
* The following control characters are processed: *
* \n \r (both used as Newline, ie CR/LF) *
* \f (used as formfeed / CLS) *
* \b (used as backspace) *
\****************************************************************************/
void SDLTWE_PrintCharTextWindow(struct TextWindowStruct* TW, char a, struct CharSetStruct* CS, color_t ink, color_t paper) {
int i;
byte bs=0;
if (WL_DEBUG) {
if ((CS->Width != 8) || (CS->Height != 8)) {
fprintf(stderr, "SDLTWE: ERROR in PrintCharTextWindow. Only 8x8 pixel charsets are supported.\n");
return;
}
}
if (a == '\f') { // formfeed / CLS
for (i = 0; i < (TW->rect.h / CS->Height); i++) { // # of char lines
SDLTWE_VerticalScrollUpOneLine(TW, CS, paper);
SDL_UpdateTexture(TW->texPtr, NULL, TW->framePtr, TW->pitch); // copy Frame to Texture
SDL_RenderCopy(renPtr, TW->texPtr, NULL, &TW->rect); // Texture to renderer
SDL_RenderPresent(renPtr);
SDL_Delay(delay); // to avoid flickering
}
TW->CurrentPrintPosX = 0;
TW->CurrentPrintPosY = 0;
return;
}
if (TW->CurrentPrintPosY >= TW->rect.h) { // Scroll
SDLTWE_VerticalScrollUpOneLine(TW, CS, paper);
TW->CurrentPrintPosX = 0;
TW->CurrentPrintPosY -= CS->Height;
}
if (a == '\r' || a == '\n') { // 0x0D or 0x0A
TW->CurrentPrintPosX = 0;
TW->CurrentPrintPosY += CS->Height;
return;
}
if (a == '\b') { // Backspace 0x08
if (TW->CurrentPrintPosX > firstEditable*CS->Width) {
SDLTWE_PrintRawCharTextWindow(TW, ' ', CS, ink, paper); // del char to the right
TW->CurrentPrintPosX -= CS->Width;
a=0; // will be substituted
} else return;
bs=1;
}
// if required substitute characters not contained in the character set
if ((a < CS->CharMin) || (a > CS->CharMax)) {
if (CS->CharSubstitute)
a = CS->CharSubstitute;
else
return;
}
SDLTWE_PrintRawCharTextWindow(TW, a, CS, ink, paper);
if (bs==0) TW->CurrentPrintPosX += CS->Width;
if (TW->CurrentPrintPosX >= TW->rect.w) {
TW->CurrentPrintPosX = 0;
TW->CurrentPrintPosY += CS->Height;
}
} // SDLTWE_PrintCharTextWindow()
/****************************************************************************\
* SDLTWE_PrintString *
* *
\****************************************************************************/
void SDLTWE_PrintString(struct TextWindowStruct* TW, char* ps, struct CharSetStruct* CS, color_t ink, color_t paper) {
while (*ps)
SDLTWE_PrintCharTextWindow(TW, *ps++, CS, ink, paper);
} // SDLTWE_PrintString()
/****************************************************************************\
* SDLTWE_ReadCharSet *
* *
* Reads a binary character set. *
* *
* INPUT: CharSetFilename = Filename (incl. path) of data to load *
* CharSet = a place to store the data *
* RETURN: pointer to data read OR NULL if error occured *
\****************************************************************************/
byte* SDLTWE_ReadCharSet(char* CharSetFilename, byte* CharSet) {
FILE *fp;
long int FileLength;
size_t BytesRead;
fp = fopen(CharSetFilename, "rb");
if (!fp) {
fprintf(stderr, "ERROR in SDLWTE.c: can't open character set file '%s'\n", CharSetFilename);
return NULL;
}
fseek(fp, 0, SEEK_END);
FileLength = ftell(fp);
fseek(fp, 0, SEEK_SET);
if (WL_DEBUG) {
printf("SDLWTE: Reading character set with %li byte from %s ... ", FileLength, CharSetFilename);
}
BytesRead = fread(CharSet, 1, FileLength, fp);
fclose(fp);
if (FileLength != BytesRead) {
if (WL_DEBUG)
printf("ERROR!\n");
return NULL;
} else {
if (WL_DEBUG)
printf("read!\n");
return CharSet;
}
} // SDLTWE_ReadCharSet()