-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import dom | ||
|
||
type | ||
CanvasRenderingContext* = ref object | ||
fillStyle* {.importc.}: cstring | ||
strokeStyle* {.importc.}: cstring | ||
|
||
{.push importcpp.} | ||
|
||
proc getContext*(canvasElement: Element, | ||
contextType: cstring): CanvasRenderingContext | ||
|
||
proc fillRect*(context: CanvasRenderingContext, x, y, width, height: int) | ||
|
||
proc moveTo*(context: CanvasRenderingContext, x, y: int) | ||
|
||
proc lineTo*(context: CanvasRenderingContext, x, y: int) | ||
|
||
proc stroke*(context: CanvasRenderingContext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import canvas, dom | ||
|
||
proc onLoad() {.exportc.} = | ||
var canvas = document.getElementById("canvas").EmbedElement | ||
canvas.width = window.innerWidth | ||
canvas.height = window.innerHeight | ||
var ctx = canvas.getContext("2d") | ||
|
||
ctx.fillStyle = "#1d4099" | ||
ctx.fillRect(0, 0, window.innerWidth, window.innerHeight) | ||
|
||
ctx.strokeStyle = "#ffffff" | ||
let letterWidth = 100 | ||
let letterLeftPos = (window.innerWidth div 2) - (letterWidth div 2) | ||
ctx.moveTo(letterLeftPos, 320) | ||
ctx.lineTo(letterLeftPos, 110) | ||
ctx.lineTo(letterLeftPos + letterWidth, 320) | ||
ctx.lineTo(letterLeftPos + letterWidth, 110) | ||
ctx.stroke() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"/> | ||
<title>Nim in Action - Chapter 8</title> | ||
<script type="text/javascript" src="canvas_test.js"></script> | ||
<style type="text/css"> | ||
canvas { border: 1px solid black; width: 100%; height: 100%; } | ||
</style> | ||
</head> | ||
<body onload="onLoad();" style="margin: 0; overflow: hidden;"> | ||
<canvas id="canvas"></canvas> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
when defined(Windows): | ||
const libName* = "SDL2.dll" | ||
elif defined(Linux): | ||
const libName* = "libSDL2.so" | ||
elif defined(MacOsX): | ||
const libName* = "libSDL2.dylib" | ||
|
||
type | ||
SdlWindow = object | ||
SdlWindowPtr* = ptr SdlWindow | ||
SdlRenderer = object | ||
SdlRendererPtr* = ptr SdlRenderer | ||
|
||
const INIT_VIDEO* = 0x00000020 | ||
|
||
{.push dynlib: libName.} | ||
proc init*(flags: uint32): cint {.importc: "SDL_Init".} | ||
|
||
proc createWindowAndRenderer*(width, height: cint, window_flags: cuint, | ||
window: var SdlWindowPtr, renderer: var SdlRendererPtr): cint | ||
{.importc: "SDL_CreateWindowAndRenderer".} | ||
|
||
proc pollEvent*(event: pointer): cint {.importc: "SDL_PollEvent".} | ||
|
||
proc setDrawColor*(renderer: SdlRendererPtr, r, g, b, a: uint8): cint | ||
{.importc: "SDL_SetRenderDrawColor", discardable.} | ||
|
||
proc present*(renderer: SdlRendererPtr) {.importc: "SDL_RenderPresent".} | ||
|
||
proc clear*(renderer: SdlRendererPtr) {.importc: "SDL_RenderClear".} | ||
|
||
proc drawLines*(renderer: SdlRendererPtr, points: ptr tuple[x, y: cint], | ||
count: cint): cint {.importc: "SDL_RenderDrawLines", discardable.} | ||
{.pop.} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import os | ||
import sdl | ||
|
||
if sdl.init(INIT_VIDEO) == -1: | ||
quit("Couldn't initialise SDL") | ||
|
||
var window: SdlWindowPtr | ||
var renderer: SdlRendererPtr | ||
if createWindowAndRenderer(640, 480, 0, window, renderer) == -1: | ||
quit("Couldn't create a window or renderer") | ||
|
||
discard pollEvent(nil) | ||
renderer.setDrawColor 29, 64, 153, 255 | ||
renderer.clear | ||
renderer.setDrawColor 255, 255, 255, 255 | ||
var points = [ | ||
(260'i32, 320'i32), | ||
(260'i32, 110'i32), | ||
(360'i32, 320'i32), | ||
(360'i32, 110'i32) | ||
] | ||
renderer.drawLines(addr points[0], points.len.cint) | ||
|
||
renderer.present | ||
sleep(5000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{.passL: "-lsfml-graphics -lsfml-system -lsfml-window".} | ||
|
||
type | ||
VideoMode* {.importcpp: "sf::VideoMode".} = object | ||
RenderWindowObj {.importcpp: "sf::RenderWindow".} = object | ||
RenderWindow* = ptr RenderWindowObj | ||
Color* {.importcpp: "sf::Color".} = object | ||
Event* {.importcpp: "sf::Event".} = object | ||
|
||
{.push cdecl, header: "<SFML/Graphics.hpp>".} | ||
|
||
proc videoMode*(modeWidth, modeHeight: cuint, modeBitsPerPixel: cuint = 32): VideoMode | ||
{.importcpp: "sf::VideoMode(@)", constructor.} | ||
|
||
proc newRenderWindow*(mode: VideoMode, title: cstring): RenderWindow | ||
{.importcpp: "new sf::RenderWindow(@)", constructor.} | ||
|
||
proc pollEvent*(window: RenderWindow, event: var Event): bool | ||
{.importcpp: "#.pollEvent(@)".} | ||
|
||
proc newColor*(red, green, blue, alpha: uint8): Color | ||
{.importcpp: "sf::Color(@)", constructor.} | ||
|
||
proc clear*(window: RenderWindow, color: Color) {.importcpp: "#.clear(@)".} | ||
|
||
proc display*(window: RenderWindow) {.importcpp: "#.display()".} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import sfml, os | ||
var window = newRenderWindow(videoMode(800, 600), "SFML works!") | ||
|
||
var event: Event | ||
discard window.pollEvent(event) | ||
window.clear(newColor(29, 64, 153, 255)) | ||
window.display() | ||
|
||
sleep(1000) |