Skip to content

Commit

Permalink
Added bit shifting example
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deluna committed Jan 22, 2024
1 parent 24246b7 commit 24fd6a5
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 7 deletions.
6 changes: 6 additions & 0 deletions docs/examples/00-about-examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ In this section you will find a collection of examples that demonstrate how to u

<div class="grid cards" markdown>

- __Visaulizing Bitshifting__

---

[![Example](./../../docs-assets/php-glfw/examples/vg/fun_bitshifting_thumb.png)](./vector-graphics/fun_bitshifting.md)

- __Plotting primes__

---
Expand Down
26 changes: 26 additions & 0 deletions docs/examples/vector-graphics/fun_bitshifting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Visaulizing Bitshifting

The first time you saw ">>" or "<<" in a programming language, you probably wondered WTF is this?
Or at least I did. The concept is actually quite simple, but it's hard to grasp at first.
Visaualizing it helps and allowing to play around with it helps a lot.

<figure markdown>
![Visaulizing Bitshifting Example (PHP-GLFW VG)](./../../docs-assets/php-glfw/examples/vg/fun_bitshifting.png){ width="600" }
</figure>

<div style="text-align: center;" markdown>
[Check out the Code](https://github.com/mario-deluna/php-glfw/blob/master/examples/vg/fun_bitshifting.php){ .md-button .md-button--primary }
</div>

Run this example:

```
php examples/vg/fun_bitshifting.php
```

Controls:

- Click on a bit to toggle it
- Click on the << and >> buttons to shift the bits
- Click on the button on the top to set the number to a constant

39 changes: 32 additions & 7 deletions docs/overrides/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@
top: 0;
}

.md-typeset .md-button--white {
border-color: #fff;
color: #fff;
}

.phpglfw-hero-logo {
text-align: center;
}
Expand Down Expand Up @@ -188,6 +193,18 @@
margin-top: 1rem;
}

.ms-1 {
margin-left: 0.5rem;
}

.me-1 {
margin-right: 0.5rem;
}

.me-2 {
margin-right: 1rem;
}

.lg-desc {
font-size: 1.4em;
}
Expand Down Expand Up @@ -417,16 +434,24 @@ <h2>Vector Graphics API</h2>
<div class="p-3 ps-0 pt-0">

<p>Sometimes, you just want something simple...</p>
<img src="{{ './../docs-assets/php-glfw/homepage/rect_basic_move_code.png' | url }}"
<img src="{{ './../docs-assets/php-glfw/homepage/rect_basic_move_code2.png' | url }}"
class="lg-image-shadow" />
<a href="{{ '/user-guide/vector-graphics/creating_a_vgcontext.html' | url }}"
class="md-button md-button--primary mt-2">
Learn more
</a>

<p>We have a ton of <strong>Interactive Examples</strong> for the Vector Graphics API as it is just fun to create things with it.</p>

<a href="{{ '/user-guide/vector-graphics/creating_a_vgcontext.html' | url }}"
class="md-button md-button--primary mt-2 me-1">
Learn more
</a>
<a href="/examples/00-about-examples.html#vector-graphics-examples"
class="md-button md-button--white mt-2">
Examples
</a>
</div>
<div>
<img class="lg-image-shadow"
src="{{ './../docs-assets/php-glfw/homepage/rect_basic_move.gif' | url }}" />
<!-- <img class="lg-image-shadow"
src="{{ './../docs-assets/php-glfw/homepage/rect_basic_move.gif' | url }}" /> -->
<img class ="lg-image-shadow" src="{{ './../docs-assets/php-glfw/user_guide/vg/shapes/rect_demo.gif' | url }}" />
</div>
</div>
</div>
Expand Down
24 changes: 24 additions & 0 deletions examples/99_example_helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,11 @@ public function easeInOutTo(string $key, float $to, int $duration) : void
$this->push($key, 'easeInOut', null, $to, $duration);
}

public function easeOutBounceTo(string $key, float $to, int $duration) : void
{
$this->push($key, 'easeOutBounce', null, $to, $duration);
}

