Got it to compile with SDL2 and SDL_ttf

This commit is contained in:
Joseph Ferano 2023-11-11 14:38:47 +07:00
parent 59473a4958
commit 983440079a
2 changed files with 127 additions and 83 deletions

View File

@ -1,10 +1,11 @@
P=bt P=bt
CFLAGS=-g -Wall -Wextra -pedantic -O0
CC=gcc CC=gcc
CFLAGS=-g -Wall -Wextra -pedantic -O0
LDLIBS=-lSDL2 -lSDL2_ttf
RM=rm -vf RM=rm -vf
bt: clean bt: clean
$(CC) $(CFLAGS) -lraylib $(P).c -o $(P) $(CC) $(CFLAGS) $(LDLIBS) $(P).c -o $(P)
clean: clean:
$(RM) $(P) $(RM) $(P)

161
bt.c
View File

@ -7,7 +7,12 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <raylib.h> #include <stdbool.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_keycode.h>
typedef struct scrollback { typedef struct scrollback {
float height; float height;
@ -40,22 +45,17 @@ void spawn(file_descriptors *fds) {
} }
} }
static float fontsize = 12; const int SCREEN_WIDTH = 900;
const int SCREEN_HEIGHT = 700;
Font *load_font(void) { const SDL_Color WHITE = {255, 255, 255, 255};
unsigned int file_size = 0; const SDL_Color BLACK = {0, 0, 0, 255};
char *font = "./fira.ttf"; const SDL_Color RED = {255, 0, 0, 255};
unsigned char *fontdata = LoadFileData(font, &file_size); const SDL_Color GREEN = {0, 255, 0, 255};
Font *fontDefault = calloc(1, sizeof(Font)); const SDL_Color BLUE = {0, 0, 255, 255};
fontDefault->baseSize = (int)fontsize; const SDL_Color YELLOW = {255, 255, 0, 255};
fontDefault->glyphCount = 95; const SDL_Color MAGENTA = {255, 0, 255, 255};
const SDL_Color SKYBLUE = {0, 255, 255, 255};
fontDefault->glyphs = LoadFontData(fontdata, (int)file_size, 16, 0, 95, FONT_DEFAULT); const SDL_Color RAYWHITE = {200, 200, 200, 255};
Image atlas = GenImageFontAtlas(fontDefault->glyphs, &fontDefault->recs, 95, 16, 4, 0);
fontDefault->texture = LoadTextureFromImage(atlas);
UnloadImage(atlas);
return fontDefault;
}
int read_pty(file_descriptors *fds, scrollback *sb) { int read_pty(file_descriptors *fds, scrollback *sb) {
ssize_t nread; ssize_t nread;
@ -90,10 +90,26 @@ int main(void) {
spawn(&fds); spawn(&fds);
const int screenWidth = 800; if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
const int screenHeight = 550; printf("error initializing SDL: %s\n", SDL_GetError());
return 1;
}
InitWindow(screenWidth, screenHeight, "basic term"); SDL_Window* window = SDL_CreateWindow( "bt",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_SHOWN );
if (window == NULL) {
printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
return 1;
}
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 130, 163, 255, 1);
TTF_Font* font = TTF_OpenFont( "./fira.ttf", 16);
struct winsize sz; struct winsize sz;
int result = ioctl(fds.master, TIOCGWINSZ, &sz); int result = ioctl(fds.master, TIOCGWINSZ, &sz);
@ -101,9 +117,8 @@ int main(void) {
sz.ws_row = 120; sz.ws_row = 120;
result = ioctl(fds.master, TIOCSWINSZ, &sz); result = ioctl(fds.master, TIOCSWINSZ, &sz);
SetTargetFPS(60); // TODO: It would be nice to figure out how to do this in SDL
// SetTargetFPS(60);
Font *fontDefault = load_font();
sb.capacity = 2048; sb.capacity = 2048;
sb.buf = malloc(sb.capacity); sb.buf = malloc(sb.capacity);
@ -123,26 +138,18 @@ int main(void) {
bool new_read = false; bool new_read = false;
bool new_char = false; bool new_char = false;
while (!WindowShouldClose()) { SDL_Event e;
float scroll_speed = 35.5f; bool quit = false;
sb.ypos += GetMouseWheelMoveV().y * scroll_speed; while (quit == false) {
sb.height = MeasureTextEx(*fontDefault, sb.buf, fontsize, 1).y; while (SDL_PollEvent( &e)) {
if (e.type == SDL_QUIT) {
quit = true;
} else if (e.type == SDL_KEYDOWN) {
if (new_read || new_char) {
if (sb.height - (float)abs((int)sb.ypos) + fontsize > (float)screenHeight) {
sb.ypos = -(sb.height - screenHeight) - fontsize;
} }
new_read = false; char key = e.key.keysym.sym;
new_char = false; // TODO: Translate this all to SDL2
} else { // int key = GetCharPressed();
if (sb.ypos > 0) {
sb.ypos = 0;
} else if (abs((int)sb.ypos) > sb.height - fontsize) {
sb.ypos = -(sb.height - fontsize);
}
}
int key = GetCharPressed();
while (key > 0) { while (key > 0) {
if ((key >= 32) && (key <= 125)) { if ((key >= 32) && (key <= 125)) {
buf[buf_len] = (char)key; buf[buf_len] = (char)key;
@ -152,11 +159,12 @@ int main(void) {
sb.buf[sb.length + 1] = '\0'; sb.buf[sb.length + 1] = '\0';
sb.length++; sb.length++;
} }
key = GetCharPressed(); // TODO: Don't remember why I was doing this
new_char = true; // key = GetCharPressed();
// new_char = true;
} }
if (IsKeyPressed(KEY_ENTER)) { if (key == SDLK_RETURN) {
sb.length -= buf_len; sb.length -= buf_len;
sb.buf[sb.length] = '\0'; sb.buf[sb.length] = '\0';
write(fds.master, buf, buf_len); write(fds.master, buf, buf_len);
@ -165,7 +173,7 @@ int main(void) {
buf_len = 0; buf_len = 0;
new_char = true; new_char = true;
} }
if (IsKeyPressed(KEY_BACKSPACE)) { if (key == SDLK_BACKSPACE) {
if (buf_len > 0) { if (buf_len > 0) {
buf_len--; buf_len--;
buf[buf_len] = '\0'; buf[buf_len] = '\0';
@ -174,17 +182,45 @@ int main(void) {
} }
new_char = true; new_char = true;
} }
}
float scroll_speed = 35.5f;
// TODO: We have to convert both these lines to SDL
// sb.ypos += GetMouseWheelMoveV().y * scroll_speed;
// sb.height = MeasureTextEx(*fontDefault, sb.buf, fontsize, 1).y;
SDL_Surface* surfaceMessage =
TTF_RenderText_Solid(font, "put your text here", WHITE);
// now you can convert it into a texture
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
// TODO: This is probably related to the font height and scrolling up, we just need
// the "fontsize" which I think is probably the height of the text
if (new_read || new_char) {
// if (sb.height - (float)abs((int)sb.ypos) + fontsize > (float)SCREEN_HEIGHT) {
// sb.ypos = -(sb.height - SCREEN_HEIGHT) - fontsize;
// }
new_read = false;
new_char = false;
} else {
// if (sb.ypos > 0) {
// sb.ypos = 0;
// } else if (abs((int)sb.ypos) > sb.height - fontsize) {
// sb.ypos = -(sb.height - fontsize);
// }
}
// Drawing // Drawing
BeginDrawing();
{ {
int nread = read_pty(&fds, &sb); int nread = read_pty(&fds, &sb);
if (nread > 0) { if (nread > 0) {
sb.length += nread; sb.length += nread;
new_read = true; new_read = true;
} }
ClearBackground(BLACK); SDL_RenderClear(renderer);
Color current_color = RAYWHITE; SDL_Color current_color = WHITE;
int col_max = 200; int col_max = 200;
char row_buf[col_max]; char row_buf[col_max];
int nrow = 0; int nrow = 0;
@ -196,8 +232,9 @@ int main(void) {
continue; continue;
} else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) { } else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) {
row_buf[ncol] = '\0'; row_buf[ncol] = '\0';
Vector2 pos = { row_posx, nrow * row_height + sb.ypos }; // TODO: Render new line or something?
DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color); // Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
// DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
nrow++; nrow++;
ncol = 0; ncol = 0;
row_posx = 0; row_posx = 0;
@ -221,7 +258,7 @@ int main(void) {
continue; continue;
} }
csi_code = substr[0]; csi_code = substr[0];
Color new_color = current_color; SDL_Color new_color = WHITE;
switch (csi_code) { switch (csi_code) {
case 'm': case 'm':
for (int i = 0; i < nargs; i++) { for (int i = 0; i < nargs; i++) {
@ -242,13 +279,16 @@ int main(void) {
} }
} }
row_buf[ncol] = '\0'; row_buf[ncol] = '\0';
Vector2 pos = { row_posx, nrow * row_height + sb.ypos }; // TODO: This looks like the actual place where we
DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color); // draw the text at a line
// Vector2 pos = { row_posx, nrow * row_height + sb.ypos };
// DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color);
current_color = new_color; current_color = new_color;
ncol = 0; ncol = 0;
c += (substr) - (sb.buf + c); c += (substr) - (sb.buf + c);
int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x; // TODO: Get the text height
row_posx += width + 0.85; // int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x;
// row_posx += width + 0.85;
break; break;
} }
break; break;
@ -259,10 +299,13 @@ int main(void) {
} }
} }
} }
EndDrawing();
} }
free(sb.buf); // TODO: Figure out when to free this thing
close(fds.master); // SDL_FreeSurface(texture);
CloseWindow();
TTF_CloseFont(font);
TTF_Quit();
SDL_Quit();
} }