diff --git a/Makefile b/Makefile index e0c1dc7..cf4f26d 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,11 @@ P=bt -CFLAGS=-g -Wall -Wextra -pedantic -O0 CC=gcc +CFLAGS=-g -Wall -Wextra -pedantic -O0 +LDLIBS=-lSDL2 -lSDL2_ttf RM=rm -vf bt: clean - $(CC) $(CFLAGS) -lraylib $(P).c -o $(P) + $(CC) $(CFLAGS) $(LDLIBS) $(P).c -o $(P) clean: $(RM) $(P) diff --git a/bt.c b/bt.c index 693e461..c22c811 100644 --- a/bt.c +++ b/bt.c @@ -7,7 +7,12 @@ #include #include #include -#include +#include + +#include +#include +#include +#include typedef struct scrollback { float height; @@ -40,22 +45,17 @@ void spawn(file_descriptors *fds) { } } -static float fontsize = 12; - -Font *load_font(void) { - unsigned int file_size = 0; - char *font = "./fira.ttf"; - unsigned char *fontdata = LoadFileData(font, &file_size); - Font *fontDefault = calloc(1, sizeof(Font)); - fontDefault->baseSize = (int)fontsize; - fontDefault->glyphCount = 95; - - fontDefault->glyphs = LoadFontData(fontdata, (int)file_size, 16, 0, 95, FONT_DEFAULT); - Image atlas = GenImageFontAtlas(fontDefault->glyphs, &fontDefault->recs, 95, 16, 4, 0); - fontDefault->texture = LoadTextureFromImage(atlas); - UnloadImage(atlas); - return fontDefault; -} +const int SCREEN_WIDTH = 900; +const int SCREEN_HEIGHT = 700; +const SDL_Color WHITE = {255, 255, 255, 255}; +const SDL_Color BLACK = {0, 0, 0, 255}; +const SDL_Color RED = {255, 0, 0, 255}; +const SDL_Color GREEN = {0, 255, 0, 255}; +const SDL_Color BLUE = {0, 0, 255, 255}; +const SDL_Color YELLOW = {255, 255, 0, 255}; +const SDL_Color MAGENTA = {255, 0, 255, 255}; +const SDL_Color SKYBLUE = {0, 255, 255, 255}; +const SDL_Color RAYWHITE = {200, 200, 200, 255}; int read_pty(file_descriptors *fds, scrollback *sb) { ssize_t nread; @@ -90,10 +90,26 @@ int main(void) { spawn(&fds); - const int screenWidth = 800; - const int screenHeight = 550; + if (SDL_Init(SDL_INIT_EVERYTHING) < 0) { + 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; int result = ioctl(fds.master, TIOCGWINSZ, &sz); @@ -101,9 +117,8 @@ int main(void) { sz.ws_row = 120; result = ioctl(fds.master, TIOCSWINSZ, &sz); - SetTargetFPS(60); - - Font *fontDefault = load_font(); + // TODO: It would be nice to figure out how to do this in SDL + // SetTargetFPS(60); sb.capacity = 2048; sb.buf = malloc(sb.capacity); @@ -123,68 +138,89 @@ int main(void) { bool new_read = false; bool new_char = false; - while (!WindowShouldClose()) { - float scroll_speed = 35.5f; - sb.ypos += GetMouseWheelMoveV().y * scroll_speed; - sb.height = MeasureTextEx(*fontDefault, sb.buf, fontsize, 1).y; + SDL_Event e; + bool quit = false; + while (quit == false) { + 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; } + char key = e.key.keysym.sym; + // TODO: Translate this all to SDL2 + // int key = GetCharPressed(); + while (key > 0) { + if ((key >= 32) && (key <= 125)) { + buf[buf_len] = (char)key; + buf[buf_len + 1] = '\0'; + buf_len++; + sb.buf[sb.length] = (char)key; + sb.buf[sb.length + 1] = '\0'; + sb.length++; + } + // TODO: Don't remember why I was doing this + // key = GetCharPressed(); + // new_char = true; + } + + if (key == SDLK_RETURN) { + sb.length -= buf_len; + sb.buf[sb.length] = '\0'; + write(fds.master, buf, buf_len); + write(fds.master, "\r", 1); + buf[0] = '\0'; + buf_len = 0; + new_char = true; + } + if (key == SDLK_BACKSPACE) { + if (buf_len > 0) { + buf_len--; + buf[buf_len] = '\0'; + sb.length--; + sb.buf[sb.length] = '\0'; + } + 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); - } + // 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) { - if ((key >= 32) && (key <= 125)) { - buf[buf_len] = (char)key; - buf[buf_len + 1] = '\0'; - buf_len++; - sb.buf[sb.length] = (char)key; - sb.buf[sb.length + 1] = '\0'; - sb.length++; - } - key = GetCharPressed(); - new_char = true; - } - - if (IsKeyPressed(KEY_ENTER)) { - sb.length -= buf_len; - sb.buf[sb.length] = '\0'; - write(fds.master, buf, buf_len); - write(fds.master, "\r", 1); - buf[0] = '\0'; - buf_len = 0; - new_char = true; - } - if (IsKeyPressed(KEY_BACKSPACE)) { - if (buf_len > 0) { - buf_len--; - buf[buf_len] = '\0'; - sb.length--; - sb.buf[sb.length] = '\0'; - } - new_char = true; - } // Drawing - BeginDrawing(); { int nread = read_pty(&fds, &sb); if (nread > 0) { sb.length += nread; new_read = true; } - ClearBackground(BLACK); - Color current_color = RAYWHITE; + SDL_RenderClear(renderer); + SDL_Color current_color = WHITE; int col_max = 200; char row_buf[col_max]; int nrow = 0; @@ -196,8 +232,9 @@ int main(void) { continue; } else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) { row_buf[ncol] = '\0'; - Vector2 pos = { row_posx, nrow * row_height + sb.ypos }; - DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color); + // TODO: Render new line or something? + // Vector2 pos = { row_posx, nrow * row_height + sb.ypos }; + // DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color); nrow++; ncol = 0; row_posx = 0; @@ -221,7 +258,7 @@ int main(void) { continue; } csi_code = substr[0]; - Color new_color = current_color; + SDL_Color new_color = WHITE; switch (csi_code) { case 'm': for (int i = 0; i < nargs; i++) { @@ -242,13 +279,16 @@ int main(void) { } } row_buf[ncol] = '\0'; - Vector2 pos = { row_posx, nrow * row_height + sb.ypos }; - DrawTextEx(*fontDefault, row_buf, pos, fontsize, 0, current_color); + // TODO: This looks like the actual place where we + // 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; ncol = 0; c += (substr) - (sb.buf + c); - int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x; - row_posx += width + 0.85; + // TODO: Get the text height + // int width = MeasureTextEx(*fontDefault, row_buf, fontsize, 1).x; + // row_posx += width + 0.85; break; } break; @@ -259,10 +299,13 @@ int main(void) { } } } - EndDrawing(); } - free(sb.buf); - close(fds.master); - CloseWindow(); + // TODO: Figure out when to free this thing + // SDL_FreeSurface(texture); + + TTF_CloseFont(font); + TTF_Quit(); + + SDL_Quit(); }