Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draw to render texture, then show in window does not seem to work. #61

Open
ruffianeo opened this issue Dec 27, 2024 · 1 comment
Open

Comments

@ruffianeo
Copy link

;; Main Loop
(loop
      with compute = t
      while (not (window-should-close))
      when compute
        do (progn
             (pre-compute-mandel-set mandel)  ;; renders mandel set into texture using DrawPixel
             (setf compute nil))
      do (let ((game-time (cl-raylib:get-time)))
           (begin-drawing)
           (clear-background
            *status-panel-background-color*)
           (render-visual mandel game-time) ;; see below - simply draws the texture.
           (end-drawing)))

;; Pre-compute
(defun pre-compute-mandel-set (state)
  (unless (mandel-target-texture state)
    (setf (mandel-target-texture state)
          (load-render-texture (visual-width state) (visual-height state))))
  (format t "computing...")
  (begin-texture-mode (mandel-target-texture state)) ;; tests in C show I don't need clear-background here.
  (loop
    for y below (visual-height state)
    do (loop
         for x below (visual-width state)
         do (draw-pixel
             x y (mandel-core
                  state
                  y x))))
  (draw-line (visual-left state)
                 (visual-top state)
                 (+ (visual-left state) (visual-width state))
                 (+ (visual-top state) (visual-height state))
                 (make-rgba 255 0 0))
  (end-texture-mode)
  (format t "...done!~%"))

;; transfer from texture to screen
(defmethod render-visual ((state mandel-state) game-time)
  (let ((target-texture (mandel-target-texture state)))
    (when target-texture
      (draw-texture
       (render-texture-texture target-texture)
       (visual-left state) (visual-top state) (make-rgba 0 0 0)))))

Neither the set pixels nor the line drawn to the texture ever show on the screen.
To see if it is raylib or the bindings (or me?!), here my C test code:

#include <raylib.h>
#include <stddef.h>

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600

static void pre_render_something(RenderTexture2D*texture) {
  int x;
  int y;
  Color color;
  
  BeginTextureMode(*texture);
  for (size_t i = 0; i< 1000; i++) {
    x = GetRandomValue(0,WINDOW_WIDTH);
    y = GetRandomValue(0,WINDOW_HEIGHT);
    color.r = GetRandomValue(0,255);
    color.g = GetRandomValue(0,255);
    color.b = GetRandomValue(0,255);
    color.a = 255;
    DrawPixel(x,y,color);
    DrawPixel(x+1,y,color);
    DrawPixel(x,y+1,color);
    DrawPixel(x+1,y+1,color);
  }
  EndTextureMode();
}

int main () {
  InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Draw Pixels into texture.");

  SetRandomSeed(6969);
  SetTargetFPS(20);
  
  RenderTexture2D tex = LoadRenderTexture(WINDOW_WIDTH,WINDOW_HEIGHT);
  pre_render_something(&tex);
  
  while (!WindowShouldClose()) {
    BeginDrawing();
    ClearBackground(RAYWHITE);
    DrawTexture(tex.texture,0,0,RAYWHITE);
    EndDrawing();
  }
  CloseWindow();
    
  return 0;
}

With the C code (which nominally does the very same logical sequence as the lisp code), the pixels show as expected.

@ruffianeo
Copy link
Author

I recommend to close this issue, since the code below is working. So it must be a problem in my code, even though I am not sure, where. Storing the RenderTexture2D instance in a CLOS object might have to do with it.

(defun render-to-texture (&key
                     (window-width 1024)
                     (window-height 1024))
  (init-window window-width window-height "Game of Life")
  (set-target-fps 20)
  (let ((texture (load-render-texture window-width window-height)))
    (begin-texture-mode texture)
    (draw-line 0 0 window-width window-height (make-rgba 255 255 0))
    (draw-pixel 100 200 (make-rgba 0 0 255))
    (draw-pixel 101 200 (make-rgba 0 0 255))
    (draw-pixel 100 201 (make-rgba 0 0 255))
    (draw-pixel 101 201 (make-rgba 0 0 255))
    (end-texture-mode)
    (loop
      while (not (window-should-close))
      do (progn
           (begin-drawing)
           (clear-background (make-rgba 0 0 0))
           (draw-texture (render-texture-texture texture)
                         0 0 (make-rgba 255 255 255))
           (end-drawing)))
    (unload-render-texture texture))
  (close-window))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant