Browse Source

add some mouse input functions; add gitignore

audio
mntmn 2 months ago
parent
commit
610c207924
4 changed files with 77 additions and 24 deletions
  1. +2
    -0
      .gitignore
  2. +9
    -4
      init.scm
  3. BIN
     
  4. +66
    -20
      interscheme.c

+ 2
- 0
.gitignore View File

@@ -0,0 +1,2 @@
interscheme
*.swp

+ 9
- 4
init.scm View File

@@ -6,14 +6,14 @@
(define (pixel-hline x1 x2 y color)
(if (> x1 x2)
#t
(begin
(begin
(pixel-put x1 y color)
(pixel-hline (+ x1 1) x2 y color))))

(define (pixel-vline x y1 y2 color)
(if (> y1 y2)
#t
(begin
(begin
(pixel-put x y1 color)
(pixel-vline x (+ y1 1) y2 color))))

@@ -26,9 +26,14 @@
(display "hello from scheme world!\n")

(define (main)
(display "(main)\n")
;;; (display "(main)\n")
(pixel-rect 32 32 128 128 #xffffff)
(pixel-rect-fill 150 32 250 128 #xffff00)
(stroke-line 300 32 350 128 #x00ffffff)
)

(if (= (mouse-buttons) 1)
(pixel-rect-fill (mouse-x) (mouse-y) (+ (mouse-x) 8) (+ (mouse-y) 8) #xffffff))

(if (= (mouse-buttons) 4)
(pixel-rect-fill (mouse-x) (mouse-y) (+ (mouse-x) 32) (+ (mouse-y) 32) #x000000))
)

BIN
View File


+ 66
- 20
interscheme.c View File

@@ -15,6 +15,15 @@ SDL_Texture* sdlTexture;
uint32_t* pixels;
int running = 1;

typedef struct scheme_input {
uint32_t mouse_buttons;
int32_t mouse_x;
int32_t mouse_y;
uint32_t keycode;
} scheme_input_t;

static scheme_input_t scheme_input;

void sdl_pixel_put(int x, int y, uint32_t color)
{
*(pixels + y*SCREEN_W + x) = color;
@@ -62,7 +71,7 @@ void sdl_stroke_line(int x1, int y1, int x2, int y2, uint32_t color) {
// for readability in the future.
sexp stroke_line(sexp ctx, sexp self, sexp n, sexp sx_x1,
sexp sx_y1, sexp sx_x2, sexp sx_y2, sexp sx_color) {
uint32_t color = (uint32_t)sexp_uint_value(sx_color);
uint32_t color = (uint32_t)sexp_uint_value(sx_color);
uint32_t x1 = sexp_unbox_fixnum(sx_x1);
uint32_t y1 = sexp_unbox_fixnum(sx_y1);
uint32_t x2 = sexp_unbox_fixnum(sx_x2);
@@ -73,15 +82,31 @@ sexp stroke_line(sexp ctx, sexp self, sexp n, sexp sx_x1,
return sx_x1;
}

sexp get_mouse_x(sexp ctx, sexp self, sexp n) {
return sexp_make_integer(ctx, scheme_input.mouse_x);
}

sexp get_mouse_y(sexp ctx, sexp self, sexp n) {
return sexp_make_integer(ctx, scheme_input.mouse_y);
}

sexp get_mouse_buttons(sexp ctx, sexp self, sexp n) {
return sexp_make_integer(ctx, scheme_input.mouse_buttons);
}

// 'provide', a shortcut for sexp_define_foreign.
// Note that I don't know the difference between: sexp_proc1 and sexp_proc5
// (or sexp_proc2, 3, 4, 6, 7).
// See scheme_define_ops for examples, but in general it's used like:
// provide(ctx, env, "shceme-proc-name", 2, (sexp_proc1)c_function);
void provide(sexp ctx, sexp env, char* name, int num_args, sexp_proc1 func) {
void provide(sexp ctx, sexp env, char* name, int num_args, int return_type, void* func) {
sexp op = sexp_define_foreign(ctx, env, name, num_args, func);
if (sexp_opcodep(op)) {
sexp_opcode_return_type(op) = SEXP_VOID;
if (return_type == 0) {
sexp_opcode_return_type(op) = SEXP_VOID;
} else {
sexp_opcode_return_type(op) = SEXP_OBJECT;
}
} else {
printf("could not register %s!\n", name);
}
@@ -91,9 +116,12 @@ void provide(sexp ctx, sexp env, char* name, int num_args, sexp_proc1 func) {
// TODO: 'provide' might be too generic of a name
// (and thus, may conflict in the future).
void scheme_define_ops(sexp ctx, sexp env) {
provide(ctx, env, "pixel-put", 3, (sexp_proc1)pixel_put);
provide(ctx, env, "stroke-line", 5, (sexp_proc1)stroke_line);
provide(ctx, env, "pixel-rect-fill", 5, (sexp_proc1)pixel_rect_fill);
provide(ctx, env, "pixel-put", 3, 0, (sexp_proc4)pixel_put);
provide(ctx, env, "stroke-line", 5, 0, (sexp_proc6)stroke_line);
provide(ctx, env, "pixel-rect-fill", 5, 0, (sexp_proc6)pixel_rect_fill);
provide(ctx, env, "mouse-x", 0, 1, (sexp_proc1)get_mouse_x);
provide(ctx, env, "mouse-y", 0, 1, (sexp_proc1)get_mouse_y);
provide(ctx, env, "mouse-buttons", 0, 1, (sexp_proc1)get_mouse_buttons);
}

void scheme_loop(sexp ctx) {
@@ -102,10 +130,11 @@ void scheme_loop(sexp ctx) {
//sdl_pixel_put(20,20,0xffffff);
SDL_UpdateTexture(sdlTexture, NULL, pixels, SCREEN_W * sizeof(uint32_t));
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
sexp_eval_string(ctx, "(main)", -1, NULL);
sexp_eval_string(ctx, "(main)", -1, NULL);
SDL_RenderPresent(sdlRenderer);
// Simple framerate limiter, so Interscheme doesn't eat CPU power during tests.
SDL_Delay(1000 / FPS);
// FIXME: this makes the actual processing too slow for me.
//SDL_Delay(1000 / FPS);
}

int main(int argc, char** argv) {
@@ -152,24 +181,41 @@ int main(int argc, char** argv) {
}

while (running) {
scheme_loop(ctx);
if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = 0;
break;
running = 0;
break;
case SDL_KEYDOWN:
{
int c = event.key.keysym.sym;
printf("key: %d\n",c);
if (c==27) {
// esc
running = 0;
}
break;
}
{
int c = event.key.keysym.sym;
printf("key: %d\n",c);
if (c==27) {
// Escape
running = 0;
}
break;
}
case SDL_MOUSEMOTION:
{
scheme_input.mouse_x = event.motion.x;
scheme_input.mouse_y = event.motion.y;
break;
}
case SDL_MOUSEBUTTONDOWN:
{
scheme_input.mouse_buttons |= (1<<(event.button.button-1));
break;
}
case SDL_MOUSEBUTTONUP:
{
scheme_input.mouse_buttons &= ~(1<<(event.button.button-1));
break;
}
}
}

scheme_loop(ctx);
}

sexp_destroy_context(ctx);


Loading…
Cancel
Save