1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
use fmt;
use sdl2;
export fn main() void = {
sdl2::SDL_Init(sdl2::SDL_INIT_VIDEO)!;
defer sdl2::SDL_Quit();
const win = sdl2::SDL_CreateWindow("hi", sdl2::SDL_WINDOWPOS_UNDEFINED, sdl2::SDL_WINDOWPOS_UNDEFINED, 640, 480, sdl2::SDL_WindowFlags::NONE)!;
defer sdl2::SDL_DestroyWindow(win);
const wsurf = sdl2::SDL_GetWindowSurface(win)!;
const format = wsurf.format;
assert(format.bytesperpixel == 4, "can only work with u32 pixels");
fmt::printfln("got format {}. bytes per pixel {}. r{:x} g{:x} b{:x} a{:x}",
format.format, format.bytesperpixel, format.rmask, format.gmask, format.bmask, format.amask)!;
fmt::printfln("red: {:x} green {:x} blue {:x}",
sdl2::SDL_MapRGB(format, 255, 0, 0),
sdl2::SDL_MapRGB(format, 0, 255, 0),
sdl2::SDL_MapRGB(format, 0, 0, 255))!;
fmt::println(wsurf.w * wsurf.h)!;
fmt::println(sdl2::SDL_GetPixelFormatName(format.format))!;
const pic = picture_from_surface(wsurf);
let quit = false;
let n = 0z;
let lasttime = sdl2::SDL_GetTicks();
let drawing = false;
let mousex = 0;
let mousey = 0;
for (!quit) {
let ev = sdl2::event { ... };
for (sdl2::SDL_PollEvent(&ev)! == 1) switch (ev.event_type) {
case sdl2::SDL_EventType::QUIT => quit = true;
case sdl2::SDL_EventType::KEYDOWN =>
const keysym = ev.key.keysym.sym;
if (keysym == sdl2::SDL_Keycode::ESCAPE) quit = true;
case sdl2::SDL_EventType::MOUSEBUTTONDOWN,
sdl2::SDL_EventType::MOUSEBUTTONUP =>
const d = ev.button;
mousex = d.x;
mousey = d.y;
if (d.button == 1)
drawing = (d.state == 1);
case sdl2::SDL_EventType::MOUSEMOTION =>
const d = ev.motion;
mousex = d.x;
mousey = d.y;
case => void;
};
if (drawing) circle(pic, mousex, mousey, 25, 0x00ffff);
// circle(pic, 100, 100, n:int, 0xff0000);
sdl2::SDL_UpdateWindowSurface(win)!;
n += 1;
sdl2::SDL_Delay(1000/60);
// if (n % 100 == 0) {
// const thistime = sdl2::SDL_GetTicks();
// const dt = (thistime - lasttime): f64;
// lasttime = thistime;
// const dt = dt / 100.0;
// const fps = 1000.0/dt;
// fmt::printfln("fps: {}",fps)!;
// };
};
};
def WHITE = 0x556602;
def BLACK = 0x0;
type picture = struct {
d: []u32,
w: size,
h: size,
};
fn pidx(pic: picture, x: size, y: size) size = (pic.w*y)+x;
fn pic_set(pic: picture, x: size, y: size, val: u32) void = pic.d[pidx(pic,x,y)] = val;
fn picture_from_surface(surf: *sdl2::SDL_Surface) picture = picture {
w = surf.w: size,
h = surf.h: size,
d = (surf.pixels as *opaque: *[*]u32)[..surf.w*surf.h],
};
fn min(a: int, b: int) int = if (a<b) a else b;
fn max(a: int, b: int) int = if (a<b) b else a;
fn circle(pic: picture, cx: int, cy: int, r: int, color: u32) void = {
const ymin = max(0, cy-r);
const ymax = min(pic.h:int-1, cy+r);
const xmin = max(0, cx-r);
const xmax = min(pic.w:int-1, cx+r);
const r2 = r*r + r;
for (let y = ymin; y<=ymax; y+=1) {
const yd = y-cy;
for (let x = xmin; x<=xmax; x+=1) {
const xd = x-cx;
if (yd*yd + xd*xd <= r2) {
// we already made sure x and y are both greater than 0
pic_set(pic, x:size, y:size, color);
};
};
};
};
|