#include "bitmaps.h"
#define OLED_SDA GPIO_NUM_4
#define OLED_SCL GPIO_NUM_15
#define OLED_ADDR 0x3C
#define OLED_RESET GPIO_UNUSED
#define SPACESHIP_COUNT 4
#define ASTEROID_COUNT 2
#define OBJECTS_COUNT (SPACESHIP_COUNT + ASTEROID_COUNT + 1)
struct MySprite : Sprite {
int velX;
int velY;
bool isAlive;
bool isAsteroid;
};
struct MyScene : public Scene {
MySprite objects_[OBJECTS_COUNT];
MyScene()
: Scene(OBJECTS_COUNT, 20, DisplayController.getViewPortWidth(), DisplayController.getViewPortHeight())
{
}
void startSprite(MySprite * sprite)
{
int q = random(4);
int x = (q == 0 ? random(getWidth()) : (q == 1 ? getWidth() - sprite->getWidth() : (q == 2 ? random(getWidth()) : 0)));
int y = (q == 0 ? 0 : (q == 1 ? random(getHeight()) : (q == 2 ? getHeight() - sprite->getHeight() : random(getHeight()))));
sprite->moveTo(x, y);
sprite->visible = true;
sprite->velX = - sprite->getWidth() / 4 + random(sprite->getWidth() / 2);
sprite->velY = - sprite->getHeight() / 4 + random(sprite->getHeight() / 2);
sprite->isAlive = true;
}
void init()
{
paintSpace();
for (int i = 0; i < OBJECTS_COUNT; ++i) {
MySprite * sprite = &objects_[i];
if (i == 0) {
sprite->addBitmap(&jupiter);
sprite->moveTo(0, getHeight() / 2);
} else if (i <= SPACESHIP_COUNT) {
sprite->addBitmap(&spaceship)->addBitmap(explosion, 8);
sprite->isAsteroid = false;
addSprite(sprite);
startSprite(sprite);
} else {
sprite->addBitmap(&asteroid);
sprite->isAsteroid = true;
addSprite(sprite);
startSprite(sprite);
}
}
DisplayController.
setSprites(objects_, OBJECTS_COUNT);
}
void update(int updateCount)
{
for (int i = 1; i < OBJECTS_COUNT; ++i) {
MySprite * sprite = &objects_[i];
if (sprite->isAlive) {
updateSpriteAndDetectCollisions(sprite);
} else if ((updateCount % 8) == 0) {
sprite->nextFrame();
if (sprite->currentFrame == 0)
startSprite(sprite);
}
}
if ((updateCount % 20) == 0)
}
void collisionDetected(Sprite * spriteA, Sprite * spriteB, Point collisionPoint)
{
MySprite * a = (MySprite*) spriteA;
MySprite * b = (MySprite*) spriteB;
if (a->isAsteroid && b->isAsteroid)
return;
a->isAlive = b->isAlive = false;
if (a->isAsteroid)
a->visible = false;
else
a->nextFrame();
if (b->isAsteroid)
b->visible = false;
else
b->nextFrame();
}
void paintSpace()
{
Canvas cv(&DisplayController);
cv.clear();
cv.setPenColor(255, 255, 255);
for (int i = 0; i < 50; ++i)
cv.setPixel(random(getWidth()), random(getHeight()));
cv.drawBitmap(5, 5, &galaxy);
cv.drawBitmap(90, 35, &galaxy);
}
};
void setup()
{
Serial.begin(115200);
I2C.begin(OLED_SDA, OLED_SCL);
DisplayController.
begin(&I2C, OLED_ADDR, OLED_RESET);
while (DisplayController.
available() ==
false) {
Serial.write("Error, SSD1306 not available!\n");
delay(5000);
}
}
void loop()
{
MyScene scene;
scene.start();
vTaskSuspend(nullptr);
}
int getViewPortHeight()
Determines vertical size of the viewport.
int getViewPortWidth()
Determines horizontal size of the viewport.
void setSprites(T *sprites, int count)
Sets the list of active sprites.
void refreshSprites()
Forces the sprites to be updated.
I2C class allows multiple tasks to communicate with I2C devices, serializing read/write jobs.
void setResolution(char const *modeline, int viewPortWidth=-1, int viewPortHeight=-1, bool doubleBuffered=false)
Sets SSD1306 resolution and viewport size.
bool available()
Checks the SSD1306 device availability.
void begin(I2C *i2c, int address=0x3C, gpio_num_t resetGPIO=GPIO_UNUSED)
Initializes SSD1306 assigning I2C bus, reset pin and address.
Display driver for SSD1306 based OLED display, with I2C connection.
This file is the all in one include file. Application can just include this file to use FabGL library...