import os import std/strformat import std/strutils import sdl2 import sdl2/ttf const WindowWidth = 900 WindowHeight = 900 OSWinX: cint = 1940 OSWinY: cint = 50 LineHeight = 20 type Cursor = object width: float height: float line_num: int = 0 col: int = 0 want_col: int = 0 Buffer = object lines: seq[string] cursor: Cursor func bufferNew(text: string, cursor: Cursor): Buffer = return Buffer(lines: text.split("\n"), cursor: cursor) type SDLException = object of Defect template sdlFailIf(condition: typed, reason: string) = if condition: raise SDLException.newException( reason & ", SDL error " & $getError() ) proc main = sdlFailIf(not sdl2.init(INIT_VIDEO or INIT_TIMER or INIT_EVENTS)): "SDL2 initialization failed" defer: sdl2.quit() let window = createWindow( title = "TODE", x = OSWinX, y = OSWinY, w = WindowWidth, h = WindowHeight, flags = SDL_WINDOW_SHOWN or SDL_WINDOW_RESIZABLE ) sdlFailIf window.isNil: "window could not be created" defer: window.destroy() let renderer = createRenderer( window = window, index = -1, flags = Renderer_Accelerated or Renderer_PresentVsync or Renderer_TargetTexture ) sdlFailIf renderer.isNil: "renderer could not be created" defer: renderer.destroy() sdlFailIf(not ttfInit()): "SDL_TTF initialization failed" defer: ttfQuit() let font = ttf.openFont("liberation-sans.ttf", 18) sdlFailIf font.isNil: "font could not be created" sdlFailIf(not setHint("SDL_RENDER_SCALE_QUALITY", "2")): "Linear texture filtering could not be enabled" var running = true dt: float32 counter: uint64 previousCounter: uint64 let textFile = readFile("../../gux/test.c") cursor = Cursor(width: 8, height: LineHeight) buffer = bufferNew(textFile, cursor) counter = getPerformanceCounter() while running: previousCounter = counter counter = getPerformanceCounter() dt = (counter - previousCounter).float / getPerformanceFrequency().float var event = defaultEvent while pollEvent(event): case event.kind of QuitEvent: running = false break of KeyDown: discard of KeyUp: discard else: discard renderer.setDrawColor 255, 255, 255, 255 # black renderer.clear() for i, line in buffer.lines: if isEmptyOrWhitespace(line): continue let textColor = color(0, 0, 0, 0) surface = ttf.renderTextBlended(font, line, textColor) texture = renderer.createTextureFromSurface(surface) sdlFailIf surface.isNil: "Could not render text surface" var r = rect(0, cint(i * 20), surface.w, surface.h) renderer.copy texture, nil, addr r surface.freeSurface defer: texture.destroy renderer.present() main()