[Add] Blinking terminal cursor.

This commit is contained in:
Ritchie Cunningham 2025-09-20 14:13:36 +01:00
parent 2d79aba17a
commit 846b8595f4
7 changed files with 28 additions and 11 deletions

View File

@ -75,6 +75,10 @@ int main(int argc, char** argv) {
Desktop* desktop = new Desktop(); Desktop* desktop = new Desktop();
desktop->add_window(test_window); desktop->add_window(test_window);
/* timer for cursor blink. */
Uint32 last_blink_time = 0;
bool show_cursor = true;
bool running = true; bool running = true;
while(running) { while(running) {
/* Event handling. */ /* Event handling. */
@ -84,11 +88,17 @@ int main(int argc, char** argv) {
desktop->handle_event(&event); desktop->handle_event(&event);
} }
Uint32 current_time = SDL_GetTicks();
if(current_time - last_blink_time > 500) { /* Every 500ms. */
show_cursor = !show_cursor;
last_blink_time = current_time;
}
/* Clear screen. */ /* Clear screen. */
glClearColor(0.1f, 0.1f, 0.1, 1.0f); glClearColor(0.1f, 0.1f, 0.1, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
desktop->render(shape_renderer_instance, txt_render_instance, SCREEN_HEIGHT); desktop->render(shape_renderer_instance, txt_render_instance, SCREEN_HEIGHT, show_cursor);
/* It's really odd to call it SwapWindow now, rather than SwapBuffer. */ /* It's really odd to call it SwapWindow now, rather than SwapBuffer. */
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);

View File

@ -42,7 +42,8 @@ void Terminal::handle_input(SDL_Event* event) {
} }
} }
void Terminal::render(TextRenderer* renderer, int x, int y, int width, int height) { void Terminal::render(TextRenderer* renderer, int x, int y, int width, int height,
bool show_cursor) {
float white[] = { 1.0f, 1.0f, 1.0f }; float white[] = { 1.0f, 1.0f, 1.0f };
float green[] = { 0.2f, 1.0f, 0.2f }; float green[] = { 0.2f, 1.0f, 0.2f };
float line_height = 20.0f; float line_height = 20.0f;
@ -59,9 +60,13 @@ void Terminal::render(TextRenderer* renderer, int x, int y, int width, int heigh
} }
/* Draw current input line. */ /* Draw current input line. */
std::string prompt = "> " + _input_buffer; std::string prompt = "> ";
std::string line_to_render = prompt + _input_buffer;
if(show_cursor) {
line_to_render += "_";
}
float prompt_y_pos = (y+height) - padding - line_height - (_history.size()*line_height); float prompt_y_pos = (y+height) - padding - line_height - (_history.size()*line_height);
renderer->render_text(prompt.c_str(), x+padding, prompt_y_pos, 1.0f, green); renderer->render_text(line_to_render.c_str(), x+padding, prompt_y_pos, 1.0f, green);
/* Disable scissor test. */ /* Disable scissor test. */
glDisable(GL_SCISSOR_TEST); glDisable(GL_SCISSOR_TEST);

View File

@ -10,7 +10,7 @@ public:
Terminal(void); Terminal(void);
void handle_input(SDL_Event* event); void handle_input(SDL_Event* event);
void render(TextRenderer* renderer, int x, int y, int width, int height); void render(TextRenderer* renderer, int x, int y, int width, int height, bool show_cursor);
private: private:
void _on_ret_press(void); void _on_ret_press(void);

View File

@ -52,8 +52,8 @@ void Desktop::handle_event(SDL_Event* event) {
} }
void Desktop::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, void Desktop::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer,
int screen_height) { int screen_height, bool show_cursor) {
for(auto win: _windows) { for(auto win: _windows) {
win->render(shape_renderer, txt_renderer, screen_height); win->render(shape_renderer, txt_renderer, screen_height, show_cursor);
} }
} }

View File

@ -14,7 +14,8 @@ public:
void add_window(UIWindow* window); void add_window(UIWindow* window);
void handle_event(SDL_Event* event); void handle_event(SDL_Event* event);
void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height); void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height,
bool show_cursor);
private: private:
std::vector<UIWindow*> _windows; std::vector<UIWindow*> _windows;

View File

@ -32,7 +32,7 @@ bool UIWindow::is_point_inside(int x, int y) {
} }
void UIWindow::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, void UIWindow::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer,
int screen_height) { int screen_height, bool show_cursor) {
int title_bar_height = 30; int title_bar_height = 30;
/* Define colours. */ /* Define colours. */
@ -63,7 +63,7 @@ void UIWindow::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer,
if(_content) { if(_content) {
int content_y_gl = y_gl; int content_y_gl = y_gl;
int content_height_gl = _height - title_bar_height; int content_height_gl = _height - title_bar_height;
_content->render(txt_renderer, _x, content_y_gl, _width, content_height_gl); _content->render(txt_renderer, _x, content_y_gl, _width, content_height_gl, show_cursor);
} }
} }

View File

@ -10,7 +10,8 @@ class UIWindow {
public: public:
UIWindow(const char* title, int x, int y, int width, int height); UIWindow(const char* title, int x, int y, int width, int height);
void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height); void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height,
bool show_cursor);
void handle_event(SDL_Event* event); void handle_event(SDL_Event* event);
void set_focused(bool focused); void set_focused(bool focused);
bool is_point_inside(int x, int y); bool is_point_inside(int x, int y);