Created
April 13, 2012 21:42
-
-
Save notjosh/2380345 to your computer and use it in GitHub Desktop.
JPod ticker (Cinder)
This file contains 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
#include "cinder/app/AppBasic.h" | |
#include "cinder/gl/gl.h" | |
using namespace ci; | |
using namespace ci::app; | |
using namespace std; | |
#define NUMBER_OF_COLUMNS 12 | |
#define NUMBER_OF_ROWS 6 | |
const int kTileSize = 100; | |
struct Tile { | |
int backgroundColour; | |
char character; | |
int characterColour; | |
double expiryTime; | |
}; | |
enum kTilePaletteColour { | |
kTilePaletteColour1, | |
kTilePaletteColour2, | |
kTilePaletteColour3, | |
kTilePaletteColour4, | |
kTilePaletteColour5, | |
kTilePaletteNumberOfColours | |
}; | |
static int kTilePalette[] = { | |
0x00A0B0, | |
0x6A4A3C, | |
0xCC333F, | |
0xEB6841, | |
0xEDC951 | |
}; | |
class JPodCinderApp : public AppBasic { | |
public: | |
void setup(); | |
void prepareSettings(Settings *settings); | |
void update(); | |
void draw(); | |
private: | |
void configureTile(Tile *tile); | |
Color rgb2Color(int rgb); | |
vector<char> chars; | |
Tile *tiles[NUMBER_OF_COLUMNS][NUMBER_OF_ROWS]; | |
}; | |
void JPodCinderApp::prepareSettings(Settings *settings) { | |
settings->setWindowSize(1200, 600); | |
settings->setFrameRate(60.0f); | |
} | |
void JPodCinderApp::setup() { | |
for (char i = '0'; i <= '9'; i++) { | |
chars.push_back(i); | |
} | |
for (char i = 'A'; i <= 'Z'; i++) { | |
chars.push_back(i); | |
} | |
for (char i = 'a'; i <= 'z'; i++) { | |
chars.push_back(i); | |
} | |
cout << "chars stores " << (int)chars.size() << " numbers.\n"; | |
for (int i = 0; i < NUMBER_OF_ROWS; i++) { | |
for (int j = 0; j < NUMBER_OF_COLUMNS; j++) { | |
Tile *tile = new Tile(); | |
configureTile(tile); | |
tiles[i][j] = tile; | |
} | |
} | |
// environment | |
gl::enableAlphaBlending(); | |
} | |
void JPodCinderApp::update() { | |
for (int i = 0; i < NUMBER_OF_ROWS; i++) { | |
for (int j = 0; j < NUMBER_OF_COLUMNS; j++) { | |
Tile *tile = tiles[i][j]; | |
if (getElapsedSeconds() > tile->expiryTime) { | |
configureTile(tile); | |
} | |
} | |
} | |
} | |
void JPodCinderApp::draw() { | |
for (int i = 0; i < NUMBER_OF_ROWS; i++) { | |
for (int j = 0; j < NUMBER_OF_COLUMNS; j++) { | |
Tile *tile = tiles[i][j]; | |
Rectf rect = Rectf(j * kTileSize, i * kTileSize, j * kTileSize + kTileSize, i * kTileSize + kTileSize); | |
gl::color(rgb2Color(tile->backgroundColour)); | |
gl::drawSolidRect(rect); | |
gl::drawStringCentered( | |
string(1, tile->character), | |
Vec2f(j * kTileSize + (kTileSize / 2.0f), i * kTileSize + 30.0f), | |
rgb2Color(tile->characterColour), | |
Font("Helvetica", 80.0f) | |
); | |
} | |
} | |
} | |
void JPodCinderApp::configureTile(Tile *tile) { | |
int tileBackgroundColourIndex = rand() % kTilePaletteNumberOfColours; | |
int tileCharacterColourIndex = rand() % kTilePaletteNumberOfColours; | |
// ensure background != foreground | |
while ( tileBackgroundColourIndex == tileCharacterColourIndex | |
&& /* sanity checking */ kTilePaletteNumberOfColours > 1) { | |
tileCharacterColourIndex = rand() % kTilePaletteNumberOfColours; | |
} | |
tile->backgroundColour = kTilePalette[tileBackgroundColourIndex]; | |
tile->characterColour = kTilePalette[tileCharacterColourIndex]; | |
tile->character = chars[rand() % chars.size()]; | |
tile->expiryTime = (int)getElapsedSeconds() + (rand() % 12); | |
} | |
Color JPodCinderApp::rgb2Color(int rgb) { | |
int r = ((rgb >> 16) & 0xFF); | |
int g = ((rgb >> 8) & 0xFF); | |
int b = (rgb & 0xFF); | |
return Color(r / 255.0f, g / 255.0f, b / 255.0f); | |
} | |
CINDER_APP_BASIC( JPodCinderApp, RendererGl ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment