2019-10-22 07:15:35 +02:00
|
|
|
/*
|
2019-10-20 23:55:36 +08:00
|
|
|
* The MIT License
|
2019-10-22 07:15:35 +02:00
|
|
|
* Copyright © 2014-2019 Ilkka Seppälä
|
|
|
|
*
|
2019-10-20 23:55:36 +08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
2019-10-22 07:15:35 +02:00
|
|
|
*
|
2019-10-20 23:55:36 +08:00
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
2019-10-22 07:15:35 +02:00
|
|
|
*
|
2019-10-20 23:55:36 +08:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.iluwatar.doublebuffer;
|
|
|
|
|
2019-11-10 23:01:20 +05:30
|
|
|
import java.util.List;
|
2019-10-20 23:55:36 +08:00
|
|
|
import org.apache.commons.lang3.tuple.MutablePair;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
2019-11-10 23:01:20 +05:30
|
|
|
* Double buffering is a term used to describe a device that has two buffers. The usage of multiple
|
|
|
|
* buffers increases the overall throughput of a device and helps prevents bottlenecks. This example
|
|
|
|
* shows using double buffer pattern on graphics. It is used to show one image or frame while a
|
|
|
|
* separate frame is being buffered to be shown next. This method makes animations and games look
|
|
|
|
* more realistic than the same done in a single buffer mode.
|
2019-10-20 23:55:36 +08:00
|
|
|
*/
|
|
|
|
public class App {
|
|
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Program main entry point.
|
2019-11-10 23:01:20 +05:30
|
|
|
*
|
2019-10-20 23:55:36 +08:00
|
|
|
* @param args runtime arguments
|
|
|
|
*/
|
|
|
|
public static void main(String[] args) {
|
2019-11-10 23:01:20 +05:30
|
|
|
final var scene = new Scene();
|
2019-12-15 00:02:45 +05:30
|
|
|
var drawPixels1 = List.of(
|
|
|
|
new MutablePair<>(1, 1),
|
|
|
|
new MutablePair<>(5, 6),
|
|
|
|
new MutablePair<>(3, 2)
|
|
|
|
);
|
|
|
|
scene.draw(drawPixels1);
|
2019-10-20 23:55:36 +08:00
|
|
|
var buffer1 = scene.getBuffer();
|
|
|
|
printBlackPixelCoordinate(buffer1);
|
|
|
|
|
2019-12-15 00:02:45 +05:30
|
|
|
var drawPixels2 = List.of(
|
|
|
|
new MutablePair<>(3, 7),
|
|
|
|
new MutablePair<>(6, 1)
|
|
|
|
);
|
|
|
|
scene.draw(drawPixels2);
|
|
|
|
var buffer2 = scene.getBuffer();
|
2019-10-20 23:55:36 +08:00
|
|
|
printBlackPixelCoordinate(buffer2);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void printBlackPixelCoordinate(Buffer buffer) {
|
2019-12-15 00:02:45 +05:30
|
|
|
StringBuilder log = new StringBuilder("Black Pixels: ");
|
|
|
|
var pixels = buffer.getPixels();
|
2019-10-20 23:55:36 +08:00
|
|
|
for (var i = 0; i < pixels.length; ++i) {
|
|
|
|
if (pixels[i] == Pixel.BLACK) {
|
|
|
|
var y = i / FrameBuffer.WIDTH;
|
|
|
|
var x = i % FrameBuffer.WIDTH;
|
2019-12-15 00:02:45 +05:30
|
|
|
log.append(" (").append(x).append(", ").append(y).append(")");
|
2019-10-20 23:55:36 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-15 00:02:45 +05:30
|
|
|
LOGGER.info(log.toString());
|
2019-10-20 23:55:36 +08:00
|
|
|
}
|
|
|
|
}
|