public function getFrameLenght() : int
{
$max = 0;
Expand Down Expand Up @@ -904,6 +909,22 @@ private function alphaEaseInOut(float $progress) : float
{
return $progress < 0.5 ? $this->alphaEaseIn($progress * 2.0) * 0.5 : $this->alphaEaseOut($progress * 2.0 - 1.0) * 0.5 + 0.5;
}

private function alphaEaseOutBounce(float $progress) : float
{
if ($progress < 1.0 / 2.75) {
return 7.5625 * $progress * $progress;
} else if ($progress < 2.0 / 2.75) {
$progress -= 1.5 / 2.75;
return 7.5625 * $progress * $progress + 0.75;
} else if ($progress < 2.5 / 2.75) {
$progress -= 2.25 / 2.75;
return 7.5625 * $progress * $progress + 0.9375;
} else {
$progress -= 2.625 / 2.75;
return 7.5625 * $progress * $progress + 0.984375;
}
}

public function defaults(array $defaults) : self
{
Expand Down Expand Up @@ -1030,6 +1051,9 @@ public function build(float $time) : void
case 'easeInOut':
$progress = $this->alphaEaseInOut($progress);
break;
case 'easeOutBounce':
$progress = $this->alphaEaseOutBounce($progress);
break;
}

$value = $from + ($to - $from) * $progress;
Expand Down
154 changes: 154 additions & 0 deletions examples/vg/fun_bitshifting.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php
/**
* Visaulizing Bitshifting
*
* The first time you saw ">>" or "<<" in a programming language, you probably wondered WTF is this?
* Or at least I did. The concept is actually quite simple, but it's hard to grasp at first.
* Visaualizing it helps and allowing to play around with it helps a lot.
*
* Controls:
*
* - Click on a bit to toggle it
* - Click on the << and >> buttons to shift the bits
* - Click on the button on the top to set the number to a constant
*
* @category Fun
*/
/**
* We utilize the example helpers here to focus on what matter in this specifc example.
*/
require __DIR__ . '/../99_example_helpers.php';

use GL\Math\Vec2;
use GL\VectorGraphics\{VGAlign, VGContext, VGColor};

$window = ExampleHelper::begin();

// initalize the a vector graphics context
$vg = new VGContext(VGContext::ANTIALIAS);

// store if the mouse was pressed in the current frame
$didClick = false;
glfwSetMouseButtonCallback($window, function($button, $action, $mods) use (&$didClick) {
if ($button == GLFW_MOUSE_BUTTON_LEFT && $action == GLFW_PRESS) {
$didClick = true;
}
});


$number = 42;

// Main Loop
// ----------------------------------------------------------------------------
while (!glfwWindowShouldClose($window))
{
// clear
glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

$didClick = false;
glfwPollEvents();

// fetch the content scale of the window
$contentScaleX;
$contentScaleY;
glfwGetWindowContentScale($window, $contentScaleX, $contentScaleY);

// begin a new frame using the window size as the viewport size
$vg->beginFrame(ExampleHelper::WIN_WIDTH, ExampleHelper::WIN_HEIGHT, $contentScaleX);

ExampleHelper::drawCoordSys($vg);

// extract the first 8 bits of the number
// and draw them as 8 circles
$bits = 8;
$bitSize = 50;
$bitGutter = 10;
$bitsLen = $bits * $bitSize + ($bits - 1) * $bitGutter;
$startPos = ExampleHelper::WIN_WIDTH / 2 + $bitsLen / 2 - $bitSize / 2;
$y = ExampleHelper::WIN_HEIGHT / 2;

$vg->fillColor(VGColor::white());
for($i = 0; $i < 8; $i++) {
$bit = ($number >> $i) & 1;
$bitDec = pow(2, $i);

$vg->beginPath();
$vg->circle($startPos, $y, $bitSize / 2);
$vg->fillColor($bit ? VGColor::white() : VGColor::gray());

// simple bounding circle check to see if the mouse is over the bit
glfwGetCursorPos($window, $mouseX, $mouseY);
$mouseVec = new Vec2($mouseX, $mouseY);
$bitVec = new Vec2($startPos, $y);
if (Vec2::distance($mouseVec, $bitVec) < $bitSize / 2) {
$vg->fillColor(VGColor::lightGray());

// if mouse is pressed, toggle the bit
if ($didClick) {
$number ^= $bitDec;
}
}

// fill the bit circle
$vg->fill();

// label to show what decimal value the bit represents
$vg->fillColor(VGColor::gray());
$vg->fontSize(20);
$vg->textAlign(VGAlign::CENTER | VGAlign::MIDDLE);
$vg->text($startPos, $y + $bitSize / 2 + 20, (string)$bitDec);

$startPos -= $bitSize + $bitGutter;
}

// large decimal number on top
$vg->fillColor(VGColor::white());
$vg->fontSize(64);
$vg->textAlign(VGAlign::CENTER | VGAlign::MIDDLE);
$vg->text(ExampleHelper::WIN_WIDTH / 2, $y - 100, (string)$number);

// large hex number on bottom
$vg->fillColor(VGColor::gray());
$vg->fontSize(48);
$vg->textAlign(VGAlign::CENTER | VGAlign::MIDDLE);
$vg->text(ExampleHelper::WIN_WIDTH / 2, $y + 100, '0x' . dechex($number));


// create bits shift buttons to the left and right
$rightEnd = ExampleHelper::WIN_WIDTH / 2 + $bitsLen / 2;
$leftEnd = ExampleHelper::WIN_WIDTH / 2 - $bitsLen / 2;

ExampleHelper::drawButton($vg, $rightEnd + 100, $y, '>>', function() use (&$number) {
$number >>= 1;
});

ExampleHelper::drawButton($vg, $leftEnd - 100, $y, '<<', function() use (&$number) {
$number <<= 1;
});

// reset button
$x = 100;
$x = 30 + ExampleHelper::drawButton($vg, $x, 50, 'Reset', function() use (&$number) {
$number = 42;
}, 'left');
$x = 30 + ExampleHelper::drawButton($vg, $x, 50, '0', function() use (&$number) {
$number = 0;
}, 'left');
$x = 30 + ExampleHelper::drawButton($vg, $x, 50, '128', function() use (&$number) {
$number = 128;
}, 'left');
$x = 30 + ExampleHelper::drawButton($vg, $x, 50, '255', function() use (&$number) {
$number = 255;
}, 'left');


// end the frame will dispatch all the draw commands to the GPU
$vg->endFrame();

// swap the windows framebuffer and
// poll queued window events.
glfwSwapBuffers($window);
}

ExampleHelper::stop($window);

0 comments on commit 24fd6a5

Please sign in to comment.