Skip to content

Commit

Permalink
add improvements to the UI, and some other small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RelativisticMechanic committed Jul 24, 2023
1 parent 717f576 commit e512835
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 44 deletions.
5 changes: 2 additions & 3 deletions src/CRTerm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "ConfigSelector.h"
#include "ContextMenu.h"


/* SDLmain requires this. It seems to define its own main. */
#undef main

Expand Down Expand Up @@ -119,8 +118,8 @@ int main()
/* Set the cursor to normal if a UI element is online */
SDL_SetCursor(normal_cur);
}
}
GPU_ClearColor(screen, SDL_Color{ 40, 40, 40, 255 });
}
GPU_ClearColor(screen, SDL_Color{ 52, 55, 64, 255 });
/* First render the terminal */
vt100_term->VT100Render();
/* Then the UI */
Expand Down
6 changes: 5 additions & 1 deletion src/CRTerm.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)lib\SDL2_gpu\include;$(ProjectDir)lib\SDL2\include;$(ProjectDir)lib\imgui\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<AdditionalIncludeDirectories>$(ProjectDir)lib\SDL2_gpu\include;$(ProjectDir)lib\SDL2\include;$(ProjectDir)lib\imgui\include;$(ProjectDir)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
Expand All @@ -126,12 +126,16 @@
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>$(ProjectDir)lib\SDL2_gpu\include;$(ProjectDir)lib\SDL2\include;$(ProjectDir)lib\imgui\include;$(ProjectDir)include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(ProjectDir)lib\SDL2;$(ProjectDir)lib\SDL2_gpu;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>SDL2main.lib;SDL2.lib;SDL2_gpu.lib;winmm.lib;Dwmapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
19 changes: 16 additions & 3 deletions src/CRTermConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ CRTermConfiguration::CRTermConfiguration(std::string json_path)
this->default_fore_color = configuration_data.at("default_fg");
this->default_back_color = configuration_data.at("default_bg");
this->crt_warp = configuration_data.at("crt_warp");

this->maxlines = configuration_data.at("maxlines");
int i = 0;
for (auto& color : configuration_data.at("color_scheme"))
{
Expand All @@ -67,9 +67,21 @@ CRTermConfiguration::CRTermConfiguration(std::string json_path)
MessageBox(GetActiveWindow(), (LPCWSTR)errW.c_str(), L"Error parsing default.json", MB_OK | MB_ICONERROR);
exit(-1);
}
catch (nlohmann::json::other_error& error)
{
std::string err = error.what();
std::wstring errW = std::wstring(err.begin(), err.end());
MessageBox(GetActiveWindow(), (LPCWSTR)errW.c_str(), L"Error loading default.json", MB_OK | MB_ICONERROR);
}
catch (nlohmann::json::type_error& error)
{
std::string err = error.what();
std::wstring errW = std::wstring(err.begin(), err.end());
MessageBox(GetActiveWindow(), (LPCWSTR)errW.c_str(), L"Error loading default.json", MB_OK | MB_ICONERROR);
}
catch (...)
{
MessageBox(GetActiveWindow(), L"An exception occurred while loading default.json", L"Error loading default.json", MB_OK | MB_ICONERROR);
MessageBox(GetActiveWindow(), L"An unknown error occurred", L"Error loading default.json", MB_OK | MB_ICONERROR);
}
}

Expand All @@ -94,6 +106,7 @@ void CRTermConfiguration::Save(std::string filename)
output_json["default_fg"] = this->default_fore_color;
output_json["default_bg"] = this->default_back_color;
output_json["crt_warp"] = this->crt_warp;
output_json["maxlines"] = this->maxlines;

int color_scheme_arr[16][3];

Expand All @@ -104,7 +117,7 @@ void CRTermConfiguration::Save(std::string filename)
color_scheme_arr[i][2] = color_scheme[i].b;
}

output_json["color_sheme"] = color_scheme_arr;
output_json["color_scheme"] = color_scheme_arr;

std::ofstream output(filename);
output << std::setw(4) << output_json;
Expand Down
1 change: 1 addition & 0 deletions src/CRTermConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class CRTermConfiguration
int default_fore_color;
int default_back_color;
int blink_interval;
int maxlines;
float crt_warp;
CRTermColor color_scheme[16];

