[Add] Implement initial interactive terminal.

This commit is contained in:
Ritchie Cunningham 2025-09-20 03:46:38 +01:00
parent f86015736d
commit 8cc30eaa17
3 changed files with 92 additions and 7 deletions

View File

@ -1,7 +1,9 @@
#include <cstdio>
#include "gfx/txt_renderer.h"
#include <GL/glew.h>
#include <SDL3/SDL.h>
#include <SDL3/SDL_keyboard.h>
#include "terminal.h"
const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 720;
@ -49,18 +51,24 @@ int main(int argc, char** argv) {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* Listen for text input. */
SDL_StartTextInput(window);
/* Init text renderer. */
TextRenderer* txt_render_instance = new TextRenderer(SCREEN_WIDTH, SCREEN_HEIGHT);
txt_render_instance->load_font("assets/fonts/hack/Hack-Regular.ttf", 24);
txt_render_instance->load_font("assets/fonts/hack/Hack-Regular.ttf", 14);
/* Create terminal. */
Terminal* term = new Terminal();
bool running = true;
while(running) {
/* Event handling. */
SDL_Event event;
while(SDL_PollEvent(&event)) {
if(event.type == SDL_EVENT_QUIT) {
running = false;
}
if(event.type == SDL_EVENT_QUIT) { running = false; }
/* Pass input to terminal. */
term->handle_input(&event);
}
/* Clear screen. */
@ -69,14 +77,14 @@ int main(int argc, char** argv) {
/* Render text. */
float white[] = { 1.0f, 1.0f, 1.0f };
txt_render_instance->render_text("Hello World", 25.0f, SCREEN_HEIGHT - 50.0f,
1.0f, white);
term->render(txt_render_instance);
/* It's really odd to call it SwapWindow now, rather than SwapBuffer. */
SDL_GL_SwapWindow(window);
}
/* Cleanup. */
delete term;
delete txt_render_instance;
SDL_GL_DestroyContext(context);
SDL_DestroyWindow(window);

57
client/src/terminal.cpp Normal file
View File

@ -0,0 +1,57 @@
#include <cstdio>
#include "gfx/txt_renderer.h"
#include "terminal.h"
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_keycode.h>
Terminal::Terminal(void) {
/* Placeholder welcome message to history. */
_history.push_back("Welcome to Bettola");
}
void Terminal::_on_ret_press(void) {
/* Add the command to history. */
_history.push_back("> " + _input_buffer);
/* For now, print the commmand clear buffer.
* TODO: Parse and execute commands.
*/
printf("Command entered: %s\n", _input_buffer.c_str());
if(_input_buffer == "clear") {
_history.clear();
} else if(_input_buffer == "help") {
_history.push_back("Commands: help, clear");
}
_input_buffer.clear();
}
void Terminal::handle_input(SDL_Event* event) {
if(event->type == SDL_EVENT_TEXT_INPUT) {
/* Append chars to the input buffer. */
_input_buffer += event->text.text;
} else if(event->type == SDL_EVENT_KEY_DOWN) {
/* Handle special keys. */
if(event->key.key == SDLK_BACKSPACE && _input_buffer.length() > 0) {
_input_buffer.pop_back();
} else if(event->key.key == SDLK_RETURN) {
_on_ret_press();
}
}
}
void Terminal::render(TextRenderer* renderer) {
float white[] = { 1.0f, 1.0f, 1.0f };
float green[] = { 0.2f, 1.0f, 0.2f };
float line_height = 28.0f;
/* Draw history. */
for(size_t i = 0; i < _history.size(); ++i) {
renderer->render_text(_history[i].c_str(), 25.0f, 720.0f-50.0f-(i*line_height), 1.0f, white);
}
/* Draw current input line. */
std::string prompt = "> " + _input_buffer;
renderer->render_text(prompt.c_str(), 25.0f, 720.0f-50.0f-(_history.size()*line_height), 1.0f, green);
}

20
client/src/terminal.h Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#include <string>
#include <vector>
#include <SDL3/SDL.h>
#include "gfx/txt_renderer.h"
class Terminal {
public:
Terminal(void);
void handle_input(SDL_Event* event);
void render(TextRenderer* renderer);
private:
void _on_ret_press(void);
std::string _input_buffer;
std::vector<std::string> _history;
};