Skip to content

Commit

Permalink
Merge pull request #24 from Open-Smartwatch/develop
Browse files Browse the repository at this point in the history
Merge develop
  • Loading branch information
RuffaloLavoisier authored Jul 20, 2022
2 parents 31ed3d6 + f2a95e6 commit 022a851
Show file tree
Hide file tree
Showing 7 changed files with 257 additions and 17 deletions.
93 changes: 85 additions & 8 deletions anim_doom_fire.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,99 @@
#ifndef P3DT_ANIM_DOOM_FIRE_H
#define P3DT_ANIM_DOOM_FIRE_H
#pragma once

#ifdef FAKE_ARDUINO
#include "FakeArduino.h"
#else
#include <Arduino.h>
#endif

#include "gfx_2d.h"
//#include "gfx_2d.h"
#include "gfx_2d_print.h"
#include "gfx_util.h"
// see: https://p3dt.net/post/2019/01/05/playing-with-doom.html

class AnimDoomFire{
public:
AnimDoomFire() {
for (int i = 0; i < 240; i++) {
firePixels[i] = new uint8_t[240];
}
setupFire(firePixels, 240, 240);
}

void setupFire(uint8_t **firePixels, uint16_t w, uint16_t h);
const uint16_t doomColorMap[36] = {
//
rgb565(0x00, 0x00, 0x00), // #000000
rgb565(0x1f, 0x07, 0x07), // #1f0707
rgb565(0x2f, 0x0f, 0x07), // #2f0f07
rgb565(0x47, 0x0f, 0x07), // #470f07
rgb565(0x57, 0x17, 0x07), // #571707
rgb565(0x67, 0x1f, 0x07), // #671f07
rgb565(0x77, 0x1f, 0x07), // #771f07
rgb565(0x8f, 0x27, 0x07), // #8f2707
rgb565(0x9f, 0x2f, 0x07), // #9f2f07
rgb565(0xaf, 0x3f, 0x07), // #af3f07
rgb565(0xbf, 0x47, 0x07), // #bf4707
rgb565(0xc7, 0x47, 0x07), // #c74707
rgb565(0xDF, 0x4F, 0x07), // #DF4F07
rgb565(0xDF, 0x57, 0x07), // #DF5707
rgb565(0xDF, 0x57, 0x07), // #DF5707
rgb565(0xD7, 0x5F, 0x07), // #D75F07
rgb565(0xD7, 0x67, 0x0F), // #D7670F
rgb565(0xcf, 0x6f, 0x0f), // #cf6f0f
rgb565(0xcf, 0x77, 0x0f), // #cf770f
rgb565(0xcf, 0x7f, 0x0f), // #cf7f0f
rgb565(0xCF, 0x87, 0x17), // #CF8717
rgb565(0xC7, 0x87, 0x17), // #C78717
rgb565(0xC7, 0x8F, 0x17), // #C78F17
rgb565(0xC7, 0x97, 0x1F), // #C7971F
rgb565(0xBF, 0x9F, 0x1F), // #BF9F1F
rgb565(0xBF, 0x9F, 0x1F), // #BF9F1F
rgb565(0xBF, 0xA7, 0x27), // #BFA727
rgb565(0xBF, 0xA7, 0x27), // #BFA727
rgb565(0xBF, 0xAF, 0x2F), // #BFAF2F
rgb565(0xB7, 0xAF, 0x2F), // #B7AF2F
rgb565(0xB7, 0xB7, 0x2F), // #B7B72F
rgb565(0xB7, 0xB7, 0x37), // #B7B737
rgb565(0xCF, 0xCF, 0x6F), // #CFCF6F
rgb565(0xDF, 0xDF, 0x9F), // #DFDF9F
rgb565(0xEF, 0xEF, 0xC7), // #EFEFC7
rgb565(0xFF, 0xFF, 0xFF) // #FFFFFF
};

void calcFire(uint8_t **firePixels, uint16_t w, uint16_t h);
void setupFire(uint8_t **firePixels, uint16_t w, uint16_t h) {
for (uint8_t y = 0; y < h; y++) {
for (uint8_t x = 0; x < w; x++) {
// last row is hot
firePixels[y][x] = y == h - 1 ? 35 : 0;
}
}
}

void mapFire(uint8_t **firePixels, uint16_t fireW, uint16_t fireH, //
Graphics2D *graphics2d, uint16_t offsetX, uint16_t offsetY);
void calcFire(uint8_t **firePixels, uint16_t w, uint16_t h,float* X ,float* Y) {
for (uint8_t y = 0; y < h - 1; y++) {
for (uint8_t x = 0; x < w; x++) {
uint8_t wind = x + random(2) + (Y == nullptr ? 0 : (abs(*Y) / 15000));
wind = wind >= w ? wind - w : wind;
uint8_t speed = y + random(2) + (X == nullptr ? 0:(abs(*X) / 15000));
speed = speed >= h ? h - 1 : speed;
firePixels[y][x] = firePixels[speed][wind] - random(2);
firePixels[y][x] = firePixels[y][x] > 35 ? 0 : firePixels[y][x]; // fix overflow
}
}
}

#endif
void mapFire(uint8_t **firePixels, uint16_t fireW, uint16_t fireH, //
Graphics2D *graphics2d, uint16_t offsetX, uint16_t offsetY) {
for (uint8_t x = 0; x < fireW; x++) {
for (uint8_t y = 0; y < fireH; y++) {
graphics2d->drawPixel(x + offsetX, y + offsetY, doomColorMap[firePixels[y][x]]);
}
}
}

void loop(Graphics2DPrint *gfx,float* X = nullptr, float* Y = nullptr) {
calcFire(firePixels, 240, 240,X,Y);
mapFire(firePixels, 240, 240, gfx, 0, 0);
}
uint8_t **firePixels = new uint8_t *[240];
};
76 changes: 76 additions & 0 deletions anim_doom_fire_old.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "anim_doom_fire_old.h"

#include "gfx_2d.h"
#include "gfx_util.h"
// see: https://p3dt.net/post/2019/01/05/playing-with-doom.html

const uint16_t doomColorMap[36] = {
//
rgb565(0x00, 0x00, 0x00), // #000000
rgb565(0x1f, 0x07, 0x07), // #1f0707
rgb565(0x2f, 0x0f, 0x07), // #2f0f07
rgb565(0x47, 0x0f, 0x07), // #470f07
rgb565(0x57, 0x17, 0x07), // #571707
rgb565(0x67, 0x1f, 0x07), // #671f07
rgb565(0x77, 0x1f, 0x07), // #771f07
rgb565(0x8f, 0x27, 0x07), // #8f2707
rgb565(0x9f, 0x2f, 0x07), // #9f2f07
rgb565(0xaf, 0x3f, 0x07), // #af3f07
rgb565(0xbf, 0x47, 0x07), // #bf4707
rgb565(0xc7, 0x47, 0x07), // #c74707
rgb565(0xDF, 0x4F, 0x07), // #DF4F07
rgb565(0xDF, 0x57, 0x07), // #DF5707
rgb565(0xDF, 0x57, 0x07), // #DF5707
rgb565(0xD7, 0x5F, 0x07), // #D75F07
rgb565(0xD7, 0x67, 0x0F), // #D7670F
rgb565(0xcf, 0x6f, 0x0f), // #cf6f0f
rgb565(0xcf, 0x77, 0x0f), // #cf770f
rgb565(0xcf, 0x7f, 0x0f), // #cf7f0f
rgb565(0xCF, 0x87, 0x17), // #CF8717
rgb565(0xC7, 0x87, 0x17), // #C78717
rgb565(0xC7, 0x8F, 0x17), // #C78F17
rgb565(0xC7, 0x97, 0x1F), // #C7971F
rgb565(0xBF, 0x9F, 0x1F), // #BF9F1F
rgb565(0xBF, 0x9F, 0x1F), // #BF9F1F
rgb565(0xBF, 0xA7, 0x27), // #BFA727
rgb565(0xBF, 0xA7, 0x27), // #BFA727
rgb565(0xBF, 0xAF, 0x2F), // #BFAF2F
rgb565(0xB7, 0xAF, 0x2F), // #B7AF2F
rgb565(0xB7, 0xB7, 0x2F), // #B7B72F
rgb565(0xB7, 0xB7, 0x37), // #B7B737
rgb565(0xCF, 0xCF, 0x6F), // #CFCF6F
rgb565(0xDF, 0xDF, 0x9F), // #DFDF9F
rgb565(0xEF, 0xEF, 0xC7), // #EFEFC7
rgb565(0xFF, 0xFF, 0xFF) // #FFFFFF
};

void setupFire(uint8_t **firePixels, uint16_t w, uint16_t h) {
for (uint8_t y = 0; y < h; y++) {
for (uint8_t x = 0; x < w; x++) {
// last row is hot
firePixels[y][x] = y == h - 1 ? 35 : 0;
}
}
}

void calcFire(uint8_t **firePixels, uint16_t w, uint16_t h) {
for (uint8_t y = 0; y < h - 1; y++) {
for (uint8_t x = 0; x < w; x++) {
uint8_t wind = x + random(2);
wind = wind >= w ? wind - w : wind;
uint8_t speed = y + random(2);
speed = speed >= h ? h - 1 : speed;
firePixels[y][x] = firePixels[speed][wind] - random(2);
firePixels[y][x] = firePixels[y][x] > 35 ? 0 : firePixels[y][x]; // fix overflow
}
}
}

void mapFire(uint8_t **firePixels, uint16_t fireW, uint16_t fireH, //
Graphics2D *graphics2d, uint16_t offsetX, uint16_t offsetY) {
for (uint8_t x = 0; x < fireW; x++) {
for (uint8_t y = 0; y < fireH; y++) {
graphics2d->drawPixel(x + offsetX, y + offsetY, doomColorMap[firePixels[y][x]]);
}
}
}
19 changes: 19 additions & 0 deletions anim_doom_fire_old.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once
#ifdef FAKE_ARDUINO
#include "FakeArduino.h"
#else
#include <Arduino.h>
#endif

#include "gfx_2d.h"
#include "gfx_util.h"
// see: https://p3dt.net/post/2019/01/05/playing-with-doom.html


void setupFire(uint8_t **firePixels, uint16_t w, uint16_t h);

void calcFire(uint8_t **firePixels, uint16_t w, uint16_t h);

void mapFire(uint8_t **firePixels, uint16_t fireW, uint16_t fireH, //
Graphics2D *graphics2d, uint16_t offsetX, uint16_t offsetY);

4 changes: 4 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ add_executable(Perlin perlin/main.cpp)
target_include_directories(Perlin PUBLIC ${EXAMPLE_INCLUDES})
target_link_libraries(Perlin PUBLIC ${EXAMPLE_LIBRARIES})

add_executable(DoomFireOld doom-fire-old/main.cpp)
target_include_directories(DoomFireOld PUBLIC ${EXAMPLE_INCLUDES})
target_link_libraries(DoomFireOld PUBLIC ${EXAMPLE_LIBRARIES})

add_executable(Rotation rotation/main.cpp)
target_include_directories(Rotation PUBLIC ${EXAMPLE_INCLUDES})
target_link_libraries(Rotation PUBLIC ${EXAMPLE_LIBRARIES})
Expand Down
39 changes: 39 additions & 0 deletions examples/doom-fire-old/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

#include <stdint.h>

#include <iostream>

#include "../../FakeArduino.h"
#include "../../FakeArduinoWindowSDL.h"
#include "../../anim_doom_fire_old.h"
#include "../../gfx_util.h"
using namespace std;

#define BUF_W 240
#define BUF_H 240
uint8_t** firePixels = new uint8_t*[BUF_H];
Graphics2D gfx2d(BUF_W, BUF_H,120);

class FireWindow : public SDLWindowRGB565 {
public:
FireWindow(Graphics2D* gfx2d, int w, int h) : SDLWindowRGB565(gfx2d, w, h) {}
void setup() {
// setup array rows
for (int i = 0; i < BUF_H; i++) {
firePixels[i] = new uint8_t[BUF_W];
}
setupFire(firePixels, BUF_W, BUF_H);
}

void loop() {
delay(1000 / 30);
calcFire(firePixels, BUF_W, BUF_H);
mapFire(firePixels, BUF_W, BUF_H, &gfx2d, 0, 0);
}
};

int main(int argc, char* argv[]) {
FireWindow fw(&gfx2d, BUF_W, BUF_H);
fw.run();
return 0;
}
19 changes: 10 additions & 9 deletions examples/doom-fire/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ using namespace std;

#define BUF_W 240
#define BUF_H 240
uint8_t** firePixels = new uint8_t*[BUF_H];
Graphics2D gfx2d(BUF_W, BUF_H,120);

Graphics2DPrint gfx2d(BUF_W, BUF_H, 120);
AnimDoomFire d;
class FireWindow : public SDLWindowRGB565 {
public:
FireWindow(Graphics2D* gfx2d, int w, int h) : SDLWindowRGB565(gfx2d, w, h) {}
void setup() {
// setup array rows
for (int i = 0; i < BUF_H; i++) {
firePixels[i] = new uint8_t[BUF_W];
}
setupFire(firePixels, BUF_W, BUF_H);

}

void loop() {
Graphics2DPrint* gfx = &gfx2d;

gfx->fill(rgb565(0, 0, 0));

// render animation
d.loop(gfx);

delay(1000 / 30);
calcFire(firePixels, BUF_W, BUF_H);
mapFire(firePixels, BUF_W, BUF_H, &gfx2d, 0, 0);
}
};

Expand Down
24 changes: 24 additions & 0 deletions gfx_2d_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,30 @@ class Graphics2DPrint : public Graphics2D, public Print {
}
print(yourNumber);
}
/**
@param str String parameter you nedd 'foo' -> E.g char foo[] = "index";
@param pos Nth count string
@param directionHead string cut head
*/
void printSlice(char *str, int pos, bool directionHead = false) {
uint8_t len = strlen(str);

if (len > abs(pos)) {
if(directionHead){
if (pos > 0)
str[pos] = 0; // 3 : (Hell)o, World -> Hell
else
str = str - pos; // -3 : Hel(lo, World) -> lo, World
} else {
if (pos > 0)
str = str + (len - pos); // 3 : Hello, Wo(rld) -> rld
else
str[len - abs(pos)] = '\0'; // -3 : (Hello, Wo)rld -> Hello, Wo
}
}
print(str);
}

// set alignment options
void setTextCenterAligned() { text_x_alignment = _text_alignment::CENTER; }
Expand Down

0 comments on commit 022a851

Please sign in to comment.