Expand Down
4 changes: 4 additions & 0 deletions src/ConfigEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ void ConfigEditor::Render(void)
ImGui::SameLine();
ImGui::InputInt("##blink_interval", &(cfg->blink_interval));

ImGui::Text("Maximum Lines");
ImGui::SameLine();
ImGui::InputInt("##maxlines", &(cfg->maxlines));

ImGui::Text("Default FG");
ImGui::SameLine();
ImGui::InputInt("##default_fg", &(cfg->default_fore_color));
Expand Down
61 changes: 50 additions & 11 deletions src/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ Console::Console(CRTermConfiguration* cfg)
this->cursor_x = 0;
this->cursor_y = 0;

this->maxlines = cfg->maxlines;
/* Check the maxlines, if greater than 10000 or less than 500 default to 1000 */
if (this->maxlines < CONSOLE_MIN_LINES || this->maxlines > CONSOLE_MAX_LINES)
this->maxlines = CONSOLE_DEFAULT_LINES;

this->console_resolution_x = this->console_w * this->font_w;
this->console_resolution_y = this->console_h * this->font_h;

Expand Down Expand Up @@ -194,6 +199,14 @@ void Console::HistoryDown()
start_line++;
}

void Console::SetSelection(bool selection, int start_x, int start_y, int end_x, int end_y)
{
this->selected_start_x = start_x;
this->selected_start_y = start_y;
this->selected_end_x = end_x;
this->selected_end_y = end_y;
this->is_selected = selection;
}
void Console::SetCursor(int x, int y)
{
this->cursor_x = x;
Expand Down Expand Up @@ -317,6 +330,8 @@ unsigned char Console::ReadChar(int x, int y)
return 0;
return this->buffer[this->start_line * this->console_w + y * this->console_w + x];
}

/* The Console Render function, this is what makes the magic happen :) */
void Console::Render(GPU_Target* t, int xloc, int yloc, float scale)
{
GPU_Clear(this->render_buffer->target);
Expand All @@ -330,6 +345,8 @@ void Console::Render(GPU_Target* t, int xloc, int yloc, float scale)
this->show_cursor = !this->show_cursor;
this->cursor_clock = time;
}
/* Set text alpha to 1.0 */
GPU_SetUniformf(GPU_GetUniformLocation(this->text_shader_id, "alpha"), 1.0);
for (int y = 0; y < this->console_h; y++)
{
for (int x = 0; x < this->console_w; x++)
Expand All @@ -353,6 +370,35 @@ void Console::Render(GPU_Target* t, int xloc, int yloc, float scale)
}
}

/* Draw the scroll bar if we are scrolling i.e. last_line != start_line and the user has scrolled up */
if (start_line != last_line)
{
GPU_Rect scrollbar;
scrollbar.h = (((float)this->console_h) / ((float)this->last_line + this->console_h)) * (this->render_buffer->h);
scrollbar.y = scrollbar.h * 0.5 * ((float)this->start_line / (float)(this->last_line + 1.0));
scrollbar.w = 8;
scrollbar.x = this->render_buffer->w - 8;
GPU_SetUniformfv(GPU_GetUniformLocation(this->text_shader_id, "text_color"), 3, 1, this->color_scheme[this->default_fore_color].returnArray());
/* Set scroll bar alpha to 0.8 */
GPU_SetUniformf(GPU_GetUniformLocation(this->text_shader_id, "alpha"), 0.8);
GPU_RectangleFilled(this->render_buffer->target, scrollbar.x, scrollbar.y, scrollbar.x + scrollbar.w, scrollbar.y + scrollbar.h, SDL_Color{ 255, 255, 255, 255 });
}

/* Draw the selection rectangle if the user has selected some text, this bool passed from VT100 class */
if (this->is_selected)
{
GPU_SetUniformfv(GPU_GetUniformLocation(this->text_shader_id, "text_color"), 3, 1, this->color_scheme[this->default_fore_color].returnArray());
/* Set selected text alpha to 0.4 */
GPU_SetUniformf(GPU_GetUniformLocation(this->text_shader_id, "alpha"), 0.4);
for (int i = this->selected_start_x + this->selected_start_y * this->console_w; i < this->selected_end_x + this->selected_end_y * this->console_w; i++)
{

int y = i / this->console_w;
int x = i % this->console_w;
GPU_RectangleFilled(this->render_buffer->target, x * this->font_w, y * this->font_h, (x + 1) * this->font_w, (y + 1) * this->font_h, SDL_Color{ 255, 255, 255, 255 });
}
}

/* Draw the cursor, if we are not scrolling */
if (this->show_cursor && this->start_line == this->last_line)
{
Expand All @@ -370,6 +416,10 @@ void Console::Render(GPU_Target* t, int xloc, int yloc, float scale)
}
}

/*
We have now rendered the terminal to the local render buffer,
now we pass it through the CRT shader and scale it up.
*/
GPU_ActivateShaderProgram(this->crt_shader_id, &this->crt_shader_block);
float resolution[2] = { (float)t->w, (float)t->h };
/* Set shader parameters */
Expand All @@ -383,17 +433,6 @@ void Console::Render(GPU_Target* t, int xloc, int yloc, float scale)
/* Now blit to screen! */
GPU_BlitScale(this->render_buffer, NULL, t, xloc + (int)(this->render_buffer->w / 2) * scale, yloc + (int)(this->render_buffer->h / 2) * scale, scale, scale);
GPU_DeactivateShaderProgram();

/* Draw the scroll bar */
if (start_line != last_line)
{
GPU_Rect scrollbar;
scrollbar.h = (((float)this->console_h) / ((float)this->last_line + this->console_h)) * (t->h - TITLE_BAR_HEIGHT);
scrollbar.y = TITLE_BAR_HEIGHT + scrollbar.h * 0.5 * ((float)this->start_line / (float)(this->last_line + 1.0));
scrollbar.w = SCROLL_BAR_WIDTH;
scrollbar.x = t->w - SIDES_WIDTH - SCROLL_BAR_WIDTH;
GPU_RectangleFilled2(t, scrollbar, SDL_Color{ 100, 100, 100, 255 });
}
/*
Now apply the CRT effect shader
The CRT effect shader applies CRT warp effect, CRT phosphor glow scanline effect, and noise.
Expand Down
26 changes: 25 additions & 1 deletion src/Console.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include "CRTermConfig.h"
#include "SDL_gpu.h"

#define CONSOLE_MIN_LINES 500
#define CONSOLE_MAX_LINES 10000
#define CONSOLE_DEFAULT_LINES 1000

/*
Helper function to construct console attributes,
which are basically VGA attributes that store
Expand Down Expand Up @@ -56,16 +60,35 @@ class Console
/* TODO: Some of these are better of in private. */
unsigned char* buffer;
unsigned char* attrib_buffer;
/* Character-wise resolution of the console */
int console_w, console_h;
GPU_Image* console_font;
GPU_Image* crt_background;
int font_w, font_h;
int cursor_x, cursor_y;
/* Actual resolution of the console (in pixels) */
int console_resolution_x, console_resolution_y;
int default_fore_color, default_back_color;
/* No. of millseconds after which the console must blink */
int blink_interval;
/*
For scrolling, console starts drawing from start_line*console_w, last_line is the latest start_line position
For all time, start_line <= last_line. start_line is decremented when the user hits mousewheel up
and Console->HistoryUp() is called.
maxlines is the maximum lines the console stores, beyond that, it starts to overwrite its old history.
Generally, the lower limit for maxlines is 500, and upper limit is 10000 lines.
*/
int start_line, last_line;
int maxlines = 1000;
int maxlines;

/* Selecting text into the terminal */
bool is_selected;
int selected_start_x = 0, selected_start_y = 0;
int selected_end_x = 0, selected_end_y = 0;
/*
Stores audio data for the bell sound.
*/
std::string bell_wave_file;

Console(CRTermConfiguration*);
Expand All @@ -86,6 +109,7 @@ class Console
void ClearExt(int fromx, int fromy, int tox, int toy);
void HistoryUp();
void HistoryDown();
void SetSelection(bool selection, int start_x=0, int start_y=0, int end_x=0, int end_y=0);
/* The 256 letters glyphs extracted from the font image */
GPU_Image* char_blocks[256];

