Browse Source

draw actual pixels from scheme world

master
Lukas F. Hartmann 5 days ago
parent
commit
c02162c479
3 changed files with 31 additions and 13 deletions
  1. +1
    -1
      build.sh
  2. +5
    -10
      init.scm
  3. +25
    -2
      interscheme.c

+ 1
- 1
build.sh View File

@@ -1,2 +1,2 @@
gcc -o interscheme interscheme.c -lchibi-scheme -lSDL2
gcc -g -o interscheme interscheme.c -lchibi-scheme -lSDL2


+ 5
- 10
init.scm View File

@@ -1,4 +1,3 @@

;;; defined in C code
;;; (define pixel-put (lambda (x y c) 0))

@@ -24,14 +23,10 @@
(pixel-vline x2 y1 y2 color)
)))

(define blit-char-list (lambda (x y str)
(if (< (length str) 1)
0
(begin
(blit-char (* cursor-w x) (* cursor-h y) (char->integer (car str)))
(blit-char-list (+ x 1) y (cdr str)))
)))
(display "hello from scheme world!\n")

(define blit-string (lambda (x y s)
(blit-char-list x y (string->list s))))
(define main (lambda ()
(begin
(display "(main)\n")
(pixel-rect 32 32 128 128 #xffffff))))


+ 25
- 2
interscheme.c View File

@@ -19,7 +19,6 @@ void sdl_pixel_put(int x, int y, uint32_t color)

static sexp pixel_put(sexp ctx, sexp self, sexp n, sexp sx_x, sexp sx_y, sexp sx_color) {
uint32_t color = (uint32_t)sexp_unbox_fixnum(sx_color);

sdl_pixel_put(sexp_unbox_fixnum(sx_x),sexp_unbox_fixnum(sx_y),(color));
return n;
}
@@ -39,9 +38,28 @@ static sexp pixel_rect_fill(sexp ctx, sexp self, sexp n, sexp sx_x, sexp sx_y, s
return n;
}

void scheme_define_ops(sexp ctx, sexp env) {
sexp op = sexp_define_foreign(ctx, env, "pixel-put", 3, (sexp_proc3)pixel_put);
if (sexp_opcodep(op)) {
sexp_opcode_return_type(op) = SEXP_VOID;
} else {
printf("could not register pixel-put!\n");
}
op = sexp_define_foreign(ctx, env, "pixel-rect-fill", 3, (sexp_proc5)pixel_rect_fill);
if (sexp_opcodep(op)) {
sexp_opcode_return_type(op) = SEXP_VOID;
} else {
printf("could not register pixel-rect-fill!\n");
}
}

void scheme_loop(sexp ctx) {
// TODO: error handling
// TODO: record elapsed time and input events and pass to main function for animation stuff?
sexp_eval_string(ctx, "(main)", -1, NULL);

//sdl_pixel_put(20,20,0xffffff);
SDL_UpdateTexture(sdlTexture, NULL, pixels, SCREEN_W * sizeof(uint32_t));
SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL);
SDL_RenderPresent(sdlRenderer);
@@ -60,12 +78,16 @@ int main(int argc, char** argv) {
SCREEN_W, SCREEN_H);
//SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); // make the scaled rendering look smoother.
SDL_RenderSetLogicalSize(sdlRenderer, SCREEN_W, SCREEN_H);

pixels = (uint32_t*)malloc(SCREEN_W*SCREEN_H*sizeof(uint32_t));
sexp_scheme_init();
ctx = sexp_make_eval_context(NULL, NULL, NULL, 0, 0);
ctx = sexp_make_eval_context(NULL, NULL, NULL, 1024*1024, 1024*1024);
sexp_load_standard_env(ctx, NULL, SEXP_SEVEN);
sexp_load_standard_ports(ctx, NULL, stdin, stdout, stderr, 1);

scheme_define_ops(ctx, sexp_context_env(ctx));

sexp_gc_var1(obj1);
sexp_gc_preserve1(ctx, obj1);
obj1 = sexp_c_string(ctx, "./init.scm", -1);
@@ -94,4 +116,5 @@ int main(int argc, char** argv) {

sexp_destroy_context(ctx);
SDL_Quit();
free(pixels);
}

Loading…
Cancel
Save