Skip to content

Commit

Permalink
Implement function keys and control codes, add reflection shader
Browse files Browse the repository at this point in the history
  • Loading branch information
RelativisticMechanic committed Aug 2, 2023
1 parent 715161e commit 53e2402
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/CRTerm.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef CRTERM_H
#define CRTERM_H

#define CRTERM_VERSION_STRING "CRTerm 0.2.6"
#define CRTERM_VERSION_STRING "CRTerm 0.2.8"
#define CRTERM_CREDIT_STRING "(C) Siddharth Gautam, 2023\nThis software comes with NO WARRANTY.\n"
#define FRAMES_PER_SEC 60

Expand Down
1 change: 0 additions & 1 deletion src/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ Console::Console(CRTermConfiguration* cfg)

/* Set CRT effect parameters from configuration */
this->crt_warp = cfg->crt_warp;

this->start_line = 0;
this->last_line = 0;
/* Clear the console */
Expand Down
46 changes: 36 additions & 10 deletions src/VT100.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ void VT100::VT100Take(unsigned char c)
{
std::cout << "Unexpected identifier after VT_ESCAPE: " << c << std::endl;
parser_state = VTSTATE_NORMAL;
//VT100Putc(c);
}
break;
case VTSTATE_ATTR:
Expand Down Expand Up @@ -489,11 +488,21 @@ void VT100::VT100Take(unsigned char c)
{
this->bracketed_mode = true;
}
/* CSI ? 7 h DEC WrapAround Enable */
/* CSI ? 2 h DEC Turn ON disabled keyboard input */
else if (this->argument_stack[0].value == 2)
{
this->keyboard_disabled = true;
}
/* CSI ? 7 h DEC Turn ON wraparound */
else if (this->argument_stack[0].value == 7)
{
con->EnableWrapAround();
}
/* CSI ? 12 h DEC Turn ON Send-Receive Mode */
else if (this->argument_stack[0].value == 12)
{
// IGNORE.
}
else
{
std::cout << "Unimplemented ON: " << this->argument_stack[0].value << std::endl;
Expand All @@ -510,11 +519,21 @@ void VT100::VT100Take(unsigned char c)
{
this->bracketed_mode = false;
}
/* CSI ? 7 l DEC WrapAround Disable */
/* CSI ? 2 l DEC Turn OFF Keyboard Disable */
else if (this->argument_stack[0].value == 2)
{
this->keyboard_disabled = false;
}
/* CSI ? 7 l DEC Turn OFF Wrap Around */
else if (this->argument_stack[0].value == 7)
{
con->DisableWrapAround();
}
/* CSI ? 12 l DEC Turn OFF Send-Receive Mode */
else if (this->argument_stack[0].value == 12)
{
// IGNORE.
}
else
{
std::cout << "Unimplemented OFF: " << this->argument_stack[0].value << std::endl;
Expand Down Expand Up @@ -543,11 +562,17 @@ void VT100::VT100HandleEvent(SDL_Event ev)
switch (ev.type)
{
case SDL_TEXTINPUT:
/* Send text input as is, SDLs text input is ASCII compliant */
WriteFile(this->toProgram, &(ev.text.text[0]), 1, NULL, NULL);
if (!keyboard_disabled)
{
/* Send text input as is, SDLs text input is ASCII compliant */
WriteFile(this->toProgram, &(ev.text.text[0]), 1, NULL, NULL);
}
break;
case SDL_KEYDOWN:
/* Send special keys */
if (keyboard_disabled)
break;

/* Send special keys */
if (special_key_map.find((int)ev.key.keysym.sym) != special_key_map.end())
{
std::string ansi_sequence = special_key_map[(int)ev.key.keysym.sym];
Expand All @@ -559,16 +584,17 @@ void VT100::VT100HandleEvent(SDL_Event ev)
this->CTRL_down = true;
}

/* If CTRL is down WITH C... send break. */
/* If CTRL is down... send the relevant sequence. */
if (this->CTRL_down)
{
if (ev.key.keysym.sym == SDLK_c)
if (control_key_map.find((int)ev.key.keysym.sym) != control_key_map.end())
{
const char brk_char = '\x3';
WriteFile(this->toProgram, &brk_char, 1, NULL, NULL);
std::string control_sequence = control_key_map[(int)ev.key.keysym.sym];
WriteFile(this->toProgram, control_sequence.c_str(), control_sequence.length(), NULL, NULL);
}
}
break;

case SDL_KEYUP:
if (ev.key.keysym.sym == SDLK_LCTRL || ev.key.keysym.sym == SDLK_RCTRL)
{
Expand Down
47 changes: 46 additions & 1 deletion src/VT100.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,57 @@ class VT100
{ SDLK_UP, "\x1B[A" },
{ SDLK_DOWN, "\x1B[B" },
{ SDLK_RIGHT, "\x1B[C" },
{ SDLK_LEFT, "\x1B[D" }
{ SDLK_LEFT, "\x1B[D" },
{ SDLK_DELETE, "\b" },
{ SDLK_F1, "\x1B[11~" },
{ SDLK_F2, "\x1B[12~" },
{ SDLK_F3, "\x1B[14~" },
{ SDLK_F4, "\x1B[15~" },
{ SDLK_F5, "\x1B[16~" },
{ SDLK_F6, "\x1B[17~" },
{ SDLK_F7, "\x1B[18~" },
{ SDLK_F8, "\x1B[19~" },
{ SDLK_F9, "\x1B[20~" },
{ SDLK_F10, "\x1B[21~" }
};

/* Control Key map, for stuff like ^C, ^O, etc. */
std::unordered_map<int, std::string> control_key_map = {
{ SDLK_SPACE, "\x0" },
{ SDLK_a, "\x1" },
{ SDLK_b, "\x2" },
{ SDLK_c, "\x3" },
{ SDLK_d, "\x4" },
{ SDLK_e, "\x5" },
{ SDLK_f, "\x6" },
{ SDLK_g, "\x7" },
{ SDLK_h, "\x8" },
{ SDLK_i, "\x9" },
{ SDLK_j, "\xA" },
{ SDLK_k, "\xB" },
{ SDLK_l, "\xC" },
{ SDLK_m, "\xD" },
{ SDLK_n, "\xE" },
{ SDLK_o, "\xF" },
{ SDLK_p, "\x10" },
{ SDLK_q, "\x11" },
{ SDLK_r, "\x12" },
{ SDLK_s, "\x13" },
{ SDLK_t, "\x14" },
{ SDLK_u, "\x15" },
{ SDLK_v, "\x16" },
{ SDLK_w, "\x17" },
{ SDLK_x, "\x18" },
{ SDLK_y, "\x19" },
{ SDLK_z, "\x1A" }
};

/* For sending ^C */
bool CTRL_down;

/* For DECKAM */
bool keyboard_disabled = false;

/* Required for mouse interactivty */
float font_scale;

Expand Down
2 changes: 1 addition & 1 deletion src/resources/config/default-wsl.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

"color_scheme":
[
[10, 10, 10],
[80, 80, 80],
[0, 0, 170],
[0, 170, 0],
[0, 170, 170],
Expand Down
23 changes: 19 additions & 4 deletions src/resources/shaders/crt.fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ uniform float time;
uniform vec2 resolution;

// Glow effect settings
const float blurSize = 1.0/512.0;
const float blurSizeDefault = 1.0/512.0;
const float intensity = 1.0;
const float speed = 20.0;
const float speed = 30.0;
const float flicker_fraction = 0.15;

// Theme settings
Expand All @@ -37,7 +37,7 @@ vec2 margin = vec2(0.03, 0.03);


/* The CRT glowing text effect, downsample, then upscale to cause a glowy blur */
vec4 crtGlow(in vec2 uv)
vec4 crtGlow(in vec2 uv, in float blurSize)
{
vec4 sum = vec4(0);
vec2 texcoord = uv.xy;
Expand Down Expand Up @@ -94,6 +94,10 @@ vec3 vigenette(in vec2 uv, in vec3 oricol)
The following code was borrowed from Cool-Retro-Term.
It creates a nice frame around the terminal screen.
*/
float rand(vec2 co)
{
return fract(sin(dot(co, vec2(12.9898, 78.233))) * 43758.5453);
}

float max2(vec2 v)
{
Expand Down Expand Up @@ -139,6 +143,17 @@ vec4 crtFrame(in vec2 staticCoords, in vec2 uv)

float screenShadow = 1.0 - prod2(positiveLog(coords * screenShadowCoeff + vec2(1.0)) * positiveLog(-coords * screenShadowCoeff + vec2(screenShadowCoeff + 1.0)));
alpha = max(0.8 * screenShadow, alpha);

/* Calculate normal vector to the center */
vec2 normal = staticCoords - vec2(0.4, 0.4);
normal = normal / length(normal);

/* Sample glow + blur for a decent reflection of the content on frame */
for(int i = 0; i < 10; i++)
{
color = mix(color, crtGlow(staticCoords - (i/256.0)*normal, 1/256.0).rgb, 0.01);
}

return vec4(color*alpha, alpha);
}

Expand All @@ -161,7 +176,7 @@ void main(void)
}
else {
float apply = abs(sin(texCoord.y)*0.5*scan);
fragColor = vec4(mix(crtGlow(uv).rgb, vec3(0.0),apply),1.0);
fragColor = vec4(mix(crtGlow(uv, blurSizeDefault).rgb, vec3(0.0),apply),1.0);
fragColor = vec4(mix(fragColor.rgb, length(texture(crt_background, uv).rgb)*back_color, background_brightness),1.0);
/* Add a scanline going up and down */
fragColor.rgb += scanline_intensity * exp(-1.0*abs((1/scanline_spread) * sin((uv.y - abs(cos(scanline_speed*time)))))) * back_color;
Expand Down

0 comments on commit 53e2402

Please sign in to comment.