Expand Down
25 changes: 12 additions & 13 deletions src/VT100.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ void VT100::VT100Take(unsigned char c)
{
std::cout << "Unexpected identifier after VT_ESCAPE: " << c << std::endl;
parser_state = VTSTATE_NORMAL;
VT100Putc(c);
//VT100Putc(c);
}
break;
case VTSTATE_ATTR:
Expand Down Expand Up @@ -551,9 +551,13 @@ void VT100::VT100HandleEvent(SDL_Event ev)
break;
case SDL_MOUSEWHEEL:
if (ev.wheel.y > 0)
{
this->con->HistoryUp();
}
else
{
this->con->HistoryDown();
}
break;
case SDL_MOUSEBUTTONUP:
if (ev.button.button == SDL_BUTTON_LEFT)
Expand Down Expand Up @@ -585,23 +589,18 @@ void VT100::VT100Send(std::string sequence)

void VT100::VT100Render(void)
{
con->Render(this->render_target, this->screen_offsetx, this->screen_offsety, this->font_scale);
GPU_DeactivateShaderProgram();

/* Draw overlay above the selected text if it is selected or dragging */
if (is_selected || is_dragging)
{
/* In case the user has selected it from right to left, orient the selection coords */
orientSelectedCoords();
for (int i = this->selected_start_x + this->selected_start_y * this->con->console_w; i < this->selected_end_x + this->selected_end_y * this->con->console_w; i++)
{
int y = i / this->con->console_w;
int x = i % this->con->console_w;
int sx, sy;
consoleToScreenCoords(x, y, &sx, &sy);
GPU_RectangleFilled(this->render_target, sx, sy, sx + con->font_w * this->font_scale, sy + con->font_h * this->font_scale, SDL_Color{255, 255, 255, 128});
}
this->con->SetSelection(true, selected_start_x, selected_start_y, selected_end_x, selected_end_y);
}
else
{
this->con->SetSelection(false);
}
con->Render(this->render_target, this->screen_offsetx, this->screen_offsety, this->font_scale);
GPU_DeactivateShaderProgram();
}
void VT100::VT100Shutdown()
{
Expand Down
4 changes: 2 additions & 2 deletions src/VT100.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ class VT100

inline void consoleToScreenCoords(int conx, int cony, int* screenx, int* screeny)
{
*screenx = screen_offsetx + conx * this->con->font_w * this->font_scale;
*screeny = screen_offsety + cony * this->con->font_h * this->font_scale;
*screenx = (int)((double)screen_offsetx + (double)conx * (double)this->con->font_w * this->font_scale);
*screeny = (int)((double)screen_offsety + (double)cony * (double)this->con->font_h * this->font_scale);
}

inline void getConsoleMouseCoords(int* conx, int* cony)
Expand Down
9 changes: 5 additions & 4 deletions src/resources/config/default_cmd.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"font": "font.png",
"font_width": 16,
"font_height": 32,
"font_scale": 0.8,
"font_width": 8,
"font_height": 16,
"font_scale": 1.2,
"console_width": 120,
"console_height": 30,
"background": "crt.png",
"text_shader": "shaders/text",
"crt_shader": "shaders/crt",
"bell": "bell.wav",
"crt_warp": 0.0,
"shell_command": "cmd.exe %userprofile%",
"shell_command": "cmd.exe %USERPROFILE%",
"blink_interval": 300,
"default_fg": 2,
"default_bg": 2,
"maxlines": 1000,

"color_scheme":
[
Expand Down
7 changes: 4 additions & 3 deletions src/resources/config/default_wsl.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"font": "font.png",
"font_width": 16,
"font_height": 32,
"font_scale": 0.8,
"font_width": 8,
"font_height": 16,
"font_scale": 1.2,
"console_width": 120,
"console_height": 30,
"background": "crt.png",
Expand All @@ -14,6 +14,7 @@
"blink_interval": 300,
"default_fg": 6,
"default_bg": 6,
"maxlines": 1000,

"color_scheme":
[
Expand Down
2 changes: 1 addition & 1 deletion src/resources/default
Original file line number Diff line number Diff line change
@@ -1 +1 @@
config\default_wsl.json
config\default_cmd.json
Binary file modified src/resources/font.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit e512835

Please sign in to comment.