Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #38977 > unrolled thread
| Started by | Graeme Geldenhuys <graemeg@example.net> |
|---|---|
| First post | 2019-06-02 00:21 +0100 |
| Last post | 2019-06-05 17:53 +0100 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.java.programmer
Game color question, with max 256 colors Graeme Geldenhuys <graemeg@example.net> - 2019-06-02 00:21 +0100
Re: Game color question, with max 256 colors Andreas Leitgeb <avl@logic.at> - 2019-06-02 14:16 +0000
Re: Game color question, with max 256 colors Graeme Geldenhuys <graemeg@example.net> - 2019-06-05 17:53 +0100
| From | Graeme Geldenhuys <graemeg@example.net> |
|---|---|
| Date | 2019-06-02 00:21 +0100 |
| Subject | Game color question, with max 256 colors |
| Message-ID | <qcv1ae$1670$1@gioia.aioe.org> |
Hi,
I'm studying a game created during Ludum Dare 22 by Notch (The creator
of Minecraft). If you don't know, Ludum Dare is a competition where you
have to create a game from scratch in 48 hours.
Anyway, Notch purposely implemented a restricted "retro" color palette
where all sprites in the spritesheet is drawn using only 4 color (white,
black or 2 shades of gray). At runtime he can then manipulate the color
of the sprite so the same sprite can be drawn using different colors.
As for as I understand, he limited the game to 256 colors and each color
channel is 4-bits. I think - not 100% sure.
Anyway, he knocked up this class really fast and without commenting how
it works or what it does.
Is there anybody here that can figure this out?
public class Color {
public static int get(int a, int b, int c, int d) {
return (get(d) << 24) + (get(c) << 16) + (get(b) << 8) + (get(a));
}
public static int get(int d) {
if (d < 0) return 255;
int r = d / 100 % 10;
int g = d / 10 % 10;
int b = d % 10;
return r * 36 + g * 6 + b;
}
}
Usage of the above class is like this (actual snippet from the game code):
if (isSwimming()) {
yo += 4;
int waterColor = Color.get(-1, -1, 115, 335);
if (tickTime / 8 % 2 == 0) {
waterColor = Color.get(-1, 335, 5, 115);
}
screen.render(xo + 0, yo + 3, 5 + 13 * 32, waterColor, 0);
screen.render(xo + 8, yo + 3, 5 + 13 * 32, waterColor, 1);
}
if (attackTime > 0 && attackDir == 1) {
screen.render(xo + 0, yo - 4, 6 + 13 * 32, Color.get(-1, 555, 555,
555), 0);
screen.render(xo + 8, yo - 4, 6 + 13 * 32, Color.get(-1, 555, 555,
555), 1);
if (attackItem != null) {
attackItem.renderIcon(screen, xo + 4, yo - 4);
}
}
From all the code I looked at, it seems the max value for the b, c and d
parameters of Color.get() call is 555, and the lowest value is -1.
The main thing that is puzzling me, is the get(int d) method inside the
Color class. If anybody can shed some light on that, it would be greatly
appreciated.
If anybody was interested, the Ludum Dare 22 video (part 1 of 4) by
Notch is:
https://www.twitch.tv/videos/38122415
Regards,
Graeme
[toc] | [next] | [standalone]
| From | Andreas Leitgeb <avl@logic.at> |
|---|---|
| Date | 2019-06-02 14:16 +0000 |
| Message-ID | <slrnqf7mh6.cfl.avl@logic.at> |
| In reply to | #38977 |
Graeme Geldenhuys <graemeg@example.net> wrote:
> Is there anybody here that can figure this out?
> public class Color {
> public static int get(int a, int b, int c, int d) {
> return (get(d) << 24) + (get(c) << 16) + (get(b) << 8) + (get(a));
> }
This method encodes 4 unsigned 1-byte values in an int. Probably these
4 values are the 4 separate colors to be used for rendering the sprite.
Encoding 4 values into one means that the caller only needs to pass
one value for all four colors. In a serious application, one would use
a class to hold the four independent color attributes.
> public static int get(int d) {
> if (d < 0) return 255;
> int r = d / 100 % 10;
> int g = d / 10 % 10;
> int b = d % 10;
> return r * 36 + g * 6 + b;
> }
> }
This method converts values from a "fake base-6" representation
into a binary representation of a "6-shades of each red,green,blue
colorcube."
E.g.:
555: 5 red, 5 green, 5 blue - decimal value 5*36 + 5*6 + 5 = 215
432: 4 red, 3 green, 2 blue - decimal value 4*36 + 3*6 + 2 = 164
-1 is a special case here, and is encoded as 255, which likely
gets a special treatment lateron. Maybe it means transparent,
but I don't know, without looking up the relevant part of the
source to see how screen.render(...) unpacks and uses the
values. That the digits would mean red,green,blue in this order
is just a guess, based on how colors are usually specified.
> screen.render(... , Color.get(-1, 555, 555, 555), 0);
> https://www.twitch.tv/videos/38122415
[toc] | [prev] | [next] | [standalone]
| From | Graeme Geldenhuys <graemeg@example.net> |
|---|---|
| Date | 2019-06-05 17:53 +0100 |
| Message-ID | <qd8s1p$tno$1@gioia.aioe.org> |
| In reply to | #38979 |
Thanks Andreas. It seems your conclusion is correct in how the colors are defined and used. Many thanks. It's the "fake base-6 color representation" that has thrown me off the scent. :-) Regards, Graeme
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web