Checking in nim version

This commit is contained in:
Joseph Ferano 2023-11-05 10:14:18 +07:00
parent 4d5ad1a562
commit 44ffba0aa4
4 changed files with 127 additions and 0 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
/cosmic-text/ /cosmic-text/
/__pycache__/
/nim-version/fide

1
nim-version/.nimble Normal file
View File

@ -0,0 +1 @@
require "sdl2"

124
nim-version/fide.nim Normal file
View File

@ -0,0 +1,124 @@
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:
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)
# echo "W ", *surface.w "H ", *surface.h
var r = rect(0, cint(i * 20), 100, 20)
renderer.copy texture, nil, addr r
surface.freeSurface
defer: texture.destroy
renderer.present()
main()

Binary file not shown.