aboutsummaryrefslogtreecommitdiff
path: root/main.ha
diff options
context:
space:
mode:
Diffstat (limited to 'main.ha')
-rw-r--r--main.ha71
1 files changed, 19 insertions, 52 deletions
diff --git a/main.ha b/main.ha
index 89d197b..ca0c68e 100644
--- a/main.ha
+++ b/main.ha
@@ -5,6 +5,8 @@ export fn main() void = {
sdl2::SDL_Init(sdl2::SDL_INIT_VIDEO)!;
defer sdl2::SDL_Quit();
+ fmt::println(size(i32), size(i64), size(int), size(size))!;
+
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);
@@ -21,14 +23,16 @@ export fn main() void = {
fmt::println(wsurf.w * wsurf.h)!;
fmt::println(sdl2::SDL_GetPixelFormatName(format.format))!;
- const pic = picture_from_surface(wsurf);
+ let dstate = drawing_state {
+ drawing = false,
+ pos = (0,0),
+ picture = 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) {
@@ -38,20 +42,22 @@ export fn main() void = {
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);
+ const edata = ev.button;
+ dstate.pos = (edata.x, edata.y);
+ if (edata.button == 1)
+ dstate.drawing = (edata.state == 1);
case sdl2::SDL_EventType::MOUSEMOTION =>
- const d = ev.motion;
- mousex = d.x;
- mousey = d.y;
+ const edata = ev.motion;
+ dstate.pos = (edata.x, edata.y);
case => void;
};
+ do_drawing(&dstate);
- if (drawing) circle(pic, mousex, mousey, 25, 0x00ffff);
+
+ // if (drawing) {
+ // circle(pic, mousex, mousey, 5, 0x00ffff);
+ // };
// circle(pic, 100, 100, n:int, 0xff0000);
@@ -70,43 +76,4 @@ export fn main() void = {
};
};
-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);
- };
- };
- };
-
-};