[Change] Render terminal conte in UI window.

This commit is contained in:
Ritchie Cunningham 2025-09-20 13:06:13 +01:00
parent a0c5eab1cf
commit 1bce18e993
5 changed files with 46 additions and 17 deletions

View File

@ -68,6 +68,7 @@ int main(int argc, char** argv) {
/* Create terminal. */
Terminal* term = new Terminal();
test_window->set_content(term);
bool running = true;
while(running) {
@ -83,8 +84,7 @@ int main(int argc, char** argv) {
glClearColor(0.1f, 0.1f, 0.1, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
test_window->render(shape_renderer_instance, txt_render_instance);
term->render(txt_render_instance);
test_window->render(shape_renderer_instance, txt_render_instance, SCREEN_HEIGHT);
/* It's really odd to call it SwapWindow now, rather than SwapBuffer. */
SDL_GL_SwapWindow(window);

View File

@ -1,8 +1,9 @@
#include <cstdio>
#include <SDL3/SDL.h>
#include <GL/glew.h>
#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. */
@ -41,17 +42,27 @@ void Terminal::handle_input(SDL_Event* event) {
}
}
void Terminal::render(TextRenderer* renderer) {
void Terminal::render(TextRenderer* renderer, int x, int y, int width, int height) {
float white[] = { 1.0f, 1.0f, 1.0f };
float green[] = { 0.2f, 1.0f, 0.2f };
float line_height = 28.0f;
float line_height = 20.0f;
float padding = 5.0f;
/* Enable scissor test to clip rendering to the window content area. */
glEnable(GL_SCISSOR_TEST);
glScissor(x, y, width, height);
/* 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);
float y_pos = (y+height) - padding - line_height - (i*line_height);
renderer->render_text(_history[i].c_str(), x+padding, y_pos, 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);
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);
/* Disable scissor test. */
glDisable(GL_SCISSOR_TEST);
}

View File

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

View File

@ -6,11 +6,17 @@ UIWindow::UIWindow(const char* title, int x, int y, int width, int height) {
_title = title;
_x = x;
_y = y;
_width = width;
_height = height;
_width = width;
_height = height;
_content = nullptr;
}
void UIWindow::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer) {
void UIWindow::set_content(Terminal* term) {
_content = term;
}
void UIWindow::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer,
int screen_height) {
int title_bar_height = 30;
/* Define colours. */
@ -18,15 +24,24 @@ void UIWindow::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer)
float title_bar_color[] = { 0.15f, 0.15f, 0.2f };
float title_text_color[] = { 0.9f, 0.9f, 0.9f };
/* Convert top-left coords to bottom-left for OpenGL. */
int y_gl = screen_height - _y - _height;
/* Draw main window frame/background. */
shape_renderer->draw_rect(_x, _y, _width, _height, frame_color);
shape_renderer->draw_rect(_x, y_gl, _width, _height, frame_color);
/* Draw title bar. */
shape_renderer->draw_rect(_x, _y+_height-title_bar_height, _width,
title_bar_height, title_bar_color);
shape_renderer->draw_rect(_x, y_gl+_height-title_bar_height, _width, title_bar_height,
title_bar_color);
/* Draw title text. */
txt_renderer->render_text(_title.c_str(), _x+5, _y+_height-title_bar_height+8,
txt_renderer->render_text(_title.c_str(), _x+5, y_gl+_height-title_bar_height+8,
1.0f, title_text_color);
if(_content) {
int content_y_gl = y_gl;
int content_height_gl = _height - title_bar_height;
_content->render(txt_renderer, _x, content_y_gl, _width, content_height_gl);
}
}

View File

@ -4,15 +4,18 @@
#include "gfx/shape_renderer.h"
#include "gfx/txt_renderer.h"
#include "terminal.h"
class UIWindow {
public:
UIWindow(const char* title, int x, int y, int width, int height);
void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer);
void render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer, int screen_height);
void set_content(Terminal* term);
private:
int _x, _y, _width, _height;
std::string _title;
Terminal* _content;
};