Browse Source

Use logical scaling for proper positioning

The aspect ratio centering correction got a bit gnarly
master
Macoy Madson 3 years ago
parent
commit
3b76e03e48
  1. BIN
      assets/Board.xcf
  2. BIN
      assets/Win.xcf
  3. BIN
      data/Board.bmp
  4. BIN
      data/Win.bmp
  5. 53
      src/Main.cake

BIN
assets/Board.xcf

Binary file not shown.

BIN
assets/Win.xcf

Binary file not shown.

BIN
data/Board.bmp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 9.6 MiB

After

Width:  |  Height:  |  Size: 9.6 MiB

BIN
data/Win.bmp

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 MiB

After

Width:  |  Height:  |  Size: 1.8 MiB

53
src/Main.cake

@ -552,6 +552,9 @@ Rush Hour database from Michael Fogleman.\n\n")
(var renderer (* SDL_Renderer) null)
(unless (sdl-intialize-2d-renderer (addr renderer) window) (return 1))
;; Scale the render to fit the window
(SDL_RenderSetLogicalSize renderer g-window-width g-window-height)
(var background-texture (* SDL_Texture) (sdl-texture-from-bmp (in-data-dir "Board.bmp")
renderer))
(unless background-texture (return 1))
@ -624,6 +627,48 @@ Rush Hour database from Michael Fogleman.\n\n")
(var mouse-y int 0)
(var mouse-button-state Uint32 (SDL_GetMouseState (addr mouse-x) (addr mouse-y)))
(var mouse-position vec2 (array (type-cast mouse-x float) (type-cast mouse-y float)))
(scope ;; Scale mouse position to match logical rendering positions
(var window-width int 1)
(var window-height int 1)
(SDL_GetWindowSize window (addr window-width) (addr window-height))
(var logical-width int 1)
(var logical-height int 1)
(SDL_RenderGetLogicalSize renderer (addr logical-width) (addr logical-height))
(when (or (= 0 window-width)
(= 0 window-height)
(= 0 logical-width)
(= 0 logical-height))
(set exit-reason "error: window or logical dimension will cause divide by zero")
(break))
(var to-logical-scale vec2
(array
(/ (to-float logical-width) window-width)
(/ (to-float logical-height) window-height)))
;; (printf "Window scale %f %f\n" (vec-xy to-logical-scale))
(var logical-aspect-ratio float (/ logical-width (to-float logical-height)))
(var window-aspect-ratio float (/ window-width (to-float window-height)))
(if (> window-aspect-ratio logical-aspect-ratio) ;; Remove centering for correct scaling
(block
(var total-margin float
(- window-width (* window-width (/ logical-aspect-ratio window-aspect-ratio))))
(set (vec-x to-logical-scale) (/ logical-width (- window-width total-margin)))
(var decentered float
(/ total-margin
2.f))
(set (vec-x mouse-position)
(- (vec-x mouse-position)
decentered)))
(block
(var total-margin float
(- window-height (* window-height (/ window-aspect-ratio logical-aspect-ratio))))
(set (vec-y to-logical-scale) (/ logical-height (- window-height total-margin)))
(var decentered float
(/ total-margin
2.f))
(set (vec-y mouse-position)
(- (vec-y mouse-position)
decentered))))
(set mouse-position (vec2-multiply mouse-position to-logical-scale)))
(if (bit-and mouse-button-state SDL_BUTTON_LMASK)
(block
@ -668,6 +713,14 @@ Rush Hour database from Michael Fogleman.\n\n")
(sdl-print-error)
(set exit-reason "Render error")))
;; Pointer position
;; (scope ;; TODO REMOVE
;; (var src-rect SDL_Rect (array (vec-xy-to-int piece-primary-origin)
;; 166 166))
;; (var dest-rect SDL_Rect (array (vec-xy-to-int mouse-position)
;; 166 166))
;; (SDL_RenderCopy renderer pieces-texture (addr src-rect) (addr dest-rect)))
(scope ;; Draw grid
(var row int 0)
(while (< row g-game-board-grid-size)

Loading…
Cancel
Save