From 32f8d37ad94a9ea3413c3f9587bb2000f14c61d1 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Fri, 22 Mar 2024 17:44:25 +0000 Subject: step 1 --- main.ha | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++-------------- spec | 1 + 2 files changed, 69 insertions(+), 18 deletions(-) diff --git a/main.ha b/main.ha index 42d42cf..89d197b 100644 --- a/main.ha +++ b/main.ha @@ -5,6 +5,7 @@ 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); @@ -18,45 +19,94 @@ export fn main() void = { 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 pixels = (wsurf.pixels as *opaque: *[*]u32)[..wsurf.w * wsurf.h]; - + 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 => { + case sdl2::SDL_EventType::KEYDOWN => const keysym = ev.key.keysym.sym; if (keysym == sdl2::SDL_Keycode::ESCAPE) quit = true; - fmt::println("got key", keysym: int)!; - }; + 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; }; - for (let i = 0z; i < 100; i+=1) { - pixels[n] = sdl2::SDL_MapRGB(format, 0, 255, 0); - n += 1; - }; + if (drawing) circle(pic, mousex, mousey, 25, 0x00ffff); + + // circle(pic, 100, 100, n:int, 0xff0000); sdl2::SDL_UpdateWindowSurface(win)!; - sdl2::SDL_Delay(1000 / 60); + 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)!; + // }; + }; +}; - // sdl2::SDL_SetRenderDrawColor(render, 50, 50, 50, 255)!; - // sdl2::SDL_RenderClear(render)!; +def WHITE = 0x556602; +def BLACK = 0x0; - // rect.x = (sdl2::SDL_GetTicks(): int)/100; - // sdl2::SDL_SetRenderDrawColor(render, 255, 0, 0, 0)!; - // sdl2::SDL_RenderFillRect(render, &rect)!; +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], +}; - // sdl2::SDL_RenderPresent(render); - // sdl2::SDL_Delay(1000 / 60); - }; +fn min(a: int, b: int) int = if (a