summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorubq323 <ubq323@ubq323.website>2024-03-22 17:44:25 +0000
committerubq323 <ubq323@ubq323.website>2024-03-22 17:44:25 +0000
commit32f8d37ad94a9ea3413c3f9587bb2000f14c61d1 (patch)
tree53ff7db244087e7ace1e2d24360d44977575fb8c
parentf09d855593cc5405937d20571526f81583f0cb8b (diff)
step 1
-rw-r--r--main.ha86
-rw-r--r--spec1
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<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);
+ };
+ };
+ };
};
+
diff --git a/spec b/spec
index eef1146..1630116 100644
--- a/spec
+++ b/spec
@@ -1,4 +1,5 @@
program where you can draw with the mouse
+ DONE
program where you can draw with the mouse and it draws lines between the points
program where you can draw with the mouse and it interpolates nicely with beziers
program where you can do that multiplayerly