Skip to content

Instantly share code, notes, and snippets.

@maavelar5
Created March 30, 2021 22:46
Show Gist options
  • Save maavelar5/e2648d0b69e3936f58e257ec1cbc488f to your computer and use it in GitHub Desktop.
Save maavelar5/e2648d0b69e3936f58e257ec1cbc488f to your computer and use it in GitHub Desktop.
Useless Bits Of Code - Graphical Cat-less
#include <cassert>
#include <ctime>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "font.xpm"
const int W = 1280;
const int H = 540;
namespace io
{
std::vector<std::string> open (std::string file_name)
{
std::vector<std::string> content;
std::ifstream file (file_name);
if (file.is_open ())
{
std::string line;
while (std::getline (file, line))
{
content.push_back (line);
}
}
return content;
}
}
namespace core
{
struct Buffer
{
int x, y;
std::vector<std::string> content;
void init ()
{
x = y = 0;
content.push_back ("empty buffer");
}
void init (std::string file_name)
{
x = y = 0;
content = io::open (file_name);
}
};
std::vector<Buffer> buffers;
Buffer active_buffer;
}
namespace ui
{
SDL_Window * window;
SDL_Renderer *renderer;
void init ()
{
SDL_Init (SDL_INIT_EVERYTHING);
window
= SDL_CreateWindow ("MTE", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, W, H, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer (window, -1, SDL_RENDERER_ACCELERATED);
SDL_RenderSetLogicalSize (renderer, W, H);
}
}
// 5s 2z g6
namespace font
{
int texture_w, texture_h;
SDL_Texture *texture;
std::vector<char> alphabet = {
'a', 'b', 'c', 'd', 'e', //
'f', 'g', 'h', 'i', 'j', //
'k', 'l', 'm', 'n', 'o', //
'p', 'q', 'r', 's', 't', //
'u', 'v', 'w', 'x', 'y', //
'z', //
'A', 'B', 'C', 'D', 'E', //
'F', 'G', 'H', 'I', 'J', //
'K', 'L', 'M', 'N', 'O', //
'P', 'Q', 'R', 'S', 'T', //
'U', 'V', 'W', 'X', 'Y', //
'Z',
'0', '1', '2', '3', '4', //
'5', '6', '7', '8', '9', //
':', '.', '!', '/', '{', //
'(', '[', ']', ')', '}', //
';', '<', '>', '=', '"', //
'_', ',', '+', '#', '-', //
'\'', ' ',
};
std::vector<std::string> keywords = {
"void", "int", "include", "typedef", "namespace",
"if", "else", "return", "struct", "const",
"auto", "bool", "for", "while", "SDL_Texture",
"SDL_Window", "SDL_Renderer", "break", "case",
};
std::vector<char> singles = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '(', ')', '{', '}', '<', '>',
};
int find (char c)
{
for (int i = 0; i < alphabet.size (); i++)
{
if (c == alphabet[i])
{
return i;
}
}
return -1;
};
void init ()
{
SDL_Surface *surface = IMG_ReadXPMFromArray (font_xpm);
texture_w = surface->w;
texture_h = surface->h;
texture = SDL_CreateTextureFromSurface (ui::renderer, surface);
SDL_FreeSurface (surface);
}
void draw (std::string word, SDL_Rect &pos)
{
SDL_Rect tile = { 0, 0, 12, 12 };
SDL_Color color;
SDL_GetTextureColorMod (texture, &color.r, &color.g, &color.b);
for (auto element : word)
{
SDL_Rect pos_ = pos;
int number = find (element);
tile.y = (number * tile.w) / texture_w;
tile.x = (number * tile.w) - (tile.y * texture_w);
tile.y *= tile.w;
for (auto single : singles)
{
if (single == element)
{
SDL_Color highlight = { 100, 100, 255, 255 };
SDL_SetTextureColorMod (texture, highlight.r, highlight.g,
highlight.b);
break;
}
}
SDL_RenderCopy (ui::renderer, texture, &tile, &pos_);
SDL_SetTextureColorMod (texture, color.r, color.g, color.b);
pos.x += pos.w;
}
}
void draw (core::Buffer b)
{
SDL_Rect pos = { 0, 0, 12, 12 };
SDL_Color color = { 255, 255, 255, 255 };
SDL_Color highlight = { 0, 255, 255, 255 };
for (auto line = b.content.begin () + b.y; line != b.content.end ();
line++)
{
pos.x = 0;
std::string shit = "";
bool found = false, render = false;
for (auto i = line->begin (); i != line->end (); i++)
{
auto c = *i;
if (c == ' ' || i == (line->end () - 1))
{
if (i == (line->end () - 1))
{
shit += c;
}
for (auto keyword : keywords)
{
if (keyword == shit)
{
found = true;
}
}
render = true;
}
if (i != (line->end () - 1))
{
shit += c;
}
if (render)
{
if (found)
{
SDL_SetTextureColorMod (texture, highlight.r,
highlight.g, highlight.b);
draw (shit, pos);
found = false;
}
else
{
SDL_SetTextureColorMod (texture, 255, 255, 255);
draw (shit, pos);
}
shit = "";
render = false;
}
}
pos.y += pos.h + 8;
}
}
}
int main (int argc, char **argv)
{
bool run = true;
srand (time (NULL));
ui::init ();
font::init ();
core::Buffer b;
assert (argc != 1);
b.init (argv[1]);
SDL_Event event;
while (run)
{
while (SDL_PollEvent (&event))
{
if (event.type == SDL_QUIT)
{
run = false;
}
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_j: b.y++; break;
case SDLK_k: b.y--; break;
case SDLK_q: run = false; break;
}
}
}
if (b.y < 0)
{
b.y = 0;
}
else if (b.y > b.content.size ())
{
b.y = b.content.size ();
}
SDL_SetRenderDrawColor (ui::renderer, 0, 0, 0, 255);
SDL_RenderClear (ui::renderer);
SDL_Rect background = { 0, 0, W, H };
SDL_SetRenderDrawColor (ui::renderer, 15, 15, 15, 255);
SDL_RenderFillRect (ui::renderer, &background);
font::draw (b);
SDL_RenderPresent (ui::renderer);
}
SDL_Quit ();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment