Created
November 2, 2018 15:26
-
-
Save superblaubeere27/49fbb89a375d016b061f15390376534a to your computer and use it in GitHub Desktop.
Draw clock (LWJGL 2 + Slick 2D)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.superblaubeere27; | |
import org.lwjgl.LWJGLException; | |
import org.lwjgl.opengl.Display; | |
import org.lwjgl.opengl.DisplayMode; | |
import org.lwjgl.opengl.GL11; | |
import org.lwjgl.opengl.PixelFormat; | |
import org.newdawn.slick.TrueTypeFont; | |
import java.awt.*; | |
import java.sql.Time; | |
import java.text.SimpleDateFormat; | |
import java.time.LocalDateTime; | |
import java.time.LocalTime; | |
import java.util.Calendar; | |
import java.util.Date; | |
import static java.lang.Math.cos; | |
import static java.lang.StrictMath.sin; | |
import static org.lwjgl.opengl.GL11.glBegin; | |
public class Clock { | |
private static final double TRIANGLE_AMOUNT = 40; | |
private TrueTypeFont font; | |
private static final SimpleDateFormat TIME_FORMATTER = new SimpleDateFormat("HH:mm:ss"); | |
private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("dd/MM/yyyy"); | |
private void start() { | |
try { | |
Display.setDisplayMode(new DisplayMode(1280, 720)); | |
for (DisplayMode availableDisplayMode : Display.getAvailableDisplayModes()) { | |
if (availableDisplayMode.getWidth() == 1920 && availableDisplayMode.getHeight() == 1080 && availableDisplayMode.isFullscreenCapable()) { | |
Display.setDisplayMode(availableDisplayMode); | |
Display.setFullscreen(true); | |
break; | |
} | |
} | |
Display.setVSyncEnabled(true); | |
Display.setResizable(true); | |
Display.setTitle("Clock"); | |
Display.create(new PixelFormat(0, 8, 0, 16)); | |
} catch (LWJGLException e) { | |
e.printStackTrace(); | |
System.exit(-1); | |
} | |
// init OpenGL | |
initGL(); | |
init(); | |
while (!Display.isCloseRequested()) { | |
// Clear the screen and depth buffer | |
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); | |
render(); | |
Display.update(); | |
update(); | |
} | |
Display.destroy(); | |
} | |
private void initGL() { | |
GL11.glEnable(GL11.GL_TEXTURE_2D); | |
GL11.glShadeModel(GL11.GL_SMOOTH); | |
GL11.glDisable(GL11.GL_DEPTH_TEST); | |
GL11.glDisable(GL11.GL_LIGHTING); | |
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
GL11.glClearDepth(1); | |
GL11.glEnable(GL11.GL_BLEND); | |
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); | |
GL11.glViewport(0, 0, Display.getWidth(), Display.getHeight()); | |
GL11.glMatrixMode(GL11.GL_MODELVIEW); | |
GL11.glMatrixMode(GL11.GL_PROJECTION); | |
GL11.glLoadIdentity(); | |
GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1); | |
GL11.glMatrixMode(GL11.GL_MODELVIEW); | |
} | |
private void init() { | |
Font awtFont = new Font("Bebas Neue Bold", Font.PLAIN, 40); | |
font = new TrueTypeFont(awtFont, true); | |
} | |
private void update() { | |
} | |
private void render() { | |
GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); | |
GL11.glPushMatrix(); | |
double radius = Display.getHeight() * 0.39; | |
LocalDateTime now = LocalDateTime.now(); | |
GL11.glTranslated(Display.getWidth() / 2.0, Display.getHeight() / 2.0 - font.getHeight(), 0); | |
GL11.glEnable(GL11.GL_TEXTURE_2D); | |
Date today = Calendar.getInstance().getTime(); | |
String time = TIME_FORMATTER.format(today); | |
font.drawString(-font.getWidth(time) / 2.0f, (float) (radius + 5.0), time); | |
time = DATE_FORMATTER.format(today); | |
font.drawString(-font.getWidth(time) / 2.0f, (float) (radius + 5.0 + font.getHeight()), time); | |
GL11.glDisable(GL11.GL_TEXTURE_2D); | |
GL11.glColor4f(1.0f, 1.0f, 1.0f, 1.0f); | |
GL11.glLineWidth(1.0f); | |
drawCircle(0, 0, 8, true); | |
drawCircle(0, 0, radius, false); | |
double seconds = (now.getSecond() + System.currentTimeMillis() % 1000 / 1000.0); | |
double minutes = (now.getMinute() + seconds / 60.0); | |
double hours = (now.getHour() + minutes / 60.0); | |
GL11.glLineWidth(1.0f); | |
drawHand(seconds / 60.0 * 100.0, radius); | |
GL11.glLineWidth(4.0f); | |
drawHand(minutes / 60.0 * 100.0, radius * 0.75); | |
GL11.glLineWidth(8.0f); | |
drawHand(hours / 12.0 * 100.0, radius * 0.5); | |
GL11.glPopMatrix(); | |
} | |
private void drawHand(double percentage, double radius) { | |
double conv = convertPercentage(percentage); | |
GL11.glBegin(GL11.GL_LINES); | |
GL11.glVertex2d(0, 0); | |
GL11.glVertex2d(-radius * Math.sin(conv), radius * Math.cos(conv)); | |
GL11.glEnd(); | |
} | |
private double convertPercentage(double percentage) { | |
return percentage / 100.0 * Math.PI * 2.0 + Math.PI; | |
} | |
private static void drawCircle(double x, double y, double radius, boolean fill) { | |
int i; | |
double twicePi = 2.0 * Math.PI; | |
int triageAmount = Math.max((int) (radius * Math.PI * 2 / 15), 10); | |
glBegin(fill ? GL11.GL_TRIANGLE_FAN : GL11.GL_LINE_STRIP); | |
for (i = 0; i <= triageAmount; i++) { | |
GL11.glVertex2d( | |
x + (radius * cos(i * twicePi / triageAmount)), | |
y + (radius * sin(i * twicePi / triageAmount)) | |
); | |
} | |
GL11.glEnd(); | |
} | |
public static void main(String[] argv) { | |
Clock quadExample = new Clock(); | |
quadExample.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment