1
Java / 15%-20% CPU usage when drawing a grid
« on: February 05, 2014, 07:11:44 am »
Hello.
I decided to attempt a quick version of Conway's Game of Life to familiarize myself with JSFML. I noticed when simply drawing a grid of rectangles, my CPU usage will spike up to 15%-20% on my i5-3570K. Would anyone be able to tell me what I am doing wrong? This amount of CPU usage for a grid seems excessive, and I'm wondering if the draw calls are being performed by the CPU, instead of the GPU, or if they aren't being batched the way I imagine.
Here's the code I am using to reproduce the issue. If I add
I decided to attempt a quick version of Conway's Game of Life to familiarize myself with JSFML. I noticed when simply drawing a grid of rectangles, my CPU usage will spike up to 15%-20% on my i5-3570K. Would anyone be able to tell me what I am doing wrong? This amount of CPU usage for a grid seems excessive, and I'm wondering if the draw calls are being performed by the CPU, instead of the GPU, or if they aren't being batched the way I imagine.
Here's the code I am using to reproduce the issue. If I add
window.setVerticalSyncEnabled(true);
to my code, the CPU usage will spike up to 25%-30%! I appreciate any help!import org.jsfml.graphics.Color;
import org.jsfml.graphics.RectangleShape;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.system.Vector2f;
import org.jsfml.window.VideoMode;
import org.jsfml.window.event.Event;
public class GameOfLife {
private RenderWindow window;
private RectangleShape rectangle;
private final static int WIDTH = 64;
private final static int HEIGHT = 48;
private final static int SIZE = 10;
public GameOfLife() {
window = new RenderWindow(new VideoMode(640, 480), "Game of Life");
window.setFramerateLimit(60);
rectangle = new RectangleShape(new Vector2f(SIZE, SIZE));
rectangle.setOutlineThickness(1);
rectangle.setOutlineColor(Color.BLACK);
}
public void run() {
while (window.isOpen()) {
processEvents();
update();
render();
}
}
private void processEvents() {
for (Event event : window.pollEvents()) {
switch (event.type) {
case CLOSED:
window.close();
break;
}
}
}
private void update() {
}
private void render() {
window.clear();
for (int row = 0; row < HEIGHT; row++) {
for (int column = 0; column < WIDTH; column++) {
int x = column * SIZE;
int y = row * SIZE;
rectangle.setPosition(x, y);
window.draw(rectangle);
}
}
window.display();
}
public static void main(String[] args) {
(new GameOfLife()).run();
}
}
import org.jsfml.graphics.RectangleShape;
import org.jsfml.graphics.RenderWindow;
import org.jsfml.system.Vector2f;
import org.jsfml.window.VideoMode;
import org.jsfml.window.event.Event;
public class GameOfLife {
private RenderWindow window;
private RectangleShape rectangle;
private final static int WIDTH = 64;
private final static int HEIGHT = 48;
private final static int SIZE = 10;
public GameOfLife() {
window = new RenderWindow(new VideoMode(640, 480), "Game of Life");
window.setFramerateLimit(60);
rectangle = new RectangleShape(new Vector2f(SIZE, SIZE));
rectangle.setOutlineThickness(1);
rectangle.setOutlineColor(Color.BLACK);
}
public void run() {
while (window.isOpen()) {
processEvents();
update();
render();
}
}
private void processEvents() {
for (Event event : window.pollEvents()) {
switch (event.type) {
case CLOSED:
window.close();
break;
}
}
}
private void update() {
}
private void render() {
window.clear();
for (int row = 0; row < HEIGHT; row++) {
for (int column = 0; column < WIDTH; column++) {
int x = column * SIZE;
int y = row * SIZE;
rectangle.setPosition(x, y);
window.draw(rectangle);
}
}
window.display();
}
public static void main(String[] args) {
(new GameOfLife()).run();
}
}